blob: 62d73733be49727cf0d796a0060eeda90402991e [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100551 if (ga_grow(instr, 1) == FAIL)
552 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
588 if (ga_grow(stack, 1) == FAIL)
589 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
1291 if (ga_grow(stack, 1) == FAIL)
1292 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
1593 if (ga_grow(stack, 1) == FAIL)
1594 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
1629 if (ga_grow(stack, 1) == FAIL)
1630 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 Moolenaar8a7d6542020-01-26 15:56:19 +01001657 if (ga_grow(stack, 1) == FAIL)
1658 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
1762 if (ga_grow(stack, 1) == FAIL)
1763 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 Moolenaar8a7d6542020-01-26 15:56:19 +01001844 if (ga_grow(stack, 1) == FAIL)
1845 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
2034 if (ga_grow(stack, 1) == FAIL)
2035 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 Moolenaar26e117e2020-02-04 21:24:15 +01002059 if (ga_grow(stack, 1) == FAIL)
2060 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 Moolenaar0e65d3d2020-05-05 17:53:16 +02002133 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002134 else
2135 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002136 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002137 return FAIL;
2138 }
2139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2141 return FAIL;
2142 isn->isn_arg.pfunc.cpf_top = at_top;
2143 isn->isn_arg.pfunc.cpf_argcount = argcount;
2144
2145 stack->ga_len -= argcount; // drop the arguments
2146
2147 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002148 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002150 // If partial is above the arguments it must be cleared and replaced with
2151 // the return value.
2152 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2153 return FAIL;
2154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 return OK;
2156}
2157
2158/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002159 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 */
2161 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002162generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163{
2164 isn_T *isn;
2165 garray_T *stack = &cctx->ctx_type_stack;
2166 type_T *type;
2167
Bram Moolenaar080457c2020-03-03 21:53:32 +01002168 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002169 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002171 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002173 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002175 if (type->tt_type != VAR_DICT && type != &t_any)
2176 {
2177 emsg(_(e_dictreq));
2178 return FAIL;
2179 }
2180 // change dict type to dict member type
2181 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002182 {
2183 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2184 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2185 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186
2187 return OK;
2188}
2189
2190/*
2191 * Generate an ISN_ECHO instruction.
2192 */
2193 static int
2194generate_ECHO(cctx_T *cctx, int with_white, int count)
2195{
2196 isn_T *isn;
2197
Bram Moolenaar080457c2020-03-03 21:53:32 +01002198 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2200 return FAIL;
2201 isn->isn_arg.echo.echo_with_white = with_white;
2202 isn->isn_arg.echo.echo_count = count;
2203
2204 return OK;
2205}
2206
Bram Moolenaarad39c092020-02-26 18:23:43 +01002207/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002208 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002209 */
2210 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002211generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002212{
2213 isn_T *isn;
2214
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002215 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002216 return FAIL;
2217 isn->isn_arg.number = count;
2218
2219 return OK;
2220}
2221
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002222/*
2223 * Generate an ISN_PUT instruction.
2224 */
2225 static int
2226generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2227{
2228 isn_T *isn;
2229
2230 RETURN_OK_IF_SKIP(cctx);
2231 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2232 return FAIL;
2233 isn->isn_arg.put.put_regname = regname;
2234 isn->isn_arg.put.put_lnum = lnum;
2235 return OK;
2236}
2237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 static int
2239generate_EXEC(cctx_T *cctx, char_u *line)
2240{
2241 isn_T *isn;
2242
Bram Moolenaar080457c2020-03-03 21:53:32 +01002243 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2245 return FAIL;
2246 isn->isn_arg.string = vim_strsave(line);
2247 return OK;
2248}
2249
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002250 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002251generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2252{
2253 isn_T *isn;
2254 garray_T *stack = &cctx->ctx_type_stack;
2255
2256 RETURN_OK_IF_SKIP(cctx);
2257 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2258 return FAIL;
2259 isn->isn_arg.string = vim_strsave(line);
2260
2261 if (ga_grow(stack, 1) == FAIL)
2262 return FAIL;
2263 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2264 ++stack->ga_len;
2265
2266 return OK;
2267}
2268
2269 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002270generate_EXECCONCAT(cctx_T *cctx, int count)
2271{
2272 isn_T *isn;
2273
2274 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2275 return FAIL;
2276 isn->isn_arg.number = count;
2277 return OK;
2278}
2279
Bram Moolenaar08597872020-12-10 19:43:40 +01002280/*
2281 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2282 */
2283 static int
2284generate_RANGE(cctx_T *cctx, char_u *range)
2285{
2286 isn_T *isn;
2287 garray_T *stack = &cctx->ctx_type_stack;
2288
2289 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2290 return FAIL;
2291 isn->isn_arg.string = range;
2292
2293 if (ga_grow(stack, 1) == FAIL)
2294 return FAIL;
2295 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2296 ++stack->ga_len;
2297 return OK;
2298}
2299
Bram Moolenaar792f7862020-11-23 08:31:18 +01002300 static int
2301generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2302{
2303 isn_T *isn;
2304
2305 RETURN_OK_IF_SKIP(cctx);
2306 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2307 return FAIL;
2308 isn->isn_arg.unpack.unp_count = var_count;
2309 isn->isn_arg.unpack.unp_semicolon = semicolon;
2310 return OK;
2311}
2312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002314 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002315 */
2316 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002317generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002318{
2319 isn_T *isn;
2320
Bram Moolenaarfa984412021-03-25 22:15:28 +01002321 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002322 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002323 cctx->ctx_has_cmdmod = TRUE;
2324
2325 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002326 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002327 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2328 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2329 return FAIL;
2330 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002331 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002332 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002333 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002334
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002335 return OK;
2336}
2337
2338 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002339generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002340{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002341 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2342 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002343 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002344 return OK;
2345}
2346
Bram Moolenaarfa984412021-03-25 22:15:28 +01002347 static int
2348misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002349{
2350 garray_T *instr = &cctx->ctx_instr;
2351
Bram Moolenaara91a7132021-03-25 21:12:15 +01002352 if (cctx->ctx_has_cmdmod
2353 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2354 == ISN_CMDMOD)
2355 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002356 emsg(_(e_misplaced_command_modifier));
2357 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002358 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002359 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002360}
2361
2362/*
2363 * Get the index of the current instruction.
2364 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2365 */
2366 static int
2367current_instr_idx(cctx_T *cctx)
2368{
2369 garray_T *instr = &cctx->ctx_instr;
2370 int idx = instr->ga_len;
2371
Bram Moolenaare99d4222021-06-13 14:01:26 +02002372 while (idx > 0)
2373 {
2374 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002375 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002376 {
2377 --idx;
2378 continue;
2379 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002380#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002381 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2382 {
2383 --idx;
2384 continue;
2385 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002386#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002387 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2388 {
2389 --idx;
2390 continue;
2391 }
2392 break;
2393 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002394 return idx;
2395}
2396
Bram Moolenaarf002a412021-01-24 13:34:18 +01002397#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002398 static void
2399may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2400{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002401 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002402 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002403}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002404#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002405
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002406/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002408 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002410 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002411reserve_local(
2412 cctx_T *cctx,
2413 char_u *name,
2414 size_t len,
2415 int isConst,
2416 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002419 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002421 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002423 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002424 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 }
2426
2427 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002428 return NULL;
2429 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002430 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002432 // Every local variable uses the next entry on the stack. We could re-use
2433 // the last ones when leaving a scope, but then variables used in a closure
2434 // might get overwritten. To keep things simple do not re-use stack
2435 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002436 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2437 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002438
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002439 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 lvar->lv_const = isConst;
2441 lvar->lv_type = type;
2442
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002443 // Remember the name for debugging.
2444 if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2445 return NULL;
2446 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2447 vim_strsave(lvar->lv_name);
2448 ++dfunc->df_var_names.ga_len;
2449
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002450 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451}
2452
2453/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002454 * Remove local variables above "new_top".
2455 */
2456 static void
2457unwind_locals(cctx_T *cctx, int new_top)
2458{
2459 if (cctx->ctx_locals.ga_len > new_top)
2460 {
2461 int idx;
2462 lvar_T *lvar;
2463
2464 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2465 {
2466 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2467 vim_free(lvar->lv_name);
2468 }
2469 }
2470 cctx->ctx_locals.ga_len = new_top;
2471}
2472
2473/*
2474 * Free all local variables.
2475 */
2476 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002477free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002478{
2479 unwind_locals(cctx, 0);
2480 ga_clear(&cctx->ctx_locals);
2481}
2482
2483/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002484 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2485 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2486 * error if the variable was defined with :const.
2487 */
2488 static int
2489check_item_writable(svar_T *sv, int check_writable, char_u *name)
2490{
2491 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2492 || (check_writable == ASSIGN_FINAL
2493 && sv->sv_const == ASSIGN_CONST))
2494 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002495 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002496 return FAIL;
2497 }
2498 return OK;
2499}
2500
2501/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002503 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 * Returns the index in "sn_var_vals" if found.
2505 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002506 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 */
2508 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002509get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510{
2511 hashtab_T *ht;
2512 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002513 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002514 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 int idx;
2516
Bram Moolenaare3d46852020-08-29 13:39:17 +02002517 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002519 if (sid == current_sctx.sc_sid)
2520 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002521 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002522
2523 if (sav == NULL)
2524 return -2;
2525 idx = sav->sav_var_vals_idx;
2526 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002527 if (check_item_writable(sv, check_writable, name) == FAIL)
2528 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002529 return idx;
2530 }
2531
2532 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 ht = &SCRIPT_VARS(sid);
2534 di = find_var_in_ht(ht, 0, name, TRUE);
2535 if (di == NULL)
2536 return -2;
2537
2538 // Now find the svar_T index in sn_var_vals.
2539 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2540 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002541 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 if (sv->sv_tv == &di->di_tv)
2543 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002544 if (check_item_writable(sv, check_writable, name) == FAIL)
2545 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 return idx;
2547 }
2548 }
2549 return -1;
2550}
2551
2552/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002553 * Find "name" in imported items of the current script or in "cctx" if not
2554 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 */
2556 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002557find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 int idx;
2560
Bram Moolenaare3d46852020-08-29 13:39:17 +02002561 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002562 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 if (cctx != NULL)
2564 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2565 {
2566 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2567 + idx;
2568
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002569 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2570 : STRLEN(import->imp_name) == len
2571 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 return import;
2573 }
2574
Bram Moolenaarefa94442020-08-08 22:16:00 +02002575 return find_imported_in_script(name, len, current_sctx.sc_sid);
2576}
2577
2578 imported_T *
2579find_imported_in_script(char_u *name, size_t len, int sid)
2580{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002581 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002582 int idx;
2583
Bram Moolenaare3d46852020-08-29 13:39:17 +02002584 if (!SCRIPT_ID_VALID(sid))
2585 return NULL;
2586 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2588 {
2589 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2590
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002591 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2592 : STRLEN(import->imp_name) == len
2593 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 return import;
2595 }
2596 return NULL;
2597}
2598
2599/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002600 * Free all imported variables.
2601 */
2602 static void
2603free_imported(cctx_T *cctx)
2604{
2605 int idx;
2606
2607 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2608 {
2609 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2610
2611 vim_free(import->imp_name);
2612 }
2613 ga_clear(&cctx->ctx_imports);
2614}
2615
2616/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002617 * Return a pointer to the next line that isn't empty or only contains a
2618 * comment. Skips over white space.
2619 * Returns NULL if there is none.
2620 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002621 char_u *
2622peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002623{
2624 int lnum = cctx->ctx_lnum;
2625
2626 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2627 {
2628 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002629 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002630
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002631 // ignore NULLs inserted for continuation lines
2632 if (line != NULL)
2633 {
2634 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002635 if (vim9_bad_comment(p))
2636 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002637 if (*p != NUL && !vim9_comment_start(p))
2638 return p;
2639 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002640 }
2641 return NULL;
2642}
2643
2644/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002645 * Called when checking for a following operator at "arg". When the rest of
2646 * the line is empty or only a comment, peek the next line. If there is a next
2647 * line return a pointer to it and set "nextp".
2648 * Otherwise skip over white space.
2649 */
2650 static char_u *
2651may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2652{
2653 char_u *p = skipwhite(arg);
2654
2655 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002656 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002657 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002658 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002659 if (*nextp != NULL)
2660 return *nextp;
2661 }
2662 return p;
2663}
2664
2665/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002666 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002667 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002668 * Returns NULL when at the end.
2669 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002670 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002671next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002672{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002673 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002674
2675 do
2676 {
2677 ++cctx->ctx_lnum;
2678 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002679 {
2680 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002681 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002682 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002683 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002684 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002685 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002686 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002687 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002688 return line;
2689}
2690
2691/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002692 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002693 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002694 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002695 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2696 */
2697 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002698may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002699{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002700 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002701 if (vim9_bad_comment(*arg))
2702 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002703 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002704 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002705 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002706
2707 if (next == NULL)
2708 return FAIL;
2709 *arg = skipwhite(next);
2710 }
2711 return OK;
2712}
2713
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002714/*
2715 * Idem, and give an error when failed.
2716 */
2717 static int
2718may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2719{
2720 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2721 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002722 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002723 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002724 return FAIL;
2725 }
2726 return OK;
2727}
2728
2729
Bram Moolenaara5565e42020-05-09 15:44:01 +02002730// Structure passed between the compile_expr* functions to keep track of
2731// constants that have been parsed but for which no code was produced yet. If
2732// possible expressions on these constants are applied at compile time. If
2733// that is not possible, the code to push the constants needs to be generated
2734// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002735// Using 50 should be more than enough of 5 levels of ().
2736#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002737typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002738 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002739 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002740 int pp_is_const; // all generated code was constants, used for a
2741 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002742} ppconst_T;
2743
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002744static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002745static int compile_expr0(char_u **arg, cctx_T *cctx);
2746static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2747
Bram Moolenaara5565e42020-05-09 15:44:01 +02002748/*
2749 * Generate a PUSH instruction for "tv".
2750 * "tv" will be consumed or cleared.
2751 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2752 */
2753 static int
2754generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2755{
2756 if (tv != NULL)
2757 {
2758 switch (tv->v_type)
2759 {
2760 case VAR_UNKNOWN:
2761 break;
2762 case VAR_BOOL:
2763 generate_PUSHBOOL(cctx, tv->vval.v_number);
2764 break;
2765 case VAR_SPECIAL:
2766 generate_PUSHSPEC(cctx, tv->vval.v_number);
2767 break;
2768 case VAR_NUMBER:
2769 generate_PUSHNR(cctx, tv->vval.v_number);
2770 break;
2771#ifdef FEAT_FLOAT
2772 case VAR_FLOAT:
2773 generate_PUSHF(cctx, tv->vval.v_float);
2774 break;
2775#endif
2776 case VAR_BLOB:
2777 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2778 tv->vval.v_blob = NULL;
2779 break;
2780 case VAR_STRING:
2781 generate_PUSHS(cctx, tv->vval.v_string);
2782 tv->vval.v_string = NULL;
2783 break;
2784 default:
2785 iemsg("constant type not supported");
2786 clear_tv(tv);
2787 return FAIL;
2788 }
2789 tv->v_type = VAR_UNKNOWN;
2790 }
2791 return OK;
2792}
2793
2794/*
2795 * Generate code for any ppconst entries.
2796 */
2797 static int
2798generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2799{
2800 int i;
2801 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002802 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002803
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002804 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002805 for (i = 0; i < ppconst->pp_used; ++i)
2806 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2807 ret = FAIL;
2808 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002809 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002810 return ret;
2811}
2812
2813/*
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002814 * Check that the last item of "ppconst" is a bool.
2815 */
2816 static int
2817check_ppconst_bool(ppconst_T *ppconst)
2818{
2819 if (ppconst->pp_used > 0)
2820 {
2821 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002822 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002823
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002824 return check_typval_type(&t_bool, tv, where);
2825 }
2826 return OK;
2827}
2828
2829/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002830 * Clear ppconst constants. Used when failing.
2831 */
2832 static void
2833clear_ppconst(ppconst_T *ppconst)
2834{
2835 int i;
2836
2837 for (i = 0; i < ppconst->pp_used; ++i)
2838 clear_tv(&ppconst->pp_tv[i]);
2839 ppconst->pp_used = 0;
2840}
2841
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002842/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002843 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002844 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002845 */
2846 static int
2847compile_member(int is_slice, cctx_T *cctx)
2848{
2849 type_T **typep;
2850 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002851 vartype_T vartype;
2852 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002853
Bram Moolenaar261417b2021-05-06 21:04:55 +02002854 // We can index a list, dict and blob. If we don't know the type
2855 // we can use the index value type. If we still don't know use an "ANY"
2856 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002857 typep = ((type_T **)stack->ga_data) + stack->ga_len
2858 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002859 vartype = (*typep)->tt_type;
2860 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002861 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002862 if (*typep == &t_any && idxtype == &t_string)
2863 vartype = VAR_DICT;
2864 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002865 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002866 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002867 return FAIL;
2868 if (is_slice)
2869 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002870 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2871 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002872 FALSE, FALSE) == FAIL)
2873 return FAIL;
2874 }
2875 }
2876
Bram Moolenaar261417b2021-05-06 21:04:55 +02002877 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002878 {
2879 if (is_slice)
2880 {
2881 emsg(_(e_cannot_slice_dictionary));
2882 return FAIL;
2883 }
2884 if ((*typep)->tt_type == VAR_DICT)
2885 {
2886 *typep = (*typep)->tt_member;
2887 if (*typep == &t_unknown)
2888 // empty dict was used
2889 *typep = &t_any;
2890 }
2891 else
2892 {
2893 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2894 FALSE, FALSE) == FAIL)
2895 return FAIL;
2896 *typep = &t_any;
2897 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002898 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002899 return FAIL;
2900 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2901 return FAIL;
2902 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002903 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002904 {
2905 *typep = &t_string;
2906 if ((is_slice
2907 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2908 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2909 return FAIL;
2910 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002911 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002912 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002913 if (is_slice)
2914 {
2915 *typep = &t_blob;
2916 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2917 return FAIL;
2918 }
2919 else
2920 {
2921 *typep = &t_number;
2922 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2923 return FAIL;
2924 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002925 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002926 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002927 {
2928 if (is_slice)
2929 {
2930 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002931 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002932 2) == FAIL)
2933 return FAIL;
2934 }
2935 else
2936 {
2937 if ((*typep)->tt_type == VAR_LIST)
2938 {
2939 *typep = (*typep)->tt_member;
2940 if (*typep == &t_unknown)
2941 // empty list was used
2942 *typep = &t_any;
2943 }
2944 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002945 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2946 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002947 return FAIL;
2948 }
2949 }
2950 else
2951 {
2952 emsg(_(e_string_list_dict_or_blob_required));
2953 return FAIL;
2954 }
2955 return OK;
2956}
2957
2958/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002959 * Generate an instruction to load script-local variable "name", without the
2960 * leading "s:".
2961 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 */
2963 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002964compile_load_scriptvar(
2965 cctx_T *cctx,
2966 char_u *name, // variable NUL terminated
2967 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002968 char_u **end, // end of variable
2969 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002971 scriptitem_T *si;
2972 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 imported_T *import;
2974
Bram Moolenaare3d46852020-08-29 13:39:17 +02002975 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2976 return FAIL;
2977 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002978 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002979 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002981 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002982 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2983 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 }
2985 if (idx >= 0)
2986 {
2987 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2988
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002989 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 current_sctx.sc_sid, idx, sv->sv_type);
2991 return OK;
2992 }
2993
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002994 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 if (import != NULL)
2996 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002997 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002998 {
2999 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003000 char_u *exp_name;
3001 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003002 ufunc_T *ufunc;
3003 type_T *type;
3004
3005 // Used "import * as Name", need to lookup the member.
3006 if (*p != '.')
3007 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003008 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003009 return FAIL;
3010 }
3011 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003012 if (VIM_ISWHITE(*p))
3013 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003014 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003015 return FAIL;
3016 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003017
Bram Moolenaar1c991142020-07-04 13:15:31 +02003018 // isolate one name
3019 exp_name = p;
3020 while (eval_isnamec(*p))
3021 ++p;
3022 cc = *p;
3023 *p = NUL;
3024
Bram Moolenaaredba7072021-03-13 21:14:18 +01003025 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3026 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003027 *p = cc;
3028 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003029 *end = p;
3030
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003031 if (idx < 0)
3032 {
3033 if (*p == '(' && ufunc != NULL)
3034 {
3035 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3036 return OK;
3037 }
3038 return FAIL;
3039 }
3040
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003041 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3042 import->imp_sid,
3043 idx,
3044 type);
3045 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003046 else if (import->imp_funcname != NULL)
3047 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003048 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003049 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3050 import->imp_sid,
3051 import->imp_var_vals_idx,
3052 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 return OK;
3054 }
3055
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003056 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003057 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 return FAIL;
3059}
3060
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003061 static int
3062generate_funcref(cctx_T *cctx, char_u *name)
3063{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003064 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003065
3066 if (ufunc == NULL)
3067 return FAIL;
3068
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003069 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003070 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3071 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003072 == FAIL)
3073 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003074 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003075}
3076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077/*
3078 * Compile a variable name into a load instruction.
3079 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003080 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 * When "error" is FALSE do not give an error when not found.
3082 */
3083 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003084compile_load(
3085 char_u **arg,
3086 char_u *end_arg,
3087 cctx_T *cctx,
3088 int is_expr,
3089 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090{
3091 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003092 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003093 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003095 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096
3097 if (*(*arg + 1) == ':')
3098 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003099 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003100 {
3101 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102
Bram Moolenaarfa596382021-04-07 21:58:16 +02003103 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003104 switch (**arg)
3105 {
3106 case 'g': isn_type = ISN_LOADGDICT; break;
3107 case 'w': isn_type = ISN_LOADWDICT; break;
3108 case 't': isn_type = ISN_LOADTDICT; break;
3109 case 'b': isn_type = ISN_LOADBDICT; break;
3110 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003111 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003112 goto theend;
3113 }
3114 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3115 goto theend;
3116 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 else
3119 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003120 isntype_T isn_type = ISN_DROP;
3121
Bram Moolenaarfa596382021-04-07 21:58:16 +02003122 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003123 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3124 if (name == NULL)
3125 return FAIL;
3126
3127 switch (**arg)
3128 {
3129 case 'v': res = generate_LOADV(cctx, name, error);
3130 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003131 case 's': if (is_expr && ASCII_ISUPPER(*name)
3132 && find_func(name, FALSE, cctx) != NULL)
3133 res = generate_funcref(cctx, name);
3134 else
3135 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003136 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003137 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003138 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003139 {
3140 if (is_expr && ASCII_ISUPPER(*name)
3141 && find_func(name, FALSE, cctx) != NULL)
3142 res = generate_funcref(cctx, name);
3143 else
3144 isn_type = ISN_LOADG;
3145 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003146 else
3147 {
3148 isn_type = ISN_LOADAUTO;
3149 vim_free(name);
3150 name = vim_strnsave(*arg, end - *arg);
3151 if (name == NULL)
3152 return FAIL;
3153 }
3154 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003155 case 'w': isn_type = ISN_LOADW; break;
3156 case 't': isn_type = ISN_LOADT; break;
3157 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003158 default: // cannot happen, just in case
3159 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003160 goto theend;
3161 }
3162 if (isn_type != ISN_DROP)
3163 {
3164 // Global, Buffer-local, Window-local and Tabpage-local
3165 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003166 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003167 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 }
3170 }
3171 else
3172 {
3173 size_t len = end - *arg;
3174 int idx;
3175 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003176 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177
3178 name = vim_strnsave(*arg, end - *arg);
3179 if (name == NULL)
3180 return FAIL;
3181
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003182 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3183 {
3184 script_autoload(name, FALSE);
3185 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3186 }
3187 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3188 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003190 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003191 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 }
3193 else
3194 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003195 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003196
Bram Moolenaar709664c2020-12-12 14:33:41 +01003197 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003199 type = lvar.lv_type;
3200 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003201 if (lvar.lv_from_outer != 0)
3202 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003203 else
3204 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 }
3206 else
3207 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003208 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003209 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003210 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003211 || find_imported(name, 0, cctx) != NULL)
3212 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003213
Bram Moolenaar0f769812020-09-12 18:32:34 +02003214 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003215 // uppercase letter it can be a user defined function.
3216 // generate_funcref() will fail if the function can't be found.
3217 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003218 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 }
3220 }
3221 if (gen_load)
3222 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003223 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003224 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003225 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003226 cctx->ctx_outer_used = TRUE;
3227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 }
3229
3230 *arg = end;
3231
3232theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003233 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003234 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 vim_free(name);
3236 return res;
3237}
3238
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003239 static void
3240clear_instr_ga(garray_T *gap)
3241{
3242 int idx;
3243
3244 for (idx = 0; idx < gap->ga_len; ++idx)
3245 delete_instr(((isn_T *)gap->ga_data) + idx);
3246 ga_clear(gap);
3247}
3248
3249/*
3250 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3251 * Returns FAIL if compilation fails.
3252 */
3253 static int
3254compile_string(isn_T *isn, cctx_T *cctx)
3255{
3256 char_u *s = isn->isn_arg.string;
3257 garray_T save_ga = cctx->ctx_instr;
3258 int expr_res;
3259 int trailing_error;
3260 int instr_count;
3261 isn_T *instr = NULL;
3262
Bram Moolenaarcd268012021-07-22 19:11:08 +02003263 // Remove the string type from the stack.
3264 --cctx->ctx_type_stack.ga_len;
3265
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003266 // Temporarily reset the list of instructions so that the jump labels are
3267 // correct.
3268 cctx->ctx_instr.ga_len = 0;
3269 cctx->ctx_instr.ga_maxlen = 0;
3270 cctx->ctx_instr.ga_data = NULL;
3271 expr_res = compile_expr0(&s, cctx);
3272 s = skipwhite(s);
3273 trailing_error = *s != NUL;
3274
Bram Moolenaarff652882021-05-16 15:24:49 +02003275 if (expr_res == FAIL || trailing_error
3276 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003277 {
3278 if (trailing_error)
3279 semsg(_(e_trailing_arg), s);
3280 clear_instr_ga(&cctx->ctx_instr);
3281 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003282 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003283 return FAIL;
3284 }
3285
3286 // Move the generated instructions into the ISN_INSTR instruction, then
3287 // restore the list of instructions.
3288 instr_count = cctx->ctx_instr.ga_len;
3289 instr = cctx->ctx_instr.ga_data;
3290 instr[instr_count].isn_type = ISN_FINISH;
3291
3292 cctx->ctx_instr = save_ga;
3293 vim_free(isn->isn_arg.string);
3294 isn->isn_type = ISN_INSTR;
3295 isn->isn_arg.instr = instr;
3296 return OK;
3297}
3298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299/*
3300 * Compile the argument expressions.
3301 * "arg" points to just after the "(" and is advanced to after the ")"
3302 */
3303 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003304compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003306 char_u *p = *arg;
3307 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003308 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003309 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310
Bram Moolenaare6085c52020-04-12 20:19:16 +02003311 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003313 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3314 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003315 if (*p == ')')
3316 {
3317 *arg = p + 1;
3318 return OK;
3319 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003320 if (must_end)
3321 {
3322 semsg(_(e_missing_comma_before_argument_str), p);
3323 return FAIL;
3324 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003325
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003326 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003327 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 return FAIL;
3329 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003330
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003331 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003332 && cctx->ctx_instr.ga_len == instr_count + 1)
3333 {
3334 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3335
3336 // {skip} argument of searchpair() can be compiled if not empty
3337 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3338 compile_string(isn, cctx);
3339 }
3340
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003341 if (*p != ',' && *skipwhite(p) == ',')
3342 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003343 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003344 p = skipwhite(p);
3345 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003347 {
3348 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003349 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003350 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003351 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003352 else
3353 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003354 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003355 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003357failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003358 emsg(_(e_missing_close));
3359 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360}
3361
3362/*
3363 * Compile a function call: name(arg1, arg2)
3364 * "arg" points to "name", "arg + varlen" to the "(".
3365 * "argcount_init" is 1 for "value->method()"
3366 * Instructions:
3367 * EVAL arg1
3368 * EVAL arg2
3369 * BCALL / DCALL / UCALL
3370 */
3371 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003372compile_call(
3373 char_u **arg,
3374 size_t varlen,
3375 cctx_T *cctx,
3376 ppconst_T *ppconst,
3377 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378{
3379 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003380 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 int argcount = argcount_init;
3382 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003383 char_u fname_buf[FLEN_FIXED + 1];
3384 char_u *tofree = NULL;
3385 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003386 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003387 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003388 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003389 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390
Bram Moolenaara5565e42020-05-09 15:44:01 +02003391 // we can evaluate "has('name')" at compile time
3392 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3393 {
3394 char_u *s = skipwhite(*arg + varlen + 1);
3395 typval_T argvars[2];
3396
3397 argvars[0].v_type = VAR_UNKNOWN;
3398 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003399 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003400 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003401 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003402 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003403 if (*s == ')' && argvars[0].v_type == VAR_STRING
3404 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003405 {
3406 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3407
3408 *arg = s + 1;
3409 argvars[1].v_type = VAR_UNKNOWN;
3410 tv->v_type = VAR_NUMBER;
3411 tv->vval.v_number = 0;
3412 f_has(argvars, tv);
3413 clear_tv(&argvars[0]);
3414 ++ppconst->pp_used;
3415 return OK;
3416 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003417 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003418 }
3419
3420 if (generate_ppconst(cctx, ppconst) == FAIL)
3421 return FAIL;
3422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 if (varlen >= sizeof(namebuf))
3424 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003425 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 return FAIL;
3427 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003428 vim_strncpy(namebuf, *arg, varlen);
3429 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003431 // We handle the "skip" argument of searchpair() and searchpairpos()
3432 // differently.
3433 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3434 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3435 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3436 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003439 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003440 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441
Bram Moolenaar03290b82020-12-19 16:30:44 +01003442 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003443 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 {
3445 int idx;
3446
3447 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003448 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003450 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003451 if (STRCMP(name, "flatten") == 0)
3452 {
3453 emsg(_(e_cannot_use_flatten_in_vim9_script));
3454 goto theend;
3455 }
3456
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003457 if (STRCMP(name, "add") == 0 && argcount == 2)
3458 {
3459 garray_T *stack = &cctx->ctx_type_stack;
3460 type_T *type = ((type_T **)stack->ga_data)[
3461 stack->ga_len - 2];
3462
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003463 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003464 if (type->tt_type == VAR_LIST)
3465 {
3466 // inline "add(list, item)" so that the type can be checked
3467 res = generate_LISTAPPEND(cctx);
3468 idx = -1;
3469 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003470 else if (type->tt_type == VAR_BLOB)
3471 {
3472 // inline "add(blob, nr)" so that the type can be checked
3473 res = generate_BLOBAPPEND(cctx);
3474 idx = -1;
3475 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003476 }
3477
3478 if (idx >= 0)
3479 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3480 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003481 else
3482 semsg(_(e_unknownfunc), namebuf);
3483 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 }
3485
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003486 // An argument or local variable can be a function reference, this
3487 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003488 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003489 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003490 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003491 // If we can find the function by name generate the right call.
3492 // Skip global functions here, a local funcref takes precedence.
3493 ufunc = find_func(name, FALSE, cctx);
3494 if (ufunc != NULL && !func_is_global(ufunc))
3495 {
3496 res = generate_CALL(cctx, ufunc, argcount);
3497 goto theend;
3498 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003499 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500
3501 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003502 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003503 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003505 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003506 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003507 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003508 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003509 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003510
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003511 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003512 goto theend;
3513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514
Bram Moolenaar0f769812020-09-12 18:32:34 +02003515 // If we can find a global function by name generate the right call.
3516 if (ufunc != NULL)
3517 {
3518 res = generate_CALL(cctx, ufunc, argcount);
3519 goto theend;
3520 }
3521
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003522 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003523 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003524 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003525 res = generate_UCALL(cctx, name, argcount);
3526 else
3527 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003528
3529theend:
3530 vim_free(tofree);
3531 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532}
3533
3534// like NAMESPACE_CHAR but with 'a' and 'l'.
3535#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3536
3537/*
3538 * Find the end of a variable or function name. Unlike find_name_end() this
3539 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003540 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 * Return a pointer to just after the name. Equal to "arg" if there is no
3542 * valid name.
3543 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003544 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003545to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546{
3547 char_u *p;
3548
3549 // Quick check for valid starting character.
3550 if (!eval_isnamec1(*arg))
3551 return arg;
3552
3553 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3554 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3555 // and can be used in slice "[n:]".
3556 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003557 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3559 break;
3560 return p;
3561}
3562
3563/*
3564 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003565 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 */
3567 char_u *
3568to_name_const_end(char_u *arg)
3569{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003570 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 typval_T rettv;
3572
3573 if (p == arg && *arg == '[')
3574 {
3575
3576 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003577 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 p = arg;
3579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 return p;
3581}
3582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583/*
3584 * parse a list: [expr, expr]
3585 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003586 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 */
3588 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003589compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590{
3591 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003592 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003594 int is_const;
3595 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003597 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003599 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003600 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003601 semsg(_(e_list_end), *arg);
3602 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003603 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003604 if (*p == ',')
3605 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003606 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003607 return FAIL;
3608 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003609 if (*p == ']')
3610 {
3611 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003612 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003613 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003614 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003615 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003616 if (!is_const)
3617 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 ++count;
3619 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003620 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003622 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3623 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003624 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003625 return FAIL;
3626 }
3627 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003628 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 p = skipwhite(p);
3630 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003631 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003633 ppconst->pp_is_const = is_all_const;
3634 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635}
3636
3637/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003638 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003639 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003640 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 */
3642 static int
3643compile_lambda(char_u **arg, cctx_T *cctx)
3644{
Bram Moolenaare462f522020-12-27 14:43:30 +01003645 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 typval_T rettv;
3647 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003648 evalarg_T evalarg;
3649
3650 CLEAR_FIELD(evalarg);
3651 evalarg.eval_flags = EVAL_EVALUATE;
3652 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653
3654 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003655 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3656 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003657 {
3658 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003659 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003660 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003661
Bram Moolenaar65c44152020-12-24 15:14:01 +01003662 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003664 ++ufunc->uf_refcount;
3665 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666
Bram Moolenaara9931532021-06-12 15:58:16 +02003667 // Compile it here to get the return type. The return type is optional,
3668 // when it's missing use t_unknown. This is recognized in
3669 // compile_return().
3670 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3671 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003672 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673
Bram Moolenaar648594e2021-07-11 17:55:01 +02003674#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003675 // When the outer function is compiled for profiling, the lambda may be
3676 // called without profiling. Compile it here in the right context.
3677 if (cctx->ctx_compile_type == CT_PROFILE)
3678 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003679#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003680
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003681 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3682 // points into it. Point to the original line to avoid a dangling pointer.
3683 if (evalarg.eval_tofree_cmdline != NULL)
3684 {
3685 size_t off = *arg - evalarg.eval_tofree_cmdline;
3686
3687 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3688 + off;
3689 }
3690
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003691 clear_evalarg(&evalarg, NULL);
3692
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003693 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003694 {
3695 // The return type will now be known.
3696 set_function_type(ufunc);
3697
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003698 // The function reference count will be 1. When the ISN_FUNCREF
3699 // instruction is deleted the reference count is decremented and the
3700 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003701 return generate_FUNCREF(cctx, ufunc);
3702 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003703
3704 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 return FAIL;
3706}
3707
3708/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003709 * Get a lambda and compile it. Uses Vim9 syntax.
3710 */
3711 int
3712get_lambda_tv_and_compile(
3713 char_u **arg,
3714 typval_T *rettv,
3715 int types_optional,
3716 evalarg_T *evalarg)
3717{
3718 int r;
3719 ufunc_T *ufunc;
3720 int save_sc_version = current_sctx.sc_version;
3721
3722 // Get the funcref in "rettv".
3723 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3724 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3725 current_sctx.sc_version = save_sc_version;
3726 if (r != OK)
3727 return r;
3728
3729 // "rettv" will now be a partial referencing the function.
3730 ufunc = rettv->vval.v_partial->pt_func;
3731
3732 // Compile it here to get the return type. The return type is optional,
3733 // when it's missing use t_unknown. This is recognized in
3734 // compile_return().
3735 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3736 ufunc->uf_ret_type = &t_unknown;
3737 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3738
3739 if (ufunc->uf_def_status == UF_COMPILED)
3740 {
3741 // The return type will now be known.
3742 set_function_type(ufunc);
3743 return OK;
3744 }
3745 clear_tv(rettv);
3746 return FAIL;
3747}
3748
3749/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003750 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003752 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 */
3754 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003755compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756{
3757 garray_T *instr = &cctx->ctx_instr;
3758 int count = 0;
3759 dict_T *d = dict_alloc();
3760 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003761 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003762 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003763 int is_const;
3764 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765
3766 if (d == NULL)
3767 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003768 if (generate_ppconst(cctx, ppconst) == FAIL)
3769 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003770 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003772 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773
Bram Moolenaar23c55272020-06-21 16:58:13 +02003774 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003775 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003776 *arg = NULL;
3777 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003778 }
3779
3780 if (**arg == '}')
3781 break;
3782
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003783 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003785 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003787 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003788 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003789 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003792 if (isn->isn_type == ISN_PUSHNR)
3793 {
3794 char buf[NUMBUFLEN];
3795
3796 // Convert to string at compile time.
3797 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3798 isn->isn_type = ISN_PUSHS;
3799 isn->isn_arg.string = vim_strsave((char_u *)buf);
3800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 if (isn->isn_type == ISN_PUSHS)
3802 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003803 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003804 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003805 *arg = skipwhite(*arg);
3806 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003807 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003808 emsg(_(e_missing_matching_bracket_after_dict_key));
3809 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003810 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003811 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003813 else
3814 {
3815 // {"name": value},
3816 // {'name': value},
3817 // {name: value} use "name" as a literal key
3818 key = get_literal_key(arg);
3819 if (key == NULL)
3820 return FAIL;
3821 if (generate_PUSHS(cctx, key) == FAIL)
3822 return FAIL;
3823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824
3825 // Check for duplicate keys, if using string keys.
3826 if (key != NULL)
3827 {
3828 item = dict_find(d, key, -1);
3829 if (item != NULL)
3830 {
3831 semsg(_(e_duplicate_key), key);
3832 goto failret;
3833 }
3834 item = dictitem_alloc(key);
3835 if (item != NULL)
3836 {
3837 item->di_tv.v_type = VAR_UNKNOWN;
3838 item->di_tv.v_lock = 0;
3839 if (dict_add(d, item) == FAIL)
3840 dictitem_free(item);
3841 }
3842 }
3843
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 if (**arg != ':')
3845 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003846 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003847 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003848 else
3849 semsg(_(e_missing_dict_colon), *arg);
3850 return FAIL;
3851 }
3852 whitep = *arg + 1;
3853 if (!IS_WHITE_OR_NUL(*whitep))
3854 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003855 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 return FAIL;
3857 }
3858
Bram Moolenaar23c55272020-06-21 16:58:13 +02003859 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003860 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003861 *arg = NULL;
3862 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003863 }
3864
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003865 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003867 if (!is_const)
3868 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 ++count;
3870
Bram Moolenaar2c330432020-04-13 14:41:35 +02003871 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003872 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003873 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003874 *arg = NULL;
3875 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 if (**arg == '}')
3878 break;
3879 if (**arg != ',')
3880 {
3881 semsg(_(e_missing_dict_comma), *arg);
3882 goto failret;
3883 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003884 if (IS_WHITE_OR_NUL(*whitep))
3885 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003886 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003887 return FAIL;
3888 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003889 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003890 if (!IS_WHITE_OR_NUL(*whitep))
3891 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003892 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003893 return FAIL;
3894 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003895 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 }
3897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 *arg = *arg + 1;
3899
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003900 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003901 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003902 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003903 *arg += STRLEN(*arg);
3904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003906 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 return generate_NEWDICT(cctx, count);
3908
3909failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003910 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003911 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003912 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003913 *arg = (char_u *)"";
3914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 dict_unref(d);
3916 return FAIL;
3917}
3918
3919/*
3920 * Compile "&option".
3921 */
3922 static int
3923compile_get_option(char_u **arg, cctx_T *cctx)
3924{
3925 typval_T rettv;
3926 char_u *start = *arg;
3927 int ret;
3928
3929 // parse the option and get the current value to get the type.
3930 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003931 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 if (ret == OK)
3933 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003934 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003935 char_u *name = vim_strnsave(start, *arg - start);
3936 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3937 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938
3939 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3940 vim_free(name);
3941 }
3942 clear_tv(&rettv);
3943
3944 return ret;
3945}
3946
3947/*
3948 * Compile "$VAR".
3949 */
3950 static int
3951compile_get_env(char_u **arg, cctx_T *cctx)
3952{
3953 char_u *start = *arg;
3954 int len;
3955 int ret;
3956 char_u *name;
3957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 ++*arg;
3959 len = get_env_len(arg);
3960 if (len == 0)
3961 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003962 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 return FAIL;
3964 }
3965
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003966 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 name = vim_strnsave(start, len + 1);
3968 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3969 vim_free(name);
3970 return ret;
3971}
3972
3973/*
3974 * Compile "@r".
3975 */
3976 static int
3977compile_get_register(char_u **arg, cctx_T *cctx)
3978{
3979 int ret;
3980
3981 ++*arg;
3982 if (**arg == NUL)
3983 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003984 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 return FAIL;
3986 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003987 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 {
3989 emsg_invreg(**arg);
3990 return FAIL;
3991 }
3992 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3993 ++*arg;
3994 return ret;
3995}
3996
3997/*
3998 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003999 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 */
4001 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004002apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004004 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005
4006 // this works from end to start
4007 while (p > start)
4008 {
4009 --p;
4010 if (*p == '-' || *p == '+')
4011 {
4012 // only '-' has an effect, for '+' we only check the type
4013#ifdef FEAT_FLOAT
4014 if (rettv->v_type == VAR_FLOAT)
4015 {
4016 if (*p == '-')
4017 rettv->vval.v_float = -rettv->vval.v_float;
4018 }
4019 else
4020#endif
4021 {
4022 varnumber_T val;
4023 int error = FALSE;
4024
4025 // tv_get_number_chk() accepts a string, but we don't want that
4026 // here
4027 if (check_not_string(rettv) == FAIL)
4028 return FAIL;
4029 val = tv_get_number_chk(rettv, &error);
4030 clear_tv(rettv);
4031 if (error)
4032 return FAIL;
4033 if (*p == '-')
4034 val = -val;
4035 rettv->v_type = VAR_NUMBER;
4036 rettv->vval.v_number = val;
4037 }
4038 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004039 else if (numeric_only)
4040 {
4041 ++p;
4042 break;
4043 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004044 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 {
4046 int v = tv2bool(rettv);
4047
4048 // '!' is permissive in the type.
4049 clear_tv(rettv);
4050 rettv->v_type = VAR_BOOL;
4051 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4052 }
4053 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004054 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 return OK;
4056}
4057
4058/*
4059 * Recognize v: variables that are constants and set "rettv".
4060 */
4061 static void
4062get_vim_constant(char_u **arg, typval_T *rettv)
4063{
4064 if (STRNCMP(*arg, "v:true", 6) == 0)
4065 {
4066 rettv->v_type = VAR_BOOL;
4067 rettv->vval.v_number = VVAL_TRUE;
4068 *arg += 6;
4069 }
4070 else if (STRNCMP(*arg, "v:false", 7) == 0)
4071 {
4072 rettv->v_type = VAR_BOOL;
4073 rettv->vval.v_number = VVAL_FALSE;
4074 *arg += 7;
4075 }
4076 else if (STRNCMP(*arg, "v:null", 6) == 0)
4077 {
4078 rettv->v_type = VAR_SPECIAL;
4079 rettv->vval.v_number = VVAL_NULL;
4080 *arg += 6;
4081 }
4082 else if (STRNCMP(*arg, "v:none", 6) == 0)
4083 {
4084 rettv->v_type = VAR_SPECIAL;
4085 rettv->vval.v_number = VVAL_NONE;
4086 *arg += 6;
4087 }
4088}
4089
Bram Moolenaar657137c2021-01-09 15:45:23 +01004090 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004091get_compare_type(char_u *p, int *len, int *type_is)
4092{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004093 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004094 int i;
4095
4096 switch (p[0])
4097 {
4098 case '=': if (p[1] == '=')
4099 type = EXPR_EQUAL;
4100 else if (p[1] == '~')
4101 type = EXPR_MATCH;
4102 break;
4103 case '!': if (p[1] == '=')
4104 type = EXPR_NEQUAL;
4105 else if (p[1] == '~')
4106 type = EXPR_NOMATCH;
4107 break;
4108 case '>': if (p[1] != '=')
4109 {
4110 type = EXPR_GREATER;
4111 *len = 1;
4112 }
4113 else
4114 type = EXPR_GEQUAL;
4115 break;
4116 case '<': if (p[1] != '=')
4117 {
4118 type = EXPR_SMALLER;
4119 *len = 1;
4120 }
4121 else
4122 type = EXPR_SEQUAL;
4123 break;
4124 case 'i': if (p[1] == 's')
4125 {
4126 // "is" and "isnot"; but not a prefix of a name
4127 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4128 *len = 5;
4129 i = p[*len];
4130 if (!isalnum(i) && i != '_')
4131 {
4132 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4133 *type_is = TRUE;
4134 }
4135 }
4136 break;
4137 }
4138 return type;
4139}
4140
4141/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004142 * Skip over an expression, ignoring most errors.
4143 */
4144 static void
4145skip_expr_cctx(char_u **arg, cctx_T *cctx)
4146{
4147 evalarg_T evalarg;
4148
4149 CLEAR_FIELD(evalarg);
4150 evalarg.eval_cctx = cctx;
4151 skip_expr(arg, &evalarg);
4152}
4153
4154/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004156 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 */
4158 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004159compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004161 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162
4163 // this works from end to start
4164 while (p > start)
4165 {
4166 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004167 while (VIM_ISWHITE(*p))
4168 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 if (*p == '-' || *p == '+')
4170 {
4171 int negate = *p == '-';
4172 isn_T *isn;
4173
4174 // TODO: check type
4175 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4176 {
4177 --p;
4178 if (*p == '-')
4179 negate = !negate;
4180 }
4181 // only '-' has an effect, for '+' we only check the type
4182 if (negate)
4183 isn = generate_instr(cctx, ISN_NEGATENR);
4184 else
4185 isn = generate_instr(cctx, ISN_CHECKNR);
4186 if (isn == NULL)
4187 return FAIL;
4188 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004189 else if (numeric_only)
4190 {
4191 ++p;
4192 break;
4193 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 else
4195 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004196 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004198 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004200 if (p[-1] == '!')
4201 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004204 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 return FAIL;
4206 }
4207 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004208 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 return OK;
4210}
4211
4212/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004213 * Compile "(expression)": recursive!
4214 * Return FAIL/OK.
4215 */
4216 static int
4217compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4218{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004219 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004220 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004221
Bram Moolenaar24156692021-01-14 20:35:49 +01004222 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4223 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004224 if (ppconst->pp_used <= PPSIZE - 10)
4225 {
4226 ret = compile_expr1(arg, cctx, ppconst);
4227 }
4228 else
4229 {
4230 // Not enough space in ppconst, flush constants.
4231 if (generate_ppconst(cctx, ppconst) == FAIL)
4232 return FAIL;
4233 ret = compile_expr0(arg, cctx);
4234 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004235 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4236 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004237 if (**arg == ')')
4238 ++*arg;
4239 else if (ret == OK)
4240 {
4241 emsg(_(e_missing_close));
4242 ret = FAIL;
4243 }
4244 return ret;
4245}
4246
4247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004249 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 */
4251 static int
4252compile_subscript(
4253 char_u **arg,
4254 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004255 char_u *start_leader,
4256 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004257 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004259 char_u *name_start = *end_leader;
4260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 for (;;)
4262 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004263 char_u *p = skipwhite(*arg);
4264
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004265 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004266 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004267 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004268
4269 // If a following line starts with "->{" or "->X" advance to that
4270 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004271 // Also if a following line starts with ".x".
4272 if (next != NULL &&
4273 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004274 && (next[2] == '{'
4275 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004276 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004277 {
4278 next = next_line_from_context(cctx, TRUE);
4279 if (next == NULL)
4280 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004281 *arg = next;
4282 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004283 }
4284 }
4285
Bram Moolenaarcd268012021-07-22 19:11:08 +02004286 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4287 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004288 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004290 garray_T *stack = &cctx->ctx_type_stack;
4291 type_T *type;
4292 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004294 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004295 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004296 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004297
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004299 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4300
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004301 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004302 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004304 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 return FAIL;
4306 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004307 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004309 char_u *pstart = p;
4310
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004311 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004312 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004313 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 // something->method()
4316 // Apply the '!', '-' and '+' first:
4317 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004318 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004321 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004322 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004323 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004324 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004325 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004326 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004327 garray_T *stack = &cctx->ctx_type_stack;
4328 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004329 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004330 int expr_isn_start = cctx->ctx_instr.ga_len;
4331 int expr_isn_end;
4332 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004333
4334 // Funcref call: list->(Refs[2])(arg)
4335 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004336 //
4337 // Fist compile the function expression.
4338 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004339 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004340
4341 // Remember the next instruction index, where the instructions
4342 // for arguments are being written.
4343 expr_isn_end = cctx->ctx_instr.ga_len;
4344
4345 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004346 if (**arg != '(')
4347 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004348 if (*skipwhite(*arg) == '(')
4349 emsg(_(e_nowhitespace));
4350 else
4351 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004352 return FAIL;
4353 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004354 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004355 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004356 return FAIL;
4357
Bram Moolenaar2927c072021-04-05 19:41:21 +02004358 // Move the instructions for the arguments to before the
4359 // instructions of the expression and move the type of the
4360 // expression after the argument types. This is what ISN_PCALL
4361 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004362 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004363 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4364 if (arg_isn_count > 0)
4365 {
4366 int expr_isn_count = expr_isn_end - expr_isn_start;
4367 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4368
4369 if (isn == NULL)
4370 return FAIL;
4371 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4372 + expr_isn_start,
4373 sizeof(isn_T) * expr_isn_count);
4374 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4375 + expr_isn_start,
4376 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4377 sizeof(isn_T) * arg_isn_count);
4378 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4379 + expr_isn_start + arg_isn_count,
4380 isn, sizeof(isn_T) * expr_isn_count);
4381 vim_free(isn);
4382
4383 type = ((type_T **)stack->ga_data)[type_idx_start];
4384 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4385 ((type_T **)stack->ga_data) + type_idx_start + 1,
4386 sizeof(type_T *)
4387 * (stack->ga_len - type_idx_start - 1));
4388 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4389 }
4390
Bram Moolenaar7e368202020-12-25 21:56:57 +01004391 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004392 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 return FAIL;
4394 }
4395 else
4396 {
4397 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004398 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004399 if (!eval_isnamec1(*p))
4400 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004401 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004402 return FAIL;
4403 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004404 if (ASCII_ISALPHA(*p) && p[1] == ':')
4405 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004406 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 ;
4408 if (*p != '(')
4409 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004410 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 return FAIL;
4412 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004413 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 return FAIL;
4415 }
4416 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004417 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004419 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004420
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004421 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004422 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004423 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004424 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004425 // TODO: more arguments
4426 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004427 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004428 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004429 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004430
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004431 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004432 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004433 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004434 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004435 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004436 // missing first index is equal to zero
4437 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004438 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004439 else
4440 {
4441 if (compile_expr0(arg, cctx) == FAIL)
4442 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004443 if (**arg == ':')
4444 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004445 semsg(_(e_white_space_required_before_and_after_str_at_str),
4446 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004447 return FAIL;
4448 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004449 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004450 return FAIL;
4451 *arg = skipwhite(*arg);
4452 }
4453 if (**arg == ':')
4454 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004455 is_slice = TRUE;
4456 ++*arg;
4457 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4458 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004459 semsg(_(e_white_space_required_before_and_after_str_at_str),
4460 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004461 return FAIL;
4462 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004463 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004464 return FAIL;
4465 if (**arg == ']')
4466 // missing second index is equal to end of string
4467 generate_PUSHNR(cctx, -1);
4468 else
4469 {
4470 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004471 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004472 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004473 return FAIL;
4474 *arg = skipwhite(*arg);
4475 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004476 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477
4478 if (**arg != ']')
4479 {
4480 emsg(_(e_missbrac));
4481 return FAIL;
4482 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004483 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484
Bram Moolenaare42939a2021-04-05 17:11:17 +02004485 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004486 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004488 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004490 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004491 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004492 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004493 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004494
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004495 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004496 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004497 {
4498 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004499 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004500 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004501 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004502 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 while (eval_isnamec(*p))
4504 MB_PTR_ADV(p);
4505 if (p == *arg)
4506 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004507 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 return FAIL;
4509 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004510 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 return FAIL;
4512 *arg = p;
4513 }
4514 else
4515 break;
4516 }
4517
4518 // TODO - see handle_subscript():
4519 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4520 // Don't do this when "Func" is already a partial that was bound
4521 // explicitly (pt_auto is FALSE).
4522
4523 return OK;
4524}
4525
4526/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004527 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4528 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004530 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004531 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004532 *
4533 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 */
4535
4536/*
4537 * number number constant
4538 * 0zFFFFFFFF Blob constant
4539 * "string" string constant
4540 * 'string' literal string constant
4541 * &option-name option value
4542 * @r register contents
4543 * identifier variable value
4544 * function() function call
4545 * $VAR environment variable
4546 * (expression) nested expression
4547 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004548 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 *
4550 * Also handle:
4551 * ! in front logical NOT
4552 * - in front unary minus
4553 * + in front unary plus (ignored)
4554 * trailing (arg) funcref/partial call
4555 * trailing [] subscript in String or List
4556 * trailing .name entry in Dictionary
4557 * trailing ->name() method call
4558 */
4559 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004560compile_expr7(
4561 char_u **arg,
4562 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004563 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 char_u *start_leader, *end_leader;
4566 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004567 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004568 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004570 ppconst->pp_is_const = FALSE;
4571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 /*
4573 * Skip '!', '-' and '+' characters. They are handled later.
4574 */
4575 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004576 if (eval_leader(arg, TRUE) == FAIL)
4577 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 end_leader = *arg;
4579
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004580 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 switch (**arg)
4582 {
4583 /*
4584 * Number constant.
4585 */
4586 case '0': // also for blob starting with 0z
4587 case '1':
4588 case '2':
4589 case '3':
4590 case '4':
4591 case '5':
4592 case '6':
4593 case '7':
4594 case '8':
4595 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004596 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004598 // Apply "-" and "+" just before the number now, right to
4599 // left. Matters especially when "->" follows. Stops at
4600 // '!'.
4601 if (apply_leader(rettv, TRUE,
4602 start_leader, &end_leader) == FAIL)
4603 {
4604 clear_tv(rettv);
4605 return FAIL;
4606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 break;
4608
4609 /*
4610 * String constant: "string".
4611 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004612 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 return FAIL;
4614 break;
4615
4616 /*
4617 * Literal string constant: 'str''ing'.
4618 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004619 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 return FAIL;
4621 break;
4622
4623 /*
4624 * Constant Vim variable.
4625 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004626 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 ret = NOTDONE;
4628 break;
4629
4630 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004631 * "true" constant
4632 */
4633 case 't': if (STRNCMP(*arg, "true", 4) == 0
4634 && !eval_isnamec((*arg)[4]))
4635 {
4636 *arg += 4;
4637 rettv->v_type = VAR_BOOL;
4638 rettv->vval.v_number = VVAL_TRUE;
4639 }
4640 else
4641 ret = NOTDONE;
4642 break;
4643
4644 /*
4645 * "false" constant
4646 */
4647 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4648 && !eval_isnamec((*arg)[5]))
4649 {
4650 *arg += 5;
4651 rettv->v_type = VAR_BOOL;
4652 rettv->vval.v_number = VVAL_FALSE;
4653 }
4654 else
4655 ret = NOTDONE;
4656 break;
4657
4658 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004659 * "null" constant
4660 */
4661 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004662 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004663 {
4664 *arg += 4;
4665 rettv->v_type = VAR_SPECIAL;
4666 rettv->vval.v_number = VVAL_NULL;
4667 }
4668 else
4669 ret = NOTDONE;
4670 break;
4671
4672 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 * List: [expr, expr]
4674 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004675 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4676 return FAIL;
4677 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 break;
4679
4680 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 * Dictionary: {'key': val, 'key': val}
4682 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004683 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4684 return FAIL;
4685 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 break;
4687
4688 /*
4689 * Option value: &name
4690 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004691 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4692 return FAIL;
4693 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 break;
4695
4696 /*
4697 * Environment variable: $VAR.
4698 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004699 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4700 return FAIL;
4701 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 break;
4703
4704 /*
4705 * Register contents: @r.
4706 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004707 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4708 return FAIL;
4709 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 break;
4711 /*
4712 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004713 * lambda: (arg, arg) => expr
4714 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004716 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4717 ret = compile_lambda(arg, cctx);
4718 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004719 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 break;
4721
4722 default: ret = NOTDONE;
4723 break;
4724 }
4725 if (ret == FAIL)
4726 return FAIL;
4727
Bram Moolenaar1c747212020-05-09 18:28:34 +02004728 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004730 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004731 clear_tv(rettv);
4732 else
4733 // A constant expression can possibly be handled compile time,
4734 // return the value instead of generating code.
4735 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 }
4737 else if (ret == NOTDONE)
4738 {
4739 char_u *p;
4740 int r;
4741
4742 if (!eval_isnamec1(**arg))
4743 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004744 if (!vim9_bad_comment(*arg))
4745 {
4746 if (ends_excmd(*skipwhite(*arg)))
4747 semsg(_(e_empty_expression_str), *arg);
4748 else
4749 semsg(_(e_name_expected_str), *arg);
4750 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004751 return FAIL;
4752 }
4753
4754 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004755 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004756 if (p - *arg == (size_t)1 && **arg == '_')
4757 {
4758 emsg(_(e_cannot_use_underscore_here));
4759 return FAIL;
4760 }
4761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004763 {
4764 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4765 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004767 {
4768 if (generate_ppconst(cctx, ppconst) == FAIL)
4769 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004770 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004771 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 if (r == FAIL)
4773 return FAIL;
4774 }
4775
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004776 // Handle following "[]", ".member", etc.
4777 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004778 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004779 ppconst) == FAIL)
4780 return FAIL;
4781 if (ppconst->pp_used > 0)
4782 {
4783 // apply the '!', '-' and '+' before the constant
4784 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004785 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004786 return FAIL;
4787 return OK;
4788 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004789 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004791 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792}
4793
4794/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004795 * Give the "white on both sides" error, taking the operator from "p[len]".
4796 */
4797 void
4798error_white_both(char_u *op, int len)
4799{
4800 char_u buf[10];
4801
4802 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004803 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004804}
4805
4806/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004807 * <type>expr7: runtime type check / conversion
4808 */
4809 static int
4810compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4811{
4812 type_T *want_type = NULL;
4813
4814 // Recognize <type>
4815 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4816 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004817 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004818 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4819 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004820 return FAIL;
4821
4822 if (**arg != '>')
4823 {
4824 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004825 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004826 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004827 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004828 return FAIL;
4829 }
4830 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004831 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004832 return FAIL;
4833 }
4834
4835 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4836 return FAIL;
4837
4838 if (want_type != NULL)
4839 {
4840 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004841 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004842 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004843
Bram Moolenaard1103582020-08-14 22:44:25 +02004844 generate_ppconst(cctx, ppconst);
4845 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004846 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004847 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004848 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004849 return FAIL;
4850 }
4851 }
4852
4853 return OK;
4854}
4855
4856/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 * * number multiplication
4858 * / number division
4859 * % number modulo
4860 */
4861 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004862compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863{
4864 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004865 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004866 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004868 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004869 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 return FAIL;
4871
4872 /*
4873 * Repeat computing, until no "*", "/" or "%" is following.
4874 */
4875 for (;;)
4876 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004877 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 if (*op != '*' && *op != '/' && *op != '%')
4879 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004880 if (next != NULL)
4881 {
4882 *arg = next_line_from_context(cctx, TRUE);
4883 op = skipwhite(*arg);
4884 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004885
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004886 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004888 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004889 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004891 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004892 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004894 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004895 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004897
4898 if (ppconst->pp_used == ppconst_used + 2
4899 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4900 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004901 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004902 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4903 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4904 varnumber_T res = 0;
4905 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004907 // both are numbers: compute the result
4908 switch (*op)
4909 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004910 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004911 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004912 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004913 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004914 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004915 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004916 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004917 break;
4918 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004919 if (failed)
4920 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004921 tv1->vval.v_number = res;
4922 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004923 }
4924 else
4925 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004926 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004927 generate_two_op(cctx, op);
4928 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929 }
4930
4931 return OK;
4932}
4933
4934/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004935 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 * - number subtraction
4937 * .. string concatenation
4938 */
4939 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004940compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941{
4942 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004943 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004945 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946
4947 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004948 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 return FAIL;
4950
4951 /*
4952 * Repeat computing, until no "+", "-" or ".." is following.
4953 */
4954 for (;;)
4955 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004956 op = may_peek_next_line(cctx, *arg, &next);
4957 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004959 if (op[0] == op[1] && *op != '.' && next)
4960 // Finding "++" or "--" on the next line is a separate command.
4961 // But ".." is concatenation.
4962 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004964 if (next != NULL)
4965 {
4966 *arg = next_line_from_context(cctx, TRUE);
4967 op = skipwhite(*arg);
4968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004970 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004972 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004973 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 }
4975
Bram Moolenaare0de1712020-12-02 17:36:54 +01004976 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004977 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004979 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004980 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 return FAIL;
4982
Bram Moolenaara5565e42020-05-09 15:44:01 +02004983 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004984 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004985 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4986 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4987 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4988 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004990 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4991 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004992
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004993 // concat/subtract/add constant numbers
4994 if (*op == '+')
4995 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4996 else if (*op == '-')
4997 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4998 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004999 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005000 // concatenate constant strings
5001 char_u *s1 = tv1->vval.v_string;
5002 char_u *s2 = tv2->vval.v_string;
5003 size_t len1 = STRLEN(s1);
5004
5005 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5006 if (tv1->vval.v_string == NULL)
5007 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005008 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005009 return FAIL;
5010 }
5011 mch_memmove(tv1->vval.v_string, s1, len1);
5012 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005013 vim_free(s1);
5014 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005015 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005016 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 }
5018 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005019 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005020 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005021 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005022 if (*op == '.')
5023 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005024 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5025 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005026 return FAIL;
5027 generate_instr_drop(cctx, ISN_CONCAT, 1);
5028 }
5029 else
5030 generate_two_op(cctx, op);
5031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 }
5033
5034 return OK;
5035}
5036
5037/*
5038 * expr5a == expr5b
5039 * expr5a =~ expr5b
5040 * expr5a != expr5b
5041 * expr5a !~ expr5b
5042 * expr5a > expr5b
5043 * expr5a >= expr5b
5044 * expr5a < expr5b
5045 * expr5a <= expr5b
5046 * expr5a is expr5b
5047 * expr5a isnot expr5b
5048 *
5049 * Produces instructions:
5050 * EVAL expr5a Push result of "expr5a"
5051 * EVAL expr5b Push result of "expr5b"
5052 * COMPARE one of the compare instructions
5053 */
5054 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005055compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005057 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005059 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005062 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063
5064 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005065 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 return FAIL;
5067
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005068 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005069 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070
5071 /*
5072 * If there is a comparative operator, use it.
5073 */
5074 if (type != EXPR_UNKNOWN)
5075 {
5076 int ic = FALSE; // Default: do not ignore case
5077
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005078 if (next != NULL)
5079 {
5080 *arg = next_line_from_context(cctx, TRUE);
5081 p = skipwhite(*arg);
5082 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083 if (type_is && (p[len] == '?' || p[len] == '#'))
5084 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005085 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 return FAIL;
5087 }
5088 // extra question mark appended: ignore case
5089 if (p[len] == '?')
5090 {
5091 ic = TRUE;
5092 ++len;
5093 }
5094 // extra '#' appended: match case (ignored)
5095 else if (p[len] == '#')
5096 ++len;
5097 // nothing appended: match case
5098
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005099 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005101 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005102 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 }
5104
5105 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005106 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005107 return FAIL;
5108
Bram Moolenaara5565e42020-05-09 15:44:01 +02005109 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 return FAIL;
5111
Bram Moolenaara5565e42020-05-09 15:44:01 +02005112 if (ppconst->pp_used == ppconst_used + 2)
5113 {
5114 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5115 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5116 int ret;
5117
5118 // Both sides are a constant, compute the result now.
5119 // First check for a valid combination of types, this is more
5120 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005121 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005122 ret = FAIL;
5123 else
5124 {
5125 ret = typval_compare(tv1, tv2, type, ic);
5126 tv1->v_type = VAR_BOOL;
5127 tv1->vval.v_number = tv1->vval.v_number
5128 ? VVAL_TRUE : VVAL_FALSE;
5129 clear_tv(tv2);
5130 --ppconst->pp_used;
5131 }
5132 return ret;
5133 }
5134
5135 generate_ppconst(cctx, ppconst);
5136 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 }
5138
5139 return OK;
5140}
5141
Bram Moolenaar7f141552020-05-09 17:35:53 +02005142static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144/*
5145 * Compile || or &&.
5146 */
5147 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005148compile_and_or(
5149 char_u **arg,
5150 cctx_T *cctx,
5151 char *op,
5152 ppconst_T *ppconst,
5153 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005154{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005155 char_u *next;
5156 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005157 int opchar = *op;
5158
5159 if (p[0] == opchar && p[1] == opchar)
5160 {
5161 garray_T *instr = &cctx->ctx_instr;
5162 garray_T end_ga;
5163
5164 /*
5165 * Repeat until there is no following "||" or "&&"
5166 */
5167 ga_init2(&end_ga, sizeof(int), 10);
5168 while (p[0] == opchar && p[1] == opchar)
5169 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005170 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005171 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005172 int start_ctx_lnum = cctx->ctx_lnum;
5173 int save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005174 int status;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005175
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005176 if (next != NULL)
5177 {
5178 *arg = next_line_from_context(cctx, TRUE);
5179 p = skipwhite(*arg);
5180 }
5181
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005182 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5183 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005184 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005185 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005186 return FAIL;
5187 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005188
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005189 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005190 SOURCING_LNUM = start_lnum;
5191 save_lnum = cctx->ctx_lnum;
5192 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005193
5194 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005195 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005196 {
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005197 // TODO: use ppconst if the value is a constant
5198 generate_ppconst(cctx, ppconst);
5199
5200 // Every part must evaluate to a bool.
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005201 status = bool_on_stack(cctx);
5202 if (status != FAIL)
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005203 status = ga_grow(&end_ga, 1);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005204 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005205 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005206 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 {
5208 ga_clear(&end_ga);
5209 return FAIL;
5210 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5213 ++end_ga.ga_len;
5214 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005215 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216
5217 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005218 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005219 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005220 {
5221 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005222 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005223 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005224
Bram Moolenaara5565e42020-05-09 15:44:01 +02005225 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5226 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227 {
5228 ga_clear(&end_ga);
5229 return FAIL;
5230 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005231
5232 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005234
5235 if (check_ppconst_bool(ppconst) == FAIL)
5236 {
5237 ga_clear(&end_ga);
5238 return FAIL;
5239 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005240 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005242 // Every part must evaluate to a bool.
5243 if (bool_on_stack(cctx) == FAIL)
5244 {
5245 ga_clear(&end_ga);
5246 return FAIL;
5247 }
5248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005249 // Fill in the end label in all jumps.
5250 while (end_ga.ga_len > 0)
5251 {
5252 isn_T *isn;
5253
5254 --end_ga.ga_len;
5255 isn = ((isn_T *)instr->ga_data)
5256 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5257 isn->isn_arg.jump.jump_where = instr->ga_len;
5258 }
5259 ga_clear(&end_ga);
5260 }
5261
5262 return OK;
5263}
5264
5265/*
5266 * expr4a && expr4a && expr4a logical AND
5267 *
5268 * Produces instructions:
5269 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005270 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005271 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005272 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005273 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005274 * EVAL expr4c Push result of "expr4c"
5275 * end:
5276 */
5277 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005278compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005280 int ppconst_used = ppconst->pp_used;
5281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005282 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005283 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284 return FAIL;
5285
5286 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005287 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288}
5289
5290/*
5291 * expr3a || expr3b || expr3c logical OR
5292 *
5293 * Produces instructions:
5294 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005295 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005296 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005298 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005299 * EVAL expr3c Push result of "expr3c"
5300 * end:
5301 */
5302 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005303compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005305 int ppconst_used = ppconst->pp_used;
5306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005307 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005308 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005309 return FAIL;
5310
5311 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005312 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005313}
5314
5315/*
5316 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005318 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005319 * JUMP_IF_FALSE alt jump if false
5320 * EVAL expr1a
5321 * JUMP_ALWAYS end
5322 * alt: EVAL expr1b
5323 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005324 *
5325 * Toplevel expression: expr2 ?? expr1
5326 * Produces instructions:
5327 * EVAL expr2 Push result of "expr2"
5328 * JUMP_AND_KEEP_IF_TRUE end jump if true
5329 * EVAL expr1
5330 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 */
5332 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005333compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005334{
5335 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005336 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005337 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338
Bram Moolenaar3988f642020-08-27 22:43:03 +02005339 // Ignore all kinds of errors when not producing code.
5340 if (cctx->ctx_skip == SKIP_YES)
5341 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005342 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005343 return OK;
5344 }
5345
Bram Moolenaar61a89812020-05-07 16:58:17 +02005346 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005347 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005348 return FAIL;
5349
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005350 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351 if (*p == '?')
5352 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005353 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 garray_T *instr = &cctx->ctx_instr;
5355 garray_T *stack = &cctx->ctx_type_stack;
5356 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005357 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005358 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005359 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005360 int has_const_expr = FALSE;
5361 int const_value = FALSE;
5362 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005363
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005364 if (next != NULL)
5365 {
5366 *arg = next_line_from_context(cctx, TRUE);
5367 p = skipwhite(*arg);
5368 }
5369
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005370 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005371 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005372 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005373 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005374 return FAIL;
5375 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005376
Bram Moolenaara5565e42020-05-09 15:44:01 +02005377 if (ppconst->pp_used == ppconst_used + 1)
5378 {
5379 // the condition is a constant, we know whether the ? or the :
5380 // expression is to be evaluated.
5381 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005382 if (op_falsy)
5383 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5384 else
5385 {
5386 int error = FALSE;
5387
5388 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5389 &error);
5390 if (error)
5391 return FAIL;
5392 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005393 cctx->ctx_skip = save_skip == SKIP_YES ||
5394 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5395
5396 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5397 // "left ?? right" and "left" is truthy: produce "left"
5398 generate_ppconst(cctx, ppconst);
5399 else
5400 {
5401 clear_tv(&ppconst->pp_tv[ppconst_used]);
5402 --ppconst->pp_used;
5403 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005404 }
5405 else
5406 {
5407 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005408 if (op_falsy)
5409 end_idx = instr->ga_len;
5410 generate_JUMP(cctx, op_falsy
5411 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5412 if (op_falsy)
5413 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005414 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005415
5416 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005417 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005418 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005419 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005420 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421
Bram Moolenaara5565e42020-05-09 15:44:01 +02005422 if (!has_const_expr)
5423 {
5424 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005425
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005426 if (!op_falsy)
5427 {
5428 // remember the type and drop it
5429 --stack->ga_len;
5430 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005432 end_idx = instr->ga_len;
5433 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005434
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005435 // jump here from JUMP_IF_FALSE
5436 isn = ((isn_T *)instr->ga_data) + alt_idx;
5437 isn->isn_arg.jump.jump_where = instr->ga_len;
5438 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005439 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005440
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005441 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005443 // Check for the ":".
5444 p = may_peek_next_line(cctx, *arg, &next);
5445 if (*p != ':')
5446 {
5447 emsg(_(e_missing_colon));
5448 return FAIL;
5449 }
5450 if (next != NULL)
5451 {
5452 *arg = next_line_from_context(cctx, TRUE);
5453 p = skipwhite(*arg);
5454 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005455
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005456 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5457 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005458 semsg(_(e_white_space_required_before_and_after_str_at_str),
5459 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005460 return FAIL;
5461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005463 // evaluate the third expression
5464 if (has_const_expr)
5465 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005466 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005467 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005468 return FAIL;
5469 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5470 return FAIL;
5471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005472
Bram Moolenaara5565e42020-05-09 15:44:01 +02005473 if (!has_const_expr)
5474 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005475 type_T **typep;
5476
Bram Moolenaara5565e42020-05-09 15:44:01 +02005477 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005478
Bram Moolenaara5565e42020-05-09 15:44:01 +02005479 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005480 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5481 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005482
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005483 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005484 isn = ((isn_T *)instr->ga_data) + end_idx;
5485 isn->isn_arg.jump.jump_where = instr->ga_len;
5486 }
5487
5488 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489 }
5490 return OK;
5491}
5492
5493/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005494 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005495 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5496 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005497 */
5498 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005499compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005500{
5501 ppconst_T ppconst;
5502
5503 CLEAR_FIELD(ppconst);
5504 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5505 {
5506 clear_ppconst(&ppconst);
5507 return FAIL;
5508 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005509 if (is_const != NULL)
5510 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005511 if (generate_ppconst(cctx, &ppconst) == FAIL)
5512 return FAIL;
5513 return OK;
5514}
5515
5516/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005517 * Toplevel expression.
5518 */
5519 static int
5520compile_expr0(char_u **arg, cctx_T *cctx)
5521{
5522 return compile_expr0_ext(arg, cctx, NULL);
5523}
5524
5525/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005526 * Compile "return [expr]".
5527 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005528 */
5529 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005530compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531{
5532 char_u *p = arg;
5533 garray_T *stack = &cctx->ctx_type_stack;
5534 type_T *stack_type;
5535
5536 if (*p != NUL && *p != '|' && *p != '\n')
5537 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005538 if (legacy)
5539 {
5540 int save_flags = cmdmod.cmod_flags;
5541
5542 generate_LEGACY_EVAL(cctx, p);
5543 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5544 0, cctx, FALSE, FALSE) == FAIL)
5545 return NULL;
5546 cmdmod.cmod_flags |= CMOD_LEGACY;
5547 (void)skip_expr(&p, NULL);
5548 cmdmod.cmod_flags = save_flags;
5549 }
5550 else
5551 {
5552 // compile return argument into instructions
5553 if (compile_expr0(&p, cctx) == FAIL)
5554 return NULL;
5555 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005557 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005558 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005559 // "check_return_type" with uf_ret_type set to &t_unknown is used
5560 // for an inline function without a specified return type. Set the
5561 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005562 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005563 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005564 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5565 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005566 || (!check_return_type
5567 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005568 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005569 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005570 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005571 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005572 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005573 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5574 && stack_type->tt_type != VAR_VOID
5575 && stack_type->tt_type != VAR_UNKNOWN)
5576 {
5577 emsg(_(e_returning_value_in_function_without_return_type));
5578 return NULL;
5579 }
5580 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005581 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005582 return NULL;
5583 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005584 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585 }
5586 else
5587 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005588 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005589 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005590 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5591 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005593 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005594 return NULL;
5595 }
5596
5597 // No argument, return zero.
5598 generate_PUSHNR(cctx, 0);
5599 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005600
5601 // Undo any command modifiers.
5602 generate_undo_cmdmods(cctx);
5603
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005604 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 return NULL;
5606
5607 // "return val | endif" is possible
5608 return skipwhite(p);
5609}
5610
5611/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005612 * Get a line from the compilation context, compatible with exarg_T getline().
5613 * Return a pointer to the line in allocated memory.
5614 * Return NULL for end-of-file or some error.
5615 */
5616 static char_u *
5617exarg_getline(
5618 int c UNUSED,
5619 void *cookie,
5620 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005621 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005622{
5623 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005624 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005625
Bram Moolenaar66250c92020-08-20 15:02:42 +02005626 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005627 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005628 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005629 return NULL;
5630 ++cctx->ctx_lnum;
5631 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5632 // Comment lines result in NULL pointers, skip them.
5633 if (p != NULL)
5634 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005635 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005636}
5637
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005638 void
5639fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5640{
5641 eap->getline = exarg_getline;
5642 eap->cookie = cctx;
5643}
5644
Bram Moolenaar04b12692020-05-04 23:24:44 +02005645/*
5646 * Compile a nested :def command.
5647 */
5648 static char_u *
5649compile_nested_function(exarg_T *eap, cctx_T *cctx)
5650{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005651 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005652 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005653 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005654 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005655 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005656 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005657 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005658
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005659 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005660 {
5661 emsg(_(e_cannot_use_bang_with_nested_def));
5662 return NULL;
5663 }
5664
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005665 if (*name_start == '/')
5666 {
5667 name_end = skip_regexp(name_start + 1, '/', TRUE);
5668 if (*name_end == '/')
5669 ++name_end;
5670 eap->nextcmd = check_nextcmd(name_end);
5671 }
5672 if (name_end == name_start || *skipwhite(name_end) != '(')
5673 {
5674 if (!ends_excmd2(name_start, name_end))
5675 {
5676 semsg(_(e_invalid_command_str), eap->cmd);
5677 return NULL;
5678 }
5679
5680 // "def" or "def Name": list functions
5681 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5682 return NULL;
5683 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5684 }
5685
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005686 // Only g:Func() can use a namespace.
5687 if (name_start[1] == ':' && !is_global)
5688 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005689 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005690 return NULL;
5691 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005692 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005693 return NULL;
5694
Bram Moolenaar04b12692020-05-04 23:24:44 +02005695 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005696 fill_exarg_from_cctx(eap, cctx);
5697
Bram Moolenaar04b12692020-05-04 23:24:44 +02005698 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005699 lambda_name = vim_strsave(get_lambda_name());
5700 if (lambda_name == NULL)
5701 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005702 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005703
Bram Moolenaar822ba242020-05-24 23:00:18 +02005704 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005705 {
5706 r = eap->skip ? OK : FAIL;
5707 goto theend;
5708 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005709
5710 // copy over the block scope IDs before compiling
5711 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5712 {
5713 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5714
5715 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5716 if (ufunc->uf_block_ids != NULL)
5717 {
5718 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5719 sizeof(int) * block_depth);
5720 ufunc->uf_block_depth = block_depth;
5721 }
5722 }
5723
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005724 compile_type = COMPILE_TYPE(ufunc);
5725#ifdef FEAT_PROFILE
5726 // If the outer function is profiled, also compile the nested function for
5727 // profiling.
5728 if (cctx->ctx_compile_type == CT_PROFILE)
5729 compile_type = CT_PROFILE;
5730#endif
5731 if (func_needs_compiling(ufunc, compile_type)
5732 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005733 {
5734 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005735 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005736 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005737
Bram Moolenaar648594e2021-07-11 17:55:01 +02005738#ifdef FEAT_PROFILE
5739 // When the outer function is compiled for profiling, the nested function
5740 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005741 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005742 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5743#endif
5744
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005745 if (is_global)
5746 {
5747 char_u *func_name = vim_strnsave(name_start + 2,
5748 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005749
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005750 if (func_name == NULL)
5751 r = FAIL;
5752 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005753 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005754 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005755 lambda_name = NULL;
5756 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005757 }
5758 else
5759 {
5760 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005761 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005762 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005763
Bram Moolenaareef21022020-08-01 22:16:43 +02005764 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005765 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005766 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005767 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005768 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5769 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005770 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005771
5772theend:
5773 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005774 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005775}
5776
5777/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005778 * Return the length of an assignment operator, or zero if there isn't one.
5779 */
5780 int
5781assignment_len(char_u *p, int *heredoc)
5782{
5783 if (*p == '=')
5784 {
5785 if (p[1] == '<' && p[2] == '<')
5786 {
5787 *heredoc = TRUE;
5788 return 3;
5789 }
5790 return 1;
5791 }
5792 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5793 return 2;
5794 if (STRNCMP(p, "..=", 3) == 0)
5795 return 3;
5796 return 0;
5797}
5798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005799/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005800 * Generate the load instruction for "name".
5801 */
5802 static void
5803generate_loadvar(
5804 cctx_T *cctx,
5805 assign_dest_T dest,
5806 char_u *name,
5807 lvar_T *lvar,
5808 type_T *type)
5809{
5810 switch (dest)
5811 {
5812 case dest_option:
5813 // TODO: check the option exists
5814 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5815 break;
5816 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005817 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5818 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5819 else
5820 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005821 break;
5822 case dest_buffer:
5823 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5824 break;
5825 case dest_window:
5826 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5827 break;
5828 case dest_tab:
5829 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5830 break;
5831 case dest_script:
5832 compile_load_scriptvar(cctx,
5833 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5834 break;
5835 case dest_env:
5836 // Include $ in the name here
5837 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5838 break;
5839 case dest_reg:
5840 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5841 break;
5842 case dest_vimvar:
5843 generate_LOADV(cctx, name + 2, TRUE);
5844 break;
5845 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005846 if (lvar->lv_from_outer > 0)
5847 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5848 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005849 else
5850 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5851 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005852 case dest_expr:
5853 // list or dict value should already be on the stack.
5854 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005855 }
5856}
5857
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005858/*
5859 * Skip over "[expr]" or ".member".
5860 * Does not check for any errors.
5861 */
5862 static char_u *
5863skip_index(char_u *start)
5864{
5865 char_u *p = start;
5866
5867 if (*p == '[')
5868 {
5869 p = skipwhite(p + 1);
5870 (void)skip_expr(&p, NULL);
5871 p = skipwhite(p);
5872 if (*p == ']')
5873 return p + 1;
5874 return p;
5875 }
5876 // if (*p == '.')
5877 return to_name_end(p + 1, TRUE);
5878}
5879
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005880 void
5881vim9_declare_error(char_u *name)
5882{
5883 char *scope = "";
5884
5885 switch (*name)
5886 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005887 case 'g': scope = _("global"); break;
5888 case 'b': scope = _("buffer"); break;
5889 case 'w': scope = _("window"); break;
5890 case 't': scope = _("tab"); break;
5891 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005892 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005893 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005894 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005895 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005896 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005897 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005898 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005899 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005900 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005901}
5902
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005903/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005904 * For one assignment figure out the type of destination. Return it in "dest".
5905 * When not recognized "dest" is not set.
5906 * For an option "opt_flags" is set.
5907 * For a v:var "vimvaridx" is set.
5908 * "type" is set to the destination type if known, unchanted otherwise.
5909 * Return FAIL if an error message was given.
5910 */
5911 static int
5912get_var_dest(
5913 char_u *name,
5914 assign_dest_T *dest,
5915 int cmdidx,
5916 int *opt_flags,
5917 int *vimvaridx,
5918 type_T **type,
5919 cctx_T *cctx)
5920{
5921 char_u *p;
5922
5923 if (*name == '&')
5924 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005925 int cc;
5926 long numval;
5927 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005928
5929 *dest = dest_option;
5930 if (cmdidx == CMD_final || cmdidx == CMD_const)
5931 {
5932 emsg(_(e_const_option));
5933 return FAIL;
5934 }
5935 p = name;
5936 p = find_option_end(&p, opt_flags);
5937 if (p == NULL)
5938 {
5939 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005940 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005941 return FAIL;
5942 }
5943 cc = *p;
5944 *p = NUL;
5945 opt_type = get_option_value(skip_option_env_lead(name),
5946 &numval, NULL, *opt_flags);
5947 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005948 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005949 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005950 case gov_unknown:
5951 semsg(_(e_unknown_option), name);
5952 return FAIL;
5953 case gov_string:
5954 case gov_hidden_string:
5955 *type = &t_string;
5956 break;
5957 case gov_bool:
5958 case gov_hidden_bool:
5959 *type = &t_bool;
5960 break;
5961 case gov_number:
5962 case gov_hidden_number:
5963 *type = &t_number;
5964 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005965 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005966 }
5967 else if (*name == '$')
5968 {
5969 *dest = dest_env;
5970 *type = &t_string;
5971 }
5972 else if (*name == '@')
5973 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005974 if (name[1] != '@'
5975 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005976 {
5977 emsg_invreg(name[1]);
5978 return FAIL;
5979 }
5980 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005981 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005982 }
5983 else if (STRNCMP(name, "g:", 2) == 0)
5984 {
5985 *dest = dest_global;
5986 }
5987 else if (STRNCMP(name, "b:", 2) == 0)
5988 {
5989 *dest = dest_buffer;
5990 }
5991 else if (STRNCMP(name, "w:", 2) == 0)
5992 {
5993 *dest = dest_window;
5994 }
5995 else if (STRNCMP(name, "t:", 2) == 0)
5996 {
5997 *dest = dest_tab;
5998 }
5999 else if (STRNCMP(name, "v:", 2) == 0)
6000 {
6001 typval_T *vtv;
6002 int di_flags;
6003
6004 *vimvaridx = find_vim_var(name + 2, &di_flags);
6005 if (*vimvaridx < 0)
6006 {
6007 semsg(_(e_variable_not_found_str), name);
6008 return FAIL;
6009 }
6010 // We use the current value of "sandbox" here, is that OK?
6011 if (var_check_ro(di_flags, name, FALSE))
6012 return FAIL;
6013 *dest = dest_vimvar;
6014 vtv = get_vim_var_tv(*vimvaridx);
6015 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6016 }
6017 return OK;
6018}
6019
6020/*
6021 * Generate a STORE instruction for "dest", not being "dest_local".
6022 * Return FAIL when out of memory.
6023 */
6024 static int
6025generate_store_var(
6026 cctx_T *cctx,
6027 assign_dest_T dest,
6028 int opt_flags,
6029 int vimvaridx,
6030 int scriptvar_idx,
6031 int scriptvar_sid,
6032 type_T *type,
6033 char_u *name)
6034{
6035 switch (dest)
6036 {
6037 case dest_option:
6038 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6039 opt_flags);
6040 case dest_global:
6041 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006042 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6043 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006044 case dest_buffer:
6045 // include b: with the name, easier to execute that way
6046 return generate_STORE(cctx, ISN_STOREB, 0, name);
6047 case dest_window:
6048 // include w: with the name, easier to execute that way
6049 return generate_STORE(cctx, ISN_STOREW, 0, name);
6050 case dest_tab:
6051 // include t: with the name, easier to execute that way
6052 return generate_STORE(cctx, ISN_STORET, 0, name);
6053 case dest_env:
6054 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6055 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006056 return generate_STORE(cctx, ISN_STOREREG,
6057 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006058 case dest_vimvar:
6059 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6060 case dest_script:
6061 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006062 // "s:" may be included in the name.
6063 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006064 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006065 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6066 scriptvar_sid, scriptvar_idx, type);
6067 case dest_local:
6068 case dest_expr:
6069 // cannot happen
6070 break;
6071 }
6072 return FAIL;
6073}
6074
Bram Moolenaar752fc692021-01-04 21:57:11 +01006075 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006076generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6077{
6078 if (lhs->lhs_dest != dest_local)
6079 return generate_store_var(cctx, lhs->lhs_dest,
6080 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6081 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6082 lhs->lhs_type, lhs->lhs_name);
6083
6084 if (lhs->lhs_lvar != NULL)
6085 {
6086 garray_T *instr = &cctx->ctx_instr;
6087 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6088
6089 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6090 // ISN_STORENR
6091 if (lhs->lhs_lvar->lv_from_outer == 0
6092 && instr->ga_len == instr_count + 1
6093 && isn->isn_type == ISN_PUSHNR)
6094 {
6095 varnumber_T val = isn->isn_arg.number;
6096 garray_T *stack = &cctx->ctx_type_stack;
6097
6098 isn->isn_type = ISN_STORENR;
6099 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6100 isn->isn_arg.storenr.stnr_val = val;
6101 if (stack->ga_len > 0)
6102 --stack->ga_len;
6103 }
6104 else if (lhs->lhs_lvar->lv_from_outer > 0)
6105 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6106 lhs->lhs_lvar->lv_from_outer);
6107 else
6108 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6109 }
6110 return OK;
6111}
6112
6113 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006114is_decl_command(int cmdidx)
6115{
6116 return cmdidx == CMD_let || cmdidx == CMD_var
6117 || cmdidx == CMD_final || cmdidx == CMD_const;
6118}
6119
6120/*
6121 * Figure out the LHS type and other properties for an assignment or one item
6122 * of ":unlet" with an index.
6123 * Returns OK or FAIL.
6124 */
6125 static int
6126compile_lhs(
6127 char_u *var_start,
6128 lhs_T *lhs,
6129 int cmdidx,
6130 int heredoc,
6131 int oplen,
6132 cctx_T *cctx)
6133{
6134 char_u *var_end;
6135 int is_decl = is_decl_command(cmdidx);
6136
6137 CLEAR_POINTER(lhs);
6138 lhs->lhs_dest = dest_local;
6139 lhs->lhs_vimvaridx = -1;
6140 lhs->lhs_scriptvar_idx = -1;
6141
6142 // "dest_end" is the end of the destination, including "[expr]" or
6143 // ".name".
6144 // "var_end" is the end of the variable/option/etc. name.
6145 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6146 if (*var_start == '@')
6147 var_end = var_start + 2;
6148 else
6149 {
6150 // skip over the leading "&", "&l:", "&g:" and "$"
6151 var_end = skip_option_env_lead(var_start);
6152 var_end = to_name_end(var_end, TRUE);
6153 }
6154
6155 // "a: type" is declaring variable "a" with a type, not dict "a:".
6156 if (is_decl && lhs->lhs_dest_end == var_start + 2
6157 && lhs->lhs_dest_end[-1] == ':')
6158 --lhs->lhs_dest_end;
6159 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6160 --var_end;
6161
6162 // compute the length of the destination without "[expr]" or ".name"
6163 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006164 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006165 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6166 if (lhs->lhs_name == NULL)
6167 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006168
6169 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6170 // Something follows after the variable: "var[idx]" or "var.key".
6171 lhs->lhs_has_index = TRUE;
6172
Bram Moolenaar752fc692021-01-04 21:57:11 +01006173 if (heredoc)
6174 lhs->lhs_type = &t_list_string;
6175 else
6176 lhs->lhs_type = &t_any;
6177
6178 if (cctx->ctx_skip != SKIP_YES)
6179 {
6180 int declare_error = FALSE;
6181
6182 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6183 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6184 &lhs->lhs_type, cctx) == FAIL)
6185 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006186 if (lhs->lhs_dest != dest_local
6187 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006188 {
6189 // Specific kind of variable recognized.
6190 declare_error = is_decl;
6191 }
6192 else
6193 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006194 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006195 if (check_reserved_name(lhs->lhs_name) == FAIL)
6196 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006197
6198 if (lookup_local(var_start, lhs->lhs_varlen,
6199 &lhs->lhs_local_lvar, cctx) == OK)
6200 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6201 else
6202 {
6203 CLEAR_FIELD(lhs->lhs_arg_lvar);
6204 if (arg_exists(var_start, lhs->lhs_varlen,
6205 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6206 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6207 {
6208 if (is_decl)
6209 {
6210 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6211 return FAIL;
6212 }
6213 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6214 }
6215 }
6216 if (lhs->lhs_lvar != NULL)
6217 {
6218 if (is_decl)
6219 {
6220 semsg(_(e_variable_already_declared), lhs->lhs_name);
6221 return FAIL;
6222 }
6223 }
6224 else
6225 {
6226 int script_namespace = lhs->lhs_varlen > 1
6227 && STRNCMP(var_start, "s:", 2) == 0;
6228 int script_var = (script_namespace
6229 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006230 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006231 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006232 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006233 imported_T *import =
6234 find_imported(var_start, lhs->lhs_varlen, cctx);
6235
6236 if (script_namespace || script_var || import != NULL)
6237 {
6238 char_u *rawname = lhs->lhs_name
6239 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6240
6241 if (is_decl)
6242 {
6243 if (script_namespace)
6244 semsg(_(e_cannot_declare_script_variable_in_function),
6245 lhs->lhs_name);
6246 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006247 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006248 lhs->lhs_name);
6249 return FAIL;
6250 }
6251 else if (cctx->ctx_ufunc->uf_script_ctx_version
6252 == SCRIPT_VERSION_VIM9
6253 && script_namespace
6254 && !script_var && import == NULL)
6255 {
6256 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6257 return FAIL;
6258 }
6259
6260 lhs->lhs_dest = dest_script;
6261
6262 // existing script-local variables should have a type
6263 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6264 if (import != NULL)
6265 lhs->lhs_scriptvar_sid = import->imp_sid;
6266 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6267 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006268 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006269 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006270 lhs->lhs_scriptvar_sid, rawname,
6271 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6272 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006273 if (lhs->lhs_scriptvar_idx >= 0)
6274 {
6275 scriptitem_T *si = SCRIPT_ITEM(
6276 lhs->lhs_scriptvar_sid);
6277 svar_T *sv =
6278 ((svar_T *)si->sn_var_vals.ga_data)
6279 + lhs->lhs_scriptvar_idx;
6280 lhs->lhs_type = sv->sv_type;
6281 }
6282 }
6283 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006284 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006285 == FAIL)
6286 return FAIL;
6287 }
6288 }
6289
6290 if (declare_error)
6291 {
6292 vim9_declare_error(lhs->lhs_name);
6293 return FAIL;
6294 }
6295 }
6296
6297 // handle "a:name" as a name, not index "name" on "a"
6298 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6299 var_end = lhs->lhs_dest_end;
6300
6301 if (lhs->lhs_dest != dest_option)
6302 {
6303 if (is_decl && *var_end == ':')
6304 {
6305 char_u *p;
6306
6307 // parse optional type: "let var: type = expr"
6308 if (!VIM_ISWHITE(var_end[1]))
6309 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006310 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006311 return FAIL;
6312 }
6313 p = skipwhite(var_end + 1);
6314 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6315 if (lhs->lhs_type == NULL)
6316 return FAIL;
6317 lhs->lhs_has_type = TRUE;
6318 }
6319 else if (lhs->lhs_lvar != NULL)
6320 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6321 }
6322
Bram Moolenaare42939a2021-04-05 17:11:17 +02006323 if (oplen == 3 && !heredoc
6324 && lhs->lhs_dest != dest_global
6325 && !lhs->lhs_has_index
6326 && lhs->lhs_type->tt_type != VAR_STRING
6327 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006328 {
6329 emsg(_(e_can_only_concatenate_to_string));
6330 return FAIL;
6331 }
6332
6333 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6334 && cctx->ctx_skip != SKIP_YES)
6335 {
6336 if (oplen > 1 && !heredoc)
6337 {
6338 // +=, /=, etc. require an existing variable
6339 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6340 return FAIL;
6341 }
6342 if (!is_decl)
6343 {
6344 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6345 return FAIL;
6346 }
6347
Bram Moolenaar3f327882021-03-17 20:56:38 +01006348 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006349 if ((lhs->lhs_type->tt_type == VAR_FUNC
6350 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006351 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006352 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006353
6354 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006355 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6356 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6357 if (lhs->lhs_lvar == NULL)
6358 return FAIL;
6359 lhs->lhs_new_local = TRUE;
6360 }
6361
6362 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006363 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006364 {
6365 // Something follows after the variable: "var[idx]" or "var.key".
6366 // TODO: should we also handle "->func()" here?
6367 if (is_decl)
6368 {
6369 emsg(_(e_cannot_use_index_when_declaring_variable));
6370 return FAIL;
6371 }
6372
6373 if (var_start[lhs->lhs_varlen] == '['
6374 || var_start[lhs->lhs_varlen] == '.')
6375 {
6376 char_u *after = var_start + lhs->lhs_varlen;
6377 char_u *p;
6378
6379 // Only the last index is used below, if there are others
6380 // before it generate code for the expression. Thus for
6381 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6382 for (;;)
6383 {
6384 p = skip_index(after);
6385 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006386 {
6387 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006388 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006389 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006390 after = p;
6391 }
6392 if (after > var_start + lhs->lhs_varlen)
6393 {
6394 lhs->lhs_varlen = after - var_start;
6395 lhs->lhs_dest = dest_expr;
6396 // We don't know the type before evaluating the expression,
6397 // use "any" until then.
6398 lhs->lhs_type = &t_any;
6399 }
6400
Bram Moolenaar752fc692021-01-04 21:57:11 +01006401 if (lhs->lhs_type->tt_member == NULL)
6402 lhs->lhs_member_type = &t_any;
6403 else
6404 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6405 }
6406 else
6407 {
6408 semsg("Not supported yet: %s", var_start);
6409 return FAIL;
6410 }
6411 }
6412 return OK;
6413}
6414
6415/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006416 * Figure out the LHS and check a few errors.
6417 */
6418 static int
6419compile_assign_lhs(
6420 char_u *var_start,
6421 lhs_T *lhs,
6422 int cmdidx,
6423 int is_decl,
6424 int heredoc,
6425 int oplen,
6426 cctx_T *cctx)
6427{
6428 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6429 return FAIL;
6430
6431 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6432 {
6433 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6434 return FAIL;
6435 }
6436 if (!is_decl && lhs->lhs_lvar != NULL
6437 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6438 {
6439 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6440 return FAIL;
6441 }
6442 return OK;
6443}
6444
6445/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006446 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6447 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006448 */
6449 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006450compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006451 char_u *var_start,
6452 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006453 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006454 cctx_T *cctx)
6455{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006456 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006457 char_u *p;
6458 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006459 int need_white_before = TRUE;
6460 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006461
Bram Moolenaar752fc692021-01-04 21:57:11 +01006462 p = var_start + varlen;
6463 if (*p == '[')
6464 {
6465 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006466 if (*p == ':')
6467 {
6468 // empty first index, push zero
6469 r = generate_PUSHNR(cctx, 0);
6470 need_white_before = FALSE;
6471 }
6472 else
6473 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006474
6475 if (r == OK && *skipwhite(p) == ':')
6476 {
6477 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006478 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006479 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006480 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006481 empty_second = *skipwhite(p + 1) == ']';
6482 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6483 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006484 {
6485 semsg(_(e_white_space_required_before_and_after_str_at_str),
6486 ":", p);
6487 return FAIL;
6488 }
6489 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006490 if (*p == ']')
6491 // empty second index, push "none"
6492 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6493 else
6494 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006495 }
6496
Bram Moolenaar752fc692021-01-04 21:57:11 +01006497 if (r == OK && *skipwhite(p) != ']')
6498 {
6499 // this should not happen
6500 emsg(_(e_missbrac));
6501 r = FAIL;
6502 }
6503 }
6504 else // if (*p == '.')
6505 {
6506 char_u *key_end = to_name_end(p + 1, TRUE);
6507 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6508
6509 r = generate_PUSHS(cctx, key);
6510 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006511 return r;
6512}
6513
6514/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006515 * For a LHS with an index, load the variable to be indexed.
6516 */
6517 static int
6518compile_load_lhs(
6519 lhs_T *lhs,
6520 char_u *var_start,
6521 type_T *rhs_type,
6522 cctx_T *cctx)
6523{
6524 if (lhs->lhs_dest == dest_expr)
6525 {
6526 size_t varlen = lhs->lhs_varlen;
6527 int c = var_start[varlen];
6528 char_u *p = var_start;
6529 garray_T *stack = &cctx->ctx_type_stack;
6530
6531 // Evaluate "ll[expr]" of "ll[expr][idx]"
6532 var_start[varlen] = NUL;
6533 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6534 {
6535 // this should not happen
6536 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006537 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006538 return FAIL;
6539 }
6540 var_start[varlen] = c;
6541
6542 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6543 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6544 // now we can properly check the type
6545 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6546 && rhs_type != &t_void
6547 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6548 FALSE, FALSE) == FAIL)
6549 return FAIL;
6550 }
6551 else
6552 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6553 lhs->lhs_lvar, lhs->lhs_type);
6554 return OK;
6555}
6556
6557/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006558 * Produce code for loading "lhs" and also take care of an index.
6559 * Return OK/FAIL.
6560 */
6561 static int
6562compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6563{
6564 compile_load_lhs(lhs, var_start, NULL, cctx);
6565
6566 if (lhs->lhs_has_index)
6567 {
6568 int range = FALSE;
6569
6570 // Get member from list or dict. First compile the
6571 // index value.
6572 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6573 return FAIL;
6574 if (range)
6575 {
6576 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6577 var_start);
6578 return FAIL;
6579 }
6580
6581 // Get the member.
6582 if (compile_member(FALSE, cctx) == FAIL)
6583 return FAIL;
6584 }
6585 return OK;
6586}
6587
6588/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006589 * Assignment to a list or dict member, or ":unlet" for the item, using the
6590 * information in "lhs".
6591 * Returns OK or FAIL.
6592 */
6593 static int
6594compile_assign_unlet(
6595 char_u *var_start,
6596 lhs_T *lhs,
6597 int is_assign,
6598 type_T *rhs_type,
6599 cctx_T *cctx)
6600{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006601 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006602 garray_T *stack = &cctx->ctx_type_stack;
6603 int range = FALSE;
6604
Bram Moolenaar68452172021-04-12 21:21:02 +02006605 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006606 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006607 if (is_assign && range && lhs->lhs_type != &t_blob
6608 && lhs->lhs_type != &t_any)
6609 {
6610 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6611 return FAIL;
6612 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006613
6614 if (lhs->lhs_type == &t_any)
6615 {
6616 // Index on variable of unknown type: check at runtime.
6617 dest_type = VAR_ANY;
6618 }
6619 else
6620 {
6621 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006622 if (dest_type == VAR_DICT && range)
6623 {
6624 emsg(e_cannot_use_range_with_dictionary);
6625 return FAIL;
6626 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006627 if (dest_type == VAR_DICT
6628 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006629 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006630 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006631 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006632 type_T *type;
6633
6634 if (range)
6635 {
6636 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6637 if (need_type(type, &t_number,
6638 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006639 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006640 }
6641 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006642 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006643 && need_type(type, &t_number,
6644 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006645 return FAIL;
6646 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006647 }
6648
6649 // Load the dict or list. On the stack we then have:
6650 // - value (for assignment, not for :unlet)
6651 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006652 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006653 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006654 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6655 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006656
Bram Moolenaar68452172021-04-12 21:21:02 +02006657 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6658 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006659 {
6660 if (is_assign)
6661 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006662 if (range)
6663 {
6664 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6665 return FAIL;
6666 }
6667 else
6668 {
6669 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006670
Bram Moolenaar68452172021-04-12 21:21:02 +02006671 if (isn == NULL)
6672 return FAIL;
6673 isn->isn_arg.vartype = dest_type;
6674 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006675 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006676 else if (range)
6677 {
6678 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6679 return FAIL;
6680 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006681 else
6682 {
6683 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6684 return FAIL;
6685 }
6686 }
6687 else
6688 {
6689 emsg(_(e_indexable_type_required));
6690 return FAIL;
6691 }
6692
6693 return OK;
6694}
6695
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006696/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006697 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006698 * "let name"
6699 * "var name = expr"
6700 * "final name = expr"
6701 * "const name = expr"
6702 * "name = expr"
6703 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006704 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006705 * Return NULL for an error.
6706 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006707 */
6708 static char_u *
6709compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6710{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006711 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006713 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 char_u *ret = NULL;
6715 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006716 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006718 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006720 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006722 int oplen = 0;
6723 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006724 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006725 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006727 int is_decl = is_decl_command(cmdidx);
6728 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006729 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006730
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006731 // Skip over the "var" or "[var, var]" to get to any "=".
6732 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6733 if (p == NULL)
6734 return *arg == '[' ? arg : NULL;
6735
6736 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006738 // TODO: should we allow this, and figure out type inference from list
6739 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006740 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006741 return NULL;
6742 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006743 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 sp = p;
6746 p = skipwhite(p);
6747 op = p;
6748 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006749
6750 if (var_count > 0 && oplen == 0)
6751 // can be something like "[1, 2]->func()"
6752 return arg;
6753
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006754 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006755 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006756 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006757 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006758 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006759 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6760 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006761 if (VIM_ISWHITE(eap->cmd[2]))
6762 {
6763 semsg(_(e_no_white_space_allowed_after_str_str),
6764 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6765 return NULL;
6766 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006767 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6768 oplen = 2;
6769 incdec = TRUE;
6770 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 if (heredoc)
6773 {
6774 list_T *l;
6775 listitem_T *li;
6776
6777 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006778 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006780 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006781 if (l == NULL)
6782 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006783
Bram Moolenaar078269b2020-09-21 20:35:55 +02006784 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006785 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006786 // Push each line and the create the list.
6787 FOR_ALL_LIST_ITEMS(l, li)
6788 {
6789 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6790 li->li_tv.vval.v_string = NULL;
6791 }
6792 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006794 list_free(l);
6795 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006796 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006798 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006799 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006800 char_u *wp;
6801
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006802 // for "[var, var] = expr" evaluate the expression here, loop over the
6803 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006804 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006805
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006806 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006807 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006808 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006809 if (compile_expr0(&p, cctx) == FAIL)
6810 return NULL;
6811 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006813 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006814 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006815 type_T *stacktype;
6816
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006817 stacktype = stack->ga_len == 0 ? &t_void
6818 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006819 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006821 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006822 goto theend;
6823 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006824 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006825 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006826 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006827 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006828 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6829 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006830 if (stacktype->tt_member != NULL)
6831 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006832 }
6833 }
6834
6835 /*
6836 * Loop over variables in "[var, var] = expr".
6837 * For "var = expr" and "let var: type" this is done only once.
6838 */
6839 if (var_count > 0)
6840 var_start = skipwhite(arg + 1); // skip over the "["
6841 else
6842 var_start = arg;
6843 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6844 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006845 int instr_count = -1;
6846 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006847
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006848 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6849 {
6850 // Ignore underscore in "[a, _, b] = list".
6851 if (var_count > 0)
6852 {
6853 var_start = skipwhite(var_start + 2);
6854 continue;
6855 }
6856 emsg(_(e_cannot_use_underscore_here));
6857 goto theend;
6858 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006859 vim_free(lhs.lhs_name);
6860
6861 /*
6862 * Figure out the LHS type and other properties.
6863 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006864 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6865 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006866 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006867 if (!heredoc)
6868 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006869 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006870 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006871 if (oplen > 0 && var_count == 0)
6872 {
6873 // skip over the "=" and the expression
6874 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006875 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006876 }
6877 }
6878 else if (oplen > 0)
6879 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006880 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006881 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006882
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006883 // for "+=", "*=", "..=" etc. first load the current value
6884 if (*op != '='
6885 && compile_load_lhs_with_index(&lhs, var_start,
6886 cctx) == FAIL)
6887 goto theend;
6888
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006889 // For "var = expr" evaluate the expression.
6890 if (var_count == 0)
6891 {
6892 int r;
6893
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006894 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006895 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006896 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006897 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006898 r = generate_PUSHNR(cctx, 1);
6899 }
6900 else
6901 {
6902 // Temporarily hide the new local variable here, it is
6903 // not available to this expression.
6904 if (lhs.lhs_new_local)
6905 --cctx->ctx_locals.ga_len;
6906 wp = op + oplen;
6907 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6908 {
6909 if (lhs.lhs_new_local)
6910 ++cctx->ctx_locals.ga_len;
6911 goto theend;
6912 }
6913 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006914 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006915 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006916 if (r == FAIL)
6917 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006918 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006919 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006920 else if (semicolon && var_idx == var_count - 1)
6921 {
6922 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006923 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006924 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6925 goto theend;
6926 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006927 else
6928 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006929 // For "[var, var] = expr" get the "var_idx" item from the
6930 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006931 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006932 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006933 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006934
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006935 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006936 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006937 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006938 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006939 if ((rhs_type->tt_type == VAR_FUNC
6940 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006941 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006942 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006943 goto theend;
6944
Bram Moolenaar752fc692021-01-04 21:57:11 +01006945 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006946 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006947 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006948 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006949 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006950 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006951 }
6952 else
6953 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006954 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006955 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006956 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006957 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006958 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006959 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006960 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006961 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006962 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006963 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006964 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006965 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006966 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006967 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006968 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006969
Bram Moolenaar77709b12021-04-03 21:01:01 +02006970 // Without operator check type here, otherwise below.
6971 // Use the line number of the assignment.
6972 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006973 if (lhs.lhs_has_index)
6974 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006975 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006976 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006977 goto theend;
6978 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006979 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006980 else
6981 {
6982 type_T *lhs_type = lhs.lhs_member_type;
6983
6984 // Special case: assigning to @# can use a number or a
6985 // string.
6986 if (lhs_type == &t_number_or_string
6987 && rhs_type->tt_type == VAR_NUMBER)
6988 lhs_type = &t_number;
6989 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006990 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006991 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006994 else if (cmdidx == CMD_final)
6995 {
6996 emsg(_(e_final_requires_a_value));
6997 goto theend;
6998 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006999 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007001 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007002 goto theend;
7003 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007004 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007005 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007006 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007007 goto theend;
7008 }
7009 else
7010 {
7011 // variables are always initialized
7012 if (ga_grow(instr, 1) == FAIL)
7013 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007014 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007015 {
7016 case VAR_BOOL:
7017 generate_PUSHBOOL(cctx, VVAL_FALSE);
7018 break;
7019 case VAR_FLOAT:
7020#ifdef FEAT_FLOAT
7021 generate_PUSHF(cctx, 0.0);
7022#endif
7023 break;
7024 case VAR_STRING:
7025 generate_PUSHS(cctx, NULL);
7026 break;
7027 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007028 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007029 break;
7030 case VAR_FUNC:
7031 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7032 break;
7033 case VAR_LIST:
7034 generate_NEWLIST(cctx, 0);
7035 break;
7036 case VAR_DICT:
7037 generate_NEWDICT(cctx, 0);
7038 break;
7039 case VAR_JOB:
7040 generate_PUSHJOB(cctx, NULL);
7041 break;
7042 case VAR_CHANNEL:
7043 generate_PUSHCHANNEL(cctx, NULL);
7044 break;
7045 case VAR_NUMBER:
7046 case VAR_UNKNOWN:
7047 case VAR_ANY:
7048 case VAR_PARTIAL:
7049 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007050 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007051 case VAR_SPECIAL: // cannot happen
7052 generate_PUSHNR(cctx, 0);
7053 break;
7054 }
7055 }
7056 if (var_count == 0)
7057 end = p;
7058 }
7059
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007060 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007061 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007062 break;
7063
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007064 if (oplen > 0 && *op != '=')
7065 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007066 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007067 type_T *stacktype;
7068
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007069 if (*op == '.')
7070 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007071 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007072 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007073 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007074 if (
7075#ifdef FEAT_FLOAT
7076 // If variable is float operation with number is OK.
7077 !(expected == &t_float && stacktype == &t_number) &&
7078#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007079 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007080 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007081 goto theend;
7082
7083 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007084 {
7085 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7086 goto theend;
7087 }
7088 else if (*op == '+')
7089 {
7090 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007091 operator_type(lhs.lhs_member_type, stacktype),
7092 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007093 goto theend;
7094 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007095 else if (generate_two_op(cctx, op) == FAIL)
7096 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007098
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007099 // Use the line number of the assignment for store instruction.
7100 save_lnum = cctx->ctx_lnum;
7101 cctx->ctx_lnum = start_lnum - 1;
7102
Bram Moolenaar752fc692021-01-04 21:57:11 +01007103 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007104 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007105 // Use the info in "lhs" to store the value at the index in the
7106 // list or dict.
7107 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7108 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007109 {
7110 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007111 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007112 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007113 }
7114 else
7115 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007116 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007117 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007118 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007119 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007120 generate_LOCKCONST(cctx);
7121
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007122 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007123 && (lhs.lhs_type->tt_type == VAR_DICT
7124 || lhs.lhs_type->tt_type == VAR_LIST)
7125 && lhs.lhs_type->tt_member != NULL
7126 && lhs.lhs_type->tt_member != &t_any
7127 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007128 // Set the type in the list or dict, so that it can be checked,
7129 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007130 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007131
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007132 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007133 {
7134 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007135 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007136 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007137 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007138 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007139
7140 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007141 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007143
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007144 // For "[var, var] = expr" drop the "expr" value.
7145 // Also for "[var, var; _] = expr".
7146 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007147 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007148 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007149 goto theend;
7150 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007151
Bram Moolenaarb2097502020-07-19 17:17:02 +02007152 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007153
7154theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007155 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007156 return ret;
7157}
7158
7159/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007160 * Check for an assignment at "eap->cmd", compile it if found.
7161 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7162 */
7163 static int
7164may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7165{
7166 char_u *pskip;
7167 char_u *p;
7168
7169 // Assuming the command starts with a variable or function name,
7170 // find what follows.
7171 // Skip over "var.member", "var[idx]" and the like.
7172 // Also "&opt = val", "$ENV = val" and "@r = val".
7173 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7174 ? eap->cmd + 1 : eap->cmd;
7175 p = to_name_end(pskip, TRUE);
7176 if (p > eap->cmd && *p != NUL)
7177 {
7178 char_u *var_end;
7179 int oplen;
7180 int heredoc;
7181
7182 if (eap->cmd[0] == '@')
7183 var_end = eap->cmd + 2;
7184 else
7185 var_end = find_name_end(pskip, NULL, NULL,
7186 FNE_CHECK_START | FNE_INCL_BR);
7187 oplen = assignment_len(skipwhite(var_end), &heredoc);
7188 if (oplen > 0)
7189 {
7190 size_t len = p - eap->cmd;
7191
7192 // Recognize an assignment if we recognize the variable
7193 // name:
7194 // "g:var = expr"
7195 // "local = expr" where "local" is a local var.
7196 // "script = expr" where "script" is a script-local var.
7197 // "import = expr" where "import" is an imported var
7198 // "&opt = expr"
7199 // "$ENV = expr"
7200 // "@r = expr"
7201 if (*eap->cmd == '&'
7202 || *eap->cmd == '$'
7203 || *eap->cmd == '@'
7204 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007205 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007206 {
7207 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7208 if (*line == NULL || *line == eap->cmd)
7209 return FAIL;
7210 return OK;
7211 }
7212 }
7213 }
7214
7215 if (*eap->cmd == '[')
7216 {
7217 // [var, var] = expr
7218 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7219 if (*line == NULL)
7220 return FAIL;
7221 if (*line != eap->cmd)
7222 return OK;
7223 }
7224 return NOTDONE;
7225}
7226
7227/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007228 * Check if "name" can be "unlet".
7229 */
7230 int
7231check_vim9_unlet(char_u *name)
7232{
7233 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7234 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007235 // "unlet s:var" is allowed in legacy script.
7236 if (*name == 's' && !script_is_vim9())
7237 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007238 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007239 return FAIL;
7240 }
7241 return OK;
7242}
7243
7244/*
7245 * Callback passed to ex_unletlock().
7246 */
7247 static int
7248compile_unlet(
7249 lval_T *lvp,
7250 char_u *name_end,
7251 exarg_T *eap,
7252 int deep UNUSED,
7253 void *coookie)
7254{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007255 cctx_T *cctx = coookie;
7256 char_u *p = lvp->ll_name;
7257 int cc = *name_end;
7258 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007259
Bram Moolenaar752fc692021-01-04 21:57:11 +01007260 if (cctx->ctx_skip == SKIP_YES)
7261 return OK;
7262
7263 *name_end = NUL;
7264 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007265 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007266 // :unlet $ENV_VAR
7267 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7268 }
7269 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7270 {
7271 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007272
Bram Moolenaar752fc692021-01-04 21:57:11 +01007273 // This is similar to assigning: lookup the list/dict, compile the
7274 // idx/key. Then instead of storing the value unlet the item.
7275 // unlet {list}[idx]
7276 // unlet {dict}[key] dict.key
7277 //
7278 // Figure out the LHS type and other properties.
7279 //
7280 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7281
7282 // : unlet an indexed item
7283 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007284 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007285 iemsg("called compile_lhs() without an index");
7286 ret = FAIL;
7287 }
7288 else
7289 {
7290 // Use the info in "lhs" to unlet the item at the index in the
7291 // list or dict.
7292 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007293 }
7294
Bram Moolenaar752fc692021-01-04 21:57:11 +01007295 vim_free(lhs.lhs_name);
7296 }
7297 else if (check_vim9_unlet(p) == FAIL)
7298 {
7299 ret = FAIL;
7300 }
7301 else
7302 {
7303 // Normal name. Only supports g:, w:, t: and b: namespaces.
7304 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007305 }
7306
Bram Moolenaar752fc692021-01-04 21:57:11 +01007307 *name_end = cc;
7308 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007309}
7310
7311/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007312 * Callback passed to ex_unletlock().
7313 */
7314 static int
7315compile_lock_unlock(
7316 lval_T *lvp,
7317 char_u *name_end,
7318 exarg_T *eap,
7319 int deep UNUSED,
7320 void *coookie)
7321{
7322 cctx_T *cctx = coookie;
7323 int cc = *name_end;
7324 char_u *p = lvp->ll_name;
7325 int ret = OK;
7326 size_t len;
7327 char_u *buf;
7328
7329 if (cctx->ctx_skip == SKIP_YES)
7330 return OK;
7331
7332 // Cannot use :lockvar and :unlockvar on local variables.
7333 if (p[1] != ':')
7334 {
7335 char_u *end = skip_var_one(p, FALSE);
7336
7337 if (lookup_local(p, end - p, NULL, cctx) == OK)
7338 {
7339 emsg(_(e_cannot_lock_unlock_local_variable));
7340 return FAIL;
7341 }
7342 }
7343
7344 // Checking is done at runtime.
7345 *name_end = NUL;
7346 len = name_end - p + 20;
7347 buf = alloc(len);
7348 if (buf == NULL)
7349 ret = FAIL;
7350 else
7351 {
7352 vim_snprintf((char *)buf, len, "%s %s",
7353 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7354 p);
7355 ret = generate_EXEC(cctx, buf);
7356
7357 vim_free(buf);
7358 *name_end = cc;
7359 }
7360 return ret;
7361}
7362
7363/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007364 * compile "unlet var", "lock var" and "unlock var"
7365 * "arg" points to "var".
7366 */
7367 static char_u *
7368compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7369{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007370 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7371 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7372 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007373 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7374}
7375
7376/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7378 */
7379 static int
7380compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7381{
7382 garray_T *instr = &cctx->ctx_instr;
7383 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7384
7385 if (endlabel == NULL)
7386 return FAIL;
7387 endlabel->el_next = *el;
7388 *el = endlabel;
7389 endlabel->el_end_label = instr->ga_len;
7390
7391 generate_JUMP(cctx, when, 0);
7392 return OK;
7393}
7394
7395 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007396compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397{
7398 garray_T *instr = &cctx->ctx_instr;
7399
7400 while (*el != NULL)
7401 {
7402 endlabel_T *cur = (*el);
7403 isn_T *isn;
7404
7405 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007406 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 *el = cur->el_next;
7408 vim_free(cur);
7409 }
7410}
7411
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007412 static void
7413compile_free_jump_to_end(endlabel_T **el)
7414{
7415 while (*el != NULL)
7416 {
7417 endlabel_T *cur = (*el);
7418
7419 *el = cur->el_next;
7420 vim_free(cur);
7421 }
7422}
7423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424/*
7425 * Create a new scope and set up the generic items.
7426 */
7427 static scope_T *
7428new_scope(cctx_T *cctx, scopetype_T type)
7429{
7430 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7431
7432 if (scope == NULL)
7433 return NULL;
7434 scope->se_outer = cctx->ctx_scope;
7435 cctx->ctx_scope = scope;
7436 scope->se_type = type;
7437 scope->se_local_count = cctx->ctx_locals.ga_len;
7438 return scope;
7439}
7440
7441/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007442 * Free the current scope and go back to the outer scope.
7443 */
7444 static void
7445drop_scope(cctx_T *cctx)
7446{
7447 scope_T *scope = cctx->ctx_scope;
7448
7449 if (scope == NULL)
7450 {
7451 iemsg("calling drop_scope() without a scope");
7452 return;
7453 }
7454 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007455 switch (scope->se_type)
7456 {
7457 case IF_SCOPE:
7458 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7459 case FOR_SCOPE:
7460 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7461 case WHILE_SCOPE:
7462 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7463 case TRY_SCOPE:
7464 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7465 case NO_SCOPE:
7466 case BLOCK_SCOPE:
7467 break;
7468 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007469 vim_free(scope);
7470}
7471
7472/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473 * compile "if expr"
7474 *
7475 * "if expr" Produces instructions:
7476 * EVAL expr Push result of "expr"
7477 * JUMP_IF_FALSE end
7478 * ... body ...
7479 * end:
7480 *
7481 * "if expr | else" Produces instructions:
7482 * EVAL expr Push result of "expr"
7483 * JUMP_IF_FALSE else
7484 * ... body ...
7485 * JUMP_ALWAYS end
7486 * else:
7487 * ... body ...
7488 * end:
7489 *
7490 * "if expr1 | elseif expr2 | else" Produces instructions:
7491 * EVAL expr Push result of "expr"
7492 * JUMP_IF_FALSE elseif
7493 * ... body ...
7494 * JUMP_ALWAYS end
7495 * elseif:
7496 * EVAL expr Push result of "expr"
7497 * JUMP_IF_FALSE else
7498 * ... body ...
7499 * JUMP_ALWAYS end
7500 * else:
7501 * ... body ...
7502 * end:
7503 */
7504 static char_u *
7505compile_if(char_u *arg, cctx_T *cctx)
7506{
7507 char_u *p = arg;
7508 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007509 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007511 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007512 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513
Bram Moolenaara5565e42020-05-09 15:44:01 +02007514 CLEAR_FIELD(ppconst);
7515 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007516 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007517 clear_ppconst(&ppconst);
7518 return NULL;
7519 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007520 if (!ends_excmd2(arg, skipwhite(p)))
7521 {
7522 semsg(_(e_trailing_arg), p);
7523 return NULL;
7524 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007525 if (cctx->ctx_skip == SKIP_YES)
7526 clear_ppconst(&ppconst);
7527 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007528 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007529 int error = FALSE;
7530 int v;
7531
Bram Moolenaara5565e42020-05-09 15:44:01 +02007532 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007533 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007534 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007535 if (error)
7536 return NULL;
7537 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007538 }
7539 else
7540 {
7541 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007542 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007543 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007544 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007545 if (bool_on_stack(cctx) == FAIL)
7546 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548
Bram Moolenaara91a7132021-03-25 21:12:15 +01007549 // CMDMOD_REV must come before the jump
7550 generate_undo_cmdmods(cctx);
7551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552 scope = new_scope(cctx, IF_SCOPE);
7553 if (scope == NULL)
7554 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007555 scope->se_skip_save = skip_save;
7556 // "is_had_return" will be reset if any block does not end in :return
7557 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007558
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007559 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007560 {
7561 // "where" is set when ":elseif", "else" or ":endif" is found
7562 scope->se_u.se_if.is_if_label = instr->ga_len;
7563 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7564 }
7565 else
7566 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567
Bram Moolenaarced68a02021-01-24 17:53:47 +01007568#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007569 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007570 && skip_save != SKIP_YES)
7571 {
7572 // generated a profile start, need to generate a profile end, since it
7573 // won't be done after returning
7574 cctx->ctx_skip = SKIP_NOT;
7575 generate_instr(cctx, ISN_PROF_END);
7576 cctx->ctx_skip = SKIP_YES;
7577 }
7578#endif
7579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580 return p;
7581}
7582
7583 static char_u *
7584compile_elseif(char_u *arg, cctx_T *cctx)
7585{
7586 char_u *p = arg;
7587 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007588 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007589 isn_T *isn;
7590 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007591 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007592 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007593
7594 if (scope == NULL || scope->se_type != IF_SCOPE)
7595 {
7596 emsg(_(e_elseif_without_if));
7597 return NULL;
7598 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007599 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007600 if (!cctx->ctx_had_return)
7601 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602
Bram Moolenaarced68a02021-01-24 17:53:47 +01007603 if (cctx->ctx_skip == SKIP_NOT)
7604 {
7605 // previous block was executed, this one and following will not
7606 cctx->ctx_skip = SKIP_YES;
7607 scope->se_u.se_if.is_seen_skip_not = TRUE;
7608 }
7609 if (scope->se_u.se_if.is_seen_skip_not)
7610 {
7611 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007612 // Do not count the "elseif" for profiling and cmdmod
7613 instr->ga_len = current_instr_idx(cctx);
7614
Bram Moolenaarced68a02021-01-24 17:53:47 +01007615 skip_expr_cctx(&p, cctx);
7616 return p;
7617 }
7618
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007619 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007620 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007621 int moved_cmdmod = FALSE;
7622
7623 // Move any CMDMOD instruction to after the jump
7624 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7625 {
7626 if (ga_grow(instr, 1) == FAIL)
7627 return NULL;
7628 ((isn_T *)instr->ga_data)[instr->ga_len] =
7629 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7630 --instr->ga_len;
7631 moved_cmdmod = TRUE;
7632 }
7633
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007634 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007636 return NULL;
7637 // previous "if" or "elseif" jumps here
7638 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7639 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007640 if (moved_cmdmod)
7641 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007642 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007643
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007644 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007645 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007646 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007647 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007648 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007649#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007650 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007651 {
7652 // the previous block was skipped, need to profile this line
7653 generate_instr(cctx, ISN_PROF_START);
7654 instr_count = instr->ga_len;
7655 }
7656#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007657 if (cctx->ctx_compile_type == CT_DEBUG)
7658 {
7659 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007660 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007661 instr_count = instr->ga_len;
7662 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007663 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007664 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007665 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007666 clear_ppconst(&ppconst);
7667 return NULL;
7668 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007669 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007670 if (!ends_excmd2(arg, skipwhite(p)))
7671 {
7672 semsg(_(e_trailing_arg), p);
7673 return NULL;
7674 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007675 if (scope->se_skip_save == SKIP_YES)
7676 clear_ppconst(&ppconst);
7677 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007678 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007679 int error = FALSE;
7680 int v;
7681
Bram Moolenaar7f141552020-05-09 17:35:53 +02007682 // The expression results in a constant.
7683 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007684 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7685 if (error)
7686 return NULL;
7687 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007688 clear_ppconst(&ppconst);
7689 scope->se_u.se_if.is_if_label = -1;
7690 }
7691 else
7692 {
7693 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007694 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007695 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007696 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007697 if (bool_on_stack(cctx) == FAIL)
7698 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007699
Bram Moolenaara91a7132021-03-25 21:12:15 +01007700 // CMDMOD_REV must come before the jump
7701 generate_undo_cmdmods(cctx);
7702
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007703 // "where" is set when ":elseif", "else" or ":endif" is found
7704 scope->se_u.se_if.is_if_label = instr->ga_len;
7705 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007707
7708 return p;
7709}
7710
7711 static char_u *
7712compile_else(char_u *arg, cctx_T *cctx)
7713{
7714 char_u *p = arg;
7715 garray_T *instr = &cctx->ctx_instr;
7716 isn_T *isn;
7717 scope_T *scope = cctx->ctx_scope;
7718
7719 if (scope == NULL || scope->se_type != IF_SCOPE)
7720 {
7721 emsg(_(e_else_without_if));
7722 return NULL;
7723 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007724 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007725 if (!cctx->ctx_had_return)
7726 scope->se_u.se_if.is_had_return = FALSE;
7727 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007728
Bram Moolenaarced68a02021-01-24 17:53:47 +01007729#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007730 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007731 {
7732 if (cctx->ctx_skip == SKIP_NOT
7733 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7734 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007735 // the previous block was executed, do not count "else" for
7736 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007737 --instr->ga_len;
7738 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7739 {
7740 // the previous block was not executed, this one will, do count the
7741 // "else" for profiling
7742 cctx->ctx_skip = SKIP_NOT;
7743 generate_instr(cctx, ISN_PROF_END);
7744 generate_instr(cctx, ISN_PROF_START);
7745 cctx->ctx_skip = SKIP_YES;
7746 }
7747 }
7748#endif
7749
7750 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007751 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007752 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007753 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007754 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007755 if (!cctx->ctx_had_return
7756 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7757 JUMP_ALWAYS, cctx) == FAIL)
7758 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007759 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007760
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007761 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007762 {
7763 if (scope->se_u.se_if.is_if_label >= 0)
7764 {
7765 // previous "if" or "elseif" jumps here
7766 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7767 isn->isn_arg.jump.jump_where = instr->ga_len;
7768 scope->se_u.se_if.is_if_label = -1;
7769 }
7770 }
7771
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007772 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007773 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007775
7776 return p;
7777}
7778
7779 static char_u *
7780compile_endif(char_u *arg, cctx_T *cctx)
7781{
7782 scope_T *scope = cctx->ctx_scope;
7783 ifscope_T *ifscope;
7784 garray_T *instr = &cctx->ctx_instr;
7785 isn_T *isn;
7786
Bram Moolenaarfa984412021-03-25 22:15:28 +01007787 if (misplaced_cmdmod(cctx))
7788 return NULL;
7789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790 if (scope == NULL || scope->se_type != IF_SCOPE)
7791 {
7792 emsg(_(e_endif_without_if));
7793 return NULL;
7794 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007795 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007796 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007797 if (!cctx->ctx_had_return)
7798 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007800 if (scope->se_u.se_if.is_if_label >= 0)
7801 {
7802 // previous "if" or "elseif" jumps here
7803 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7804 isn->isn_arg.jump.jump_where = instr->ga_len;
7805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007806 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007807 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007808
7809#ifdef FEAT_PROFILE
7810 // even when skipping we count the endif as executed, unless the block it's
7811 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007812 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007813 && scope->se_skip_save != SKIP_YES)
7814 {
7815 cctx->ctx_skip = SKIP_NOT;
7816 generate_instr(cctx, ISN_PROF_START);
7817 }
7818#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007819 cctx->ctx_skip = scope->se_skip_save;
7820
7821 // If all the blocks end in :return and there is an :else then the
7822 // had_return flag is set.
7823 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007825 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826 return arg;
7827}
7828
7829/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007830 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007831 *
7832 * Produces instructions:
7833 * PUSHNR -1
7834 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007835 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7837 * - if beyond end, jump to "end"
7838 * - otherwise get item from list and push it
7839 * STORE var Store item in "var"
7840 * ... body ...
7841 * JUMP top Jump back to repeat
7842 * end: DROP Drop the result of "expr"
7843 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007844 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7845 * UNPACK 2 Split item in 2
7846 * STORE var1 Store item in "var1"
7847 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848 */
7849 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007850compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007851{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007852 char_u *arg;
7853 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007854 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007855 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007856 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007857 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007858 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007859 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007860 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007861 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007862 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007863 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007864 lvar_T *loop_lvar; // loop iteration variable
7865 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007866 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007867 type_T *item_type = &t_any;
7868 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007869 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870
Bram Moolenaar792f7862020-11-23 08:31:18 +01007871 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007872 if (p == NULL)
7873 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007874 if (var_count == 0)
7875 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007876 else
7877 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878
7879 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007880 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007881 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7882 return NULL;
7883 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007884 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007885 if (*p == ':' && wp != p)
7886 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7887 else
7888 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007889 return NULL;
7890 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007891 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007892 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7893 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007895 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7896 // instruction.
7897 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7898 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7899 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007900 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007901 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007902 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7903 .isn_arg.debug.dbg_break_lnum;
7904 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007906 scope = new_scope(cctx, FOR_SCOPE);
7907 if (scope == NULL)
7908 return NULL;
7909
Bram Moolenaar792f7862020-11-23 08:31:18 +01007910 // Reserve a variable to store the loop iteration counter and initialize it
7911 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007912 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7913 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007914 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007915 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007916 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007917 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007918 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007919 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007920
7921 // compile "expr", it remains on the stack until "endfor"
7922 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007923 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007924 {
7925 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007927 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007928 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007929
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007930 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007931 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007933 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007934 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007936 semsg(_(e_for_loop_on_str_not_supported),
7937 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007938 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007939 return NULL;
7940 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007941
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007942 if (vartype->tt_type == VAR_STRING)
7943 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007944 else if (vartype->tt_type == VAR_BLOB)
7945 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007946 else if (vartype->tt_type == VAR_LIST
7947 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007948 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007949 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007950 item_type = vartype->tt_member;
7951 else if (vartype->tt_member->tt_type == VAR_LIST
7952 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007953 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007954 item_type = vartype->tt_member->tt_member;
7955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007957 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007958 generate_undo_cmdmods(cctx);
7959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007960 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007961 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007962
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007963 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007964
Bram Moolenaar792f7862020-11-23 08:31:18 +01007965 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007966 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007967 {
7968 generate_UNPACK(cctx, var_count, semicolon);
7969 arg = skipwhite(arg + 1); // skip white after '['
7970
7971 // the list item is replaced by a number of items
7972 if (ga_grow(stack, var_count - 1) == FAIL)
7973 {
7974 drop_scope(cctx);
7975 return NULL;
7976 }
7977 --stack->ga_len;
7978 for (idx = 0; idx < var_count; ++idx)
7979 {
7980 ((type_T **)stack->ga_data)[stack->ga_len] =
7981 (semicolon && idx == 0) ? vartype : item_type;
7982 ++stack->ga_len;
7983 }
7984 }
7985
7986 for (idx = 0; idx < var_count; ++idx)
7987 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007988 assign_dest_T dest = dest_local;
7989 int opt_flags = 0;
7990 int vimvaridx = -1;
7991 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007992 type_T *lhs_type = &t_any;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02007993 where_T where = WHERE_INIT;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007994
7995 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007996 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007997 name = vim_strnsave(arg, varlen);
7998 if (name == NULL)
7999 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008000 if (*p == ':')
8001 {
8002 p = skipwhite(p + 1);
8003 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8004 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008005
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008006 // TODO: script var not supported?
8007 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
8008 &vimvaridx, &type, cctx) == FAIL)
8009 goto failed;
8010 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008011 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008012 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
8013 0, 0, type, name) == FAIL)
8014 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008015 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02008016 else if (varlen == 1 && *arg == '_')
8017 {
8018 // Assigning to "_": drop the value.
8019 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8020 goto failed;
8021 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008022 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008023 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01008024 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008025 {
8026 semsg(_(e_variable_already_declared), arg);
8027 goto failed;
8028 }
8029
Bram Moolenaarea870692020-12-02 14:24:30 +01008030 if (STRNCMP(name, "s:", 2) == 0)
8031 {
8032 semsg(_(e_cannot_declare_script_variable_in_function), name);
8033 goto failed;
8034 }
8035
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008036 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02008037 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008038 where.wt_variable = TRUE;
8039 if (lhs_type == &t_any)
8040 lhs_type = item_type;
8041 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02008042 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02008043 ? need_type(item_type, lhs_type,
8044 -1, 0, cctx, FALSE, FALSE)
8045 : check_type(lhs_type, item_type, TRUE, where))
8046 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008047 goto failed;
8048 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008049 if (var_lvar == NULL)
8050 // out of memory or used as an argument
8051 goto failed;
8052
8053 if (semicolon && idx == var_count - 1)
8054 var_lvar->lv_type = vartype;
8055 else
8056 var_lvar->lv_type = item_type;
8057 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8058 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008059
8060 if (*p == ',' || *p == ';')
8061 ++p;
8062 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008063 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008064 }
8065
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008066 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008067 {
8068 int save_prev_lnum = cctx->ctx_prev_lnum;
8069
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008070 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008071 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8072 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008073 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008074 cctx->ctx_prev_lnum = save_prev_lnum;
8075 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008076
Bram Moolenaar792f7862020-11-23 08:31:18 +01008077 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008078
8079failed:
8080 vim_free(name);
8081 drop_scope(cctx);
8082 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008083}
8084
8085/*
8086 * compile "endfor"
8087 */
8088 static char_u *
8089compile_endfor(char_u *arg, cctx_T *cctx)
8090{
8091 garray_T *instr = &cctx->ctx_instr;
8092 scope_T *scope = cctx->ctx_scope;
8093 forscope_T *forscope;
8094 isn_T *isn;
8095
Bram Moolenaarfa984412021-03-25 22:15:28 +01008096 if (misplaced_cmdmod(cctx))
8097 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008099 if (scope == NULL || scope->se_type != FOR_SCOPE)
8100 {
8101 emsg(_(e_for));
8102 return NULL;
8103 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008104 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008105 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008106 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008107
8108 // At end of ":for" scope jump back to the FOR instruction.
8109 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8110
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008111 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008112 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8113 isn->isn_arg.forloop.for_end = instr->ga_len;
8114
8115 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008116 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117
8118 // Below the ":for" scope drop the "expr" list from the stack.
8119 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8120 return NULL;
8121
8122 vim_free(scope);
8123
8124 return arg;
8125}
8126
8127/*
8128 * compile "while expr"
8129 *
8130 * Produces instructions:
8131 * top: EVAL expr Push result of "expr"
8132 * JUMP_IF_FALSE end jump if false
8133 * ... body ...
8134 * JUMP top Jump back to repeat
8135 * end:
8136 *
8137 */
8138 static char_u *
8139compile_while(char_u *arg, cctx_T *cctx)
8140{
8141 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008142 scope_T *scope;
8143
8144 scope = new_scope(cctx, WHILE_SCOPE);
8145 if (scope == NULL)
8146 return NULL;
8147
Bram Moolenaara91a7132021-03-25 21:12:15 +01008148 // "endwhile" jumps back here, one before when profiling or using cmdmods
8149 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008150
8151 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008152 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008154 if (!ends_excmd2(arg, skipwhite(p)))
8155 {
8156 semsg(_(e_trailing_arg), p);
8157 return NULL;
8158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008159
Bram Moolenaar13106602020-10-04 16:06:05 +02008160 if (bool_on_stack(cctx) == FAIL)
8161 return FAIL;
8162
Bram Moolenaara91a7132021-03-25 21:12:15 +01008163 // CMDMOD_REV must come before the jump
8164 generate_undo_cmdmods(cctx);
8165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008167 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008168 JUMP_IF_FALSE, cctx) == FAIL)
8169 return FAIL;
8170
8171 return p;
8172}
8173
8174/*
8175 * compile "endwhile"
8176 */
8177 static char_u *
8178compile_endwhile(char_u *arg, cctx_T *cctx)
8179{
8180 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008181 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008182
Bram Moolenaarfa984412021-03-25 22:15:28 +01008183 if (misplaced_cmdmod(cctx))
8184 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008185 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8186 {
8187 emsg(_(e_while));
8188 return NULL;
8189 }
8190 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008191 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008192
Bram Moolenaarf002a412021-01-24 13:34:18 +01008193#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008194 // count the endwhile before jumping
8195 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008196#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008198 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008199 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008200
8201 // Fill in the "end" label in the WHILE statement so it can jump here.
8202 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008203 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8204 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008205
8206 vim_free(scope);
8207
8208 return arg;
8209}
8210
8211/*
8212 * compile "continue"
8213 */
8214 static char_u *
8215compile_continue(char_u *arg, cctx_T *cctx)
8216{
8217 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008218 int try_scopes = 0;
8219 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008220
8221 for (;;)
8222 {
8223 if (scope == NULL)
8224 {
8225 emsg(_(e_continue));
8226 return NULL;
8227 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008228 if (scope->se_type == FOR_SCOPE)
8229 {
8230 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008231 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008232 }
8233 if (scope->se_type == WHILE_SCOPE)
8234 {
8235 loop_label = scope->se_u.se_while.ws_top_label;
8236 break;
8237 }
8238 if (scope->se_type == TRY_SCOPE)
8239 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008240 scope = scope->se_outer;
8241 }
8242
Bram Moolenaarc150c092021-02-13 15:02:46 +01008243 if (try_scopes > 0)
8244 // Inside one or more try/catch blocks we first need to jump to the
8245 // "finally" or "endtry" to cleanup.
8246 generate_TRYCONT(cctx, try_scopes, loop_label);
8247 else
8248 // Jump back to the FOR or WHILE instruction.
8249 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008251 return arg;
8252}
8253
8254/*
8255 * compile "break"
8256 */
8257 static char_u *
8258compile_break(char_u *arg, cctx_T *cctx)
8259{
8260 scope_T *scope = cctx->ctx_scope;
8261 endlabel_T **el;
8262
8263 for (;;)
8264 {
8265 if (scope == NULL)
8266 {
8267 emsg(_(e_break));
8268 return NULL;
8269 }
8270 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8271 break;
8272 scope = scope->se_outer;
8273 }
8274
8275 // Jump to the end of the FOR or WHILE loop.
8276 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008277 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008278 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008279 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008280 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8281 return FAIL;
8282
8283 return arg;
8284}
8285
8286/*
8287 * compile "{" start of block
8288 */
8289 static char_u *
8290compile_block(char_u *arg, cctx_T *cctx)
8291{
8292 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8293 return NULL;
8294 return skipwhite(arg + 1);
8295}
8296
8297/*
8298 * compile end of block: drop one scope
8299 */
8300 static void
8301compile_endblock(cctx_T *cctx)
8302{
8303 scope_T *scope = cctx->ctx_scope;
8304
8305 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008306 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008307 vim_free(scope);
8308}
8309
8310/*
8311 * compile "try"
8312 * Creates a new scope for the try-endtry, pointing to the first catch and
8313 * finally.
8314 * Creates another scope for the "try" block itself.
8315 * TRY instruction sets up exception handling at runtime.
8316 *
8317 * "try"
8318 * TRY -> catch1, -> finally push trystack entry
8319 * ... try block
8320 * "throw {exception}"
8321 * EVAL {exception}
8322 * THROW create exception
8323 * ... try block
8324 * " catch {expr}"
8325 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008326 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008327 * EVAL {expr}
8328 * MATCH
8329 * JUMP nomatch -> catch2
8330 * CATCH remove exception
8331 * ... catch block
8332 * " catch"
8333 * JUMP -> finally
8334 * catch2: CATCH remove exception
8335 * ... catch block
8336 * " finally"
8337 * finally:
8338 * ... finally block
8339 * " endtry"
8340 * ENDTRY pop trystack entry, may rethrow
8341 */
8342 static char_u *
8343compile_try(char_u *arg, cctx_T *cctx)
8344{
8345 garray_T *instr = &cctx->ctx_instr;
8346 scope_T *try_scope;
8347 scope_T *scope;
8348
Bram Moolenaarfa984412021-03-25 22:15:28 +01008349 if (misplaced_cmdmod(cctx))
8350 return NULL;
8351
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008352 // scope that holds the jumps that go to catch/finally/endtry
8353 try_scope = new_scope(cctx, TRY_SCOPE);
8354 if (try_scope == NULL)
8355 return NULL;
8356
Bram Moolenaar69f70502021-01-01 16:10:46 +01008357 if (cctx->ctx_skip != SKIP_YES)
8358 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008359 isn_T *isn;
8360
8361 // "try_catch" is set when the first ":catch" is found or when no catch
8362 // is found and ":finally" is found.
8363 // "try_finally" is set when ":finally" is found
8364 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008365 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008366 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8367 return NULL;
8368 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8369 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008370 return NULL;
8371 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008372
8373 // scope for the try block itself
8374 scope = new_scope(cctx, BLOCK_SCOPE);
8375 if (scope == NULL)
8376 return NULL;
8377
8378 return arg;
8379}
8380
8381/*
8382 * compile "catch {expr}"
8383 */
8384 static char_u *
8385compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8386{
8387 scope_T *scope = cctx->ctx_scope;
8388 garray_T *instr = &cctx->ctx_instr;
8389 char_u *p;
8390 isn_T *isn;
8391
Bram Moolenaarfa984412021-03-25 22:15:28 +01008392 if (misplaced_cmdmod(cctx))
8393 return NULL;
8394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008395 // end block scope from :try or :catch
8396 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8397 compile_endblock(cctx);
8398 scope = cctx->ctx_scope;
8399
8400 // Error if not in a :try scope
8401 if (scope == NULL || scope->se_type != TRY_SCOPE)
8402 {
8403 emsg(_(e_catch));
8404 return NULL;
8405 }
8406
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008407 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008408 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008409 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008410 return NULL;
8411 }
8412
Bram Moolenaar69f70502021-01-01 16:10:46 +01008413 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008414 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008415#ifdef FEAT_PROFILE
8416 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008417 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008418 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008419 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008420 .isn_type == ISN_PROF_START)
8421 --instr->ga_len;
8422#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008423 // Jump from end of previous block to :finally or :endtry
8424 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8425 JUMP_ALWAYS, cctx) == FAIL)
8426 return NULL;
8427
8428 // End :try or :catch scope: set value in ISN_TRY instruction
8429 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008430 if (isn->isn_arg.try.try_ref->try_catch == 0)
8431 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008432 if (scope->se_u.se_try.ts_catch_label != 0)
8433 {
8434 // Previous catch without match jumps here
8435 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8436 isn->isn_arg.jump.jump_where = instr->ga_len;
8437 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008438#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008439 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008440 {
8441 // a "throw" that jumps here needs to be counted
8442 generate_instr(cctx, ISN_PROF_END);
8443 // the "catch" is also counted
8444 generate_instr(cctx, ISN_PROF_START);
8445 }
8446#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008447 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008448 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008449 }
8450
8451 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008452 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008453 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008454 scope->se_u.se_try.ts_caught_all = TRUE;
8455 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008456 }
8457 else
8458 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008459 char_u *end;
8460 char_u *pat;
8461 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008462 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008463 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008465 // Push v:exception, push {expr} and MATCH
8466 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8467
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008468 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008469 if (*end != *p)
8470 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008471 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008472 vim_free(tofree);
8473 return FAIL;
8474 }
8475 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008476 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008477 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008478 len = (int)(end - tofree);
8479 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008480 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008481 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008482 if (pat == NULL)
8483 return FAIL;
8484 if (generate_PUSHS(cctx, pat) == FAIL)
8485 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008487 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8488 return NULL;
8489
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008490 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008491 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8492 return NULL;
8493 }
8494
Bram Moolenaar69f70502021-01-01 16:10:46 +01008495 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008496 return NULL;
8497
8498 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8499 return NULL;
8500 return p;
8501}
8502
8503 static char_u *
8504compile_finally(char_u *arg, cctx_T *cctx)
8505{
8506 scope_T *scope = cctx->ctx_scope;
8507 garray_T *instr = &cctx->ctx_instr;
8508 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008509 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008510
Bram Moolenaarfa984412021-03-25 22:15:28 +01008511 if (misplaced_cmdmod(cctx))
8512 return NULL;
8513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008514 // end block scope from :try or :catch
8515 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8516 compile_endblock(cctx);
8517 scope = cctx->ctx_scope;
8518
8519 // Error if not in a :try scope
8520 if (scope == NULL || scope->se_type != TRY_SCOPE)
8521 {
8522 emsg(_(e_finally));
8523 return NULL;
8524 }
8525
8526 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008527 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008528 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008529 {
8530 emsg(_(e_finally_dup));
8531 return NULL;
8532 }
8533
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008534 this_instr = instr->ga_len;
8535#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008536 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008537 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008538 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008539 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008540 // jump to the profile start of the "finally"
8541 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008542
8543 // jump to the profile end above it
8544 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8545 .isn_type == ISN_PROF_END)
8546 --this_instr;
8547 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008548#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008549
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008550 // Fill in the "end" label in jumps at the end of the blocks.
8551 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8552 this_instr, cctx);
8553
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008554 // If there is no :catch then an exception jumps to :finally.
8555 if (isn->isn_arg.try.try_ref->try_catch == 0)
8556 isn->isn_arg.try.try_ref->try_catch = this_instr;
8557 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008558 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008559 {
8560 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008561 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008562 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008563 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008564 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008565 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8566 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008568 // TODO: set index in ts_finally_label jumps
8569
8570 return arg;
8571}
8572
8573 static char_u *
8574compile_endtry(char_u *arg, cctx_T *cctx)
8575{
8576 scope_T *scope = cctx->ctx_scope;
8577 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008578 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008579
Bram Moolenaarfa984412021-03-25 22:15:28 +01008580 if (misplaced_cmdmod(cctx))
8581 return NULL;
8582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008583 // end block scope from :catch or :finally
8584 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8585 compile_endblock(cctx);
8586 scope = cctx->ctx_scope;
8587
8588 // Error if not in a :try scope
8589 if (scope == NULL || scope->se_type != TRY_SCOPE)
8590 {
8591 if (scope == NULL)
8592 emsg(_(e_no_endtry));
8593 else if (scope->se_type == WHILE_SCOPE)
8594 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008595 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008596 emsg(_(e_endfor));
8597 else
8598 emsg(_(e_endif));
8599 return NULL;
8600 }
8601
Bram Moolenaarc150c092021-02-13 15:02:46 +01008602 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008603 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008604 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008605 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8606 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008607 {
8608 emsg(_(e_missing_catch_or_finally));
8609 return NULL;
8610 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008611
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008612#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008613 if (cctx->ctx_compile_type == CT_PROFILE
8614 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008615 .isn_type == ISN_PROF_START)
8616 // move the profile start after "endtry" so that it's not counted when
8617 // the exception is rethrown.
8618 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008619#endif
8620
Bram Moolenaar69f70502021-01-01 16:10:46 +01008621 // Fill in the "end" label in jumps at the end of the blocks, if not
8622 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008623 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8624 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008625
Bram Moolenaar69f70502021-01-01 16:10:46 +01008626 if (scope->se_u.se_try.ts_catch_label != 0)
8627 {
8628 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008629 isn_T *isn = ((isn_T *)instr->ga_data)
8630 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008631 isn->isn_arg.jump.jump_where = instr->ga_len;
8632 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008633 }
8634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008635 compile_endblock(cctx);
8636
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008637 if (cctx->ctx_skip != SKIP_YES)
8638 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008639 // End :catch or :finally scope: set instruction index in ISN_TRY
8640 // instruction
8641 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008642 if (cctx->ctx_skip != SKIP_YES
8643 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8644 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008645#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008646 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008647 generate_instr(cctx, ISN_PROF_START);
8648#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008650 return arg;
8651}
8652
8653/*
8654 * compile "throw {expr}"
8655 */
8656 static char_u *
8657compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8658{
8659 char_u *p = skipwhite(arg);
8660
Bram Moolenaara5565e42020-05-09 15:44:01 +02008661 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008662 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008663 if (cctx->ctx_skip == SKIP_YES)
8664 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008665 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008666 return NULL;
8667 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8668 return NULL;
8669
8670 return p;
8671}
8672
Bram Moolenaarc3235272021-07-10 19:42:03 +02008673 static char_u *
8674compile_eval(char_u *arg, cctx_T *cctx)
8675{
8676 char_u *p = arg;
8677 int name_only;
8678 char_u *alias;
8679 long lnum = SOURCING_LNUM;
8680
8681 // find_ex_command() will consider a variable name an expression, assuming
8682 // that something follows on the next line. Check that something actually
8683 // follows, otherwise it's probably a misplaced command.
8684 get_name_len(&p, &alias, FALSE, FALSE);
8685 name_only = ends_excmd2(arg, skipwhite(p));
8686 vim_free(alias);
8687
8688 p = arg;
8689 if (compile_expr0(&p, cctx) == FAIL)
8690 return NULL;
8691
8692 if (name_only && lnum == SOURCING_LNUM)
8693 {
8694 semsg(_(e_expression_without_effect_str), arg);
8695 return NULL;
8696 }
8697
8698 // drop the result
8699 generate_instr_drop(cctx, ISN_DROP, 1);
8700
8701 return skipwhite(p);
8702}
8703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008704/*
8705 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008706 * compile "echomsg expr"
8707 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008708 * compile "execute expr"
8709 */
8710 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008711compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008712{
8713 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008714 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008715 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008716 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008717 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008718 garray_T *stack = &cctx->ctx_type_stack;
8719 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008720
8721 for (;;)
8722 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008723 if (ends_excmd2(prev, p))
8724 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008725 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008726 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008727 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008728
8729 if (cctx->ctx_skip != SKIP_YES)
8730 {
8731 // check for non-void type
8732 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8733 if (type->tt_type == VAR_VOID)
8734 {
8735 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8736 return NULL;
8737 }
8738 }
8739
Bram Moolenaarad39c092020-02-26 18:23:43 +01008740 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008741 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008742 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008743 }
8744
Bram Moolenaare4984292020-12-13 14:19:25 +01008745 if (count > 0)
8746 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008747 long save_lnum = cctx->ctx_lnum;
8748
8749 // Use the line number where the command started.
8750 cctx->ctx_lnum = start_ctx_lnum;
8751
Bram Moolenaare4984292020-12-13 14:19:25 +01008752 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8753 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8754 else if (cmdidx == CMD_execute)
8755 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8756 else if (cmdidx == CMD_echomsg)
8757 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8758 else
8759 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008760
8761 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008763 return p;
8764}
8765
8766/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008767 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008768 * instruction to compute it and return OK.
8769 * Otherwise return FAIL, the caller must deal with any range.
8770 */
8771 static int
8772compile_variable_range(exarg_T *eap, cctx_T *cctx)
8773{
8774 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8775 char_u *p = skipdigits(eap->cmd);
8776
8777 if (p == range_end)
8778 return FAIL;
8779 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8780}
8781
8782/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008783 * :put r
8784 * :put ={expr}
8785 */
8786 static char_u *
8787compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8788{
8789 char_u *line = arg;
8790 linenr_T lnum;
8791 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008792 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008793
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008794 eap->regname = *line;
8795
8796 if (eap->regname == '=')
8797 {
8798 char_u *p = line + 1;
8799
8800 if (compile_expr0(&p, cctx) == FAIL)
8801 return NULL;
8802 line = p;
8803 }
8804 else if (eap->regname != NUL)
8805 ++line;
8806
Bram Moolenaar08597872020-12-10 19:43:40 +01008807 if (compile_variable_range(eap, cctx) == OK)
8808 {
8809 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8810 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008811 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008812 {
8813 // Either no range or a number.
8814 // "errormsg" will not be set because the range is ADDR_LINES.
8815 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008816 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008817 return NULL;
8818 if (eap->addr_count == 0)
8819 lnum = -1;
8820 else
8821 lnum = eap->line2;
8822 if (above)
8823 --lnum;
8824 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008825
8826 generate_PUT(cctx, eap->regname, lnum);
8827 return line;
8828}
8829
8830/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008831 * A command that is not compiled, execute with legacy code.
8832 */
8833 static char_u *
8834compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8835{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008836 char_u *p;
8837 int has_expr = FALSE;
8838 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008839
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008840 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008841 goto theend;
8842
Bram Moolenaare729ce22021-06-06 21:38:09 +02008843 // If there was a prececing command modifier, drop it and include it in the
8844 // EXEC command.
8845 if (cctx->ctx_has_cmdmod)
8846 {
8847 garray_T *instr = &cctx->ctx_instr;
8848 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8849
8850 if (isn->isn_type == ISN_CMDMOD)
8851 {
8852 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8853 ->cmod_filter_regmatch.regprog);
8854 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8855 --instr->ga_len;
8856 cctx->ctx_has_cmdmod = FALSE;
8857 }
8858 }
8859
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008860 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008861 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008862 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008863 int usefilter = FALSE;
8864
8865 has_expr = argt & (EX_XFILE | EX_EXPAND);
8866
8867 // If the command can be followed by a bar, find the bar and truncate
8868 // it, so that the following command can be compiled.
8869 // The '|' is overwritten with a NUL, it is put back below.
8870 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8871 && *eap->arg == '!')
8872 // :w !filter or :r !filter or :r! filter
8873 usefilter = TRUE;
8874 if ((argt & EX_TRLBAR) && !usefilter)
8875 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008876 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008877 separate_nextcmd(eap);
8878 if (eap->nextcmd != NULL)
8879 nextcmd = eap->nextcmd;
8880 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008881 else if (eap->cmdidx == CMD_wincmd)
8882 {
8883 p = eap->arg;
8884 if (*p != NUL)
8885 ++p;
8886 if (*p == 'g' || *p == Ctrl_G)
8887 ++p;
8888 p = skipwhite(p);
8889 if (*p == '|')
8890 {
8891 *p = NUL;
8892 nextcmd = p + 1;
8893 }
8894 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008895 }
8896
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008897 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8898 {
8899 // expand filename in "syntax include [@group] filename"
8900 has_expr = TRUE;
8901 eap->arg = skipwhite(eap->arg + 7);
8902 if (*eap->arg == '@')
8903 eap->arg = skiptowhite(eap->arg);
8904 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008905
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008906 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8907 && STRLEN(eap->arg) > 4)
8908 {
8909 int delim = *eap->arg;
8910
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008911 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008912 if (*p == delim)
8913 {
8914 eap->arg = p + 1;
8915 has_expr = TRUE;
8916 }
8917 }
8918
Bram Moolenaarecac5912021-01-05 19:23:28 +01008919 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8920 {
8921 // TODO: should only expand when appropriate for the command
8922 eap->arg = skiptowhite(eap->arg);
8923 has_expr = TRUE;
8924 }
8925
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008926 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008927 {
8928 int count = 0;
8929 char_u *start = skipwhite(line);
8930
8931 // :cmd xxx`=expr1`yyy`=expr2`zzz
8932 // PUSHS ":cmd xxx"
8933 // eval expr1
8934 // PUSHS "yyy"
8935 // eval expr2
8936 // PUSHS "zzz"
8937 // EXECCONCAT 5
8938 for (;;)
8939 {
8940 if (p > start)
8941 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008942 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008943 ++count;
8944 }
8945 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008946 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008947 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008948 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008949 ++count;
8950 p = skipwhite(p);
8951 if (*p != '`')
8952 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008953 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008954 return NULL;
8955 }
8956 start = p + 1;
8957
8958 p = (char_u *)strstr((char *)start, "`=");
8959 if (p == NULL)
8960 {
8961 if (*skipwhite(start) != NUL)
8962 {
8963 generate_PUSHS(cctx, vim_strsave(start));
8964 ++count;
8965 }
8966 break;
8967 }
8968 }
8969 generate_EXECCONCAT(cctx, count);
8970 }
8971 else
8972 generate_EXEC(cctx, line);
8973
8974theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008975 if (*nextcmd != NUL)
8976 {
8977 // the parser expects a pointer to the bar, put it back
8978 --nextcmd;
8979 *nextcmd = '|';
8980 }
8981
8982 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008983}
8984
Bram Moolenaar20677332021-06-06 17:02:53 +02008985/*
8986 * A script command with heredoc, e.g.
8987 * ruby << EOF
8988 * command
8989 * EOF
8990 * Has been turned into one long line with NL characters by
8991 * get_function_body():
8992 * ruby << EOF<NL> command<NL>EOF
8993 */
8994 static char_u *
8995compile_script(char_u *line, cctx_T *cctx)
8996{
8997 if (cctx->ctx_skip != SKIP_YES)
8998 {
8999 isn_T *isn;
9000
9001 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9002 return NULL;
9003 isn->isn_arg.string = vim_strsave(line);
9004 }
9005 return (char_u *)"";
9006}
9007
Bram Moolenaar8238f082021-04-20 21:10:48 +02009008
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009009/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009010 * :s/pat/repl/
9011 */
9012 static char_u *
9013compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9014{
9015 char_u *cmd = eap->arg;
9016 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9017
9018 if (expr != NULL)
9019 {
9020 int delimiter = *cmd++;
9021
9022 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009023 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009024 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9025 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009026 garray_T save_ga = cctx->ctx_instr;
9027 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009028 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009029 int trailing_error;
9030 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009031 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009032 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009033
9034 cmd += 3;
9035 end = skip_substitute(cmd, delimiter);
9036
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009037 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009038 // labels are correct.
9039 cctx->ctx_instr.ga_len = 0;
9040 cctx->ctx_instr.ga_maxlen = 0;
9041 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009042 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009043 if (end[-1] == NUL)
9044 end[-1] = delimiter;
9045 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009046 trailing_error = *cmd != delimiter && *cmd != NUL;
9047
Bram Moolenaar169502f2021-04-21 12:19:35 +02009048 if (expr_res == FAIL || trailing_error
9049 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02009050 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009051 if (trailing_error)
9052 semsg(_(e_trailing_arg), cmd);
9053 clear_instr_ga(&cctx->ctx_instr);
9054 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009055 return NULL;
9056 }
9057
Bram Moolenaar8238f082021-04-20 21:10:48 +02009058 // Move the generated instructions into the ISN_SUBSTITUTE
9059 // instructions, then restore the list of instructions before
9060 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009061 instr_count = cctx->ctx_instr.ga_len;
9062 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009063 instr[instr_count].isn_type = ISN_FINISH;
9064
9065 cctx->ctx_instr = save_ga;
9066 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9067 {
9068 int idx;
9069
9070 for (idx = 0; idx < instr_count; ++idx)
9071 delete_instr(instr + idx);
9072 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009073 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009074 }
9075 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9076 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009077
9078 // skip over flags
9079 if (*end == '&')
9080 ++end;
9081 while (ASCII_ISALPHA(*end) || *end == '#')
9082 ++end;
9083 return end;
9084 }
9085 }
9086
9087 return compile_exec(arg, eap, cctx);
9088}
9089
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009090 static char_u *
9091compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9092{
9093 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009094 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009095
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009096 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009097 {
9098 if (STRNCMP(arg, "END", 3) == 0)
9099 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009100 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009101 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009102 // First load the current variable value.
9103 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9104 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009105 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009106 }
9107
9108 // Gets the redirected text and put it on the stack, then store it
9109 // in the variable.
9110 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9111
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009112 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009113 generate_instr_drop(cctx, ISN_CONCAT, 1);
9114
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009115 if (lhs->lhs_has_index)
9116 {
9117 // Use the info in "lhs" to store the value at the index in the
9118 // list or dict.
9119 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9120 &t_string, cctx) == FAIL)
9121 return NULL;
9122 }
9123 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009124 return NULL;
9125
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009126 VIM_CLEAR(lhs->lhs_name);
9127 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009128 return arg + 3;
9129 }
9130 emsg(_(e_cannot_nest_redir));
9131 return NULL;
9132 }
9133
9134 if (arg[0] == '=' && arg[1] == '>')
9135 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009136 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009137
9138 // redirect to a variable is compiled
9139 arg += 2;
9140 if (*arg == '>')
9141 {
9142 ++arg;
9143 append = TRUE;
9144 }
9145 arg = skipwhite(arg);
9146
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009147 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009148 FALSE, FALSE, 1, cctx) == FAIL)
9149 return NULL;
9150 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009151 lhs->lhs_append = append;
9152 if (lhs->lhs_has_index)
9153 {
9154 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9155 if (lhs->lhs_whole == NULL)
9156 return NULL;
9157 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009158
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009159 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009160 }
9161
9162 // other redirects are handled like at script level
9163 return compile_exec(line, eap, cctx);
9164}
9165
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009166#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009167 static char_u *
9168compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9169{
9170 isn_T *isn;
9171 char_u *p;
9172
9173 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9174 if (isn == NULL)
9175 return NULL;
9176 isn->isn_arg.number = eap->cmdidx;
9177
9178 p = eap->arg;
9179 if (compile_expr0(&p, cctx) == FAIL)
9180 return NULL;
9181
9182 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9183 if (isn == NULL)
9184 return NULL;
9185 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9186 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9187 return NULL;
9188 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9189 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9190 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9191
9192 return p;
9193}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009194#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009195
Bram Moolenaar4c137212021-04-19 16:48:48 +02009196/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009197 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009198 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009199 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009200 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009201add_def_function(ufunc_T *ufunc)
9202{
9203 dfunc_T *dfunc;
9204
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009205 if (def_functions.ga_len == 0)
9206 {
9207 // The first position is not used, so that a zero uf_dfunc_idx means it
9208 // wasn't set.
9209 if (ga_grow(&def_functions, 1) == FAIL)
9210 return FAIL;
9211 ++def_functions.ga_len;
9212 }
9213
Bram Moolenaar09689a02020-05-09 22:50:08 +02009214 // Add the function to "def_functions".
9215 if (ga_grow(&def_functions, 1) == FAIL)
9216 return FAIL;
9217 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9218 CLEAR_POINTER(dfunc);
9219 dfunc->df_idx = def_functions.ga_len;
9220 ufunc->uf_dfunc_idx = dfunc->df_idx;
9221 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009222 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009223 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009224 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009225 ++def_functions.ga_len;
9226 return OK;
9227}
9228
9229/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009230 * After ex_function() has collected all the function lines: parse and compile
9231 * the lines into instructions.
9232 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009233 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9234 * the return statement (used for lambda). When uf_ret_type is already set
9235 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009236 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009237 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009238 * This can be used recursively through compile_lambda(), which may reallocate
9239 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009240 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009241 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009242 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009243compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009244 ufunc_T *ufunc,
9245 int check_return_type,
9246 compiletype_T compile_type,
9247 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009248{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009249 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009250 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009251 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009252 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009253 cctx_T cctx;
9254 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009255 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009256 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009257 int ret = FAIL;
9258 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009259 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009260 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009261 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009262 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009263#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009264 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009265#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009266 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009267
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009268 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009269 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009270 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009271 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009272 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9273 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009274 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009275
9276 switch (compile_type)
9277 {
9278 case CT_PROFILE:
9279#ifdef FEAT_PROFILE
9280 instr_dest = dfunc->df_instr_prof; break;
9281#endif
9282 case CT_NONE: instr_dest = dfunc->df_instr; break;
9283 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9284 }
9285 if (instr_dest != NULL)
9286 // Was compiled in this mode before: Free old instructions.
9287 delete_def_function_contents(dfunc, FALSE);
9288 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009289 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009290 else
9291 {
9292 if (add_def_function(ufunc) == FAIL)
9293 return FAIL;
9294 new_def_function = TRUE;
9295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009296
Bram Moolenaar985116a2020-07-12 17:31:09 +02009297 ufunc->uf_def_status = UF_COMPILING;
9298
Bram Moolenaara80faa82020-04-12 19:37:17 +02009299 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009300
Bram Moolenaare99d4222021-06-13 14:01:26 +02009301 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009302 cctx.ctx_ufunc = ufunc;
9303 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009304 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009305 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9306 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9307 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9308 cctx.ctx_type_list = &ufunc->uf_type_list;
9309 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9310 instr = &cctx.ctx_instr;
9311
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009312 // Set the context to the function, it may be compiled when called from
9313 // another script. Set the script version to the most modern one.
9314 // The line number will be set in next_line_from_context().
9315 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009316 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9317
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009318 // Don't use the flag from ":legacy" here.
9319 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9320
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009321 // Make sure error messages are OK.
9322 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9323 if (do_estack_push)
9324 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009325 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009326
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009327 if (ufunc->uf_def_args.ga_len > 0)
9328 {
9329 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009330 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009331 int i;
9332 char_u *arg;
9333 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009334 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009335
9336 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009337 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009338 for (i = 0; i < count; ++i)
9339 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009340 garray_T *stack = &cctx.ctx_type_stack;
9341 type_T *val_type;
9342 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009343 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009344 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009345 int jump_instr_idx = instr->ga_len;
9346 isn_T *isn;
9347
9348 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9349 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9350 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009351
9352 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009353 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009354
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009355 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009356 r = compile_expr0(&arg, &cctx);
9357
Bram Moolenaar12bce952021-03-11 20:04:04 +01009358 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009359 goto erret;
9360
9361 // If no type specified use the type of the default value.
9362 // Otherwise check that the default value type matches the
9363 // specified type.
9364 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009365 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009366 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009367 {
9368 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009369 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009370 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009371 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009372 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009373 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009374
9375 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009376 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009377
9378 // set instruction index in JUMP_IF_ARG_SET to here
9379 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9380 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009381 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009382
9383 if (did_set_arg_type)
9384 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009385 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009386 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009387
9388 /*
9389 * Loop over all the lines of the function and generate instructions.
9390 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009391 for (;;)
9392 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009393 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009394 int starts_with_colon = FALSE;
9395 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009396 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009397
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009398 // Bail out on the first error to avoid a flood of errors and report
9399 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009400 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009401 goto erret;
9402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009403 if (line != NULL && *line == '|')
9404 // the line continues after a '|'
9405 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009406 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009407 && !(*line == '#' && (line == cctx.ctx_line_start
9408 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009409 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009410 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009411 goto erret;
9412 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009413 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9414 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009415 else
9416 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009417 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009418 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009419 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009420 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009421#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009422 if (cctx.ctx_skip != SKIP_YES)
9423 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009424#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009425 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009426 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009427 // Make a copy, splitting off nextcmd and removing trailing spaces
9428 // may change it.
9429 if (line != NULL)
9430 {
9431 line = vim_strsave(line);
9432 vim_free(line_to_free);
9433 line_to_free = line;
9434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009435 }
9436
Bram Moolenaara80faa82020-04-12 19:37:17 +02009437 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009438 ea.cmdlinep = &line;
9439 ea.cmd = skipwhite(line);
9440
Bram Moolenaarb2049902021-01-24 12:53:53 +01009441 if (*ea.cmd == '#')
9442 {
9443 // "#" starts a comment
9444 line = (char_u *)"";
9445 continue;
9446 }
9447
Bram Moolenaarf002a412021-01-24 13:34:18 +01009448#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009449 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9450 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009451 {
9452 may_generate_prof_end(&cctx, prof_lnum);
9453
9454 prof_lnum = cctx.ctx_lnum;
9455 generate_instr(&cctx, ISN_PROF_START);
9456 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009457#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009458 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9459 && cctx.ctx_skip != SKIP_YES)
9460 {
9461 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009462 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009463 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009464 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009465
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009466 // Some things can be recognized by the first character.
9467 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009468 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009469 case '}':
9470 {
9471 // "}" ends a block scope
9472 scopetype_T stype = cctx.ctx_scope == NULL
9473 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009474
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009475 if (stype == BLOCK_SCOPE)
9476 {
9477 compile_endblock(&cctx);
9478 line = ea.cmd;
9479 }
9480 else
9481 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009482 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009483 goto erret;
9484 }
9485 if (line != NULL)
9486 line = skipwhite(ea.cmd + 1);
9487 continue;
9488 }
9489
9490 case '{':
9491 // "{" starts a block scope
9492 // "{'a': 1}->func() is something else
9493 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9494 {
9495 line = compile_block(ea.cmd, &cctx);
9496 continue;
9497 }
9498 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009499 }
9500
9501 /*
9502 * COMMAND MODIFIERS
9503 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009504 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009505 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9506 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009507 {
9508 if (errormsg != NULL)
9509 goto erret;
9510 // empty line or comment
9511 line = (char_u *)"";
9512 continue;
9513 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009514 generate_cmdmods(&cctx, &local_cmdmod);
9515 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009516
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009517 // Check if there was a colon after the last command modifier or before
9518 // the current position.
9519 for (p = ea.cmd; p >= line; --p)
9520 {
9521 if (*p == ':')
9522 starts_with_colon = TRUE;
9523 if (p < ea.cmd && !VIM_ISWHITE(*p))
9524 break;
9525 }
9526
Bram Moolenaarce024c32021-06-26 13:00:49 +02009527 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009528 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009529 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009530 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009531 if (checkforcmd(&ea.cmd, "call", 3))
9532 {
9533 if (*ea.cmd == '(')
9534 // not for "call()"
9535 ea.cmd = p;
9536 else
9537 ea.cmd = skipwhite(ea.cmd);
9538 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009539
Bram Moolenaarce024c32021-06-26 13:00:49 +02009540 if (!starts_with_colon)
9541 {
9542 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009543
Bram Moolenaarce024c32021-06-26 13:00:49 +02009544 // Check for assignment after command modifiers.
9545 assign = may_compile_assignment(&ea, &line, &cctx);
9546 if (assign == OK)
9547 goto nextline;
9548 if (assign == FAIL)
9549 goto erret;
9550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009551 }
9552
9553 /*
9554 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009555 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009556 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009557 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009558 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009559 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9560 && (starts_with_colon || !(*cmd == '\''
9561 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009562 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009563 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009564 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009565 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009566 if (!starts_with_colon)
9567 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009568 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009569 goto erret;
9570 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009571 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009572 if (ends_excmd2(line, ea.cmd))
9573 {
9574 // A range without a command: jump to the line.
9575 // TODO: compile to a more efficient command, possibly
9576 // calling parse_cmd_address().
9577 ea.cmdidx = CMD_SIZE;
9578 line = compile_exec(line, &ea, &cctx);
9579 goto nextline;
9580 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009581 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009582 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009583 p = find_ex_command(&ea, NULL,
9584 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009585 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009586
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009587 if (p == NULL)
9588 {
9589 if (cctx.ctx_skip != SKIP_YES)
9590 emsg(_(e_ambiguous_use_of_user_defined_command));
9591 goto erret;
9592 }
9593
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009594 // When using ":legacy cmd" always use compile_exec().
9595 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009596 {
9597 char_u *start = ea.cmd;
9598
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009599 switch (ea.cmdidx)
9600 {
9601 case CMD_if:
9602 case CMD_elseif:
9603 case CMD_else:
9604 case CMD_endif:
9605 case CMD_for:
9606 case CMD_endfor:
9607 case CMD_continue:
9608 case CMD_break:
9609 case CMD_while:
9610 case CMD_endwhile:
9611 case CMD_try:
9612 case CMD_catch:
9613 case CMD_finally:
9614 case CMD_endtry:
9615 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9616 goto erret;
9617 default: break;
9618 }
9619
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009620 // ":legacy return expr" needs to be handled differently.
9621 if (checkforcmd(&start, "return", 4))
9622 ea.cmdidx = CMD_return;
9623 else
9624 ea.cmdidx = CMD_legacy;
9625 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009627 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9628 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009629 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009630 {
9631 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009632 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009633 }
9634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009635 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009636 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009637 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009638 // CMD_var cannot happen, compile_assignment() above would be
9639 // used. Most likely an assignment to a non-existing variable.
9640 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009641 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009642 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009643 }
9644
Bram Moolenaar3988f642020-08-27 22:43:03 +02009645 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009646 && ea.cmdidx != CMD_elseif
9647 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009648 && ea.cmdidx != CMD_endif
9649 && ea.cmdidx != CMD_endfor
9650 && ea.cmdidx != CMD_endwhile
9651 && ea.cmdidx != CMD_catch
9652 && ea.cmdidx != CMD_finally
9653 && ea.cmdidx != CMD_endtry)
9654 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009655 emsg(_(e_unreachable_code_after_return));
9656 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009657 }
9658
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009659 p = skipwhite(p);
9660 if (ea.cmdidx != CMD_SIZE
9661 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9662 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009663 if (ea.cmdidx >= 0)
9664 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009665 if ((ea.argt & EX_BANG) && *p == '!')
9666 {
9667 ea.forceit = TRUE;
9668 p = skipwhite(p + 1);
9669 }
9670 }
9671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009672 switch (ea.cmdidx)
9673 {
9674 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009675 ea.arg = p;
9676 line = compile_nested_function(&ea, &cctx);
9677 break;
9678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009679 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009680 // TODO: should we allow this, e.g. to declare a global
9681 // function?
9682 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009683 goto erret;
9684
9685 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009686 line = compile_return(p, check_return_type,
9687 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009688 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009689 break;
9690
9691 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009692 emsg(_(e_cannot_use_let_in_vim9_script));
9693 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009694 case CMD_var:
9695 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009696 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009697 case CMD_increment:
9698 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009699 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009700 if (line == p)
9701 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009702 break;
9703
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009704 case CMD_unlet:
9705 case CMD_unlockvar:
9706 case CMD_lockvar:
9707 line = compile_unletlock(p, &ea, &cctx);
9708 break;
9709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009710 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009711 emsg(_(e_import_can_only_be_used_in_script));
9712 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009713 break;
9714
9715 case CMD_if:
9716 line = compile_if(p, &cctx);
9717 break;
9718 case CMD_elseif:
9719 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009720 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009721 break;
9722 case CMD_else:
9723 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009724 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009725 break;
9726 case CMD_endif:
9727 line = compile_endif(p, &cctx);
9728 break;
9729
9730 case CMD_while:
9731 line = compile_while(p, &cctx);
9732 break;
9733 case CMD_endwhile:
9734 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009735 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009736 break;
9737
9738 case CMD_for:
9739 line = compile_for(p, &cctx);
9740 break;
9741 case CMD_endfor:
9742 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009743 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009744 break;
9745 case CMD_continue:
9746 line = compile_continue(p, &cctx);
9747 break;
9748 case CMD_break:
9749 line = compile_break(p, &cctx);
9750 break;
9751
9752 case CMD_try:
9753 line = compile_try(p, &cctx);
9754 break;
9755 case CMD_catch:
9756 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009757 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009758 break;
9759 case CMD_finally:
9760 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009761 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009762 break;
9763 case CMD_endtry:
9764 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009765 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009766 break;
9767 case CMD_throw:
9768 line = compile_throw(p, &cctx);
9769 break;
9770
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009771 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009772 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009773 break;
9774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009775 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009776 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009777 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009778 case CMD_echomsg:
9779 case CMD_echoerr:
9780 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009781 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009782
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009783 case CMD_put:
9784 ea.cmd = cmd;
9785 line = compile_put(p, &ea, &cctx);
9786 break;
9787
Bram Moolenaar4c137212021-04-19 16:48:48 +02009788 case CMD_substitute:
9789 if (cctx.ctx_skip == SKIP_YES)
9790 line = (char_u *)"";
9791 else
9792 {
9793 ea.arg = p;
9794 line = compile_substitute(line, &ea, &cctx);
9795 }
9796 break;
9797
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009798 case CMD_redir:
9799 ea.arg = p;
9800 line = compile_redir(line, &ea, &cctx);
9801 break;
9802
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009803 case CMD_cexpr:
9804 case CMD_lexpr:
9805 case CMD_caddexpr:
9806 case CMD_laddexpr:
9807 case CMD_cgetexpr:
9808 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009809#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009810 ea.arg = p;
9811 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009812#else
9813 ex_ni(&ea);
9814 line = NULL;
9815#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009816 break;
9817
Bram Moolenaar3988f642020-08-27 22:43:03 +02009818 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009819
Bram Moolenaarae616492020-07-28 20:07:27 +02009820 case CMD_append:
9821 case CMD_change:
9822 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009823 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009824 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009825 case CMD_xit:
9826 not_in_vim9(&ea);
9827 goto erret;
9828
Bram Moolenaar002262f2020-07-08 17:47:57 +02009829 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009830 if (cctx.ctx_skip != SKIP_YES)
9831 {
9832 semsg(_(e_invalid_command_str), ea.cmd);
9833 goto erret;
9834 }
9835 // We don't check for a next command here.
9836 line = (char_u *)"";
9837 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009838
Bram Moolenaar20677332021-06-06 17:02:53 +02009839 case CMD_lua:
9840 case CMD_mzscheme:
9841 case CMD_perl:
9842 case CMD_py3:
9843 case CMD_python3:
9844 case CMD_python:
9845 case CMD_pythonx:
9846 case CMD_ruby:
9847 case CMD_tcl:
9848 ea.arg = p;
9849 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009850 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009851 else
9852 // heredoc lines have been concatenated with NL
9853 // characters in get_function_body()
9854 line = compile_script(line, &cctx);
9855 break;
9856
9857 default:
9858 // Not recognized, execute with do_cmdline_cmd().
9859 ea.arg = p;
9860 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009861 break;
9862 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009863nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009864 if (line == NULL)
9865 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009866 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009867
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009868 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009869 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009871 if (cctx.ctx_type_stack.ga_len < 0)
9872 {
9873 iemsg("Type stack underflow");
9874 goto erret;
9875 }
9876 }
9877
9878 if (cctx.ctx_scope != NULL)
9879 {
9880 if (cctx.ctx_scope->se_type == IF_SCOPE)
9881 emsg(_(e_endif));
9882 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9883 emsg(_(e_endwhile));
9884 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9885 emsg(_(e_endfor));
9886 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009887 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009888 goto erret;
9889 }
9890
Bram Moolenaarefd88552020-06-18 20:50:10 +02009891 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009892 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009893 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9894 ufunc->uf_ret_type = &t_void;
9895 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009896 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009897 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009898 goto erret;
9899 }
9900
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009901 // Return void if there is no return at the end.
9902 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009903 }
9904
Bram Moolenaar599410c2021-04-10 14:03:43 +02009905 // When compiled with ":silent!" and there was an error don't consider the
9906 // function compiled.
9907 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009908 {
9909 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9910 + ufunc->uf_dfunc_idx;
9911 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009912 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009913#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009914 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009915 {
9916 dfunc->df_instr_prof = instr->ga_data;
9917 dfunc->df_instr_prof_count = instr->ga_len;
9918 }
9919 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009920#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009921 if (cctx.ctx_compile_type == CT_DEBUG)
9922 {
9923 dfunc->df_instr_debug = instr->ga_data;
9924 dfunc->df_instr_debug_count = instr->ga_len;
9925 }
9926 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009927 {
9928 dfunc->df_instr = instr->ga_data;
9929 dfunc->df_instr_count = instr->ga_len;
9930 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009931 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009932 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009933 if (cctx.ctx_outer_used)
9934 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009935 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009937
9938 ret = OK;
9939
9940erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009941 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009942 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009943 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9944 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009945
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009946 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009947 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009948 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009949 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009950
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009951 // If using the last entry in the table and it was added above, we
9952 // might as well remove it.
9953 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009954 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009955 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009956 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009957 ufunc->uf_dfunc_idx = 0;
9958 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009959 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009960
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009961 while (cctx.ctx_scope != NULL)
9962 drop_scope(&cctx);
9963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009964 if (errormsg != NULL)
9965 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009966 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009967 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009968 }
9969
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009970 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9971 {
9972 if (ret == OK)
9973 {
9974 emsg(_(e_missing_redir_end));
9975 ret = FAIL;
9976 }
9977 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009978 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009979 }
9980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009981 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009982 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009983 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009984 if (do_estack_push)
9985 estack_pop();
9986
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009987 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009988 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009989 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009990 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009991 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009992}
9993
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009994 void
9995set_function_type(ufunc_T *ufunc)
9996{
9997 int varargs = ufunc->uf_va_name != NULL;
9998 int argcount = ufunc->uf_args.ga_len;
9999
10000 // Create a type for the function, with the return type and any
10001 // argument types.
10002 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10003 // The type is included in "tt_args".
10004 if (argcount > 0 || varargs)
10005 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010006 if (ufunc->uf_type_list.ga_itemsize == 0)
10007 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010008 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10009 argcount, &ufunc->uf_type_list);
10010 // Add argument types to the function type.
10011 if (func_type_add_arg_types(ufunc->uf_func_type,
10012 argcount + varargs,
10013 &ufunc->uf_type_list) == FAIL)
10014 return;
10015 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10016 ufunc->uf_func_type->tt_min_argcount =
10017 argcount - ufunc->uf_def_args.ga_len;
10018 if (ufunc->uf_arg_types == NULL)
10019 {
10020 int i;
10021
10022 // lambda does not have argument types.
10023 for (i = 0; i < argcount; ++i)
10024 ufunc->uf_func_type->tt_args[i] = &t_any;
10025 }
10026 else
10027 mch_memmove(ufunc->uf_func_type->tt_args,
10028 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10029 if (varargs)
10030 {
10031 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010032 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010033 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10034 }
10035 }
10036 else
10037 // No arguments, can use a predefined type.
10038 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10039 argcount, &ufunc->uf_type_list);
10040}
10041
10042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010043/*
10044 * Delete an instruction, free what it contains.
10045 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010046 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010047delete_instr(isn_T *isn)
10048{
10049 switch (isn->isn_type)
10050 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010051 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010052 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010053 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010054 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010055 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010056 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010057 case ISN_LOADENV:
10058 case ISN_LOADG:
10059 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010060 case ISN_LOADT:
10061 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010062 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010063 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010064 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010065 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010066 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010067 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010068 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010069 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010070 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010071 case ISN_STOREW:
10072 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010073 vim_free(isn->isn_arg.string);
10074 break;
10075
Bram Moolenaar4c137212021-04-19 16:48:48 +020010076 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010077 {
10078 int idx;
10079 isn_T *list = isn->isn_arg.subs.subs_instr;
10080
10081 vim_free(isn->isn_arg.subs.subs_cmd);
10082 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10083 delete_instr(list + idx);
10084 vim_free(list);
10085 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010086 break;
10087
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010088 case ISN_INSTR:
10089 {
10090 int idx;
10091 isn_T *list = isn->isn_arg.instr;
10092
10093 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10094 delete_instr(list + idx);
10095 vim_free(list);
10096 }
10097 break;
10098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010099 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010100 case ISN_STORES:
10101 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010102 break;
10103
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010104 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010105 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010106 vim_free(isn->isn_arg.unlet.ul_name);
10107 break;
10108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010109 case ISN_STOREOPT:
10110 vim_free(isn->isn_arg.storeopt.so_name);
10111 break;
10112
10113 case ISN_PUSHBLOB: // push blob isn_arg.blob
10114 blob_unref(isn->isn_arg.blob);
10115 break;
10116
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010117 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010118#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010119 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010120#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010121 break;
10122
10123 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010124#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010125 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010126#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010127 break;
10128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010129 case ISN_UCALL:
10130 vim_free(isn->isn_arg.ufunc.cuf_name);
10131 break;
10132
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010133 case ISN_FUNCREF:
10134 {
10135 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10136 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010137 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010138
Bram Moolenaar077a4232020-12-22 18:33:27 +010010139 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10140 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010141 }
10142 break;
10143
10144 case ISN_DCALL:
10145 {
10146 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10147 + isn->isn_arg.dfunc.cdf_idx;
10148
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010149 if (dfunc->df_ufunc != NULL
10150 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010151 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010152 }
10153 break;
10154
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010155 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010156 {
10157 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10158 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10159
10160 if (ufunc != NULL)
10161 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010162 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010163 func_ptr_unref(ufunc);
10164 }
10165
10166 vim_free(lambda);
10167 vim_free(isn->isn_arg.newfunc.nf_global);
10168 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010169 break;
10170
Bram Moolenaar5e654232020-09-16 15:22:00 +020010171 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010172 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010173 free_type(isn->isn_arg.type.ct_type);
10174 break;
10175
Bram Moolenaar02194d22020-10-24 23:08:38 +020010176 case ISN_CMDMOD:
10177 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10178 ->cmod_filter_regmatch.regprog);
10179 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10180 break;
10181
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010182 case ISN_LOADSCRIPT:
10183 case ISN_STORESCRIPT:
10184 vim_free(isn->isn_arg.script.scriptref);
10185 break;
10186
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010187 case ISN_TRY:
10188 vim_free(isn->isn_arg.try.try_ref);
10189 break;
10190
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010191 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010192 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010193 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10194 break;
10195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010196 case ISN_2BOOL:
10197 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010198 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010199 case ISN_ADDBLOB:
10200 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010201 case ISN_ANYINDEX:
10202 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010203 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010204 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010205 case ISN_BLOBINDEX:
10206 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010207 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010208 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010209 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010210 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010211 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010212 case ISN_COMPAREANY:
10213 case ISN_COMPAREBLOB:
10214 case ISN_COMPAREBOOL:
10215 case ISN_COMPAREDICT:
10216 case ISN_COMPAREFLOAT:
10217 case ISN_COMPAREFUNC:
10218 case ISN_COMPARELIST:
10219 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010220 case ISN_COMPARESPECIAL:
10221 case ISN_COMPARESTRING:
10222 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010223 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010224 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010225 case ISN_DROP:
10226 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010227 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010228 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010229 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010230 case ISN_EXECCONCAT:
10231 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010232 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010233 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010234 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010235 case ISN_GETITEM:
10236 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010237 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010238 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010239 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010240 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010241 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010242 case ISN_LOADBDICT:
10243 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010244 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010245 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010246 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010247 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010248 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010249 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010250 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010251 case ISN_NEGATENR:
10252 case ISN_NEWDICT:
10253 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010254 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010255 case ISN_OPFLOAT:
10256 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010257 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010258 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010259 case ISN_PROF_END:
10260 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010261 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010262 case ISN_PUSHF:
10263 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010264 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010265 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010266 case ISN_REDIREND:
10267 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010268 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010269 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010270 case ISN_SHUFFLE:
10271 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010272 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010273 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010274 case ISN_STORENR:
10275 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010276 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010277 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010278 case ISN_STOREV:
10279 case ISN_STRINDEX:
10280 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010281 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010282 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010283 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010284 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010285 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010286 // nothing allocated
10287 break;
10288 }
10289}
10290
10291/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010292 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010293 */
10294 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010295delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010296{
10297 int idx;
10298
10299 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010300 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010301
10302 if (dfunc->df_instr != NULL)
10303 {
10304 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10305 delete_instr(dfunc->df_instr + idx);
10306 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010307 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010308 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010309 if (dfunc->df_instr_debug != NULL)
10310 {
10311 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10312 delete_instr(dfunc->df_instr_debug + idx);
10313 VIM_CLEAR(dfunc->df_instr_debug);
10314 dfunc->df_instr_debug = NULL;
10315 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010316#ifdef FEAT_PROFILE
10317 if (dfunc->df_instr_prof != NULL)
10318 {
10319 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10320 delete_instr(dfunc->df_instr_prof + idx);
10321 VIM_CLEAR(dfunc->df_instr_prof);
10322 dfunc->df_instr_prof = NULL;
10323 }
10324#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010325
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010326 if (mark_deleted)
10327 dfunc->df_deleted = TRUE;
10328 if (dfunc->df_ufunc != NULL)
10329 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010330}
10331
10332/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010333 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010334 * function, unless another user function still uses it.
10335 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010336 */
10337 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010338unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010339{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010340 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010341 {
10342 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10343 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010344
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010345 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010346 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010347 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010348 ufunc->uf_dfunc_idx = 0;
10349 if (dfunc->df_ufunc == ufunc)
10350 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010351 }
10352}
10353
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010354/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010355 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010356 */
10357 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010358link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010359{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010360 if (ufunc->uf_dfunc_idx > 0)
10361 {
10362 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10363 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010364
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010365 ++dfunc->df_refcount;
10366 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010367}
10368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010369#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010370/*
10371 * Free all functions defined with ":def".
10372 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010373 void
10374free_def_functions(void)
10375{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010376 int idx;
10377
10378 for (idx = 0; idx < def_functions.ga_len; ++idx)
10379 {
10380 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10381
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010382 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010383 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010384 }
10385
10386 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010387}
10388#endif
10389
10390
10391#endif // FEAT_EVAL