blob: c5359fe54df8222f7aa8f4cae02c9c5d67753c72 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200117// Destination for an assignment or ":unlet" with an index.
118typedef enum {
119 dest_local,
120 dest_option,
121 dest_env,
122 dest_global,
123 dest_buffer,
124 dest_window,
125 dest_tab,
126 dest_vimvar,
127 dest_script,
128 dest_reg,
129 dest_expr,
130} assign_dest_T;
131
132// Used by compile_lhs() to store information about the LHS of an assignment
133// and one argument of ":unlet" with an index.
134typedef struct {
135 assign_dest_T lhs_dest; // type of destination
136
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200137 char_u *lhs_name; // allocated name excluding the last
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200138 // "[expr]" or ".name".
139 size_t lhs_varlen; // length of the variable without
140 // "[expr]" or ".name"
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200141 char_u *lhs_whole; // allocated name including the last
142 // "[expr]" or ".name" for :redir
143 size_t lhs_varlen_total; // length of the variable including
144 // any "[expr]" or ".name"
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200145 char_u *lhs_dest_end; // end of the destination, including
146 // "[expr]" or ".name".
147
148 int lhs_has_index; // has "[expr]" or ".name"
149
150 int lhs_new_local; // create new local variable
151 int lhs_opt_flags; // for when destination is an option
152 int lhs_vimvaridx; // for when destination is a v:var
153
154 lvar_T lhs_local_lvar; // used for existing local destination
155 lvar_T lhs_arg_lvar; // used for argument destination
156 lvar_T *lhs_lvar; // points to destination lvar
157 int lhs_scriptvar_sid;
158 int lhs_scriptvar_idx;
159
160 int lhs_has_type; // type was specified
161 type_T *lhs_type;
162 type_T *lhs_member_type;
163
164 int lhs_append; // used by ISN_REDIREND
165} lhs_T;
166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167/*
168 * Context for compiling lines of Vim script.
169 * Stores info about the local variables and condition stack.
170 */
171struct cctx_S {
172 ufunc_T *ctx_ufunc; // current function
173 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200174 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 garray_T ctx_instr; // generated instructions
176
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200177 int ctx_prev_lnum; // line number below previous command, for
178 // debugging
179
Bram Moolenaare99d4222021-06-13 14:01:26 +0200180 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200184 int ctx_has_closure; // set to one if a closures was created in
185 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100187 garray_T ctx_imports; // imported items
188
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200189 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200191 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193 cctx_T *ctx_outer; // outer scope for lambda or nested
194 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200198 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200199
Bram Moolenaar02194d22020-10-24 23:08:38 +0200200 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200201
202 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
203 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204};
205
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100206static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207
208/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100209 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100210 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100211 * If "lvar" is NULL only check if the variable can be found.
212 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100214 static int
215lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100218 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100221 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200222
223 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
225 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100226 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
227 if (STRNCMP(name, lvp->lv_name, len) == 0
228 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200229 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100230 if (lvar != NULL)
231 {
232 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100233 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100234 }
235 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200238
239 // Find local in outer function scope.
240 if (cctx->ctx_outer != NULL)
241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100242 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200243 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100244 if (lvar != NULL)
245 {
246 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100247 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100248 }
249 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200250 }
251 }
252
Bram Moolenaar709664c2020-12-12 14:33:41 +0100253 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254}
255
256/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200257 * Lookup an argument in the current function and an enclosing function.
258 * Returns the argument index in "idxp"
259 * Returns the argument type in "type"
260 * Sets "gen_load_outer" to TRUE if found in outer scope.
261 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 */
263 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200265 char_u *name,
266 size_t len,
267 int *idxp,
268 type_T **type,
269 int *gen_load_outer,
270 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271{
272 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200273 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100275 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200276 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200277 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
279 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
280
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200281 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
282 {
283 if (idxp != NULL)
284 {
285 // Arguments are located above the frame pointer. One further
286 // if there is a vararg argument
287 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
288 + STACK_FRAME_SIZE)
289 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
290
291 if (cctx->ctx_ufunc->uf_arg_types != NULL)
292 *type = cctx->ctx_ufunc->uf_arg_types[idx];
293 else
294 *type = &t_any;
295 }
296 return OK;
297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200300 va_name = cctx->ctx_ufunc->uf_va_name;
301 if (va_name != NULL
302 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
303 {
304 if (idxp != NULL)
305 {
306 // varargs is always the last argument
307 *idxp = -STACK_FRAME_SIZE - 1;
308 *type = cctx->ctx_ufunc->uf_va_type;
309 }
310 return OK;
311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200313 if (cctx->ctx_outer != NULL)
314 {
315 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200316 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200317 == OK)
318 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100319 if (gen_load_outer != NULL)
320 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200321 return OK;
322 }
323 }
324
325 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326}
327
328/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329 * Lookup a script-local variable in the current script, possibly defined in a
330 * block that contains the function "cctx->ctx_ufunc".
331 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100332 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * Return NULL when not found.
334 */
335 static sallvar_T *
336find_script_var(char_u *name, size_t len, cctx_T *cctx)
337{
338 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
339 hashitem_T *hi;
340 int cc;
341 sallvar_T *sav;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200342 sallvar_T *found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 ufunc_T *ufunc;
344
345 // Find the list of all script variables with the right name.
346 if (len > 0)
347 {
348 cc = name[len];
349 name[len] = NUL;
350 }
351 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
352 if (len > 0)
353 name[len] = cc;
354 if (HASHITEM_EMPTY(hi))
355 return NULL;
356
357 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200358 if (sav->sav_block_id == 0)
359 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200360 return sav;
361
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200362 if (cctx == NULL)
363 {
364 // Not in a function scope, find variable with block id equal to or
365 // smaller than the current block id.
366 while (sav != NULL)
367 {
368 if (sav->sav_block_id <= si->sn_current_block_id)
369 break;
370 sav = sav->sav_next;
371 }
372 return sav;
373 }
374
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200375 // Go over the variables with this name and find one that was visible
376 // from the function.
377 ufunc = cctx->ctx_ufunc;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200378 found_sav = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200379 while (sav != NULL)
380 {
381 int idx;
382
383 // Go over the blocks that this function was defined in. If the
384 // variable block ID matches it was visible to the function.
385 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
386 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
387 return sav;
388 sav = sav->sav_next;
389 }
390
Bram Moolenaar88421d62021-07-24 14:14:52 +0200391 // Not found, assume variable at script level was visible.
392 return found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200393}
394
395/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100396 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200397 */
398 static int
399script_is_vim9()
400{
401 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
402}
403
404/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200405 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200406 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407 * Returns OK or FAIL.
408 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200409 static int
410script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200412 if (current_sctx.sc_sid <= 0)
413 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200414 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200415 {
416 // Check script variables that were visible where the function was
417 // defined.
418 if (find_script_var(name, len, cctx) != NULL)
419 return OK;
420 }
421 else
422 {
423 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
424 dictitem_T *di;
425 int cc;
426
427 // Check script variables that are currently visible
428 cc = name[len];
429 name[len] = NUL;
430 di = find_var_in_ht(ht, 0, name, TRUE);
431 name[len] = cc;
432 if (di != NULL)
433 return OK;
434 }
435
436 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437}
438
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100439/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100440 * Return TRUE if "name" is a local variable, argument, script variable or
441 * imported.
442 */
443 static int
444variable_exists(char_u *name, size_t len, cctx_T *cctx)
445{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100446 return (cctx != NULL
447 && (lookup_local(name, len, NULL, cctx) == OK
448 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200449 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100450 || find_imported(name, len, cctx) != NULL;
451}
452
453/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100454 * Return TRUE if "name" is a local variable, argument, script variable,
455 * imported or function.
456 */
457 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100458item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100459{
460 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100461 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100462
463 if (variable_exists(name, len, cctx))
464 return TRUE;
465
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100466 // This is similar to what is in lookup_scriptitem():
467 // Find a function, so that a following "->" works.
468 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
469 // a function call.
470 p = skipwhite(name + len);
471
472 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
473 {
474 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200475 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100476 // Skip "g:" before a function name.
477 is_global = (name[0] == 'g' && name[1] == ':');
478 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
479 }
480 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100481}
482
483/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100484 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200485 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200486 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100487 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100488 * Return FAIL and give an error if it defined.
489 */
490 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100491check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100492{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200493 int c = p[len];
494 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200495
Bram Moolenaarda479c72021-04-10 21:01:38 +0200496 // underscore argument is OK
497 if (len == 1 && *p == '_')
498 return OK;
499
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200500 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100501 {
502 if (is_arg)
503 semsg(_(e_argument_already_declared_in_script_str), p);
504 else
505 semsg(_(e_variable_already_declared_in_script_str), p);
506 return FAIL;
507 }
508
Bram Moolenaarad486a02020-08-01 23:22:18 +0200509 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100510 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100511 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200512 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200513 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200514 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100515 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200516 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200517 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
518 && (!func_is_global(ufunc)
519 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200520 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100521 if (is_arg)
522 semsg(_(e_argument_name_shadows_existing_variable_str), p);
523 else
524 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200525 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200526 return FAIL;
527 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100528 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200529 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100530 return OK;
531}
532
Bram Moolenaar65b95452020-07-19 14:03:09 +0200533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534/////////////////////////////////////////////////////////////////////
535// Following generate_ functions expect the caller to call ga_grow().
536
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200537#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
538#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100540/*
541 * Generate an instruction without arguments.
542 * Returns a pointer to the new instruction, NULL if failed.
543 */
544 static isn_T *
545generate_instr(cctx_T *cctx, isntype_T isn_type)
546{
547 garray_T *instr = &cctx->ctx_instr;
548 isn_T *isn;
549
Bram Moolenaar080457c2020-03-03 21:53:32 +0100550 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar35578162021-08-02 19:10:38 +0200551 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 return NULL;
553 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
554 isn->isn_type = isn_type;
555 isn->isn_lnum = cctx->ctx_lnum + 1;
556 ++instr->ga_len;
557
558 return isn;
559}
560
561/*
562 * Generate an instruction without arguments.
563 * "drop" will be removed from the stack.
564 * Returns a pointer to the new instruction, NULL if failed.
565 */
566 static isn_T *
567generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
568{
569 garray_T *stack = &cctx->ctx_type_stack;
570
Bram Moolenaar080457c2020-03-03 21:53:32 +0100571 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 stack->ga_len -= drop;
573 return generate_instr(cctx, isn_type);
574}
575
576/*
577 * Generate instruction "isn_type" and put "type" on the type stack.
578 */
579 static isn_T *
580generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
581{
582 isn_T *isn;
583 garray_T *stack = &cctx->ctx_type_stack;
584
585 if ((isn = generate_instr(cctx, isn_type)) == NULL)
586 return NULL;
587
Bram Moolenaar35578162021-08-02 19:10:38 +0200588 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200590 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 ++stack->ga_len;
592
593 return isn;
594}
595
596/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200597 * Generate an ISN_DEBUG instruction.
598 */
599 static isn_T *
600generate_instr_debug(cctx_T *cctx)
601{
602 isn_T *isn;
603 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
604 + cctx->ctx_ufunc->uf_dfunc_idx;
605
606 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
607 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200608 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
609 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200610 return isn;
611}
612
613/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200615 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200616 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 */
618 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200619may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620{
621 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200622 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100624 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100626 RETURN_OK_IF_SKIP(cctx);
627 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200628 switch ((*type)->tt_type)
629 {
630 // nothing to be done
631 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200633 // conversion possible
634 case VAR_SPECIAL:
635 case VAR_BOOL:
636 case VAR_NUMBER:
637 case VAR_FLOAT:
638 break;
639
640 // conversion possible (with runtime check)
641 case VAR_ANY:
642 case VAR_UNKNOWN:
643 isntype = ISN_2STRING_ANY;
644 break;
645
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200646 // conversion possible when tolerant
647 case VAR_LIST:
648 if (tolerant)
649 {
650 isntype = ISN_2STRING_ANY;
651 break;
652 }
653 // FALLTHROUGH
654
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200655 // conversion not possible
656 case VAR_VOID:
657 case VAR_BLOB:
658 case VAR_FUNC:
659 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200660 case VAR_DICT:
661 case VAR_JOB:
662 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200663 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200664 to_string_error((*type)->tt_type);
665 return FAIL;
666 }
667
668 *type = &t_string;
669 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200671 isn->isn_arg.tostring.offset = offset;
672 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673
674 return OK;
675}
676
677 static int
678check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
679{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200680 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 {
684 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200685 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200687 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 return FAIL;
689 }
690 return OK;
691}
692
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200693 static int
694generate_add_instr(
695 cctx_T *cctx,
696 vartype_T vartype,
697 type_T *type1,
698 type_T *type2)
699{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100700 garray_T *stack = &cctx->ctx_type_stack;
701 isn_T *isn = generate_instr_drop(cctx,
702 vartype == VAR_NUMBER ? ISN_OPNR
703 : vartype == VAR_LIST ? ISN_ADDLIST
704 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200705#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100706 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200707#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100708 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200709
710 if (vartype != VAR_LIST && vartype != VAR_BLOB
711 && type1->tt_type != VAR_ANY
712 && type2->tt_type != VAR_ANY
713 && check_number_or_float(
714 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
715 return FAIL;
716
717 if (isn != NULL)
718 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100719
720 // When concatenating two lists with different member types the member type
721 // becomes "any".
722 if (vartype == VAR_LIST
723 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
724 && type1->tt_member != type2->tt_member)
725 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
726
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200727 return isn == NULL ? FAIL : OK;
728}
729
730/*
731 * Get the type to use for an instruction for an operation on "type1" and
732 * "type2". If they are matching use a type-specific instruction. Otherwise
733 * fall back to runtime type checking.
734 */
735 static vartype_T
736operator_type(type_T *type1, type_T *type2)
737{
738 if (type1->tt_type == type2->tt_type
739 && (type1->tt_type == VAR_NUMBER
740 || type1->tt_type == VAR_LIST
741#ifdef FEAT_FLOAT
742 || type1->tt_type == VAR_FLOAT
743#endif
744 || type1->tt_type == VAR_BLOB))
745 return type1->tt_type;
746 return VAR_ANY;
747}
748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749/*
750 * Generate an instruction with two arguments. The instruction depends on the
751 * type of the arguments.
752 */
753 static int
754generate_two_op(cctx_T *cctx, char_u *op)
755{
756 garray_T *stack = &cctx->ctx_type_stack;
757 type_T *type1;
758 type_T *type2;
759 vartype_T vartype;
760 isn_T *isn;
761
Bram Moolenaar080457c2020-03-03 21:53:32 +0100762 RETURN_OK_IF_SKIP(cctx);
763
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200764 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
766 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200767 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768
769 switch (*op)
770 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200771 case '+':
772 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 break;
775
776 case '-':
777 case '*':
778 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
779 op) == FAIL)
780 return FAIL;
781 if (vartype == VAR_NUMBER)
782 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
783#ifdef FEAT_FLOAT
784 else if (vartype == VAR_FLOAT)
785 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
786#endif
787 else
788 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
789 if (isn != NULL)
790 isn->isn_arg.op.op_type = *op == '*'
791 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
792 break;
793
Bram Moolenaar4c683752020-04-05 21:38:23 +0200794 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200796 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100797 && type2->tt_type != VAR_NUMBER))
798 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200799 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 return FAIL;
801 }
802 isn = generate_instr_drop(cctx,
803 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
804 if (isn != NULL)
805 isn->isn_arg.op.op_type = EXPR_REM;
806 break;
807 }
808
809 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200810 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 {
812 type_T *type = &t_any;
813
814#ifdef FEAT_FLOAT
815 // float+number and number+float results in float
816 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
817 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
818 type = &t_float;
819#endif
820 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
821 }
822
823 return OK;
824}
825
826/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200827 * Get the instruction to use for comparing "type1" with "type2"
828 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200830 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100831get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832{
833 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834
Bram Moolenaar4c683752020-04-05 21:38:23 +0200835 if (type1 == VAR_UNKNOWN)
836 type1 = VAR_ANY;
837 if (type2 == VAR_UNKNOWN)
838 type2 = VAR_ANY;
839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 if (type1 == type2)
841 {
842 switch (type1)
843 {
844 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
845 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
846 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
847 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
848 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
849 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
850 case VAR_LIST: isntype = ISN_COMPARELIST; break;
851 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
852 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 default: isntype = ISN_COMPAREANY; break;
854 }
855 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200856 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100858 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 isntype = ISN_COMPAREANY;
860
Bram Moolenaar657137c2021-01-09 15:45:23 +0100861 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 && (isntype == ISN_COMPAREBOOL
863 || isntype == ISN_COMPARESPECIAL
864 || isntype == ISN_COMPARENR
865 || isntype == ISN_COMPAREFLOAT))
866 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200867 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100868 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200869 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 }
871 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100872 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
874 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100875 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
876 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 && (type1 == VAR_BLOB || type2 == VAR_BLOB
878 || type1 == VAR_LIST || type2 == VAR_LIST))))
879 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200880 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200882 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200884 return isntype;
885}
886
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200887 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100888check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200889{
890 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
891 return FAIL;
892 return OK;
893}
894
Bram Moolenaara5565e42020-05-09 15:44:01 +0200895/*
896 * Generate an ISN_COMPARE* instruction with a boolean result.
897 */
898 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100899generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200900{
901 isntype_T isntype;
902 isn_T *isn;
903 garray_T *stack = &cctx->ctx_type_stack;
904 vartype_T type1;
905 vartype_T type2;
906
907 RETURN_OK_IF_SKIP(cctx);
908
909 // Get the known type of the two items on the stack. If they are matching
910 // use a type-specific instruction. Otherwise fall back to runtime type
911 // checking.
912 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
913 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100914 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200915 if (isntype == ISN_DROP)
916 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917
918 if ((isn = generate_instr(cctx, isntype)) == NULL)
919 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100920 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921 isn->isn_arg.op.op_ic = ic;
922
923 // takes two arguments, puts one bool back
924 if (stack->ga_len >= 2)
925 {
926 --stack->ga_len;
927 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
928 }
929
930 return OK;
931}
932
933/*
934 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200935 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 */
937 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200938generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939{
940 isn_T *isn;
941 garray_T *stack = &cctx->ctx_type_stack;
942
Bram Moolenaar080457c2020-03-03 21:53:32 +0100943 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
945 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200946 isn->isn_arg.tobool.invert = invert;
947 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948
949 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200950 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951
952 return OK;
953}
954
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200955/*
956 * Generate an ISN_COND2BOOL instruction.
957 */
958 static int
959generate_COND2BOOL(cctx_T *cctx)
960{
961 isn_T *isn;
962 garray_T *stack = &cctx->ctx_type_stack;
963
964 RETURN_OK_IF_SKIP(cctx);
965 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
966 return FAIL;
967
968 // type becomes bool
969 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
970
971 return OK;
972}
973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200975generate_TYPECHECK(
976 cctx_T *cctx,
977 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100978 int offset,
979 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980{
981 isn_T *isn;
982 garray_T *stack = &cctx->ctx_type_stack;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
986 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200987 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100988 isn->isn_arg.type.ct_off = (int8_T)offset;
989 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990
Bram Moolenaar5e654232020-09-16 15:22:00 +0200991 // type becomes expected
992 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993
994 return OK;
995}
996
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100997 static int
998generate_SETTYPE(
999 cctx_T *cctx,
1000 type_T *expected)
1001{
1002 isn_T *isn;
1003
1004 RETURN_OK_IF_SKIP(cctx);
1005 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
1006 return FAIL;
1007 isn->isn_arg.type.ct_type = alloc_type(expected);
1008 return OK;
1009}
1010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001012 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
1013 * used. Return FALSE if the types will never match.
1014 */
Bram Moolenaar193f6202020-11-16 20:08:35 +01001015 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001016use_typecheck(type_T *actual, type_T *expected)
1017{
1018 if (actual->tt_type == VAR_ANY
1019 || actual->tt_type == VAR_UNKNOWN
1020 || (actual->tt_type == VAR_FUNC
1021 && (expected->tt_type == VAR_FUNC
1022 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001023 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1024 && ((actual->tt_member == &t_void)
1025 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001026 return TRUE;
1027 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1028 && actual->tt_type == expected->tt_type)
1029 // This takes care of a nested list or dict.
1030 return use_typecheck(actual->tt_member, expected->tt_member);
1031 return FALSE;
1032}
1033
1034/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001035 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001036 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001037 * - "actual" is a type that can be "expected" type: add a runtime check; or
1038 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001039 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1040 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001041 */
Bram Moolenaar351ead02021-01-16 16:07:01 +01001042 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001043need_type(
1044 type_T *actual,
1045 type_T *expected,
1046 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001047 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001048 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001049 int silent,
1050 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001051{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001052 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001053
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001054 if (expected == &t_bool && actual != &t_bool
1055 && (actual->tt_flags & TTFLAG_BOOL_OK))
1056 {
1057 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1058 // boolean is OK but requires a conversion.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001059 generate_2BOOL(cctx, FALSE, offset);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001060 return OK;
1061 }
1062
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001063 where.wt_index = arg_idx;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001064 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001065 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001066
1067 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001068 // If it's a constant a runtime check makes no sense.
Bram Moolenaar378697a2021-07-15 19:23:18 +02001069 if ((!actual_is_const || actual == &t_any)
1070 && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001071 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001072 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001073 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001074 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001075
1076 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001077 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001078 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001079}
1080
1081/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001082 * Check that the top of the type stack has a type that can be used as a
1083 * condition. Give an error and return FAIL if not.
1084 */
1085 static int
1086bool_on_stack(cctx_T *cctx)
1087{
1088 garray_T *stack = &cctx->ctx_type_stack;
1089 type_T *type;
1090
1091 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1092 if (type == &t_bool)
1093 return OK;
1094
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001095 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001096 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1097 // This requires a runtime type check.
1098 return generate_COND2BOOL(cctx);
1099
Bram Moolenaar351ead02021-01-16 16:07:01 +01001100 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001101}
1102
1103/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 * Generate an ISN_PUSHNR instruction.
1105 */
1106 static int
1107generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1108{
1109 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001110 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111
Bram Moolenaar080457c2020-03-03 21:53:32 +01001112 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1114 return FAIL;
1115 isn->isn_arg.number = number;
1116
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001117 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001118 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001119 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 return OK;
1121}
1122
1123/*
1124 * Generate an ISN_PUSHBOOL instruction.
1125 */
1126 static int
1127generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1128{
1129 isn_T *isn;
1130
Bram Moolenaar080457c2020-03-03 21:53:32 +01001131 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1133 return FAIL;
1134 isn->isn_arg.number = number;
1135
1136 return OK;
1137}
1138
1139/*
1140 * Generate an ISN_PUSHSPEC instruction.
1141 */
1142 static int
1143generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1144{
1145 isn_T *isn;
1146
Bram Moolenaar080457c2020-03-03 21:53:32 +01001147 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1149 return FAIL;
1150 isn->isn_arg.number = number;
1151
1152 return OK;
1153}
1154
1155#ifdef FEAT_FLOAT
1156/*
1157 * Generate an ISN_PUSHF instruction.
1158 */
1159 static int
1160generate_PUSHF(cctx_T *cctx, float_T fnumber)
1161{
1162 isn_T *isn;
1163
Bram Moolenaar080457c2020-03-03 21:53:32 +01001164 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1166 return FAIL;
1167 isn->isn_arg.fnumber = fnumber;
1168
1169 return OK;
1170}
1171#endif
1172
1173/*
1174 * Generate an ISN_PUSHS instruction.
1175 * Consumes "str".
1176 */
1177 static int
1178generate_PUSHS(cctx_T *cctx, char_u *str)
1179{
1180 isn_T *isn;
1181
Bram Moolenaar508b5612021-01-02 18:17:26 +01001182 if (cctx->ctx_skip == SKIP_YES)
1183 {
1184 vim_free(str);
1185 return OK;
1186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1188 return FAIL;
1189 isn->isn_arg.string = str;
1190
1191 return OK;
1192}
1193
1194/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001195 * Generate an ISN_PUSHCHANNEL instruction.
1196 * Consumes "channel".
1197 */
1198 static int
1199generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1200{
1201 isn_T *isn;
1202
Bram Moolenaar080457c2020-03-03 21:53:32 +01001203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001204 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1205 return FAIL;
1206 isn->isn_arg.channel = channel;
1207
1208 return OK;
1209}
1210
1211/*
1212 * Generate an ISN_PUSHJOB instruction.
1213 * Consumes "job".
1214 */
1215 static int
1216generate_PUSHJOB(cctx_T *cctx, job_T *job)
1217{
1218 isn_T *isn;
1219
Bram Moolenaar080457c2020-03-03 21:53:32 +01001220 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001221 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001222 return FAIL;
1223 isn->isn_arg.job = job;
1224
1225 return OK;
1226}
1227
1228/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 * Generate an ISN_PUSHBLOB instruction.
1230 * Consumes "blob".
1231 */
1232 static int
1233generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1234{
1235 isn_T *isn;
1236
Bram Moolenaar080457c2020-03-03 21:53:32 +01001237 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1239 return FAIL;
1240 isn->isn_arg.blob = blob;
1241
1242 return OK;
1243}
1244
1245/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001246 * Generate an ISN_PUSHFUNC instruction with name "name".
1247 * Consumes "name".
1248 */
1249 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001250generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001251{
1252 isn_T *isn;
1253
Bram Moolenaar080457c2020-03-03 21:53:32 +01001254 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001255 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001256 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001257 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001258
1259 return OK;
1260}
1261
1262/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001263 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001264 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1265 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001266 */
1267 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001268generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001269{
1270 isn_T *isn;
1271 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001272 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1273 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001274 type_T *item_type = &t_any;
1275
1276 RETURN_OK_IF_SKIP(cctx);
1277
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001278 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001279 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001280 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001281 emsg(_(e_listreq));
1282 return FAIL;
1283 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001284 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001285 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1286 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001287 isn->isn_arg.getitem.gi_index = index;
1288 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001289
1290 // add the item type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001291 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001292 return FAIL;
1293 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1294 ++stack->ga_len;
1295 return OK;
1296}
1297
1298/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001299 * Generate an ISN_SLICE instruction with "count".
1300 */
1301 static int
1302generate_SLICE(cctx_T *cctx, int count)
1303{
1304 isn_T *isn;
1305
1306 RETURN_OK_IF_SKIP(cctx);
1307 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.number = count;
1310 return OK;
1311}
1312
1313/*
1314 * Generate an ISN_CHECKLEN instruction with "min_len".
1315 */
1316 static int
1317generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1318{
1319 isn_T *isn;
1320
1321 RETURN_OK_IF_SKIP(cctx);
1322
1323 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1324 return FAIL;
1325 isn->isn_arg.checklen.cl_min_len = min_len;
1326 isn->isn_arg.checklen.cl_more_OK = more_OK;
1327
1328 return OK;
1329}
1330
1331/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 * Generate an ISN_STORE instruction.
1333 */
1334 static int
1335generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1336{
1337 isn_T *isn;
1338
Bram Moolenaar080457c2020-03-03 21:53:32 +01001339 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1341 return FAIL;
1342 if (name != NULL)
1343 isn->isn_arg.string = vim_strsave(name);
1344 else
1345 isn->isn_arg.number = idx;
1346
1347 return OK;
1348}
1349
1350/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001351 * Generate an ISN_STOREOUTER instruction.
1352 */
1353 static int
1354generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1355{
1356 isn_T *isn;
1357
1358 RETURN_OK_IF_SKIP(cctx);
1359 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1360 return FAIL;
1361 isn->isn_arg.outer.outer_idx = idx;
1362 isn->isn_arg.outer.outer_depth = level;
1363
1364 return OK;
1365}
1366
1367/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1369 */
1370 static int
1371generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1372{
1373 isn_T *isn;
1374
Bram Moolenaar080457c2020-03-03 21:53:32 +01001375 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1377 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001378 isn->isn_arg.storenr.stnr_idx = idx;
1379 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380
1381 return OK;
1382}
1383
1384/*
1385 * Generate an ISN_STOREOPT instruction
1386 */
1387 static int
1388generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1389{
1390 isn_T *isn;
1391
Bram Moolenaar080457c2020-03-03 21:53:32 +01001392 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001393 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 return FAIL;
1395 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1396 isn->isn_arg.storeopt.so_flags = opt_flags;
1397
1398 return OK;
1399}
1400
1401/*
1402 * Generate an ISN_LOAD or similar instruction.
1403 */
1404 static int
1405generate_LOAD(
1406 cctx_T *cctx,
1407 isntype_T isn_type,
1408 int idx,
1409 char_u *name,
1410 type_T *type)
1411{
1412 isn_T *isn;
1413
Bram Moolenaar080457c2020-03-03 21:53:32 +01001414 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1416 return FAIL;
1417 if (name != NULL)
1418 isn->isn_arg.string = vim_strsave(name);
1419 else
1420 isn->isn_arg.number = idx;
1421
1422 return OK;
1423}
1424
1425/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001426 * Generate an ISN_LOADOUTER instruction
1427 */
1428 static int
1429generate_LOADOUTER(
1430 cctx_T *cctx,
1431 int idx,
1432 int nesting,
1433 type_T *type)
1434{
1435 isn_T *isn;
1436
1437 RETURN_OK_IF_SKIP(cctx);
1438 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1439 return FAIL;
1440 isn->isn_arg.outer.outer_idx = idx;
1441 isn->isn_arg.outer.outer_depth = nesting;
1442
1443 return OK;
1444}
1445
1446/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001447 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001448 */
1449 static int
1450generate_LOADV(
1451 cctx_T *cctx,
1452 char_u *name,
1453 int error)
1454{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001455 int di_flags;
1456 int vidx = find_vim_var(name, &di_flags);
1457 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001458
Bram Moolenaar080457c2020-03-03 21:53:32 +01001459 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001460 if (vidx < 0)
1461 {
1462 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001463 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001464 return FAIL;
1465 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001466 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001467
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001468 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001469}
1470
1471/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001472 * Generate an ISN_UNLET instruction.
1473 */
1474 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001475generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001476{
1477 isn_T *isn;
1478
1479 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001480 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001481 return FAIL;
1482 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1483 isn->isn_arg.unlet.ul_forceit = forceit;
1484
1485 return OK;
1486}
1487
1488/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001489 * Generate an ISN_LOCKCONST instruction.
1490 */
1491 static int
1492generate_LOCKCONST(cctx_T *cctx)
1493{
1494 isn_T *isn;
1495
1496 RETURN_OK_IF_SKIP(cctx);
1497 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1498 return FAIL;
1499 return OK;
1500}
1501
1502/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 * Generate an ISN_LOADS instruction.
1504 */
1505 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001506generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001508 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001510 int sid,
1511 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512{
1513 isn_T *isn;
1514
Bram Moolenaar080457c2020-03-03 21:53:32 +01001515 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001516 if (isn_type == ISN_LOADS)
1517 isn = generate_instr_type(cctx, isn_type, type);
1518 else
1519 isn = generate_instr_drop(cctx, isn_type, 1);
1520 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001522 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1523 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001524
1525 return OK;
1526}
1527
1528/*
1529 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1530 */
1531 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001532generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 cctx_T *cctx,
1534 isntype_T isn_type,
1535 int sid,
1536 int idx,
1537 type_T *type)
1538{
1539 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001540 scriptref_T *sref;
1541 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542
Bram Moolenaar080457c2020-03-03 21:53:32 +01001543 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 if (isn_type == ISN_LOADSCRIPT)
1545 isn = generate_instr_type(cctx, isn_type, type);
1546 else
1547 isn = generate_instr_drop(cctx, isn_type, 1);
1548 if (isn == NULL)
1549 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001550
1551 // This requires three arguments, which doesn't fit in an instruction, thus
1552 // we need to allocate a struct for this.
1553 sref = ALLOC_ONE(scriptref_T);
1554 if (sref == NULL)
1555 return FAIL;
1556 isn->isn_arg.script.scriptref = sref;
1557 sref->sref_sid = sid;
1558 sref->sref_idx = idx;
1559 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001560 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 return OK;
1562}
1563
1564/*
1565 * Generate an ISN_NEWLIST instruction.
1566 */
1567 static int
1568generate_NEWLIST(cctx_T *cctx, int count)
1569{
1570 isn_T *isn;
1571 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 type_T *type;
1573 type_T *member;
1574
Bram Moolenaar080457c2020-03-03 21:53:32 +01001575 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1577 return FAIL;
1578 isn->isn_arg.number = count;
1579
Bram Moolenaar127542b2020-08-09 17:22:04 +02001580 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001581 if (count == 0)
1582 member = &t_void;
1583 else
1584 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001585 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1586 cctx->ctx_type_list);
1587 type = get_list_type(member, cctx->ctx_type_list);
1588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 // drop the value types
1590 stack->ga_len -= count;
1591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 // add the list type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001593 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 return FAIL;
1595 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1596 ++stack->ga_len;
1597
1598 return OK;
1599}
1600
1601/*
1602 * Generate an ISN_NEWDICT instruction.
1603 */
1604 static int
1605generate_NEWDICT(cctx_T *cctx, int count)
1606{
1607 isn_T *isn;
1608 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 type_T *type;
1610 type_T *member;
1611
Bram Moolenaar080457c2020-03-03 21:53:32 +01001612 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1614 return FAIL;
1615 isn->isn_arg.number = count;
1616
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001617 if (count == 0)
1618 member = &t_void;
1619 else
1620 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001621 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1622 cctx->ctx_type_list);
1623 type = get_dict_type(member, cctx->ctx_type_list);
1624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 // drop the key and value types
1626 stack->ga_len -= 2 * count;
1627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 // add the dict type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001629 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 return FAIL;
1631 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1632 ++stack->ga_len;
1633
1634 return OK;
1635}
1636
1637/*
1638 * Generate an ISN_FUNCREF instruction.
1639 */
1640 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001641generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642{
1643 isn_T *isn;
1644 garray_T *stack = &cctx->ctx_type_stack;
1645
Bram Moolenaar080457c2020-03-03 21:53:32 +01001646 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1648 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001649 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001650 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001652 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001653 // the nested context, including this one.
1654 if (ufunc->uf_flags & FC_CLOSURE)
1655 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1656
Bram Moolenaar35578162021-08-02 19:10:38 +02001657 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001659 ((type_T **)stack->ga_data)[stack->ga_len] =
1660 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661 ++stack->ga_len;
1662
1663 return OK;
1664}
1665
1666/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001667 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001668 * "lambda_name" and "func_name" must be in allocated memory and will be
1669 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001670 */
1671 static int
1672generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1673{
1674 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001675
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001676 if (cctx->ctx_skip == SKIP_YES)
1677 {
1678 vim_free(lambda_name);
1679 vim_free(func_name);
1680 return OK;
1681 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001682 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001683 {
1684 vim_free(lambda_name);
1685 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001686 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001687 }
1688 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001689 isn->isn_arg.newfunc.nf_global = func_name;
1690
1691 return OK;
1692}
1693
1694/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001695 * Generate an ISN_DEF instruction: list functions
1696 */
1697 static int
1698generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1699{
1700 isn_T *isn;
1701
1702 RETURN_OK_IF_SKIP(cctx);
1703 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1704 return FAIL;
1705 if (len > 0)
1706 {
1707 isn->isn_arg.string = vim_strnsave(name, len);
1708 if (isn->isn_arg.string == NULL)
1709 return FAIL;
1710 }
1711 return OK;
1712}
1713
1714/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715 * Generate an ISN_JUMP instruction.
1716 */
1717 static int
1718generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1719{
1720 isn_T *isn;
1721 garray_T *stack = &cctx->ctx_type_stack;
1722
Bram Moolenaar080457c2020-03-03 21:53:32 +01001723 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1725 return FAIL;
1726 isn->isn_arg.jump.jump_when = when;
1727 isn->isn_arg.jump.jump_where = where;
1728
1729 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1730 --stack->ga_len;
1731
1732 return OK;
1733}
1734
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001735/*
1736 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1737 */
1738 static int
1739generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1740{
1741 isn_T *isn;
1742
1743 RETURN_OK_IF_SKIP(cctx);
1744 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1745 return FAIL;
1746 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1747 // jump_where is set later
1748 return OK;
1749}
1750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 static int
1752generate_FOR(cctx_T *cctx, int loop_idx)
1753{
1754 isn_T *isn;
1755 garray_T *stack = &cctx->ctx_type_stack;
1756
Bram Moolenaar080457c2020-03-03 21:53:32 +01001757 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1759 return FAIL;
1760 isn->isn_arg.forloop.for_idx = loop_idx;
1761
Bram Moolenaar35578162021-08-02 19:10:38 +02001762 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763 return FAIL;
1764 // type doesn't matter, will be stored next
1765 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1766 ++stack->ga_len;
1767
1768 return OK;
1769}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001770/*
1771 * Generate an ISN_TRYCONT instruction.
1772 */
1773 static int
1774generate_TRYCONT(cctx_T *cctx, int levels, int where)
1775{
1776 isn_T *isn;
1777
1778 RETURN_OK_IF_SKIP(cctx);
1779 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1780 return FAIL;
1781 isn->isn_arg.trycont.tct_levels = levels;
1782 isn->isn_arg.trycont.tct_where = where;
1783
1784 return OK;
1785}
1786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787
1788/*
1789 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001790 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 * Return FAIL if the number of arguments is wrong.
1792 */
1793 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001794generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795{
1796 isn_T *isn;
1797 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001798 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001799 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001800 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001801 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802
Bram Moolenaar080457c2020-03-03 21:53:32 +01001803 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001804 argoff = check_internal_func(func_idx, argcount);
1805 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 return FAIL;
1807
Bram Moolenaar389df252020-07-09 21:20:47 +02001808 if (method_call && argoff > 1)
1809 {
1810 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1811 return FAIL;
1812 isn->isn_arg.shuffle.shfl_item = argcount;
1813 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1814 }
1815
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001816 if (argcount > 0)
1817 {
1818 // Check the types of the arguments.
1819 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001820 if (method_call && argoff > 1)
1821 {
1822 int i;
1823
1824 for (i = 0; i < argcount; ++i)
1825 shuffled_argtypes[i] = (i < argoff - 1)
1826 ? argtypes[i + 1]
1827 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1828 argtypes = shuffled_argtypes;
1829 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001830 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1831 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001832 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001833 if (internal_func_is_map(func_idx))
1834 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001835 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1838 return FAIL;
1839 isn->isn_arg.bfunc.cbf_idx = func_idx;
1840 isn->isn_arg.bfunc.cbf_argcount = argcount;
1841
Bram Moolenaar94738d82020-10-21 14:25:07 +02001842 // Drop the argument types and push the return type.
1843 stack->ga_len -= argcount;
Bram Moolenaar35578162021-08-02 19:10:38 +02001844 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 return FAIL;
1846 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001847 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001848 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001850 if (maptype != NULL && maptype->tt_member != NULL
1851 && maptype->tt_member != &t_any)
1852 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001853 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001854
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 return OK;
1856}
1857
1858/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001859 * Generate an ISN_LISTAPPEND instruction. Works like add().
1860 * Argument count is already checked.
1861 */
1862 static int
1863generate_LISTAPPEND(cctx_T *cctx)
1864{
1865 garray_T *stack = &cctx->ctx_type_stack;
1866 type_T *list_type;
1867 type_T *item_type;
1868 type_T *expected;
1869
1870 // Caller already checked that list_type is a list.
1871 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1872 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1873 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001874 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001875 return FAIL;
1876
1877 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1878 return FAIL;
1879
1880 --stack->ga_len; // drop the argument
1881 return OK;
1882}
1883
1884/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001885 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1886 * Argument count is already checked.
1887 */
1888 static int
1889generate_BLOBAPPEND(cctx_T *cctx)
1890{
1891 garray_T *stack = &cctx->ctx_type_stack;
1892 type_T *item_type;
1893
1894 // Caller already checked that blob_type is a blob.
1895 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001896 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001897 return FAIL;
1898
1899 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1900 return FAIL;
1901
1902 --stack->ga_len; // drop the argument
1903 return OK;
1904}
1905
1906/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001907 * Return TRUE if "ufunc" should be compiled, taking into account whether
1908 * "profile" indicates profiling is to be done.
1909 */
1910 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001911func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001912{
1913 switch (ufunc->uf_def_status)
1914 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001915 case UF_TO_BE_COMPILED:
1916 return TRUE;
1917
Bram Moolenaarb2049902021-01-24 12:53:53 +01001918 case UF_COMPILED:
1919 {
1920 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1921 + ufunc->uf_dfunc_idx;
1922
Bram Moolenaare99d4222021-06-13 14:01:26 +02001923 switch (compile_type)
1924 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001925 case CT_PROFILE:
1926#ifdef FEAT_PROFILE
1927 return dfunc->df_instr_prof == NULL;
1928#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001929 case CT_NONE:
1930 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001931 case CT_DEBUG:
1932 return dfunc->df_instr_debug == NULL;
1933 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001934 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001935
1936 case UF_NOT_COMPILED:
1937 case UF_COMPILE_ERROR:
1938 case UF_COMPILING:
1939 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001940 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001941 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001942}
1943
1944/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 * Generate an ISN_DCALL or ISN_UCALL instruction.
1946 * Return FAIL if the number of arguments is wrong.
1947 */
1948 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001949generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950{
1951 isn_T *isn;
1952 garray_T *stack = &cctx->ctx_type_stack;
1953 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001954 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955
Bram Moolenaar080457c2020-03-03 21:53:32 +01001956 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 if (argcount > regular_args && !has_varargs(ufunc))
1958 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001959 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 return FAIL;
1961 }
1962 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1963 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001964 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 return FAIL;
1966 }
1967
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001968 if (ufunc->uf_def_status != UF_NOT_COMPILED
1969 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001970 {
1971 int i;
1972
1973 for (i = 0; i < argcount; ++i)
1974 {
1975 type_T *expected;
1976 type_T *actual;
1977
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001978 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1979 if (actual == &t_special
1980 && i >= regular_args - ufunc->uf_def_args.ga_len)
1981 {
1982 // assume v:none used for default argument value
1983 continue;
1984 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001985 if (i < regular_args)
1986 {
1987 if (ufunc->uf_arg_types == NULL)
1988 continue;
1989 expected = ufunc->uf_arg_types[i];
1990 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001991 else if (ufunc->uf_va_type == NULL
1992 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001993 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001994 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001995 else
1996 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001997 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001998 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001999 {
2000 arg_type_mismatch(expected, actual, i + 1);
2001 return FAIL;
2002 }
2003 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002004 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002005 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002006 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002007 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002008 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002009 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2010 {
2011 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2012 ufunc->uf_name);
2013 return FAIL;
2014 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002017 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002018 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002020 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 {
2022 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2023 isn->isn_arg.dfunc.cdf_argcount = argcount;
2024 }
2025 else
2026 {
2027 // A user function may be deleted and redefined later, can't use the
2028 // ufunc pointer, need to look it up again at runtime.
2029 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2030 isn->isn_arg.ufunc.cuf_argcount = argcount;
2031 }
2032
2033 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002034 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 return FAIL;
2036 // add return value
2037 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2038 ++stack->ga_len;
2039
2040 return OK;
2041}
2042
2043/*
2044 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2045 */
2046 static int
2047generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2048{
2049 isn_T *isn;
2050 garray_T *stack = &cctx->ctx_type_stack;
2051
Bram Moolenaar080457c2020-03-03 21:53:32 +01002052 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2054 return FAIL;
2055 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2056 isn->isn_arg.ufunc.cuf_argcount = argcount;
2057
2058 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002059 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002060 return FAIL;
2061 // add return value
2062 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2063 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064
2065 return OK;
2066}
2067
2068/*
2069 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002070 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 */
2072 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002073generate_PCALL(
2074 cctx_T *cctx,
2075 int argcount,
2076 char_u *name,
2077 type_T *type,
2078 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079{
2080 isn_T *isn;
2081 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002082 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083
Bram Moolenaar080457c2020-03-03 21:53:32 +01002084 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002085
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002086 if (type->tt_type == VAR_ANY)
2087 ret_type = &t_any;
2088 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002089 {
2090 if (type->tt_argcount != -1)
2091 {
2092 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2093
2094 if (argcount < type->tt_min_argcount - varargs)
2095 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002096 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002097 return FAIL;
2098 }
2099 if (!varargs && argcount > type->tt_argcount)
2100 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002101 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002102 return FAIL;
2103 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002104 if (type->tt_args != NULL)
2105 {
2106 int i;
2107
2108 for (i = 0; i < argcount; ++i)
2109 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002110 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002111 type_T *actual = ((type_T **)stack->ga_data)[
2112 stack->ga_len + offset];
2113 type_T *expected;
2114
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002115 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002116 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002117 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002118 else if (i >= type->tt_min_argcount
2119 && actual == &t_special)
2120 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002121 else
2122 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002123 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002124 cctx, TRUE, FALSE) == FAIL)
2125 {
2126 arg_type_mismatch(expected, actual, i + 1);
2127 return FAIL;
2128 }
2129 }
2130 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002131 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002132 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002133 if (ret_type == &t_unknown)
2134 // return type not known yet, use a runtime check
2135 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002136 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002137 else
2138 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002139 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002140 return FAIL;
2141 }
2142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2144 return FAIL;
2145 isn->isn_arg.pfunc.cpf_top = at_top;
2146 isn->isn_arg.pfunc.cpf_argcount = argcount;
2147
2148 stack->ga_len -= argcount; // drop the arguments
2149
2150 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002151 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002153 // If partial is above the arguments it must be cleared and replaced with
2154 // the return value.
2155 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2156 return FAIL;
2157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 return OK;
2159}
2160
2161/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002162 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 */
2164 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002165generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166{
2167 isn_T *isn;
2168 garray_T *stack = &cctx->ctx_type_stack;
2169 type_T *type;
2170
Bram Moolenaar080457c2020-03-03 21:53:32 +01002171 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002172 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002174 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002176 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002178 if (type->tt_type != VAR_DICT && type != &t_any)
2179 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002180 char *tofree;
2181
2182 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2183 name, type_name(type, &tofree));
2184 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002185 return FAIL;
2186 }
2187 // change dict type to dict member type
2188 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002189 {
2190 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2191 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193
2194 return OK;
2195}
2196
2197/*
2198 * Generate an ISN_ECHO instruction.
2199 */
2200 static int
2201generate_ECHO(cctx_T *cctx, int with_white, int count)
2202{
2203 isn_T *isn;
2204
Bram Moolenaar080457c2020-03-03 21:53:32 +01002205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2207 return FAIL;
2208 isn->isn_arg.echo.echo_with_white = with_white;
2209 isn->isn_arg.echo.echo_count = count;
2210
2211 return OK;
2212}
2213
Bram Moolenaarad39c092020-02-26 18:23:43 +01002214/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002215 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002216 */
2217 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002218generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002219{
2220 isn_T *isn;
2221
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002222 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002223 return FAIL;
2224 isn->isn_arg.number = count;
2225
2226 return OK;
2227}
2228
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002229/*
2230 * Generate an ISN_PUT instruction.
2231 */
2232 static int
2233generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2234{
2235 isn_T *isn;
2236
2237 RETURN_OK_IF_SKIP(cctx);
2238 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2239 return FAIL;
2240 isn->isn_arg.put.put_regname = regname;
2241 isn->isn_arg.put.put_lnum = lnum;
2242 return OK;
2243}
2244
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 static int
2246generate_EXEC(cctx_T *cctx, char_u *line)
2247{
2248 isn_T *isn;
2249
Bram Moolenaar080457c2020-03-03 21:53:32 +01002250 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2252 return FAIL;
2253 isn->isn_arg.string = vim_strsave(line);
2254 return OK;
2255}
2256
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002257 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002258generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2259{
2260 isn_T *isn;
2261 garray_T *stack = &cctx->ctx_type_stack;
2262
2263 RETURN_OK_IF_SKIP(cctx);
2264 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2265 return FAIL;
2266 isn->isn_arg.string = vim_strsave(line);
2267
Bram Moolenaar35578162021-08-02 19:10:38 +02002268 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002269 return FAIL;
2270 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2271 ++stack->ga_len;
2272
2273 return OK;
2274}
2275
2276 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002277generate_EXECCONCAT(cctx_T *cctx, int count)
2278{
2279 isn_T *isn;
2280
2281 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2282 return FAIL;
2283 isn->isn_arg.number = count;
2284 return OK;
2285}
2286
Bram Moolenaar08597872020-12-10 19:43:40 +01002287/*
2288 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2289 */
2290 static int
2291generate_RANGE(cctx_T *cctx, char_u *range)
2292{
2293 isn_T *isn;
2294 garray_T *stack = &cctx->ctx_type_stack;
2295
2296 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2297 return FAIL;
2298 isn->isn_arg.string = range;
2299
Bram Moolenaar35578162021-08-02 19:10:38 +02002300 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002301 return FAIL;
2302 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2303 ++stack->ga_len;
2304 return OK;
2305}
2306
Bram Moolenaar792f7862020-11-23 08:31:18 +01002307 static int
2308generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2309{
2310 isn_T *isn;
2311
2312 RETURN_OK_IF_SKIP(cctx);
2313 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2314 return FAIL;
2315 isn->isn_arg.unpack.unp_count = var_count;
2316 isn->isn_arg.unpack.unp_semicolon = semicolon;
2317 return OK;
2318}
2319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002321 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002322 */
2323 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002324generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002325{
2326 isn_T *isn;
2327
Bram Moolenaarfa984412021-03-25 22:15:28 +01002328 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002329 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002330 cctx->ctx_has_cmdmod = TRUE;
2331
2332 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002333 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002334 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2335 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2336 return FAIL;
2337 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002338 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002339 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002340 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002341
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002342 return OK;
2343}
2344
2345 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002346generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002347{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002348 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2349 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002350 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002351 return OK;
2352}
2353
Bram Moolenaarfa984412021-03-25 22:15:28 +01002354 static int
2355misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002356{
2357 garray_T *instr = &cctx->ctx_instr;
2358
Bram Moolenaara91a7132021-03-25 21:12:15 +01002359 if (cctx->ctx_has_cmdmod
2360 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2361 == ISN_CMDMOD)
2362 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002363 emsg(_(e_misplaced_command_modifier));
2364 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002365 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002366 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002367}
2368
2369/*
2370 * Get the index of the current instruction.
2371 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2372 */
2373 static int
2374current_instr_idx(cctx_T *cctx)
2375{
2376 garray_T *instr = &cctx->ctx_instr;
2377 int idx = instr->ga_len;
2378
Bram Moolenaare99d4222021-06-13 14:01:26 +02002379 while (idx > 0)
2380 {
2381 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002382 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002383 {
2384 --idx;
2385 continue;
2386 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002387#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002388 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2389 {
2390 --idx;
2391 continue;
2392 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002393#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002394 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2395 {
2396 --idx;
2397 continue;
2398 }
2399 break;
2400 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002401 return idx;
2402}
2403
Bram Moolenaarf002a412021-01-24 13:34:18 +01002404#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002405 static void
2406may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2407{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002408 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002409 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002410}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002411#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002412
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002415 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002417 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002418reserve_local(
2419 cctx_T *cctx,
2420 char_u *name,
2421 size_t len,
2422 int isConst,
2423 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002426 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002428 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002430 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002431 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 }
2433
Bram Moolenaar35578162021-08-02 19:10:38 +02002434 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002435 return NULL;
2436 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002437 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002439 // Every local variable uses the next entry on the stack. We could re-use
2440 // the last ones when leaving a scope, but then variables used in a closure
2441 // might get overwritten. To keep things simple do not re-use stack
2442 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002443 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2444 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002445
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002446 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 lvar->lv_const = isConst;
2448 lvar->lv_type = type;
2449
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002450 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002451 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002452 return NULL;
2453 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2454 vim_strsave(lvar->lv_name);
2455 ++dfunc->df_var_names.ga_len;
2456
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002457 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458}
2459
2460/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002461 * Remove local variables above "new_top".
2462 */
2463 static void
2464unwind_locals(cctx_T *cctx, int new_top)
2465{
2466 if (cctx->ctx_locals.ga_len > new_top)
2467 {
2468 int idx;
2469 lvar_T *lvar;
2470
2471 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2472 {
2473 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2474 vim_free(lvar->lv_name);
2475 }
2476 }
2477 cctx->ctx_locals.ga_len = new_top;
2478}
2479
2480/*
2481 * Free all local variables.
2482 */
2483 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002484free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002485{
2486 unwind_locals(cctx, 0);
2487 ga_clear(&cctx->ctx_locals);
2488}
2489
2490/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002491 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2492 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2493 * error if the variable was defined with :const.
2494 */
2495 static int
2496check_item_writable(svar_T *sv, int check_writable, char_u *name)
2497{
2498 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2499 || (check_writable == ASSIGN_FINAL
2500 && sv->sv_const == ASSIGN_CONST))
2501 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002502 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002503 return FAIL;
2504 }
2505 return OK;
2506}
2507
2508/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002510 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 * Returns the index in "sn_var_vals" if found.
2512 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002513 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 */
2515 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002516get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517{
2518 hashtab_T *ht;
2519 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002520 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002521 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 int idx;
2523
Bram Moolenaare3d46852020-08-29 13:39:17 +02002524 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002526 if (sid == current_sctx.sc_sid)
2527 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002528 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002529
2530 if (sav == NULL)
2531 return -2;
2532 idx = sav->sav_var_vals_idx;
2533 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002534 if (check_item_writable(sv, check_writable, name) == FAIL)
2535 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002536 return idx;
2537 }
2538
2539 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 ht = &SCRIPT_VARS(sid);
2541 di = find_var_in_ht(ht, 0, name, TRUE);
2542 if (di == NULL)
2543 return -2;
2544
2545 // Now find the svar_T index in sn_var_vals.
2546 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2547 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002548 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 if (sv->sv_tv == &di->di_tv)
2550 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002551 if (check_item_writable(sv, check_writable, name) == FAIL)
2552 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 return idx;
2554 }
2555 }
2556 return -1;
2557}
2558
2559/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002560 * Find "name" in imported items of the current script or in "cctx" if not
2561 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 */
2563 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002564find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 int idx;
2567
Bram Moolenaare3d46852020-08-29 13:39:17 +02002568 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002569 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 if (cctx != NULL)
2571 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2572 {
2573 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2574 + idx;
2575
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002576 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2577 : STRLEN(import->imp_name) == len
2578 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 return import;
2580 }
2581
Bram Moolenaarefa94442020-08-08 22:16:00 +02002582 return find_imported_in_script(name, len, current_sctx.sc_sid);
2583}
2584
2585 imported_T *
2586find_imported_in_script(char_u *name, size_t len, int sid)
2587{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002588 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002589 int idx;
2590
Bram Moolenaare3d46852020-08-29 13:39:17 +02002591 if (!SCRIPT_ID_VALID(sid))
2592 return NULL;
2593 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2595 {
2596 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2597
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002598 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2599 : STRLEN(import->imp_name) == len
2600 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 return import;
2602 }
2603 return NULL;
2604}
2605
2606/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002607 * Free all imported variables.
2608 */
2609 static void
2610free_imported(cctx_T *cctx)
2611{
2612 int idx;
2613
2614 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2615 {
2616 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2617
2618 vim_free(import->imp_name);
2619 }
2620 ga_clear(&cctx->ctx_imports);
2621}
2622
2623/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002624 * Return a pointer to the next line that isn't empty or only contains a
2625 * comment. Skips over white space.
2626 * Returns NULL if there is none.
2627 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002628 char_u *
2629peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002630{
2631 int lnum = cctx->ctx_lnum;
2632
2633 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2634 {
2635 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002636 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002637
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002638 // ignore NULLs inserted for continuation lines
2639 if (line != NULL)
2640 {
2641 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002642 if (vim9_bad_comment(p))
2643 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002644 if (*p != NUL && !vim9_comment_start(p))
2645 return p;
2646 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002647 }
2648 return NULL;
2649}
2650
2651/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002652 * Called when checking for a following operator at "arg". When the rest of
2653 * the line is empty or only a comment, peek the next line. If there is a next
2654 * line return a pointer to it and set "nextp".
2655 * Otherwise skip over white space.
2656 */
2657 static char_u *
2658may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2659{
2660 char_u *p = skipwhite(arg);
2661
2662 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002663 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002664 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002665 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002666 if (*nextp != NULL)
2667 return *nextp;
2668 }
2669 return p;
2670}
2671
2672/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002673 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002674 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002675 * Returns NULL when at the end.
2676 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002677 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002678next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002679{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002680 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002681
2682 do
2683 {
2684 ++cctx->ctx_lnum;
2685 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002686 {
2687 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002688 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002689 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002690 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002691 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002692 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002693 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002694 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002695 return line;
2696}
2697
2698/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002699 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002700 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002701 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002702 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2703 */
2704 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002705may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002706{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002707 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002708 if (vim9_bad_comment(*arg))
2709 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002710 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002711 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002712 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002713
2714 if (next == NULL)
2715 return FAIL;
2716 *arg = skipwhite(next);
2717 }
2718 return OK;
2719}
2720
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002721/*
2722 * Idem, and give an error when failed.
2723 */
2724 static int
2725may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2726{
2727 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2728 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002729 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002730 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002731 return FAIL;
2732 }
2733 return OK;
2734}
2735
2736
Bram Moolenaara5565e42020-05-09 15:44:01 +02002737// Structure passed between the compile_expr* functions to keep track of
2738// constants that have been parsed but for which no code was produced yet. If
2739// possible expressions on these constants are applied at compile time. If
2740// that is not possible, the code to push the constants needs to be generated
2741// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002742// Using 50 should be more than enough of 5 levels of ().
2743#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002744typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002745 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002746 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002747 int pp_is_const; // all generated code was constants, used for a
2748 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002749} ppconst_T;
2750
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002751static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002752static int compile_expr0(char_u **arg, cctx_T *cctx);
2753static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2754
Bram Moolenaara5565e42020-05-09 15:44:01 +02002755/*
2756 * Generate a PUSH instruction for "tv".
2757 * "tv" will be consumed or cleared.
2758 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2759 */
2760 static int
2761generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2762{
2763 if (tv != NULL)
2764 {
2765 switch (tv->v_type)
2766 {
2767 case VAR_UNKNOWN:
2768 break;
2769 case VAR_BOOL:
2770 generate_PUSHBOOL(cctx, tv->vval.v_number);
2771 break;
2772 case VAR_SPECIAL:
2773 generate_PUSHSPEC(cctx, tv->vval.v_number);
2774 break;
2775 case VAR_NUMBER:
2776 generate_PUSHNR(cctx, tv->vval.v_number);
2777 break;
2778#ifdef FEAT_FLOAT
2779 case VAR_FLOAT:
2780 generate_PUSHF(cctx, tv->vval.v_float);
2781 break;
2782#endif
2783 case VAR_BLOB:
2784 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2785 tv->vval.v_blob = NULL;
2786 break;
2787 case VAR_STRING:
2788 generate_PUSHS(cctx, tv->vval.v_string);
2789 tv->vval.v_string = NULL;
2790 break;
2791 default:
2792 iemsg("constant type not supported");
2793 clear_tv(tv);
2794 return FAIL;
2795 }
2796 tv->v_type = VAR_UNKNOWN;
2797 }
2798 return OK;
2799}
2800
2801/*
2802 * Generate code for any ppconst entries.
2803 */
2804 static int
2805generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2806{
2807 int i;
2808 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002809 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002810
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002811 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002812 for (i = 0; i < ppconst->pp_used; ++i)
2813 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2814 ret = FAIL;
2815 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002816 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002817 return ret;
2818}
2819
2820/*
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002821 * Check that the last item of "ppconst" is a bool.
2822 */
2823 static int
2824check_ppconst_bool(ppconst_T *ppconst)
2825{
2826 if (ppconst->pp_used > 0)
2827 {
2828 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002829 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002830
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002831 return check_typval_type(&t_bool, tv, where);
2832 }
2833 return OK;
2834}
2835
2836/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002837 * Clear ppconst constants. Used when failing.
2838 */
2839 static void
2840clear_ppconst(ppconst_T *ppconst)
2841{
2842 int i;
2843
2844 for (i = 0; i < ppconst->pp_used; ++i)
2845 clear_tv(&ppconst->pp_tv[i]);
2846 ppconst->pp_used = 0;
2847}
2848
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002849/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002850 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002851 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002852 */
2853 static int
2854compile_member(int is_slice, cctx_T *cctx)
2855{
2856 type_T **typep;
2857 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002858 vartype_T vartype;
2859 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002860
Bram Moolenaar261417b2021-05-06 21:04:55 +02002861 // We can index a list, dict and blob. If we don't know the type
2862 // we can use the index value type. If we still don't know use an "ANY"
2863 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002864 typep = ((type_T **)stack->ga_data) + stack->ga_len
2865 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002866 vartype = (*typep)->tt_type;
2867 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002868 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002869 if (*typep == &t_any && idxtype == &t_string)
2870 vartype = VAR_DICT;
2871 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002872 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002873 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002874 return FAIL;
2875 if (is_slice)
2876 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002877 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2878 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002879 FALSE, FALSE) == FAIL)
2880 return FAIL;
2881 }
2882 }
2883
Bram Moolenaar261417b2021-05-06 21:04:55 +02002884 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002885 {
2886 if (is_slice)
2887 {
2888 emsg(_(e_cannot_slice_dictionary));
2889 return FAIL;
2890 }
2891 if ((*typep)->tt_type == VAR_DICT)
2892 {
2893 *typep = (*typep)->tt_member;
2894 if (*typep == &t_unknown)
2895 // empty dict was used
2896 *typep = &t_any;
2897 }
2898 else
2899 {
2900 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2901 FALSE, FALSE) == FAIL)
2902 return FAIL;
2903 *typep = &t_any;
2904 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002905 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002906 return FAIL;
2907 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2908 return FAIL;
2909 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002910 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002911 {
2912 *typep = &t_string;
2913 if ((is_slice
2914 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2915 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2916 return FAIL;
2917 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002918 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002919 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002920 if (is_slice)
2921 {
2922 *typep = &t_blob;
2923 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2924 return FAIL;
2925 }
2926 else
2927 {
2928 *typep = &t_number;
2929 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2930 return FAIL;
2931 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002932 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002933 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002934 {
2935 if (is_slice)
2936 {
2937 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002938 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002939 2) == FAIL)
2940 return FAIL;
2941 }
2942 else
2943 {
2944 if ((*typep)->tt_type == VAR_LIST)
2945 {
2946 *typep = (*typep)->tt_member;
2947 if (*typep == &t_unknown)
2948 // empty list was used
2949 *typep = &t_any;
2950 }
2951 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002952 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2953 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002954 return FAIL;
2955 }
2956 }
2957 else
2958 {
2959 emsg(_(e_string_list_dict_or_blob_required));
2960 return FAIL;
2961 }
2962 return OK;
2963}
2964
2965/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002966 * Generate an instruction to load script-local variable "name", without the
2967 * leading "s:".
2968 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 */
2970 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002971compile_load_scriptvar(
2972 cctx_T *cctx,
2973 char_u *name, // variable NUL terminated
2974 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002975 char_u **end, // end of variable
2976 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002978 scriptitem_T *si;
2979 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 imported_T *import;
2981
Bram Moolenaare3d46852020-08-29 13:39:17 +02002982 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2983 return FAIL;
2984 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002985 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002986 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002988 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002989 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2990 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 }
2992 if (idx >= 0)
2993 {
2994 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2995
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002996 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 current_sctx.sc_sid, idx, sv->sv_type);
2998 return OK;
2999 }
3000
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003001 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 if (import != NULL)
3003 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003004 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003005 {
3006 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003007 char_u *exp_name;
3008 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003009 ufunc_T *ufunc;
3010 type_T *type;
3011
3012 // Used "import * as Name", need to lookup the member.
3013 if (*p != '.')
3014 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003015 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003016 return FAIL;
3017 }
3018 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003019 if (VIM_ISWHITE(*p))
3020 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003021 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003022 return FAIL;
3023 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003024
Bram Moolenaar1c991142020-07-04 13:15:31 +02003025 // isolate one name
3026 exp_name = p;
3027 while (eval_isnamec(*p))
3028 ++p;
3029 cc = *p;
3030 *p = NUL;
3031
Bram Moolenaaredba7072021-03-13 21:14:18 +01003032 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3033 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003034 *p = cc;
3035 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003036 *end = p;
3037
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003038 if (idx < 0)
3039 {
3040 if (*p == '(' && ufunc != NULL)
3041 {
3042 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3043 return OK;
3044 }
3045 return FAIL;
3046 }
3047
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003048 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3049 import->imp_sid,
3050 idx,
3051 type);
3052 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003053 else if (import->imp_funcname != NULL)
3054 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003055 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003056 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3057 import->imp_sid,
3058 import->imp_var_vals_idx,
3059 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 return OK;
3061 }
3062
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003063 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003064 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 return FAIL;
3066}
3067
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003068 static int
3069generate_funcref(cctx_T *cctx, char_u *name)
3070{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003071 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003072
3073 if (ufunc == NULL)
3074 return FAIL;
3075
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003076 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003077 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3078 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003079 == FAIL)
3080 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003081 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003082}
3083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084/*
3085 * Compile a variable name into a load instruction.
3086 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003087 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 * When "error" is FALSE do not give an error when not found.
3089 */
3090 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003091compile_load(
3092 char_u **arg,
3093 char_u *end_arg,
3094 cctx_T *cctx,
3095 int is_expr,
3096 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097{
3098 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003099 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003100 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003102 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103
3104 if (*(*arg + 1) == ':')
3105 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003106 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003107 {
3108 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109
Bram Moolenaarfa596382021-04-07 21:58:16 +02003110 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003111 switch (**arg)
3112 {
3113 case 'g': isn_type = ISN_LOADGDICT; break;
3114 case 'w': isn_type = ISN_LOADWDICT; break;
3115 case 't': isn_type = ISN_LOADTDICT; break;
3116 case 'b': isn_type = ISN_LOADBDICT; break;
3117 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003118 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003119 goto theend;
3120 }
3121 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3122 goto theend;
3123 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 else
3126 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003127 isntype_T isn_type = ISN_DROP;
3128
Bram Moolenaarfa596382021-04-07 21:58:16 +02003129 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003130 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3131 if (name == NULL)
3132 return FAIL;
3133
3134 switch (**arg)
3135 {
3136 case 'v': res = generate_LOADV(cctx, name, error);
3137 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003138 case 's': if (is_expr && ASCII_ISUPPER(*name)
3139 && find_func(name, FALSE, cctx) != NULL)
3140 res = generate_funcref(cctx, name);
3141 else
3142 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003143 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003144 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003145 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003146 {
3147 if (is_expr && ASCII_ISUPPER(*name)
3148 && find_func(name, FALSE, cctx) != NULL)
3149 res = generate_funcref(cctx, name);
3150 else
3151 isn_type = ISN_LOADG;
3152 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003153 else
3154 {
3155 isn_type = ISN_LOADAUTO;
3156 vim_free(name);
3157 name = vim_strnsave(*arg, end - *arg);
3158 if (name == NULL)
3159 return FAIL;
3160 }
3161 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003162 case 'w': isn_type = ISN_LOADW; break;
3163 case 't': isn_type = ISN_LOADT; break;
3164 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003165 default: // cannot happen, just in case
3166 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003167 goto theend;
3168 }
3169 if (isn_type != ISN_DROP)
3170 {
3171 // Global, Buffer-local, Window-local and Tabpage-local
3172 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003173 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003174 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 }
3177 }
3178 else
3179 {
3180 size_t len = end - *arg;
3181 int idx;
3182 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003183 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184
3185 name = vim_strnsave(*arg, end - *arg);
3186 if (name == NULL)
3187 return FAIL;
3188
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003189 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3190 {
3191 script_autoload(name, FALSE);
3192 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3193 }
3194 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3195 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003197 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003198 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 }
3200 else
3201 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003202 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003203
Bram Moolenaar709664c2020-12-12 14:33:41 +01003204 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003206 type = lvar.lv_type;
3207 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003208 if (lvar.lv_from_outer != 0)
3209 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003210 else
3211 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 }
3213 else
3214 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003215 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003216 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003217 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003218 || find_imported(name, 0, cctx) != NULL)
3219 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003220
Bram Moolenaar0f769812020-09-12 18:32:34 +02003221 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003222 // uppercase letter it can be a user defined function.
3223 // generate_funcref() will fail if the function can't be found.
3224 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003225 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 }
3227 }
3228 if (gen_load)
3229 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003230 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003231 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003232 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003233 cctx->ctx_outer_used = TRUE;
3234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 }
3236
3237 *arg = end;
3238
3239theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003240 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003241 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 vim_free(name);
3243 return res;
3244}
3245
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003246 static void
3247clear_instr_ga(garray_T *gap)
3248{
3249 int idx;
3250
3251 for (idx = 0; idx < gap->ga_len; ++idx)
3252 delete_instr(((isn_T *)gap->ga_data) + idx);
3253 ga_clear(gap);
3254}
3255
3256/*
3257 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3258 * Returns FAIL if compilation fails.
3259 */
3260 static int
3261compile_string(isn_T *isn, cctx_T *cctx)
3262{
3263 char_u *s = isn->isn_arg.string;
3264 garray_T save_ga = cctx->ctx_instr;
3265 int expr_res;
3266 int trailing_error;
3267 int instr_count;
3268 isn_T *instr = NULL;
3269
Bram Moolenaarcd268012021-07-22 19:11:08 +02003270 // Remove the string type from the stack.
3271 --cctx->ctx_type_stack.ga_len;
3272
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003273 // Temporarily reset the list of instructions so that the jump labels are
3274 // correct.
3275 cctx->ctx_instr.ga_len = 0;
3276 cctx->ctx_instr.ga_maxlen = 0;
3277 cctx->ctx_instr.ga_data = NULL;
3278 expr_res = compile_expr0(&s, cctx);
3279 s = skipwhite(s);
3280 trailing_error = *s != NUL;
3281
Bram Moolenaarff652882021-05-16 15:24:49 +02003282 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003283 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003284 {
3285 if (trailing_error)
3286 semsg(_(e_trailing_arg), s);
3287 clear_instr_ga(&cctx->ctx_instr);
3288 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003289 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003290 return FAIL;
3291 }
3292
3293 // Move the generated instructions into the ISN_INSTR instruction, then
3294 // restore the list of instructions.
3295 instr_count = cctx->ctx_instr.ga_len;
3296 instr = cctx->ctx_instr.ga_data;
3297 instr[instr_count].isn_type = ISN_FINISH;
3298
3299 cctx->ctx_instr = save_ga;
3300 vim_free(isn->isn_arg.string);
3301 isn->isn_type = ISN_INSTR;
3302 isn->isn_arg.instr = instr;
3303 return OK;
3304}
3305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306/*
3307 * Compile the argument expressions.
3308 * "arg" points to just after the "(" and is advanced to after the ")"
3309 */
3310 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003311compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003313 char_u *p = *arg;
3314 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003315 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003316 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317
Bram Moolenaare6085c52020-04-12 20:19:16 +02003318 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003320 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3321 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003322 if (*p == ')')
3323 {
3324 *arg = p + 1;
3325 return OK;
3326 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003327 if (must_end)
3328 {
3329 semsg(_(e_missing_comma_before_argument_str), p);
3330 return FAIL;
3331 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003332
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003333 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003334 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 return FAIL;
3336 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003337
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003338 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003339 && cctx->ctx_instr.ga_len == instr_count + 1)
3340 {
3341 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3342
3343 // {skip} argument of searchpair() can be compiled if not empty
3344 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3345 compile_string(isn, cctx);
3346 }
3347
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003348 if (*p != ',' && *skipwhite(p) == ',')
3349 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003350 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003351 p = skipwhite(p);
3352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003354 {
3355 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003356 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003357 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003358 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003359 else
3360 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003361 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003362 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003364failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003365 emsg(_(e_missing_close));
3366 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367}
3368
3369/*
3370 * Compile a function call: name(arg1, arg2)
3371 * "arg" points to "name", "arg + varlen" to the "(".
3372 * "argcount_init" is 1 for "value->method()"
3373 * Instructions:
3374 * EVAL arg1
3375 * EVAL arg2
3376 * BCALL / DCALL / UCALL
3377 */
3378 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003379compile_call(
3380 char_u **arg,
3381 size_t varlen,
3382 cctx_T *cctx,
3383 ppconst_T *ppconst,
3384 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385{
3386 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003387 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 int argcount = argcount_init;
3389 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003390 char_u fname_buf[FLEN_FIXED + 1];
3391 char_u *tofree = NULL;
3392 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003393 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003394 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003395 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003396 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003398 // We can evaluate "has('name')" at compile time.
3399 // We can evaluate some "exists()" values at compile time.
3400 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3401 || (varlen == 6 && STRNCMP(*arg, "exists", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003402 {
3403 char_u *s = skipwhite(*arg + varlen + 1);
3404 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003405 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003406
3407 argvars[0].v_type = VAR_UNKNOWN;
3408 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003409 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003410 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003411 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003412 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003413 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003414 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
3415 || (!is_has && (*argvars[0].vval.v_string == '+'
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003416 || *argvars[0].vval.v_string == '&'))))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003417 {
3418 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3419
3420 *arg = s + 1;
3421 argvars[1].v_type = VAR_UNKNOWN;
3422 tv->v_type = VAR_NUMBER;
3423 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003424 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003425 f_has(argvars, tv);
3426 else
3427 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003428 clear_tv(&argvars[0]);
3429 ++ppconst->pp_used;
3430 return OK;
3431 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003432 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003433 }
3434
3435 if (generate_ppconst(cctx, ppconst) == FAIL)
3436 return FAIL;
3437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 if (varlen >= sizeof(namebuf))
3439 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003440 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 return FAIL;
3442 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003443 vim_strncpy(namebuf, *arg, varlen);
3444 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003446 // We handle the "skip" argument of searchpair() and searchpairpos()
3447 // differently.
3448 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3449 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3450 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3451 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003454 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003455 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456
Bram Moolenaar03290b82020-12-19 16:30:44 +01003457 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003458 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 {
3460 int idx;
3461
3462 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003463 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003465 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003466 if (STRCMP(name, "flatten") == 0)
3467 {
3468 emsg(_(e_cannot_use_flatten_in_vim9_script));
3469 goto theend;
3470 }
3471
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003472 if (STRCMP(name, "add") == 0 && argcount == 2)
3473 {
3474 garray_T *stack = &cctx->ctx_type_stack;
3475 type_T *type = ((type_T **)stack->ga_data)[
3476 stack->ga_len - 2];
3477
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003478 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003479 if (type->tt_type == VAR_LIST)
3480 {
3481 // inline "add(list, item)" so that the type can be checked
3482 res = generate_LISTAPPEND(cctx);
3483 idx = -1;
3484 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003485 else if (type->tt_type == VAR_BLOB)
3486 {
3487 // inline "add(blob, nr)" so that the type can be checked
3488 res = generate_BLOBAPPEND(cctx);
3489 idx = -1;
3490 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003491 }
3492
3493 if (idx >= 0)
3494 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3495 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003496 else
3497 semsg(_(e_unknownfunc), namebuf);
3498 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 }
3500
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003501 // An argument or local variable can be a function reference, this
3502 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003503 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003504 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003505 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003506 // If we can find the function by name generate the right call.
3507 // Skip global functions here, a local funcref takes precedence.
3508 ufunc = find_func(name, FALSE, cctx);
3509 if (ufunc != NULL && !func_is_global(ufunc))
3510 {
3511 res = generate_CALL(cctx, ufunc, argcount);
3512 goto theend;
3513 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515
3516 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003517 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003518 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003520 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003521 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003522 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003523 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003524 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003525
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003526 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003527 goto theend;
3528 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529
Bram Moolenaar0f769812020-09-12 18:32:34 +02003530 // If we can find a global function by name generate the right call.
3531 if (ufunc != NULL)
3532 {
3533 res = generate_CALL(cctx, ufunc, argcount);
3534 goto theend;
3535 }
3536
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003537 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003538 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003539 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003540 res = generate_UCALL(cctx, name, argcount);
3541 else
3542 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003543
3544theend:
3545 vim_free(tofree);
3546 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547}
3548
3549// like NAMESPACE_CHAR but with 'a' and 'l'.
3550#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3551
3552/*
3553 * Find the end of a variable or function name. Unlike find_name_end() this
3554 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003555 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 * Return a pointer to just after the name. Equal to "arg" if there is no
3557 * valid name.
3558 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003559 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003560to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561{
3562 char_u *p;
3563
3564 // Quick check for valid starting character.
3565 if (!eval_isnamec1(*arg))
3566 return arg;
3567
3568 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3569 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3570 // and can be used in slice "[n:]".
3571 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003572 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3574 break;
3575 return p;
3576}
3577
3578/*
3579 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003580 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003581 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 */
3583 char_u *
3584to_name_const_end(char_u *arg)
3585{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003586 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 typval_T rettv;
3588
Bram Moolenaar678b2072021-07-26 21:10:11 +02003589 if (STRNCMP(p, "<SNR>", 5) == 0)
3590 p = skipdigits(p + 5);
3591 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 if (p == arg && *arg == '[')
3593 {
3594
3595 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003596 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 p = arg;
3598 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 return p;
3600}
3601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602/*
3603 * parse a list: [expr, expr]
3604 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003605 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 */
3607 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003608compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609{
3610 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003611 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003613 int is_const;
3614 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003616 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003618 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003619 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003620 semsg(_(e_list_end), *arg);
3621 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003622 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003623 if (*p == ',')
3624 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003625 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003626 return FAIL;
3627 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003628 if (*p == ']')
3629 {
3630 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003631 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003632 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003633 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003634 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003635 if (!is_const)
3636 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 ++count;
3638 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003639 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003641 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3642 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003643 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003644 return FAIL;
3645 }
3646 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003647 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 p = skipwhite(p);
3649 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003650 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003652 ppconst->pp_is_const = is_all_const;
3653 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654}
3655
3656/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003657 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003658 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003659 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 */
3661 static int
3662compile_lambda(char_u **arg, cctx_T *cctx)
3663{
Bram Moolenaare462f522020-12-27 14:43:30 +01003664 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 typval_T rettv;
3666 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003667 evalarg_T evalarg;
3668
3669 CLEAR_FIELD(evalarg);
3670 evalarg.eval_flags = EVAL_EVALUATE;
3671 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672
3673 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003674 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3675 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003676 {
3677 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003678 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003679 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003680
Bram Moolenaar65c44152020-12-24 15:14:01 +01003681 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003683 ++ufunc->uf_refcount;
3684 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685
Bram Moolenaara9931532021-06-12 15:58:16 +02003686 // Compile it here to get the return type. The return type is optional,
3687 // when it's missing use t_unknown. This is recognized in
3688 // compile_return().
3689 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3690 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003691 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692
Bram Moolenaar648594e2021-07-11 17:55:01 +02003693#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003694 // When the outer function is compiled for profiling, the lambda may be
3695 // called without profiling. Compile it here in the right context.
3696 if (cctx->ctx_compile_type == CT_PROFILE)
3697 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003698#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003699
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003700 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3701 // points into it. Point to the original line to avoid a dangling pointer.
3702 if (evalarg.eval_tofree_cmdline != NULL)
3703 {
3704 size_t off = *arg - evalarg.eval_tofree_cmdline;
3705
3706 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3707 + off;
3708 }
3709
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003710 clear_evalarg(&evalarg, NULL);
3711
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003712 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003713 {
3714 // The return type will now be known.
3715 set_function_type(ufunc);
3716
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003717 // The function reference count will be 1. When the ISN_FUNCREF
3718 // instruction is deleted the reference count is decremented and the
3719 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003720 return generate_FUNCREF(cctx, ufunc);
3721 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003722
3723 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 return FAIL;
3725}
3726
3727/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003728 * Get a lambda and compile it. Uses Vim9 syntax.
3729 */
3730 int
3731get_lambda_tv_and_compile(
3732 char_u **arg,
3733 typval_T *rettv,
3734 int types_optional,
3735 evalarg_T *evalarg)
3736{
3737 int r;
3738 ufunc_T *ufunc;
3739 int save_sc_version = current_sctx.sc_version;
3740
3741 // Get the funcref in "rettv".
3742 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3743 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3744 current_sctx.sc_version = save_sc_version;
3745 if (r != OK)
3746 return r;
3747
3748 // "rettv" will now be a partial referencing the function.
3749 ufunc = rettv->vval.v_partial->pt_func;
3750
3751 // Compile it here to get the return type. The return type is optional,
3752 // when it's missing use t_unknown. This is recognized in
3753 // compile_return().
3754 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3755 ufunc->uf_ret_type = &t_unknown;
3756 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3757
3758 if (ufunc->uf_def_status == UF_COMPILED)
3759 {
3760 // The return type will now be known.
3761 set_function_type(ufunc);
3762 return OK;
3763 }
3764 clear_tv(rettv);
3765 return FAIL;
3766}
3767
3768/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003769 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003771 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 */
3773 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003774compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775{
3776 garray_T *instr = &cctx->ctx_instr;
3777 int count = 0;
3778 dict_T *d = dict_alloc();
3779 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003780 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003781 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003782 int is_const;
3783 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784
3785 if (d == NULL)
3786 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003787 if (generate_ppconst(cctx, ppconst) == FAIL)
3788 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003789 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003791 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792
Bram Moolenaar23c55272020-06-21 16:58:13 +02003793 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003794 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003795 *arg = NULL;
3796 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003797 }
3798
3799 if (**arg == '}')
3800 break;
3801
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003802 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003804 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003806 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003807 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003808 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003811 if (isn->isn_type == ISN_PUSHNR)
3812 {
3813 char buf[NUMBUFLEN];
3814
3815 // Convert to string at compile time.
3816 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3817 isn->isn_type = ISN_PUSHS;
3818 isn->isn_arg.string = vim_strsave((char_u *)buf);
3819 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 if (isn->isn_type == ISN_PUSHS)
3821 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003822 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003823 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003824 *arg = skipwhite(*arg);
3825 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003826 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003827 emsg(_(e_missing_matching_bracket_after_dict_key));
3828 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003829 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003830 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003832 else
3833 {
3834 // {"name": value},
3835 // {'name': value},
3836 // {name: value} use "name" as a literal key
3837 key = get_literal_key(arg);
3838 if (key == NULL)
3839 return FAIL;
3840 if (generate_PUSHS(cctx, key) == FAIL)
3841 return FAIL;
3842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843
3844 // Check for duplicate keys, if using string keys.
3845 if (key != NULL)
3846 {
3847 item = dict_find(d, key, -1);
3848 if (item != NULL)
3849 {
3850 semsg(_(e_duplicate_key), key);
3851 goto failret;
3852 }
3853 item = dictitem_alloc(key);
3854 if (item != NULL)
3855 {
3856 item->di_tv.v_type = VAR_UNKNOWN;
3857 item->di_tv.v_lock = 0;
3858 if (dict_add(d, item) == FAIL)
3859 dictitem_free(item);
3860 }
3861 }
3862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 if (**arg != ':')
3864 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003865 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003866 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003867 else
3868 semsg(_(e_missing_dict_colon), *arg);
3869 return FAIL;
3870 }
3871 whitep = *arg + 1;
3872 if (!IS_WHITE_OR_NUL(*whitep))
3873 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003874 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 return FAIL;
3876 }
3877
Bram Moolenaar23c55272020-06-21 16:58:13 +02003878 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003879 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003880 *arg = NULL;
3881 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003882 }
3883
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003884 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003886 if (!is_const)
3887 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 ++count;
3889
Bram Moolenaar2c330432020-04-13 14:41:35 +02003890 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003891 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003892 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003893 *arg = NULL;
3894 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003895 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 if (**arg == '}')
3897 break;
3898 if (**arg != ',')
3899 {
3900 semsg(_(e_missing_dict_comma), *arg);
3901 goto failret;
3902 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003903 if (IS_WHITE_OR_NUL(*whitep))
3904 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003905 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003906 return FAIL;
3907 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003908 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003909 if (!IS_WHITE_OR_NUL(*whitep))
3910 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003911 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003912 return FAIL;
3913 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003914 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 }
3916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 *arg = *arg + 1;
3918
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003919 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003920 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003921 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003922 *arg += STRLEN(*arg);
3923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003925 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 return generate_NEWDICT(cctx, count);
3927
3928failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003929 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003930 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003931 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003932 *arg = (char_u *)"";
3933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 dict_unref(d);
3935 return FAIL;
3936}
3937
3938/*
3939 * Compile "&option".
3940 */
3941 static int
3942compile_get_option(char_u **arg, cctx_T *cctx)
3943{
3944 typval_T rettv;
3945 char_u *start = *arg;
3946 int ret;
3947
3948 // parse the option and get the current value to get the type.
3949 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003950 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 if (ret == OK)
3952 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003953 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003954 char_u *name = vim_strnsave(start, *arg - start);
3955 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3956 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957
3958 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3959 vim_free(name);
3960 }
3961 clear_tv(&rettv);
3962
3963 return ret;
3964}
3965
3966/*
3967 * Compile "$VAR".
3968 */
3969 static int
3970compile_get_env(char_u **arg, cctx_T *cctx)
3971{
3972 char_u *start = *arg;
3973 int len;
3974 int ret;
3975 char_u *name;
3976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 ++*arg;
3978 len = get_env_len(arg);
3979 if (len == 0)
3980 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003981 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 return FAIL;
3983 }
3984
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003985 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 name = vim_strnsave(start, len + 1);
3987 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3988 vim_free(name);
3989 return ret;
3990}
3991
3992/*
3993 * Compile "@r".
3994 */
3995 static int
3996compile_get_register(char_u **arg, cctx_T *cctx)
3997{
3998 int ret;
3999
4000 ++*arg;
4001 if (**arg == NUL)
4002 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004003 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 return FAIL;
4005 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004006 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 {
4008 emsg_invreg(**arg);
4009 return FAIL;
4010 }
4011 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4012 ++*arg;
4013 return ret;
4014}
4015
4016/*
4017 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004018 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 */
4020 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004021apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004023 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024
4025 // this works from end to start
4026 while (p > start)
4027 {
4028 --p;
4029 if (*p == '-' || *p == '+')
4030 {
4031 // only '-' has an effect, for '+' we only check the type
4032#ifdef FEAT_FLOAT
4033 if (rettv->v_type == VAR_FLOAT)
4034 {
4035 if (*p == '-')
4036 rettv->vval.v_float = -rettv->vval.v_float;
4037 }
4038 else
4039#endif
4040 {
4041 varnumber_T val;
4042 int error = FALSE;
4043
4044 // tv_get_number_chk() accepts a string, but we don't want that
4045 // here
4046 if (check_not_string(rettv) == FAIL)
4047 return FAIL;
4048 val = tv_get_number_chk(rettv, &error);
4049 clear_tv(rettv);
4050 if (error)
4051 return FAIL;
4052 if (*p == '-')
4053 val = -val;
4054 rettv->v_type = VAR_NUMBER;
4055 rettv->vval.v_number = val;
4056 }
4057 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004058 else if (numeric_only)
4059 {
4060 ++p;
4061 break;
4062 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004063 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 {
4065 int v = tv2bool(rettv);
4066
4067 // '!' is permissive in the type.
4068 clear_tv(rettv);
4069 rettv->v_type = VAR_BOOL;
4070 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4071 }
4072 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004073 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 return OK;
4075}
4076
4077/*
4078 * Recognize v: variables that are constants and set "rettv".
4079 */
4080 static void
4081get_vim_constant(char_u **arg, typval_T *rettv)
4082{
4083 if (STRNCMP(*arg, "v:true", 6) == 0)
4084 {
4085 rettv->v_type = VAR_BOOL;
4086 rettv->vval.v_number = VVAL_TRUE;
4087 *arg += 6;
4088 }
4089 else if (STRNCMP(*arg, "v:false", 7) == 0)
4090 {
4091 rettv->v_type = VAR_BOOL;
4092 rettv->vval.v_number = VVAL_FALSE;
4093 *arg += 7;
4094 }
4095 else if (STRNCMP(*arg, "v:null", 6) == 0)
4096 {
4097 rettv->v_type = VAR_SPECIAL;
4098 rettv->vval.v_number = VVAL_NULL;
4099 *arg += 6;
4100 }
4101 else if (STRNCMP(*arg, "v:none", 6) == 0)
4102 {
4103 rettv->v_type = VAR_SPECIAL;
4104 rettv->vval.v_number = VVAL_NONE;
4105 *arg += 6;
4106 }
4107}
4108
Bram Moolenaar657137c2021-01-09 15:45:23 +01004109 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004110get_compare_type(char_u *p, int *len, int *type_is)
4111{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004112 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004113 int i;
4114
4115 switch (p[0])
4116 {
4117 case '=': if (p[1] == '=')
4118 type = EXPR_EQUAL;
4119 else if (p[1] == '~')
4120 type = EXPR_MATCH;
4121 break;
4122 case '!': if (p[1] == '=')
4123 type = EXPR_NEQUAL;
4124 else if (p[1] == '~')
4125 type = EXPR_NOMATCH;
4126 break;
4127 case '>': if (p[1] != '=')
4128 {
4129 type = EXPR_GREATER;
4130 *len = 1;
4131 }
4132 else
4133 type = EXPR_GEQUAL;
4134 break;
4135 case '<': if (p[1] != '=')
4136 {
4137 type = EXPR_SMALLER;
4138 *len = 1;
4139 }
4140 else
4141 type = EXPR_SEQUAL;
4142 break;
4143 case 'i': if (p[1] == 's')
4144 {
4145 // "is" and "isnot"; but not a prefix of a name
4146 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4147 *len = 5;
4148 i = p[*len];
4149 if (!isalnum(i) && i != '_')
4150 {
4151 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4152 *type_is = TRUE;
4153 }
4154 }
4155 break;
4156 }
4157 return type;
4158}
4159
4160/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004161 * Skip over an expression, ignoring most errors.
4162 */
4163 static void
4164skip_expr_cctx(char_u **arg, cctx_T *cctx)
4165{
4166 evalarg_T evalarg;
4167
4168 CLEAR_FIELD(evalarg);
4169 evalarg.eval_cctx = cctx;
4170 skip_expr(arg, &evalarg);
4171}
4172
4173/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004175 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 */
4177 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004178compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004180 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181
4182 // this works from end to start
4183 while (p > start)
4184 {
4185 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004186 while (VIM_ISWHITE(*p))
4187 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188 if (*p == '-' || *p == '+')
4189 {
4190 int negate = *p == '-';
4191 isn_T *isn;
4192
4193 // TODO: check type
4194 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4195 {
4196 --p;
4197 if (*p == '-')
4198 negate = !negate;
4199 }
4200 // only '-' has an effect, for '+' we only check the type
4201 if (negate)
4202 isn = generate_instr(cctx, ISN_NEGATENR);
4203 else
4204 isn = generate_instr(cctx, ISN_CHECKNR);
4205 if (isn == NULL)
4206 return FAIL;
4207 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004208 else if (numeric_only)
4209 {
4210 ++p;
4211 break;
4212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 else
4214 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004215 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004217 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004219 if (p[-1] == '!')
4220 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004223 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 return FAIL;
4225 }
4226 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004227 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 return OK;
4229}
4230
4231/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004232 * Compile "(expression)": recursive!
4233 * Return FAIL/OK.
4234 */
4235 static int
4236compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4237{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004238 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004239 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004240
Bram Moolenaar24156692021-01-14 20:35:49 +01004241 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4242 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004243 if (ppconst->pp_used <= PPSIZE - 10)
4244 {
4245 ret = compile_expr1(arg, cctx, ppconst);
4246 }
4247 else
4248 {
4249 // Not enough space in ppconst, flush constants.
4250 if (generate_ppconst(cctx, ppconst) == FAIL)
4251 return FAIL;
4252 ret = compile_expr0(arg, cctx);
4253 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004254 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4255 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004256 if (**arg == ')')
4257 ++*arg;
4258 else if (ret == OK)
4259 {
4260 emsg(_(e_missing_close));
4261 ret = FAIL;
4262 }
4263 return ret;
4264}
4265
4266/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004268 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 */
4270 static int
4271compile_subscript(
4272 char_u **arg,
4273 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004274 char_u *start_leader,
4275 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004276 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004278 char_u *name_start = *end_leader;
4279
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 for (;;)
4281 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004282 char_u *p = skipwhite(*arg);
4283
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004284 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004285 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004286 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004287
4288 // If a following line starts with "->{" or "->X" advance to that
4289 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004290 // Also if a following line starts with ".x".
4291 if (next != NULL &&
4292 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004293 && (next[2] == '{'
4294 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004295 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004296 {
4297 next = next_line_from_context(cctx, TRUE);
4298 if (next == NULL)
4299 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004300 *arg = next;
4301 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004302 }
4303 }
4304
Bram Moolenaarcd268012021-07-22 19:11:08 +02004305 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4306 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004307 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004309 garray_T *stack = &cctx->ctx_type_stack;
4310 type_T *type;
4311 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004313 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004314 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004315 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004316
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004318 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4319
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004320 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004321 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004323 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 return FAIL;
4325 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004326 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004328 char_u *pstart = p;
4329
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004330 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004331 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004332 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004333
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 // something->method()
4335 // Apply the '!', '-' and '+' first:
4336 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004337 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004340 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004341 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004342 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004343 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004344 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004345 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004346 garray_T *stack = &cctx->ctx_type_stack;
4347 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004348 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004349 int expr_isn_start = cctx->ctx_instr.ga_len;
4350 int expr_isn_end;
4351 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004352
4353 // Funcref call: list->(Refs[2])(arg)
4354 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004355 //
4356 // Fist compile the function expression.
4357 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004358 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004359
4360 // Remember the next instruction index, where the instructions
4361 // for arguments are being written.
4362 expr_isn_end = cctx->ctx_instr.ga_len;
4363
4364 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004365 if (**arg != '(')
4366 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004367 if (*skipwhite(*arg) == '(')
4368 emsg(_(e_nowhitespace));
4369 else
4370 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004371 return FAIL;
4372 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004373 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004374 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004375 return FAIL;
4376
Bram Moolenaar2927c072021-04-05 19:41:21 +02004377 // Move the instructions for the arguments to before the
4378 // instructions of the expression and move the type of the
4379 // expression after the argument types. This is what ISN_PCALL
4380 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004381 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004382 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4383 if (arg_isn_count > 0)
4384 {
4385 int expr_isn_count = expr_isn_end - expr_isn_start;
4386 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4387
4388 if (isn == NULL)
4389 return FAIL;
4390 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4391 + expr_isn_start,
4392 sizeof(isn_T) * expr_isn_count);
4393 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4394 + expr_isn_start,
4395 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4396 sizeof(isn_T) * arg_isn_count);
4397 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4398 + expr_isn_start + arg_isn_count,
4399 isn, sizeof(isn_T) * expr_isn_count);
4400 vim_free(isn);
4401
4402 type = ((type_T **)stack->ga_data)[type_idx_start];
4403 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4404 ((type_T **)stack->ga_data) + type_idx_start + 1,
4405 sizeof(type_T *)
4406 * (stack->ga_len - type_idx_start - 1));
4407 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4408 }
4409
Bram Moolenaar7e368202020-12-25 21:56:57 +01004410 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004411 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 return FAIL;
4413 }
4414 else
4415 {
4416 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004417 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004418 if (!eval_isnamec1(*p))
4419 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004420 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004421 return FAIL;
4422 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004423 if (ASCII_ISALPHA(*p) && p[1] == ':')
4424 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004425 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 ;
4427 if (*p != '(')
4428 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004429 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 return FAIL;
4431 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004432 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 return FAIL;
4434 }
4435 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004436 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004438 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004439
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004440 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004441 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004442 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004443 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004444 // TODO: more arguments
4445 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004446 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004447 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004448 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004449
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004450 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004451 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004452 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004453 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004454 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004455 // missing first index is equal to zero
4456 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004457 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004458 else
4459 {
4460 if (compile_expr0(arg, cctx) == FAIL)
4461 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004462 if (**arg == ':')
4463 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004464 semsg(_(e_white_space_required_before_and_after_str_at_str),
4465 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004466 return FAIL;
4467 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004468 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004469 return FAIL;
4470 *arg = skipwhite(*arg);
4471 }
4472 if (**arg == ':')
4473 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004474 is_slice = TRUE;
4475 ++*arg;
4476 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4477 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004478 semsg(_(e_white_space_required_before_and_after_str_at_str),
4479 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004480 return FAIL;
4481 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004482 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004483 return FAIL;
4484 if (**arg == ']')
4485 // missing second index is equal to end of string
4486 generate_PUSHNR(cctx, -1);
4487 else
4488 {
4489 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004490 return FAIL;
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 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496
4497 if (**arg != ']')
4498 {
4499 emsg(_(e_missbrac));
4500 return FAIL;
4501 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004502 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503
Bram Moolenaare42939a2021-04-05 17:11:17 +02004504 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004505 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004507 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004509 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004510 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004511 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004512 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004513
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004514 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004515 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004516 {
4517 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004518 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004519 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004520 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004521 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 while (eval_isnamec(*p))
4523 MB_PTR_ADV(p);
4524 if (p == *arg)
4525 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004526 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 return FAIL;
4528 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004529 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 return FAIL;
4531 *arg = p;
4532 }
4533 else
4534 break;
4535 }
4536
4537 // TODO - see handle_subscript():
4538 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4539 // Don't do this when "Func" is already a partial that was bound
4540 // explicitly (pt_auto is FALSE).
4541
4542 return OK;
4543}
4544
4545/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004546 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4547 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004549 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004550 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004551 *
4552 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 */
4554
4555/*
4556 * number number constant
4557 * 0zFFFFFFFF Blob constant
4558 * "string" string constant
4559 * 'string' literal string constant
4560 * &option-name option value
4561 * @r register contents
4562 * identifier variable value
4563 * function() function call
4564 * $VAR environment variable
4565 * (expression) nested expression
4566 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004567 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 *
4569 * Also handle:
4570 * ! in front logical NOT
4571 * - in front unary minus
4572 * + in front unary plus (ignored)
4573 * trailing (arg) funcref/partial call
4574 * trailing [] subscript in String or List
4575 * trailing .name entry in Dictionary
4576 * trailing ->name() method call
4577 */
4578 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004579compile_expr7(
4580 char_u **arg,
4581 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004582 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 char_u *start_leader, *end_leader;
4585 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004586 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004587 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004589 ppconst->pp_is_const = FALSE;
4590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 /*
4592 * Skip '!', '-' and '+' characters. They are handled later.
4593 */
4594 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004595 if (eval_leader(arg, TRUE) == FAIL)
4596 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 end_leader = *arg;
4598
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004599 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 switch (**arg)
4601 {
4602 /*
4603 * Number constant.
4604 */
4605 case '0': // also for blob starting with 0z
4606 case '1':
4607 case '2':
4608 case '3':
4609 case '4':
4610 case '5':
4611 case '6':
4612 case '7':
4613 case '8':
4614 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004615 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004617 // Apply "-" and "+" just before the number now, right to
4618 // left. Matters especially when "->" follows. Stops at
4619 // '!'.
4620 if (apply_leader(rettv, TRUE,
4621 start_leader, &end_leader) == FAIL)
4622 {
4623 clear_tv(rettv);
4624 return FAIL;
4625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 break;
4627
4628 /*
4629 * String constant: "string".
4630 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004631 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 return FAIL;
4633 break;
4634
4635 /*
4636 * Literal string constant: 'str''ing'.
4637 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004638 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 return FAIL;
4640 break;
4641
4642 /*
4643 * Constant Vim variable.
4644 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004645 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 ret = NOTDONE;
4647 break;
4648
4649 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004650 * "true" constant
4651 */
4652 case 't': if (STRNCMP(*arg, "true", 4) == 0
4653 && !eval_isnamec((*arg)[4]))
4654 {
4655 *arg += 4;
4656 rettv->v_type = VAR_BOOL;
4657 rettv->vval.v_number = VVAL_TRUE;
4658 }
4659 else
4660 ret = NOTDONE;
4661 break;
4662
4663 /*
4664 * "false" constant
4665 */
4666 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4667 && !eval_isnamec((*arg)[5]))
4668 {
4669 *arg += 5;
4670 rettv->v_type = VAR_BOOL;
4671 rettv->vval.v_number = VVAL_FALSE;
4672 }
4673 else
4674 ret = NOTDONE;
4675 break;
4676
4677 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004678 * "null" constant
4679 */
4680 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004681 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004682 {
4683 *arg += 4;
4684 rettv->v_type = VAR_SPECIAL;
4685 rettv->vval.v_number = VVAL_NULL;
4686 }
4687 else
4688 ret = NOTDONE;
4689 break;
4690
4691 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 * List: [expr, expr]
4693 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004694 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4695 return FAIL;
4696 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 break;
4698
4699 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 * Dictionary: {'key': val, 'key': val}
4701 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004702 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4703 return FAIL;
4704 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 break;
4706
4707 /*
4708 * Option value: &name
4709 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004710 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4711 return FAIL;
4712 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 break;
4714
4715 /*
4716 * Environment variable: $VAR.
4717 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004718 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4719 return FAIL;
4720 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 break;
4722
4723 /*
4724 * Register contents: @r.
4725 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004726 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4727 return FAIL;
4728 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 break;
4730 /*
4731 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004732 * lambda: (arg, arg) => expr
4733 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004735 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4736 ret = compile_lambda(arg, cctx);
4737 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004738 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 break;
4740
4741 default: ret = NOTDONE;
4742 break;
4743 }
4744 if (ret == FAIL)
4745 return FAIL;
4746
Bram Moolenaar1c747212020-05-09 18:28:34 +02004747 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004749 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004750 clear_tv(rettv);
4751 else
4752 // A constant expression can possibly be handled compile time,
4753 // return the value instead of generating code.
4754 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 }
4756 else if (ret == NOTDONE)
4757 {
4758 char_u *p;
4759 int r;
4760
4761 if (!eval_isnamec1(**arg))
4762 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004763 if (!vim9_bad_comment(*arg))
4764 {
4765 if (ends_excmd(*skipwhite(*arg)))
4766 semsg(_(e_empty_expression_str), *arg);
4767 else
4768 semsg(_(e_name_expected_str), *arg);
4769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 return FAIL;
4771 }
4772
4773 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004774 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004775 if (p - *arg == (size_t)1 && **arg == '_')
4776 {
4777 emsg(_(e_cannot_use_underscore_here));
4778 return FAIL;
4779 }
4780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004782 {
4783 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004786 {
4787 if (generate_ppconst(cctx, ppconst) == FAIL)
4788 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004789 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 if (r == FAIL)
4792 return FAIL;
4793 }
4794
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004795 // Handle following "[]", ".member", etc.
4796 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004797 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004798 ppconst) == FAIL)
4799 return FAIL;
4800 if (ppconst->pp_used > 0)
4801 {
4802 // apply the '!', '-' and '+' before the constant
4803 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004804 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004805 return FAIL;
4806 return OK;
4807 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004808 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004810 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811}
4812
4813/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004814 * Give the "white on both sides" error, taking the operator from "p[len]".
4815 */
4816 void
4817error_white_both(char_u *op, int len)
4818{
4819 char_u buf[10];
4820
4821 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004822 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004823}
4824
4825/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004826 * <type>expr7: runtime type check / conversion
4827 */
4828 static int
4829compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4830{
4831 type_T *want_type = NULL;
4832
4833 // Recognize <type>
4834 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4835 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004836 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004837 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4838 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004839 return FAIL;
4840
4841 if (**arg != '>')
4842 {
4843 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004844 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004845 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004846 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004847 return FAIL;
4848 }
4849 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004850 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004851 return FAIL;
4852 }
4853
4854 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4855 return FAIL;
4856
4857 if (want_type != NULL)
4858 {
4859 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004860 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004861 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004862
Bram Moolenaard1103582020-08-14 22:44:25 +02004863 generate_ppconst(cctx, ppconst);
4864 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004865 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004866 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004867 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004868 return FAIL;
4869 }
4870 }
4871
4872 return OK;
4873}
4874
4875/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 * * number multiplication
4877 * / number division
4878 * % number modulo
4879 */
4880 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004881compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882{
4883 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004884 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004885 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004887 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004888 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 return FAIL;
4890
4891 /*
4892 * Repeat computing, until no "*", "/" or "%" is following.
4893 */
4894 for (;;)
4895 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004896 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 if (*op != '*' && *op != '/' && *op != '%')
4898 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004899 if (next != NULL)
4900 {
4901 *arg = next_line_from_context(cctx, TRUE);
4902 op = skipwhite(*arg);
4903 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004904
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004905 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004907 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004908 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004910 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004911 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004913 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004914 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004916
4917 if (ppconst->pp_used == ppconst_used + 2
4918 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4919 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004920 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004921 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4922 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4923 varnumber_T res = 0;
4924 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004926 // both are numbers: compute the result
4927 switch (*op)
4928 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004929 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004930 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004931 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004932 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004933 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004934 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004935 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004936 break;
4937 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004938 if (failed)
4939 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004940 tv1->vval.v_number = res;
4941 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004942 }
4943 else
4944 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004945 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004946 generate_two_op(cctx, op);
4947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 }
4949
4950 return OK;
4951}
4952
4953/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004954 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 * - number subtraction
4956 * .. string concatenation
4957 */
4958 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004959compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960{
4961 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004962 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004964 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965
4966 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004967 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 return FAIL;
4969
4970 /*
4971 * Repeat computing, until no "+", "-" or ".." is following.
4972 */
4973 for (;;)
4974 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004975 op = may_peek_next_line(cctx, *arg, &next);
4976 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004978 if (op[0] == op[1] && *op != '.' && next)
4979 // Finding "++" or "--" on the next line is a separate command.
4980 // But ".." is concatenation.
4981 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004983 if (next != NULL)
4984 {
4985 *arg = next_line_from_context(cctx, TRUE);
4986 op = skipwhite(*arg);
4987 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004989 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004991 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004992 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 }
4994
Bram Moolenaare0de1712020-12-02 17:36:54 +01004995 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004996 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004998 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004999 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 return FAIL;
5001
Bram Moolenaara5565e42020-05-09 15:44:01 +02005002 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005003 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005004 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5005 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5006 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5007 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005009 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5010 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005011
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005012 // concat/subtract/add constant numbers
5013 if (*op == '+')
5014 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5015 else if (*op == '-')
5016 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5017 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005018 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005019 // concatenate constant strings
5020 char_u *s1 = tv1->vval.v_string;
5021 char_u *s2 = tv2->vval.v_string;
5022 size_t len1 = STRLEN(s1);
5023
5024 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5025 if (tv1->vval.v_string == NULL)
5026 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005027 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005028 return FAIL;
5029 }
5030 mch_memmove(tv1->vval.v_string, s1, len1);
5031 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005032 vim_free(s1);
5033 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005034 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005035 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 }
5037 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005038 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005039 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005040 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005041 if (*op == '.')
5042 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005043 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5044 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005045 return FAIL;
5046 generate_instr_drop(cctx, ISN_CONCAT, 1);
5047 }
5048 else
5049 generate_two_op(cctx, op);
5050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 }
5052
5053 return OK;
5054}
5055
5056/*
5057 * expr5a == expr5b
5058 * expr5a =~ expr5b
5059 * expr5a != expr5b
5060 * expr5a !~ expr5b
5061 * expr5a > expr5b
5062 * expr5a >= expr5b
5063 * expr5a < expr5b
5064 * expr5a <= expr5b
5065 * expr5a is expr5b
5066 * expr5a isnot expr5b
5067 *
5068 * Produces instructions:
5069 * EVAL expr5a Push result of "expr5a"
5070 * EVAL expr5b Push result of "expr5b"
5071 * COMPARE one of the compare instructions
5072 */
5073 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005074compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005076 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005078 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005081 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082
5083 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005084 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 return FAIL;
5086
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005087 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005088 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089
5090 /*
5091 * If there is a comparative operator, use it.
5092 */
5093 if (type != EXPR_UNKNOWN)
5094 {
5095 int ic = FALSE; // Default: do not ignore case
5096
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005097 if (next != NULL)
5098 {
5099 *arg = next_line_from_context(cctx, TRUE);
5100 p = skipwhite(*arg);
5101 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 if (type_is && (p[len] == '?' || p[len] == '#'))
5103 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005104 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 return FAIL;
5106 }
5107 // extra question mark appended: ignore case
5108 if (p[len] == '?')
5109 {
5110 ic = TRUE;
5111 ++len;
5112 }
5113 // extra '#' appended: match case (ignored)
5114 else if (p[len] == '#')
5115 ++len;
5116 // nothing appended: match case
5117
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005118 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005120 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005121 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122 }
5123
5124 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005125 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005126 return FAIL;
5127
Bram Moolenaara5565e42020-05-09 15:44:01 +02005128 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129 return FAIL;
5130
Bram Moolenaara5565e42020-05-09 15:44:01 +02005131 if (ppconst->pp_used == ppconst_used + 2)
5132 {
5133 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5134 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5135 int ret;
5136
5137 // Both sides are a constant, compute the result now.
5138 // First check for a valid combination of types, this is more
5139 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005140 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005141 ret = FAIL;
5142 else
5143 {
5144 ret = typval_compare(tv1, tv2, type, ic);
5145 tv1->v_type = VAR_BOOL;
5146 tv1->vval.v_number = tv1->vval.v_number
5147 ? VVAL_TRUE : VVAL_FALSE;
5148 clear_tv(tv2);
5149 --ppconst->pp_used;
5150 }
5151 return ret;
5152 }
5153
5154 generate_ppconst(cctx, ppconst);
5155 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005156 }
5157
5158 return OK;
5159}
5160
Bram Moolenaar7f141552020-05-09 17:35:53 +02005161static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163/*
5164 * Compile || or &&.
5165 */
5166 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005167compile_and_or(
5168 char_u **arg,
5169 cctx_T *cctx,
5170 char *op,
5171 ppconst_T *ppconst,
5172 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005174 char_u *next;
5175 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 int opchar = *op;
5177
5178 if (p[0] == opchar && p[1] == opchar)
5179 {
5180 garray_T *instr = &cctx->ctx_instr;
5181 garray_T end_ga;
5182
5183 /*
5184 * Repeat until there is no following "||" or "&&"
5185 */
5186 ga_init2(&end_ga, sizeof(int), 10);
5187 while (p[0] == opchar && p[1] == opchar)
5188 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005189 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005190 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005191 int start_ctx_lnum = cctx->ctx_lnum;
5192 int save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005193 int status;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005194
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005195 if (next != NULL)
5196 {
5197 *arg = next_line_from_context(cctx, TRUE);
5198 p = skipwhite(*arg);
5199 }
5200
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005201 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5202 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005203 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005204 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005205 return FAIL;
5206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005208 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005209 SOURCING_LNUM = start_lnum;
5210 save_lnum = cctx->ctx_lnum;
5211 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005212
5213 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005214 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005215 {
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005216 // TODO: use ppconst if the value is a constant
5217 generate_ppconst(cctx, ppconst);
5218
5219 // Every part must evaluate to a bool.
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005220 status = bool_on_stack(cctx);
5221 if (status != FAIL)
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005222 status = ga_grow(&end_ga, 1);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005223 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005224 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005225 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005226 {
5227 ga_clear(&end_ga);
5228 return FAIL;
5229 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5232 ++end_ga.ga_len;
5233 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005234 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235
5236 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005237 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005238 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005239 {
5240 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005241 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005242 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005243
Bram Moolenaara5565e42020-05-09 15:44:01 +02005244 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5245 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246 {
5247 ga_clear(&end_ga);
5248 return FAIL;
5249 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005250
5251 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005253
5254 if (check_ppconst_bool(ppconst) == FAIL)
5255 {
5256 ga_clear(&end_ga);
5257 return FAIL;
5258 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005259 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005260
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005261 // Every part must evaluate to a bool.
5262 if (bool_on_stack(cctx) == FAIL)
5263 {
5264 ga_clear(&end_ga);
5265 return FAIL;
5266 }
5267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005268 // Fill in the end label in all jumps.
5269 while (end_ga.ga_len > 0)
5270 {
5271 isn_T *isn;
5272
5273 --end_ga.ga_len;
5274 isn = ((isn_T *)instr->ga_data)
5275 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5276 isn->isn_arg.jump.jump_where = instr->ga_len;
5277 }
5278 ga_clear(&end_ga);
5279 }
5280
5281 return OK;
5282}
5283
5284/*
5285 * expr4a && expr4a && expr4a logical AND
5286 *
5287 * Produces instructions:
5288 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005289 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005290 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005292 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005293 * EVAL expr4c Push result of "expr4c"
5294 * end:
5295 */
5296 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005297compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005298{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005299 int ppconst_used = ppconst->pp_used;
5300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005302 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303 return FAIL;
5304
5305 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005306 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005307}
5308
5309/*
5310 * expr3a || expr3b || expr3c logical OR
5311 *
5312 * Produces instructions:
5313 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005314 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005315 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005316 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005317 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 * EVAL expr3c Push result of "expr3c"
5319 * end:
5320 */
5321 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005322compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005323{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005324 int ppconst_used = ppconst->pp_used;
5325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005326 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005327 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328 return FAIL;
5329
5330 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005331 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005332}
5333
5334/*
5335 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005337 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338 * JUMP_IF_FALSE alt jump if false
5339 * EVAL expr1a
5340 * JUMP_ALWAYS end
5341 * alt: EVAL expr1b
5342 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005343 *
5344 * Toplevel expression: expr2 ?? expr1
5345 * Produces instructions:
5346 * EVAL expr2 Push result of "expr2"
5347 * JUMP_AND_KEEP_IF_TRUE end jump if true
5348 * EVAL expr1
5349 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350 */
5351 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005352compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005353{
5354 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005355 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005356 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357
Bram Moolenaar3988f642020-08-27 22:43:03 +02005358 // Ignore all kinds of errors when not producing code.
5359 if (cctx->ctx_skip == SKIP_YES)
5360 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005361 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005362 return OK;
5363 }
5364
Bram Moolenaar61a89812020-05-07 16:58:17 +02005365 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005366 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367 return FAIL;
5368
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005369 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370 if (*p == '?')
5371 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005372 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373 garray_T *instr = &cctx->ctx_instr;
5374 garray_T *stack = &cctx->ctx_type_stack;
5375 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005376 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005378 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005379 int has_const_expr = FALSE;
5380 int const_value = FALSE;
5381 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005382
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005383 if (next != NULL)
5384 {
5385 *arg = next_line_from_context(cctx, TRUE);
5386 p = skipwhite(*arg);
5387 }
5388
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005389 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005390 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005391 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005392 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005393 return FAIL;
5394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395
Bram Moolenaara5565e42020-05-09 15:44:01 +02005396 if (ppconst->pp_used == ppconst_used + 1)
5397 {
5398 // the condition is a constant, we know whether the ? or the :
5399 // expression is to be evaluated.
5400 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005401 if (op_falsy)
5402 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5403 else
5404 {
5405 int error = FALSE;
5406
5407 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5408 &error);
5409 if (error)
5410 return FAIL;
5411 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005412 cctx->ctx_skip = save_skip == SKIP_YES ||
5413 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5414
5415 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5416 // "left ?? right" and "left" is truthy: produce "left"
5417 generate_ppconst(cctx, ppconst);
5418 else
5419 {
5420 clear_tv(&ppconst->pp_tv[ppconst_used]);
5421 --ppconst->pp_used;
5422 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005423 }
5424 else
5425 {
5426 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005427 if (op_falsy)
5428 end_idx = instr->ga_len;
5429 generate_JUMP(cctx, op_falsy
5430 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5431 if (op_falsy)
5432 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005433 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434
5435 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005436 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005437 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005438 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005439 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005440
Bram Moolenaara5565e42020-05-09 15:44:01 +02005441 if (!has_const_expr)
5442 {
5443 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005444
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005445 if (!op_falsy)
5446 {
5447 // remember the type and drop it
5448 --stack->ga_len;
5449 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005451 end_idx = instr->ga_len;
5452 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005453
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005454 // jump here from JUMP_IF_FALSE
5455 isn = ((isn_T *)instr->ga_data) + alt_idx;
5456 isn->isn_arg.jump.jump_where = instr->ga_len;
5457 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005458 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005460 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005461 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005462 // Check for the ":".
5463 p = may_peek_next_line(cctx, *arg, &next);
5464 if (*p != ':')
5465 {
5466 emsg(_(e_missing_colon));
5467 return FAIL;
5468 }
5469 if (next != NULL)
5470 {
5471 *arg = next_line_from_context(cctx, TRUE);
5472 p = skipwhite(*arg);
5473 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005474
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005475 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5476 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005477 semsg(_(e_white_space_required_before_and_after_str_at_str),
5478 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005479 return FAIL;
5480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005481
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005482 // evaluate the third expression
5483 if (has_const_expr)
5484 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005485 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005486 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005487 return FAIL;
5488 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5489 return FAIL;
5490 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491
Bram Moolenaara5565e42020-05-09 15:44:01 +02005492 if (!has_const_expr)
5493 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005494 type_T **typep;
5495
Bram Moolenaara5565e42020-05-09 15:44:01 +02005496 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497
Bram Moolenaara5565e42020-05-09 15:44:01 +02005498 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005499 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5500 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005501
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005502 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005503 isn = ((isn_T *)instr->ga_data) + end_idx;
5504 isn->isn_arg.jump.jump_where = instr->ga_len;
5505 }
5506
5507 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005508 }
5509 return OK;
5510}
5511
5512/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005513 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005514 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5515 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005516 */
5517 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005518compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005519{
5520 ppconst_T ppconst;
5521
5522 CLEAR_FIELD(ppconst);
5523 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5524 {
5525 clear_ppconst(&ppconst);
5526 return FAIL;
5527 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005528 if (is_const != NULL)
5529 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005530 if (generate_ppconst(cctx, &ppconst) == FAIL)
5531 return FAIL;
5532 return OK;
5533}
5534
5535/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005536 * Toplevel expression.
5537 */
5538 static int
5539compile_expr0(char_u **arg, cctx_T *cctx)
5540{
5541 return compile_expr0_ext(arg, cctx, NULL);
5542}
5543
5544/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005545 * Compile "return [expr]".
5546 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005547 */
5548 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005549compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550{
5551 char_u *p = arg;
5552 garray_T *stack = &cctx->ctx_type_stack;
5553 type_T *stack_type;
5554
5555 if (*p != NUL && *p != '|' && *p != '\n')
5556 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005557 if (legacy)
5558 {
5559 int save_flags = cmdmod.cmod_flags;
5560
5561 generate_LEGACY_EVAL(cctx, p);
5562 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5563 0, cctx, FALSE, FALSE) == FAIL)
5564 return NULL;
5565 cmdmod.cmod_flags |= CMOD_LEGACY;
5566 (void)skip_expr(&p, NULL);
5567 cmdmod.cmod_flags = save_flags;
5568 }
5569 else
5570 {
5571 // compile return argument into instructions
5572 if (compile_expr0(&p, cctx) == FAIL)
5573 return NULL;
5574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005575
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005576 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005577 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005578 // "check_return_type" with uf_ret_type set to &t_unknown is used
5579 // for an inline function without a specified return type. Set the
5580 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005581 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005582 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005583 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5584 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005585 || (!check_return_type
5586 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005587 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005588 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005589 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005590 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005591 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005592 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5593 && stack_type->tt_type != VAR_VOID
5594 && stack_type->tt_type != VAR_UNKNOWN)
5595 {
5596 emsg(_(e_returning_value_in_function_without_return_type));
5597 return NULL;
5598 }
5599 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005600 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005601 return NULL;
5602 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005604 }
5605 else
5606 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005607 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005608 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005609 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5610 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005611 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005612 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613 return NULL;
5614 }
5615
5616 // No argument, return zero.
5617 generate_PUSHNR(cctx, 0);
5618 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005619
5620 // Undo any command modifiers.
5621 generate_undo_cmdmods(cctx);
5622
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005623 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005624 return NULL;
5625
5626 // "return val | endif" is possible
5627 return skipwhite(p);
5628}
5629
5630/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005631 * Get a line from the compilation context, compatible with exarg_T getline().
5632 * Return a pointer to the line in allocated memory.
5633 * Return NULL for end-of-file or some error.
5634 */
5635 static char_u *
5636exarg_getline(
5637 int c UNUSED,
5638 void *cookie,
5639 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005640 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005641{
5642 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005643 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005644
Bram Moolenaar66250c92020-08-20 15:02:42 +02005645 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005646 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005647 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005648 return NULL;
5649 ++cctx->ctx_lnum;
5650 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5651 // Comment lines result in NULL pointers, skip them.
5652 if (p != NULL)
5653 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005654 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005655}
5656
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005657 void
5658fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5659{
5660 eap->getline = exarg_getline;
5661 eap->cookie = cctx;
5662}
5663
Bram Moolenaar04b12692020-05-04 23:24:44 +02005664/*
5665 * Compile a nested :def command.
5666 */
5667 static char_u *
5668compile_nested_function(exarg_T *eap, cctx_T *cctx)
5669{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005670 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005671 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005672 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005673 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005674 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005675 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005676 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005677
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005678 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005679 {
5680 emsg(_(e_cannot_use_bang_with_nested_def));
5681 return NULL;
5682 }
5683
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005684 if (*name_start == '/')
5685 {
5686 name_end = skip_regexp(name_start + 1, '/', TRUE);
5687 if (*name_end == '/')
5688 ++name_end;
5689 eap->nextcmd = check_nextcmd(name_end);
5690 }
5691 if (name_end == name_start || *skipwhite(name_end) != '(')
5692 {
5693 if (!ends_excmd2(name_start, name_end))
5694 {
5695 semsg(_(e_invalid_command_str), eap->cmd);
5696 return NULL;
5697 }
5698
5699 // "def" or "def Name": list functions
5700 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5701 return NULL;
5702 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5703 }
5704
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005705 // Only g:Func() can use a namespace.
5706 if (name_start[1] == ':' && !is_global)
5707 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005708 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005709 return NULL;
5710 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005711 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005712 return NULL;
5713
Bram Moolenaar04b12692020-05-04 23:24:44 +02005714 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005715 fill_exarg_from_cctx(eap, cctx);
5716
Bram Moolenaar04b12692020-05-04 23:24:44 +02005717 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005718 lambda_name = vim_strsave(get_lambda_name());
5719 if (lambda_name == NULL)
5720 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005721 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005722
Bram Moolenaar822ba242020-05-24 23:00:18 +02005723 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005724 {
5725 r = eap->skip ? OK : FAIL;
5726 goto theend;
5727 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005728
5729 // copy over the block scope IDs before compiling
5730 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5731 {
5732 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5733
5734 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5735 if (ufunc->uf_block_ids != NULL)
5736 {
5737 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5738 sizeof(int) * block_depth);
5739 ufunc->uf_block_depth = block_depth;
5740 }
5741 }
5742
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005743 compile_type = COMPILE_TYPE(ufunc);
5744#ifdef FEAT_PROFILE
5745 // If the outer function is profiled, also compile the nested function for
5746 // profiling.
5747 if (cctx->ctx_compile_type == CT_PROFILE)
5748 compile_type = CT_PROFILE;
5749#endif
5750 if (func_needs_compiling(ufunc, compile_type)
5751 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005752 {
5753 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005754 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005755 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005756
Bram Moolenaar648594e2021-07-11 17:55:01 +02005757#ifdef FEAT_PROFILE
5758 // When the outer function is compiled for profiling, the nested function
5759 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005760 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005761 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5762#endif
5763
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005764 if (is_global)
5765 {
5766 char_u *func_name = vim_strnsave(name_start + 2,
5767 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005768
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005769 if (func_name == NULL)
5770 r = FAIL;
5771 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005772 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005773 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005774 lambda_name = NULL;
5775 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005776 }
5777 else
5778 {
5779 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005780 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005781 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005782
Bram Moolenaareef21022020-08-01 22:16:43 +02005783 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005784 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005785 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005786 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005787 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5788 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005789 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005790
5791theend:
5792 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005793 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005794}
5795
5796/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005797 * Return the length of an assignment operator, or zero if there isn't one.
5798 */
5799 int
5800assignment_len(char_u *p, int *heredoc)
5801{
5802 if (*p == '=')
5803 {
5804 if (p[1] == '<' && p[2] == '<')
5805 {
5806 *heredoc = TRUE;
5807 return 3;
5808 }
5809 return 1;
5810 }
5811 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5812 return 2;
5813 if (STRNCMP(p, "..=", 3) == 0)
5814 return 3;
5815 return 0;
5816}
5817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005818/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005819 * Generate the load instruction for "name".
5820 */
5821 static void
5822generate_loadvar(
5823 cctx_T *cctx,
5824 assign_dest_T dest,
5825 char_u *name,
5826 lvar_T *lvar,
5827 type_T *type)
5828{
5829 switch (dest)
5830 {
5831 case dest_option:
5832 // TODO: check the option exists
5833 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5834 break;
5835 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005836 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5837 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5838 else
5839 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005840 break;
5841 case dest_buffer:
5842 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5843 break;
5844 case dest_window:
5845 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5846 break;
5847 case dest_tab:
5848 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5849 break;
5850 case dest_script:
5851 compile_load_scriptvar(cctx,
5852 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5853 break;
5854 case dest_env:
5855 // Include $ in the name here
5856 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5857 break;
5858 case dest_reg:
5859 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5860 break;
5861 case dest_vimvar:
5862 generate_LOADV(cctx, name + 2, TRUE);
5863 break;
5864 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005865 if (lvar->lv_from_outer > 0)
5866 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5867 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005868 else
5869 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5870 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005871 case dest_expr:
5872 // list or dict value should already be on the stack.
5873 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005874 }
5875}
5876
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005877/*
5878 * Skip over "[expr]" or ".member".
5879 * Does not check for any errors.
5880 */
5881 static char_u *
5882skip_index(char_u *start)
5883{
5884 char_u *p = start;
5885
5886 if (*p == '[')
5887 {
5888 p = skipwhite(p + 1);
5889 (void)skip_expr(&p, NULL);
5890 p = skipwhite(p);
5891 if (*p == ']')
5892 return p + 1;
5893 return p;
5894 }
5895 // if (*p == '.')
5896 return to_name_end(p + 1, TRUE);
5897}
5898
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005899 void
5900vim9_declare_error(char_u *name)
5901{
5902 char *scope = "";
5903
5904 switch (*name)
5905 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005906 case 'g': scope = _("global"); break;
5907 case 'b': scope = _("buffer"); break;
5908 case 'w': scope = _("window"); break;
5909 case 't': scope = _("tab"); break;
5910 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005911 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005912 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005913 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005914 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005915 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005916 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005917 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005918 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005919 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005920}
5921
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005922/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005923 * For one assignment figure out the type of destination. Return it in "dest".
5924 * When not recognized "dest" is not set.
5925 * For an option "opt_flags" is set.
5926 * For a v:var "vimvaridx" is set.
5927 * "type" is set to the destination type if known, unchanted otherwise.
5928 * Return FAIL if an error message was given.
5929 */
5930 static int
5931get_var_dest(
5932 char_u *name,
5933 assign_dest_T *dest,
5934 int cmdidx,
5935 int *opt_flags,
5936 int *vimvaridx,
5937 type_T **type,
5938 cctx_T *cctx)
5939{
5940 char_u *p;
5941
5942 if (*name == '&')
5943 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005944 int cc;
5945 long numval;
5946 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005947
5948 *dest = dest_option;
5949 if (cmdidx == CMD_final || cmdidx == CMD_const)
5950 {
5951 emsg(_(e_const_option));
5952 return FAIL;
5953 }
5954 p = name;
5955 p = find_option_end(&p, opt_flags);
5956 if (p == NULL)
5957 {
5958 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005959 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005960 return FAIL;
5961 }
5962 cc = *p;
5963 *p = NUL;
5964 opt_type = get_option_value(skip_option_env_lead(name),
5965 &numval, NULL, *opt_flags);
5966 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005967 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005968 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005969 case gov_unknown:
5970 semsg(_(e_unknown_option), name);
5971 return FAIL;
5972 case gov_string:
5973 case gov_hidden_string:
5974 *type = &t_string;
5975 break;
5976 case gov_bool:
5977 case gov_hidden_bool:
5978 *type = &t_bool;
5979 break;
5980 case gov_number:
5981 case gov_hidden_number:
5982 *type = &t_number;
5983 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005984 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005985 }
5986 else if (*name == '$')
5987 {
5988 *dest = dest_env;
5989 *type = &t_string;
5990 }
5991 else if (*name == '@')
5992 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005993 if (name[1] != '@'
5994 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005995 {
5996 emsg_invreg(name[1]);
5997 return FAIL;
5998 }
5999 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006000 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006001 }
6002 else if (STRNCMP(name, "g:", 2) == 0)
6003 {
6004 *dest = dest_global;
6005 }
6006 else if (STRNCMP(name, "b:", 2) == 0)
6007 {
6008 *dest = dest_buffer;
6009 }
6010 else if (STRNCMP(name, "w:", 2) == 0)
6011 {
6012 *dest = dest_window;
6013 }
6014 else if (STRNCMP(name, "t:", 2) == 0)
6015 {
6016 *dest = dest_tab;
6017 }
6018 else if (STRNCMP(name, "v:", 2) == 0)
6019 {
6020 typval_T *vtv;
6021 int di_flags;
6022
6023 *vimvaridx = find_vim_var(name + 2, &di_flags);
6024 if (*vimvaridx < 0)
6025 {
6026 semsg(_(e_variable_not_found_str), name);
6027 return FAIL;
6028 }
6029 // We use the current value of "sandbox" here, is that OK?
6030 if (var_check_ro(di_flags, name, FALSE))
6031 return FAIL;
6032 *dest = dest_vimvar;
6033 vtv = get_vim_var_tv(*vimvaridx);
6034 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6035 }
6036 return OK;
6037}
6038
6039/*
6040 * Generate a STORE instruction for "dest", not being "dest_local".
6041 * Return FAIL when out of memory.
6042 */
6043 static int
6044generate_store_var(
6045 cctx_T *cctx,
6046 assign_dest_T dest,
6047 int opt_flags,
6048 int vimvaridx,
6049 int scriptvar_idx,
6050 int scriptvar_sid,
6051 type_T *type,
6052 char_u *name)
6053{
6054 switch (dest)
6055 {
6056 case dest_option:
6057 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6058 opt_flags);
6059 case dest_global:
6060 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006061 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6062 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006063 case dest_buffer:
6064 // include b: with the name, easier to execute that way
6065 return generate_STORE(cctx, ISN_STOREB, 0, name);
6066 case dest_window:
6067 // include w: with the name, easier to execute that way
6068 return generate_STORE(cctx, ISN_STOREW, 0, name);
6069 case dest_tab:
6070 // include t: with the name, easier to execute that way
6071 return generate_STORE(cctx, ISN_STORET, 0, name);
6072 case dest_env:
6073 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6074 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006075 return generate_STORE(cctx, ISN_STOREREG,
6076 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006077 case dest_vimvar:
6078 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6079 case dest_script:
6080 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006081 // "s:" may be included in the name.
6082 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006083 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006084 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6085 scriptvar_sid, scriptvar_idx, type);
6086 case dest_local:
6087 case dest_expr:
6088 // cannot happen
6089 break;
6090 }
6091 return FAIL;
6092}
6093
Bram Moolenaar752fc692021-01-04 21:57:11 +01006094 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006095generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6096{
6097 if (lhs->lhs_dest != dest_local)
6098 return generate_store_var(cctx, lhs->lhs_dest,
6099 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6100 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6101 lhs->lhs_type, lhs->lhs_name);
6102
6103 if (lhs->lhs_lvar != NULL)
6104 {
6105 garray_T *instr = &cctx->ctx_instr;
6106 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6107
6108 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6109 // ISN_STORENR
6110 if (lhs->lhs_lvar->lv_from_outer == 0
6111 && instr->ga_len == instr_count + 1
6112 && isn->isn_type == ISN_PUSHNR)
6113 {
6114 varnumber_T val = isn->isn_arg.number;
6115 garray_T *stack = &cctx->ctx_type_stack;
6116
6117 isn->isn_type = ISN_STORENR;
6118 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6119 isn->isn_arg.storenr.stnr_val = val;
6120 if (stack->ga_len > 0)
6121 --stack->ga_len;
6122 }
6123 else if (lhs->lhs_lvar->lv_from_outer > 0)
6124 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6125 lhs->lhs_lvar->lv_from_outer);
6126 else
6127 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6128 }
6129 return OK;
6130}
6131
6132 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006133is_decl_command(int cmdidx)
6134{
6135 return cmdidx == CMD_let || cmdidx == CMD_var
6136 || cmdidx == CMD_final || cmdidx == CMD_const;
6137}
6138
6139/*
6140 * Figure out the LHS type and other properties for an assignment or one item
6141 * of ":unlet" with an index.
6142 * Returns OK or FAIL.
6143 */
6144 static int
6145compile_lhs(
6146 char_u *var_start,
6147 lhs_T *lhs,
6148 int cmdidx,
6149 int heredoc,
6150 int oplen,
6151 cctx_T *cctx)
6152{
6153 char_u *var_end;
6154 int is_decl = is_decl_command(cmdidx);
6155
6156 CLEAR_POINTER(lhs);
6157 lhs->lhs_dest = dest_local;
6158 lhs->lhs_vimvaridx = -1;
6159 lhs->lhs_scriptvar_idx = -1;
6160
6161 // "dest_end" is the end of the destination, including "[expr]" or
6162 // ".name".
6163 // "var_end" is the end of the variable/option/etc. name.
6164 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6165 if (*var_start == '@')
6166 var_end = var_start + 2;
6167 else
6168 {
6169 // skip over the leading "&", "&l:", "&g:" and "$"
6170 var_end = skip_option_env_lead(var_start);
6171 var_end = to_name_end(var_end, TRUE);
6172 }
6173
6174 // "a: type" is declaring variable "a" with a type, not dict "a:".
6175 if (is_decl && lhs->lhs_dest_end == var_start + 2
6176 && lhs->lhs_dest_end[-1] == ':')
6177 --lhs->lhs_dest_end;
6178 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6179 --var_end;
6180
6181 // compute the length of the destination without "[expr]" or ".name"
6182 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006183 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006184 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6185 if (lhs->lhs_name == NULL)
6186 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006187
6188 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6189 // Something follows after the variable: "var[idx]" or "var.key".
6190 lhs->lhs_has_index = TRUE;
6191
Bram Moolenaar752fc692021-01-04 21:57:11 +01006192 if (heredoc)
6193 lhs->lhs_type = &t_list_string;
6194 else
6195 lhs->lhs_type = &t_any;
6196
6197 if (cctx->ctx_skip != SKIP_YES)
6198 {
6199 int declare_error = FALSE;
6200
6201 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6202 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6203 &lhs->lhs_type, cctx) == FAIL)
6204 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006205 if (lhs->lhs_dest != dest_local
6206 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006207 {
6208 // Specific kind of variable recognized.
6209 declare_error = is_decl;
6210 }
6211 else
6212 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006213 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006214 if (check_reserved_name(lhs->lhs_name) == FAIL)
6215 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006216
6217 if (lookup_local(var_start, lhs->lhs_varlen,
6218 &lhs->lhs_local_lvar, cctx) == OK)
6219 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6220 else
6221 {
6222 CLEAR_FIELD(lhs->lhs_arg_lvar);
6223 if (arg_exists(var_start, lhs->lhs_varlen,
6224 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6225 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6226 {
6227 if (is_decl)
6228 {
6229 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6230 return FAIL;
6231 }
6232 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6233 }
6234 }
6235 if (lhs->lhs_lvar != NULL)
6236 {
6237 if (is_decl)
6238 {
6239 semsg(_(e_variable_already_declared), lhs->lhs_name);
6240 return FAIL;
6241 }
6242 }
6243 else
6244 {
6245 int script_namespace = lhs->lhs_varlen > 1
6246 && STRNCMP(var_start, "s:", 2) == 0;
6247 int script_var = (script_namespace
6248 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006249 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006250 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006251 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006252 imported_T *import =
6253 find_imported(var_start, lhs->lhs_varlen, cctx);
6254
6255 if (script_namespace || script_var || import != NULL)
6256 {
6257 char_u *rawname = lhs->lhs_name
6258 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6259
6260 if (is_decl)
6261 {
6262 if (script_namespace)
6263 semsg(_(e_cannot_declare_script_variable_in_function),
6264 lhs->lhs_name);
6265 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006266 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006267 lhs->lhs_name);
6268 return FAIL;
6269 }
6270 else if (cctx->ctx_ufunc->uf_script_ctx_version
6271 == SCRIPT_VERSION_VIM9
6272 && script_namespace
6273 && !script_var && import == NULL)
6274 {
6275 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6276 return FAIL;
6277 }
6278
6279 lhs->lhs_dest = dest_script;
6280
6281 // existing script-local variables should have a type
6282 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6283 if (import != NULL)
6284 lhs->lhs_scriptvar_sid = import->imp_sid;
6285 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6286 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006287 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006288 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006289 lhs->lhs_scriptvar_sid, rawname,
6290 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6291 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006292 if (lhs->lhs_scriptvar_idx >= 0)
6293 {
6294 scriptitem_T *si = SCRIPT_ITEM(
6295 lhs->lhs_scriptvar_sid);
6296 svar_T *sv =
6297 ((svar_T *)si->sn_var_vals.ga_data)
6298 + lhs->lhs_scriptvar_idx;
6299 lhs->lhs_type = sv->sv_type;
6300 }
6301 }
6302 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006303 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006304 == FAIL)
6305 return FAIL;
6306 }
6307 }
6308
6309 if (declare_error)
6310 {
6311 vim9_declare_error(lhs->lhs_name);
6312 return FAIL;
6313 }
6314 }
6315
6316 // handle "a:name" as a name, not index "name" on "a"
6317 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6318 var_end = lhs->lhs_dest_end;
6319
6320 if (lhs->lhs_dest != dest_option)
6321 {
6322 if (is_decl && *var_end == ':')
6323 {
6324 char_u *p;
6325
6326 // parse optional type: "let var: type = expr"
6327 if (!VIM_ISWHITE(var_end[1]))
6328 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006329 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006330 return FAIL;
6331 }
6332 p = skipwhite(var_end + 1);
6333 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6334 if (lhs->lhs_type == NULL)
6335 return FAIL;
6336 lhs->lhs_has_type = TRUE;
6337 }
6338 else if (lhs->lhs_lvar != NULL)
6339 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6340 }
6341
Bram Moolenaare42939a2021-04-05 17:11:17 +02006342 if (oplen == 3 && !heredoc
6343 && lhs->lhs_dest != dest_global
6344 && !lhs->lhs_has_index
6345 && lhs->lhs_type->tt_type != VAR_STRING
6346 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006347 {
6348 emsg(_(e_can_only_concatenate_to_string));
6349 return FAIL;
6350 }
6351
6352 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6353 && cctx->ctx_skip != SKIP_YES)
6354 {
6355 if (oplen > 1 && !heredoc)
6356 {
6357 // +=, /=, etc. require an existing variable
6358 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6359 return FAIL;
6360 }
6361 if (!is_decl)
6362 {
6363 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6364 return FAIL;
6365 }
6366
Bram Moolenaar3f327882021-03-17 20:56:38 +01006367 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006368 if ((lhs->lhs_type->tt_type == VAR_FUNC
6369 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006370 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006371 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006372
6373 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006374 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6375 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6376 if (lhs->lhs_lvar == NULL)
6377 return FAIL;
6378 lhs->lhs_new_local = TRUE;
6379 }
6380
6381 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006382 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006383 {
6384 // Something follows after the variable: "var[idx]" or "var.key".
6385 // TODO: should we also handle "->func()" here?
6386 if (is_decl)
6387 {
6388 emsg(_(e_cannot_use_index_when_declaring_variable));
6389 return FAIL;
6390 }
6391
6392 if (var_start[lhs->lhs_varlen] == '['
6393 || var_start[lhs->lhs_varlen] == '.')
6394 {
6395 char_u *after = var_start + lhs->lhs_varlen;
6396 char_u *p;
6397
6398 // Only the last index is used below, if there are others
6399 // before it generate code for the expression. Thus for
6400 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6401 for (;;)
6402 {
6403 p = skip_index(after);
6404 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006405 {
6406 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006407 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006408 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006409 after = p;
6410 }
6411 if (after > var_start + lhs->lhs_varlen)
6412 {
6413 lhs->lhs_varlen = after - var_start;
6414 lhs->lhs_dest = dest_expr;
6415 // We don't know the type before evaluating the expression,
6416 // use "any" until then.
6417 lhs->lhs_type = &t_any;
6418 }
6419
Bram Moolenaar752fc692021-01-04 21:57:11 +01006420 if (lhs->lhs_type->tt_member == NULL)
6421 lhs->lhs_member_type = &t_any;
6422 else
6423 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6424 }
6425 else
6426 {
6427 semsg("Not supported yet: %s", var_start);
6428 return FAIL;
6429 }
6430 }
6431 return OK;
6432}
6433
6434/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006435 * Figure out the LHS and check a few errors.
6436 */
6437 static int
6438compile_assign_lhs(
6439 char_u *var_start,
6440 lhs_T *lhs,
6441 int cmdidx,
6442 int is_decl,
6443 int heredoc,
6444 int oplen,
6445 cctx_T *cctx)
6446{
6447 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6448 return FAIL;
6449
6450 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6451 {
6452 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6453 return FAIL;
6454 }
6455 if (!is_decl && lhs->lhs_lvar != NULL
6456 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6457 {
6458 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6459 return FAIL;
6460 }
6461 return OK;
6462}
6463
6464/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006465 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6466 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006467 */
6468 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006469compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006470 char_u *var_start,
6471 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006472 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006473 cctx_T *cctx)
6474{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006475 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006476 char_u *p;
6477 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006478 int need_white_before = TRUE;
6479 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006480
Bram Moolenaar752fc692021-01-04 21:57:11 +01006481 p = var_start + varlen;
6482 if (*p == '[')
6483 {
6484 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006485 if (*p == ':')
6486 {
6487 // empty first index, push zero
6488 r = generate_PUSHNR(cctx, 0);
6489 need_white_before = FALSE;
6490 }
6491 else
6492 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006493
6494 if (r == OK && *skipwhite(p) == ':')
6495 {
6496 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006497 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006498 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006499 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006500 empty_second = *skipwhite(p + 1) == ']';
6501 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6502 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006503 {
6504 semsg(_(e_white_space_required_before_and_after_str_at_str),
6505 ":", p);
6506 return FAIL;
6507 }
6508 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006509 if (*p == ']')
6510 // empty second index, push "none"
6511 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6512 else
6513 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006514 }
6515
Bram Moolenaar752fc692021-01-04 21:57:11 +01006516 if (r == OK && *skipwhite(p) != ']')
6517 {
6518 // this should not happen
6519 emsg(_(e_missbrac));
6520 r = FAIL;
6521 }
6522 }
6523 else // if (*p == '.')
6524 {
6525 char_u *key_end = to_name_end(p + 1, TRUE);
6526 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6527
6528 r = generate_PUSHS(cctx, key);
6529 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006530 return r;
6531}
6532
6533/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006534 * For a LHS with an index, load the variable to be indexed.
6535 */
6536 static int
6537compile_load_lhs(
6538 lhs_T *lhs,
6539 char_u *var_start,
6540 type_T *rhs_type,
6541 cctx_T *cctx)
6542{
6543 if (lhs->lhs_dest == dest_expr)
6544 {
6545 size_t varlen = lhs->lhs_varlen;
6546 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006547 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006548 char_u *p = var_start;
6549 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006550 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006551
Bram Moolenaare97976b2021-08-01 13:17:17 +02006552 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6553 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006554 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006555 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6556 res = compile_expr0(&p, cctx);
6557 var_start[varlen] = c;
6558 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6559 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006560 {
6561 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006562 if (res != FAIL)
6563 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006564 return FAIL;
6565 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006566
6567 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6568 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6569 // now we can properly check the type
6570 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6571 && rhs_type != &t_void
6572 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6573 FALSE, FALSE) == FAIL)
6574 return FAIL;
6575 }
6576 else
6577 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6578 lhs->lhs_lvar, lhs->lhs_type);
6579 return OK;
6580}
6581
6582/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006583 * Produce code for loading "lhs" and also take care of an index.
6584 * Return OK/FAIL.
6585 */
6586 static int
6587compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6588{
6589 compile_load_lhs(lhs, var_start, NULL, cctx);
6590
6591 if (lhs->lhs_has_index)
6592 {
6593 int range = FALSE;
6594
6595 // Get member from list or dict. First compile the
6596 // index value.
6597 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6598 return FAIL;
6599 if (range)
6600 {
6601 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6602 var_start);
6603 return FAIL;
6604 }
6605
6606 // Get the member.
6607 if (compile_member(FALSE, cctx) == FAIL)
6608 return FAIL;
6609 }
6610 return OK;
6611}
6612
6613/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006614 * Assignment to a list or dict member, or ":unlet" for the item, using the
6615 * information in "lhs".
6616 * Returns OK or FAIL.
6617 */
6618 static int
6619compile_assign_unlet(
6620 char_u *var_start,
6621 lhs_T *lhs,
6622 int is_assign,
6623 type_T *rhs_type,
6624 cctx_T *cctx)
6625{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006626 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006627 garray_T *stack = &cctx->ctx_type_stack;
6628 int range = FALSE;
6629
Bram Moolenaar68452172021-04-12 21:21:02 +02006630 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006631 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006632 if (is_assign && range && lhs->lhs_type != &t_blob
6633 && lhs->lhs_type != &t_any)
6634 {
6635 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6636 return FAIL;
6637 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006638
6639 if (lhs->lhs_type == &t_any)
6640 {
6641 // Index on variable of unknown type: check at runtime.
6642 dest_type = VAR_ANY;
6643 }
6644 else
6645 {
6646 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006647 if (dest_type == VAR_DICT && range)
6648 {
6649 emsg(e_cannot_use_range_with_dictionary);
6650 return FAIL;
6651 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006652 if (dest_type == VAR_DICT
6653 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006654 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006655 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006656 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006657 type_T *type;
6658
6659 if (range)
6660 {
6661 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6662 if (need_type(type, &t_number,
6663 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006664 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006665 }
6666 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006667 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006668 && need_type(type, &t_number,
6669 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006670 return FAIL;
6671 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006672 }
6673
6674 // Load the dict or list. On the stack we then have:
6675 // - value (for assignment, not for :unlet)
6676 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006677 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006678 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006679 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6680 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006681
Bram Moolenaar68452172021-04-12 21:21:02 +02006682 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6683 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006684 {
6685 if (is_assign)
6686 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006687 if (range)
6688 {
6689 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6690 return FAIL;
6691 }
6692 else
6693 {
6694 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006695
Bram Moolenaar68452172021-04-12 21:21:02 +02006696 if (isn == NULL)
6697 return FAIL;
6698 isn->isn_arg.vartype = dest_type;
6699 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006700 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006701 else if (range)
6702 {
6703 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6704 return FAIL;
6705 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006706 else
6707 {
6708 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6709 return FAIL;
6710 }
6711 }
6712 else
6713 {
6714 emsg(_(e_indexable_type_required));
6715 return FAIL;
6716 }
6717
6718 return OK;
6719}
6720
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006721/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006722 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006723 * "let name"
6724 * "var name = expr"
6725 * "final name = expr"
6726 * "const name = expr"
6727 * "name = expr"
6728 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006729 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006730 * Return NULL for an error.
6731 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732 */
6733 static char_u *
6734compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6735{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006736 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006738 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739 char_u *ret = NULL;
6740 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006741 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006742 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006743 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006745 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747 int oplen = 0;
6748 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006749 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006750 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006752 int is_decl = is_decl_command(cmdidx);
6753 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006754 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006755
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006756 // Skip over the "var" or "[var, var]" to get to any "=".
6757 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6758 if (p == NULL)
6759 return *arg == '[' ? arg : NULL;
6760
6761 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006763 // TODO: should we allow this, and figure out type inference from list
6764 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006765 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006766 return NULL;
6767 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006768 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770 sp = p;
6771 p = skipwhite(p);
6772 op = p;
6773 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006774
6775 if (var_count > 0 && oplen == 0)
6776 // can be something like "[1, 2]->func()"
6777 return arg;
6778
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006779 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006781 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006782 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006783 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006784 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6785 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006786 if (VIM_ISWHITE(eap->cmd[2]))
6787 {
6788 semsg(_(e_no_white_space_allowed_after_str_str),
6789 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6790 return NULL;
6791 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006792 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6793 oplen = 2;
6794 incdec = TRUE;
6795 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006796
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797 if (heredoc)
6798 {
6799 list_T *l;
6800 listitem_T *li;
6801
6802 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006803 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006805 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006806 if (l == NULL)
6807 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808
Bram Moolenaar078269b2020-09-21 20:35:55 +02006809 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006811 // Push each line and the create the list.
6812 FOR_ALL_LIST_ITEMS(l, li)
6813 {
6814 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6815 li->li_tv.vval.v_string = NULL;
6816 }
6817 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006818 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819 list_free(l);
6820 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006821 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006822 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006823 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006825 char_u *wp;
6826
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006827 // for "[var, var] = expr" evaluate the expression here, loop over the
6828 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006829 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006830
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006831 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006832 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006833 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006834 if (compile_expr0(&p, cctx) == FAIL)
6835 return NULL;
6836 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006837
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006838 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006840 type_T *stacktype;
6841
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006842 stacktype = stack->ga_len == 0 ? &t_void
6843 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006844 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006846 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006847 goto theend;
6848 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006849 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006850 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006851 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006852 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006853 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6854 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006855 if (stacktype->tt_member != NULL)
6856 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006857 }
6858 }
6859
6860 /*
6861 * Loop over variables in "[var, var] = expr".
6862 * For "var = expr" and "let var: type" this is done only once.
6863 */
6864 if (var_count > 0)
6865 var_start = skipwhite(arg + 1); // skip over the "["
6866 else
6867 var_start = arg;
6868 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6869 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006870 int instr_count = -1;
6871 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006872
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006873 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6874 {
6875 // Ignore underscore in "[a, _, b] = list".
6876 if (var_count > 0)
6877 {
6878 var_start = skipwhite(var_start + 2);
6879 continue;
6880 }
6881 emsg(_(e_cannot_use_underscore_here));
6882 goto theend;
6883 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006884 vim_free(lhs.lhs_name);
6885
6886 /*
6887 * Figure out the LHS type and other properties.
6888 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006889 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6890 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006891 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02006892 if (heredoc)
6893 {
6894 SOURCING_LNUM = start_lnum;
6895 if (lhs.lhs_has_type
6896 && need_type(&t_list_string, lhs.lhs_type,
6897 -1, 0, cctx, FALSE, FALSE) == FAIL)
6898 goto theend;
6899 }
6900 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006901 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006902 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006903 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006904 if (oplen > 0 && var_count == 0)
6905 {
6906 // skip over the "=" and the expression
6907 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006908 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006909 }
6910 }
6911 else if (oplen > 0)
6912 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006913 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006914 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006915
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006916 // for "+=", "*=", "..=" etc. first load the current value
6917 if (*op != '='
6918 && compile_load_lhs_with_index(&lhs, var_start,
6919 cctx) == FAIL)
6920 goto theend;
6921
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006922 // For "var = expr" evaluate the expression.
6923 if (var_count == 0)
6924 {
6925 int r;
6926
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006927 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006928 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006929 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006930 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006931 r = generate_PUSHNR(cctx, 1);
6932 }
6933 else
6934 {
6935 // Temporarily hide the new local variable here, it is
6936 // not available to this expression.
6937 if (lhs.lhs_new_local)
6938 --cctx->ctx_locals.ga_len;
6939 wp = op + oplen;
6940 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6941 {
6942 if (lhs.lhs_new_local)
6943 ++cctx->ctx_locals.ga_len;
6944 goto theend;
6945 }
6946 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006947 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006948 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006949 if (r == FAIL)
6950 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006951 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006952 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006953 else if (semicolon && var_idx == var_count - 1)
6954 {
6955 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006956 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006957 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6958 goto theend;
6959 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006960 else
6961 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006962 // For "[var, var] = expr" get the "var_idx" item from the
6963 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006964 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006965 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006966 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006967
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006968 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006969 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006970 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006971 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006972 if ((rhs_type->tt_type == VAR_FUNC
6973 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006974 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006975 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006976 goto theend;
6977
Bram Moolenaar752fc692021-01-04 21:57:11 +01006978 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006979 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006980 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006981 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006982 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006983 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006984 }
6985 else
6986 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006987 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006988 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006989 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006990 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006991 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006992 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006993 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006994 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006995 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006996 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006997 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006998 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006999 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007000 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007001 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007002
Bram Moolenaar77709b12021-04-03 21:01:01 +02007003 // Without operator check type here, otherwise below.
7004 // Use the line number of the assignment.
7005 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007006 if (lhs.lhs_has_index)
7007 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01007008 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007009 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007010 goto theend;
7011 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007012 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007013 else
7014 {
7015 type_T *lhs_type = lhs.lhs_member_type;
7016
7017 // Special case: assigning to @# can use a number or a
7018 // string.
7019 if (lhs_type == &t_number_or_string
7020 && rhs_type->tt_type == VAR_NUMBER)
7021 lhs_type = &t_number;
7022 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007023 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007024 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007027 else if (cmdidx == CMD_final)
7028 {
7029 emsg(_(e_final_requires_a_value));
7030 goto theend;
7031 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007032 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007034 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007035 goto theend;
7036 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007037 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007038 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007039 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007040 goto theend;
7041 }
7042 else
7043 {
7044 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007045 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007046 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007047 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007048 {
7049 case VAR_BOOL:
7050 generate_PUSHBOOL(cctx, VVAL_FALSE);
7051 break;
7052 case VAR_FLOAT:
7053#ifdef FEAT_FLOAT
7054 generate_PUSHF(cctx, 0.0);
7055#endif
7056 break;
7057 case VAR_STRING:
7058 generate_PUSHS(cctx, NULL);
7059 break;
7060 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007061 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007062 break;
7063 case VAR_FUNC:
7064 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7065 break;
7066 case VAR_LIST:
7067 generate_NEWLIST(cctx, 0);
7068 break;
7069 case VAR_DICT:
7070 generate_NEWDICT(cctx, 0);
7071 break;
7072 case VAR_JOB:
7073 generate_PUSHJOB(cctx, NULL);
7074 break;
7075 case VAR_CHANNEL:
7076 generate_PUSHCHANNEL(cctx, NULL);
7077 break;
7078 case VAR_NUMBER:
7079 case VAR_UNKNOWN:
7080 case VAR_ANY:
7081 case VAR_PARTIAL:
7082 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007083 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007084 case VAR_SPECIAL: // cannot happen
7085 generate_PUSHNR(cctx, 0);
7086 break;
7087 }
7088 }
7089 if (var_count == 0)
7090 end = p;
7091 }
7092
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007093 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007094 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007095 break;
7096
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007097 if (oplen > 0 && *op != '=')
7098 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007099 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007100 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007101
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007102 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007103 {
7104 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7105 goto theend;
7106 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007107 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007108 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007109 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007110 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7111 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007112#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007113 // If variable is float operation with number is OK.
7114 !(expected == &t_float && stacktype == &t_number) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007115#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007116 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007117 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007118 goto theend;
7119 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007120
7121 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007122 {
7123 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7124 goto theend;
7125 }
7126 else if (*op == '+')
7127 {
7128 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007129 operator_type(lhs.lhs_member_type, stacktype),
7130 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007131 goto theend;
7132 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007133 else if (generate_two_op(cctx, op) == FAIL)
7134 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007137 // Use the line number of the assignment for store instruction.
7138 save_lnum = cctx->ctx_lnum;
7139 cctx->ctx_lnum = start_lnum - 1;
7140
Bram Moolenaar752fc692021-01-04 21:57:11 +01007141 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007142 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007143 // Use the info in "lhs" to store the value at the index in the
7144 // list or dict.
7145 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7146 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007147 {
7148 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007149 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007150 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007151 }
7152 else
7153 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007154 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007155 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007156 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007157 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007158 generate_LOCKCONST(cctx);
7159
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007160 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007161 && (lhs.lhs_type->tt_type == VAR_DICT
7162 || lhs.lhs_type->tt_type == VAR_LIST)
7163 && lhs.lhs_type->tt_member != NULL
7164 && lhs.lhs_type->tt_member != &t_any
7165 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007166 // Set the type in the list or dict, so that it can be checked,
7167 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007168 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007169
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007170 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007171 {
7172 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007173 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007174 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007175 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007176 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007177
7178 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007179 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007181
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007182 // For "[var, var] = expr" drop the "expr" value.
7183 // Also for "[var, var; _] = expr".
7184 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007185 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007186 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007187 goto theend;
7188 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007189
Bram Moolenaarb2097502020-07-19 17:17:02 +02007190 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007191
7192theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007193 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194 return ret;
7195}
7196
7197/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007198 * Check for an assignment at "eap->cmd", compile it if found.
7199 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7200 */
7201 static int
7202may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7203{
7204 char_u *pskip;
7205 char_u *p;
7206
7207 // Assuming the command starts with a variable or function name,
7208 // find what follows.
7209 // Skip over "var.member", "var[idx]" and the like.
7210 // Also "&opt = val", "$ENV = val" and "@r = val".
7211 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7212 ? eap->cmd + 1 : eap->cmd;
7213 p = to_name_end(pskip, TRUE);
7214 if (p > eap->cmd && *p != NUL)
7215 {
7216 char_u *var_end;
7217 int oplen;
7218 int heredoc;
7219
7220 if (eap->cmd[0] == '@')
7221 var_end = eap->cmd + 2;
7222 else
7223 var_end = find_name_end(pskip, NULL, NULL,
7224 FNE_CHECK_START | FNE_INCL_BR);
7225 oplen = assignment_len(skipwhite(var_end), &heredoc);
7226 if (oplen > 0)
7227 {
7228 size_t len = p - eap->cmd;
7229
7230 // Recognize an assignment if we recognize the variable
7231 // name:
7232 // "g:var = expr"
7233 // "local = expr" where "local" is a local var.
7234 // "script = expr" where "script" is a script-local var.
7235 // "import = expr" where "import" is an imported var
7236 // "&opt = expr"
7237 // "$ENV = expr"
7238 // "@r = expr"
7239 if (*eap->cmd == '&'
7240 || *eap->cmd == '$'
7241 || *eap->cmd == '@'
7242 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007243 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007244 {
7245 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7246 if (*line == NULL || *line == eap->cmd)
7247 return FAIL;
7248 return OK;
7249 }
7250 }
7251 }
7252
7253 if (*eap->cmd == '[')
7254 {
7255 // [var, var] = expr
7256 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7257 if (*line == NULL)
7258 return FAIL;
7259 if (*line != eap->cmd)
7260 return OK;
7261 }
7262 return NOTDONE;
7263}
7264
7265/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007266 * Check if "name" can be "unlet".
7267 */
7268 int
7269check_vim9_unlet(char_u *name)
7270{
7271 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7272 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007273 // "unlet s:var" is allowed in legacy script.
7274 if (*name == 's' && !script_is_vim9())
7275 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007276 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007277 return FAIL;
7278 }
7279 return OK;
7280}
7281
7282/*
7283 * Callback passed to ex_unletlock().
7284 */
7285 static int
7286compile_unlet(
7287 lval_T *lvp,
7288 char_u *name_end,
7289 exarg_T *eap,
7290 int deep UNUSED,
7291 void *coookie)
7292{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007293 cctx_T *cctx = coookie;
7294 char_u *p = lvp->ll_name;
7295 int cc = *name_end;
7296 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007297
Bram Moolenaar752fc692021-01-04 21:57:11 +01007298 if (cctx->ctx_skip == SKIP_YES)
7299 return OK;
7300
7301 *name_end = NUL;
7302 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007303 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007304 // :unlet $ENV_VAR
7305 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7306 }
7307 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7308 {
7309 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007310
Bram Moolenaar752fc692021-01-04 21:57:11 +01007311 // This is similar to assigning: lookup the list/dict, compile the
7312 // idx/key. Then instead of storing the value unlet the item.
7313 // unlet {list}[idx]
7314 // unlet {dict}[key] dict.key
7315 //
7316 // Figure out the LHS type and other properties.
7317 //
7318 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7319
7320 // : unlet an indexed item
7321 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007322 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007323 iemsg("called compile_lhs() without an index");
7324 ret = FAIL;
7325 }
7326 else
7327 {
7328 // Use the info in "lhs" to unlet the item at the index in the
7329 // list or dict.
7330 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007331 }
7332
Bram Moolenaar752fc692021-01-04 21:57:11 +01007333 vim_free(lhs.lhs_name);
7334 }
7335 else if (check_vim9_unlet(p) == FAIL)
7336 {
7337 ret = FAIL;
7338 }
7339 else
7340 {
7341 // Normal name. Only supports g:, w:, t: and b: namespaces.
7342 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007343 }
7344
Bram Moolenaar752fc692021-01-04 21:57:11 +01007345 *name_end = cc;
7346 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007347}
7348
7349/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007350 * Callback passed to ex_unletlock().
7351 */
7352 static int
7353compile_lock_unlock(
7354 lval_T *lvp,
7355 char_u *name_end,
7356 exarg_T *eap,
7357 int deep UNUSED,
7358 void *coookie)
7359{
7360 cctx_T *cctx = coookie;
7361 int cc = *name_end;
7362 char_u *p = lvp->ll_name;
7363 int ret = OK;
7364 size_t len;
7365 char_u *buf;
7366
7367 if (cctx->ctx_skip == SKIP_YES)
7368 return OK;
7369
7370 // Cannot use :lockvar and :unlockvar on local variables.
7371 if (p[1] != ':')
7372 {
7373 char_u *end = skip_var_one(p, FALSE);
7374
7375 if (lookup_local(p, end - p, NULL, cctx) == OK)
7376 {
7377 emsg(_(e_cannot_lock_unlock_local_variable));
7378 return FAIL;
7379 }
7380 }
7381
7382 // Checking is done at runtime.
7383 *name_end = NUL;
7384 len = name_end - p + 20;
7385 buf = alloc(len);
7386 if (buf == NULL)
7387 ret = FAIL;
7388 else
7389 {
7390 vim_snprintf((char *)buf, len, "%s %s",
7391 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7392 p);
7393 ret = generate_EXEC(cctx, buf);
7394
7395 vim_free(buf);
7396 *name_end = cc;
7397 }
7398 return ret;
7399}
7400
7401/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007402 * compile "unlet var", "lock var" and "unlock var"
7403 * "arg" points to "var".
7404 */
7405 static char_u *
7406compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7407{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007408 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7409 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7410 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007411 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7412}
7413
7414/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007415 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7416 */
7417 static int
7418compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7419{
7420 garray_T *instr = &cctx->ctx_instr;
7421 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7422
7423 if (endlabel == NULL)
7424 return FAIL;
7425 endlabel->el_next = *el;
7426 *el = endlabel;
7427 endlabel->el_end_label = instr->ga_len;
7428
7429 generate_JUMP(cctx, when, 0);
7430 return OK;
7431}
7432
7433 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007434compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435{
7436 garray_T *instr = &cctx->ctx_instr;
7437
7438 while (*el != NULL)
7439 {
7440 endlabel_T *cur = (*el);
7441 isn_T *isn;
7442
7443 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007444 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445 *el = cur->el_next;
7446 vim_free(cur);
7447 }
7448}
7449
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007450 static void
7451compile_free_jump_to_end(endlabel_T **el)
7452{
7453 while (*el != NULL)
7454 {
7455 endlabel_T *cur = (*el);
7456
7457 *el = cur->el_next;
7458 vim_free(cur);
7459 }
7460}
7461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462/*
7463 * Create a new scope and set up the generic items.
7464 */
7465 static scope_T *
7466new_scope(cctx_T *cctx, scopetype_T type)
7467{
7468 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7469
7470 if (scope == NULL)
7471 return NULL;
7472 scope->se_outer = cctx->ctx_scope;
7473 cctx->ctx_scope = scope;
7474 scope->se_type = type;
7475 scope->se_local_count = cctx->ctx_locals.ga_len;
7476 return scope;
7477}
7478
7479/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007480 * Free the current scope and go back to the outer scope.
7481 */
7482 static void
7483drop_scope(cctx_T *cctx)
7484{
7485 scope_T *scope = cctx->ctx_scope;
7486
7487 if (scope == NULL)
7488 {
7489 iemsg("calling drop_scope() without a scope");
7490 return;
7491 }
7492 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007493 switch (scope->se_type)
7494 {
7495 case IF_SCOPE:
7496 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7497 case FOR_SCOPE:
7498 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7499 case WHILE_SCOPE:
7500 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7501 case TRY_SCOPE:
7502 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7503 case NO_SCOPE:
7504 case BLOCK_SCOPE:
7505 break;
7506 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007507 vim_free(scope);
7508}
7509
7510/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511 * compile "if expr"
7512 *
7513 * "if expr" Produces instructions:
7514 * EVAL expr Push result of "expr"
7515 * JUMP_IF_FALSE end
7516 * ... body ...
7517 * end:
7518 *
7519 * "if expr | else" Produces instructions:
7520 * EVAL expr Push result of "expr"
7521 * JUMP_IF_FALSE else
7522 * ... body ...
7523 * JUMP_ALWAYS end
7524 * else:
7525 * ... body ...
7526 * end:
7527 *
7528 * "if expr1 | elseif expr2 | else" Produces instructions:
7529 * EVAL expr Push result of "expr"
7530 * JUMP_IF_FALSE elseif
7531 * ... body ...
7532 * JUMP_ALWAYS end
7533 * elseif:
7534 * EVAL expr Push result of "expr"
7535 * JUMP_IF_FALSE else
7536 * ... body ...
7537 * JUMP_ALWAYS end
7538 * else:
7539 * ... body ...
7540 * end:
7541 */
7542 static char_u *
7543compile_if(char_u *arg, cctx_T *cctx)
7544{
7545 char_u *p = arg;
7546 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007547 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007549 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007550 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551
Bram Moolenaara5565e42020-05-09 15:44:01 +02007552 CLEAR_FIELD(ppconst);
7553 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007554 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007555 clear_ppconst(&ppconst);
7556 return NULL;
7557 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007558 if (!ends_excmd2(arg, skipwhite(p)))
7559 {
7560 semsg(_(e_trailing_arg), p);
7561 return NULL;
7562 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007563 if (cctx->ctx_skip == SKIP_YES)
7564 clear_ppconst(&ppconst);
7565 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007566 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007567 int error = FALSE;
7568 int v;
7569
Bram Moolenaara5565e42020-05-09 15:44:01 +02007570 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007571 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007572 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007573 if (error)
7574 return NULL;
7575 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007576 }
7577 else
7578 {
7579 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007580 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007581 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007582 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007583 if (bool_on_stack(cctx) == FAIL)
7584 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007585 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007586
Bram Moolenaara91a7132021-03-25 21:12:15 +01007587 // CMDMOD_REV must come before the jump
7588 generate_undo_cmdmods(cctx);
7589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007590 scope = new_scope(cctx, IF_SCOPE);
7591 if (scope == NULL)
7592 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007593 scope->se_skip_save = skip_save;
7594 // "is_had_return" will be reset if any block does not end in :return
7595 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007596
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007597 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007598 {
7599 // "where" is set when ":elseif", "else" or ":endif" is found
7600 scope->se_u.se_if.is_if_label = instr->ga_len;
7601 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7602 }
7603 else
7604 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007605
Bram Moolenaarced68a02021-01-24 17:53:47 +01007606#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007607 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007608 && skip_save != SKIP_YES)
7609 {
7610 // generated a profile start, need to generate a profile end, since it
7611 // won't be done after returning
7612 cctx->ctx_skip = SKIP_NOT;
7613 generate_instr(cctx, ISN_PROF_END);
7614 cctx->ctx_skip = SKIP_YES;
7615 }
7616#endif
7617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007618 return p;
7619}
7620
7621 static char_u *
7622compile_elseif(char_u *arg, cctx_T *cctx)
7623{
7624 char_u *p = arg;
7625 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007626 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627 isn_T *isn;
7628 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007629 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007630 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631
7632 if (scope == NULL || scope->se_type != IF_SCOPE)
7633 {
7634 emsg(_(e_elseif_without_if));
7635 return NULL;
7636 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007637 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007638 if (!cctx->ctx_had_return)
7639 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640
Bram Moolenaarced68a02021-01-24 17:53:47 +01007641 if (cctx->ctx_skip == SKIP_NOT)
7642 {
7643 // previous block was executed, this one and following will not
7644 cctx->ctx_skip = SKIP_YES;
7645 scope->se_u.se_if.is_seen_skip_not = TRUE;
7646 }
7647 if (scope->se_u.se_if.is_seen_skip_not)
7648 {
7649 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007650 // Do not count the "elseif" for profiling and cmdmod
7651 instr->ga_len = current_instr_idx(cctx);
7652
Bram Moolenaarced68a02021-01-24 17:53:47 +01007653 skip_expr_cctx(&p, cctx);
7654 return p;
7655 }
7656
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007657 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007658 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007659 int moved_cmdmod = FALSE;
7660
7661 // Move any CMDMOD instruction to after the jump
7662 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7663 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007664 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007665 return NULL;
7666 ((isn_T *)instr->ga_data)[instr->ga_len] =
7667 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7668 --instr->ga_len;
7669 moved_cmdmod = TRUE;
7670 }
7671
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007672 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007673 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007674 return NULL;
7675 // previous "if" or "elseif" jumps here
7676 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7677 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007678 if (moved_cmdmod)
7679 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007682 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007683 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007684 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007685 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007686 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007687#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007688 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007689 {
7690 // the previous block was skipped, need to profile this line
7691 generate_instr(cctx, ISN_PROF_START);
7692 instr_count = instr->ga_len;
7693 }
7694#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007695 if (cctx->ctx_compile_type == CT_DEBUG)
7696 {
7697 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007698 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007699 instr_count = instr->ga_len;
7700 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007701 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007702 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007703 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007704 clear_ppconst(&ppconst);
7705 return NULL;
7706 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007707 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007708 if (!ends_excmd2(arg, skipwhite(p)))
7709 {
7710 semsg(_(e_trailing_arg), p);
7711 return NULL;
7712 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007713 if (scope->se_skip_save == SKIP_YES)
7714 clear_ppconst(&ppconst);
7715 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007716 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007717 int error = FALSE;
7718 int v;
7719
Bram Moolenaar7f141552020-05-09 17:35:53 +02007720 // The expression results in a constant.
7721 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007722 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7723 if (error)
7724 return NULL;
7725 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007726 clear_ppconst(&ppconst);
7727 scope->se_u.se_if.is_if_label = -1;
7728 }
7729 else
7730 {
7731 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007732 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007733 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007734 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007735 if (bool_on_stack(cctx) == FAIL)
7736 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737
Bram Moolenaara91a7132021-03-25 21:12:15 +01007738 // CMDMOD_REV must come before the jump
7739 generate_undo_cmdmods(cctx);
7740
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007741 // "where" is set when ":elseif", "else" or ":endif" is found
7742 scope->se_u.se_if.is_if_label = instr->ga_len;
7743 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745
7746 return p;
7747}
7748
7749 static char_u *
7750compile_else(char_u *arg, cctx_T *cctx)
7751{
7752 char_u *p = arg;
7753 garray_T *instr = &cctx->ctx_instr;
7754 isn_T *isn;
7755 scope_T *scope = cctx->ctx_scope;
7756
7757 if (scope == NULL || scope->se_type != IF_SCOPE)
7758 {
7759 emsg(_(e_else_without_if));
7760 return NULL;
7761 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007762 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007763 if (!cctx->ctx_had_return)
7764 scope->se_u.se_if.is_had_return = FALSE;
7765 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007766
Bram Moolenaarced68a02021-01-24 17:53:47 +01007767#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007768 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007769 {
7770 if (cctx->ctx_skip == SKIP_NOT
7771 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7772 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007773 // the previous block was executed, do not count "else" for
7774 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007775 --instr->ga_len;
7776 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7777 {
7778 // the previous block was not executed, this one will, do count the
7779 // "else" for profiling
7780 cctx->ctx_skip = SKIP_NOT;
7781 generate_instr(cctx, ISN_PROF_END);
7782 generate_instr(cctx, ISN_PROF_START);
7783 cctx->ctx_skip = SKIP_YES;
7784 }
7785 }
7786#endif
7787
7788 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007789 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007790 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007791 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007792 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007793 if (!cctx->ctx_had_return
7794 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7795 JUMP_ALWAYS, cctx) == FAIL)
7796 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007797 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007798
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007799 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007800 {
7801 if (scope->se_u.se_if.is_if_label >= 0)
7802 {
7803 // previous "if" or "elseif" jumps here
7804 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7805 isn->isn_arg.jump.jump_where = instr->ga_len;
7806 scope->se_u.se_if.is_if_label = -1;
7807 }
7808 }
7809
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007810 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007811 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7812 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007813
7814 return p;
7815}
7816
7817 static char_u *
7818compile_endif(char_u *arg, cctx_T *cctx)
7819{
7820 scope_T *scope = cctx->ctx_scope;
7821 ifscope_T *ifscope;
7822 garray_T *instr = &cctx->ctx_instr;
7823 isn_T *isn;
7824
Bram Moolenaarfa984412021-03-25 22:15:28 +01007825 if (misplaced_cmdmod(cctx))
7826 return NULL;
7827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007828 if (scope == NULL || scope->se_type != IF_SCOPE)
7829 {
7830 emsg(_(e_endif_without_if));
7831 return NULL;
7832 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007833 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007834 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007835 if (!cctx->ctx_had_return)
7836 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007837
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007838 if (scope->se_u.se_if.is_if_label >= 0)
7839 {
7840 // previous "if" or "elseif" jumps here
7841 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7842 isn->isn_arg.jump.jump_where = instr->ga_len;
7843 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007844 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007845 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007846
7847#ifdef FEAT_PROFILE
7848 // even when skipping we count the endif as executed, unless the block it's
7849 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007850 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007851 && scope->se_skip_save != SKIP_YES)
7852 {
7853 cctx->ctx_skip = SKIP_NOT;
7854 generate_instr(cctx, ISN_PROF_START);
7855 }
7856#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007857 cctx->ctx_skip = scope->se_skip_save;
7858
7859 // If all the blocks end in :return and there is an :else then the
7860 // had_return flag is set.
7861 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007863 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007864 return arg;
7865}
7866
7867/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007868 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007869 *
7870 * Produces instructions:
7871 * PUSHNR -1
7872 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007873 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007874 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7875 * - if beyond end, jump to "end"
7876 * - otherwise get item from list and push it
7877 * STORE var Store item in "var"
7878 * ... body ...
7879 * JUMP top Jump back to repeat
7880 * end: DROP Drop the result of "expr"
7881 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007882 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7883 * UNPACK 2 Split item in 2
7884 * STORE var1 Store item in "var1"
7885 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007886 */
7887 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007888compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007889{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007890 char_u *arg;
7891 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007892 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007893 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007894 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007895 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007896 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007897 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007898 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007899 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007900 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007901 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007902 lvar_T *loop_lvar; // loop iteration variable
7903 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007904 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007905 type_T *item_type = &t_any;
7906 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007907 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908
Bram Moolenaar792f7862020-11-23 08:31:18 +01007909 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007910 if (p == NULL)
7911 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007912 if (var_count == 0)
7913 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007914 else
7915 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007916
7917 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007918 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007919 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7920 return NULL;
7921 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007922 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007923 if (*p == ':' && wp != p)
7924 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7925 else
7926 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007927 return NULL;
7928 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007929 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007930 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7931 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007933 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7934 // instruction.
7935 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7936 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7937 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007938 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007939 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007940 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7941 .isn_arg.debug.dbg_break_lnum;
7942 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007944 scope = new_scope(cctx, FOR_SCOPE);
7945 if (scope == NULL)
7946 return NULL;
7947
Bram Moolenaar792f7862020-11-23 08:31:18 +01007948 // Reserve a variable to store the loop iteration counter and initialize it
7949 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007950 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7951 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007952 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007953 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007954 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007955 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007956 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007957 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007958
7959 // compile "expr", it remains on the stack until "endfor"
7960 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007961 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007962 {
7963 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007964 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007965 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007966 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007967
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007968 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007969 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007970 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007971 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007972 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007973 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007974 semsg(_(e_for_loop_on_str_not_supported),
7975 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007976 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007977 return NULL;
7978 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007979
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007980 if (vartype->tt_type == VAR_STRING)
7981 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007982 else if (vartype->tt_type == VAR_BLOB)
7983 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007984 else if (vartype->tt_type == VAR_LIST
7985 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007986 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007987 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007988 item_type = vartype->tt_member;
7989 else if (vartype->tt_member->tt_type == VAR_LIST
7990 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007991 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007992 item_type = vartype->tt_member->tt_member;
7993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007994
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007995 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007996 generate_undo_cmdmods(cctx);
7997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007998 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007999 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008000
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008001 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008002
Bram Moolenaar792f7862020-11-23 08:31:18 +01008003 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008004 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008005 {
8006 generate_UNPACK(cctx, var_count, semicolon);
8007 arg = skipwhite(arg + 1); // skip white after '['
8008
8009 // the list item is replaced by a number of items
Bram Moolenaar35578162021-08-02 19:10:38 +02008010 if (GA_GROW_FAILS(stack, var_count - 1))
Bram Moolenaar792f7862020-11-23 08:31:18 +01008011 {
8012 drop_scope(cctx);
8013 return NULL;
8014 }
8015 --stack->ga_len;
8016 for (idx = 0; idx < var_count; ++idx)
8017 {
8018 ((type_T **)stack->ga_data)[stack->ga_len] =
8019 (semicolon && idx == 0) ? vartype : item_type;
8020 ++stack->ga_len;
8021 }
8022 }
8023
8024 for (idx = 0; idx < var_count; ++idx)
8025 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008026 assign_dest_T dest = dest_local;
8027 int opt_flags = 0;
8028 int vimvaridx = -1;
8029 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008030 type_T *lhs_type = &t_any;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02008031 where_T where = WHERE_INIT;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008032
8033 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008034 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008035 name = vim_strnsave(arg, varlen);
8036 if (name == NULL)
8037 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008038 if (*p == ':')
8039 {
8040 p = skipwhite(p + 1);
8041 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8042 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008043
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008044 // TODO: script var not supported?
8045 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
8046 &vimvaridx, &type, cctx) == FAIL)
8047 goto failed;
8048 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008049 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008050 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
8051 0, 0, type, name) == FAIL)
8052 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008053 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02008054 else if (varlen == 1 && *arg == '_')
8055 {
8056 // Assigning to "_": drop the value.
8057 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8058 goto failed;
8059 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008060 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008061 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01008062 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008063 {
8064 semsg(_(e_variable_already_declared), arg);
8065 goto failed;
8066 }
8067
Bram Moolenaarea870692020-12-02 14:24:30 +01008068 if (STRNCMP(name, "s:", 2) == 0)
8069 {
8070 semsg(_(e_cannot_declare_script_variable_in_function), name);
8071 goto failed;
8072 }
8073
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008074 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02008075 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008076 where.wt_variable = TRUE;
8077 if (lhs_type == &t_any)
8078 lhs_type = item_type;
8079 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02008080 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02008081 ? need_type(item_type, lhs_type,
8082 -1, 0, cctx, FALSE, FALSE)
8083 : check_type(lhs_type, item_type, TRUE, where))
8084 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008085 goto failed;
8086 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008087 if (var_lvar == NULL)
8088 // out of memory or used as an argument
8089 goto failed;
8090
8091 if (semicolon && idx == var_count - 1)
8092 var_lvar->lv_type = vartype;
8093 else
8094 var_lvar->lv_type = item_type;
8095 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8096 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008097
8098 if (*p == ',' || *p == ';')
8099 ++p;
8100 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008101 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008102 }
8103
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008104 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008105 {
8106 int save_prev_lnum = cctx->ctx_prev_lnum;
8107
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008108 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008109 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8110 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008111 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008112 cctx->ctx_prev_lnum = save_prev_lnum;
8113 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008114
Bram Moolenaar792f7862020-11-23 08:31:18 +01008115 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008116
8117failed:
8118 vim_free(name);
8119 drop_scope(cctx);
8120 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008121}
8122
8123/*
8124 * compile "endfor"
8125 */
8126 static char_u *
8127compile_endfor(char_u *arg, cctx_T *cctx)
8128{
8129 garray_T *instr = &cctx->ctx_instr;
8130 scope_T *scope = cctx->ctx_scope;
8131 forscope_T *forscope;
8132 isn_T *isn;
8133
Bram Moolenaarfa984412021-03-25 22:15:28 +01008134 if (misplaced_cmdmod(cctx))
8135 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008137 if (scope == NULL || scope->se_type != FOR_SCOPE)
8138 {
8139 emsg(_(e_for));
8140 return NULL;
8141 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008142 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008143 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008144 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008145
8146 // At end of ":for" scope jump back to the FOR instruction.
8147 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8148
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008149 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008150 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8151 isn->isn_arg.forloop.for_end = instr->ga_len;
8152
8153 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008154 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155
8156 // Below the ":for" scope drop the "expr" list from the stack.
8157 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8158 return NULL;
8159
8160 vim_free(scope);
8161
8162 return arg;
8163}
8164
8165/*
8166 * compile "while expr"
8167 *
8168 * Produces instructions:
8169 * top: EVAL expr Push result of "expr"
8170 * JUMP_IF_FALSE end jump if false
8171 * ... body ...
8172 * JUMP top Jump back to repeat
8173 * end:
8174 *
8175 */
8176 static char_u *
8177compile_while(char_u *arg, cctx_T *cctx)
8178{
8179 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008180 scope_T *scope;
8181
8182 scope = new_scope(cctx, WHILE_SCOPE);
8183 if (scope == NULL)
8184 return NULL;
8185
Bram Moolenaara91a7132021-03-25 21:12:15 +01008186 // "endwhile" jumps back here, one before when profiling or using cmdmods
8187 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188
8189 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008190 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008191 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008192 if (!ends_excmd2(arg, skipwhite(p)))
8193 {
8194 semsg(_(e_trailing_arg), p);
8195 return NULL;
8196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008197
Bram Moolenaar13106602020-10-04 16:06:05 +02008198 if (bool_on_stack(cctx) == FAIL)
8199 return FAIL;
8200
Bram Moolenaara91a7132021-03-25 21:12:15 +01008201 // CMDMOD_REV must come before the jump
8202 generate_undo_cmdmods(cctx);
8203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008204 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008205 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008206 JUMP_IF_FALSE, cctx) == FAIL)
8207 return FAIL;
8208
8209 return p;
8210}
8211
8212/*
8213 * compile "endwhile"
8214 */
8215 static char_u *
8216compile_endwhile(char_u *arg, cctx_T *cctx)
8217{
8218 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008219 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008220
Bram Moolenaarfa984412021-03-25 22:15:28 +01008221 if (misplaced_cmdmod(cctx))
8222 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008223 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8224 {
8225 emsg(_(e_while));
8226 return NULL;
8227 }
8228 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008229 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008230
Bram Moolenaarf002a412021-01-24 13:34:18 +01008231#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008232 // count the endwhile before jumping
8233 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008234#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008236 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008237 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008238
8239 // Fill in the "end" label in the WHILE statement so it can jump here.
8240 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008241 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8242 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008243
8244 vim_free(scope);
8245
8246 return arg;
8247}
8248
8249/*
8250 * compile "continue"
8251 */
8252 static char_u *
8253compile_continue(char_u *arg, cctx_T *cctx)
8254{
8255 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008256 int try_scopes = 0;
8257 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008258
8259 for (;;)
8260 {
8261 if (scope == NULL)
8262 {
8263 emsg(_(e_continue));
8264 return NULL;
8265 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008266 if (scope->se_type == FOR_SCOPE)
8267 {
8268 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008269 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008270 }
8271 if (scope->se_type == WHILE_SCOPE)
8272 {
8273 loop_label = scope->se_u.se_while.ws_top_label;
8274 break;
8275 }
8276 if (scope->se_type == TRY_SCOPE)
8277 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008278 scope = scope->se_outer;
8279 }
8280
Bram Moolenaarc150c092021-02-13 15:02:46 +01008281 if (try_scopes > 0)
8282 // Inside one or more try/catch blocks we first need to jump to the
8283 // "finally" or "endtry" to cleanup.
8284 generate_TRYCONT(cctx, try_scopes, loop_label);
8285 else
8286 // Jump back to the FOR or WHILE instruction.
8287 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008289 return arg;
8290}
8291
8292/*
8293 * compile "break"
8294 */
8295 static char_u *
8296compile_break(char_u *arg, cctx_T *cctx)
8297{
8298 scope_T *scope = cctx->ctx_scope;
8299 endlabel_T **el;
8300
8301 for (;;)
8302 {
8303 if (scope == NULL)
8304 {
8305 emsg(_(e_break));
8306 return NULL;
8307 }
8308 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8309 break;
8310 scope = scope->se_outer;
8311 }
8312
8313 // Jump to the end of the FOR or WHILE loop.
8314 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008315 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008316 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008317 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008318 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8319 return FAIL;
8320
8321 return arg;
8322}
8323
8324/*
8325 * compile "{" start of block
8326 */
8327 static char_u *
8328compile_block(char_u *arg, cctx_T *cctx)
8329{
8330 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8331 return NULL;
8332 return skipwhite(arg + 1);
8333}
8334
8335/*
8336 * compile end of block: drop one scope
8337 */
8338 static void
8339compile_endblock(cctx_T *cctx)
8340{
8341 scope_T *scope = cctx->ctx_scope;
8342
8343 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008344 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008345 vim_free(scope);
8346}
8347
8348/*
8349 * compile "try"
8350 * Creates a new scope for the try-endtry, pointing to the first catch and
8351 * finally.
8352 * Creates another scope for the "try" block itself.
8353 * TRY instruction sets up exception handling at runtime.
8354 *
8355 * "try"
8356 * TRY -> catch1, -> finally push trystack entry
8357 * ... try block
8358 * "throw {exception}"
8359 * EVAL {exception}
8360 * THROW create exception
8361 * ... try block
8362 * " catch {expr}"
8363 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008364 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008365 * EVAL {expr}
8366 * MATCH
8367 * JUMP nomatch -> catch2
8368 * CATCH remove exception
8369 * ... catch block
8370 * " catch"
8371 * JUMP -> finally
8372 * catch2: CATCH remove exception
8373 * ... catch block
8374 * " finally"
8375 * finally:
8376 * ... finally block
8377 * " endtry"
8378 * ENDTRY pop trystack entry, may rethrow
8379 */
8380 static char_u *
8381compile_try(char_u *arg, cctx_T *cctx)
8382{
8383 garray_T *instr = &cctx->ctx_instr;
8384 scope_T *try_scope;
8385 scope_T *scope;
8386
Bram Moolenaarfa984412021-03-25 22:15:28 +01008387 if (misplaced_cmdmod(cctx))
8388 return NULL;
8389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008390 // scope that holds the jumps that go to catch/finally/endtry
8391 try_scope = new_scope(cctx, TRY_SCOPE);
8392 if (try_scope == NULL)
8393 return NULL;
8394
Bram Moolenaar69f70502021-01-01 16:10:46 +01008395 if (cctx->ctx_skip != SKIP_YES)
8396 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008397 isn_T *isn;
8398
8399 // "try_catch" is set when the first ":catch" is found or when no catch
8400 // is found and ":finally" is found.
8401 // "try_finally" is set when ":finally" is found
8402 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008403 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008404 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8405 return NULL;
8406 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8407 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008408 return NULL;
8409 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008410
8411 // scope for the try block itself
8412 scope = new_scope(cctx, BLOCK_SCOPE);
8413 if (scope == NULL)
8414 return NULL;
8415
8416 return arg;
8417}
8418
8419/*
8420 * compile "catch {expr}"
8421 */
8422 static char_u *
8423compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8424{
8425 scope_T *scope = cctx->ctx_scope;
8426 garray_T *instr = &cctx->ctx_instr;
8427 char_u *p;
8428 isn_T *isn;
8429
Bram Moolenaarfa984412021-03-25 22:15:28 +01008430 if (misplaced_cmdmod(cctx))
8431 return NULL;
8432
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433 // end block scope from :try or :catch
8434 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8435 compile_endblock(cctx);
8436 scope = cctx->ctx_scope;
8437
8438 // Error if not in a :try scope
8439 if (scope == NULL || scope->se_type != TRY_SCOPE)
8440 {
8441 emsg(_(e_catch));
8442 return NULL;
8443 }
8444
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008445 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008446 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008447 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008448 return NULL;
8449 }
8450
Bram Moolenaar69f70502021-01-01 16:10:46 +01008451 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008452 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008453#ifdef FEAT_PROFILE
8454 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008455 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008456 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008457 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008458 .isn_type == ISN_PROF_START)
8459 --instr->ga_len;
8460#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008461 // Jump from end of previous block to :finally or :endtry
8462 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8463 JUMP_ALWAYS, cctx) == FAIL)
8464 return NULL;
8465
8466 // End :try or :catch scope: set value in ISN_TRY instruction
8467 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008468 if (isn->isn_arg.try.try_ref->try_catch == 0)
8469 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008470 if (scope->se_u.se_try.ts_catch_label != 0)
8471 {
8472 // Previous catch without match jumps here
8473 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8474 isn->isn_arg.jump.jump_where = instr->ga_len;
8475 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008476#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008477 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008478 {
8479 // a "throw" that jumps here needs to be counted
8480 generate_instr(cctx, ISN_PROF_END);
8481 // the "catch" is also counted
8482 generate_instr(cctx, ISN_PROF_START);
8483 }
8484#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008485 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008486 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008487 }
8488
8489 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008490 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008491 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008492 scope->se_u.se_try.ts_caught_all = TRUE;
8493 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008494 }
8495 else
8496 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008497 char_u *end;
8498 char_u *pat;
8499 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008500 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008501 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008503 // Push v:exception, push {expr} and MATCH
8504 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8505
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008506 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008507 if (*end != *p)
8508 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008509 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008510 vim_free(tofree);
8511 return FAIL;
8512 }
8513 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008514 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008515 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008516 len = (int)(end - tofree);
8517 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008518 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008519 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008520 if (pat == NULL)
8521 return FAIL;
8522 if (generate_PUSHS(cctx, pat) == FAIL)
8523 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008525 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8526 return NULL;
8527
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008528 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008529 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8530 return NULL;
8531 }
8532
Bram Moolenaar69f70502021-01-01 16:10:46 +01008533 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008534 return NULL;
8535
8536 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8537 return NULL;
8538 return p;
8539}
8540
8541 static char_u *
8542compile_finally(char_u *arg, cctx_T *cctx)
8543{
8544 scope_T *scope = cctx->ctx_scope;
8545 garray_T *instr = &cctx->ctx_instr;
8546 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008547 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008548
Bram Moolenaarfa984412021-03-25 22:15:28 +01008549 if (misplaced_cmdmod(cctx))
8550 return NULL;
8551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552 // end block scope from :try or :catch
8553 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8554 compile_endblock(cctx);
8555 scope = cctx->ctx_scope;
8556
8557 // Error if not in a :try scope
8558 if (scope == NULL || scope->se_type != TRY_SCOPE)
8559 {
8560 emsg(_(e_finally));
8561 return NULL;
8562 }
8563
8564 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008565 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008566 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008567 {
8568 emsg(_(e_finally_dup));
8569 return NULL;
8570 }
8571
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008572 this_instr = instr->ga_len;
8573#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008574 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008575 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008576 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008577 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008578 // jump to the profile start of the "finally"
8579 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008580
8581 // jump to the profile end above it
8582 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8583 .isn_type == ISN_PROF_END)
8584 --this_instr;
8585 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008586#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008587
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008588 // Fill in the "end" label in jumps at the end of the blocks.
8589 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8590 this_instr, cctx);
8591
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008592 // If there is no :catch then an exception jumps to :finally.
8593 if (isn->isn_arg.try.try_ref->try_catch == 0)
8594 isn->isn_arg.try.try_ref->try_catch = this_instr;
8595 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008596 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008597 {
8598 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008599 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008600 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008601 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008602 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008603 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8604 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008606 // TODO: set index in ts_finally_label jumps
8607
8608 return arg;
8609}
8610
8611 static char_u *
8612compile_endtry(char_u *arg, cctx_T *cctx)
8613{
8614 scope_T *scope = cctx->ctx_scope;
8615 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008616 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008617
Bram Moolenaarfa984412021-03-25 22:15:28 +01008618 if (misplaced_cmdmod(cctx))
8619 return NULL;
8620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008621 // end block scope from :catch or :finally
8622 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8623 compile_endblock(cctx);
8624 scope = cctx->ctx_scope;
8625
8626 // Error if not in a :try scope
8627 if (scope == NULL || scope->se_type != TRY_SCOPE)
8628 {
8629 if (scope == NULL)
8630 emsg(_(e_no_endtry));
8631 else if (scope->se_type == WHILE_SCOPE)
8632 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008633 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008634 emsg(_(e_endfor));
8635 else
8636 emsg(_(e_endif));
8637 return NULL;
8638 }
8639
Bram Moolenaarc150c092021-02-13 15:02:46 +01008640 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008641 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008642 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008643 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8644 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008645 {
8646 emsg(_(e_missing_catch_or_finally));
8647 return NULL;
8648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008649
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008650#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008651 if (cctx->ctx_compile_type == CT_PROFILE
8652 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008653 .isn_type == ISN_PROF_START)
8654 // move the profile start after "endtry" so that it's not counted when
8655 // the exception is rethrown.
8656 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008657#endif
8658
Bram Moolenaar69f70502021-01-01 16:10:46 +01008659 // Fill in the "end" label in jumps at the end of the blocks, if not
8660 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008661 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8662 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008663
Bram Moolenaar69f70502021-01-01 16:10:46 +01008664 if (scope->se_u.se_try.ts_catch_label != 0)
8665 {
8666 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008667 isn_T *isn = ((isn_T *)instr->ga_data)
8668 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008669 isn->isn_arg.jump.jump_where = instr->ga_len;
8670 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008671 }
8672
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008673 compile_endblock(cctx);
8674
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008675 if (cctx->ctx_skip != SKIP_YES)
8676 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008677 // End :catch or :finally scope: set instruction index in ISN_TRY
8678 // instruction
8679 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008680 if (cctx->ctx_skip != SKIP_YES
8681 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8682 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008683#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008684 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008685 generate_instr(cctx, ISN_PROF_START);
8686#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008687 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008688 return arg;
8689}
8690
8691/*
8692 * compile "throw {expr}"
8693 */
8694 static char_u *
8695compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8696{
8697 char_u *p = skipwhite(arg);
8698
Bram Moolenaara5565e42020-05-09 15:44:01 +02008699 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008700 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008701 if (cctx->ctx_skip == SKIP_YES)
8702 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008703 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008704 return NULL;
8705 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8706 return NULL;
8707
8708 return p;
8709}
8710
Bram Moolenaarc3235272021-07-10 19:42:03 +02008711 static char_u *
8712compile_eval(char_u *arg, cctx_T *cctx)
8713{
8714 char_u *p = arg;
8715 int name_only;
8716 char_u *alias;
8717 long lnum = SOURCING_LNUM;
8718
8719 // find_ex_command() will consider a variable name an expression, assuming
8720 // that something follows on the next line. Check that something actually
8721 // follows, otherwise it's probably a misplaced command.
8722 get_name_len(&p, &alias, FALSE, FALSE);
8723 name_only = ends_excmd2(arg, skipwhite(p));
8724 vim_free(alias);
8725
8726 p = arg;
8727 if (compile_expr0(&p, cctx) == FAIL)
8728 return NULL;
8729
8730 if (name_only && lnum == SOURCING_LNUM)
8731 {
8732 semsg(_(e_expression_without_effect_str), arg);
8733 return NULL;
8734 }
8735
8736 // drop the result
8737 generate_instr_drop(cctx, ISN_DROP, 1);
8738
8739 return skipwhite(p);
8740}
8741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008742/*
8743 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008744 * compile "echomsg expr"
8745 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008746 * compile "execute expr"
8747 */
8748 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008749compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008750{
8751 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008752 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008753 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008754 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008755 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008756 garray_T *stack = &cctx->ctx_type_stack;
8757 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008758
8759 for (;;)
8760 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008761 if (ends_excmd2(prev, p))
8762 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008763 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008764 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008765 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008766
8767 if (cctx->ctx_skip != SKIP_YES)
8768 {
8769 // check for non-void type
8770 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8771 if (type->tt_type == VAR_VOID)
8772 {
8773 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8774 return NULL;
8775 }
8776 }
8777
Bram Moolenaarad39c092020-02-26 18:23:43 +01008778 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008779 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008780 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008781 }
8782
Bram Moolenaare4984292020-12-13 14:19:25 +01008783 if (count > 0)
8784 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008785 long save_lnum = cctx->ctx_lnum;
8786
8787 // Use the line number where the command started.
8788 cctx->ctx_lnum = start_ctx_lnum;
8789
Bram Moolenaare4984292020-12-13 14:19:25 +01008790 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8791 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8792 else if (cmdidx == CMD_execute)
8793 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8794 else if (cmdidx == CMD_echomsg)
8795 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8796 else
8797 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008798
8799 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008801 return p;
8802}
8803
8804/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008805 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008806 * instruction to compute it and return OK.
8807 * Otherwise return FAIL, the caller must deal with any range.
8808 */
8809 static int
8810compile_variable_range(exarg_T *eap, cctx_T *cctx)
8811{
8812 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8813 char_u *p = skipdigits(eap->cmd);
8814
8815 if (p == range_end)
8816 return FAIL;
8817 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8818}
8819
8820/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008821 * :put r
8822 * :put ={expr}
8823 */
8824 static char_u *
8825compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8826{
8827 char_u *line = arg;
8828 linenr_T lnum;
8829 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008830 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008831
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008832 eap->regname = *line;
8833
8834 if (eap->regname == '=')
8835 {
8836 char_u *p = line + 1;
8837
8838 if (compile_expr0(&p, cctx) == FAIL)
8839 return NULL;
8840 line = p;
8841 }
8842 else if (eap->regname != NUL)
8843 ++line;
8844
Bram Moolenaar08597872020-12-10 19:43:40 +01008845 if (compile_variable_range(eap, cctx) == OK)
8846 {
8847 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8848 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008849 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008850 {
8851 // Either no range or a number.
8852 // "errormsg" will not be set because the range is ADDR_LINES.
8853 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008854 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008855 return NULL;
8856 if (eap->addr_count == 0)
8857 lnum = -1;
8858 else
8859 lnum = eap->line2;
8860 if (above)
8861 --lnum;
8862 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008863
8864 generate_PUT(cctx, eap->regname, lnum);
8865 return line;
8866}
8867
8868/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008869 * A command that is not compiled, execute with legacy code.
8870 */
8871 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008872compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008873{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008874 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02008875 char_u *p;
8876 int has_expr = FALSE;
8877 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008878 char_u *tofree = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008879
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008880 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008881 goto theend;
8882
Bram Moolenaare729ce22021-06-06 21:38:09 +02008883 // If there was a prececing command modifier, drop it and include it in the
8884 // EXEC command.
8885 if (cctx->ctx_has_cmdmod)
8886 {
8887 garray_T *instr = &cctx->ctx_instr;
8888 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8889
8890 if (isn->isn_type == ISN_CMDMOD)
8891 {
8892 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8893 ->cmod_filter_regmatch.regprog);
8894 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8895 --instr->ga_len;
8896 cctx->ctx_has_cmdmod = FALSE;
8897 }
8898 }
8899
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008900 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008901 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008902 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008903 int usefilter = FALSE;
8904
8905 has_expr = argt & (EX_XFILE | EX_EXPAND);
8906
8907 // If the command can be followed by a bar, find the bar and truncate
8908 // it, so that the following command can be compiled.
8909 // The '|' is overwritten with a NUL, it is put back below.
8910 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8911 && *eap->arg == '!')
8912 // :w !filter or :r !filter or :r! filter
8913 usefilter = TRUE;
8914 if ((argt & EX_TRLBAR) && !usefilter)
8915 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008916 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008917 separate_nextcmd(eap);
8918 if (eap->nextcmd != NULL)
8919 nextcmd = eap->nextcmd;
8920 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008921 else if (eap->cmdidx == CMD_wincmd)
8922 {
8923 p = eap->arg;
8924 if (*p != NUL)
8925 ++p;
8926 if (*p == 'g' || *p == Ctrl_G)
8927 ++p;
8928 p = skipwhite(p);
8929 if (*p == '|')
8930 {
8931 *p = NUL;
8932 nextcmd = p + 1;
8933 }
8934 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008935 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
8936 {
8937 // If there is a trailing '{' read lines until the '}'
8938 p = eap->arg + STRLEN(eap->arg) - 1;
8939 while (p > eap->arg && VIM_ISWHITE(*p))
8940 --p;
8941 if (*p == '{')
8942 {
8943 exarg_T ea;
8944 int flags; // unused
8945 int start_lnum = SOURCING_LNUM;
8946
8947 CLEAR_FIELD(ea);
8948 ea.arg = eap->arg;
8949 fill_exarg_from_cctx(&ea, cctx);
8950 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
8951 if (tofree != NULL)
8952 {
8953 *p = NUL;
8954 line = concat_str(line, tofree);
8955 if (line == NULL)
8956 goto theend;
8957 vim_free(tofree);
8958 tofree = line;
8959 SOURCING_LNUM = start_lnum;
8960 }
8961 }
8962 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008963 }
8964
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008965 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8966 {
8967 // expand filename in "syntax include [@group] filename"
8968 has_expr = TRUE;
8969 eap->arg = skipwhite(eap->arg + 7);
8970 if (*eap->arg == '@')
8971 eap->arg = skiptowhite(eap->arg);
8972 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008973
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008974 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8975 && STRLEN(eap->arg) > 4)
8976 {
8977 int delim = *eap->arg;
8978
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008979 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008980 if (*p == delim)
8981 {
8982 eap->arg = p + 1;
8983 has_expr = TRUE;
8984 }
8985 }
8986
Bram Moolenaarecac5912021-01-05 19:23:28 +01008987 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8988 {
8989 // TODO: should only expand when appropriate for the command
8990 eap->arg = skiptowhite(eap->arg);
8991 has_expr = TRUE;
8992 }
8993
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008994 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008995 {
8996 int count = 0;
8997 char_u *start = skipwhite(line);
8998
8999 // :cmd xxx`=expr1`yyy`=expr2`zzz
9000 // PUSHS ":cmd xxx"
9001 // eval expr1
9002 // PUSHS "yyy"
9003 // eval expr2
9004 // PUSHS "zzz"
9005 // EXECCONCAT 5
9006 for (;;)
9007 {
9008 if (p > start)
9009 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02009010 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009011 ++count;
9012 }
9013 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009014 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009015 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009016 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009017 ++count;
9018 p = skipwhite(p);
9019 if (*p != '`')
9020 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009021 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009022 return NULL;
9023 }
9024 start = p + 1;
9025
9026 p = (char_u *)strstr((char *)start, "`=");
9027 if (p == NULL)
9028 {
9029 if (*skipwhite(start) != NUL)
9030 {
9031 generate_PUSHS(cctx, vim_strsave(start));
9032 ++count;
9033 }
9034 break;
9035 }
9036 }
9037 generate_EXECCONCAT(cctx, count);
9038 }
9039 else
9040 generate_EXEC(cctx, line);
9041
9042theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009043 if (*nextcmd != NUL)
9044 {
9045 // the parser expects a pointer to the bar, put it back
9046 --nextcmd;
9047 *nextcmd = '|';
9048 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009049 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009050
9051 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009052}
9053
Bram Moolenaar20677332021-06-06 17:02:53 +02009054/*
9055 * A script command with heredoc, e.g.
9056 * ruby << EOF
9057 * command
9058 * EOF
9059 * Has been turned into one long line with NL characters by
9060 * get_function_body():
9061 * ruby << EOF<NL> command<NL>EOF
9062 */
9063 static char_u *
9064compile_script(char_u *line, cctx_T *cctx)
9065{
9066 if (cctx->ctx_skip != SKIP_YES)
9067 {
9068 isn_T *isn;
9069
9070 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9071 return NULL;
9072 isn->isn_arg.string = vim_strsave(line);
9073 }
9074 return (char_u *)"";
9075}
9076
Bram Moolenaar8238f082021-04-20 21:10:48 +02009077
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009078/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009079 * :s/pat/repl/
9080 */
9081 static char_u *
9082compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9083{
9084 char_u *cmd = eap->arg;
9085 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9086
9087 if (expr != NULL)
9088 {
9089 int delimiter = *cmd++;
9090
9091 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009092 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009093 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9094 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009095 garray_T save_ga = cctx->ctx_instr;
9096 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009097 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009098 int trailing_error;
9099 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009100 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009101 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009102
9103 cmd += 3;
9104 end = skip_substitute(cmd, delimiter);
9105
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009106 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009107 // labels are correct.
9108 cctx->ctx_instr.ga_len = 0;
9109 cctx->ctx_instr.ga_maxlen = 0;
9110 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009111 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009112 if (end[-1] == NUL)
9113 end[-1] = delimiter;
9114 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009115 trailing_error = *cmd != delimiter && *cmd != NUL;
9116
Bram Moolenaar169502f2021-04-21 12:19:35 +02009117 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009118 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009119 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009120 if (trailing_error)
9121 semsg(_(e_trailing_arg), cmd);
9122 clear_instr_ga(&cctx->ctx_instr);
9123 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009124 return NULL;
9125 }
9126
Bram Moolenaar8238f082021-04-20 21:10:48 +02009127 // Move the generated instructions into the ISN_SUBSTITUTE
9128 // instructions, then restore the list of instructions before
9129 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009130 instr_count = cctx->ctx_instr.ga_len;
9131 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009132 instr[instr_count].isn_type = ISN_FINISH;
9133
9134 cctx->ctx_instr = save_ga;
9135 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9136 {
9137 int idx;
9138
9139 for (idx = 0; idx < instr_count; ++idx)
9140 delete_instr(instr + idx);
9141 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009142 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009143 }
9144 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9145 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009146
9147 // skip over flags
9148 if (*end == '&')
9149 ++end;
9150 while (ASCII_ISALPHA(*end) || *end == '#')
9151 ++end;
9152 return end;
9153 }
9154 }
9155
9156 return compile_exec(arg, eap, cctx);
9157}
9158
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009159 static char_u *
9160compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9161{
9162 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009163 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009164
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009165 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009166 {
9167 if (STRNCMP(arg, "END", 3) == 0)
9168 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009169 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009170 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009171 // First load the current variable value.
9172 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9173 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009174 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009175 }
9176
9177 // Gets the redirected text and put it on the stack, then store it
9178 // in the variable.
9179 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9180
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009181 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009182 generate_instr_drop(cctx, ISN_CONCAT, 1);
9183
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009184 if (lhs->lhs_has_index)
9185 {
9186 // Use the info in "lhs" to store the value at the index in the
9187 // list or dict.
9188 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9189 &t_string, cctx) == FAIL)
9190 return NULL;
9191 }
9192 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009193 return NULL;
9194
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009195 VIM_CLEAR(lhs->lhs_name);
9196 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009197 return arg + 3;
9198 }
9199 emsg(_(e_cannot_nest_redir));
9200 return NULL;
9201 }
9202
9203 if (arg[0] == '=' && arg[1] == '>')
9204 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009205 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009206
9207 // redirect to a variable is compiled
9208 arg += 2;
9209 if (*arg == '>')
9210 {
9211 ++arg;
9212 append = TRUE;
9213 }
9214 arg = skipwhite(arg);
9215
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009216 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009217 FALSE, FALSE, 1, cctx) == FAIL)
9218 return NULL;
9219 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009220 lhs->lhs_append = append;
9221 if (lhs->lhs_has_index)
9222 {
9223 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9224 if (lhs->lhs_whole == NULL)
9225 return NULL;
9226 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009227
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009228 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009229 }
9230
9231 // other redirects are handled like at script level
9232 return compile_exec(line, eap, cctx);
9233}
9234
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009235#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009236 static char_u *
9237compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9238{
9239 isn_T *isn;
9240 char_u *p;
9241
9242 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9243 if (isn == NULL)
9244 return NULL;
9245 isn->isn_arg.number = eap->cmdidx;
9246
9247 p = eap->arg;
9248 if (compile_expr0(&p, cctx) == FAIL)
9249 return NULL;
9250
9251 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9252 if (isn == NULL)
9253 return NULL;
9254 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9255 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9256 return NULL;
9257 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9258 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9259 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9260
9261 return p;
9262}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009263#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009264
Bram Moolenaar4c137212021-04-19 16:48:48 +02009265/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009266 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009267 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009268 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009269 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009270add_def_function(ufunc_T *ufunc)
9271{
9272 dfunc_T *dfunc;
9273
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009274 if (def_functions.ga_len == 0)
9275 {
9276 // The first position is not used, so that a zero uf_dfunc_idx means it
9277 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009278 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009279 return FAIL;
9280 ++def_functions.ga_len;
9281 }
9282
Bram Moolenaar09689a02020-05-09 22:50:08 +02009283 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009284 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009285 return FAIL;
9286 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9287 CLEAR_POINTER(dfunc);
9288 dfunc->df_idx = def_functions.ga_len;
9289 ufunc->uf_dfunc_idx = dfunc->df_idx;
9290 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009291 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009292 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009293 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009294 ++def_functions.ga_len;
9295 return OK;
9296}
9297
9298/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009299 * After ex_function() has collected all the function lines: parse and compile
9300 * the lines into instructions.
9301 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009302 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9303 * the return statement (used for lambda). When uf_ret_type is already set
9304 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009305 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009306 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009307 * This can be used recursively through compile_lambda(), which may reallocate
9308 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009309 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009310 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009311 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009312compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009313 ufunc_T *ufunc,
9314 int check_return_type,
9315 compiletype_T compile_type,
9316 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009317{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009318 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009319 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009320 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009321 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009322 cctx_T cctx;
9323 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009324 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009325 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009326 int ret = FAIL;
9327 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009328 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009329 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009330 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009331 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009332#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009333 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009334#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009335 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009336
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009337 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009338 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009339 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009340 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009341 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9342 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009343 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009344
9345 switch (compile_type)
9346 {
9347 case CT_PROFILE:
9348#ifdef FEAT_PROFILE
9349 instr_dest = dfunc->df_instr_prof; break;
9350#endif
9351 case CT_NONE: instr_dest = dfunc->df_instr; break;
9352 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9353 }
9354 if (instr_dest != NULL)
9355 // Was compiled in this mode before: Free old instructions.
9356 delete_def_function_contents(dfunc, FALSE);
9357 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009358 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009359 else
9360 {
9361 if (add_def_function(ufunc) == FAIL)
9362 return FAIL;
9363 new_def_function = TRUE;
9364 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009365
Bram Moolenaar985116a2020-07-12 17:31:09 +02009366 ufunc->uf_def_status = UF_COMPILING;
9367
Bram Moolenaara80faa82020-04-12 19:37:17 +02009368 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009369
Bram Moolenaare99d4222021-06-13 14:01:26 +02009370 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009371 cctx.ctx_ufunc = ufunc;
9372 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009373 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009374 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9375 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9376 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9377 cctx.ctx_type_list = &ufunc->uf_type_list;
9378 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9379 instr = &cctx.ctx_instr;
9380
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009381 // Set the context to the function, it may be compiled when called from
9382 // another script. Set the script version to the most modern one.
9383 // The line number will be set in next_line_from_context().
9384 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009385 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9386
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009387 // Don't use the flag from ":legacy" here.
9388 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9389
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009390 // Make sure error messages are OK.
9391 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9392 if (do_estack_push)
9393 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009394 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009395
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009396 if (ufunc->uf_def_args.ga_len > 0)
9397 {
9398 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009399 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009400 int i;
9401 char_u *arg;
9402 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009403 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009404
9405 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009406 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009407 for (i = 0; i < count; ++i)
9408 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009409 garray_T *stack = &cctx.ctx_type_stack;
9410 type_T *val_type;
9411 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009412 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009413 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009414 int jump_instr_idx = instr->ga_len;
9415 isn_T *isn;
9416
9417 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9418 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9419 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009420
9421 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009422 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009423
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009424 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009425 r = compile_expr0(&arg, &cctx);
9426
Bram Moolenaar12bce952021-03-11 20:04:04 +01009427 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009428 goto erret;
9429
9430 // If no type specified use the type of the default value.
9431 // Otherwise check that the default value type matches the
9432 // specified type.
9433 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009434 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009435 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009436 {
9437 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009438 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009439 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009440 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009441 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009442 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009443
9444 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009445 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009446
9447 // set instruction index in JUMP_IF_ARG_SET to here
9448 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9449 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009450 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009451
9452 if (did_set_arg_type)
9453 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009454 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009455 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009456
9457 /*
9458 * Loop over all the lines of the function and generate instructions.
9459 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009460 for (;;)
9461 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009462 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009463 int starts_with_colon = FALSE;
9464 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009465 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009466
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009467 // Bail out on the first error to avoid a flood of errors and report
9468 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009469 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009470 goto erret;
9471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009472 if (line != NULL && *line == '|')
9473 // the line continues after a '|'
9474 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009475 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009476 && !(*line == '#' && (line == cctx.ctx_line_start
9477 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009478 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009479 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009480 goto erret;
9481 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009482 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9483 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009484 else
9485 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009486 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009487 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009488 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009489 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009490#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009491 if (cctx.ctx_skip != SKIP_YES)
9492 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009493#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009494 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009495 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009496 // Make a copy, splitting off nextcmd and removing trailing spaces
9497 // may change it.
9498 if (line != NULL)
9499 {
9500 line = vim_strsave(line);
9501 vim_free(line_to_free);
9502 line_to_free = line;
9503 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009504 }
9505
Bram Moolenaara80faa82020-04-12 19:37:17 +02009506 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009507 ea.cmdlinep = &line;
9508 ea.cmd = skipwhite(line);
9509
Bram Moolenaarb2049902021-01-24 12:53:53 +01009510 if (*ea.cmd == '#')
9511 {
9512 // "#" starts a comment
9513 line = (char_u *)"";
9514 continue;
9515 }
9516
Bram Moolenaarf002a412021-01-24 13:34:18 +01009517#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009518 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9519 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009520 {
9521 may_generate_prof_end(&cctx, prof_lnum);
9522
9523 prof_lnum = cctx.ctx_lnum;
9524 generate_instr(&cctx, ISN_PROF_START);
9525 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009526#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009527 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9528 && cctx.ctx_skip != SKIP_YES)
9529 {
9530 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009531 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009532 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009533 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009534
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009535 // Some things can be recognized by the first character.
9536 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009537 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009538 case '}':
9539 {
9540 // "}" ends a block scope
9541 scopetype_T stype = cctx.ctx_scope == NULL
9542 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009543
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009544 if (stype == BLOCK_SCOPE)
9545 {
9546 compile_endblock(&cctx);
9547 line = ea.cmd;
9548 }
9549 else
9550 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009551 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009552 goto erret;
9553 }
9554 if (line != NULL)
9555 line = skipwhite(ea.cmd + 1);
9556 continue;
9557 }
9558
9559 case '{':
9560 // "{" starts a block scope
9561 // "{'a': 1}->func() is something else
9562 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9563 {
9564 line = compile_block(ea.cmd, &cctx);
9565 continue;
9566 }
9567 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009568 }
9569
9570 /*
9571 * COMMAND MODIFIERS
9572 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009573 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009574 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9575 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009576 {
9577 if (errormsg != NULL)
9578 goto erret;
9579 // empty line or comment
9580 line = (char_u *)"";
9581 continue;
9582 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009583 generate_cmdmods(&cctx, &local_cmdmod);
9584 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009585
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009586 // Check if there was a colon after the last command modifier or before
9587 // the current position.
9588 for (p = ea.cmd; p >= line; --p)
9589 {
9590 if (*p == ':')
9591 starts_with_colon = TRUE;
9592 if (p < ea.cmd && !VIM_ISWHITE(*p))
9593 break;
9594 }
9595
Bram Moolenaarce024c32021-06-26 13:00:49 +02009596 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009597 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009598 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009599 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009600 if (checkforcmd(&ea.cmd, "call", 3))
9601 {
9602 if (*ea.cmd == '(')
9603 // not for "call()"
9604 ea.cmd = p;
9605 else
9606 ea.cmd = skipwhite(ea.cmd);
9607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009608
Bram Moolenaarce024c32021-06-26 13:00:49 +02009609 if (!starts_with_colon)
9610 {
9611 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009612
Bram Moolenaarce024c32021-06-26 13:00:49 +02009613 // Check for assignment after command modifiers.
9614 assign = may_compile_assignment(&ea, &line, &cctx);
9615 if (assign == OK)
9616 goto nextline;
9617 if (assign == FAIL)
9618 goto erret;
9619 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009620 }
9621
9622 /*
9623 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009624 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009625 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009626 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009627 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009628 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9629 && (starts_with_colon || !(*cmd == '\''
9630 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009631 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009632 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009633 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009634 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009635 if (!starts_with_colon)
9636 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009637 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009638 goto erret;
9639 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009640 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009641 if (ends_excmd2(line, ea.cmd))
9642 {
9643 // A range without a command: jump to the line.
9644 // TODO: compile to a more efficient command, possibly
9645 // calling parse_cmd_address().
9646 ea.cmdidx = CMD_SIZE;
9647 line = compile_exec(line, &ea, &cctx);
9648 goto nextline;
9649 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009650 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009651 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009652 p = find_ex_command(&ea, NULL,
9653 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009654 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009655
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009656 if (p == NULL)
9657 {
9658 if (cctx.ctx_skip != SKIP_YES)
9659 emsg(_(e_ambiguous_use_of_user_defined_command));
9660 goto erret;
9661 }
9662
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009663 // When using ":legacy cmd" always use compile_exec().
9664 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009665 {
9666 char_u *start = ea.cmd;
9667
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009668 switch (ea.cmdidx)
9669 {
9670 case CMD_if:
9671 case CMD_elseif:
9672 case CMD_else:
9673 case CMD_endif:
9674 case CMD_for:
9675 case CMD_endfor:
9676 case CMD_continue:
9677 case CMD_break:
9678 case CMD_while:
9679 case CMD_endwhile:
9680 case CMD_try:
9681 case CMD_catch:
9682 case CMD_finally:
9683 case CMD_endtry:
9684 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9685 goto erret;
9686 default: break;
9687 }
9688
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009689 // ":legacy return expr" needs to be handled differently.
9690 if (checkforcmd(&start, "return", 4))
9691 ea.cmdidx = CMD_return;
9692 else
9693 ea.cmdidx = CMD_legacy;
9694 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009696 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9697 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009698 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009699 {
9700 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009701 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009702 }
9703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009704 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009705 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009706 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009707 // CMD_var cannot happen, compile_assignment() above would be
9708 // used. Most likely an assignment to a non-existing variable.
9709 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009710 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009711 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009712 }
9713
Bram Moolenaar3988f642020-08-27 22:43:03 +02009714 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009715 && ea.cmdidx != CMD_elseif
9716 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009717 && ea.cmdidx != CMD_endif
9718 && ea.cmdidx != CMD_endfor
9719 && ea.cmdidx != CMD_endwhile
9720 && ea.cmdidx != CMD_catch
9721 && ea.cmdidx != CMD_finally
9722 && ea.cmdidx != CMD_endtry)
9723 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009724 emsg(_(e_unreachable_code_after_return));
9725 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009726 }
9727
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009728 p = skipwhite(p);
9729 if (ea.cmdidx != CMD_SIZE
9730 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9731 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009732 if (ea.cmdidx >= 0)
9733 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009734 if ((ea.argt & EX_BANG) && *p == '!')
9735 {
9736 ea.forceit = TRUE;
9737 p = skipwhite(p + 1);
9738 }
9739 }
9740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009741 switch (ea.cmdidx)
9742 {
9743 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009744 ea.arg = p;
9745 line = compile_nested_function(&ea, &cctx);
9746 break;
9747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009748 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009749 // TODO: should we allow this, e.g. to declare a global
9750 // function?
9751 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009752 goto erret;
9753
9754 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009755 line = compile_return(p, check_return_type,
9756 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009757 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009758 break;
9759
9760 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009761 emsg(_(e_cannot_use_let_in_vim9_script));
9762 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009763 case CMD_var:
9764 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009765 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009766 case CMD_increment:
9767 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009768 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009769 if (line == p)
9770 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009771 break;
9772
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009773 case CMD_unlet:
9774 case CMD_unlockvar:
9775 case CMD_lockvar:
9776 line = compile_unletlock(p, &ea, &cctx);
9777 break;
9778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009779 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009780 emsg(_(e_import_can_only_be_used_in_script));
9781 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009782 break;
9783
9784 case CMD_if:
9785 line = compile_if(p, &cctx);
9786 break;
9787 case CMD_elseif:
9788 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009789 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009790 break;
9791 case CMD_else:
9792 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009793 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009794 break;
9795 case CMD_endif:
9796 line = compile_endif(p, &cctx);
9797 break;
9798
9799 case CMD_while:
9800 line = compile_while(p, &cctx);
9801 break;
9802 case CMD_endwhile:
9803 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009804 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009805 break;
9806
9807 case CMD_for:
9808 line = compile_for(p, &cctx);
9809 break;
9810 case CMD_endfor:
9811 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009812 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009813 break;
9814 case CMD_continue:
9815 line = compile_continue(p, &cctx);
9816 break;
9817 case CMD_break:
9818 line = compile_break(p, &cctx);
9819 break;
9820
9821 case CMD_try:
9822 line = compile_try(p, &cctx);
9823 break;
9824 case CMD_catch:
9825 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009826 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009827 break;
9828 case CMD_finally:
9829 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009830 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009831 break;
9832 case CMD_endtry:
9833 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009834 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009835 break;
9836 case CMD_throw:
9837 line = compile_throw(p, &cctx);
9838 break;
9839
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009840 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009841 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009842 break;
9843
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009844 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009845 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009846 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009847 case CMD_echomsg:
9848 case CMD_echoerr:
9849 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009850 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009851
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009852 case CMD_put:
9853 ea.cmd = cmd;
9854 line = compile_put(p, &ea, &cctx);
9855 break;
9856
Bram Moolenaar4c137212021-04-19 16:48:48 +02009857 case CMD_substitute:
9858 if (cctx.ctx_skip == SKIP_YES)
9859 line = (char_u *)"";
9860 else
9861 {
9862 ea.arg = p;
9863 line = compile_substitute(line, &ea, &cctx);
9864 }
9865 break;
9866
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009867 case CMD_redir:
9868 ea.arg = p;
9869 line = compile_redir(line, &ea, &cctx);
9870 break;
9871
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009872 case CMD_cexpr:
9873 case CMD_lexpr:
9874 case CMD_caddexpr:
9875 case CMD_laddexpr:
9876 case CMD_cgetexpr:
9877 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009878#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009879 ea.arg = p;
9880 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009881#else
9882 ex_ni(&ea);
9883 line = NULL;
9884#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009885 break;
9886
Bram Moolenaar3988f642020-08-27 22:43:03 +02009887 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009888
Bram Moolenaarae616492020-07-28 20:07:27 +02009889 case CMD_append:
9890 case CMD_change:
9891 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009892 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009893 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009894 case CMD_xit:
9895 not_in_vim9(&ea);
9896 goto erret;
9897
Bram Moolenaar002262f2020-07-08 17:47:57 +02009898 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009899 if (cctx.ctx_skip != SKIP_YES)
9900 {
9901 semsg(_(e_invalid_command_str), ea.cmd);
9902 goto erret;
9903 }
9904 // We don't check for a next command here.
9905 line = (char_u *)"";
9906 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009907
Bram Moolenaar20677332021-06-06 17:02:53 +02009908 case CMD_lua:
9909 case CMD_mzscheme:
9910 case CMD_perl:
9911 case CMD_py3:
9912 case CMD_python3:
9913 case CMD_python:
9914 case CMD_pythonx:
9915 case CMD_ruby:
9916 case CMD_tcl:
9917 ea.arg = p;
9918 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009919 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009920 else
9921 // heredoc lines have been concatenated with NL
9922 // characters in get_function_body()
9923 line = compile_script(line, &cctx);
9924 break;
9925
9926 default:
9927 // Not recognized, execute with do_cmdline_cmd().
9928 ea.arg = p;
9929 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009930 break;
9931 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009932nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009933 if (line == NULL)
9934 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009935 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009936
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009937 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009938 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009940 if (cctx.ctx_type_stack.ga_len < 0)
9941 {
9942 iemsg("Type stack underflow");
9943 goto erret;
9944 }
9945 }
9946
9947 if (cctx.ctx_scope != NULL)
9948 {
9949 if (cctx.ctx_scope->se_type == IF_SCOPE)
9950 emsg(_(e_endif));
9951 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9952 emsg(_(e_endwhile));
9953 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9954 emsg(_(e_endfor));
9955 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009956 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009957 goto erret;
9958 }
9959
Bram Moolenaarefd88552020-06-18 20:50:10 +02009960 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009961 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009962 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9963 ufunc->uf_ret_type = &t_void;
9964 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009965 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009966 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009967 goto erret;
9968 }
9969
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009970 // Return void if there is no return at the end.
9971 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009972 }
9973
Bram Moolenaar599410c2021-04-10 14:03:43 +02009974 // When compiled with ":silent!" and there was an error don't consider the
9975 // function compiled.
9976 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009977 {
9978 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9979 + ufunc->uf_dfunc_idx;
9980 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009981 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009982#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009983 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009984 {
9985 dfunc->df_instr_prof = instr->ga_data;
9986 dfunc->df_instr_prof_count = instr->ga_len;
9987 }
9988 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009989#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009990 if (cctx.ctx_compile_type == CT_DEBUG)
9991 {
9992 dfunc->df_instr_debug = instr->ga_data;
9993 dfunc->df_instr_debug_count = instr->ga_len;
9994 }
9995 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009996 {
9997 dfunc->df_instr = instr->ga_data;
9998 dfunc->df_instr_count = instr->ga_len;
9999 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010000 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010001 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010002 if (cctx.ctx_outer_used)
10003 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010004 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010006
10007 ret = OK;
10008
10009erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010010 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010011 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010012 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10013 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010014
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010015 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010016 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010017 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010018 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010019
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010020 // If using the last entry in the table and it was added above, we
10021 // might as well remove it.
10022 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010023 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010024 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010025 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010026 ufunc->uf_dfunc_idx = 0;
10027 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010028 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010029
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010030 while (cctx.ctx_scope != NULL)
10031 drop_scope(&cctx);
10032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010033 if (errormsg != NULL)
10034 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010035 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010036 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010037 }
10038
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010039 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10040 {
10041 if (ret == OK)
10042 {
10043 emsg(_(e_missing_redir_end));
10044 ret = FAIL;
10045 }
10046 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010047 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010048 }
10049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010050 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010051 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010052 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010053 if (do_estack_push)
10054 estack_pop();
10055
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010056 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010057 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010058 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010059 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010060 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010061}
10062
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010063 void
10064set_function_type(ufunc_T *ufunc)
10065{
10066 int varargs = ufunc->uf_va_name != NULL;
10067 int argcount = ufunc->uf_args.ga_len;
10068
10069 // Create a type for the function, with the return type and any
10070 // argument types.
10071 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10072 // The type is included in "tt_args".
10073 if (argcount > 0 || varargs)
10074 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010075 if (ufunc->uf_type_list.ga_itemsize == 0)
10076 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010077 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10078 argcount, &ufunc->uf_type_list);
10079 // Add argument types to the function type.
10080 if (func_type_add_arg_types(ufunc->uf_func_type,
10081 argcount + varargs,
10082 &ufunc->uf_type_list) == FAIL)
10083 return;
10084 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10085 ufunc->uf_func_type->tt_min_argcount =
10086 argcount - ufunc->uf_def_args.ga_len;
10087 if (ufunc->uf_arg_types == NULL)
10088 {
10089 int i;
10090
10091 // lambda does not have argument types.
10092 for (i = 0; i < argcount; ++i)
10093 ufunc->uf_func_type->tt_args[i] = &t_any;
10094 }
10095 else
10096 mch_memmove(ufunc->uf_func_type->tt_args,
10097 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10098 if (varargs)
10099 {
10100 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010101 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010102 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10103 }
10104 }
10105 else
10106 // No arguments, can use a predefined type.
10107 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10108 argcount, &ufunc->uf_type_list);
10109}
10110
10111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010112/*
10113 * Delete an instruction, free what it contains.
10114 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010115 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010116delete_instr(isn_T *isn)
10117{
10118 switch (isn->isn_type)
10119 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010120 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010121 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010122 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010123 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010124 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010125 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010126 case ISN_LOADENV:
10127 case ISN_LOADG:
10128 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010129 case ISN_LOADT:
10130 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010131 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010132 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010133 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010134 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010135 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010136 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010137 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010138 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010139 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010140 case ISN_STOREW:
10141 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010142 vim_free(isn->isn_arg.string);
10143 break;
10144
Bram Moolenaar4c137212021-04-19 16:48:48 +020010145 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010146 {
10147 int idx;
10148 isn_T *list = isn->isn_arg.subs.subs_instr;
10149
10150 vim_free(isn->isn_arg.subs.subs_cmd);
10151 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10152 delete_instr(list + idx);
10153 vim_free(list);
10154 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010155 break;
10156
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010157 case ISN_INSTR:
10158 {
10159 int idx;
10160 isn_T *list = isn->isn_arg.instr;
10161
10162 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10163 delete_instr(list + idx);
10164 vim_free(list);
10165 }
10166 break;
10167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010168 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010169 case ISN_STORES:
10170 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010171 break;
10172
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010173 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010174 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010175 vim_free(isn->isn_arg.unlet.ul_name);
10176 break;
10177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010178 case ISN_STOREOPT:
10179 vim_free(isn->isn_arg.storeopt.so_name);
10180 break;
10181
10182 case ISN_PUSHBLOB: // push blob isn_arg.blob
10183 blob_unref(isn->isn_arg.blob);
10184 break;
10185
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010186 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010187#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010188 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010189#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010190 break;
10191
10192 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010193#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010194 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010195#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010196 break;
10197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010198 case ISN_UCALL:
10199 vim_free(isn->isn_arg.ufunc.cuf_name);
10200 break;
10201
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010202 case ISN_FUNCREF:
10203 {
10204 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10205 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010206 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010207
Bram Moolenaar077a4232020-12-22 18:33:27 +010010208 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10209 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010210 }
10211 break;
10212
10213 case ISN_DCALL:
10214 {
10215 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10216 + isn->isn_arg.dfunc.cdf_idx;
10217
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010218 if (dfunc->df_ufunc != NULL
10219 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010220 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010221 }
10222 break;
10223
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010224 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010225 {
10226 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10227 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10228
10229 if (ufunc != NULL)
10230 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010231 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010232 func_ptr_unref(ufunc);
10233 }
10234
10235 vim_free(lambda);
10236 vim_free(isn->isn_arg.newfunc.nf_global);
10237 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010238 break;
10239
Bram Moolenaar5e654232020-09-16 15:22:00 +020010240 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010241 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010242 free_type(isn->isn_arg.type.ct_type);
10243 break;
10244
Bram Moolenaar02194d22020-10-24 23:08:38 +020010245 case ISN_CMDMOD:
10246 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10247 ->cmod_filter_regmatch.regprog);
10248 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10249 break;
10250
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010251 case ISN_LOADSCRIPT:
10252 case ISN_STORESCRIPT:
10253 vim_free(isn->isn_arg.script.scriptref);
10254 break;
10255
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010256 case ISN_TRY:
10257 vim_free(isn->isn_arg.try.try_ref);
10258 break;
10259
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010260 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010261 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010262 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10263 break;
10264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010265 case ISN_2BOOL:
10266 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010267 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010268 case ISN_ADDBLOB:
10269 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010270 case ISN_ANYINDEX:
10271 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010272 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010273 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010274 case ISN_BLOBINDEX:
10275 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010276 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010277 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010278 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010279 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010280 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010281 case ISN_COMPAREANY:
10282 case ISN_COMPAREBLOB:
10283 case ISN_COMPAREBOOL:
10284 case ISN_COMPAREDICT:
10285 case ISN_COMPAREFLOAT:
10286 case ISN_COMPAREFUNC:
10287 case ISN_COMPARELIST:
10288 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010289 case ISN_COMPARESPECIAL:
10290 case ISN_COMPARESTRING:
10291 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010292 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010293 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010294 case ISN_DROP:
10295 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010296 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010297 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010298 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010299 case ISN_EXECCONCAT:
10300 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010301 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010302 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010303 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010304 case ISN_GETITEM:
10305 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010306 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010307 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010308 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010309 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010310 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010311 case ISN_LOADBDICT:
10312 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010313 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010314 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010315 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010316 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010317 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010318 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010319 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010320 case ISN_NEGATENR:
10321 case ISN_NEWDICT:
10322 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010323 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010324 case ISN_OPFLOAT:
10325 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010326 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010327 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010328 case ISN_PROF_END:
10329 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010330 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010331 case ISN_PUSHF:
10332 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010333 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010334 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010335 case ISN_REDIREND:
10336 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010337 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010338 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010339 case ISN_SHUFFLE:
10340 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010341 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010342 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010343 case ISN_STORENR:
10344 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010345 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010346 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010347 case ISN_STOREV:
10348 case ISN_STRINDEX:
10349 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010350 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010351 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010352 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010353 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010354 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010355 // nothing allocated
10356 break;
10357 }
10358}
10359
10360/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010361 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010362 */
10363 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010364delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010365{
10366 int idx;
10367
10368 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010369 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010370
10371 if (dfunc->df_instr != NULL)
10372 {
10373 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10374 delete_instr(dfunc->df_instr + idx);
10375 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010376 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010377 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010378 if (dfunc->df_instr_debug != NULL)
10379 {
10380 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10381 delete_instr(dfunc->df_instr_debug + idx);
10382 VIM_CLEAR(dfunc->df_instr_debug);
10383 dfunc->df_instr_debug = NULL;
10384 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010385#ifdef FEAT_PROFILE
10386 if (dfunc->df_instr_prof != NULL)
10387 {
10388 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10389 delete_instr(dfunc->df_instr_prof + idx);
10390 VIM_CLEAR(dfunc->df_instr_prof);
10391 dfunc->df_instr_prof = NULL;
10392 }
10393#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010394
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010395 if (mark_deleted)
10396 dfunc->df_deleted = TRUE;
10397 if (dfunc->df_ufunc != NULL)
10398 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010399}
10400
10401/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010402 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010403 * function, unless another user function still uses it.
10404 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010405 */
10406 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010407unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010408{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010409 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010410 {
10411 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10412 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010413
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010414 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010415 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010416 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010417 ufunc->uf_dfunc_idx = 0;
10418 if (dfunc->df_ufunc == ufunc)
10419 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010420 }
10421}
10422
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010423/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010424 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010425 */
10426 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010427link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010428{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010429 if (ufunc->uf_dfunc_idx > 0)
10430 {
10431 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10432 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010433
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010434 ++dfunc->df_refcount;
10435 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010436}
10437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010438#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010439/*
10440 * Free all functions defined with ":def".
10441 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010442 void
10443free_def_functions(void)
10444{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010445 int idx;
10446
10447 for (idx = 0; idx < def_functions.ga_len; ++idx)
10448 {
10449 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10450
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010451 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010452 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010453 }
10454
10455 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010456}
10457#endif
10458
10459
10460#endif // FEAT_EVAL