blob: c2becbe5dec4331a53f13bd332cb85379a4ec8b5 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200117// Destination for an assignment or ":unlet" with an index.
118typedef enum {
119 dest_local,
120 dest_option,
121 dest_env,
122 dest_global,
123 dest_buffer,
124 dest_window,
125 dest_tab,
126 dest_vimvar,
127 dest_script,
128 dest_reg,
129 dest_expr,
130} assign_dest_T;
131
132// Used by compile_lhs() to store information about the LHS of an assignment
133// and one argument of ":unlet" with an index.
134typedef struct {
135 assign_dest_T lhs_dest; // type of destination
136
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200137 char_u *lhs_name; // allocated name excluding the last
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200138 // "[expr]" or ".name".
139 size_t lhs_varlen; // length of the variable without
140 // "[expr]" or ".name"
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200141 char_u *lhs_whole; // allocated name including the last
142 // "[expr]" or ".name" for :redir
143 size_t lhs_varlen_total; // length of the variable including
144 // any "[expr]" or ".name"
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200145 char_u *lhs_dest_end; // end of the destination, including
146 // "[expr]" or ".name".
147
148 int lhs_has_index; // has "[expr]" or ".name"
149
150 int lhs_new_local; // create new local variable
151 int lhs_opt_flags; // for when destination is an option
152 int lhs_vimvaridx; // for when destination is a v:var
153
154 lvar_T lhs_local_lvar; // used for existing local destination
155 lvar_T lhs_arg_lvar; // used for argument destination
156 lvar_T *lhs_lvar; // points to destination lvar
157 int lhs_scriptvar_sid;
158 int lhs_scriptvar_idx;
159
160 int lhs_has_type; // type was specified
161 type_T *lhs_type;
162 type_T *lhs_member_type;
163
164 int lhs_append; // used by ISN_REDIREND
165} lhs_T;
166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167/*
168 * Context for compiling lines of Vim script.
169 * Stores info about the local variables and condition stack.
170 */
171struct cctx_S {
172 ufunc_T *ctx_ufunc; // current function
173 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200174 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 garray_T ctx_instr; // generated instructions
176
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200177 int ctx_prev_lnum; // line number below previous command, for
178 // debugging
179
Bram Moolenaare99d4222021-06-13 14:01:26 +0200180 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200184 int ctx_has_closure; // set to one if a closures was created in
185 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100187 garray_T ctx_imports; // imported items
188
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200189 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200191 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193 cctx_T *ctx_outer; // outer scope for lambda or nested
194 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200198 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200199
Bram Moolenaar02194d22020-10-24 23:08:38 +0200200 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200201
202 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
203 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204};
205
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100206static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207
208/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100209 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100210 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100211 * If "lvar" is NULL only check if the variable can be found.
212 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100214 static int
215lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100218 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100221 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200222
223 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
225 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100226 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
227 if (STRNCMP(name, lvp->lv_name, len) == 0
228 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200229 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100230 if (lvar != NULL)
231 {
232 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100233 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100234 }
235 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200238
239 // Find local in outer function scope.
240 if (cctx->ctx_outer != NULL)
241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100242 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200243 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100244 if (lvar != NULL)
245 {
246 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100247 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100248 }
249 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200250 }
251 }
252
Bram Moolenaar709664c2020-12-12 14:33:41 +0100253 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254}
255
256/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200257 * Lookup an argument in the current function and an enclosing function.
258 * Returns the argument index in "idxp"
259 * Returns the argument type in "type"
260 * Sets "gen_load_outer" to TRUE if found in outer scope.
261 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 */
263 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200265 char_u *name,
266 size_t len,
267 int *idxp,
268 type_T **type,
269 int *gen_load_outer,
270 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271{
272 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200273 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100275 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200276 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200277 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
279 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
280
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200281 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
282 {
283 if (idxp != NULL)
284 {
285 // Arguments are located above the frame pointer. One further
286 // if there is a vararg argument
287 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
288 + STACK_FRAME_SIZE)
289 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
290
291 if (cctx->ctx_ufunc->uf_arg_types != NULL)
292 *type = cctx->ctx_ufunc->uf_arg_types[idx];
293 else
294 *type = &t_any;
295 }
296 return OK;
297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200300 va_name = cctx->ctx_ufunc->uf_va_name;
301 if (va_name != NULL
302 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
303 {
304 if (idxp != NULL)
305 {
306 // varargs is always the last argument
307 *idxp = -STACK_FRAME_SIZE - 1;
308 *type = cctx->ctx_ufunc->uf_va_type;
309 }
310 return OK;
311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200313 if (cctx->ctx_outer != NULL)
314 {
315 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200316 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200317 == OK)
318 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100319 if (gen_load_outer != NULL)
320 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200321 return OK;
322 }
323 }
324
325 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326}
327
328/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329 * Lookup a script-local variable in the current script, possibly defined in a
330 * block that contains the function "cctx->ctx_ufunc".
331 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100332 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * Return NULL when not found.
334 */
335 static sallvar_T *
336find_script_var(char_u *name, size_t len, cctx_T *cctx)
337{
338 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
339 hashitem_T *hi;
340 int cc;
341 sallvar_T *sav;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200342 sallvar_T *found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 ufunc_T *ufunc;
344
345 // Find the list of all script variables with the right name.
346 if (len > 0)
347 {
348 cc = name[len];
349 name[len] = NUL;
350 }
351 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
352 if (len > 0)
353 name[len] = cc;
354 if (HASHITEM_EMPTY(hi))
355 return NULL;
356
357 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200358 if (sav->sav_block_id == 0)
359 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200360 return sav;
361
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200362 if (cctx == NULL)
363 {
364 // Not in a function scope, find variable with block id equal to or
365 // smaller than the current block id.
366 while (sav != NULL)
367 {
368 if (sav->sav_block_id <= si->sn_current_block_id)
369 break;
370 sav = sav->sav_next;
371 }
372 return sav;
373 }
374
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200375 // Go over the variables with this name and find one that was visible
376 // from the function.
377 ufunc = cctx->ctx_ufunc;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200378 found_sav = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200379 while (sav != NULL)
380 {
381 int idx;
382
383 // Go over the blocks that this function was defined in. If the
384 // variable block ID matches it was visible to the function.
385 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
386 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
387 return sav;
388 sav = sav->sav_next;
389 }
390
Bram Moolenaar88421d62021-07-24 14:14:52 +0200391 // Not found, assume variable at script level was visible.
392 return found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200393}
394
395/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100396 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200397 */
398 static int
399script_is_vim9()
400{
401 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
402}
403
404/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200405 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200406 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407 * Returns OK or FAIL.
408 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200409 static int
410script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200412 if (current_sctx.sc_sid <= 0)
413 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200414 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200415 {
416 // Check script variables that were visible where the function was
417 // defined.
418 if (find_script_var(name, len, cctx) != NULL)
419 return OK;
420 }
421 else
422 {
423 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
424 dictitem_T *di;
425 int cc;
426
427 // Check script variables that are currently visible
428 cc = name[len];
429 name[len] = NUL;
430 di = find_var_in_ht(ht, 0, name, TRUE);
431 name[len] = cc;
432 if (di != NULL)
433 return OK;
434 }
435
436 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437}
438
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100439/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100440 * Return TRUE if "name" is a local variable, argument, script variable or
441 * imported.
442 */
443 static int
444variable_exists(char_u *name, size_t len, cctx_T *cctx)
445{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100446 return (cctx != NULL
447 && (lookup_local(name, len, NULL, cctx) == OK
448 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200449 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100450 || find_imported(name, len, cctx) != NULL;
451}
452
453/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100454 * Return TRUE if "name" is a local variable, argument, script variable,
455 * imported or function.
456 */
457 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100458item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100459{
460 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100461 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100462
463 if (variable_exists(name, len, cctx))
464 return TRUE;
465
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100466 // This is similar to what is in lookup_scriptitem():
467 // Find a function, so that a following "->" works.
468 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
469 // a function call.
470 p = skipwhite(name + len);
471
472 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
473 {
474 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200475 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100476 // Skip "g:" before a function name.
477 is_global = (name[0] == 'g' && name[1] == ':');
478 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
479 }
480 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100481}
482
483/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100484 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200485 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200486 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100487 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100488 * Return FAIL and give an error if it defined.
489 */
490 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100491check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100492{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200493 int c = p[len];
494 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200495
Bram Moolenaarda479c72021-04-10 21:01:38 +0200496 // underscore argument is OK
497 if (len == 1 && *p == '_')
498 return OK;
499
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200500 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100501 {
502 if (is_arg)
503 semsg(_(e_argument_already_declared_in_script_str), p);
504 else
505 semsg(_(e_variable_already_declared_in_script_str), p);
506 return FAIL;
507 }
508
Bram Moolenaarad486a02020-08-01 23:22:18 +0200509 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100510 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100511 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200512 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200513 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200514 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100515 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200516 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200517 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
518 && (!func_is_global(ufunc)
519 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200520 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100521 if (is_arg)
522 semsg(_(e_argument_name_shadows_existing_variable_str), p);
523 else
524 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200525 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200526 return FAIL;
527 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100528 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200529 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100530 return OK;
531}
532
Bram Moolenaar65b95452020-07-19 14:03:09 +0200533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534/////////////////////////////////////////////////////////////////////
535// Following generate_ functions expect the caller to call ga_grow().
536
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200537#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
538#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100540/*
541 * Generate an instruction without arguments.
542 * Returns a pointer to the new instruction, NULL if failed.
543 */
544 static isn_T *
545generate_instr(cctx_T *cctx, isntype_T isn_type)
546{
547 garray_T *instr = &cctx->ctx_instr;
548 isn_T *isn;
549
Bram Moolenaar080457c2020-03-03 21:53:32 +0100550 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar35578162021-08-02 19:10:38 +0200551 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 return NULL;
553 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
554 isn->isn_type = isn_type;
555 isn->isn_lnum = cctx->ctx_lnum + 1;
556 ++instr->ga_len;
557
558 return isn;
559}
560
561/*
562 * Generate an instruction without arguments.
563 * "drop" will be removed from the stack.
564 * Returns a pointer to the new instruction, NULL if failed.
565 */
566 static isn_T *
567generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
568{
569 garray_T *stack = &cctx->ctx_type_stack;
570
Bram Moolenaar080457c2020-03-03 21:53:32 +0100571 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 stack->ga_len -= drop;
573 return generate_instr(cctx, isn_type);
574}
575
576/*
577 * Generate instruction "isn_type" and put "type" on the type stack.
578 */
579 static isn_T *
580generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
581{
582 isn_T *isn;
583 garray_T *stack = &cctx->ctx_type_stack;
584
585 if ((isn = generate_instr(cctx, isn_type)) == NULL)
586 return NULL;
587
Bram Moolenaar35578162021-08-02 19:10:38 +0200588 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200590 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 ++stack->ga_len;
592
593 return isn;
594}
595
596/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200597 * Generate an ISN_DEBUG instruction.
598 */
599 static isn_T *
600generate_instr_debug(cctx_T *cctx)
601{
602 isn_T *isn;
603 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
604 + cctx->ctx_ufunc->uf_dfunc_idx;
605
606 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
607 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200608 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
609 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200610 return isn;
611}
612
613/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200615 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200616 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 */
618 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200619may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620{
621 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200622 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100624 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100626 RETURN_OK_IF_SKIP(cctx);
627 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200628 switch ((*type)->tt_type)
629 {
630 // nothing to be done
631 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200633 // conversion possible
634 case VAR_SPECIAL:
635 case VAR_BOOL:
636 case VAR_NUMBER:
637 case VAR_FLOAT:
638 break;
639
640 // conversion possible (with runtime check)
641 case VAR_ANY:
642 case VAR_UNKNOWN:
643 isntype = ISN_2STRING_ANY;
644 break;
645
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200646 // conversion possible when tolerant
647 case VAR_LIST:
648 if (tolerant)
649 {
650 isntype = ISN_2STRING_ANY;
651 break;
652 }
653 // FALLTHROUGH
654
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200655 // conversion not possible
656 case VAR_VOID:
657 case VAR_BLOB:
658 case VAR_FUNC:
659 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200660 case VAR_DICT:
661 case VAR_JOB:
662 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200663 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200664 to_string_error((*type)->tt_type);
665 return FAIL;
666 }
667
668 *type = &t_string;
669 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200671 isn->isn_arg.tostring.offset = offset;
672 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673
674 return OK;
675}
676
677 static int
678check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
679{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200680 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 {
684 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200685 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200687 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 return FAIL;
689 }
690 return OK;
691}
692
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200693 static int
694generate_add_instr(
695 cctx_T *cctx,
696 vartype_T vartype,
697 type_T *type1,
698 type_T *type2)
699{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100700 garray_T *stack = &cctx->ctx_type_stack;
701 isn_T *isn = generate_instr_drop(cctx,
702 vartype == VAR_NUMBER ? ISN_OPNR
703 : vartype == VAR_LIST ? ISN_ADDLIST
704 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200705#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100706 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200707#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100708 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200709
710 if (vartype != VAR_LIST && vartype != VAR_BLOB
711 && type1->tt_type != VAR_ANY
712 && type2->tt_type != VAR_ANY
713 && check_number_or_float(
714 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
715 return FAIL;
716
717 if (isn != NULL)
718 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100719
720 // When concatenating two lists with different member types the member type
721 // becomes "any".
722 if (vartype == VAR_LIST
723 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
724 && type1->tt_member != type2->tt_member)
725 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
726
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200727 return isn == NULL ? FAIL : OK;
728}
729
730/*
731 * Get the type to use for an instruction for an operation on "type1" and
732 * "type2". If they are matching use a type-specific instruction. Otherwise
733 * fall back to runtime type checking.
734 */
735 static vartype_T
736operator_type(type_T *type1, type_T *type2)
737{
738 if (type1->tt_type == type2->tt_type
739 && (type1->tt_type == VAR_NUMBER
740 || type1->tt_type == VAR_LIST
741#ifdef FEAT_FLOAT
742 || type1->tt_type == VAR_FLOAT
743#endif
744 || type1->tt_type == VAR_BLOB))
745 return type1->tt_type;
746 return VAR_ANY;
747}
748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749/*
750 * Generate an instruction with two arguments. The instruction depends on the
751 * type of the arguments.
752 */
753 static int
754generate_two_op(cctx_T *cctx, char_u *op)
755{
756 garray_T *stack = &cctx->ctx_type_stack;
757 type_T *type1;
758 type_T *type2;
759 vartype_T vartype;
760 isn_T *isn;
761
Bram Moolenaar080457c2020-03-03 21:53:32 +0100762 RETURN_OK_IF_SKIP(cctx);
763
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200764 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
766 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200767 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768
769 switch (*op)
770 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200771 case '+':
772 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 break;
775
776 case '-':
777 case '*':
778 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
779 op) == FAIL)
780 return FAIL;
781 if (vartype == VAR_NUMBER)
782 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
783#ifdef FEAT_FLOAT
784 else if (vartype == VAR_FLOAT)
785 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
786#endif
787 else
788 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
789 if (isn != NULL)
790 isn->isn_arg.op.op_type = *op == '*'
791 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
792 break;
793
Bram Moolenaar4c683752020-04-05 21:38:23 +0200794 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200796 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100797 && type2->tt_type != VAR_NUMBER))
798 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200799 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 return FAIL;
801 }
802 isn = generate_instr_drop(cctx,
803 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
804 if (isn != NULL)
805 isn->isn_arg.op.op_type = EXPR_REM;
806 break;
807 }
808
809 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200810 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 {
812 type_T *type = &t_any;
813
814#ifdef FEAT_FLOAT
815 // float+number and number+float results in float
816 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
817 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
818 type = &t_float;
819#endif
820 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
821 }
822
823 return OK;
824}
825
826/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200827 * Get the instruction to use for comparing "type1" with "type2"
828 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200830 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100831get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832{
833 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834
Bram Moolenaar4c683752020-04-05 21:38:23 +0200835 if (type1 == VAR_UNKNOWN)
836 type1 = VAR_ANY;
837 if (type2 == VAR_UNKNOWN)
838 type2 = VAR_ANY;
839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 if (type1 == type2)
841 {
842 switch (type1)
843 {
844 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
845 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
846 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
847 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
848 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
849 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
850 case VAR_LIST: isntype = ISN_COMPARELIST; break;
851 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
852 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 default: isntype = ISN_COMPAREANY; break;
854 }
855 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200856 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100858 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 isntype = ISN_COMPAREANY;
860
Bram Moolenaar657137c2021-01-09 15:45:23 +0100861 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 && (isntype == ISN_COMPAREBOOL
863 || isntype == ISN_COMPARESPECIAL
864 || isntype == ISN_COMPARENR
865 || isntype == ISN_COMPAREFLOAT))
866 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200867 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100868 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200869 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 }
871 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100872 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
874 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100875 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
876 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 && (type1 == VAR_BLOB || type2 == VAR_BLOB
878 || type1 == VAR_LIST || type2 == VAR_LIST))))
879 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200880 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200882 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200884 return isntype;
885}
886
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200887 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100888check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200889{
890 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
891 return FAIL;
892 return OK;
893}
894
Bram Moolenaara5565e42020-05-09 15:44:01 +0200895/*
896 * Generate an ISN_COMPARE* instruction with a boolean result.
897 */
898 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100899generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200900{
901 isntype_T isntype;
902 isn_T *isn;
903 garray_T *stack = &cctx->ctx_type_stack;
904 vartype_T type1;
905 vartype_T type2;
906
907 RETURN_OK_IF_SKIP(cctx);
908
909 // Get the known type of the two items on the stack. If they are matching
910 // use a type-specific instruction. Otherwise fall back to runtime type
911 // checking.
912 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
913 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100914 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200915 if (isntype == ISN_DROP)
916 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917
918 if ((isn = generate_instr(cctx, isntype)) == NULL)
919 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100920 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921 isn->isn_arg.op.op_ic = ic;
922
923 // takes two arguments, puts one bool back
924 if (stack->ga_len >= 2)
925 {
926 --stack->ga_len;
927 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
928 }
929
930 return OK;
931}
932
933/*
934 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200935 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 */
937 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200938generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939{
940 isn_T *isn;
941 garray_T *stack = &cctx->ctx_type_stack;
942
Bram Moolenaar080457c2020-03-03 21:53:32 +0100943 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
945 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200946 isn->isn_arg.tobool.invert = invert;
947 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948
949 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200950 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951
952 return OK;
953}
954
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200955/*
956 * Generate an ISN_COND2BOOL instruction.
957 */
958 static int
959generate_COND2BOOL(cctx_T *cctx)
960{
961 isn_T *isn;
962 garray_T *stack = &cctx->ctx_type_stack;
963
964 RETURN_OK_IF_SKIP(cctx);
965 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
966 return FAIL;
967
968 // type becomes bool
969 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
970
971 return OK;
972}
973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200975generate_TYPECHECK(
976 cctx_T *cctx,
977 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100978 int offset,
979 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980{
981 isn_T *isn;
982 garray_T *stack = &cctx->ctx_type_stack;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
986 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200987 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100988 isn->isn_arg.type.ct_off = (int8_T)offset;
989 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990
Bram Moolenaar5e654232020-09-16 15:22:00 +0200991 // type becomes expected
992 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993
994 return OK;
995}
996
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100997 static int
998generate_SETTYPE(
999 cctx_T *cctx,
1000 type_T *expected)
1001{
1002 isn_T *isn;
1003
1004 RETURN_OK_IF_SKIP(cctx);
1005 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
1006 return FAIL;
1007 isn->isn_arg.type.ct_type = alloc_type(expected);
1008 return OK;
1009}
1010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001012 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
1013 * used. Return FALSE if the types will never match.
1014 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02001015 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001016use_typecheck(type_T *actual, type_T *expected)
1017{
1018 if (actual->tt_type == VAR_ANY
1019 || actual->tt_type == VAR_UNKNOWN
1020 || (actual->tt_type == VAR_FUNC
1021 && (expected->tt_type == VAR_FUNC
1022 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001023 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1024 && ((actual->tt_member == &t_void)
1025 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001026 return TRUE;
1027 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1028 && actual->tt_type == expected->tt_type)
1029 // This takes care of a nested list or dict.
1030 return use_typecheck(actual->tt_member, expected->tt_member);
1031 return FALSE;
1032}
1033
1034/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001035 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001036 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001037 * - "actual" is a type that can be "expected" type: add a runtime check; or
1038 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001039 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1040 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001041 */
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001042 static int
1043need_type_where(
1044 type_T *actual,
1045 type_T *expected,
1046 int offset,
1047 where_T where,
1048 cctx_T *cctx,
1049 int silent,
1050 int actual_is_const)
1051{
1052 if (expected == &t_bool && actual != &t_bool
1053 && (actual->tt_flags & TTFLAG_BOOL_OK))
1054 {
1055 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1056 // boolean is OK but requires a conversion.
1057 generate_2BOOL(cctx, FALSE, offset);
1058 return OK;
1059 }
1060
1061 if (check_type(expected, actual, FALSE, where) == OK)
1062 return OK;
1063
1064 // If the actual type can be the expected type add a runtime check.
1065 // If it's a constant a runtime check makes no sense.
1066 if ((!actual_is_const || actual == &t_any)
1067 && use_typecheck(actual, expected))
1068 {
1069 generate_TYPECHECK(cctx, expected, offset, where.wt_index);
1070 return OK;
1071 }
1072
1073 if (!silent)
1074 type_mismatch_where(expected, actual, where);
1075 return FAIL;
1076}
1077
Bram Moolenaar351ead02021-01-16 16:07:01 +01001078 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001079need_type(
1080 type_T *actual,
1081 type_T *expected,
1082 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001083 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001084 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001085 int silent,
1086 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001087{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001088 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001089
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001090 where.wt_index = arg_idx;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001091 return need_type_where(actual, expected, offset, where,
1092 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001093}
1094
1095/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001096 * Check that the top of the type stack has a type that can be used as a
1097 * condition. Give an error and return FAIL if not.
1098 */
1099 static int
1100bool_on_stack(cctx_T *cctx)
1101{
1102 garray_T *stack = &cctx->ctx_type_stack;
1103 type_T *type;
1104
1105 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1106 if (type == &t_bool)
1107 return OK;
1108
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001109 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001110 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1111 // This requires a runtime type check.
1112 return generate_COND2BOOL(cctx);
1113
Bram Moolenaar351ead02021-01-16 16:07:01 +01001114 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001115}
1116
1117/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 * Generate an ISN_PUSHNR instruction.
1119 */
1120 static int
1121generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1122{
1123 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001124 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125
Bram Moolenaar080457c2020-03-03 21:53:32 +01001126 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1128 return FAIL;
1129 isn->isn_arg.number = number;
1130
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001131 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001132 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001133 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 return OK;
1135}
1136
1137/*
1138 * Generate an ISN_PUSHBOOL instruction.
1139 */
1140 static int
1141generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1142{
1143 isn_T *isn;
1144
Bram Moolenaar080457c2020-03-03 21:53:32 +01001145 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1147 return FAIL;
1148 isn->isn_arg.number = number;
1149
1150 return OK;
1151}
1152
1153/*
1154 * Generate an ISN_PUSHSPEC instruction.
1155 */
1156 static int
1157generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1158{
1159 isn_T *isn;
1160
Bram Moolenaar080457c2020-03-03 21:53:32 +01001161 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1163 return FAIL;
1164 isn->isn_arg.number = number;
1165
1166 return OK;
1167}
1168
1169#ifdef FEAT_FLOAT
1170/*
1171 * Generate an ISN_PUSHF instruction.
1172 */
1173 static int
1174generate_PUSHF(cctx_T *cctx, float_T fnumber)
1175{
1176 isn_T *isn;
1177
Bram Moolenaar080457c2020-03-03 21:53:32 +01001178 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1180 return FAIL;
1181 isn->isn_arg.fnumber = fnumber;
1182
1183 return OK;
1184}
1185#endif
1186
1187/*
1188 * Generate an ISN_PUSHS instruction.
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001189 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 */
1191 static int
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001192generate_PUSHS(cctx_T *cctx, char_u **str)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193{
1194 isn_T *isn;
1195
Bram Moolenaar508b5612021-01-02 18:17:26 +01001196 if (cctx->ctx_skip == SKIP_YES)
1197 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001198 if (str != NULL)
1199 VIM_CLEAR(*str);
Bram Moolenaar508b5612021-01-02 18:17:26 +01001200 return OK;
1201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001203 {
1204 if (str != NULL)
1205 VIM_CLEAR(*str);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001207 }
1208 isn->isn_arg.string = str == NULL ? NULL : *str;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209
1210 return OK;
1211}
1212
1213/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001214 * Generate an ISN_PUSHCHANNEL instruction.
1215 * Consumes "channel".
1216 */
1217 static int
1218generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1219{
1220 isn_T *isn;
1221
Bram Moolenaar080457c2020-03-03 21:53:32 +01001222 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001223 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1224 return FAIL;
1225 isn->isn_arg.channel = channel;
1226
1227 return OK;
1228}
1229
1230/*
1231 * Generate an ISN_PUSHJOB instruction.
1232 * Consumes "job".
1233 */
1234 static int
1235generate_PUSHJOB(cctx_T *cctx, job_T *job)
1236{
1237 isn_T *isn;
1238
Bram Moolenaar080457c2020-03-03 21:53:32 +01001239 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001240 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001241 return FAIL;
1242 isn->isn_arg.job = job;
1243
1244 return OK;
1245}
1246
1247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 * Generate an ISN_PUSHBLOB instruction.
1249 * Consumes "blob".
1250 */
1251 static int
1252generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1253{
1254 isn_T *isn;
1255
Bram Moolenaar080457c2020-03-03 21:53:32 +01001256 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1258 return FAIL;
1259 isn->isn_arg.blob = blob;
1260
1261 return OK;
1262}
1263
1264/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001265 * Generate an ISN_PUSHFUNC instruction with name "name".
1266 * Consumes "name".
1267 */
1268 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001269generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001270{
1271 isn_T *isn;
1272
Bram Moolenaar080457c2020-03-03 21:53:32 +01001273 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001274 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001275 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001276 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001277
1278 return OK;
1279}
1280
1281/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001282 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001283 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1284 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001285 */
1286 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001287generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001288{
1289 isn_T *isn;
1290 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001291 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1292 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001293 type_T *item_type = &t_any;
1294
1295 RETURN_OK_IF_SKIP(cctx);
1296
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001297 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001298 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001299 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001300 emsg(_(e_listreq));
1301 return FAIL;
1302 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001303 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001304 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1305 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001306 isn->isn_arg.getitem.gi_index = index;
1307 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001308
1309 // add the item type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001310 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001311 return FAIL;
1312 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1313 ++stack->ga_len;
1314 return OK;
1315}
1316
1317/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001318 * Generate an ISN_SLICE instruction with "count".
1319 */
1320 static int
1321generate_SLICE(cctx_T *cctx, int count)
1322{
1323 isn_T *isn;
1324
1325 RETURN_OK_IF_SKIP(cctx);
1326 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1327 return FAIL;
1328 isn->isn_arg.number = count;
1329 return OK;
1330}
1331
1332/*
1333 * Generate an ISN_CHECKLEN instruction with "min_len".
1334 */
1335 static int
1336generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1337{
1338 isn_T *isn;
1339
1340 RETURN_OK_IF_SKIP(cctx);
1341
1342 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1343 return FAIL;
1344 isn->isn_arg.checklen.cl_min_len = min_len;
1345 isn->isn_arg.checklen.cl_more_OK = more_OK;
1346
1347 return OK;
1348}
1349
1350/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 * Generate an ISN_STORE instruction.
1352 */
1353 static int
1354generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1355{
1356 isn_T *isn;
1357
Bram Moolenaar080457c2020-03-03 21:53:32 +01001358 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1360 return FAIL;
1361 if (name != NULL)
1362 isn->isn_arg.string = vim_strsave(name);
1363 else
1364 isn->isn_arg.number = idx;
1365
1366 return OK;
1367}
1368
1369/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001370 * Generate an ISN_STOREOUTER instruction.
1371 */
1372 static int
1373generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1374{
1375 isn_T *isn;
1376
1377 RETURN_OK_IF_SKIP(cctx);
1378 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1379 return FAIL;
1380 isn->isn_arg.outer.outer_idx = idx;
1381 isn->isn_arg.outer.outer_depth = level;
1382
1383 return OK;
1384}
1385
1386/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1388 */
1389 static int
1390generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1391{
1392 isn_T *isn;
1393
Bram Moolenaar080457c2020-03-03 21:53:32 +01001394 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1396 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001397 isn->isn_arg.storenr.stnr_idx = idx;
1398 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399
1400 return OK;
1401}
1402
1403/*
1404 * Generate an ISN_STOREOPT instruction
1405 */
1406 static int
1407generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1408{
1409 isn_T *isn;
1410
Bram Moolenaar080457c2020-03-03 21:53:32 +01001411 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001412 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 return FAIL;
1414 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1415 isn->isn_arg.storeopt.so_flags = opt_flags;
1416
1417 return OK;
1418}
1419
1420/*
1421 * Generate an ISN_LOAD or similar instruction.
1422 */
1423 static int
1424generate_LOAD(
1425 cctx_T *cctx,
1426 isntype_T isn_type,
1427 int idx,
1428 char_u *name,
1429 type_T *type)
1430{
1431 isn_T *isn;
1432
Bram Moolenaar080457c2020-03-03 21:53:32 +01001433 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1435 return FAIL;
1436 if (name != NULL)
1437 isn->isn_arg.string = vim_strsave(name);
1438 else
1439 isn->isn_arg.number = idx;
1440
1441 return OK;
1442}
1443
1444/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001445 * Generate an ISN_LOADOUTER instruction
1446 */
1447 static int
1448generate_LOADOUTER(
1449 cctx_T *cctx,
1450 int idx,
1451 int nesting,
1452 type_T *type)
1453{
1454 isn_T *isn;
1455
1456 RETURN_OK_IF_SKIP(cctx);
1457 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1458 return FAIL;
1459 isn->isn_arg.outer.outer_idx = idx;
1460 isn->isn_arg.outer.outer_depth = nesting;
1461
1462 return OK;
1463}
1464
1465/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001466 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001467 */
1468 static int
1469generate_LOADV(
1470 cctx_T *cctx,
1471 char_u *name,
1472 int error)
1473{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001474 int di_flags;
1475 int vidx = find_vim_var(name, &di_flags);
1476 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001477
Bram Moolenaar080457c2020-03-03 21:53:32 +01001478 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001479 if (vidx < 0)
1480 {
1481 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001482 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001483 return FAIL;
1484 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001485 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001486
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001487 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001488}
1489
1490/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001491 * Generate an ISN_UNLET instruction.
1492 */
1493 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001494generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001495{
1496 isn_T *isn;
1497
1498 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001499 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001500 return FAIL;
1501 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1502 isn->isn_arg.unlet.ul_forceit = forceit;
1503
1504 return OK;
1505}
1506
1507/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001508 * Generate an ISN_LOCKCONST instruction.
1509 */
1510 static int
1511generate_LOCKCONST(cctx_T *cctx)
1512{
1513 isn_T *isn;
1514
1515 RETURN_OK_IF_SKIP(cctx);
1516 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1517 return FAIL;
1518 return OK;
1519}
1520
1521/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 * Generate an ISN_LOADS instruction.
1523 */
1524 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001525generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001527 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001529 int sid,
1530 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531{
1532 isn_T *isn;
1533
Bram Moolenaar080457c2020-03-03 21:53:32 +01001534 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001535 if (isn_type == ISN_LOADS)
1536 isn = generate_instr_type(cctx, isn_type, type);
1537 else
1538 isn = generate_instr_drop(cctx, isn_type, 1);
1539 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001541 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1542 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543
1544 return OK;
1545}
1546
1547/*
1548 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1549 */
1550 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001551generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 cctx_T *cctx,
1553 isntype_T isn_type,
1554 int sid,
1555 int idx,
1556 type_T *type)
1557{
1558 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001559 scriptref_T *sref;
1560 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561
Bram Moolenaar080457c2020-03-03 21:53:32 +01001562 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 if (isn_type == ISN_LOADSCRIPT)
1564 isn = generate_instr_type(cctx, isn_type, type);
1565 else
1566 isn = generate_instr_drop(cctx, isn_type, 1);
1567 if (isn == NULL)
1568 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001569
1570 // This requires three arguments, which doesn't fit in an instruction, thus
1571 // we need to allocate a struct for this.
1572 sref = ALLOC_ONE(scriptref_T);
1573 if (sref == NULL)
1574 return FAIL;
1575 isn->isn_arg.script.scriptref = sref;
1576 sref->sref_sid = sid;
1577 sref->sref_idx = idx;
1578 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001579 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 return OK;
1581}
1582
1583/*
1584 * Generate an ISN_NEWLIST instruction.
1585 */
1586 static int
1587generate_NEWLIST(cctx_T *cctx, int count)
1588{
1589 isn_T *isn;
1590 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 type_T *type;
1592 type_T *member;
1593
Bram Moolenaar080457c2020-03-03 21:53:32 +01001594 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1596 return FAIL;
1597 isn->isn_arg.number = count;
1598
Bram Moolenaar127542b2020-08-09 17:22:04 +02001599 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001600 if (count == 0)
Bram Moolenaar6e48b842021-08-10 22:52:02 +02001601 member = &t_unknown;
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001602 else
1603 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001604 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1605 cctx->ctx_type_list);
1606 type = get_list_type(member, cctx->ctx_type_list);
1607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 // drop the value types
1609 stack->ga_len -= count;
1610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 // add the list type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001612 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 return FAIL;
1614 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1615 ++stack->ga_len;
1616
1617 return OK;
1618}
1619
1620/*
1621 * Generate an ISN_NEWDICT instruction.
1622 */
1623 static int
1624generate_NEWDICT(cctx_T *cctx, int count)
1625{
1626 isn_T *isn;
1627 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 type_T *type;
1629 type_T *member;
1630
Bram Moolenaar080457c2020-03-03 21:53:32 +01001631 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1633 return FAIL;
1634 isn->isn_arg.number = count;
1635
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001636 if (count == 0)
1637 member = &t_void;
1638 else
1639 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001640 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1641 cctx->ctx_type_list);
1642 type = get_dict_type(member, cctx->ctx_type_list);
1643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 // drop the key and value types
1645 stack->ga_len -= 2 * count;
1646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 // add the dict type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001648 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 return FAIL;
1650 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1651 ++stack->ga_len;
1652
1653 return OK;
1654}
1655
1656/*
1657 * Generate an ISN_FUNCREF instruction.
1658 */
1659 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001660generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661{
1662 isn_T *isn;
1663 garray_T *stack = &cctx->ctx_type_stack;
1664
Bram Moolenaar080457c2020-03-03 21:53:32 +01001665 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1667 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001668 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001669 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001671 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001672 // the nested context, including this one.
1673 if (ufunc->uf_flags & FC_CLOSURE)
1674 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1675
Bram Moolenaar35578162021-08-02 19:10:38 +02001676 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001678 ((type_T **)stack->ga_data)[stack->ga_len] =
1679 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 ++stack->ga_len;
1681
1682 return OK;
1683}
1684
1685/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001686 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001687 * "lambda_name" and "func_name" must be in allocated memory and will be
1688 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001689 */
1690 static int
1691generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1692{
1693 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001694
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001695 if (cctx->ctx_skip == SKIP_YES)
1696 {
1697 vim_free(lambda_name);
1698 vim_free(func_name);
1699 return OK;
1700 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001701 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001702 {
1703 vim_free(lambda_name);
1704 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001705 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001706 }
1707 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001708 isn->isn_arg.newfunc.nf_global = func_name;
1709
1710 return OK;
1711}
1712
1713/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001714 * Generate an ISN_DEF instruction: list functions
1715 */
1716 static int
1717generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1718{
1719 isn_T *isn;
1720
1721 RETURN_OK_IF_SKIP(cctx);
1722 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1723 return FAIL;
1724 if (len > 0)
1725 {
1726 isn->isn_arg.string = vim_strnsave(name, len);
1727 if (isn->isn_arg.string == NULL)
1728 return FAIL;
1729 }
1730 return OK;
1731}
1732
1733/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 * Generate an ISN_JUMP instruction.
1735 */
1736 static int
1737generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1738{
1739 isn_T *isn;
1740 garray_T *stack = &cctx->ctx_type_stack;
1741
Bram Moolenaar080457c2020-03-03 21:53:32 +01001742 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1744 return FAIL;
1745 isn->isn_arg.jump.jump_when = when;
1746 isn->isn_arg.jump.jump_where = where;
1747
1748 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1749 --stack->ga_len;
1750
1751 return OK;
1752}
1753
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001754/*
1755 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1756 */
1757 static int
1758generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1759{
1760 isn_T *isn;
1761
1762 RETURN_OK_IF_SKIP(cctx);
1763 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1764 return FAIL;
1765 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1766 // jump_where is set later
1767 return OK;
1768}
1769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 static int
1771generate_FOR(cctx_T *cctx, int loop_idx)
1772{
1773 isn_T *isn;
1774 garray_T *stack = &cctx->ctx_type_stack;
1775
Bram Moolenaar080457c2020-03-03 21:53:32 +01001776 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1778 return FAIL;
1779 isn->isn_arg.forloop.for_idx = loop_idx;
1780
Bram Moolenaar35578162021-08-02 19:10:38 +02001781 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 return FAIL;
1783 // type doesn't matter, will be stored next
1784 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1785 ++stack->ga_len;
1786
1787 return OK;
1788}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001789/*
1790 * Generate an ISN_TRYCONT instruction.
1791 */
1792 static int
1793generate_TRYCONT(cctx_T *cctx, int levels, int where)
1794{
1795 isn_T *isn;
1796
1797 RETURN_OK_IF_SKIP(cctx);
1798 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1799 return FAIL;
1800 isn->isn_arg.trycont.tct_levels = levels;
1801 isn->isn_arg.trycont.tct_where = where;
1802
1803 return OK;
1804}
1805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806
1807/*
1808 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001809 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 * Return FAIL if the number of arguments is wrong.
1811 */
1812 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001813generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814{
1815 isn_T *isn;
1816 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001817 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001818 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001819 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001820 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821
Bram Moolenaar080457c2020-03-03 21:53:32 +01001822 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001823 argoff = check_internal_func(func_idx, argcount);
1824 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 return FAIL;
1826
Bram Moolenaar389df252020-07-09 21:20:47 +02001827 if (method_call && argoff > 1)
1828 {
1829 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1830 return FAIL;
1831 isn->isn_arg.shuffle.shfl_item = argcount;
1832 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1833 }
1834
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001835 if (argcount > 0)
1836 {
1837 // Check the types of the arguments.
1838 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001839 if (method_call && argoff > 1)
1840 {
1841 int i;
1842
1843 for (i = 0; i < argcount; ++i)
1844 shuffled_argtypes[i] = (i < argoff - 1)
1845 ? argtypes[i + 1]
1846 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1847 argtypes = shuffled_argtypes;
1848 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001849 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1850 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001851 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001852 if (internal_func_is_map(func_idx))
1853 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001854 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1857 return FAIL;
1858 isn->isn_arg.bfunc.cbf_idx = func_idx;
1859 isn->isn_arg.bfunc.cbf_argcount = argcount;
1860
Bram Moolenaar94738d82020-10-21 14:25:07 +02001861 // Drop the argument types and push the return type.
1862 stack->ga_len -= argcount;
Bram Moolenaar35578162021-08-02 19:10:38 +02001863 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 return FAIL;
1865 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001866 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001867 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001869 if (maptype != NULL && maptype->tt_member != NULL
1870 && maptype->tt_member != &t_any)
1871 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001872 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 return OK;
1875}
1876
1877/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001878 * Generate an ISN_LISTAPPEND instruction. Works like add().
1879 * Argument count is already checked.
1880 */
1881 static int
1882generate_LISTAPPEND(cctx_T *cctx)
1883{
1884 garray_T *stack = &cctx->ctx_type_stack;
1885 type_T *list_type;
1886 type_T *item_type;
1887 type_T *expected;
1888
1889 // Caller already checked that list_type is a list.
1890 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1891 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1892 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001893 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001894 return FAIL;
1895
1896 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1897 return FAIL;
1898
1899 --stack->ga_len; // drop the argument
1900 return OK;
1901}
1902
1903/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001904 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1905 * Argument count is already checked.
1906 */
1907 static int
1908generate_BLOBAPPEND(cctx_T *cctx)
1909{
1910 garray_T *stack = &cctx->ctx_type_stack;
1911 type_T *item_type;
1912
1913 // Caller already checked that blob_type is a blob.
1914 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001915 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001916 return FAIL;
1917
1918 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1919 return FAIL;
1920
1921 --stack->ga_len; // drop the argument
1922 return OK;
1923}
1924
1925/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001926 * Return TRUE if "ufunc" should be compiled, taking into account whether
1927 * "profile" indicates profiling is to be done.
1928 */
1929 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001930func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001931{
1932 switch (ufunc->uf_def_status)
1933 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001934 case UF_TO_BE_COMPILED:
1935 return TRUE;
1936
Bram Moolenaarb2049902021-01-24 12:53:53 +01001937 case UF_COMPILED:
1938 {
1939 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1940 + ufunc->uf_dfunc_idx;
1941
Bram Moolenaare99d4222021-06-13 14:01:26 +02001942 switch (compile_type)
1943 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001944 case CT_PROFILE:
1945#ifdef FEAT_PROFILE
1946 return dfunc->df_instr_prof == NULL;
1947#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001948 case CT_NONE:
1949 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001950 case CT_DEBUG:
1951 return dfunc->df_instr_debug == NULL;
1952 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001953 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001954
1955 case UF_NOT_COMPILED:
1956 case UF_COMPILE_ERROR:
1957 case UF_COMPILING:
1958 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001959 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001960 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001961}
1962
1963/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 * Generate an ISN_DCALL or ISN_UCALL instruction.
1965 * Return FAIL if the number of arguments is wrong.
1966 */
1967 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001968generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969{
1970 isn_T *isn;
1971 garray_T *stack = &cctx->ctx_type_stack;
1972 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001973 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974
Bram Moolenaar080457c2020-03-03 21:53:32 +01001975 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 if (argcount > regular_args && !has_varargs(ufunc))
1977 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001978 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 return FAIL;
1980 }
1981 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1982 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001983 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 return FAIL;
1985 }
1986
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001987 if (ufunc->uf_def_status != UF_NOT_COMPILED
1988 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001989 {
1990 int i;
1991
1992 for (i = 0; i < argcount; ++i)
1993 {
1994 type_T *expected;
1995 type_T *actual;
1996
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001997 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1998 if (actual == &t_special
1999 && i >= regular_args - ufunc->uf_def_args.ga_len)
2000 {
2001 // assume v:none used for default argument value
2002 continue;
2003 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002004 if (i < regular_args)
2005 {
2006 if (ufunc->uf_arg_types == NULL)
2007 continue;
2008 expected = ufunc->uf_arg_types[i];
2009 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02002010 else if (ufunc->uf_va_type == NULL
2011 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02002012 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02002013 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002014 else
2015 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01002016 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002017 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002018 {
2019 arg_type_mismatch(expected, actual, i + 1);
2020 return FAIL;
2021 }
2022 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002023 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002024 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002025 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002026 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002027 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002028 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2029 {
2030 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2031 ufunc->uf_name);
2032 return FAIL;
2033 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002036 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002037 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002039 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 {
2041 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2042 isn->isn_arg.dfunc.cdf_argcount = argcount;
2043 }
2044 else
2045 {
2046 // A user function may be deleted and redefined later, can't use the
2047 // ufunc pointer, need to look it up again at runtime.
2048 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2049 isn->isn_arg.ufunc.cuf_argcount = argcount;
2050 }
2051
2052 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002053 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 return FAIL;
2055 // add return value
2056 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2057 ++stack->ga_len;
2058
2059 return OK;
2060}
2061
2062/*
2063 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2064 */
2065 static int
2066generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2067{
2068 isn_T *isn;
2069 garray_T *stack = &cctx->ctx_type_stack;
2070
Bram Moolenaar080457c2020-03-03 21:53:32 +01002071 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2073 return FAIL;
2074 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2075 isn->isn_arg.ufunc.cuf_argcount = argcount;
2076
2077 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002078 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002079 return FAIL;
2080 // add return value
2081 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2082 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083
2084 return OK;
2085}
2086
2087/*
2088 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002089 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 */
2091 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002092generate_PCALL(
2093 cctx_T *cctx,
2094 int argcount,
2095 char_u *name,
2096 type_T *type,
2097 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098{
2099 isn_T *isn;
2100 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002101 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102
Bram Moolenaar080457c2020-03-03 21:53:32 +01002103 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002104
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002105 if (type->tt_type == VAR_ANY)
2106 ret_type = &t_any;
2107 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002108 {
2109 if (type->tt_argcount != -1)
2110 {
2111 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2112
2113 if (argcount < type->tt_min_argcount - varargs)
2114 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002115 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002116 return FAIL;
2117 }
2118 if (!varargs && argcount > type->tt_argcount)
2119 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002120 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002121 return FAIL;
2122 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002123 if (type->tt_args != NULL)
2124 {
2125 int i;
2126
2127 for (i = 0; i < argcount; ++i)
2128 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002129 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002130 type_T *actual = ((type_T **)stack->ga_data)[
2131 stack->ga_len + offset];
2132 type_T *expected;
2133
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002134 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002135 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002136 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002137 else if (i >= type->tt_min_argcount
2138 && actual == &t_special)
2139 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002140 else
2141 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002142 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002143 cctx, TRUE, FALSE) == FAIL)
2144 {
2145 arg_type_mismatch(expected, actual, i + 1);
2146 return FAIL;
2147 }
2148 }
2149 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002150 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002151 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002152 if (ret_type == &t_unknown)
2153 // return type not known yet, use a runtime check
2154 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002155 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002156 else
2157 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002158 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002159 return FAIL;
2160 }
2161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2163 return FAIL;
2164 isn->isn_arg.pfunc.cpf_top = at_top;
2165 isn->isn_arg.pfunc.cpf_argcount = argcount;
2166
2167 stack->ga_len -= argcount; // drop the arguments
2168
2169 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002170 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002172 // If partial is above the arguments it must be cleared and replaced with
2173 // the return value.
2174 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2175 return FAIL;
2176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 return OK;
2178}
2179
2180/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002181 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 */
2183 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002184generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185{
2186 isn_T *isn;
2187 garray_T *stack = &cctx->ctx_type_stack;
2188 type_T *type;
2189
Bram Moolenaar080457c2020-03-03 21:53:32 +01002190 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002191 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002193 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002195 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002197 if (type->tt_type != VAR_DICT && type != &t_any)
2198 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002199 char *tofree;
2200
2201 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2202 name, type_name(type, &tofree));
2203 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002204 return FAIL;
2205 }
2206 // change dict type to dict member type
2207 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002208 {
2209 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2210 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212
2213 return OK;
2214}
2215
2216/*
2217 * Generate an ISN_ECHO instruction.
2218 */
2219 static int
2220generate_ECHO(cctx_T *cctx, int with_white, int count)
2221{
2222 isn_T *isn;
2223
Bram Moolenaar080457c2020-03-03 21:53:32 +01002224 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2226 return FAIL;
2227 isn->isn_arg.echo.echo_with_white = with_white;
2228 isn->isn_arg.echo.echo_count = count;
2229
2230 return OK;
2231}
2232
Bram Moolenaarad39c092020-02-26 18:23:43 +01002233/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002234 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002235 */
2236 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002237generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002238{
2239 isn_T *isn;
2240
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002241 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002242 return FAIL;
2243 isn->isn_arg.number = count;
2244
2245 return OK;
2246}
2247
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002248/*
2249 * Generate an ISN_PUT instruction.
2250 */
2251 static int
2252generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2253{
2254 isn_T *isn;
2255
2256 RETURN_OK_IF_SKIP(cctx);
2257 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2258 return FAIL;
2259 isn->isn_arg.put.put_regname = regname;
2260 isn->isn_arg.put.put_lnum = lnum;
2261 return OK;
2262}
2263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 static int
2265generate_EXEC(cctx_T *cctx, char_u *line)
2266{
2267 isn_T *isn;
2268
Bram Moolenaar080457c2020-03-03 21:53:32 +01002269 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2271 return FAIL;
2272 isn->isn_arg.string = vim_strsave(line);
2273 return OK;
2274}
2275
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002276 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002277generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2278{
2279 isn_T *isn;
2280 garray_T *stack = &cctx->ctx_type_stack;
2281
2282 RETURN_OK_IF_SKIP(cctx);
2283 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2284 return FAIL;
2285 isn->isn_arg.string = vim_strsave(line);
2286
Bram Moolenaar35578162021-08-02 19:10:38 +02002287 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002288 return FAIL;
2289 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2290 ++stack->ga_len;
2291
2292 return OK;
2293}
2294
2295 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002296generate_EXECCONCAT(cctx_T *cctx, int count)
2297{
2298 isn_T *isn;
2299
2300 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2301 return FAIL;
2302 isn->isn_arg.number = count;
2303 return OK;
2304}
2305
Bram Moolenaar08597872020-12-10 19:43:40 +01002306/*
2307 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2308 */
2309 static int
2310generate_RANGE(cctx_T *cctx, char_u *range)
2311{
2312 isn_T *isn;
2313 garray_T *stack = &cctx->ctx_type_stack;
2314
2315 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2316 return FAIL;
2317 isn->isn_arg.string = range;
2318
Bram Moolenaar35578162021-08-02 19:10:38 +02002319 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002320 return FAIL;
2321 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2322 ++stack->ga_len;
2323 return OK;
2324}
2325
Bram Moolenaar792f7862020-11-23 08:31:18 +01002326 static int
2327generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2328{
2329 isn_T *isn;
2330
2331 RETURN_OK_IF_SKIP(cctx);
2332 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2333 return FAIL;
2334 isn->isn_arg.unpack.unp_count = var_count;
2335 isn->isn_arg.unpack.unp_semicolon = semicolon;
2336 return OK;
2337}
2338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002340 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002341 */
2342 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002343generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002344{
2345 isn_T *isn;
2346
Bram Moolenaar917c46a2021-08-10 19:53:01 +02002347 if (has_cmdmod(cmod, FALSE))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002348 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002349 cctx->ctx_has_cmdmod = TRUE;
2350
2351 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002352 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002353 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2354 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2355 return FAIL;
2356 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002357 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002358 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002359 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002360
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002361 return OK;
2362}
2363
2364 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002365generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002366{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002367 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2368 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002369 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002370 return OK;
2371}
2372
Bram Moolenaarfa984412021-03-25 22:15:28 +01002373 static int
2374misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002375{
2376 garray_T *instr = &cctx->ctx_instr;
2377
Bram Moolenaara91a7132021-03-25 21:12:15 +01002378 if (cctx->ctx_has_cmdmod
2379 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2380 == ISN_CMDMOD)
2381 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002382 emsg(_(e_misplaced_command_modifier));
2383 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002384 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002385 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002386}
2387
2388/*
2389 * Get the index of the current instruction.
2390 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2391 */
2392 static int
2393current_instr_idx(cctx_T *cctx)
2394{
2395 garray_T *instr = &cctx->ctx_instr;
2396 int idx = instr->ga_len;
2397
Bram Moolenaare99d4222021-06-13 14:01:26 +02002398 while (idx > 0)
2399 {
2400 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002401 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002402 {
2403 --idx;
2404 continue;
2405 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002406#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002407 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2408 {
2409 --idx;
2410 continue;
2411 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002412#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002413 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2414 {
2415 --idx;
2416 continue;
2417 }
2418 break;
2419 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002420 return idx;
2421}
2422
Bram Moolenaarf002a412021-01-24 13:34:18 +01002423#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002424 static void
2425may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2426{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002427 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002428 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002429}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002430#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002431
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002432/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002434 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002436 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002437reserve_local(
2438 cctx_T *cctx,
2439 char_u *name,
2440 size_t len,
2441 int isConst,
2442 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002445 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002447 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002449 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002450 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 }
2452
Bram Moolenaar35578162021-08-02 19:10:38 +02002453 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002454 return NULL;
2455 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002456 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002458 // Every local variable uses the next entry on the stack. We could re-use
2459 // the last ones when leaving a scope, but then variables used in a closure
2460 // might get overwritten. To keep things simple do not re-use stack
2461 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002462 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2463 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002464
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002465 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 lvar->lv_const = isConst;
2467 lvar->lv_type = type;
2468
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002469 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002470 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002471 return NULL;
2472 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2473 vim_strsave(lvar->lv_name);
2474 ++dfunc->df_var_names.ga_len;
2475
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002476 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477}
2478
2479/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002480 * Remove local variables above "new_top".
2481 */
2482 static void
2483unwind_locals(cctx_T *cctx, int new_top)
2484{
2485 if (cctx->ctx_locals.ga_len > new_top)
2486 {
2487 int idx;
2488 lvar_T *lvar;
2489
2490 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2491 {
2492 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2493 vim_free(lvar->lv_name);
2494 }
2495 }
2496 cctx->ctx_locals.ga_len = new_top;
2497}
2498
2499/*
2500 * Free all local variables.
2501 */
2502 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002503free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002504{
2505 unwind_locals(cctx, 0);
2506 ga_clear(&cctx->ctx_locals);
2507}
2508
2509/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002510 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2511 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2512 * error if the variable was defined with :const.
2513 */
2514 static int
2515check_item_writable(svar_T *sv, int check_writable, char_u *name)
2516{
2517 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2518 || (check_writable == ASSIGN_FINAL
2519 && sv->sv_const == ASSIGN_CONST))
2520 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002521 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002522 return FAIL;
2523 }
2524 return OK;
2525}
2526
2527/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002529 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 * Returns the index in "sn_var_vals" if found.
2531 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002532 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 */
2534 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002535get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536{
2537 hashtab_T *ht;
2538 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002539 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002540 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 int idx;
2542
Bram Moolenaare3d46852020-08-29 13:39:17 +02002543 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002545 if (sid == current_sctx.sc_sid)
2546 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002547 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002548
2549 if (sav == NULL)
2550 return -2;
2551 idx = sav->sav_var_vals_idx;
2552 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002553 if (check_item_writable(sv, check_writable, name) == FAIL)
2554 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002555 return idx;
2556 }
2557
2558 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 ht = &SCRIPT_VARS(sid);
2560 di = find_var_in_ht(ht, 0, name, TRUE);
2561 if (di == NULL)
2562 return -2;
2563
2564 // Now find the svar_T index in sn_var_vals.
2565 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2566 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002567 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 if (sv->sv_tv == &di->di_tv)
2569 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002570 if (check_item_writable(sv, check_writable, name) == FAIL)
2571 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 return idx;
2573 }
2574 }
2575 return -1;
2576}
2577
2578/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002579 * Find "name" in imported items of the current script or in "cctx" if not
2580 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 */
2582 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002583find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 int idx;
2586
Bram Moolenaare3d46852020-08-29 13:39:17 +02002587 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002588 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 if (cctx != NULL)
2590 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2591 {
2592 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2593 + idx;
2594
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002595 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2596 : STRLEN(import->imp_name) == len
2597 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 return import;
2599 }
2600
Bram Moolenaarefa94442020-08-08 22:16:00 +02002601 return find_imported_in_script(name, len, current_sctx.sc_sid);
2602}
2603
2604 imported_T *
2605find_imported_in_script(char_u *name, size_t len, int sid)
2606{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002607 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002608 int idx;
2609
Bram Moolenaare3d46852020-08-29 13:39:17 +02002610 if (!SCRIPT_ID_VALID(sid))
2611 return NULL;
2612 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2614 {
2615 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2616
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002617 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2618 : STRLEN(import->imp_name) == len
2619 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 return import;
2621 }
2622 return NULL;
2623}
2624
2625/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002626 * Free all imported variables.
2627 */
2628 static void
2629free_imported(cctx_T *cctx)
2630{
2631 int idx;
2632
2633 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2634 {
2635 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2636
2637 vim_free(import->imp_name);
2638 }
2639 ga_clear(&cctx->ctx_imports);
2640}
2641
2642/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002643 * Return a pointer to the next line that isn't empty or only contains a
2644 * comment. Skips over white space.
2645 * Returns NULL if there is none.
2646 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002647 char_u *
2648peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002649{
2650 int lnum = cctx->ctx_lnum;
2651
2652 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2653 {
2654 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002655 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002656
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002657 // ignore NULLs inserted for continuation lines
2658 if (line != NULL)
2659 {
2660 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002661 if (vim9_bad_comment(p))
2662 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002663 if (*p != NUL && !vim9_comment_start(p))
2664 return p;
2665 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002666 }
2667 return NULL;
2668}
2669
2670/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002671 * Called when checking for a following operator at "arg". When the rest of
2672 * the line is empty or only a comment, peek the next line. If there is a next
2673 * line return a pointer to it and set "nextp".
2674 * Otherwise skip over white space.
2675 */
2676 static char_u *
2677may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2678{
2679 char_u *p = skipwhite(arg);
2680
2681 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002682 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002683 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002684 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002685 if (*nextp != NULL)
2686 return *nextp;
2687 }
2688 return p;
2689}
2690
2691/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002692 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002693 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002694 * Returns NULL when at the end.
2695 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002696 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002697next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002698{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002699 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002700
2701 do
2702 {
2703 ++cctx->ctx_lnum;
2704 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002705 {
2706 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002707 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002708 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002709 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002710 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002711 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002712 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002713 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002714 return line;
2715}
2716
2717/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002718 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002719 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002720 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002721 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2722 */
2723 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002724may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002725{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002726 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002727 if (vim9_bad_comment(*arg))
2728 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002729 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002730 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002731 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002732
2733 if (next == NULL)
2734 return FAIL;
2735 *arg = skipwhite(next);
2736 }
2737 return OK;
2738}
2739
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002740/*
2741 * Idem, and give an error when failed.
2742 */
2743 static int
2744may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2745{
2746 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2747 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002748 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002749 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002750 return FAIL;
2751 }
2752 return OK;
2753}
2754
2755
Bram Moolenaara5565e42020-05-09 15:44:01 +02002756// Structure passed between the compile_expr* functions to keep track of
2757// constants that have been parsed but for which no code was produced yet. If
2758// possible expressions on these constants are applied at compile time. If
2759// that is not possible, the code to push the constants needs to be generated
2760// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002761// Using 50 should be more than enough of 5 levels of ().
2762#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002763typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002764 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002765 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002766 int pp_is_const; // all generated code was constants, used for a
2767 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002768} ppconst_T;
2769
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002770static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002771static int compile_expr0(char_u **arg, cctx_T *cctx);
2772static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2773
Bram Moolenaara5565e42020-05-09 15:44:01 +02002774/*
2775 * Generate a PUSH instruction for "tv".
2776 * "tv" will be consumed or cleared.
2777 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2778 */
2779 static int
2780generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2781{
2782 if (tv != NULL)
2783 {
2784 switch (tv->v_type)
2785 {
2786 case VAR_UNKNOWN:
2787 break;
2788 case VAR_BOOL:
2789 generate_PUSHBOOL(cctx, tv->vval.v_number);
2790 break;
2791 case VAR_SPECIAL:
2792 generate_PUSHSPEC(cctx, tv->vval.v_number);
2793 break;
2794 case VAR_NUMBER:
2795 generate_PUSHNR(cctx, tv->vval.v_number);
2796 break;
2797#ifdef FEAT_FLOAT
2798 case VAR_FLOAT:
2799 generate_PUSHF(cctx, tv->vval.v_float);
2800 break;
2801#endif
2802 case VAR_BLOB:
2803 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2804 tv->vval.v_blob = NULL;
2805 break;
2806 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002807 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002808 tv->vval.v_string = NULL;
2809 break;
2810 default:
2811 iemsg("constant type not supported");
2812 clear_tv(tv);
2813 return FAIL;
2814 }
2815 tv->v_type = VAR_UNKNOWN;
2816 }
2817 return OK;
2818}
2819
2820/*
2821 * Generate code for any ppconst entries.
2822 */
2823 static int
2824generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2825{
2826 int i;
2827 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002828 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002829
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002830 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002831 for (i = 0; i < ppconst->pp_used; ++i)
2832 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2833 ret = FAIL;
2834 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002835 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002836 return ret;
2837}
2838
2839/*
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002840 * Check that the last item of "ppconst" is a bool.
2841 */
2842 static int
2843check_ppconst_bool(ppconst_T *ppconst)
2844{
2845 if (ppconst->pp_used > 0)
2846 {
2847 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002848 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002849
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002850 return check_typval_type(&t_bool, tv, where);
2851 }
2852 return OK;
2853}
2854
2855/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002856 * Clear ppconst constants. Used when failing.
2857 */
2858 static void
2859clear_ppconst(ppconst_T *ppconst)
2860{
2861 int i;
2862
2863 for (i = 0; i < ppconst->pp_used; ++i)
2864 clear_tv(&ppconst->pp_tv[i]);
2865 ppconst->pp_used = 0;
2866}
2867
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002868/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002869 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002870 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002871 */
2872 static int
2873compile_member(int is_slice, cctx_T *cctx)
2874{
2875 type_T **typep;
2876 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002877 vartype_T vartype;
2878 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002879
Bram Moolenaar261417b2021-05-06 21:04:55 +02002880 // We can index a list, dict and blob. If we don't know the type
2881 // we can use the index value type. If we still don't know use an "ANY"
2882 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002883 typep = ((type_T **)stack->ga_data) + stack->ga_len
2884 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002885 vartype = (*typep)->tt_type;
2886 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002887 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002888 if (*typep == &t_any && idxtype == &t_string)
2889 vartype = VAR_DICT;
2890 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002891 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002892 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002893 return FAIL;
2894 if (is_slice)
2895 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002896 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2897 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002898 FALSE, FALSE) == FAIL)
2899 return FAIL;
2900 }
2901 }
2902
Bram Moolenaar261417b2021-05-06 21:04:55 +02002903 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002904 {
2905 if (is_slice)
2906 {
2907 emsg(_(e_cannot_slice_dictionary));
2908 return FAIL;
2909 }
2910 if ((*typep)->tt_type == VAR_DICT)
2911 {
2912 *typep = (*typep)->tt_member;
2913 if (*typep == &t_unknown)
2914 // empty dict was used
2915 *typep = &t_any;
2916 }
2917 else
2918 {
2919 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2920 FALSE, FALSE) == FAIL)
2921 return FAIL;
2922 *typep = &t_any;
2923 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002924 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002925 return FAIL;
2926 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2927 return FAIL;
2928 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002929 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002930 {
2931 *typep = &t_string;
2932 if ((is_slice
2933 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2934 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2935 return FAIL;
2936 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002937 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002938 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002939 if (is_slice)
2940 {
2941 *typep = &t_blob;
2942 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2943 return FAIL;
2944 }
2945 else
2946 {
2947 *typep = &t_number;
2948 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2949 return FAIL;
2950 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002951 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002952 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002953 {
2954 if (is_slice)
2955 {
2956 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002957 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002958 2) == FAIL)
2959 return FAIL;
2960 }
2961 else
2962 {
2963 if ((*typep)->tt_type == VAR_LIST)
2964 {
2965 *typep = (*typep)->tt_member;
2966 if (*typep == &t_unknown)
2967 // empty list was used
2968 *typep = &t_any;
2969 }
2970 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002971 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2972 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002973 return FAIL;
2974 }
2975 }
2976 else
2977 {
2978 emsg(_(e_string_list_dict_or_blob_required));
2979 return FAIL;
2980 }
2981 return OK;
2982}
2983
2984/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002985 * Generate an instruction to load script-local variable "name", without the
2986 * leading "s:".
2987 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 */
2989 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002990compile_load_scriptvar(
2991 cctx_T *cctx,
2992 char_u *name, // variable NUL terminated
2993 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002994 char_u **end, // end of variable
2995 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002997 scriptitem_T *si;
2998 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 imported_T *import;
3000
Bram Moolenaare3d46852020-08-29 13:39:17 +02003001 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3002 return FAIL;
3003 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003004 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003005 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003007 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003008 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3009 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 }
3011 if (idx >= 0)
3012 {
3013 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3014
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003015 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 current_sctx.sc_sid, idx, sv->sv_type);
3017 return OK;
3018 }
3019
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003020 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 if (import != NULL)
3022 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003023 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003024 {
3025 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003026 char_u *exp_name;
3027 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003028 ufunc_T *ufunc;
3029 type_T *type;
3030
3031 // Used "import * as Name", need to lookup the member.
3032 if (*p != '.')
3033 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003034 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003035 return FAIL;
3036 }
3037 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003038 if (VIM_ISWHITE(*p))
3039 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003040 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003041 return FAIL;
3042 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003043
Bram Moolenaar1c991142020-07-04 13:15:31 +02003044 // isolate one name
3045 exp_name = p;
3046 while (eval_isnamec(*p))
3047 ++p;
3048 cc = *p;
3049 *p = NUL;
3050
Bram Moolenaaredba7072021-03-13 21:14:18 +01003051 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3052 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003053 *p = cc;
3054 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003055 *end = p;
3056
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003057 if (idx < 0)
3058 {
3059 if (*p == '(' && ufunc != NULL)
3060 {
3061 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3062 return OK;
3063 }
3064 return FAIL;
3065 }
3066
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003067 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3068 import->imp_sid,
3069 idx,
3070 type);
3071 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003072 else if (import->imp_funcname != NULL)
3073 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003074 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003075 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3076 import->imp_sid,
3077 import->imp_var_vals_idx,
3078 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 return OK;
3080 }
3081
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003082 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003083 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 return FAIL;
3085}
3086
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003087 static int
3088generate_funcref(cctx_T *cctx, char_u *name)
3089{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003090 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003091
3092 if (ufunc == NULL)
3093 return FAIL;
3094
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003095 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003096 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3097 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003098 == FAIL)
3099 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003100 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003101}
3102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103/*
3104 * Compile a variable name into a load instruction.
3105 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003106 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 * When "error" is FALSE do not give an error when not found.
3108 */
3109 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003110compile_load(
3111 char_u **arg,
3112 char_u *end_arg,
3113 cctx_T *cctx,
3114 int is_expr,
3115 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116{
3117 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003118 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003119 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003121 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122
3123 if (*(*arg + 1) == ':')
3124 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003125 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003126 {
3127 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128
Bram Moolenaarfa596382021-04-07 21:58:16 +02003129 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003130 switch (**arg)
3131 {
3132 case 'g': isn_type = ISN_LOADGDICT; break;
3133 case 'w': isn_type = ISN_LOADWDICT; break;
3134 case 't': isn_type = ISN_LOADTDICT; break;
3135 case 'b': isn_type = ISN_LOADBDICT; break;
3136 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003137 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003138 goto theend;
3139 }
3140 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3141 goto theend;
3142 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 else
3145 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003146 isntype_T isn_type = ISN_DROP;
3147
Bram Moolenaarfa596382021-04-07 21:58:16 +02003148 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003149 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3150 if (name == NULL)
3151 return FAIL;
3152
3153 switch (**arg)
3154 {
3155 case 'v': res = generate_LOADV(cctx, name, error);
3156 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003157 case 's': if (is_expr && ASCII_ISUPPER(*name)
3158 && find_func(name, FALSE, cctx) != NULL)
3159 res = generate_funcref(cctx, name);
3160 else
3161 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003162 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003163 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003164 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003165 {
3166 if (is_expr && ASCII_ISUPPER(*name)
3167 && find_func(name, FALSE, cctx) != NULL)
3168 res = generate_funcref(cctx, name);
3169 else
3170 isn_type = ISN_LOADG;
3171 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003172 else
3173 {
3174 isn_type = ISN_LOADAUTO;
3175 vim_free(name);
3176 name = vim_strnsave(*arg, end - *arg);
3177 if (name == NULL)
3178 return FAIL;
3179 }
3180 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003181 case 'w': isn_type = ISN_LOADW; break;
3182 case 't': isn_type = ISN_LOADT; break;
3183 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003184 default: // cannot happen, just in case
3185 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003186 goto theend;
3187 }
3188 if (isn_type != ISN_DROP)
3189 {
3190 // Global, Buffer-local, Window-local and Tabpage-local
3191 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003192 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003193 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 }
3196 }
3197 else
3198 {
3199 size_t len = end - *arg;
3200 int idx;
3201 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003202 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203
3204 name = vim_strnsave(*arg, end - *arg);
3205 if (name == NULL)
3206 return FAIL;
3207
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003208 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3209 {
3210 script_autoload(name, FALSE);
3211 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3212 }
3213 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3214 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003216 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003217 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 }
3219 else
3220 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003221 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003222
Bram Moolenaar709664c2020-12-12 14:33:41 +01003223 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003225 type = lvar.lv_type;
3226 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003227 if (lvar.lv_from_outer != 0)
3228 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003229 else
3230 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 }
3232 else
3233 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003234 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003235 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003236 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003237 || find_imported(name, 0, cctx) != NULL)
3238 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003239
Bram Moolenaar0f769812020-09-12 18:32:34 +02003240 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003241 // uppercase letter it can be a user defined function.
3242 // generate_funcref() will fail if the function can't be found.
3243 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003244 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 }
3246 }
3247 if (gen_load)
3248 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003249 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003250 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003251 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003252 cctx->ctx_outer_used = TRUE;
3253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 }
3255
3256 *arg = end;
3257
3258theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003259 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003260 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 vim_free(name);
3262 return res;
3263}
3264
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003265 static void
3266clear_instr_ga(garray_T *gap)
3267{
3268 int idx;
3269
3270 for (idx = 0; idx < gap->ga_len; ++idx)
3271 delete_instr(((isn_T *)gap->ga_data) + idx);
3272 ga_clear(gap);
3273}
3274
3275/*
3276 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3277 * Returns FAIL if compilation fails.
3278 */
3279 static int
3280compile_string(isn_T *isn, cctx_T *cctx)
3281{
3282 char_u *s = isn->isn_arg.string;
3283 garray_T save_ga = cctx->ctx_instr;
3284 int expr_res;
3285 int trailing_error;
3286 int instr_count;
3287 isn_T *instr = NULL;
3288
Bram Moolenaarcd268012021-07-22 19:11:08 +02003289 // Remove the string type from the stack.
3290 --cctx->ctx_type_stack.ga_len;
3291
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003292 // Temporarily reset the list of instructions so that the jump labels are
3293 // correct.
3294 cctx->ctx_instr.ga_len = 0;
3295 cctx->ctx_instr.ga_maxlen = 0;
3296 cctx->ctx_instr.ga_data = NULL;
3297 expr_res = compile_expr0(&s, cctx);
3298 s = skipwhite(s);
3299 trailing_error = *s != NUL;
3300
Bram Moolenaarff652882021-05-16 15:24:49 +02003301 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003302 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003303 {
3304 if (trailing_error)
3305 semsg(_(e_trailing_arg), s);
3306 clear_instr_ga(&cctx->ctx_instr);
3307 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003308 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003309 return FAIL;
3310 }
3311
3312 // Move the generated instructions into the ISN_INSTR instruction, then
3313 // restore the list of instructions.
3314 instr_count = cctx->ctx_instr.ga_len;
3315 instr = cctx->ctx_instr.ga_data;
3316 instr[instr_count].isn_type = ISN_FINISH;
3317
3318 cctx->ctx_instr = save_ga;
3319 vim_free(isn->isn_arg.string);
3320 isn->isn_type = ISN_INSTR;
3321 isn->isn_arg.instr = instr;
3322 return OK;
3323}
3324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325/*
3326 * Compile the argument expressions.
3327 * "arg" points to just after the "(" and is advanced to after the ")"
3328 */
3329 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003330compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003332 char_u *p = *arg;
3333 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003334 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003335 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336
Bram Moolenaare6085c52020-04-12 20:19:16 +02003337 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003339 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3340 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003341 if (*p == ')')
3342 {
3343 *arg = p + 1;
3344 return OK;
3345 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003346 if (must_end)
3347 {
3348 semsg(_(e_missing_comma_before_argument_str), p);
3349 return FAIL;
3350 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003351
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003352 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003353 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 return FAIL;
3355 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003356
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003357 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003358 && cctx->ctx_instr.ga_len == instr_count + 1)
3359 {
3360 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3361
3362 // {skip} argument of searchpair() can be compiled if not empty
3363 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3364 compile_string(isn, cctx);
3365 }
3366
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003367 if (*p != ',' && *skipwhite(p) == ',')
3368 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003369 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003370 p = skipwhite(p);
3371 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003373 {
3374 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003375 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003376 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003377 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003378 else
3379 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003380 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003381 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003383failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003384 emsg(_(e_missing_close));
3385 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386}
3387
3388/*
3389 * Compile a function call: name(arg1, arg2)
3390 * "arg" points to "name", "arg + varlen" to the "(".
3391 * "argcount_init" is 1 for "value->method()"
3392 * Instructions:
3393 * EVAL arg1
3394 * EVAL arg2
3395 * BCALL / DCALL / UCALL
3396 */
3397 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003398compile_call(
3399 char_u **arg,
3400 size_t varlen,
3401 cctx_T *cctx,
3402 ppconst_T *ppconst,
3403 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404{
3405 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003406 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 int argcount = argcount_init;
3408 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003409 char_u fname_buf[FLEN_FIXED + 1];
3410 char_u *tofree = NULL;
3411 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003412 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003413 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003414 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003415 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003417 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003418 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003419 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003420 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003421 {
3422 char_u *s = skipwhite(*arg + varlen + 1);
3423 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003424 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003425
3426 argvars[0].v_type = VAR_UNKNOWN;
3427 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003428 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003429 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003430 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003431 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003432 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003433 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003434 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003435 {
3436 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3437
3438 *arg = s + 1;
3439 argvars[1].v_type = VAR_UNKNOWN;
3440 tv->v_type = VAR_NUMBER;
3441 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003442 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003443 f_has(argvars, tv);
3444 else
3445 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003446 clear_tv(&argvars[0]);
3447 ++ppconst->pp_used;
3448 return OK;
3449 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003450 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003451 if (!is_has)
3452 {
3453 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3454 return FAIL;
3455 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003456 }
3457
3458 if (generate_ppconst(cctx, ppconst) == FAIL)
3459 return FAIL;
3460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 if (varlen >= sizeof(namebuf))
3462 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003463 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 return FAIL;
3465 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003466 vim_strncpy(namebuf, *arg, varlen);
3467 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003469 // We handle the "skip" argument of searchpair() and searchpairpos()
3470 // differently.
3471 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3472 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3473 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3474 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003477 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003478 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479
Bram Moolenaar03290b82020-12-19 16:30:44 +01003480 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003481 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 {
3483 int idx;
3484
3485 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003486 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003488 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003489 if (STRCMP(name, "flatten") == 0)
3490 {
3491 emsg(_(e_cannot_use_flatten_in_vim9_script));
3492 goto theend;
3493 }
3494
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003495 if (STRCMP(name, "add") == 0 && argcount == 2)
3496 {
3497 garray_T *stack = &cctx->ctx_type_stack;
3498 type_T *type = ((type_T **)stack->ga_data)[
3499 stack->ga_len - 2];
3500
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003501 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003502 if (type->tt_type == VAR_LIST)
3503 {
3504 // inline "add(list, item)" so that the type can be checked
3505 res = generate_LISTAPPEND(cctx);
3506 idx = -1;
3507 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003508 else if (type->tt_type == VAR_BLOB)
3509 {
3510 // inline "add(blob, nr)" so that the type can be checked
3511 res = generate_BLOBAPPEND(cctx);
3512 idx = -1;
3513 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003514 }
3515
3516 if (idx >= 0)
3517 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3518 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003519 else
3520 semsg(_(e_unknownfunc), namebuf);
3521 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 }
3523
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003524 // An argument or local variable can be a function reference, this
3525 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003526 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003527 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003528 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003529 // If we can find the function by name generate the right call.
3530 // Skip global functions here, a local funcref takes precedence.
3531 ufunc = find_func(name, FALSE, cctx);
3532 if (ufunc != NULL && !func_is_global(ufunc))
3533 {
3534 res = generate_CALL(cctx, ufunc, argcount);
3535 goto theend;
3536 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538
3539 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003540 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003541 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003543 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003544 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003545 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003546 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003547 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003548
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003549 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003550 goto theend;
3551 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552
Bram Moolenaar0f769812020-09-12 18:32:34 +02003553 // If we can find a global function by name generate the right call.
3554 if (ufunc != NULL)
3555 {
3556 res = generate_CALL(cctx, ufunc, argcount);
3557 goto theend;
3558 }
3559
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003560 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003561 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003562 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003563 res = generate_UCALL(cctx, name, argcount);
3564 else
3565 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003566
3567theend:
3568 vim_free(tofree);
3569 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570}
3571
3572// like NAMESPACE_CHAR but with 'a' and 'l'.
3573#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3574
3575/*
3576 * Find the end of a variable or function name. Unlike find_name_end() this
3577 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003578 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 * Return a pointer to just after the name. Equal to "arg" if there is no
3580 * valid name.
3581 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02003582 static char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003583to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584{
3585 char_u *p;
3586
3587 // Quick check for valid starting character.
3588 if (!eval_isnamec1(*arg))
3589 return arg;
3590
3591 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3592 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3593 // and can be used in slice "[n:]".
3594 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003595 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3597 break;
3598 return p;
3599}
3600
3601/*
3602 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003603 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003604 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 */
3606 char_u *
3607to_name_const_end(char_u *arg)
3608{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003609 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 typval_T rettv;
3611
Bram Moolenaar678b2072021-07-26 21:10:11 +02003612 if (STRNCMP(p, "<SNR>", 5) == 0)
3613 p = skipdigits(p + 5);
3614 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 if (p == arg && *arg == '[')
3616 {
3617
3618 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003619 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 p = arg;
3621 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 return p;
3623}
3624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625/*
3626 * parse a list: [expr, expr]
3627 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003628 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 */
3630 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003631compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632{
3633 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003634 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003636 int is_const;
3637 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003639 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003641 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003642 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003643 semsg(_(e_list_end), *arg);
3644 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003645 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003646 if (*p == ',')
3647 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003648 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003649 return FAIL;
3650 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003651 if (*p == ']')
3652 {
3653 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003654 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003655 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003656 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003657 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003658 if (!is_const)
3659 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 ++count;
3661 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003662 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003664 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3665 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003666 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003667 return FAIL;
3668 }
3669 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003670 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 p = skipwhite(p);
3672 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003673 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003675 ppconst->pp_is_const = is_all_const;
3676 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677}
3678
3679/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003680 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003681 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003682 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 */
3684 static int
3685compile_lambda(char_u **arg, cctx_T *cctx)
3686{
Bram Moolenaare462f522020-12-27 14:43:30 +01003687 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 typval_T rettv;
3689 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003690 evalarg_T evalarg;
3691
3692 CLEAR_FIELD(evalarg);
3693 evalarg.eval_flags = EVAL_EVALUATE;
3694 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695
3696 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003697 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3698 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003699 {
3700 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003701 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003702 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003703
Bram Moolenaar65c44152020-12-24 15:14:01 +01003704 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003706 ++ufunc->uf_refcount;
3707 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708
Bram Moolenaara9931532021-06-12 15:58:16 +02003709 // Compile it here to get the return type. The return type is optional,
3710 // when it's missing use t_unknown. This is recognized in
3711 // compile_return().
3712 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3713 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003714 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715
Bram Moolenaar648594e2021-07-11 17:55:01 +02003716#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003717 // When the outer function is compiled for profiling, the lambda may be
3718 // called without profiling. Compile it here in the right context.
3719 if (cctx->ctx_compile_type == CT_PROFILE)
3720 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003721#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003722
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003723 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3724 // points into it. Point to the original line to avoid a dangling pointer.
3725 if (evalarg.eval_tofree_cmdline != NULL)
3726 {
3727 size_t off = *arg - evalarg.eval_tofree_cmdline;
3728
3729 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3730 + off;
3731 }
3732
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003733 clear_evalarg(&evalarg, NULL);
3734
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003735 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003736 {
3737 // The return type will now be known.
3738 set_function_type(ufunc);
3739
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003740 // The function reference count will be 1. When the ISN_FUNCREF
3741 // instruction is deleted the reference count is decremented and the
3742 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003743 return generate_FUNCREF(cctx, ufunc);
3744 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003745
3746 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 return FAIL;
3748}
3749
3750/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003751 * Get a lambda and compile it. Uses Vim9 syntax.
3752 */
3753 int
3754get_lambda_tv_and_compile(
3755 char_u **arg,
3756 typval_T *rettv,
3757 int types_optional,
3758 evalarg_T *evalarg)
3759{
3760 int r;
3761 ufunc_T *ufunc;
3762 int save_sc_version = current_sctx.sc_version;
3763
3764 // Get the funcref in "rettv".
3765 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3766 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3767 current_sctx.sc_version = save_sc_version;
3768 if (r != OK)
3769 return r;
3770
3771 // "rettv" will now be a partial referencing the function.
3772 ufunc = rettv->vval.v_partial->pt_func;
3773
3774 // Compile it here to get the return type. The return type is optional,
3775 // when it's missing use t_unknown. This is recognized in
3776 // compile_return().
3777 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3778 ufunc->uf_ret_type = &t_unknown;
3779 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3780
3781 if (ufunc->uf_def_status == UF_COMPILED)
3782 {
3783 // The return type will now be known.
3784 set_function_type(ufunc);
3785 return OK;
3786 }
3787 clear_tv(rettv);
3788 return FAIL;
3789}
3790
3791/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003792 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003794 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 */
3796 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003797compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798{
3799 garray_T *instr = &cctx->ctx_instr;
3800 int count = 0;
3801 dict_T *d = dict_alloc();
3802 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003803 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003804 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003805 int is_const;
3806 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
3808 if (d == NULL)
3809 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003810 if (generate_ppconst(cctx, ppconst) == FAIL)
3811 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003812 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003814 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815
Bram Moolenaar23c55272020-06-21 16:58:13 +02003816 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003817 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003818 *arg = NULL;
3819 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003820 }
3821
3822 if (**arg == '}')
3823 break;
3824
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003825 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003827 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003829 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003830 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003831 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003834 if (isn->isn_type == ISN_PUSHNR)
3835 {
3836 char buf[NUMBUFLEN];
3837
3838 // Convert to string at compile time.
3839 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3840 isn->isn_type = ISN_PUSHS;
3841 isn->isn_arg.string = vim_strsave((char_u *)buf);
3842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 if (isn->isn_type == ISN_PUSHS)
3844 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003845 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003846 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003847 *arg = skipwhite(*arg);
3848 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003849 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003850 emsg(_(e_missing_matching_bracket_after_dict_key));
3851 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003852 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003853 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003855 else
3856 {
3857 // {"name": value},
3858 // {'name': value},
3859 // {name: value} use "name" as a literal key
3860 key = get_literal_key(arg);
3861 if (key == NULL)
3862 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003863 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003864 return FAIL;
3865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866
3867 // Check for duplicate keys, if using string keys.
3868 if (key != NULL)
3869 {
3870 item = dict_find(d, key, -1);
3871 if (item != NULL)
3872 {
3873 semsg(_(e_duplicate_key), key);
3874 goto failret;
3875 }
3876 item = dictitem_alloc(key);
3877 if (item != NULL)
3878 {
3879 item->di_tv.v_type = VAR_UNKNOWN;
3880 item->di_tv.v_lock = 0;
3881 if (dict_add(d, item) == FAIL)
3882 dictitem_free(item);
3883 }
3884 }
3885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 if (**arg != ':')
3887 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003888 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003889 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003890 else
3891 semsg(_(e_missing_dict_colon), *arg);
3892 return FAIL;
3893 }
3894 whitep = *arg + 1;
3895 if (!IS_WHITE_OR_NUL(*whitep))
3896 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003897 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 return FAIL;
3899 }
3900
Bram Moolenaar23c55272020-06-21 16:58:13 +02003901 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003902 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003903 *arg = NULL;
3904 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003905 }
3906
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003907 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003909 if (!is_const)
3910 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 ++count;
3912
Bram Moolenaar2c330432020-04-13 14:41:35 +02003913 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003914 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003915 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003916 *arg = NULL;
3917 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 if (**arg == '}')
3920 break;
3921 if (**arg != ',')
3922 {
3923 semsg(_(e_missing_dict_comma), *arg);
3924 goto failret;
3925 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003926 if (IS_WHITE_OR_NUL(*whitep))
3927 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003928 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003929 return FAIL;
3930 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003931 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003932 if (!IS_WHITE_OR_NUL(*whitep))
3933 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003934 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003935 return FAIL;
3936 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003937 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 }
3939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 *arg = *arg + 1;
3941
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003942 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003943 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003944 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003945 *arg += STRLEN(*arg);
3946
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003948 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 return generate_NEWDICT(cctx, count);
3950
3951failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003952 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003953 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003954 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003955 *arg = (char_u *)"";
3956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 dict_unref(d);
3958 return FAIL;
3959}
3960
3961/*
3962 * Compile "&option".
3963 */
3964 static int
3965compile_get_option(char_u **arg, cctx_T *cctx)
3966{
3967 typval_T rettv;
3968 char_u *start = *arg;
3969 int ret;
3970
3971 // parse the option and get the current value to get the type.
3972 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003973 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 if (ret == OK)
3975 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003976 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003977 char_u *name = vim_strnsave(start, *arg - start);
3978 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3979 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980
3981 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3982 vim_free(name);
3983 }
3984 clear_tv(&rettv);
3985
3986 return ret;
3987}
3988
3989/*
3990 * Compile "$VAR".
3991 */
3992 static int
3993compile_get_env(char_u **arg, cctx_T *cctx)
3994{
3995 char_u *start = *arg;
3996 int len;
3997 int ret;
3998 char_u *name;
3999
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 ++*arg;
4001 len = get_env_len(arg);
4002 if (len == 0)
4003 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004004 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 return FAIL;
4006 }
4007
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004008 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 name = vim_strnsave(start, len + 1);
4010 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4011 vim_free(name);
4012 return ret;
4013}
4014
4015/*
4016 * Compile "@r".
4017 */
4018 static int
4019compile_get_register(char_u **arg, cctx_T *cctx)
4020{
4021 int ret;
4022
4023 ++*arg;
4024 if (**arg == NUL)
4025 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004026 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 return FAIL;
4028 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004029 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 {
4031 emsg_invreg(**arg);
4032 return FAIL;
4033 }
4034 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4035 ++*arg;
4036 return ret;
4037}
4038
4039/*
4040 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004041 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 */
4043 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004044apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004046 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047
4048 // this works from end to start
4049 while (p > start)
4050 {
4051 --p;
4052 if (*p == '-' || *p == '+')
4053 {
4054 // only '-' has an effect, for '+' we only check the type
4055#ifdef FEAT_FLOAT
4056 if (rettv->v_type == VAR_FLOAT)
4057 {
4058 if (*p == '-')
4059 rettv->vval.v_float = -rettv->vval.v_float;
4060 }
4061 else
4062#endif
4063 {
4064 varnumber_T val;
4065 int error = FALSE;
4066
4067 // tv_get_number_chk() accepts a string, but we don't want that
4068 // here
4069 if (check_not_string(rettv) == FAIL)
4070 return FAIL;
4071 val = tv_get_number_chk(rettv, &error);
4072 clear_tv(rettv);
4073 if (error)
4074 return FAIL;
4075 if (*p == '-')
4076 val = -val;
4077 rettv->v_type = VAR_NUMBER;
4078 rettv->vval.v_number = val;
4079 }
4080 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004081 else if (numeric_only)
4082 {
4083 ++p;
4084 break;
4085 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004086 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 {
4088 int v = tv2bool(rettv);
4089
4090 // '!' is permissive in the type.
4091 clear_tv(rettv);
4092 rettv->v_type = VAR_BOOL;
4093 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4094 }
4095 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004096 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 return OK;
4098}
4099
4100/*
4101 * Recognize v: variables that are constants and set "rettv".
4102 */
4103 static void
4104get_vim_constant(char_u **arg, typval_T *rettv)
4105{
4106 if (STRNCMP(*arg, "v:true", 6) == 0)
4107 {
4108 rettv->v_type = VAR_BOOL;
4109 rettv->vval.v_number = VVAL_TRUE;
4110 *arg += 6;
4111 }
4112 else if (STRNCMP(*arg, "v:false", 7) == 0)
4113 {
4114 rettv->v_type = VAR_BOOL;
4115 rettv->vval.v_number = VVAL_FALSE;
4116 *arg += 7;
4117 }
4118 else if (STRNCMP(*arg, "v:null", 6) == 0)
4119 {
4120 rettv->v_type = VAR_SPECIAL;
4121 rettv->vval.v_number = VVAL_NULL;
4122 *arg += 6;
4123 }
4124 else if (STRNCMP(*arg, "v:none", 6) == 0)
4125 {
4126 rettv->v_type = VAR_SPECIAL;
4127 rettv->vval.v_number = VVAL_NONE;
4128 *arg += 6;
4129 }
4130}
4131
Bram Moolenaar657137c2021-01-09 15:45:23 +01004132 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004133get_compare_type(char_u *p, int *len, int *type_is)
4134{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004135 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004136 int i;
4137
4138 switch (p[0])
4139 {
4140 case '=': if (p[1] == '=')
4141 type = EXPR_EQUAL;
4142 else if (p[1] == '~')
4143 type = EXPR_MATCH;
4144 break;
4145 case '!': if (p[1] == '=')
4146 type = EXPR_NEQUAL;
4147 else if (p[1] == '~')
4148 type = EXPR_NOMATCH;
4149 break;
4150 case '>': if (p[1] != '=')
4151 {
4152 type = EXPR_GREATER;
4153 *len = 1;
4154 }
4155 else
4156 type = EXPR_GEQUAL;
4157 break;
4158 case '<': if (p[1] != '=')
4159 {
4160 type = EXPR_SMALLER;
4161 *len = 1;
4162 }
4163 else
4164 type = EXPR_SEQUAL;
4165 break;
4166 case 'i': if (p[1] == 's')
4167 {
4168 // "is" and "isnot"; but not a prefix of a name
4169 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4170 *len = 5;
4171 i = p[*len];
4172 if (!isalnum(i) && i != '_')
4173 {
4174 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4175 *type_is = TRUE;
4176 }
4177 }
4178 break;
4179 }
4180 return type;
4181}
4182
4183/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004184 * Skip over an expression, ignoring most errors.
4185 */
4186 static void
4187skip_expr_cctx(char_u **arg, cctx_T *cctx)
4188{
4189 evalarg_T evalarg;
4190
4191 CLEAR_FIELD(evalarg);
4192 evalarg.eval_cctx = cctx;
4193 skip_expr(arg, &evalarg);
4194}
4195
4196/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004198 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 */
4200 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004201compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004203 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204
4205 // this works from end to start
4206 while (p > start)
4207 {
4208 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004209 while (VIM_ISWHITE(*p))
4210 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 if (*p == '-' || *p == '+')
4212 {
4213 int negate = *p == '-';
4214 isn_T *isn;
4215
4216 // TODO: check type
4217 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4218 {
4219 --p;
4220 if (*p == '-')
4221 negate = !negate;
4222 }
4223 // only '-' has an effect, for '+' we only check the type
4224 if (negate)
4225 isn = generate_instr(cctx, ISN_NEGATENR);
4226 else
4227 isn = generate_instr(cctx, ISN_CHECKNR);
4228 if (isn == NULL)
4229 return FAIL;
4230 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004231 else if (numeric_only)
4232 {
4233 ++p;
4234 break;
4235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 else
4237 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004238 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004240 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004242 if (p[-1] == '!')
4243 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004246 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 return FAIL;
4248 }
4249 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004250 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 return OK;
4252}
4253
4254/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004255 * Compile "(expression)": recursive!
4256 * Return FAIL/OK.
4257 */
4258 static int
4259compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4260{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004261 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004262 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004263
Bram Moolenaar24156692021-01-14 20:35:49 +01004264 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4265 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004266 if (ppconst->pp_used <= PPSIZE - 10)
4267 {
4268 ret = compile_expr1(arg, cctx, ppconst);
4269 }
4270 else
4271 {
4272 // Not enough space in ppconst, flush constants.
4273 if (generate_ppconst(cctx, ppconst) == FAIL)
4274 return FAIL;
4275 ret = compile_expr0(arg, cctx);
4276 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004277 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4278 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004279 if (**arg == ')')
4280 ++*arg;
4281 else if (ret == OK)
4282 {
4283 emsg(_(e_missing_close));
4284 ret = FAIL;
4285 }
4286 return ret;
4287}
4288
4289/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004291 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 */
4293 static int
4294compile_subscript(
4295 char_u **arg,
4296 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004297 char_u *start_leader,
4298 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004299 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004301 char_u *name_start = *end_leader;
4302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 for (;;)
4304 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004305 char_u *p = skipwhite(*arg);
4306
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004307 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004308 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004309 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004310
4311 // If a following line starts with "->{" or "->X" advance to that
4312 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004313 // Also if a following line starts with ".x".
4314 if (next != NULL &&
4315 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004316 && (next[2] == '{'
4317 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004318 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004319 {
4320 next = next_line_from_context(cctx, TRUE);
4321 if (next == NULL)
4322 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004323 *arg = next;
4324 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004325 }
4326 }
4327
Bram Moolenaarcd268012021-07-22 19:11:08 +02004328 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4329 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004330 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004332 garray_T *stack = &cctx->ctx_type_stack;
4333 type_T *type;
4334 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004336 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004337 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004338 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004341 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4342
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004343 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004344 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004346 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 return FAIL;
4348 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004349 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004351 char_u *pstart = p;
4352
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004353 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004354 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004355 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 // something->method()
4358 // Apply the '!', '-' and '+' first:
4359 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004360 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004363 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004364 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004365 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004366 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004367 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004368 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004369 garray_T *stack = &cctx->ctx_type_stack;
4370 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004371 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004372 int expr_isn_start = cctx->ctx_instr.ga_len;
4373 int expr_isn_end;
4374 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004375
4376 // Funcref call: list->(Refs[2])(arg)
4377 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004378 //
4379 // Fist compile the function expression.
4380 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004381 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004382
4383 // Remember the next instruction index, where the instructions
4384 // for arguments are being written.
4385 expr_isn_end = cctx->ctx_instr.ga_len;
4386
4387 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004388 if (**arg != '(')
4389 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004390 if (*skipwhite(*arg) == '(')
4391 emsg(_(e_nowhitespace));
4392 else
4393 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004394 return FAIL;
4395 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004396 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004397 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004398 return FAIL;
4399
Bram Moolenaar2927c072021-04-05 19:41:21 +02004400 // Move the instructions for the arguments to before the
4401 // instructions of the expression and move the type of the
4402 // expression after the argument types. This is what ISN_PCALL
4403 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004404 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004405 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4406 if (arg_isn_count > 0)
4407 {
4408 int expr_isn_count = expr_isn_end - expr_isn_start;
4409 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4410
4411 if (isn == NULL)
4412 return FAIL;
4413 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4414 + expr_isn_start,
4415 sizeof(isn_T) * expr_isn_count);
4416 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4417 + expr_isn_start,
4418 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4419 sizeof(isn_T) * arg_isn_count);
4420 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4421 + expr_isn_start + arg_isn_count,
4422 isn, sizeof(isn_T) * expr_isn_count);
4423 vim_free(isn);
4424
4425 type = ((type_T **)stack->ga_data)[type_idx_start];
4426 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4427 ((type_T **)stack->ga_data) + type_idx_start + 1,
4428 sizeof(type_T *)
4429 * (stack->ga_len - type_idx_start - 1));
4430 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4431 }
4432
Bram Moolenaar7e368202020-12-25 21:56:57 +01004433 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004434 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 return FAIL;
4436 }
4437 else
4438 {
4439 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004440 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004441 if (!eval_isnamec1(*p))
4442 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004443 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004444 return FAIL;
4445 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004446 if (ASCII_ISALPHA(*p) && p[1] == ':')
4447 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004448 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 ;
4450 if (*p != '(')
4451 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004452 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 return FAIL;
4454 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004455 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 return FAIL;
4457 }
4458 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004459 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004461 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004462
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004463 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004464 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004465 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004466 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004467 // TODO: more arguments
4468 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004469 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004470 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004471 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004472
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004473 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004474 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004475 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004476 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004477 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004478 // missing first index is equal to zero
4479 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004480 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004481 else
4482 {
4483 if (compile_expr0(arg, cctx) == FAIL)
4484 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004485 if (**arg == ':')
4486 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004487 semsg(_(e_white_space_required_before_and_after_str_at_str),
4488 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004489 return FAIL;
4490 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004491 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004492 return FAIL;
4493 *arg = skipwhite(*arg);
4494 }
4495 if (**arg == ':')
4496 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004497 is_slice = TRUE;
4498 ++*arg;
4499 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4500 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004501 semsg(_(e_white_space_required_before_and_after_str_at_str),
4502 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004503 return FAIL;
4504 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004505 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004506 return FAIL;
4507 if (**arg == ']')
4508 // missing second index is equal to end of string
4509 generate_PUSHNR(cctx, -1);
4510 else
4511 {
4512 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004513 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004514 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004515 return FAIL;
4516 *arg = skipwhite(*arg);
4517 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519
4520 if (**arg != ']')
4521 {
4522 emsg(_(e_missbrac));
4523 return FAIL;
4524 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004525 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526
Bram Moolenaare42939a2021-04-05 17:11:17 +02004527 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004528 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004530 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004532 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004533 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004534 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004535 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004536
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004537 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004538 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004539 {
4540 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004541 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004542 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004543 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004544 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 while (eval_isnamec(*p))
4546 MB_PTR_ADV(p);
4547 if (p == *arg)
4548 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004549 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 return FAIL;
4551 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004552 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 return FAIL;
4554 *arg = p;
4555 }
4556 else
4557 break;
4558 }
4559
4560 // TODO - see handle_subscript():
4561 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4562 // Don't do this when "Func" is already a partial that was bound
4563 // explicitly (pt_auto is FALSE).
4564
4565 return OK;
4566}
4567
4568/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004569 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4570 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004572 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004573 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004574 *
4575 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 */
4577
4578/*
4579 * number number constant
4580 * 0zFFFFFFFF Blob constant
4581 * "string" string constant
4582 * 'string' literal string constant
4583 * &option-name option value
4584 * @r register contents
4585 * identifier variable value
4586 * function() function call
4587 * $VAR environment variable
4588 * (expression) nested expression
4589 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004590 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 *
4592 * Also handle:
4593 * ! in front logical NOT
4594 * - in front unary minus
4595 * + in front unary plus (ignored)
4596 * trailing (arg) funcref/partial call
4597 * trailing [] subscript in String or List
4598 * trailing .name entry in Dictionary
4599 * trailing ->name() method call
4600 */
4601 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004602compile_expr7(
4603 char_u **arg,
4604 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004605 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 char_u *start_leader, *end_leader;
4608 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004609 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004610 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004612 ppconst->pp_is_const = FALSE;
4613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 /*
4615 * Skip '!', '-' and '+' characters. They are handled later.
4616 */
4617 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004618 if (eval_leader(arg, TRUE) == FAIL)
4619 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 end_leader = *arg;
4621
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004622 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 switch (**arg)
4624 {
4625 /*
4626 * Number constant.
4627 */
4628 case '0': // also for blob starting with 0z
4629 case '1':
4630 case '2':
4631 case '3':
4632 case '4':
4633 case '5':
4634 case '6':
4635 case '7':
4636 case '8':
4637 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004638 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004640 // Apply "-" and "+" just before the number now, right to
4641 // left. Matters especially when "->" follows. Stops at
4642 // '!'.
4643 if (apply_leader(rettv, TRUE,
4644 start_leader, &end_leader) == FAIL)
4645 {
4646 clear_tv(rettv);
4647 return FAIL;
4648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 break;
4650
4651 /*
4652 * String constant: "string".
4653 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004654 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 return FAIL;
4656 break;
4657
4658 /*
4659 * Literal string constant: 'str''ing'.
4660 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004661 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 return FAIL;
4663 break;
4664
4665 /*
4666 * Constant Vim variable.
4667 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004668 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 ret = NOTDONE;
4670 break;
4671
4672 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004673 * "true" constant
4674 */
4675 case 't': if (STRNCMP(*arg, "true", 4) == 0
4676 && !eval_isnamec((*arg)[4]))
4677 {
4678 *arg += 4;
4679 rettv->v_type = VAR_BOOL;
4680 rettv->vval.v_number = VVAL_TRUE;
4681 }
4682 else
4683 ret = NOTDONE;
4684 break;
4685
4686 /*
4687 * "false" constant
4688 */
4689 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4690 && !eval_isnamec((*arg)[5]))
4691 {
4692 *arg += 5;
4693 rettv->v_type = VAR_BOOL;
4694 rettv->vval.v_number = VVAL_FALSE;
4695 }
4696 else
4697 ret = NOTDONE;
4698 break;
4699
4700 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004701 * "null" constant
4702 */
4703 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004704 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004705 {
4706 *arg += 4;
4707 rettv->v_type = VAR_SPECIAL;
4708 rettv->vval.v_number = VVAL_NULL;
4709 }
4710 else
4711 ret = NOTDONE;
4712 break;
4713
4714 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 * List: [expr, expr]
4716 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004717 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4718 return FAIL;
4719 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 break;
4721
4722 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 * Dictionary: {'key': val, 'key': val}
4724 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004725 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4726 return FAIL;
4727 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728 break;
4729
4730 /*
4731 * Option value: &name
4732 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004733 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4734 return FAIL;
4735 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 break;
4737
4738 /*
4739 * Environment variable: $VAR.
4740 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004741 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4742 return FAIL;
4743 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 break;
4745
4746 /*
4747 * Register contents: @r.
4748 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004749 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4750 return FAIL;
4751 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 break;
4753 /*
4754 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004755 * lambda: (arg, arg) => expr
4756 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004758 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4759 ret = compile_lambda(arg, cctx);
4760 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004761 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 break;
4763
4764 default: ret = NOTDONE;
4765 break;
4766 }
4767 if (ret == FAIL)
4768 return FAIL;
4769
Bram Moolenaar1c747212020-05-09 18:28:34 +02004770 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004772 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004773 clear_tv(rettv);
4774 else
4775 // A constant expression can possibly be handled compile time,
4776 // return the value instead of generating code.
4777 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 }
4779 else if (ret == NOTDONE)
4780 {
4781 char_u *p;
4782 int r;
4783
4784 if (!eval_isnamec1(**arg))
4785 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004786 if (!vim9_bad_comment(*arg))
4787 {
4788 if (ends_excmd(*skipwhite(*arg)))
4789 semsg(_(e_empty_expression_str), *arg);
4790 else
4791 semsg(_(e_name_expected_str), *arg);
4792 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 return FAIL;
4794 }
4795
4796 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004797 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004798 if (p - *arg == (size_t)1 && **arg == '_')
4799 {
4800 emsg(_(e_cannot_use_underscore_here));
4801 return FAIL;
4802 }
4803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004805 {
4806 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4807 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004809 {
4810 if (generate_ppconst(cctx, ppconst) == FAIL)
4811 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004812 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004813 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 if (r == FAIL)
4815 return FAIL;
4816 }
4817
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004818 // Handle following "[]", ".member", etc.
4819 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004820 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004821 ppconst) == FAIL)
4822 return FAIL;
4823 if (ppconst->pp_used > 0)
4824 {
4825 // apply the '!', '-' and '+' before the constant
4826 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004827 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004828 return FAIL;
4829 return OK;
4830 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004831 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004833 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834}
4835
4836/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004837 * Give the "white on both sides" error, taking the operator from "p[len]".
4838 */
4839 void
4840error_white_both(char_u *op, int len)
4841{
4842 char_u buf[10];
4843
4844 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004845 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004846}
4847
4848/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004849 * <type>expr7: runtime type check / conversion
4850 */
4851 static int
4852compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4853{
4854 type_T *want_type = NULL;
4855
4856 // Recognize <type>
4857 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4858 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004859 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004860 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4861 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004862 return FAIL;
4863
4864 if (**arg != '>')
4865 {
4866 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004867 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004868 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004869 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004870 return FAIL;
4871 }
4872 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004873 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004874 return FAIL;
4875 }
4876
4877 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4878 return FAIL;
4879
4880 if (want_type != NULL)
4881 {
4882 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004883 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004884 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004885
Bram Moolenaard1103582020-08-14 22:44:25 +02004886 generate_ppconst(cctx, ppconst);
4887 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004888 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004889 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004890 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004891 return FAIL;
4892 }
4893 }
4894
4895 return OK;
4896}
4897
4898/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 * * number multiplication
4900 * / number division
4901 * % number modulo
4902 */
4903 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004904compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905{
4906 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004907 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004908 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004910 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004911 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 return FAIL;
4913
4914 /*
4915 * Repeat computing, until no "*", "/" or "%" is following.
4916 */
4917 for (;;)
4918 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004919 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 if (*op != '*' && *op != '/' && *op != '%')
4921 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004922 if (next != NULL)
4923 {
4924 *arg = next_line_from_context(cctx, TRUE);
4925 op = skipwhite(*arg);
4926 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004927
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004928 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004930 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004931 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004933 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004934 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004936 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004937 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004939
4940 if (ppconst->pp_used == ppconst_used + 2
4941 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4942 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004943 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004944 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4945 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4946 varnumber_T res = 0;
4947 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004949 // both are numbers: compute the result
4950 switch (*op)
4951 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004952 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004953 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004954 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004955 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004956 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004957 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004958 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004959 break;
4960 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004961 if (failed)
4962 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004963 tv1->vval.v_number = res;
4964 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004965 }
4966 else
4967 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004968 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004969 generate_two_op(cctx, op);
4970 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 }
4972
4973 return OK;
4974}
4975
4976/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004977 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978 * - number subtraction
4979 * .. string concatenation
4980 */
4981 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004982compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983{
4984 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004985 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004987 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988
4989 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004990 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 return FAIL;
4992
4993 /*
4994 * Repeat computing, until no "+", "-" or ".." is following.
4995 */
4996 for (;;)
4997 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004998 op = may_peek_next_line(cctx, *arg, &next);
4999 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005001 if (op[0] == op[1] && *op != '.' && next)
5002 // Finding "++" or "--" on the next line is a separate command.
5003 // But ".." is concatenation.
5004 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005006 if (next != NULL)
5007 {
5008 *arg = next_line_from_context(cctx, TRUE);
5009 op = skipwhite(*arg);
5010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005012 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005014 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005015 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 }
5017
Bram Moolenaare0de1712020-12-02 17:36:54 +01005018 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005019 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005021 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005022 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 return FAIL;
5024
Bram Moolenaara5565e42020-05-09 15:44:01 +02005025 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005026 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005027 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5028 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5029 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5030 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005032 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5033 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005034
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005035 // concat/subtract/add constant numbers
5036 if (*op == '+')
5037 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5038 else if (*op == '-')
5039 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5040 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005041 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005042 // concatenate constant strings
5043 char_u *s1 = tv1->vval.v_string;
5044 char_u *s2 = tv2->vval.v_string;
5045 size_t len1 = STRLEN(s1);
5046
5047 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5048 if (tv1->vval.v_string == NULL)
5049 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005050 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005051 return FAIL;
5052 }
5053 mch_memmove(tv1->vval.v_string, s1, len1);
5054 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005055 vim_free(s1);
5056 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005057 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005058 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 }
5060 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005061 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005062 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005063 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005064 if (*op == '.')
5065 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005066 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5067 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005068 return FAIL;
5069 generate_instr_drop(cctx, ISN_CONCAT, 1);
5070 }
5071 else
5072 generate_two_op(cctx, op);
5073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005074 }
5075
5076 return OK;
5077}
5078
5079/*
5080 * expr5a == expr5b
5081 * expr5a =~ expr5b
5082 * expr5a != expr5b
5083 * expr5a !~ expr5b
5084 * expr5a > expr5b
5085 * expr5a >= expr5b
5086 * expr5a < expr5b
5087 * expr5a <= expr5b
5088 * expr5a is expr5b
5089 * expr5a isnot expr5b
5090 *
5091 * Produces instructions:
5092 * EVAL expr5a Push result of "expr5a"
5093 * EVAL expr5b Push result of "expr5b"
5094 * COMPARE one of the compare instructions
5095 */
5096 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005097compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005099 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005101 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005104 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105
5106 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005107 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108 return FAIL;
5109
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005110 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005111 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112
5113 /*
5114 * If there is a comparative operator, use it.
5115 */
5116 if (type != EXPR_UNKNOWN)
5117 {
5118 int ic = FALSE; // Default: do not ignore case
5119
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005120 if (next != NULL)
5121 {
5122 *arg = next_line_from_context(cctx, TRUE);
5123 p = skipwhite(*arg);
5124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 if (type_is && (p[len] == '?' || p[len] == '#'))
5126 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005127 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 return FAIL;
5129 }
5130 // extra question mark appended: ignore case
5131 if (p[len] == '?')
5132 {
5133 ic = TRUE;
5134 ++len;
5135 }
5136 // extra '#' appended: match case (ignored)
5137 else if (p[len] == '#')
5138 ++len;
5139 // nothing appended: match case
5140
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005141 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005143 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005144 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145 }
5146
5147 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005148 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005149 return FAIL;
5150
Bram Moolenaara5565e42020-05-09 15:44:01 +02005151 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152 return FAIL;
5153
Bram Moolenaara5565e42020-05-09 15:44:01 +02005154 if (ppconst->pp_used == ppconst_used + 2)
5155 {
5156 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5157 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5158 int ret;
5159
5160 // Both sides are a constant, compute the result now.
5161 // First check for a valid combination of types, this is more
5162 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005163 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005164 ret = FAIL;
5165 else
5166 {
5167 ret = typval_compare(tv1, tv2, type, ic);
5168 tv1->v_type = VAR_BOOL;
5169 tv1->vval.v_number = tv1->vval.v_number
5170 ? VVAL_TRUE : VVAL_FALSE;
5171 clear_tv(tv2);
5172 --ppconst->pp_used;
5173 }
5174 return ret;
5175 }
5176
5177 generate_ppconst(cctx, ppconst);
5178 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 }
5180
5181 return OK;
5182}
5183
Bram Moolenaar7f141552020-05-09 17:35:53 +02005184static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5185
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186/*
5187 * Compile || or &&.
5188 */
5189 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005190compile_and_or(
5191 char_u **arg,
5192 cctx_T *cctx,
5193 char *op,
5194 ppconst_T *ppconst,
5195 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005197 char_u *next;
5198 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199 int opchar = *op;
5200
5201 if (p[0] == opchar && p[1] == opchar)
5202 {
5203 garray_T *instr = &cctx->ctx_instr;
5204 garray_T end_ga;
5205
5206 /*
5207 * Repeat until there is no following "||" or "&&"
5208 */
5209 ga_init2(&end_ga, sizeof(int), 10);
5210 while (p[0] == opchar && p[1] == opchar)
5211 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005212 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005213 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005214 int start_ctx_lnum = cctx->ctx_lnum;
5215 int save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005216 int status;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005217
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005218 if (next != NULL)
5219 {
5220 *arg = next_line_from_context(cctx, TRUE);
5221 p = skipwhite(*arg);
5222 }
5223
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005224 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5225 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005226 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005227 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005228 return FAIL;
5229 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005231 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005232 SOURCING_LNUM = start_lnum;
5233 save_lnum = cctx->ctx_lnum;
5234 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005235
5236 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005237 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005238 {
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005239 // TODO: use ppconst if the value is a constant
5240 generate_ppconst(cctx, ppconst);
5241
5242 // Every part must evaluate to a bool.
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005243 status = bool_on_stack(cctx);
5244 if (status != FAIL)
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005245 status = ga_grow(&end_ga, 1);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005246 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005247 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005248 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005249 {
5250 ga_clear(&end_ga);
5251 return FAIL;
5252 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5255 ++end_ga.ga_len;
5256 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005257 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258
5259 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005260 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005261 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005262 {
5263 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005264 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005265 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005266
Bram Moolenaara5565e42020-05-09 15:44:01 +02005267 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5268 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269 {
5270 ga_clear(&end_ga);
5271 return FAIL;
5272 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005273
5274 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005275 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005276
5277 if (check_ppconst_bool(ppconst) == FAIL)
5278 {
5279 ga_clear(&end_ga);
5280 return FAIL;
5281 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005282 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005283
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005284 // Every part must evaluate to a bool.
5285 if (bool_on_stack(cctx) == FAIL)
5286 {
5287 ga_clear(&end_ga);
5288 return FAIL;
5289 }
5290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291 // Fill in the end label in all jumps.
5292 while (end_ga.ga_len > 0)
5293 {
5294 isn_T *isn;
5295
5296 --end_ga.ga_len;
5297 isn = ((isn_T *)instr->ga_data)
5298 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5299 isn->isn_arg.jump.jump_where = instr->ga_len;
5300 }
5301 ga_clear(&end_ga);
5302 }
5303
5304 return OK;
5305}
5306
5307/*
5308 * expr4a && expr4a && expr4a logical AND
5309 *
5310 * Produces instructions:
5311 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005312 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005313 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005314 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005315 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005316 * EVAL expr4c Push result of "expr4c"
5317 * end:
5318 */
5319 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005320compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005321{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005322 int ppconst_used = ppconst->pp_used;
5323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005325 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005326 return FAIL;
5327
5328 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005329 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005330}
5331
5332/*
5333 * expr3a || expr3b || expr3c logical OR
5334 *
5335 * Produces instructions:
5336 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005337 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005338 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005340 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341 * EVAL expr3c Push result of "expr3c"
5342 * end:
5343 */
5344 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005345compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005347 int ppconst_used = ppconst->pp_used;
5348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005349 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005350 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351 return FAIL;
5352
5353 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005354 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355}
5356
5357/*
5358 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005359 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005360 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 * JUMP_IF_FALSE alt jump if false
5362 * EVAL expr1a
5363 * JUMP_ALWAYS end
5364 * alt: EVAL expr1b
5365 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005366 *
5367 * Toplevel expression: expr2 ?? expr1
5368 * Produces instructions:
5369 * EVAL expr2 Push result of "expr2"
5370 * JUMP_AND_KEEP_IF_TRUE end jump if true
5371 * EVAL expr1
5372 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373 */
5374 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005375compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005376{
5377 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005378 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005379 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005380
Bram Moolenaar3988f642020-08-27 22:43:03 +02005381 // Ignore all kinds of errors when not producing code.
5382 if (cctx->ctx_skip == SKIP_YES)
5383 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005384 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005385 return OK;
5386 }
5387
Bram Moolenaar61a89812020-05-07 16:58:17 +02005388 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005389 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390 return FAIL;
5391
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005392 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393 if (*p == '?')
5394 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005395 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 garray_T *instr = &cctx->ctx_instr;
5397 garray_T *stack = &cctx->ctx_type_stack;
5398 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005399 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005401 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005402 int has_const_expr = FALSE;
5403 int const_value = FALSE;
5404 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005406 if (next != NULL)
5407 {
5408 *arg = next_line_from_context(cctx, TRUE);
5409 p = skipwhite(*arg);
5410 }
5411
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005412 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005413 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005414 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005415 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005416 return FAIL;
5417 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418
Bram Moolenaara5565e42020-05-09 15:44:01 +02005419 if (ppconst->pp_used == ppconst_used + 1)
5420 {
5421 // the condition is a constant, we know whether the ? or the :
5422 // expression is to be evaluated.
5423 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005424 if (op_falsy)
5425 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5426 else
5427 {
5428 int error = FALSE;
5429
5430 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5431 &error);
5432 if (error)
5433 return FAIL;
5434 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005435 cctx->ctx_skip = save_skip == SKIP_YES ||
5436 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5437
5438 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5439 // "left ?? right" and "left" is truthy: produce "left"
5440 generate_ppconst(cctx, ppconst);
5441 else
5442 {
5443 clear_tv(&ppconst->pp_tv[ppconst_used]);
5444 --ppconst->pp_used;
5445 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005446 }
5447 else
5448 {
5449 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005450 if (op_falsy)
5451 end_idx = instr->ga_len;
5452 generate_JUMP(cctx, op_falsy
5453 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5454 if (op_falsy)
5455 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457
5458 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005459 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005460 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005461 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005462 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463
Bram Moolenaara5565e42020-05-09 15:44:01 +02005464 if (!has_const_expr)
5465 {
5466 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005467
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005468 if (!op_falsy)
5469 {
5470 // remember the type and drop it
5471 --stack->ga_len;
5472 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005473
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005474 end_idx = instr->ga_len;
5475 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005476
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005477 // jump here from JUMP_IF_FALSE
5478 isn = ((isn_T *)instr->ga_data) + alt_idx;
5479 isn->isn_arg.jump.jump_where = instr->ga_len;
5480 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005483 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005484 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005485 // Check for the ":".
5486 p = may_peek_next_line(cctx, *arg, &next);
5487 if (*p != ':')
5488 {
5489 emsg(_(e_missing_colon));
5490 return FAIL;
5491 }
5492 if (next != NULL)
5493 {
5494 *arg = next_line_from_context(cctx, TRUE);
5495 p = skipwhite(*arg);
5496 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005497
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005498 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5499 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005500 semsg(_(e_white_space_required_before_and_after_str_at_str),
5501 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005502 return FAIL;
5503 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005504
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005505 // evaluate the third expression
5506 if (has_const_expr)
5507 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005508 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005509 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005510 return FAIL;
5511 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5512 return FAIL;
5513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005514
Bram Moolenaara5565e42020-05-09 15:44:01 +02005515 if (!has_const_expr)
5516 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005517 type_T **typep;
5518
Bram Moolenaara5565e42020-05-09 15:44:01 +02005519 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005520
Bram Moolenaara5565e42020-05-09 15:44:01 +02005521 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005522 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5523 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005524
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005525 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005526 isn = ((isn_T *)instr->ga_data) + end_idx;
5527 isn->isn_arg.jump.jump_where = instr->ga_len;
5528 }
5529
5530 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531 }
5532 return OK;
5533}
5534
5535/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005536 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005537 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5538 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005539 */
5540 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005541compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005542{
5543 ppconst_T ppconst;
5544
5545 CLEAR_FIELD(ppconst);
5546 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5547 {
5548 clear_ppconst(&ppconst);
5549 return FAIL;
5550 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005551 if (is_const != NULL)
5552 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005553 if (generate_ppconst(cctx, &ppconst) == FAIL)
5554 return FAIL;
5555 return OK;
5556}
5557
5558/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005559 * Toplevel expression.
5560 */
5561 static int
5562compile_expr0(char_u **arg, cctx_T *cctx)
5563{
5564 return compile_expr0_ext(arg, cctx, NULL);
5565}
5566
5567/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005568 * Compile "return [expr]".
5569 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005570 */
5571 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005572compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005573{
5574 char_u *p = arg;
5575 garray_T *stack = &cctx->ctx_type_stack;
5576 type_T *stack_type;
5577
5578 if (*p != NUL && *p != '|' && *p != '\n')
5579 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005580 if (legacy)
5581 {
5582 int save_flags = cmdmod.cmod_flags;
5583
5584 generate_LEGACY_EVAL(cctx, p);
5585 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5586 0, cctx, FALSE, FALSE) == FAIL)
5587 return NULL;
5588 cmdmod.cmod_flags |= CMOD_LEGACY;
5589 (void)skip_expr(&p, NULL);
5590 cmdmod.cmod_flags = save_flags;
5591 }
5592 else
5593 {
5594 // compile return argument into instructions
5595 if (compile_expr0(&p, cctx) == FAIL)
5596 return NULL;
5597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005598
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005599 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005600 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005601 // "check_return_type" with uf_ret_type set to &t_unknown is used
5602 // for an inline function without a specified return type. Set the
5603 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005604 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005605 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005606 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5607 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005608 || (!check_return_type
5609 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005610 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005611 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005612 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005613 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005614 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005615 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5616 && stack_type->tt_type != VAR_VOID
5617 && stack_type->tt_type != VAR_UNKNOWN)
5618 {
5619 emsg(_(e_returning_value_in_function_without_return_type));
5620 return NULL;
5621 }
5622 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005623 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005624 return NULL;
5625 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005627 }
5628 else
5629 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005630 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005631 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005632 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5633 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005634 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005635 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005636 return NULL;
5637 }
5638
5639 // No argument, return zero.
5640 generate_PUSHNR(cctx, 0);
5641 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005642
5643 // Undo any command modifiers.
5644 generate_undo_cmdmods(cctx);
5645
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005646 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647 return NULL;
5648
5649 // "return val | endif" is possible
5650 return skipwhite(p);
5651}
5652
5653/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005654 * Get a line from the compilation context, compatible with exarg_T getline().
5655 * Return a pointer to the line in allocated memory.
5656 * Return NULL for end-of-file or some error.
5657 */
5658 static char_u *
5659exarg_getline(
5660 int c UNUSED,
5661 void *cookie,
5662 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005663 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005664{
5665 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005666 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005667
Bram Moolenaar66250c92020-08-20 15:02:42 +02005668 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005669 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005670 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005671 return NULL;
5672 ++cctx->ctx_lnum;
5673 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5674 // Comment lines result in NULL pointers, skip them.
5675 if (p != NULL)
5676 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005677 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005678}
5679
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005680 void
5681fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5682{
5683 eap->getline = exarg_getline;
5684 eap->cookie = cctx;
5685}
5686
Bram Moolenaar04b12692020-05-04 23:24:44 +02005687/*
5688 * Compile a nested :def command.
5689 */
5690 static char_u *
5691compile_nested_function(exarg_T *eap, cctx_T *cctx)
5692{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005693 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005694 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005695 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005696 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005697 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005698 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005699 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005700
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005701 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005702 {
5703 emsg(_(e_cannot_use_bang_with_nested_def));
5704 return NULL;
5705 }
5706
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005707 if (*name_start == '/')
5708 {
5709 name_end = skip_regexp(name_start + 1, '/', TRUE);
5710 if (*name_end == '/')
5711 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005712 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005713 }
5714 if (name_end == name_start || *skipwhite(name_end) != '(')
5715 {
5716 if (!ends_excmd2(name_start, name_end))
5717 {
5718 semsg(_(e_invalid_command_str), eap->cmd);
5719 return NULL;
5720 }
5721
5722 // "def" or "def Name": list functions
5723 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5724 return NULL;
5725 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5726 }
5727
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005728 // Only g:Func() can use a namespace.
5729 if (name_start[1] == ':' && !is_global)
5730 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005731 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005732 return NULL;
5733 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005734 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005735 return NULL;
5736
Bram Moolenaar04b12692020-05-04 23:24:44 +02005737 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005738 fill_exarg_from_cctx(eap, cctx);
5739
Bram Moolenaar04b12692020-05-04 23:24:44 +02005740 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005741 lambda_name = vim_strsave(get_lambda_name());
5742 if (lambda_name == NULL)
5743 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005744 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005745
Bram Moolenaar822ba242020-05-24 23:00:18 +02005746 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005747 {
5748 r = eap->skip ? OK : FAIL;
5749 goto theend;
5750 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005751
5752 // copy over the block scope IDs before compiling
5753 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5754 {
5755 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5756
5757 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5758 if (ufunc->uf_block_ids != NULL)
5759 {
5760 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5761 sizeof(int) * block_depth);
5762 ufunc->uf_block_depth = block_depth;
5763 }
5764 }
5765
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005766 compile_type = COMPILE_TYPE(ufunc);
5767#ifdef FEAT_PROFILE
5768 // If the outer function is profiled, also compile the nested function for
5769 // profiling.
5770 if (cctx->ctx_compile_type == CT_PROFILE)
5771 compile_type = CT_PROFILE;
5772#endif
5773 if (func_needs_compiling(ufunc, compile_type)
5774 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005775 {
5776 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005777 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005778 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005779
Bram Moolenaar648594e2021-07-11 17:55:01 +02005780#ifdef FEAT_PROFILE
5781 // When the outer function is compiled for profiling, the nested function
5782 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005783 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005784 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5785#endif
5786
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005787 if (is_global)
5788 {
5789 char_u *func_name = vim_strnsave(name_start + 2,
5790 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005791
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005792 if (func_name == NULL)
5793 r = FAIL;
5794 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005795 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005796 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005797 lambda_name = NULL;
5798 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005799 }
5800 else
5801 {
5802 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005803 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005804 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005805
Bram Moolenaareef21022020-08-01 22:16:43 +02005806 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005807 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005808 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005809 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005810 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5811 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005812 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005813
5814theend:
5815 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005816 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005817}
5818
5819/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820 * Return the length of an assignment operator, or zero if there isn't one.
5821 */
5822 int
5823assignment_len(char_u *p, int *heredoc)
5824{
5825 if (*p == '=')
5826 {
5827 if (p[1] == '<' && p[2] == '<')
5828 {
5829 *heredoc = TRUE;
5830 return 3;
5831 }
5832 return 1;
5833 }
5834 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5835 return 2;
5836 if (STRNCMP(p, "..=", 3) == 0)
5837 return 3;
5838 return 0;
5839}
5840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005841/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005842 * Generate the load instruction for "name".
5843 */
5844 static void
5845generate_loadvar(
5846 cctx_T *cctx,
5847 assign_dest_T dest,
5848 char_u *name,
5849 lvar_T *lvar,
5850 type_T *type)
5851{
5852 switch (dest)
5853 {
5854 case dest_option:
5855 // TODO: check the option exists
5856 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5857 break;
5858 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005859 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5860 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5861 else
5862 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005863 break;
5864 case dest_buffer:
5865 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5866 break;
5867 case dest_window:
5868 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5869 break;
5870 case dest_tab:
5871 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5872 break;
5873 case dest_script:
5874 compile_load_scriptvar(cctx,
5875 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5876 break;
5877 case dest_env:
5878 // Include $ in the name here
5879 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5880 break;
5881 case dest_reg:
5882 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5883 break;
5884 case dest_vimvar:
5885 generate_LOADV(cctx, name + 2, TRUE);
5886 break;
5887 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005888 if (lvar->lv_from_outer > 0)
5889 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5890 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005891 else
5892 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5893 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005894 case dest_expr:
5895 // list or dict value should already be on the stack.
5896 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005897 }
5898}
5899
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005900/*
5901 * Skip over "[expr]" or ".member".
5902 * Does not check for any errors.
5903 */
5904 static char_u *
5905skip_index(char_u *start)
5906{
5907 char_u *p = start;
5908
5909 if (*p == '[')
5910 {
5911 p = skipwhite(p + 1);
5912 (void)skip_expr(&p, NULL);
5913 p = skipwhite(p);
5914 if (*p == ']')
5915 return p + 1;
5916 return p;
5917 }
5918 // if (*p == '.')
5919 return to_name_end(p + 1, TRUE);
5920}
5921
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005922 void
5923vim9_declare_error(char_u *name)
5924{
5925 char *scope = "";
5926
5927 switch (*name)
5928 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005929 case 'g': scope = _("global"); break;
5930 case 'b': scope = _("buffer"); break;
5931 case 'w': scope = _("window"); break;
5932 case 't': scope = _("tab"); break;
5933 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005934 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005935 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005936 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005937 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005938 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005939 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005940 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005941 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005942 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005943}
5944
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005945/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005946 * For one assignment figure out the type of destination. Return it in "dest".
5947 * When not recognized "dest" is not set.
5948 * For an option "opt_flags" is set.
5949 * For a v:var "vimvaridx" is set.
5950 * "type" is set to the destination type if known, unchanted otherwise.
5951 * Return FAIL if an error message was given.
5952 */
5953 static int
5954get_var_dest(
5955 char_u *name,
5956 assign_dest_T *dest,
5957 int cmdidx,
5958 int *opt_flags,
5959 int *vimvaridx,
5960 type_T **type,
5961 cctx_T *cctx)
5962{
5963 char_u *p;
5964
5965 if (*name == '&')
5966 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005967 int cc;
5968 long numval;
5969 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005970
5971 *dest = dest_option;
5972 if (cmdidx == CMD_final || cmdidx == CMD_const)
5973 {
5974 emsg(_(e_const_option));
5975 return FAIL;
5976 }
5977 p = name;
5978 p = find_option_end(&p, opt_flags);
5979 if (p == NULL)
5980 {
5981 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005982 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005983 return FAIL;
5984 }
5985 cc = *p;
5986 *p = NUL;
5987 opt_type = get_option_value(skip_option_env_lead(name),
5988 &numval, NULL, *opt_flags);
5989 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005990 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005991 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005992 case gov_unknown:
5993 semsg(_(e_unknown_option), name);
5994 return FAIL;
5995 case gov_string:
5996 case gov_hidden_string:
5997 *type = &t_string;
5998 break;
5999 case gov_bool:
6000 case gov_hidden_bool:
6001 *type = &t_bool;
6002 break;
6003 case gov_number:
6004 case gov_hidden_number:
6005 *type = &t_number;
6006 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006007 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006008 }
6009 else if (*name == '$')
6010 {
6011 *dest = dest_env;
6012 *type = &t_string;
6013 }
6014 else if (*name == '@')
6015 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006016 if (name[1] != '@'
6017 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006018 {
6019 emsg_invreg(name[1]);
6020 return FAIL;
6021 }
6022 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006023 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006024 }
6025 else if (STRNCMP(name, "g:", 2) == 0)
6026 {
6027 *dest = dest_global;
6028 }
6029 else if (STRNCMP(name, "b:", 2) == 0)
6030 {
6031 *dest = dest_buffer;
6032 }
6033 else if (STRNCMP(name, "w:", 2) == 0)
6034 {
6035 *dest = dest_window;
6036 }
6037 else if (STRNCMP(name, "t:", 2) == 0)
6038 {
6039 *dest = dest_tab;
6040 }
6041 else if (STRNCMP(name, "v:", 2) == 0)
6042 {
6043 typval_T *vtv;
6044 int di_flags;
6045
6046 *vimvaridx = find_vim_var(name + 2, &di_flags);
6047 if (*vimvaridx < 0)
6048 {
6049 semsg(_(e_variable_not_found_str), name);
6050 return FAIL;
6051 }
6052 // We use the current value of "sandbox" here, is that OK?
6053 if (var_check_ro(di_flags, name, FALSE))
6054 return FAIL;
6055 *dest = dest_vimvar;
6056 vtv = get_vim_var_tv(*vimvaridx);
6057 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6058 }
6059 return OK;
6060}
6061
6062/*
6063 * Generate a STORE instruction for "dest", not being "dest_local".
6064 * Return FAIL when out of memory.
6065 */
6066 static int
6067generate_store_var(
6068 cctx_T *cctx,
6069 assign_dest_T dest,
6070 int opt_flags,
6071 int vimvaridx,
6072 int scriptvar_idx,
6073 int scriptvar_sid,
6074 type_T *type,
6075 char_u *name)
6076{
6077 switch (dest)
6078 {
6079 case dest_option:
6080 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6081 opt_flags);
6082 case dest_global:
6083 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006084 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6085 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006086 case dest_buffer:
6087 // include b: with the name, easier to execute that way
6088 return generate_STORE(cctx, ISN_STOREB, 0, name);
6089 case dest_window:
6090 // include w: with the name, easier to execute that way
6091 return generate_STORE(cctx, ISN_STOREW, 0, name);
6092 case dest_tab:
6093 // include t: with the name, easier to execute that way
6094 return generate_STORE(cctx, ISN_STORET, 0, name);
6095 case dest_env:
6096 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6097 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006098 return generate_STORE(cctx, ISN_STOREREG,
6099 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006100 case dest_vimvar:
6101 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6102 case dest_script:
6103 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006104 // "s:" may be included in the name.
6105 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006106 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006107 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6108 scriptvar_sid, scriptvar_idx, type);
6109 case dest_local:
6110 case dest_expr:
6111 // cannot happen
6112 break;
6113 }
6114 return FAIL;
6115}
6116
Bram Moolenaar752fc692021-01-04 21:57:11 +01006117 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006118generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6119{
6120 if (lhs->lhs_dest != dest_local)
6121 return generate_store_var(cctx, lhs->lhs_dest,
6122 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6123 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6124 lhs->lhs_type, lhs->lhs_name);
6125
6126 if (lhs->lhs_lvar != NULL)
6127 {
6128 garray_T *instr = &cctx->ctx_instr;
6129 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6130
6131 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6132 // ISN_STORENR
6133 if (lhs->lhs_lvar->lv_from_outer == 0
6134 && instr->ga_len == instr_count + 1
6135 && isn->isn_type == ISN_PUSHNR)
6136 {
6137 varnumber_T val = isn->isn_arg.number;
6138 garray_T *stack = &cctx->ctx_type_stack;
6139
6140 isn->isn_type = ISN_STORENR;
6141 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6142 isn->isn_arg.storenr.stnr_val = val;
6143 if (stack->ga_len > 0)
6144 --stack->ga_len;
6145 }
6146 else if (lhs->lhs_lvar->lv_from_outer > 0)
6147 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6148 lhs->lhs_lvar->lv_from_outer);
6149 else
6150 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6151 }
6152 return OK;
6153}
6154
6155 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006156is_decl_command(int cmdidx)
6157{
6158 return cmdidx == CMD_let || cmdidx == CMD_var
6159 || cmdidx == CMD_final || cmdidx == CMD_const;
6160}
6161
6162/*
6163 * Figure out the LHS type and other properties for an assignment or one item
6164 * of ":unlet" with an index.
6165 * Returns OK or FAIL.
6166 */
6167 static int
6168compile_lhs(
6169 char_u *var_start,
6170 lhs_T *lhs,
6171 int cmdidx,
6172 int heredoc,
6173 int oplen,
6174 cctx_T *cctx)
6175{
6176 char_u *var_end;
6177 int is_decl = is_decl_command(cmdidx);
6178
6179 CLEAR_POINTER(lhs);
6180 lhs->lhs_dest = dest_local;
6181 lhs->lhs_vimvaridx = -1;
6182 lhs->lhs_scriptvar_idx = -1;
6183
6184 // "dest_end" is the end of the destination, including "[expr]" or
6185 // ".name".
6186 // "var_end" is the end of the variable/option/etc. name.
6187 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6188 if (*var_start == '@')
6189 var_end = var_start + 2;
6190 else
6191 {
6192 // skip over the leading "&", "&l:", "&g:" and "$"
6193 var_end = skip_option_env_lead(var_start);
6194 var_end = to_name_end(var_end, TRUE);
6195 }
6196
6197 // "a: type" is declaring variable "a" with a type, not dict "a:".
6198 if (is_decl && lhs->lhs_dest_end == var_start + 2
6199 && lhs->lhs_dest_end[-1] == ':')
6200 --lhs->lhs_dest_end;
6201 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6202 --var_end;
6203
6204 // compute the length of the destination without "[expr]" or ".name"
6205 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006206 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006207 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6208 if (lhs->lhs_name == NULL)
6209 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006210
6211 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6212 // Something follows after the variable: "var[idx]" or "var.key".
6213 lhs->lhs_has_index = TRUE;
6214
Bram Moolenaar752fc692021-01-04 21:57:11 +01006215 if (heredoc)
6216 lhs->lhs_type = &t_list_string;
6217 else
6218 lhs->lhs_type = &t_any;
6219
6220 if (cctx->ctx_skip != SKIP_YES)
6221 {
6222 int declare_error = FALSE;
6223
6224 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6225 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6226 &lhs->lhs_type, cctx) == FAIL)
6227 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006228 if (lhs->lhs_dest != dest_local
6229 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006230 {
6231 // Specific kind of variable recognized.
6232 declare_error = is_decl;
6233 }
6234 else
6235 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006236 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006237 if (check_reserved_name(lhs->lhs_name) == FAIL)
6238 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006239
6240 if (lookup_local(var_start, lhs->lhs_varlen,
6241 &lhs->lhs_local_lvar, cctx) == OK)
6242 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6243 else
6244 {
6245 CLEAR_FIELD(lhs->lhs_arg_lvar);
6246 if (arg_exists(var_start, lhs->lhs_varlen,
6247 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6248 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6249 {
6250 if (is_decl)
6251 {
6252 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6253 return FAIL;
6254 }
6255 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6256 }
6257 }
6258 if (lhs->lhs_lvar != NULL)
6259 {
6260 if (is_decl)
6261 {
6262 semsg(_(e_variable_already_declared), lhs->lhs_name);
6263 return FAIL;
6264 }
6265 }
6266 else
6267 {
6268 int script_namespace = lhs->lhs_varlen > 1
6269 && STRNCMP(var_start, "s:", 2) == 0;
6270 int script_var = (script_namespace
6271 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006272 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006273 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006274 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006275 imported_T *import =
6276 find_imported(var_start, lhs->lhs_varlen, cctx);
6277
6278 if (script_namespace || script_var || import != NULL)
6279 {
6280 char_u *rawname = lhs->lhs_name
6281 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6282
6283 if (is_decl)
6284 {
6285 if (script_namespace)
6286 semsg(_(e_cannot_declare_script_variable_in_function),
6287 lhs->lhs_name);
6288 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006289 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006290 lhs->lhs_name);
6291 return FAIL;
6292 }
6293 else if (cctx->ctx_ufunc->uf_script_ctx_version
6294 == SCRIPT_VERSION_VIM9
6295 && script_namespace
6296 && !script_var && import == NULL)
6297 {
6298 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6299 return FAIL;
6300 }
6301
6302 lhs->lhs_dest = dest_script;
6303
6304 // existing script-local variables should have a type
6305 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6306 if (import != NULL)
6307 lhs->lhs_scriptvar_sid = import->imp_sid;
6308 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6309 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006310 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006311 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006312 lhs->lhs_scriptvar_sid, rawname,
6313 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6314 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006315 if (lhs->lhs_scriptvar_idx >= 0)
6316 {
6317 scriptitem_T *si = SCRIPT_ITEM(
6318 lhs->lhs_scriptvar_sid);
6319 svar_T *sv =
6320 ((svar_T *)si->sn_var_vals.ga_data)
6321 + lhs->lhs_scriptvar_idx;
6322 lhs->lhs_type = sv->sv_type;
6323 }
6324 }
6325 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006326 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006327 == FAIL)
6328 return FAIL;
6329 }
6330 }
6331
6332 if (declare_error)
6333 {
6334 vim9_declare_error(lhs->lhs_name);
6335 return FAIL;
6336 }
6337 }
6338
6339 // handle "a:name" as a name, not index "name" on "a"
6340 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6341 var_end = lhs->lhs_dest_end;
6342
6343 if (lhs->lhs_dest != dest_option)
6344 {
6345 if (is_decl && *var_end == ':')
6346 {
6347 char_u *p;
6348
6349 // parse optional type: "let var: type = expr"
6350 if (!VIM_ISWHITE(var_end[1]))
6351 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006352 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006353 return FAIL;
6354 }
6355 p = skipwhite(var_end + 1);
6356 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6357 if (lhs->lhs_type == NULL)
6358 return FAIL;
6359 lhs->lhs_has_type = TRUE;
6360 }
6361 else if (lhs->lhs_lvar != NULL)
6362 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6363 }
6364
Bram Moolenaare42939a2021-04-05 17:11:17 +02006365 if (oplen == 3 && !heredoc
6366 && lhs->lhs_dest != dest_global
6367 && !lhs->lhs_has_index
6368 && lhs->lhs_type->tt_type != VAR_STRING
6369 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006370 {
6371 emsg(_(e_can_only_concatenate_to_string));
6372 return FAIL;
6373 }
6374
6375 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6376 && cctx->ctx_skip != SKIP_YES)
6377 {
6378 if (oplen > 1 && !heredoc)
6379 {
6380 // +=, /=, etc. require an existing variable
6381 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6382 return FAIL;
6383 }
6384 if (!is_decl)
6385 {
6386 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6387 return FAIL;
6388 }
6389
Bram Moolenaar3f327882021-03-17 20:56:38 +01006390 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006391 if ((lhs->lhs_type->tt_type == VAR_FUNC
6392 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006393 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006394 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006395
6396 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006397 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6398 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6399 if (lhs->lhs_lvar == NULL)
6400 return FAIL;
6401 lhs->lhs_new_local = TRUE;
6402 }
6403
6404 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006405 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006406 {
6407 // Something follows after the variable: "var[idx]" or "var.key".
6408 // TODO: should we also handle "->func()" here?
6409 if (is_decl)
6410 {
6411 emsg(_(e_cannot_use_index_when_declaring_variable));
6412 return FAIL;
6413 }
6414
6415 if (var_start[lhs->lhs_varlen] == '['
6416 || var_start[lhs->lhs_varlen] == '.')
6417 {
6418 char_u *after = var_start + lhs->lhs_varlen;
6419 char_u *p;
6420
6421 // Only the last index is used below, if there are others
6422 // before it generate code for the expression. Thus for
6423 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6424 for (;;)
6425 {
6426 p = skip_index(after);
6427 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006428 {
6429 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006430 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006431 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006432 after = p;
6433 }
6434 if (after > var_start + lhs->lhs_varlen)
6435 {
6436 lhs->lhs_varlen = after - var_start;
6437 lhs->lhs_dest = dest_expr;
6438 // We don't know the type before evaluating the expression,
6439 // use "any" until then.
6440 lhs->lhs_type = &t_any;
6441 }
6442
Bram Moolenaar752fc692021-01-04 21:57:11 +01006443 if (lhs->lhs_type->tt_member == NULL)
6444 lhs->lhs_member_type = &t_any;
6445 else
6446 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6447 }
6448 else
6449 {
6450 semsg("Not supported yet: %s", var_start);
6451 return FAIL;
6452 }
6453 }
6454 return OK;
6455}
6456
6457/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006458 * Figure out the LHS and check a few errors.
6459 */
6460 static int
6461compile_assign_lhs(
6462 char_u *var_start,
6463 lhs_T *lhs,
6464 int cmdidx,
6465 int is_decl,
6466 int heredoc,
6467 int oplen,
6468 cctx_T *cctx)
6469{
6470 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6471 return FAIL;
6472
6473 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6474 {
6475 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6476 return FAIL;
6477 }
6478 if (!is_decl && lhs->lhs_lvar != NULL
6479 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6480 {
6481 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6482 return FAIL;
6483 }
6484 return OK;
6485}
6486
6487/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006488 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6489 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006490 */
6491 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006492compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006493 char_u *var_start,
6494 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006495 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006496 cctx_T *cctx)
6497{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006498 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006499 char_u *p;
6500 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006501 int need_white_before = TRUE;
6502 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006503
Bram Moolenaar752fc692021-01-04 21:57:11 +01006504 p = var_start + varlen;
6505 if (*p == '[')
6506 {
6507 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006508 if (*p == ':')
6509 {
6510 // empty first index, push zero
6511 r = generate_PUSHNR(cctx, 0);
6512 need_white_before = FALSE;
6513 }
6514 else
6515 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006516
6517 if (r == OK && *skipwhite(p) == ':')
6518 {
6519 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006520 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006521 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006522 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006523 empty_second = *skipwhite(p + 1) == ']';
6524 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6525 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006526 {
6527 semsg(_(e_white_space_required_before_and_after_str_at_str),
6528 ":", p);
6529 return FAIL;
6530 }
6531 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006532 if (*p == ']')
6533 // empty second index, push "none"
6534 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6535 else
6536 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006537 }
6538
Bram Moolenaar752fc692021-01-04 21:57:11 +01006539 if (r == OK && *skipwhite(p) != ']')
6540 {
6541 // this should not happen
6542 emsg(_(e_missbrac));
6543 r = FAIL;
6544 }
6545 }
6546 else // if (*p == '.')
6547 {
6548 char_u *key_end = to_name_end(p + 1, TRUE);
6549 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6550
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006551 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006552 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006553 return r;
6554}
6555
6556/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006557 * For a LHS with an index, load the variable to be indexed.
6558 */
6559 static int
6560compile_load_lhs(
6561 lhs_T *lhs,
6562 char_u *var_start,
6563 type_T *rhs_type,
6564 cctx_T *cctx)
6565{
6566 if (lhs->lhs_dest == dest_expr)
6567 {
6568 size_t varlen = lhs->lhs_varlen;
6569 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006570 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006571 char_u *p = var_start;
6572 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006573 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006574
Bram Moolenaare97976b2021-08-01 13:17:17 +02006575 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6576 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006577 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006578 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6579 res = compile_expr0(&p, cctx);
6580 var_start[varlen] = c;
6581 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6582 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006583 {
6584 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006585 if (res != FAIL)
6586 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006587 return FAIL;
6588 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006589
6590 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6591 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6592 // now we can properly check the type
6593 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6594 && rhs_type != &t_void
6595 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6596 FALSE, FALSE) == FAIL)
6597 return FAIL;
6598 }
6599 else
6600 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6601 lhs->lhs_lvar, lhs->lhs_type);
6602 return OK;
6603}
6604
6605/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006606 * Produce code for loading "lhs" and also take care of an index.
6607 * Return OK/FAIL.
6608 */
6609 static int
6610compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6611{
6612 compile_load_lhs(lhs, var_start, NULL, cctx);
6613
6614 if (lhs->lhs_has_index)
6615 {
6616 int range = FALSE;
6617
6618 // Get member from list or dict. First compile the
6619 // index value.
6620 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6621 return FAIL;
6622 if (range)
6623 {
6624 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6625 var_start);
6626 return FAIL;
6627 }
6628
6629 // Get the member.
6630 if (compile_member(FALSE, cctx) == FAIL)
6631 return FAIL;
6632 }
6633 return OK;
6634}
6635
6636/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006637 * Assignment to a list or dict member, or ":unlet" for the item, using the
6638 * information in "lhs".
6639 * Returns OK or FAIL.
6640 */
6641 static int
6642compile_assign_unlet(
6643 char_u *var_start,
6644 lhs_T *lhs,
6645 int is_assign,
6646 type_T *rhs_type,
6647 cctx_T *cctx)
6648{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006649 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006650 garray_T *stack = &cctx->ctx_type_stack;
6651 int range = FALSE;
6652
Bram Moolenaar68452172021-04-12 21:21:02 +02006653 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006654 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006655 if (is_assign && range && lhs->lhs_type != &t_blob
6656 && lhs->lhs_type != &t_any)
6657 {
6658 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6659 return FAIL;
6660 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006661
6662 if (lhs->lhs_type == &t_any)
6663 {
6664 // Index on variable of unknown type: check at runtime.
6665 dest_type = VAR_ANY;
6666 }
6667 else
6668 {
6669 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006670 if (dest_type == VAR_DICT && range)
6671 {
6672 emsg(e_cannot_use_range_with_dictionary);
6673 return FAIL;
6674 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006675 if (dest_type == VAR_DICT
6676 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006677 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006678 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006679 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006680 type_T *type;
6681
6682 if (range)
6683 {
6684 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6685 if (need_type(type, &t_number,
6686 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006687 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006688 }
6689 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006690 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006691 && need_type(type, &t_number,
6692 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006693 return FAIL;
6694 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006695 }
6696
6697 // Load the dict or list. On the stack we then have:
6698 // - value (for assignment, not for :unlet)
6699 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006700 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006701 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006702 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6703 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006704
Bram Moolenaar68452172021-04-12 21:21:02 +02006705 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6706 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006707 {
6708 if (is_assign)
6709 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006710 if (range)
6711 {
6712 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6713 return FAIL;
6714 }
6715 else
6716 {
6717 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006718
Bram Moolenaar68452172021-04-12 21:21:02 +02006719 if (isn == NULL)
6720 return FAIL;
6721 isn->isn_arg.vartype = dest_type;
6722 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006723 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006724 else if (range)
6725 {
6726 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6727 return FAIL;
6728 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006729 else
6730 {
6731 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6732 return FAIL;
6733 }
6734 }
6735 else
6736 {
6737 emsg(_(e_indexable_type_required));
6738 return FAIL;
6739 }
6740
6741 return OK;
6742}
6743
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006744/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006745 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006746 * "let name"
6747 * "var name = expr"
6748 * "final name = expr"
6749 * "const name = expr"
6750 * "name = expr"
6751 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006752 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006753 * Return NULL for an error.
6754 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006755 */
6756 static char_u *
6757compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6758{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006759 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006761 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 char_u *ret = NULL;
6763 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006764 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006765 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006766 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006768 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770 int oplen = 0;
6771 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006772 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006773 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006774 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006775 int is_decl = is_decl_command(cmdidx);
6776 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006777 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006779 // Skip over the "var" or "[var, var]" to get to any "=".
6780 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6781 if (p == NULL)
6782 return *arg == '[' ? arg : NULL;
6783
6784 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006785 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006786 // TODO: should we allow this, and figure out type inference from list
6787 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006788 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789 return NULL;
6790 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006791 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 sp = p;
6794 p = skipwhite(p);
6795 op = p;
6796 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006797
6798 if (var_count > 0 && oplen == 0)
6799 // can be something like "[1, 2]->func()"
6800 return arg;
6801
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006802 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006804 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006805 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006806 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006807 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6808 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006809 if (VIM_ISWHITE(eap->cmd[2]))
6810 {
6811 semsg(_(e_no_white_space_allowed_after_str_str),
6812 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6813 return NULL;
6814 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006815 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6816 oplen = 2;
6817 incdec = TRUE;
6818 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820 if (heredoc)
6821 {
6822 list_T *l;
6823 listitem_T *li;
6824
6825 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006826 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006827 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006828 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006829 if (l == NULL)
6830 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831
Bram Moolenaar078269b2020-09-21 20:35:55 +02006832 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006833 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006834 // Push each line and the create the list.
6835 FOR_ALL_LIST_ITEMS(l, li)
6836 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006837 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02006838 li->li_tv.vval.v_string = NULL;
6839 }
6840 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006841 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006842 list_free(l);
6843 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006844 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006846 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006848 char_u *wp;
6849
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006850 // for "[var, var] = expr" evaluate the expression here, loop over the
6851 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006852 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006853
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006854 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006855 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006856 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006857 if (compile_expr0(&p, cctx) == FAIL)
6858 return NULL;
6859 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006861 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006862 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006863 type_T *stacktype;
6864
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006865 stacktype = stack->ga_len == 0 ? &t_void
6866 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006867 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006869 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006870 goto theend;
6871 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006872 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006873 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006874 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006875 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006876 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6877 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006878 if (stacktype->tt_member != NULL)
6879 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006880 }
6881 }
6882
6883 /*
6884 * Loop over variables in "[var, var] = expr".
6885 * For "var = expr" and "let var: type" this is done only once.
6886 */
6887 if (var_count > 0)
6888 var_start = skipwhite(arg + 1); // skip over the "["
6889 else
6890 var_start = arg;
6891 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6892 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006893 int instr_count = -1;
6894 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006895
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006896 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6897 {
6898 // Ignore underscore in "[a, _, b] = list".
6899 if (var_count > 0)
6900 {
6901 var_start = skipwhite(var_start + 2);
6902 continue;
6903 }
6904 emsg(_(e_cannot_use_underscore_here));
6905 goto theend;
6906 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006907 vim_free(lhs.lhs_name);
6908
6909 /*
6910 * Figure out the LHS type and other properties.
6911 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006912 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6913 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006914 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02006915 if (heredoc)
6916 {
6917 SOURCING_LNUM = start_lnum;
6918 if (lhs.lhs_has_type
6919 && need_type(&t_list_string, lhs.lhs_type,
6920 -1, 0, cctx, FALSE, FALSE) == FAIL)
6921 goto theend;
6922 }
6923 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006924 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006925 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006926 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006927 if (oplen > 0 && var_count == 0)
6928 {
6929 // skip over the "=" and the expression
6930 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006931 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006932 }
6933 }
6934 else if (oplen > 0)
6935 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006936 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006937 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006938
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006939 // for "+=", "*=", "..=" etc. first load the current value
6940 if (*op != '='
6941 && compile_load_lhs_with_index(&lhs, var_start,
6942 cctx) == FAIL)
6943 goto theend;
6944
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006945 // For "var = expr" evaluate the expression.
6946 if (var_count == 0)
6947 {
6948 int r;
6949
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006950 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006951 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006952 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006953 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006954 r = generate_PUSHNR(cctx, 1);
6955 }
6956 else
6957 {
6958 // Temporarily hide the new local variable here, it is
6959 // not available to this expression.
6960 if (lhs.lhs_new_local)
6961 --cctx->ctx_locals.ga_len;
6962 wp = op + oplen;
6963 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6964 {
6965 if (lhs.lhs_new_local)
6966 ++cctx->ctx_locals.ga_len;
6967 goto theend;
6968 }
6969 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006970 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006971 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006972 if (r == FAIL)
6973 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006974 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006975 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006976 else if (semicolon && var_idx == var_count - 1)
6977 {
6978 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006979 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006980 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6981 goto theend;
6982 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006983 else
6984 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006985 // For "[var, var] = expr" get the "var_idx" item from the
6986 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006987 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006988 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006989 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006990
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006991 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006992 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006993 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006994 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006995 if ((rhs_type->tt_type == VAR_FUNC
6996 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006997 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006998 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006999 goto theend;
7000
Bram Moolenaar752fc692021-01-04 21:57:11 +01007001 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007002 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007003 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007004 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007005 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007006 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007007 }
7008 else
7009 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007010 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007011 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007012 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007013 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007014 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007015 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007016 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007017 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007018 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007019 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007020 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007021 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007022 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007023 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007024 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007025 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007026
Bram Moolenaar77709b12021-04-03 21:01:01 +02007027 // Without operator check type here, otherwise below.
7028 // Use the line number of the assignment.
7029 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007030 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7031 where.wt_variable = var_count > 0;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007032 if (lhs.lhs_has_index)
7033 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007034 if (need_type_where(rhs_type, use_type, -1, where,
7035 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007036 goto theend;
7037 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007038 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007039 else
7040 {
7041 type_T *lhs_type = lhs.lhs_member_type;
7042
7043 // Special case: assigning to @# can use a number or a
7044 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007045 // Also: can assign a number to a float.
7046 if ((lhs_type == &t_number_or_string
7047 || lhs_type == &t_float)
7048 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007049 lhs_type = &t_number;
7050 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007051 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007052 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007053 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007055 else if (cmdidx == CMD_final)
7056 {
7057 emsg(_(e_final_requires_a_value));
7058 goto theend;
7059 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007060 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007062 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007063 goto theend;
7064 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007065 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007066 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007067 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007068 goto theend;
7069 }
7070 else
7071 {
7072 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007073 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007074 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007075 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007076 {
7077 case VAR_BOOL:
7078 generate_PUSHBOOL(cctx, VVAL_FALSE);
7079 break;
7080 case VAR_FLOAT:
7081#ifdef FEAT_FLOAT
7082 generate_PUSHF(cctx, 0.0);
7083#endif
7084 break;
7085 case VAR_STRING:
7086 generate_PUSHS(cctx, NULL);
7087 break;
7088 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007089 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007090 break;
7091 case VAR_FUNC:
7092 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7093 break;
7094 case VAR_LIST:
7095 generate_NEWLIST(cctx, 0);
7096 break;
7097 case VAR_DICT:
7098 generate_NEWDICT(cctx, 0);
7099 break;
7100 case VAR_JOB:
7101 generate_PUSHJOB(cctx, NULL);
7102 break;
7103 case VAR_CHANNEL:
7104 generate_PUSHCHANNEL(cctx, NULL);
7105 break;
7106 case VAR_NUMBER:
7107 case VAR_UNKNOWN:
7108 case VAR_ANY:
7109 case VAR_PARTIAL:
7110 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007111 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007112 case VAR_SPECIAL: // cannot happen
7113 generate_PUSHNR(cctx, 0);
7114 break;
7115 }
7116 }
7117 if (var_count == 0)
7118 end = p;
7119 }
7120
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007121 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007122 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007123 break;
7124
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007125 if (oplen > 0 && *op != '=')
7126 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007127 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007128 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007129
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007130 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007131 {
7132 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7133 goto theend;
7134 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007135 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007136 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007137 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007138 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7139 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007140#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007141 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007142 !(expected == &t_float && (stacktype == &t_number
7143 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007144#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007145 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007146 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007147 goto theend;
7148 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007149
7150 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007151 {
7152 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7153 goto theend;
7154 }
7155 else if (*op == '+')
7156 {
7157 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007158 operator_type(lhs.lhs_member_type, stacktype),
7159 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007160 goto theend;
7161 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007162 else if (generate_two_op(cctx, op) == FAIL)
7163 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007165
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007166 // Use the line number of the assignment for store instruction.
7167 save_lnum = cctx->ctx_lnum;
7168 cctx->ctx_lnum = start_lnum - 1;
7169
Bram Moolenaar752fc692021-01-04 21:57:11 +01007170 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007171 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007172 // Use the info in "lhs" to store the value at the index in the
7173 // list or dict.
7174 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7175 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007176 {
7177 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007178 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007179 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007180 }
7181 else
7182 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007183 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007184 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007185 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007186 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007187 generate_LOCKCONST(cctx);
7188
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007189 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007190 && (lhs.lhs_type->tt_type == VAR_DICT
7191 || lhs.lhs_type->tt_type == VAR_LIST)
7192 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007193 && !(lhs.lhs_type->tt_member == &t_any
7194 && oplen > 0
7195 && rhs_type != NULL
7196 && rhs_type->tt_type == lhs.lhs_type->tt_type
7197 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007198 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007199 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007200 // also in legacy script. Not for "list<any> = val", then the
7201 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007202 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007203
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007204 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007205 {
7206 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007207 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007208 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007209 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007210 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007211
7212 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007213 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007215
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007216 // For "[var, var] = expr" drop the "expr" value.
7217 // Also for "[var, var; _] = expr".
7218 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007219 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007220 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007221 goto theend;
7222 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007223
Bram Moolenaarb2097502020-07-19 17:17:02 +02007224 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225
7226theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007227 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228 return ret;
7229}
7230
7231/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007232 * Check for an assignment at "eap->cmd", compile it if found.
7233 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7234 */
7235 static int
7236may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7237{
7238 char_u *pskip;
7239 char_u *p;
7240
7241 // Assuming the command starts with a variable or function name,
7242 // find what follows.
7243 // Skip over "var.member", "var[idx]" and the like.
7244 // Also "&opt = val", "$ENV = val" and "@r = val".
7245 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7246 ? eap->cmd + 1 : eap->cmd;
7247 p = to_name_end(pskip, TRUE);
7248 if (p > eap->cmd && *p != NUL)
7249 {
7250 char_u *var_end;
7251 int oplen;
7252 int heredoc;
7253
7254 if (eap->cmd[0] == '@')
7255 var_end = eap->cmd + 2;
7256 else
7257 var_end = find_name_end(pskip, NULL, NULL,
7258 FNE_CHECK_START | FNE_INCL_BR);
7259 oplen = assignment_len(skipwhite(var_end), &heredoc);
7260 if (oplen > 0)
7261 {
7262 size_t len = p - eap->cmd;
7263
7264 // Recognize an assignment if we recognize the variable
7265 // name:
7266 // "g:var = expr"
7267 // "local = expr" where "local" is a local var.
7268 // "script = expr" where "script" is a script-local var.
7269 // "import = expr" where "import" is an imported var
7270 // "&opt = expr"
7271 // "$ENV = expr"
7272 // "@r = expr"
7273 if (*eap->cmd == '&'
7274 || *eap->cmd == '$'
7275 || *eap->cmd == '@'
7276 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007277 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007278 {
7279 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7280 if (*line == NULL || *line == eap->cmd)
7281 return FAIL;
7282 return OK;
7283 }
7284 }
7285 }
7286
7287 if (*eap->cmd == '[')
7288 {
7289 // [var, var] = expr
7290 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7291 if (*line == NULL)
7292 return FAIL;
7293 if (*line != eap->cmd)
7294 return OK;
7295 }
7296 return NOTDONE;
7297}
7298
7299/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007300 * Check if "name" can be "unlet".
7301 */
7302 int
7303check_vim9_unlet(char_u *name)
7304{
7305 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7306 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007307 // "unlet s:var" is allowed in legacy script.
7308 if (*name == 's' && !script_is_vim9())
7309 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007310 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007311 return FAIL;
7312 }
7313 return OK;
7314}
7315
7316/*
7317 * Callback passed to ex_unletlock().
7318 */
7319 static int
7320compile_unlet(
7321 lval_T *lvp,
7322 char_u *name_end,
7323 exarg_T *eap,
7324 int deep UNUSED,
7325 void *coookie)
7326{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007327 cctx_T *cctx = coookie;
7328 char_u *p = lvp->ll_name;
7329 int cc = *name_end;
7330 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007331
Bram Moolenaar752fc692021-01-04 21:57:11 +01007332 if (cctx->ctx_skip == SKIP_YES)
7333 return OK;
7334
7335 *name_end = NUL;
7336 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007337 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007338 // :unlet $ENV_VAR
7339 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7340 }
7341 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7342 {
7343 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007344
Bram Moolenaar752fc692021-01-04 21:57:11 +01007345 // This is similar to assigning: lookup the list/dict, compile the
7346 // idx/key. Then instead of storing the value unlet the item.
7347 // unlet {list}[idx]
7348 // unlet {dict}[key] dict.key
7349 //
7350 // Figure out the LHS type and other properties.
7351 //
7352 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7353
7354 // : unlet an indexed item
7355 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007356 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007357 iemsg("called compile_lhs() without an index");
7358 ret = FAIL;
7359 }
7360 else
7361 {
7362 // Use the info in "lhs" to unlet the item at the index in the
7363 // list or dict.
7364 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007365 }
7366
Bram Moolenaar752fc692021-01-04 21:57:11 +01007367 vim_free(lhs.lhs_name);
7368 }
7369 else if (check_vim9_unlet(p) == FAIL)
7370 {
7371 ret = FAIL;
7372 }
7373 else
7374 {
7375 // Normal name. Only supports g:, w:, t: and b: namespaces.
7376 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007377 }
7378
Bram Moolenaar752fc692021-01-04 21:57:11 +01007379 *name_end = cc;
7380 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007381}
7382
7383/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007384 * Callback passed to ex_unletlock().
7385 */
7386 static int
7387compile_lock_unlock(
7388 lval_T *lvp,
7389 char_u *name_end,
7390 exarg_T *eap,
7391 int deep UNUSED,
7392 void *coookie)
7393{
7394 cctx_T *cctx = coookie;
7395 int cc = *name_end;
7396 char_u *p = lvp->ll_name;
7397 int ret = OK;
7398 size_t len;
7399 char_u *buf;
7400
7401 if (cctx->ctx_skip == SKIP_YES)
7402 return OK;
7403
7404 // Cannot use :lockvar and :unlockvar on local variables.
7405 if (p[1] != ':')
7406 {
7407 char_u *end = skip_var_one(p, FALSE);
7408
7409 if (lookup_local(p, end - p, NULL, cctx) == OK)
7410 {
7411 emsg(_(e_cannot_lock_unlock_local_variable));
7412 return FAIL;
7413 }
7414 }
7415
7416 // Checking is done at runtime.
7417 *name_end = NUL;
7418 len = name_end - p + 20;
7419 buf = alloc(len);
7420 if (buf == NULL)
7421 ret = FAIL;
7422 else
7423 {
7424 vim_snprintf((char *)buf, len, "%s %s",
7425 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7426 p);
7427 ret = generate_EXEC(cctx, buf);
7428
7429 vim_free(buf);
7430 *name_end = cc;
7431 }
7432 return ret;
7433}
7434
7435/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007436 * compile "unlet var", "lock var" and "unlock var"
7437 * "arg" points to "var".
7438 */
7439 static char_u *
7440compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7441{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007442 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7443 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7444 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007445 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7446}
7447
7448/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7450 */
7451 static int
7452compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7453{
7454 garray_T *instr = &cctx->ctx_instr;
7455 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7456
7457 if (endlabel == NULL)
7458 return FAIL;
7459 endlabel->el_next = *el;
7460 *el = endlabel;
7461 endlabel->el_end_label = instr->ga_len;
7462
7463 generate_JUMP(cctx, when, 0);
7464 return OK;
7465}
7466
7467 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007468compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469{
7470 garray_T *instr = &cctx->ctx_instr;
7471
7472 while (*el != NULL)
7473 {
7474 endlabel_T *cur = (*el);
7475 isn_T *isn;
7476
7477 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007478 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 *el = cur->el_next;
7480 vim_free(cur);
7481 }
7482}
7483
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007484 static void
7485compile_free_jump_to_end(endlabel_T **el)
7486{
7487 while (*el != NULL)
7488 {
7489 endlabel_T *cur = (*el);
7490
7491 *el = cur->el_next;
7492 vim_free(cur);
7493 }
7494}
7495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496/*
7497 * Create a new scope and set up the generic items.
7498 */
7499 static scope_T *
7500new_scope(cctx_T *cctx, scopetype_T type)
7501{
7502 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7503
7504 if (scope == NULL)
7505 return NULL;
7506 scope->se_outer = cctx->ctx_scope;
7507 cctx->ctx_scope = scope;
7508 scope->se_type = type;
7509 scope->se_local_count = cctx->ctx_locals.ga_len;
7510 return scope;
7511}
7512
7513/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007514 * Free the current scope and go back to the outer scope.
7515 */
7516 static void
7517drop_scope(cctx_T *cctx)
7518{
7519 scope_T *scope = cctx->ctx_scope;
7520
7521 if (scope == NULL)
7522 {
7523 iemsg("calling drop_scope() without a scope");
7524 return;
7525 }
7526 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007527 switch (scope->se_type)
7528 {
7529 case IF_SCOPE:
7530 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7531 case FOR_SCOPE:
7532 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7533 case WHILE_SCOPE:
7534 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7535 case TRY_SCOPE:
7536 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7537 case NO_SCOPE:
7538 case BLOCK_SCOPE:
7539 break;
7540 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007541 vim_free(scope);
7542}
7543
7544/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007545 * compile "if expr"
7546 *
7547 * "if expr" Produces instructions:
7548 * EVAL expr Push result of "expr"
7549 * JUMP_IF_FALSE end
7550 * ... body ...
7551 * end:
7552 *
7553 * "if expr | else" Produces instructions:
7554 * EVAL expr Push result of "expr"
7555 * JUMP_IF_FALSE else
7556 * ... body ...
7557 * JUMP_ALWAYS end
7558 * else:
7559 * ... body ...
7560 * end:
7561 *
7562 * "if expr1 | elseif expr2 | else" Produces instructions:
7563 * EVAL expr Push result of "expr"
7564 * JUMP_IF_FALSE elseif
7565 * ... body ...
7566 * JUMP_ALWAYS end
7567 * elseif:
7568 * EVAL expr Push result of "expr"
7569 * JUMP_IF_FALSE else
7570 * ... body ...
7571 * JUMP_ALWAYS end
7572 * else:
7573 * ... body ...
7574 * end:
7575 */
7576 static char_u *
7577compile_if(char_u *arg, cctx_T *cctx)
7578{
7579 char_u *p = arg;
7580 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007581 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007582 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007583 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007584 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585
Bram Moolenaara5565e42020-05-09 15:44:01 +02007586 CLEAR_FIELD(ppconst);
7587 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007588 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007589 clear_ppconst(&ppconst);
7590 return NULL;
7591 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007592 if (!ends_excmd2(arg, skipwhite(p)))
7593 {
7594 semsg(_(e_trailing_arg), p);
7595 return NULL;
7596 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007597 if (cctx->ctx_skip == SKIP_YES)
7598 clear_ppconst(&ppconst);
7599 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007600 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007601 int error = FALSE;
7602 int v;
7603
Bram Moolenaara5565e42020-05-09 15:44:01 +02007604 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007605 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007606 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007607 if (error)
7608 return NULL;
7609 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007610 }
7611 else
7612 {
7613 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007614 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007615 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007616 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007617 if (bool_on_stack(cctx) == FAIL)
7618 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007619 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007620
Bram Moolenaara91a7132021-03-25 21:12:15 +01007621 // CMDMOD_REV must come before the jump
7622 generate_undo_cmdmods(cctx);
7623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624 scope = new_scope(cctx, IF_SCOPE);
7625 if (scope == NULL)
7626 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007627 scope->se_skip_save = skip_save;
7628 // "is_had_return" will be reset if any block does not end in :return
7629 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007630
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007631 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007632 {
7633 // "where" is set when ":elseif", "else" or ":endif" is found
7634 scope->se_u.se_if.is_if_label = instr->ga_len;
7635 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7636 }
7637 else
7638 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007639
Bram Moolenaarced68a02021-01-24 17:53:47 +01007640#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007641 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007642 && skip_save != SKIP_YES)
7643 {
7644 // generated a profile start, need to generate a profile end, since it
7645 // won't be done after returning
7646 cctx->ctx_skip = SKIP_NOT;
7647 generate_instr(cctx, ISN_PROF_END);
7648 cctx->ctx_skip = SKIP_YES;
7649 }
7650#endif
7651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007652 return p;
7653}
7654
7655 static char_u *
7656compile_elseif(char_u *arg, cctx_T *cctx)
7657{
7658 char_u *p = arg;
7659 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007660 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007661 isn_T *isn;
7662 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007663 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007664 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007665
7666 if (scope == NULL || scope->se_type != IF_SCOPE)
7667 {
7668 emsg(_(e_elseif_without_if));
7669 return NULL;
7670 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007671 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007672 if (!cctx->ctx_had_return)
7673 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007674
Bram Moolenaarced68a02021-01-24 17:53:47 +01007675 if (cctx->ctx_skip == SKIP_NOT)
7676 {
7677 // previous block was executed, this one and following will not
7678 cctx->ctx_skip = SKIP_YES;
7679 scope->se_u.se_if.is_seen_skip_not = TRUE;
7680 }
7681 if (scope->se_u.se_if.is_seen_skip_not)
7682 {
7683 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007684 // Do not count the "elseif" for profiling and cmdmod
7685 instr->ga_len = current_instr_idx(cctx);
7686
Bram Moolenaarced68a02021-01-24 17:53:47 +01007687 skip_expr_cctx(&p, cctx);
7688 return p;
7689 }
7690
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007691 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007692 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007693 int moved_cmdmod = FALSE;
7694
7695 // Move any CMDMOD instruction to after the jump
7696 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7697 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007698 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007699 return NULL;
7700 ((isn_T *)instr->ga_data)[instr->ga_len] =
7701 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7702 --instr->ga_len;
7703 moved_cmdmod = TRUE;
7704 }
7705
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007706 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007707 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007708 return NULL;
7709 // previous "if" or "elseif" jumps here
7710 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7711 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007712 if (moved_cmdmod)
7713 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007714 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007716 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007717 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007718 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007719 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007720 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007721#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007722 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007723 {
7724 // the previous block was skipped, need to profile this line
7725 generate_instr(cctx, ISN_PROF_START);
7726 instr_count = instr->ga_len;
7727 }
7728#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007729 if (cctx->ctx_compile_type == CT_DEBUG)
7730 {
7731 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007732 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007733 instr_count = instr->ga_len;
7734 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007735 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007736 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007737 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007738 clear_ppconst(&ppconst);
7739 return NULL;
7740 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007741 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007742 if (!ends_excmd2(arg, skipwhite(p)))
7743 {
7744 semsg(_(e_trailing_arg), p);
7745 return NULL;
7746 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007747 if (scope->se_skip_save == SKIP_YES)
7748 clear_ppconst(&ppconst);
7749 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007750 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007751 int error = FALSE;
7752 int v;
7753
Bram Moolenaar7f141552020-05-09 17:35:53 +02007754 // The expression results in a constant.
7755 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007756 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7757 if (error)
7758 return NULL;
7759 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007760 clear_ppconst(&ppconst);
7761 scope->se_u.se_if.is_if_label = -1;
7762 }
7763 else
7764 {
7765 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007766 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007767 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007768 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007769 if (bool_on_stack(cctx) == FAIL)
7770 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771
Bram Moolenaara91a7132021-03-25 21:12:15 +01007772 // CMDMOD_REV must come before the jump
7773 generate_undo_cmdmods(cctx);
7774
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007775 // "where" is set when ":elseif", "else" or ":endif" is found
7776 scope->se_u.se_if.is_if_label = instr->ga_len;
7777 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7778 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779
7780 return p;
7781}
7782
7783 static char_u *
7784compile_else(char_u *arg, cctx_T *cctx)
7785{
7786 char_u *p = arg;
7787 garray_T *instr = &cctx->ctx_instr;
7788 isn_T *isn;
7789 scope_T *scope = cctx->ctx_scope;
7790
7791 if (scope == NULL || scope->se_type != IF_SCOPE)
7792 {
7793 emsg(_(e_else_without_if));
7794 return NULL;
7795 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007796 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007797 if (!cctx->ctx_had_return)
7798 scope->se_u.se_if.is_had_return = FALSE;
7799 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007800
Bram Moolenaarced68a02021-01-24 17:53:47 +01007801#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007802 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007803 {
7804 if (cctx->ctx_skip == SKIP_NOT
7805 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7806 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007807 // the previous block was executed, do not count "else" for
7808 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007809 --instr->ga_len;
7810 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7811 {
7812 // the previous block was not executed, this one will, do count the
7813 // "else" for profiling
7814 cctx->ctx_skip = SKIP_NOT;
7815 generate_instr(cctx, ISN_PROF_END);
7816 generate_instr(cctx, ISN_PROF_START);
7817 cctx->ctx_skip = SKIP_YES;
7818 }
7819 }
7820#endif
7821
7822 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007823 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007824 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007825 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007826 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007827 if (!cctx->ctx_had_return
7828 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7829 JUMP_ALWAYS, cctx) == FAIL)
7830 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007831 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007832
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007833 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007834 {
7835 if (scope->se_u.se_if.is_if_label >= 0)
7836 {
7837 // previous "if" or "elseif" jumps here
7838 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7839 isn->isn_arg.jump.jump_where = instr->ga_len;
7840 scope->se_u.se_if.is_if_label = -1;
7841 }
7842 }
7843
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007844 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007845 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007847
7848 return p;
7849}
7850
7851 static char_u *
7852compile_endif(char_u *arg, cctx_T *cctx)
7853{
7854 scope_T *scope = cctx->ctx_scope;
7855 ifscope_T *ifscope;
7856 garray_T *instr = &cctx->ctx_instr;
7857 isn_T *isn;
7858
Bram Moolenaarfa984412021-03-25 22:15:28 +01007859 if (misplaced_cmdmod(cctx))
7860 return NULL;
7861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862 if (scope == NULL || scope->se_type != IF_SCOPE)
7863 {
7864 emsg(_(e_endif_without_if));
7865 return NULL;
7866 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007867 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007868 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007869 if (!cctx->ctx_had_return)
7870 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007871
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007872 if (scope->se_u.se_if.is_if_label >= 0)
7873 {
7874 // previous "if" or "elseif" jumps here
7875 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7876 isn->isn_arg.jump.jump_where = instr->ga_len;
7877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007879 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007880
7881#ifdef FEAT_PROFILE
7882 // even when skipping we count the endif as executed, unless the block it's
7883 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007884 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007885 && scope->se_skip_save != SKIP_YES)
7886 {
7887 cctx->ctx_skip = SKIP_NOT;
7888 generate_instr(cctx, ISN_PROF_START);
7889 }
7890#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007891 cctx->ctx_skip = scope->se_skip_save;
7892
7893 // If all the blocks end in :return and there is an :else then the
7894 // had_return flag is set.
7895 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007896
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007897 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007898 return arg;
7899}
7900
7901/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007902 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007903 *
7904 * Produces instructions:
7905 * PUSHNR -1
7906 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007907 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7909 * - if beyond end, jump to "end"
7910 * - otherwise get item from list and push it
7911 * STORE var Store item in "var"
7912 * ... body ...
7913 * JUMP top Jump back to repeat
7914 * end: DROP Drop the result of "expr"
7915 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007916 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7917 * UNPACK 2 Split item in 2
7918 * STORE var1 Store item in "var1"
7919 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007920 */
7921 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007922compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007923{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007924 char_u *arg;
7925 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007926 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007927 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007928 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007929 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007930 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007931 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007933 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007934 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007936 lvar_T *loop_lvar; // loop iteration variable
7937 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007938 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007939 type_T *item_type = &t_any;
7940 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007941 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007942
Bram Moolenaar792f7862020-11-23 08:31:18 +01007943 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007944 if (p == NULL)
7945 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007946 if (var_count == 0)
7947 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007948 else
7949 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950
7951 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007952 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007953 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7954 return NULL;
7955 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007957 if (*p == ':' && wp != p)
7958 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7959 else
7960 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007961 return NULL;
7962 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007963 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007964 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7965 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007966
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007967 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7968 // instruction.
7969 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7970 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7971 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007972 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007973 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007974 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7975 .isn_arg.debug.dbg_break_lnum;
7976 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007978 scope = new_scope(cctx, FOR_SCOPE);
7979 if (scope == NULL)
7980 return NULL;
7981
Bram Moolenaar792f7862020-11-23 08:31:18 +01007982 // Reserve a variable to store the loop iteration counter and initialize it
7983 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007984 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7985 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007986 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007987 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007988 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007989 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007990 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007991 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007992
7993 // compile "expr", it remains on the stack until "endfor"
7994 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007995 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007996 {
7997 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007998 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007999 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008000 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008001
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008002 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01008003 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008004 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01008005 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008006 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008007 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01008008 semsg(_(e_for_loop_on_str_not_supported),
8009 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008010 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011 return NULL;
8012 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008013
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008014 if (vartype->tt_type == VAR_STRING)
8015 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008016 else if (vartype->tt_type == VAR_BLOB)
8017 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008018 else if (vartype->tt_type == VAR_LIST
8019 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008020 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02008021 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008022 item_type = vartype->tt_member;
8023 else if (vartype->tt_member->tt_type == VAR_LIST
8024 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008025 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01008026 item_type = vartype->tt_member->tt_member;
8027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008028
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008029 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01008030 generate_undo_cmdmods(cctx);
8031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008032 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01008033 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008034
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008035 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008036
Bram Moolenaar792f7862020-11-23 08:31:18 +01008037 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008038 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008039 {
8040 generate_UNPACK(cctx, var_count, semicolon);
8041 arg = skipwhite(arg + 1); // skip white after '['
8042
8043 // the list item is replaced by a number of items
Bram Moolenaar35578162021-08-02 19:10:38 +02008044 if (GA_GROW_FAILS(stack, var_count - 1))
Bram Moolenaar792f7862020-11-23 08:31:18 +01008045 {
8046 drop_scope(cctx);
8047 return NULL;
8048 }
8049 --stack->ga_len;
8050 for (idx = 0; idx < var_count; ++idx)
8051 {
8052 ((type_T **)stack->ga_data)[stack->ga_len] =
8053 (semicolon && idx == 0) ? vartype : item_type;
8054 ++stack->ga_len;
8055 }
8056 }
8057
8058 for (idx = 0; idx < var_count; ++idx)
8059 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008060 assign_dest_T dest = dest_local;
8061 int opt_flags = 0;
8062 int vimvaridx = -1;
8063 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008064 type_T *lhs_type = &t_any;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02008065 where_T where = WHERE_INIT;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008066
8067 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008068 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008069 name = vim_strnsave(arg, varlen);
8070 if (name == NULL)
8071 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008072 if (*p == ':')
8073 {
8074 p = skipwhite(p + 1);
8075 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8076 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008077
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008078 // TODO: script var not supported?
8079 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
8080 &vimvaridx, &type, cctx) == FAIL)
8081 goto failed;
8082 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008083 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008084 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
8085 0, 0, type, name) == FAIL)
8086 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008087 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02008088 else if (varlen == 1 && *arg == '_')
8089 {
8090 // Assigning to "_": drop the value.
8091 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8092 goto failed;
8093 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008094 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008095 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01008096 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008097 {
8098 semsg(_(e_variable_already_declared), arg);
8099 goto failed;
8100 }
8101
Bram Moolenaarea870692020-12-02 14:24:30 +01008102 if (STRNCMP(name, "s:", 2) == 0)
8103 {
8104 semsg(_(e_cannot_declare_script_variable_in_function), name);
8105 goto failed;
8106 }
8107
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008108 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02008109 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008110 where.wt_variable = TRUE;
8111 if (lhs_type == &t_any)
8112 lhs_type = item_type;
8113 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02008114 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02008115 ? need_type(item_type, lhs_type,
8116 -1, 0, cctx, FALSE, FALSE)
8117 : check_type(lhs_type, item_type, TRUE, where))
8118 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008119 goto failed;
8120 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008121 if (var_lvar == NULL)
8122 // out of memory or used as an argument
8123 goto failed;
8124
8125 if (semicolon && idx == var_count - 1)
8126 var_lvar->lv_type = vartype;
8127 else
8128 var_lvar->lv_type = item_type;
8129 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8130 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008131
8132 if (*p == ',' || *p == ';')
8133 ++p;
8134 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008135 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008136 }
8137
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008138 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008139 {
8140 int save_prev_lnum = cctx->ctx_prev_lnum;
8141
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008142 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008143 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8144 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008145 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008146 cctx->ctx_prev_lnum = save_prev_lnum;
8147 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008148
Bram Moolenaar792f7862020-11-23 08:31:18 +01008149 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008150
8151failed:
8152 vim_free(name);
8153 drop_scope(cctx);
8154 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155}
8156
8157/*
8158 * compile "endfor"
8159 */
8160 static char_u *
8161compile_endfor(char_u *arg, cctx_T *cctx)
8162{
8163 garray_T *instr = &cctx->ctx_instr;
8164 scope_T *scope = cctx->ctx_scope;
8165 forscope_T *forscope;
8166 isn_T *isn;
8167
Bram Moolenaarfa984412021-03-25 22:15:28 +01008168 if (misplaced_cmdmod(cctx))
8169 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008171 if (scope == NULL || scope->se_type != FOR_SCOPE)
8172 {
8173 emsg(_(e_for));
8174 return NULL;
8175 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008176 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008178 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008179
8180 // At end of ":for" scope jump back to the FOR instruction.
8181 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8182
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008183 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008184 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8185 isn->isn_arg.forloop.for_end = instr->ga_len;
8186
8187 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008188 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189
8190 // Below the ":for" scope drop the "expr" list from the stack.
8191 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8192 return NULL;
8193
8194 vim_free(scope);
8195
8196 return arg;
8197}
8198
8199/*
8200 * compile "while expr"
8201 *
8202 * Produces instructions:
8203 * top: EVAL expr Push result of "expr"
8204 * JUMP_IF_FALSE end jump if false
8205 * ... body ...
8206 * JUMP top Jump back to repeat
8207 * end:
8208 *
8209 */
8210 static char_u *
8211compile_while(char_u *arg, cctx_T *cctx)
8212{
8213 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008214 scope_T *scope;
8215
8216 scope = new_scope(cctx, WHILE_SCOPE);
8217 if (scope == NULL)
8218 return NULL;
8219
Bram Moolenaara91a7132021-03-25 21:12:15 +01008220 // "endwhile" jumps back here, one before when profiling or using cmdmods
8221 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008222
8223 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008224 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008225 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008226 if (!ends_excmd2(arg, skipwhite(p)))
8227 {
8228 semsg(_(e_trailing_arg), p);
8229 return NULL;
8230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008231
Bram Moolenaar13106602020-10-04 16:06:05 +02008232 if (bool_on_stack(cctx) == FAIL)
8233 return FAIL;
8234
Bram Moolenaara91a7132021-03-25 21:12:15 +01008235 // CMDMOD_REV must come before the jump
8236 generate_undo_cmdmods(cctx);
8237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008238 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008239 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008240 JUMP_IF_FALSE, cctx) == FAIL)
8241 return FAIL;
8242
8243 return p;
8244}
8245
8246/*
8247 * compile "endwhile"
8248 */
8249 static char_u *
8250compile_endwhile(char_u *arg, cctx_T *cctx)
8251{
8252 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008253 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008254
Bram Moolenaarfa984412021-03-25 22:15:28 +01008255 if (misplaced_cmdmod(cctx))
8256 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008257 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8258 {
8259 emsg(_(e_while));
8260 return NULL;
8261 }
8262 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008263 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008264
Bram Moolenaarf002a412021-01-24 13:34:18 +01008265#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008266 // count the endwhile before jumping
8267 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008268#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008269
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008270 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008271 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008272
8273 // Fill in the "end" label in the WHILE statement so it can jump here.
8274 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008275 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8276 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008277
8278 vim_free(scope);
8279
8280 return arg;
8281}
8282
8283/*
8284 * compile "continue"
8285 */
8286 static char_u *
8287compile_continue(char_u *arg, cctx_T *cctx)
8288{
8289 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008290 int try_scopes = 0;
8291 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008292
8293 for (;;)
8294 {
8295 if (scope == NULL)
8296 {
8297 emsg(_(e_continue));
8298 return NULL;
8299 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008300 if (scope->se_type == FOR_SCOPE)
8301 {
8302 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008303 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008304 }
8305 if (scope->se_type == WHILE_SCOPE)
8306 {
8307 loop_label = scope->se_u.se_while.ws_top_label;
8308 break;
8309 }
8310 if (scope->se_type == TRY_SCOPE)
8311 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008312 scope = scope->se_outer;
8313 }
8314
Bram Moolenaarc150c092021-02-13 15:02:46 +01008315 if (try_scopes > 0)
8316 // Inside one or more try/catch blocks we first need to jump to the
8317 // "finally" or "endtry" to cleanup.
8318 generate_TRYCONT(cctx, try_scopes, loop_label);
8319 else
8320 // Jump back to the FOR or WHILE instruction.
8321 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008323 return arg;
8324}
8325
8326/*
8327 * compile "break"
8328 */
8329 static char_u *
8330compile_break(char_u *arg, cctx_T *cctx)
8331{
8332 scope_T *scope = cctx->ctx_scope;
8333 endlabel_T **el;
8334
8335 for (;;)
8336 {
8337 if (scope == NULL)
8338 {
8339 emsg(_(e_break));
8340 return NULL;
8341 }
8342 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8343 break;
8344 scope = scope->se_outer;
8345 }
8346
8347 // Jump to the end of the FOR or WHILE loop.
8348 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008349 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008350 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008351 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008352 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8353 return FAIL;
8354
8355 return arg;
8356}
8357
8358/*
8359 * compile "{" start of block
8360 */
8361 static char_u *
8362compile_block(char_u *arg, cctx_T *cctx)
8363{
8364 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8365 return NULL;
8366 return skipwhite(arg + 1);
8367}
8368
8369/*
8370 * compile end of block: drop one scope
8371 */
8372 static void
8373compile_endblock(cctx_T *cctx)
8374{
8375 scope_T *scope = cctx->ctx_scope;
8376
8377 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008378 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008379 vim_free(scope);
8380}
8381
8382/*
8383 * compile "try"
8384 * Creates a new scope for the try-endtry, pointing to the first catch and
8385 * finally.
8386 * Creates another scope for the "try" block itself.
8387 * TRY instruction sets up exception handling at runtime.
8388 *
8389 * "try"
8390 * TRY -> catch1, -> finally push trystack entry
8391 * ... try block
8392 * "throw {exception}"
8393 * EVAL {exception}
8394 * THROW create exception
8395 * ... try block
8396 * " catch {expr}"
8397 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008398 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008399 * EVAL {expr}
8400 * MATCH
8401 * JUMP nomatch -> catch2
8402 * CATCH remove exception
8403 * ... catch block
8404 * " catch"
8405 * JUMP -> finally
8406 * catch2: CATCH remove exception
8407 * ... catch block
8408 * " finally"
8409 * finally:
8410 * ... finally block
8411 * " endtry"
8412 * ENDTRY pop trystack entry, may rethrow
8413 */
8414 static char_u *
8415compile_try(char_u *arg, cctx_T *cctx)
8416{
8417 garray_T *instr = &cctx->ctx_instr;
8418 scope_T *try_scope;
8419 scope_T *scope;
8420
Bram Moolenaarfa984412021-03-25 22:15:28 +01008421 if (misplaced_cmdmod(cctx))
8422 return NULL;
8423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008424 // scope that holds the jumps that go to catch/finally/endtry
8425 try_scope = new_scope(cctx, TRY_SCOPE);
8426 if (try_scope == NULL)
8427 return NULL;
8428
Bram Moolenaar69f70502021-01-01 16:10:46 +01008429 if (cctx->ctx_skip != SKIP_YES)
8430 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008431 isn_T *isn;
8432
8433 // "try_catch" is set when the first ":catch" is found or when no catch
8434 // is found and ":finally" is found.
8435 // "try_finally" is set when ":finally" is found
8436 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008437 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008438 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8439 return NULL;
8440 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8441 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008442 return NULL;
8443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008444
8445 // scope for the try block itself
8446 scope = new_scope(cctx, BLOCK_SCOPE);
8447 if (scope == NULL)
8448 return NULL;
8449
8450 return arg;
8451}
8452
8453/*
8454 * compile "catch {expr}"
8455 */
8456 static char_u *
8457compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8458{
8459 scope_T *scope = cctx->ctx_scope;
8460 garray_T *instr = &cctx->ctx_instr;
8461 char_u *p;
8462 isn_T *isn;
8463
Bram Moolenaarfa984412021-03-25 22:15:28 +01008464 if (misplaced_cmdmod(cctx))
8465 return NULL;
8466
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008467 // end block scope from :try or :catch
8468 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8469 compile_endblock(cctx);
8470 scope = cctx->ctx_scope;
8471
8472 // Error if not in a :try scope
8473 if (scope == NULL || scope->se_type != TRY_SCOPE)
8474 {
8475 emsg(_(e_catch));
8476 return NULL;
8477 }
8478
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008479 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008480 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008481 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008482 return NULL;
8483 }
8484
Bram Moolenaar69f70502021-01-01 16:10:46 +01008485 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008486 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008487#ifdef FEAT_PROFILE
8488 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008489 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008490 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008491 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008492 .isn_type == ISN_PROF_START)
8493 --instr->ga_len;
8494#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008495 // Jump from end of previous block to :finally or :endtry
8496 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8497 JUMP_ALWAYS, cctx) == FAIL)
8498 return NULL;
8499
8500 // End :try or :catch scope: set value in ISN_TRY instruction
8501 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008502 if (isn->isn_arg.try.try_ref->try_catch == 0)
8503 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008504 if (scope->se_u.se_try.ts_catch_label != 0)
8505 {
8506 // Previous catch without match jumps here
8507 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8508 isn->isn_arg.jump.jump_where = instr->ga_len;
8509 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008510#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008511 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008512 {
8513 // a "throw" that jumps here needs to be counted
8514 generate_instr(cctx, ISN_PROF_END);
8515 // the "catch" is also counted
8516 generate_instr(cctx, ISN_PROF_START);
8517 }
8518#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008519 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008520 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008521 }
8522
8523 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008524 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008525 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008526 scope->se_u.se_try.ts_caught_all = TRUE;
8527 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008528 }
8529 else
8530 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008531 char_u *end;
8532 char_u *pat;
8533 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008534 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008535 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008537 // Push v:exception, push {expr} and MATCH
8538 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8539
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008540 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008541 if (*end != *p)
8542 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008543 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008544 vim_free(tofree);
8545 return FAIL;
8546 }
8547 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008548 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008549 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008550 len = (int)(end - tofree);
8551 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008552 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008553 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008554 if (pat == NULL)
8555 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008556 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008557 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008559 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8560 return NULL;
8561
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008562 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008563 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8564 return NULL;
8565 }
8566
Bram Moolenaar69f70502021-01-01 16:10:46 +01008567 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008568 return NULL;
8569
8570 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8571 return NULL;
8572 return p;
8573}
8574
8575 static char_u *
8576compile_finally(char_u *arg, cctx_T *cctx)
8577{
8578 scope_T *scope = cctx->ctx_scope;
8579 garray_T *instr = &cctx->ctx_instr;
8580 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008581 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008582
Bram Moolenaarfa984412021-03-25 22:15:28 +01008583 if (misplaced_cmdmod(cctx))
8584 return NULL;
8585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008586 // end block scope from :try or :catch
8587 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8588 compile_endblock(cctx);
8589 scope = cctx->ctx_scope;
8590
8591 // Error if not in a :try scope
8592 if (scope == NULL || scope->se_type != TRY_SCOPE)
8593 {
8594 emsg(_(e_finally));
8595 return NULL;
8596 }
8597
rbtnn84934992021-08-07 13:26:53 +02008598 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008599 {
rbtnn84934992021-08-07 13:26:53 +02008600 // End :catch or :finally scope: set value in ISN_TRY instruction
8601 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8602 if (isn->isn_arg.try.try_ref->try_finally != 0)
8603 {
8604 emsg(_(e_finally_dup));
8605 return NULL;
8606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008607
rbtnn84934992021-08-07 13:26:53 +02008608 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008609#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008610 if (cctx->ctx_compile_type == CT_PROFILE
8611 && ((isn_T *)instr->ga_data)[this_instr - 1]
8612 .isn_type == ISN_PROF_START)
8613 {
8614 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008615 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008616
8617 // jump to the profile end above it
8618 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8619 .isn_type == ISN_PROF_END)
8620 --this_instr;
8621 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008622#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008623
rbtnn84934992021-08-07 13:26:53 +02008624 // Fill in the "end" label in jumps at the end of the blocks.
8625 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8626 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008627
rbtnn84934992021-08-07 13:26:53 +02008628 // If there is no :catch then an exception jumps to :finally.
8629 if (isn->isn_arg.try.try_ref->try_catch == 0)
8630 isn->isn_arg.try.try_ref->try_catch = this_instr;
8631 isn->isn_arg.try.try_ref->try_finally = this_instr;
8632 if (scope->se_u.se_try.ts_catch_label != 0)
8633 {
8634 // Previous catch without match jumps here
8635 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8636 isn->isn_arg.jump.jump_where = this_instr;
8637 scope->se_u.se_try.ts_catch_label = 0;
8638 }
8639 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8640 return NULL;
8641
8642 // TODO: set index in ts_finally_label jumps
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008644
8645 return arg;
8646}
8647
8648 static char_u *
8649compile_endtry(char_u *arg, cctx_T *cctx)
8650{
8651 scope_T *scope = cctx->ctx_scope;
8652 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008653 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008654
Bram Moolenaarfa984412021-03-25 22:15:28 +01008655 if (misplaced_cmdmod(cctx))
8656 return NULL;
8657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008658 // end block scope from :catch or :finally
8659 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8660 compile_endblock(cctx);
8661 scope = cctx->ctx_scope;
8662
8663 // Error if not in a :try scope
8664 if (scope == NULL || scope->se_type != TRY_SCOPE)
8665 {
8666 if (scope == NULL)
8667 emsg(_(e_no_endtry));
8668 else if (scope->se_type == WHILE_SCOPE)
8669 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008670 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008671 emsg(_(e_endfor));
8672 else
8673 emsg(_(e_endif));
8674 return NULL;
8675 }
8676
Bram Moolenaarc150c092021-02-13 15:02:46 +01008677 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008678 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008679 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008680 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8681 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008682 {
8683 emsg(_(e_missing_catch_or_finally));
8684 return NULL;
8685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008686
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008687#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008688 if (cctx->ctx_compile_type == CT_PROFILE
8689 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008690 .isn_type == ISN_PROF_START)
8691 // move the profile start after "endtry" so that it's not counted when
8692 // the exception is rethrown.
8693 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008694#endif
8695
Bram Moolenaar69f70502021-01-01 16:10:46 +01008696 // Fill in the "end" label in jumps at the end of the blocks, if not
8697 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008698 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8699 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008700
Bram Moolenaar69f70502021-01-01 16:10:46 +01008701 if (scope->se_u.se_try.ts_catch_label != 0)
8702 {
8703 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008704 isn_T *isn = ((isn_T *)instr->ga_data)
8705 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008706 isn->isn_arg.jump.jump_where = instr->ga_len;
8707 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008708 }
8709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008710 compile_endblock(cctx);
8711
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008712 if (cctx->ctx_skip != SKIP_YES)
8713 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008714 // End :catch or :finally scope: set instruction index in ISN_TRY
8715 // instruction
8716 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008717 if (cctx->ctx_skip != SKIP_YES
8718 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8719 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008720#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008721 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008722 generate_instr(cctx, ISN_PROF_START);
8723#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008724 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008725 return arg;
8726}
8727
8728/*
8729 * compile "throw {expr}"
8730 */
8731 static char_u *
8732compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8733{
8734 char_u *p = skipwhite(arg);
8735
Bram Moolenaara5565e42020-05-09 15:44:01 +02008736 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008737 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008738 if (cctx->ctx_skip == SKIP_YES)
8739 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008740 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008741 return NULL;
8742 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8743 return NULL;
8744
8745 return p;
8746}
8747
Bram Moolenaarc3235272021-07-10 19:42:03 +02008748 static char_u *
8749compile_eval(char_u *arg, cctx_T *cctx)
8750{
8751 char_u *p = arg;
8752 int name_only;
8753 char_u *alias;
8754 long lnum = SOURCING_LNUM;
8755
8756 // find_ex_command() will consider a variable name an expression, assuming
8757 // that something follows on the next line. Check that something actually
8758 // follows, otherwise it's probably a misplaced command.
8759 get_name_len(&p, &alias, FALSE, FALSE);
8760 name_only = ends_excmd2(arg, skipwhite(p));
8761 vim_free(alias);
8762
8763 p = arg;
8764 if (compile_expr0(&p, cctx) == FAIL)
8765 return NULL;
8766
8767 if (name_only && lnum == SOURCING_LNUM)
8768 {
8769 semsg(_(e_expression_without_effect_str), arg);
8770 return NULL;
8771 }
8772
8773 // drop the result
8774 generate_instr_drop(cctx, ISN_DROP, 1);
8775
8776 return skipwhite(p);
8777}
8778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008779/*
8780 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008781 * compile "echomsg expr"
8782 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02008783 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008784 * compile "execute expr"
8785 */
8786 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008787compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008788{
8789 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008790 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008791 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008792 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008793 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008794 garray_T *stack = &cctx->ctx_type_stack;
8795 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008796
8797 for (;;)
8798 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008799 if (ends_excmd2(prev, p))
8800 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008801 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008802 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008803 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008804
8805 if (cctx->ctx_skip != SKIP_YES)
8806 {
8807 // check for non-void type
8808 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8809 if (type->tt_type == VAR_VOID)
8810 {
8811 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8812 return NULL;
8813 }
8814 }
8815
Bram Moolenaarad39c092020-02-26 18:23:43 +01008816 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008817 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008818 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008819 }
8820
Bram Moolenaare4984292020-12-13 14:19:25 +01008821 if (count > 0)
8822 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008823 long save_lnum = cctx->ctx_lnum;
8824
8825 // Use the line number where the command started.
8826 cctx->ctx_lnum = start_ctx_lnum;
8827
Bram Moolenaare4984292020-12-13 14:19:25 +01008828 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8829 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8830 else if (cmdidx == CMD_execute)
8831 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8832 else if (cmdidx == CMD_echomsg)
8833 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02008834 else if (cmdidx == CMD_echoconsole)
8835 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01008836 else
8837 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008838
8839 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008841 return p;
8842}
8843
8844/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008845 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008846 * instruction to compute it and return OK.
8847 * Otherwise return FAIL, the caller must deal with any range.
8848 */
8849 static int
8850compile_variable_range(exarg_T *eap, cctx_T *cctx)
8851{
8852 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8853 char_u *p = skipdigits(eap->cmd);
8854
8855 if (p == range_end)
8856 return FAIL;
8857 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8858}
8859
8860/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008861 * :put r
8862 * :put ={expr}
8863 */
8864 static char_u *
8865compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8866{
8867 char_u *line = arg;
8868 linenr_T lnum;
8869 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008870 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008871
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008872 eap->regname = *line;
8873
8874 if (eap->regname == '=')
8875 {
8876 char_u *p = line + 1;
8877
8878 if (compile_expr0(&p, cctx) == FAIL)
8879 return NULL;
8880 line = p;
8881 }
8882 else if (eap->regname != NUL)
8883 ++line;
8884
Bram Moolenaar08597872020-12-10 19:43:40 +01008885 if (compile_variable_range(eap, cctx) == OK)
8886 {
8887 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8888 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008889 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008890 {
8891 // Either no range or a number.
8892 // "errormsg" will not be set because the range is ADDR_LINES.
8893 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008894 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008895 return NULL;
8896 if (eap->addr_count == 0)
8897 lnum = -1;
8898 else
8899 lnum = eap->line2;
8900 if (above)
8901 --lnum;
8902 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008903
8904 generate_PUT(cctx, eap->regname, lnum);
8905 return line;
8906}
8907
8908/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008909 * A command that is not compiled, execute with legacy code.
8910 */
8911 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008912compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008913{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008914 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02008915 char_u *p;
8916 int has_expr = FALSE;
8917 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008918 char_u *tofree = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008919
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008920 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008921 goto theend;
8922
Bram Moolenaare729ce22021-06-06 21:38:09 +02008923 // If there was a prececing command modifier, drop it and include it in the
8924 // EXEC command.
8925 if (cctx->ctx_has_cmdmod)
8926 {
8927 garray_T *instr = &cctx->ctx_instr;
8928 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8929
8930 if (isn->isn_type == ISN_CMDMOD)
8931 {
8932 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8933 ->cmod_filter_regmatch.regprog);
8934 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8935 --instr->ga_len;
8936 cctx->ctx_has_cmdmod = FALSE;
8937 }
8938 }
8939
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008940 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008941 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008942 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008943 int usefilter = FALSE;
8944
8945 has_expr = argt & (EX_XFILE | EX_EXPAND);
8946
8947 // If the command can be followed by a bar, find the bar and truncate
8948 // it, so that the following command can be compiled.
8949 // The '|' is overwritten with a NUL, it is put back below.
8950 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8951 && *eap->arg == '!')
8952 // :w !filter or :r !filter or :r! filter
8953 usefilter = TRUE;
8954 if ((argt & EX_TRLBAR) && !usefilter)
8955 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008956 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008957 separate_nextcmd(eap);
8958 if (eap->nextcmd != NULL)
8959 nextcmd = eap->nextcmd;
8960 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008961 else if (eap->cmdidx == CMD_wincmd)
8962 {
8963 p = eap->arg;
8964 if (*p != NUL)
8965 ++p;
8966 if (*p == 'g' || *p == Ctrl_G)
8967 ++p;
8968 p = skipwhite(p);
8969 if (*p == '|')
8970 {
8971 *p = NUL;
8972 nextcmd = p + 1;
8973 }
8974 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008975 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
8976 {
8977 // If there is a trailing '{' read lines until the '}'
8978 p = eap->arg + STRLEN(eap->arg) - 1;
8979 while (p > eap->arg && VIM_ISWHITE(*p))
8980 --p;
8981 if (*p == '{')
8982 {
8983 exarg_T ea;
8984 int flags; // unused
8985 int start_lnum = SOURCING_LNUM;
8986
8987 CLEAR_FIELD(ea);
8988 ea.arg = eap->arg;
8989 fill_exarg_from_cctx(&ea, cctx);
8990 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
8991 if (tofree != NULL)
8992 {
8993 *p = NUL;
8994 line = concat_str(line, tofree);
8995 if (line == NULL)
8996 goto theend;
8997 vim_free(tofree);
8998 tofree = line;
8999 SOURCING_LNUM = start_lnum;
9000 }
9001 }
9002 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009003 }
9004
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009005 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9006 {
9007 // expand filename in "syntax include [@group] filename"
9008 has_expr = TRUE;
9009 eap->arg = skipwhite(eap->arg + 7);
9010 if (*eap->arg == '@')
9011 eap->arg = skiptowhite(eap->arg);
9012 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009013
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009014 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9015 && STRLEN(eap->arg) > 4)
9016 {
9017 int delim = *eap->arg;
9018
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009019 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009020 if (*p == delim)
9021 {
9022 eap->arg = p + 1;
9023 has_expr = TRUE;
9024 }
9025 }
9026
Bram Moolenaarecac5912021-01-05 19:23:28 +01009027 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
9028 {
9029 // TODO: should only expand when appropriate for the command
9030 eap->arg = skiptowhite(eap->arg);
9031 has_expr = TRUE;
9032 }
9033
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009034 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009035 {
9036 int count = 0;
9037 char_u *start = skipwhite(line);
9038
9039 // :cmd xxx`=expr1`yyy`=expr2`zzz
9040 // PUSHS ":cmd xxx"
9041 // eval expr1
9042 // PUSHS "yyy"
9043 // eval expr2
9044 // PUSHS "zzz"
9045 // EXECCONCAT 5
9046 for (;;)
9047 {
9048 if (p > start)
9049 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009050 char_u *val = vim_strnsave(start, p - start);
9051
9052 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009053 ++count;
9054 }
9055 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009056 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009057 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009058 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009059 ++count;
9060 p = skipwhite(p);
9061 if (*p != '`')
9062 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009063 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009064 return NULL;
9065 }
9066 start = p + 1;
9067
9068 p = (char_u *)strstr((char *)start, "`=");
9069 if (p == NULL)
9070 {
9071 if (*skipwhite(start) != NUL)
9072 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009073 char_u *val = vim_strsave(start);
9074
9075 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009076 ++count;
9077 }
9078 break;
9079 }
9080 }
9081 generate_EXECCONCAT(cctx, count);
9082 }
9083 else
9084 generate_EXEC(cctx, line);
9085
9086theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009087 if (*nextcmd != NUL)
9088 {
9089 // the parser expects a pointer to the bar, put it back
9090 --nextcmd;
9091 *nextcmd = '|';
9092 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009093 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009094
9095 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009096}
9097
Bram Moolenaar20677332021-06-06 17:02:53 +02009098/*
9099 * A script command with heredoc, e.g.
9100 * ruby << EOF
9101 * command
9102 * EOF
9103 * Has been turned into one long line with NL characters by
9104 * get_function_body():
9105 * ruby << EOF<NL> command<NL>EOF
9106 */
9107 static char_u *
9108compile_script(char_u *line, cctx_T *cctx)
9109{
9110 if (cctx->ctx_skip != SKIP_YES)
9111 {
9112 isn_T *isn;
9113
9114 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9115 return NULL;
9116 isn->isn_arg.string = vim_strsave(line);
9117 }
9118 return (char_u *)"";
9119}
9120
Bram Moolenaar8238f082021-04-20 21:10:48 +02009121
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009122/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009123 * :s/pat/repl/
9124 */
9125 static char_u *
9126compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9127{
9128 char_u *cmd = eap->arg;
9129 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9130
9131 if (expr != NULL)
9132 {
9133 int delimiter = *cmd++;
9134
9135 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009136 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009137 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9138 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009139 garray_T save_ga = cctx->ctx_instr;
9140 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009141 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009142 int trailing_error;
9143 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009144 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009145 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009146
9147 cmd += 3;
9148 end = skip_substitute(cmd, delimiter);
9149
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009150 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009151 // labels are correct.
9152 cctx->ctx_instr.ga_len = 0;
9153 cctx->ctx_instr.ga_maxlen = 0;
9154 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009155 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009156 if (end[-1] == NUL)
9157 end[-1] = delimiter;
9158 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009159 trailing_error = *cmd != delimiter && *cmd != NUL;
9160
Bram Moolenaar169502f2021-04-21 12:19:35 +02009161 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009162 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009163 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009164 if (trailing_error)
9165 semsg(_(e_trailing_arg), cmd);
9166 clear_instr_ga(&cctx->ctx_instr);
9167 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009168 return NULL;
9169 }
9170
Bram Moolenaar8238f082021-04-20 21:10:48 +02009171 // Move the generated instructions into the ISN_SUBSTITUTE
9172 // instructions, then restore the list of instructions before
9173 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009174 instr_count = cctx->ctx_instr.ga_len;
9175 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009176 instr[instr_count].isn_type = ISN_FINISH;
9177
9178 cctx->ctx_instr = save_ga;
9179 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9180 {
9181 int idx;
9182
9183 for (idx = 0; idx < instr_count; ++idx)
9184 delete_instr(instr + idx);
9185 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009186 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009187 }
9188 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9189 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009190
9191 // skip over flags
9192 if (*end == '&')
9193 ++end;
9194 while (ASCII_ISALPHA(*end) || *end == '#')
9195 ++end;
9196 return end;
9197 }
9198 }
9199
9200 return compile_exec(arg, eap, cctx);
9201}
9202
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009203 static char_u *
9204compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9205{
9206 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009207 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009208
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009209 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009210 {
9211 if (STRNCMP(arg, "END", 3) == 0)
9212 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009213 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009214 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009215 // First load the current variable value.
9216 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9217 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009218 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009219 }
9220
9221 // Gets the redirected text and put it on the stack, then store it
9222 // in the variable.
9223 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9224
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009225 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009226 generate_instr_drop(cctx, ISN_CONCAT, 1);
9227
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009228 if (lhs->lhs_has_index)
9229 {
9230 // Use the info in "lhs" to store the value at the index in the
9231 // list or dict.
9232 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9233 &t_string, cctx) == FAIL)
9234 return NULL;
9235 }
9236 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009237 return NULL;
9238
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009239 VIM_CLEAR(lhs->lhs_name);
9240 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009241 return arg + 3;
9242 }
9243 emsg(_(e_cannot_nest_redir));
9244 return NULL;
9245 }
9246
9247 if (arg[0] == '=' && arg[1] == '>')
9248 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009249 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009250
9251 // redirect to a variable is compiled
9252 arg += 2;
9253 if (*arg == '>')
9254 {
9255 ++arg;
9256 append = TRUE;
9257 }
9258 arg = skipwhite(arg);
9259
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009260 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009261 FALSE, FALSE, 1, cctx) == FAIL)
9262 return NULL;
9263 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009264 lhs->lhs_append = append;
9265 if (lhs->lhs_has_index)
9266 {
9267 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9268 if (lhs->lhs_whole == NULL)
9269 return NULL;
9270 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009271
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009272 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009273 }
9274
9275 // other redirects are handled like at script level
9276 return compile_exec(line, eap, cctx);
9277}
9278
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009279#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009280 static char_u *
9281compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9282{
9283 isn_T *isn;
9284 char_u *p;
9285
9286 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9287 if (isn == NULL)
9288 return NULL;
9289 isn->isn_arg.number = eap->cmdidx;
9290
9291 p = eap->arg;
9292 if (compile_expr0(&p, cctx) == FAIL)
9293 return NULL;
9294
9295 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9296 if (isn == NULL)
9297 return NULL;
9298 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9299 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9300 return NULL;
9301 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9302 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9303 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9304
9305 return p;
9306}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009307#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009308
Bram Moolenaar4c137212021-04-19 16:48:48 +02009309/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009310 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009311 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009312 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009313 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009314add_def_function(ufunc_T *ufunc)
9315{
9316 dfunc_T *dfunc;
9317
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009318 if (def_functions.ga_len == 0)
9319 {
9320 // The first position is not used, so that a zero uf_dfunc_idx means it
9321 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009322 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009323 return FAIL;
9324 ++def_functions.ga_len;
9325 }
9326
Bram Moolenaar09689a02020-05-09 22:50:08 +02009327 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009328 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009329 return FAIL;
9330 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9331 CLEAR_POINTER(dfunc);
9332 dfunc->df_idx = def_functions.ga_len;
9333 ufunc->uf_dfunc_idx = dfunc->df_idx;
9334 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009335 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009336 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009337 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009338 ++def_functions.ga_len;
9339 return OK;
9340}
9341
9342/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009343 * After ex_function() has collected all the function lines: parse and compile
9344 * the lines into instructions.
9345 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009346 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9347 * the return statement (used for lambda). When uf_ret_type is already set
9348 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009349 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009350 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009351 * This can be used recursively through compile_lambda(), which may reallocate
9352 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009353 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009354 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009355 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009356compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009357 ufunc_T *ufunc,
9358 int check_return_type,
9359 compiletype_T compile_type,
9360 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009361{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009362 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009363 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009364 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009365 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009366 cctx_T cctx;
9367 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009368 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009369 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009370 int ret = FAIL;
9371 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009372 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009373 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009374 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009375 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009376#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009377 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009378#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009379 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009380
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009381 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009382 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009383 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009384 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009385 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9386 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009387 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009388
9389 switch (compile_type)
9390 {
9391 case CT_PROFILE:
9392#ifdef FEAT_PROFILE
9393 instr_dest = dfunc->df_instr_prof; break;
9394#endif
9395 case CT_NONE: instr_dest = dfunc->df_instr; break;
9396 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9397 }
9398 if (instr_dest != NULL)
9399 // Was compiled in this mode before: Free old instructions.
9400 delete_def_function_contents(dfunc, FALSE);
9401 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009402 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009403 else
9404 {
9405 if (add_def_function(ufunc) == FAIL)
9406 return FAIL;
9407 new_def_function = TRUE;
9408 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009409
Bram Moolenaar985116a2020-07-12 17:31:09 +02009410 ufunc->uf_def_status = UF_COMPILING;
9411
Bram Moolenaara80faa82020-04-12 19:37:17 +02009412 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009413
Bram Moolenaare99d4222021-06-13 14:01:26 +02009414 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009415 cctx.ctx_ufunc = ufunc;
9416 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009417 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009418 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9419 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9420 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9421 cctx.ctx_type_list = &ufunc->uf_type_list;
9422 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9423 instr = &cctx.ctx_instr;
9424
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009425 // Set the context to the function, it may be compiled when called from
9426 // another script. Set the script version to the most modern one.
9427 // The line number will be set in next_line_from_context().
9428 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009429 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9430
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009431 // Don't use the flag from ":legacy" here.
9432 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9433
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009434 // Make sure error messages are OK.
9435 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9436 if (do_estack_push)
9437 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009438 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009439
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009440 if (ufunc->uf_def_args.ga_len > 0)
9441 {
9442 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009443 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009444 int i;
9445 char_u *arg;
9446 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009447 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009448
9449 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009450 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009451 for (i = 0; i < count; ++i)
9452 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009453 garray_T *stack = &cctx.ctx_type_stack;
9454 type_T *val_type;
9455 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009456 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009457 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009458 int jump_instr_idx = instr->ga_len;
9459 isn_T *isn;
9460
9461 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9462 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9463 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009464
9465 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009466 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009467
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009468 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009469 r = compile_expr0(&arg, &cctx);
9470
Bram Moolenaar12bce952021-03-11 20:04:04 +01009471 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009472 goto erret;
9473
9474 // If no type specified use the type of the default value.
9475 // Otherwise check that the default value type matches the
9476 // specified type.
9477 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009478 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009479 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009480 {
9481 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009482 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009483 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009484 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009485 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009486 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009487
9488 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009489 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009490
9491 // set instruction index in JUMP_IF_ARG_SET to here
9492 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9493 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009494 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009495
9496 if (did_set_arg_type)
9497 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009498 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009499 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009500
9501 /*
9502 * Loop over all the lines of the function and generate instructions.
9503 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009504 for (;;)
9505 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009506 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009507 int starts_with_colon = FALSE;
9508 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009509 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009510
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009511 // Bail out on the first error to avoid a flood of errors and report
9512 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009513 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009514 goto erret;
9515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009516 if (line != NULL && *line == '|')
9517 // the line continues after a '|'
9518 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009519 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009520 && !(*line == '#' && (line == cctx.ctx_line_start
9521 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009522 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009523 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009524 goto erret;
9525 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009526 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9527 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009528 else
9529 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009530 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009531 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009532 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009533 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009534#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009535 if (cctx.ctx_skip != SKIP_YES)
9536 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009537#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009538 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009539 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009540 // Make a copy, splitting off nextcmd and removing trailing spaces
9541 // may change it.
9542 if (line != NULL)
9543 {
9544 line = vim_strsave(line);
9545 vim_free(line_to_free);
9546 line_to_free = line;
9547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009548 }
9549
Bram Moolenaara80faa82020-04-12 19:37:17 +02009550 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009551 ea.cmdlinep = &line;
9552 ea.cmd = skipwhite(line);
9553
Bram Moolenaarb2049902021-01-24 12:53:53 +01009554 if (*ea.cmd == '#')
9555 {
9556 // "#" starts a comment
9557 line = (char_u *)"";
9558 continue;
9559 }
9560
Bram Moolenaarf002a412021-01-24 13:34:18 +01009561#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009562 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9563 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009564 {
9565 may_generate_prof_end(&cctx, prof_lnum);
9566
9567 prof_lnum = cctx.ctx_lnum;
9568 generate_instr(&cctx, ISN_PROF_START);
9569 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009570#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009571 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9572 && cctx.ctx_skip != SKIP_YES)
9573 {
9574 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009575 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009576 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009577 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009578
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009579 // Some things can be recognized by the first character.
9580 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009581 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009582 case '}':
9583 {
9584 // "}" ends a block scope
9585 scopetype_T stype = cctx.ctx_scope == NULL
9586 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009587
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009588 if (stype == BLOCK_SCOPE)
9589 {
9590 compile_endblock(&cctx);
9591 line = ea.cmd;
9592 }
9593 else
9594 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009595 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009596 goto erret;
9597 }
9598 if (line != NULL)
9599 line = skipwhite(ea.cmd + 1);
9600 continue;
9601 }
9602
9603 case '{':
9604 // "{" starts a block scope
9605 // "{'a': 1}->func() is something else
9606 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9607 {
9608 line = compile_block(ea.cmd, &cctx);
9609 continue;
9610 }
9611 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009612 }
9613
9614 /*
9615 * COMMAND MODIFIERS
9616 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009617 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009618 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9619 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009620 {
9621 if (errormsg != NULL)
9622 goto erret;
9623 // empty line or comment
9624 line = (char_u *)"";
9625 continue;
9626 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009627 generate_cmdmods(&cctx, &local_cmdmod);
9628 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009629
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009630 // Check if there was a colon after the last command modifier or before
9631 // the current position.
9632 for (p = ea.cmd; p >= line; --p)
9633 {
9634 if (*p == ':')
9635 starts_with_colon = TRUE;
9636 if (p < ea.cmd && !VIM_ISWHITE(*p))
9637 break;
9638 }
9639
Bram Moolenaarce024c32021-06-26 13:00:49 +02009640 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009641 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009642 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009643 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009644 if (checkforcmd(&ea.cmd, "call", 3))
9645 {
9646 if (*ea.cmd == '(')
9647 // not for "call()"
9648 ea.cmd = p;
9649 else
9650 ea.cmd = skipwhite(ea.cmd);
9651 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009652
Bram Moolenaarce024c32021-06-26 13:00:49 +02009653 if (!starts_with_colon)
9654 {
9655 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009656
Bram Moolenaarce024c32021-06-26 13:00:49 +02009657 // Check for assignment after command modifiers.
9658 assign = may_compile_assignment(&ea, &line, &cctx);
9659 if (assign == OK)
9660 goto nextline;
9661 if (assign == FAIL)
9662 goto erret;
9663 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009664 }
9665
9666 /*
9667 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009668 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009669 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009670 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009671 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009672 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9673 && (starts_with_colon || !(*cmd == '\''
9674 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009675 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009676 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009677 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009678 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009679 if (!starts_with_colon)
9680 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009681 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009682 goto erret;
9683 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009684 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009685 if (ends_excmd2(line, ea.cmd))
9686 {
9687 // A range without a command: jump to the line.
9688 // TODO: compile to a more efficient command, possibly
9689 // calling parse_cmd_address().
9690 ea.cmdidx = CMD_SIZE;
9691 line = compile_exec(line, &ea, &cctx);
9692 goto nextline;
9693 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009694 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009695 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009696 p = find_ex_command(&ea, NULL,
9697 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009698 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009699
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009700 if (p == NULL)
9701 {
9702 if (cctx.ctx_skip != SKIP_YES)
9703 emsg(_(e_ambiguous_use_of_user_defined_command));
9704 goto erret;
9705 }
9706
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009707 // When using ":legacy cmd" always use compile_exec().
9708 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009709 {
9710 char_u *start = ea.cmd;
9711
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009712 switch (ea.cmdidx)
9713 {
9714 case CMD_if:
9715 case CMD_elseif:
9716 case CMD_else:
9717 case CMD_endif:
9718 case CMD_for:
9719 case CMD_endfor:
9720 case CMD_continue:
9721 case CMD_break:
9722 case CMD_while:
9723 case CMD_endwhile:
9724 case CMD_try:
9725 case CMD_catch:
9726 case CMD_finally:
9727 case CMD_endtry:
9728 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9729 goto erret;
9730 default: break;
9731 }
9732
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009733 // ":legacy return expr" needs to be handled differently.
9734 if (checkforcmd(&start, "return", 4))
9735 ea.cmdidx = CMD_return;
9736 else
9737 ea.cmdidx = CMD_legacy;
9738 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009740 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9741 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009742 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009743 {
9744 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009745 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009746 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009747 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009748 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009749 // CMD_var cannot happen, compile_assignment() above would be
9750 // used. Most likely an assignment to a non-existing variable.
9751 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009752 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009753 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009754 }
9755
Bram Moolenaar3988f642020-08-27 22:43:03 +02009756 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009757 && ea.cmdidx != CMD_elseif
9758 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009759 && ea.cmdidx != CMD_endif
9760 && ea.cmdidx != CMD_endfor
9761 && ea.cmdidx != CMD_endwhile
9762 && ea.cmdidx != CMD_catch
9763 && ea.cmdidx != CMD_finally
9764 && ea.cmdidx != CMD_endtry)
9765 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009766 emsg(_(e_unreachable_code_after_return));
9767 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009768 }
9769
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009770 p = skipwhite(p);
9771 if (ea.cmdidx != CMD_SIZE
9772 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9773 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009774 if (ea.cmdidx >= 0)
9775 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009776 if ((ea.argt & EX_BANG) && *p == '!')
9777 {
9778 ea.forceit = TRUE;
9779 p = skipwhite(p + 1);
9780 }
9781 }
9782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009783 switch (ea.cmdidx)
9784 {
9785 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009786 ea.arg = p;
9787 line = compile_nested_function(&ea, &cctx);
9788 break;
9789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009790 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009791 // TODO: should we allow this, e.g. to declare a global
9792 // function?
9793 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009794 goto erret;
9795
9796 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009797 line = compile_return(p, check_return_type,
9798 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009799 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009800 break;
9801
9802 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009803 emsg(_(e_cannot_use_let_in_vim9_script));
9804 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009805 case CMD_var:
9806 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009807 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009808 case CMD_increment:
9809 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009810 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009811 if (line == p)
9812 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009813 break;
9814
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009815 case CMD_unlet:
9816 case CMD_unlockvar:
9817 case CMD_lockvar:
9818 line = compile_unletlock(p, &ea, &cctx);
9819 break;
9820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009821 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009822 emsg(_(e_import_can_only_be_used_in_script));
9823 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009824 break;
9825
9826 case CMD_if:
9827 line = compile_if(p, &cctx);
9828 break;
9829 case CMD_elseif:
9830 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009831 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009832 break;
9833 case CMD_else:
9834 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009835 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009836 break;
9837 case CMD_endif:
9838 line = compile_endif(p, &cctx);
9839 break;
9840
9841 case CMD_while:
9842 line = compile_while(p, &cctx);
9843 break;
9844 case CMD_endwhile:
9845 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009846 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009847 break;
9848
9849 case CMD_for:
9850 line = compile_for(p, &cctx);
9851 break;
9852 case CMD_endfor:
9853 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009854 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009855 break;
9856 case CMD_continue:
9857 line = compile_continue(p, &cctx);
9858 break;
9859 case CMD_break:
9860 line = compile_break(p, &cctx);
9861 break;
9862
9863 case CMD_try:
9864 line = compile_try(p, &cctx);
9865 break;
9866 case CMD_catch:
9867 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009868 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009869 break;
9870 case CMD_finally:
9871 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009872 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009873 break;
9874 case CMD_endtry:
9875 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009876 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009877 break;
9878 case CMD_throw:
9879 line = compile_throw(p, &cctx);
9880 break;
9881
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009882 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009883 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009884 break;
9885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009886 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009887 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009888 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009889 case CMD_echomsg:
9890 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +02009891 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009892 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009893 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009894
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009895 case CMD_put:
9896 ea.cmd = cmd;
9897 line = compile_put(p, &ea, &cctx);
9898 break;
9899
Bram Moolenaar4c137212021-04-19 16:48:48 +02009900 case CMD_substitute:
9901 if (cctx.ctx_skip == SKIP_YES)
9902 line = (char_u *)"";
9903 else
9904 {
9905 ea.arg = p;
9906 line = compile_substitute(line, &ea, &cctx);
9907 }
9908 break;
9909
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009910 case CMD_redir:
9911 ea.arg = p;
9912 line = compile_redir(line, &ea, &cctx);
9913 break;
9914
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009915 case CMD_cexpr:
9916 case CMD_lexpr:
9917 case CMD_caddexpr:
9918 case CMD_laddexpr:
9919 case CMD_cgetexpr:
9920 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009921#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009922 ea.arg = p;
9923 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009924#else
9925 ex_ni(&ea);
9926 line = NULL;
9927#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009928 break;
9929
Bram Moolenaarae616492020-07-28 20:07:27 +02009930 case CMD_append:
9931 case CMD_change:
9932 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009933 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009934 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009935 case CMD_xit:
9936 not_in_vim9(&ea);
9937 goto erret;
9938
Bram Moolenaar002262f2020-07-08 17:47:57 +02009939 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009940 if (cctx.ctx_skip != SKIP_YES)
9941 {
9942 semsg(_(e_invalid_command_str), ea.cmd);
9943 goto erret;
9944 }
9945 // We don't check for a next command here.
9946 line = (char_u *)"";
9947 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009948
Bram Moolenaar20677332021-06-06 17:02:53 +02009949 case CMD_lua:
9950 case CMD_mzscheme:
9951 case CMD_perl:
9952 case CMD_py3:
9953 case CMD_python3:
9954 case CMD_python:
9955 case CMD_pythonx:
9956 case CMD_ruby:
9957 case CMD_tcl:
9958 ea.arg = p;
9959 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009960 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009961 else
9962 // heredoc lines have been concatenated with NL
9963 // characters in get_function_body()
9964 line = compile_script(line, &cctx);
9965 break;
9966
9967 default:
9968 // Not recognized, execute with do_cmdline_cmd().
9969 ea.arg = p;
9970 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009971 break;
9972 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009973nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009974 if (line == NULL)
9975 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009976 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009977
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009978 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009979 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009981 if (cctx.ctx_type_stack.ga_len < 0)
9982 {
9983 iemsg("Type stack underflow");
9984 goto erret;
9985 }
9986 }
9987
9988 if (cctx.ctx_scope != NULL)
9989 {
9990 if (cctx.ctx_scope->se_type == IF_SCOPE)
9991 emsg(_(e_endif));
9992 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9993 emsg(_(e_endwhile));
9994 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9995 emsg(_(e_endfor));
9996 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009997 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009998 goto erret;
9999 }
10000
Bram Moolenaarefd88552020-06-18 20:50:10 +020010001 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010002 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010003 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10004 ufunc->uf_ret_type = &t_void;
10005 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010006 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010007 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010008 goto erret;
10009 }
10010
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010011 // Return void if there is no return at the end.
10012 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010013 }
10014
Bram Moolenaar599410c2021-04-10 14:03:43 +020010015 // When compiled with ":silent!" and there was an error don't consider the
10016 // function compiled.
10017 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010018 {
10019 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10020 + ufunc->uf_dfunc_idx;
10021 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010022 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010023#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010024 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010025 {
10026 dfunc->df_instr_prof = instr->ga_data;
10027 dfunc->df_instr_prof_count = instr->ga_len;
10028 }
10029 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010030#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010031 if (cctx.ctx_compile_type == CT_DEBUG)
10032 {
10033 dfunc->df_instr_debug = instr->ga_data;
10034 dfunc->df_instr_debug_count = instr->ga_len;
10035 }
10036 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010037 {
10038 dfunc->df_instr = instr->ga_data;
10039 dfunc->df_instr_count = instr->ga_len;
10040 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010041 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010042 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010043 if (cctx.ctx_outer_used)
10044 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010045 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010046 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010047
10048 ret = OK;
10049
10050erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010051 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010052 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010053 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10054 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010055
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010056 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010057 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010058 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010059 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010060
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010061 // If using the last entry in the table and it was added above, we
10062 // might as well remove it.
10063 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010064 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010065 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010066 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010067 ufunc->uf_dfunc_idx = 0;
10068 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010069 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010070
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010071 while (cctx.ctx_scope != NULL)
10072 drop_scope(&cctx);
10073
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010074 if (errormsg != NULL)
10075 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010076 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010077 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010078 }
10079
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010080 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10081 {
10082 if (ret == OK)
10083 {
10084 emsg(_(e_missing_redir_end));
10085 ret = FAIL;
10086 }
10087 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010088 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010089 }
10090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010091 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010092 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010093 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010094 if (do_estack_push)
10095 estack_pop();
10096
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010097 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010098 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010099 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010100 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010101 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010102}
10103
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010104 void
10105set_function_type(ufunc_T *ufunc)
10106{
10107 int varargs = ufunc->uf_va_name != NULL;
10108 int argcount = ufunc->uf_args.ga_len;
10109
10110 // Create a type for the function, with the return type and any
10111 // argument types.
10112 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10113 // The type is included in "tt_args".
10114 if (argcount > 0 || varargs)
10115 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010116 if (ufunc->uf_type_list.ga_itemsize == 0)
10117 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010118 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10119 argcount, &ufunc->uf_type_list);
10120 // Add argument types to the function type.
10121 if (func_type_add_arg_types(ufunc->uf_func_type,
10122 argcount + varargs,
10123 &ufunc->uf_type_list) == FAIL)
10124 return;
10125 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10126 ufunc->uf_func_type->tt_min_argcount =
10127 argcount - ufunc->uf_def_args.ga_len;
10128 if (ufunc->uf_arg_types == NULL)
10129 {
10130 int i;
10131
10132 // lambda does not have argument types.
10133 for (i = 0; i < argcount; ++i)
10134 ufunc->uf_func_type->tt_args[i] = &t_any;
10135 }
10136 else
10137 mch_memmove(ufunc->uf_func_type->tt_args,
10138 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10139 if (varargs)
10140 {
10141 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010142 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010143 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10144 }
10145 }
10146 else
10147 // No arguments, can use a predefined type.
10148 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10149 argcount, &ufunc->uf_type_list);
10150}
10151
10152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010153/*
10154 * Delete an instruction, free what it contains.
10155 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010156 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010157delete_instr(isn_T *isn)
10158{
10159 switch (isn->isn_type)
10160 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010161 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010162 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010163 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010164 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010165 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010166 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010167 case ISN_LOADENV:
10168 case ISN_LOADG:
10169 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010170 case ISN_LOADT:
10171 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010172 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010173 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010174 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010175 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010176 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010177 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010178 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010179 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010180 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010181 case ISN_STOREW:
10182 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010183 vim_free(isn->isn_arg.string);
10184 break;
10185
Bram Moolenaar4c137212021-04-19 16:48:48 +020010186 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010187 {
10188 int idx;
10189 isn_T *list = isn->isn_arg.subs.subs_instr;
10190
10191 vim_free(isn->isn_arg.subs.subs_cmd);
10192 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10193 delete_instr(list + idx);
10194 vim_free(list);
10195 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010196 break;
10197
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010198 case ISN_INSTR:
10199 {
10200 int idx;
10201 isn_T *list = isn->isn_arg.instr;
10202
10203 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10204 delete_instr(list + idx);
10205 vim_free(list);
10206 }
10207 break;
10208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010209 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010210 case ISN_STORES:
10211 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010212 break;
10213
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010214 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010215 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010216 vim_free(isn->isn_arg.unlet.ul_name);
10217 break;
10218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010219 case ISN_STOREOPT:
10220 vim_free(isn->isn_arg.storeopt.so_name);
10221 break;
10222
10223 case ISN_PUSHBLOB: // push blob isn_arg.blob
10224 blob_unref(isn->isn_arg.blob);
10225 break;
10226
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010227 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010228#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010229 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010230#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010231 break;
10232
10233 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010234#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010235 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010236#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010237 break;
10238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010239 case ISN_UCALL:
10240 vim_free(isn->isn_arg.ufunc.cuf_name);
10241 break;
10242
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010243 case ISN_FUNCREF:
10244 {
10245 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10246 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010247 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010248
Bram Moolenaar077a4232020-12-22 18:33:27 +010010249 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10250 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010251 }
10252 break;
10253
10254 case ISN_DCALL:
10255 {
10256 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10257 + isn->isn_arg.dfunc.cdf_idx;
10258
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010259 if (dfunc->df_ufunc != NULL
10260 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010261 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010262 }
10263 break;
10264
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010265 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010266 {
10267 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10268 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10269
10270 if (ufunc != NULL)
10271 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010272 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010273 func_ptr_unref(ufunc);
10274 }
10275
10276 vim_free(lambda);
10277 vim_free(isn->isn_arg.newfunc.nf_global);
10278 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010279 break;
10280
Bram Moolenaar5e654232020-09-16 15:22:00 +020010281 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010282 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010283 free_type(isn->isn_arg.type.ct_type);
10284 break;
10285
Bram Moolenaar02194d22020-10-24 23:08:38 +020010286 case ISN_CMDMOD:
10287 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10288 ->cmod_filter_regmatch.regprog);
10289 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10290 break;
10291
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010292 case ISN_LOADSCRIPT:
10293 case ISN_STORESCRIPT:
10294 vim_free(isn->isn_arg.script.scriptref);
10295 break;
10296
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010297 case ISN_TRY:
10298 vim_free(isn->isn_arg.try.try_ref);
10299 break;
10300
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010301 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010302 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010303 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10304 break;
10305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010306 case ISN_2BOOL:
10307 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010308 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010309 case ISN_ADDBLOB:
10310 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010311 case ISN_ANYINDEX:
10312 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010313 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010314 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010315 case ISN_BLOBINDEX:
10316 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010317 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010318 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010319 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010320 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010321 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010322 case ISN_COMPAREANY:
10323 case ISN_COMPAREBLOB:
10324 case ISN_COMPAREBOOL:
10325 case ISN_COMPAREDICT:
10326 case ISN_COMPAREFLOAT:
10327 case ISN_COMPAREFUNC:
10328 case ISN_COMPARELIST:
10329 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010330 case ISN_COMPARESPECIAL:
10331 case ISN_COMPARESTRING:
10332 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010333 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010334 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010335 case ISN_DROP:
10336 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010337 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010338 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010339 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010340 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010341 case ISN_EXECCONCAT:
10342 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010343 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010344 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010345 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010346 case ISN_GETITEM:
10347 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010348 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010349 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010350 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010351 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010352 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010353 case ISN_LOADBDICT:
10354 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010355 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010356 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010357 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010358 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010359 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010360 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010361 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010362 case ISN_NEGATENR:
10363 case ISN_NEWDICT:
10364 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010365 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010366 case ISN_OPFLOAT:
10367 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010368 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010369 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010370 case ISN_PROF_END:
10371 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010372 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010373 case ISN_PUSHF:
10374 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010375 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010376 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010377 case ISN_REDIREND:
10378 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010379 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010380 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010381 case ISN_SHUFFLE:
10382 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010383 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010384 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010385 case ISN_STORENR:
10386 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010387 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010388 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010389 case ISN_STOREV:
10390 case ISN_STRINDEX:
10391 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010392 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010393 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010394 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010395 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010396 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010397 // nothing allocated
10398 break;
10399 }
10400}
10401
10402/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010403 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010404 */
10405 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010406delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010407{
10408 int idx;
10409
10410 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010411 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010412
10413 if (dfunc->df_instr != NULL)
10414 {
10415 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10416 delete_instr(dfunc->df_instr + idx);
10417 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010418 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010419 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010420 if (dfunc->df_instr_debug != NULL)
10421 {
10422 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10423 delete_instr(dfunc->df_instr_debug + idx);
10424 VIM_CLEAR(dfunc->df_instr_debug);
10425 dfunc->df_instr_debug = NULL;
10426 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010427#ifdef FEAT_PROFILE
10428 if (dfunc->df_instr_prof != NULL)
10429 {
10430 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10431 delete_instr(dfunc->df_instr_prof + idx);
10432 VIM_CLEAR(dfunc->df_instr_prof);
10433 dfunc->df_instr_prof = NULL;
10434 }
10435#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010436
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010437 if (mark_deleted)
10438 dfunc->df_deleted = TRUE;
10439 if (dfunc->df_ufunc != NULL)
10440 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010441}
10442
10443/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010444 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010445 * function, unless another user function still uses it.
10446 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010447 */
10448 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010449unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010450{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010451 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010452 {
10453 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10454 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010455
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010456 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010457 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010458 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010459 ufunc->uf_dfunc_idx = 0;
10460 if (dfunc->df_ufunc == ufunc)
10461 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010462 }
10463}
10464
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010465/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010466 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010467 */
10468 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010469link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010470{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010471 if (ufunc->uf_dfunc_idx > 0)
10472 {
10473 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10474 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010475
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010476 ++dfunc->df_refcount;
10477 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010478}
10479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010480#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010481/*
10482 * Free all functions defined with ":def".
10483 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010484 void
10485free_def_functions(void)
10486{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010487 int idx;
10488
10489 for (idx = 0; idx < def_functions.ga_len; ++idx)
10490 {
10491 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10492
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010493 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010494 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010495 }
10496
10497 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010498}
10499#endif
10500
10501
10502#endif // FEAT_EVAL