blob: 0d54db3d5b65038bf4a1a8ecaf2a10a0accdb001 [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 Moolenaarf7237012021-07-27 22:21:44 +02002133 if (ret_type == &t_unknown)
2134 // return type not known yet, use a runtime check
2135 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002136 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002137 else
2138 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002139 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002140 return FAIL;
2141 }
2142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2144 return FAIL;
2145 isn->isn_arg.pfunc.cpf_top = at_top;
2146 isn->isn_arg.pfunc.cpf_argcount = argcount;
2147
2148 stack->ga_len -= argcount; // drop the arguments
2149
2150 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002151 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002153 // If partial is above the arguments it must be cleared and replaced with
2154 // the return value.
2155 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2156 return FAIL;
2157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 return OK;
2159}
2160
2161/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002162 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 */
2164 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002165generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166{
2167 isn_T *isn;
2168 garray_T *stack = &cctx->ctx_type_stack;
2169 type_T *type;
2170
Bram Moolenaar080457c2020-03-03 21:53:32 +01002171 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002172 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002174 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002176 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002178 if (type->tt_type != VAR_DICT && type != &t_any)
2179 {
2180 emsg(_(e_dictreq));
2181 return FAIL;
2182 }
2183 // change dict type to dict member type
2184 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002185 {
2186 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2187 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2188 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189
2190 return OK;
2191}
2192
2193/*
2194 * Generate an ISN_ECHO instruction.
2195 */
2196 static int
2197generate_ECHO(cctx_T *cctx, int with_white, int count)
2198{
2199 isn_T *isn;
2200
Bram Moolenaar080457c2020-03-03 21:53:32 +01002201 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2203 return FAIL;
2204 isn->isn_arg.echo.echo_with_white = with_white;
2205 isn->isn_arg.echo.echo_count = count;
2206
2207 return OK;
2208}
2209
Bram Moolenaarad39c092020-02-26 18:23:43 +01002210/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002211 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002212 */
2213 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002214generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002215{
2216 isn_T *isn;
2217
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002218 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002219 return FAIL;
2220 isn->isn_arg.number = count;
2221
2222 return OK;
2223}
2224
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002225/*
2226 * Generate an ISN_PUT instruction.
2227 */
2228 static int
2229generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2230{
2231 isn_T *isn;
2232
2233 RETURN_OK_IF_SKIP(cctx);
2234 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2235 return FAIL;
2236 isn->isn_arg.put.put_regname = regname;
2237 isn->isn_arg.put.put_lnum = lnum;
2238 return OK;
2239}
2240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 static int
2242generate_EXEC(cctx_T *cctx, char_u *line)
2243{
2244 isn_T *isn;
2245
Bram Moolenaar080457c2020-03-03 21:53:32 +01002246 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2248 return FAIL;
2249 isn->isn_arg.string = vim_strsave(line);
2250 return OK;
2251}
2252
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002253 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002254generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2255{
2256 isn_T *isn;
2257 garray_T *stack = &cctx->ctx_type_stack;
2258
2259 RETURN_OK_IF_SKIP(cctx);
2260 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2261 return FAIL;
2262 isn->isn_arg.string = vim_strsave(line);
2263
2264 if (ga_grow(stack, 1) == FAIL)
2265 return FAIL;
2266 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2267 ++stack->ga_len;
2268
2269 return OK;
2270}
2271
2272 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002273generate_EXECCONCAT(cctx_T *cctx, int count)
2274{
2275 isn_T *isn;
2276
2277 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2278 return FAIL;
2279 isn->isn_arg.number = count;
2280 return OK;
2281}
2282
Bram Moolenaar08597872020-12-10 19:43:40 +01002283/*
2284 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2285 */
2286 static int
2287generate_RANGE(cctx_T *cctx, char_u *range)
2288{
2289 isn_T *isn;
2290 garray_T *stack = &cctx->ctx_type_stack;
2291
2292 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2293 return FAIL;
2294 isn->isn_arg.string = range;
2295
2296 if (ga_grow(stack, 1) == FAIL)
2297 return FAIL;
2298 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2299 ++stack->ga_len;
2300 return OK;
2301}
2302
Bram Moolenaar792f7862020-11-23 08:31:18 +01002303 static int
2304generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2305{
2306 isn_T *isn;
2307
2308 RETURN_OK_IF_SKIP(cctx);
2309 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2310 return FAIL;
2311 isn->isn_arg.unpack.unp_count = var_count;
2312 isn->isn_arg.unpack.unp_semicolon = semicolon;
2313 return OK;
2314}
2315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002317 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002318 */
2319 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002320generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002321{
2322 isn_T *isn;
2323
Bram Moolenaarfa984412021-03-25 22:15:28 +01002324 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002325 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002326 cctx->ctx_has_cmdmod = TRUE;
2327
2328 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002329 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002330 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2331 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2332 return FAIL;
2333 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002334 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002335 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002336 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002337
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002338 return OK;
2339}
2340
2341 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002342generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002343{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002344 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2345 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002346 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002347 return OK;
2348}
2349
Bram Moolenaarfa984412021-03-25 22:15:28 +01002350 static int
2351misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002352{
2353 garray_T *instr = &cctx->ctx_instr;
2354
Bram Moolenaara91a7132021-03-25 21:12:15 +01002355 if (cctx->ctx_has_cmdmod
2356 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2357 == ISN_CMDMOD)
2358 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002359 emsg(_(e_misplaced_command_modifier));
2360 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002361 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002362 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002363}
2364
2365/*
2366 * Get the index of the current instruction.
2367 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2368 */
2369 static int
2370current_instr_idx(cctx_T *cctx)
2371{
2372 garray_T *instr = &cctx->ctx_instr;
2373 int idx = instr->ga_len;
2374
Bram Moolenaare99d4222021-06-13 14:01:26 +02002375 while (idx > 0)
2376 {
2377 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002378 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002379 {
2380 --idx;
2381 continue;
2382 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002383#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002384 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2385 {
2386 --idx;
2387 continue;
2388 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002389#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002390 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2391 {
2392 --idx;
2393 continue;
2394 }
2395 break;
2396 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002397 return idx;
2398}
2399
Bram Moolenaarf002a412021-01-24 13:34:18 +01002400#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002401 static void
2402may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2403{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002404 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002405 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002406}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002407#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002408
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002409/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002411 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002413 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002414reserve_local(
2415 cctx_T *cctx,
2416 char_u *name,
2417 size_t len,
2418 int isConst,
2419 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002422 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002424 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002426 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002427 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 }
2429
2430 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002431 return NULL;
2432 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002433 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002435 // Every local variable uses the next entry on the stack. We could re-use
2436 // the last ones when leaving a scope, but then variables used in a closure
2437 // might get overwritten. To keep things simple do not re-use stack
2438 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002439 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2440 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002441
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002442 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 lvar->lv_const = isConst;
2444 lvar->lv_type = type;
2445
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002446 // Remember the name for debugging.
2447 if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2448 return NULL;
2449 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2450 vim_strsave(lvar->lv_name);
2451 ++dfunc->df_var_names.ga_len;
2452
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002453 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454}
2455
2456/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002457 * Remove local variables above "new_top".
2458 */
2459 static void
2460unwind_locals(cctx_T *cctx, int new_top)
2461{
2462 if (cctx->ctx_locals.ga_len > new_top)
2463 {
2464 int idx;
2465 lvar_T *lvar;
2466
2467 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2468 {
2469 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2470 vim_free(lvar->lv_name);
2471 }
2472 }
2473 cctx->ctx_locals.ga_len = new_top;
2474}
2475
2476/*
2477 * Free all local variables.
2478 */
2479 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002480free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002481{
2482 unwind_locals(cctx, 0);
2483 ga_clear(&cctx->ctx_locals);
2484}
2485
2486/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002487 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2488 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2489 * error if the variable was defined with :const.
2490 */
2491 static int
2492check_item_writable(svar_T *sv, int check_writable, char_u *name)
2493{
2494 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2495 || (check_writable == ASSIGN_FINAL
2496 && sv->sv_const == ASSIGN_CONST))
2497 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002498 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002499 return FAIL;
2500 }
2501 return OK;
2502}
2503
2504/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002506 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 * Returns the index in "sn_var_vals" if found.
2508 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002509 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 */
2511 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002512get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513{
2514 hashtab_T *ht;
2515 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002516 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002517 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 int idx;
2519
Bram Moolenaare3d46852020-08-29 13:39:17 +02002520 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002522 if (sid == current_sctx.sc_sid)
2523 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002524 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002525
2526 if (sav == NULL)
2527 return -2;
2528 idx = sav->sav_var_vals_idx;
2529 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002530 if (check_item_writable(sv, check_writable, name) == FAIL)
2531 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002532 return idx;
2533 }
2534
2535 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 ht = &SCRIPT_VARS(sid);
2537 di = find_var_in_ht(ht, 0, name, TRUE);
2538 if (di == NULL)
2539 return -2;
2540
2541 // Now find the svar_T index in sn_var_vals.
2542 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2543 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002544 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 if (sv->sv_tv == &di->di_tv)
2546 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002547 if (check_item_writable(sv, check_writable, name) == FAIL)
2548 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 return idx;
2550 }
2551 }
2552 return -1;
2553}
2554
2555/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002556 * Find "name" in imported items of the current script or in "cctx" if not
2557 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 */
2559 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002560find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 int idx;
2563
Bram Moolenaare3d46852020-08-29 13:39:17 +02002564 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002565 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 if (cctx != NULL)
2567 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2568 {
2569 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2570 + idx;
2571
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002572 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2573 : STRLEN(import->imp_name) == len
2574 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 return import;
2576 }
2577
Bram Moolenaarefa94442020-08-08 22:16:00 +02002578 return find_imported_in_script(name, len, current_sctx.sc_sid);
2579}
2580
2581 imported_T *
2582find_imported_in_script(char_u *name, size_t len, int sid)
2583{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002584 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002585 int idx;
2586
Bram Moolenaare3d46852020-08-29 13:39:17 +02002587 if (!SCRIPT_ID_VALID(sid))
2588 return NULL;
2589 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2591 {
2592 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2593
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002594 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2595 : STRLEN(import->imp_name) == len
2596 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 return import;
2598 }
2599 return NULL;
2600}
2601
2602/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002603 * Free all imported variables.
2604 */
2605 static void
2606free_imported(cctx_T *cctx)
2607{
2608 int idx;
2609
2610 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2611 {
2612 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2613
2614 vim_free(import->imp_name);
2615 }
2616 ga_clear(&cctx->ctx_imports);
2617}
2618
2619/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002620 * Return a pointer to the next line that isn't empty or only contains a
2621 * comment. Skips over white space.
2622 * Returns NULL if there is none.
2623 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002624 char_u *
2625peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002626{
2627 int lnum = cctx->ctx_lnum;
2628
2629 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2630 {
2631 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002632 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002633
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002634 // ignore NULLs inserted for continuation lines
2635 if (line != NULL)
2636 {
2637 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002638 if (vim9_bad_comment(p))
2639 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002640 if (*p != NUL && !vim9_comment_start(p))
2641 return p;
2642 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002643 }
2644 return NULL;
2645}
2646
2647/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002648 * Called when checking for a following operator at "arg". When the rest of
2649 * the line is empty or only a comment, peek the next line. If there is a next
2650 * line return a pointer to it and set "nextp".
2651 * Otherwise skip over white space.
2652 */
2653 static char_u *
2654may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2655{
2656 char_u *p = skipwhite(arg);
2657
2658 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002659 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002660 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002661 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002662 if (*nextp != NULL)
2663 return *nextp;
2664 }
2665 return p;
2666}
2667
2668/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002669 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002670 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002671 * Returns NULL when at the end.
2672 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002673 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002674next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002675{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002676 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002677
2678 do
2679 {
2680 ++cctx->ctx_lnum;
2681 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002682 {
2683 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002684 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002685 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002686 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002687 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002688 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002689 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002690 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002691 return line;
2692}
2693
2694/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002695 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002696 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002697 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002698 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2699 */
2700 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002701may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002702{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002703 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002704 if (vim9_bad_comment(*arg))
2705 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002706 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002707 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002708 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002709
2710 if (next == NULL)
2711 return FAIL;
2712 *arg = skipwhite(next);
2713 }
2714 return OK;
2715}
2716
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002717/*
2718 * Idem, and give an error when failed.
2719 */
2720 static int
2721may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2722{
2723 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2724 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002725 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002726 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002727 return FAIL;
2728 }
2729 return OK;
2730}
2731
2732
Bram Moolenaara5565e42020-05-09 15:44:01 +02002733// Structure passed between the compile_expr* functions to keep track of
2734// constants that have been parsed but for which no code was produced yet. If
2735// possible expressions on these constants are applied at compile time. If
2736// that is not possible, the code to push the constants needs to be generated
2737// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002738// Using 50 should be more than enough of 5 levels of ().
2739#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002740typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002741 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002742 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002743 int pp_is_const; // all generated code was constants, used for a
2744 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002745} ppconst_T;
2746
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002747static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002748static int compile_expr0(char_u **arg, cctx_T *cctx);
2749static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2750
Bram Moolenaara5565e42020-05-09 15:44:01 +02002751/*
2752 * Generate a PUSH instruction for "tv".
2753 * "tv" will be consumed or cleared.
2754 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2755 */
2756 static int
2757generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2758{
2759 if (tv != NULL)
2760 {
2761 switch (tv->v_type)
2762 {
2763 case VAR_UNKNOWN:
2764 break;
2765 case VAR_BOOL:
2766 generate_PUSHBOOL(cctx, tv->vval.v_number);
2767 break;
2768 case VAR_SPECIAL:
2769 generate_PUSHSPEC(cctx, tv->vval.v_number);
2770 break;
2771 case VAR_NUMBER:
2772 generate_PUSHNR(cctx, tv->vval.v_number);
2773 break;
2774#ifdef FEAT_FLOAT
2775 case VAR_FLOAT:
2776 generate_PUSHF(cctx, tv->vval.v_float);
2777 break;
2778#endif
2779 case VAR_BLOB:
2780 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2781 tv->vval.v_blob = NULL;
2782 break;
2783 case VAR_STRING:
2784 generate_PUSHS(cctx, tv->vval.v_string);
2785 tv->vval.v_string = NULL;
2786 break;
2787 default:
2788 iemsg("constant type not supported");
2789 clear_tv(tv);
2790 return FAIL;
2791 }
2792 tv->v_type = VAR_UNKNOWN;
2793 }
2794 return OK;
2795}
2796
2797/*
2798 * Generate code for any ppconst entries.
2799 */
2800 static int
2801generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2802{
2803 int i;
2804 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002805 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002806
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002807 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002808 for (i = 0; i < ppconst->pp_used; ++i)
2809 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2810 ret = FAIL;
2811 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002812 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002813 return ret;
2814}
2815
2816/*
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002817 * Check that the last item of "ppconst" is a bool.
2818 */
2819 static int
2820check_ppconst_bool(ppconst_T *ppconst)
2821{
2822 if (ppconst->pp_used > 0)
2823 {
2824 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002825 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002826
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002827 return check_typval_type(&t_bool, tv, where);
2828 }
2829 return OK;
2830}
2831
2832/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002833 * Clear ppconst constants. Used when failing.
2834 */
2835 static void
2836clear_ppconst(ppconst_T *ppconst)
2837{
2838 int i;
2839
2840 for (i = 0; i < ppconst->pp_used; ++i)
2841 clear_tv(&ppconst->pp_tv[i]);
2842 ppconst->pp_used = 0;
2843}
2844
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002845/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002846 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002847 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002848 */
2849 static int
2850compile_member(int is_slice, cctx_T *cctx)
2851{
2852 type_T **typep;
2853 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002854 vartype_T vartype;
2855 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002856
Bram Moolenaar261417b2021-05-06 21:04:55 +02002857 // We can index a list, dict and blob. If we don't know the type
2858 // we can use the index value type. If we still don't know use an "ANY"
2859 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002860 typep = ((type_T **)stack->ga_data) + stack->ga_len
2861 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002862 vartype = (*typep)->tt_type;
2863 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002864 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002865 if (*typep == &t_any && idxtype == &t_string)
2866 vartype = VAR_DICT;
2867 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002868 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002869 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002870 return FAIL;
2871 if (is_slice)
2872 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002873 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2874 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002875 FALSE, FALSE) == FAIL)
2876 return FAIL;
2877 }
2878 }
2879
Bram Moolenaar261417b2021-05-06 21:04:55 +02002880 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002881 {
2882 if (is_slice)
2883 {
2884 emsg(_(e_cannot_slice_dictionary));
2885 return FAIL;
2886 }
2887 if ((*typep)->tt_type == VAR_DICT)
2888 {
2889 *typep = (*typep)->tt_member;
2890 if (*typep == &t_unknown)
2891 // empty dict was used
2892 *typep = &t_any;
2893 }
2894 else
2895 {
2896 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2897 FALSE, FALSE) == FAIL)
2898 return FAIL;
2899 *typep = &t_any;
2900 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002901 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002902 return FAIL;
2903 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2904 return FAIL;
2905 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002906 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002907 {
2908 *typep = &t_string;
2909 if ((is_slice
2910 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2911 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2912 return FAIL;
2913 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002914 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002915 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002916 if (is_slice)
2917 {
2918 *typep = &t_blob;
2919 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2920 return FAIL;
2921 }
2922 else
2923 {
2924 *typep = &t_number;
2925 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2926 return FAIL;
2927 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002928 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002929 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002930 {
2931 if (is_slice)
2932 {
2933 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002934 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002935 2) == FAIL)
2936 return FAIL;
2937 }
2938 else
2939 {
2940 if ((*typep)->tt_type == VAR_LIST)
2941 {
2942 *typep = (*typep)->tt_member;
2943 if (*typep == &t_unknown)
2944 // empty list was used
2945 *typep = &t_any;
2946 }
2947 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002948 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2949 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002950 return FAIL;
2951 }
2952 }
2953 else
2954 {
2955 emsg(_(e_string_list_dict_or_blob_required));
2956 return FAIL;
2957 }
2958 return OK;
2959}
2960
2961/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002962 * Generate an instruction to load script-local variable "name", without the
2963 * leading "s:".
2964 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 */
2966 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002967compile_load_scriptvar(
2968 cctx_T *cctx,
2969 char_u *name, // variable NUL terminated
2970 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002971 char_u **end, // end of variable
2972 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002974 scriptitem_T *si;
2975 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 imported_T *import;
2977
Bram Moolenaare3d46852020-08-29 13:39:17 +02002978 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2979 return FAIL;
2980 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002981 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002982 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002984 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002985 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2986 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 }
2988 if (idx >= 0)
2989 {
2990 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2991
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002992 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 current_sctx.sc_sid, idx, sv->sv_type);
2994 return OK;
2995 }
2996
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002997 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 if (import != NULL)
2999 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003000 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003001 {
3002 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003003 char_u *exp_name;
3004 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003005 ufunc_T *ufunc;
3006 type_T *type;
3007
3008 // Used "import * as Name", need to lookup the member.
3009 if (*p != '.')
3010 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003011 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003012 return FAIL;
3013 }
3014 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003015 if (VIM_ISWHITE(*p))
3016 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003017 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003018 return FAIL;
3019 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003020
Bram Moolenaar1c991142020-07-04 13:15:31 +02003021 // isolate one name
3022 exp_name = p;
3023 while (eval_isnamec(*p))
3024 ++p;
3025 cc = *p;
3026 *p = NUL;
3027
Bram Moolenaaredba7072021-03-13 21:14:18 +01003028 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3029 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003030 *p = cc;
3031 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003032 *end = p;
3033
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003034 if (idx < 0)
3035 {
3036 if (*p == '(' && ufunc != NULL)
3037 {
3038 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3039 return OK;
3040 }
3041 return FAIL;
3042 }
3043
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003044 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3045 import->imp_sid,
3046 idx,
3047 type);
3048 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003049 else if (import->imp_funcname != NULL)
3050 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003051 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003052 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3053 import->imp_sid,
3054 import->imp_var_vals_idx,
3055 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 return OK;
3057 }
3058
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003059 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003060 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 return FAIL;
3062}
3063
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003064 static int
3065generate_funcref(cctx_T *cctx, char_u *name)
3066{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003067 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003068
3069 if (ufunc == NULL)
3070 return FAIL;
3071
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003072 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003073 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3074 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003075 == FAIL)
3076 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003077 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003078}
3079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080/*
3081 * Compile a variable name into a load instruction.
3082 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003083 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 * When "error" is FALSE do not give an error when not found.
3085 */
3086 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003087compile_load(
3088 char_u **arg,
3089 char_u *end_arg,
3090 cctx_T *cctx,
3091 int is_expr,
3092 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093{
3094 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003095 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003096 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003098 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099
3100 if (*(*arg + 1) == ':')
3101 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003102 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003103 {
3104 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105
Bram Moolenaarfa596382021-04-07 21:58:16 +02003106 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003107 switch (**arg)
3108 {
3109 case 'g': isn_type = ISN_LOADGDICT; break;
3110 case 'w': isn_type = ISN_LOADWDICT; break;
3111 case 't': isn_type = ISN_LOADTDICT; break;
3112 case 'b': isn_type = ISN_LOADBDICT; break;
3113 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003114 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003115 goto theend;
3116 }
3117 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3118 goto theend;
3119 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 else
3122 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003123 isntype_T isn_type = ISN_DROP;
3124
Bram Moolenaarfa596382021-04-07 21:58:16 +02003125 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003126 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3127 if (name == NULL)
3128 return FAIL;
3129
3130 switch (**arg)
3131 {
3132 case 'v': res = generate_LOADV(cctx, name, error);
3133 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003134 case 's': if (is_expr && ASCII_ISUPPER(*name)
3135 && find_func(name, FALSE, cctx) != NULL)
3136 res = generate_funcref(cctx, name);
3137 else
3138 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003139 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003140 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003141 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003142 {
3143 if (is_expr && ASCII_ISUPPER(*name)
3144 && find_func(name, FALSE, cctx) != NULL)
3145 res = generate_funcref(cctx, name);
3146 else
3147 isn_type = ISN_LOADG;
3148 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003149 else
3150 {
3151 isn_type = ISN_LOADAUTO;
3152 vim_free(name);
3153 name = vim_strnsave(*arg, end - *arg);
3154 if (name == NULL)
3155 return FAIL;
3156 }
3157 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003158 case 'w': isn_type = ISN_LOADW; break;
3159 case 't': isn_type = ISN_LOADT; break;
3160 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003161 default: // cannot happen, just in case
3162 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003163 goto theend;
3164 }
3165 if (isn_type != ISN_DROP)
3166 {
3167 // Global, Buffer-local, Window-local and Tabpage-local
3168 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003169 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003170 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 }
3173 }
3174 else
3175 {
3176 size_t len = end - *arg;
3177 int idx;
3178 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003179 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180
3181 name = vim_strnsave(*arg, end - *arg);
3182 if (name == NULL)
3183 return FAIL;
3184
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003185 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3186 {
3187 script_autoload(name, FALSE);
3188 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3189 }
3190 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3191 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003193 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003194 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 }
3196 else
3197 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003198 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003199
Bram Moolenaar709664c2020-12-12 14:33:41 +01003200 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003202 type = lvar.lv_type;
3203 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003204 if (lvar.lv_from_outer != 0)
3205 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003206 else
3207 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 }
3209 else
3210 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003211 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003212 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003213 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003214 || find_imported(name, 0, cctx) != NULL)
3215 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003216
Bram Moolenaar0f769812020-09-12 18:32:34 +02003217 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003218 // uppercase letter it can be a user defined function.
3219 // generate_funcref() will fail if the function can't be found.
3220 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003221 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 }
3223 }
3224 if (gen_load)
3225 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003226 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003227 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003228 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003229 cctx->ctx_outer_used = TRUE;
3230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 }
3232
3233 *arg = end;
3234
3235theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003236 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003237 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 vim_free(name);
3239 return res;
3240}
3241
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003242 static void
3243clear_instr_ga(garray_T *gap)
3244{
3245 int idx;
3246
3247 for (idx = 0; idx < gap->ga_len; ++idx)
3248 delete_instr(((isn_T *)gap->ga_data) + idx);
3249 ga_clear(gap);
3250}
3251
3252/*
3253 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3254 * Returns FAIL if compilation fails.
3255 */
3256 static int
3257compile_string(isn_T *isn, cctx_T *cctx)
3258{
3259 char_u *s = isn->isn_arg.string;
3260 garray_T save_ga = cctx->ctx_instr;
3261 int expr_res;
3262 int trailing_error;
3263 int instr_count;
3264 isn_T *instr = NULL;
3265
Bram Moolenaarcd268012021-07-22 19:11:08 +02003266 // Remove the string type from the stack.
3267 --cctx->ctx_type_stack.ga_len;
3268
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003269 // Temporarily reset the list of instructions so that the jump labels are
3270 // correct.
3271 cctx->ctx_instr.ga_len = 0;
3272 cctx->ctx_instr.ga_maxlen = 0;
3273 cctx->ctx_instr.ga_data = NULL;
3274 expr_res = compile_expr0(&s, cctx);
3275 s = skipwhite(s);
3276 trailing_error = *s != NUL;
3277
Bram Moolenaarff652882021-05-16 15:24:49 +02003278 if (expr_res == FAIL || trailing_error
3279 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003280 {
3281 if (trailing_error)
3282 semsg(_(e_trailing_arg), s);
3283 clear_instr_ga(&cctx->ctx_instr);
3284 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003285 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003286 return FAIL;
3287 }
3288
3289 // Move the generated instructions into the ISN_INSTR instruction, then
3290 // restore the list of instructions.
3291 instr_count = cctx->ctx_instr.ga_len;
3292 instr = cctx->ctx_instr.ga_data;
3293 instr[instr_count].isn_type = ISN_FINISH;
3294
3295 cctx->ctx_instr = save_ga;
3296 vim_free(isn->isn_arg.string);
3297 isn->isn_type = ISN_INSTR;
3298 isn->isn_arg.instr = instr;
3299 return OK;
3300}
3301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302/*
3303 * Compile the argument expressions.
3304 * "arg" points to just after the "(" and is advanced to after the ")"
3305 */
3306 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003307compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003309 char_u *p = *arg;
3310 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003311 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003312 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313
Bram Moolenaare6085c52020-04-12 20:19:16 +02003314 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003316 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3317 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003318 if (*p == ')')
3319 {
3320 *arg = p + 1;
3321 return OK;
3322 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003323 if (must_end)
3324 {
3325 semsg(_(e_missing_comma_before_argument_str), p);
3326 return FAIL;
3327 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003328
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003329 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003330 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 return FAIL;
3332 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003333
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003334 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003335 && cctx->ctx_instr.ga_len == instr_count + 1)
3336 {
3337 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3338
3339 // {skip} argument of searchpair() can be compiled if not empty
3340 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3341 compile_string(isn, cctx);
3342 }
3343
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003344 if (*p != ',' && *skipwhite(p) == ',')
3345 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003346 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003347 p = skipwhite(p);
3348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003350 {
3351 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003352 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003353 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003354 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003355 else
3356 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003357 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003358 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003360failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003361 emsg(_(e_missing_close));
3362 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363}
3364
3365/*
3366 * Compile a function call: name(arg1, arg2)
3367 * "arg" points to "name", "arg + varlen" to the "(".
3368 * "argcount_init" is 1 for "value->method()"
3369 * Instructions:
3370 * EVAL arg1
3371 * EVAL arg2
3372 * BCALL / DCALL / UCALL
3373 */
3374 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003375compile_call(
3376 char_u **arg,
3377 size_t varlen,
3378 cctx_T *cctx,
3379 ppconst_T *ppconst,
3380 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381{
3382 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003383 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 int argcount = argcount_init;
3385 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003386 char_u fname_buf[FLEN_FIXED + 1];
3387 char_u *tofree = NULL;
3388 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003389 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003390 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003391 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003392 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393
Bram Moolenaara5565e42020-05-09 15:44:01 +02003394 // we can evaluate "has('name')" at compile time
3395 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3396 {
3397 char_u *s = skipwhite(*arg + varlen + 1);
3398 typval_T argvars[2];
3399
3400 argvars[0].v_type = VAR_UNKNOWN;
3401 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003402 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003403 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003404 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003405 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003406 if (*s == ')' && argvars[0].v_type == VAR_STRING
3407 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003408 {
3409 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3410
3411 *arg = s + 1;
3412 argvars[1].v_type = VAR_UNKNOWN;
3413 tv->v_type = VAR_NUMBER;
3414 tv->vval.v_number = 0;
3415 f_has(argvars, tv);
3416 clear_tv(&argvars[0]);
3417 ++ppconst->pp_used;
3418 return OK;
3419 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003420 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003421 }
3422
3423 if (generate_ppconst(cctx, ppconst) == FAIL)
3424 return FAIL;
3425
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 if (varlen >= sizeof(namebuf))
3427 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003428 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 return FAIL;
3430 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003431 vim_strncpy(namebuf, *arg, varlen);
3432 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003434 // We handle the "skip" argument of searchpair() and searchpairpos()
3435 // differently.
3436 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3437 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3438 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3439 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003442 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003443 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444
Bram Moolenaar03290b82020-12-19 16:30:44 +01003445 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003446 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 {
3448 int idx;
3449
3450 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003451 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003453 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003454 if (STRCMP(name, "flatten") == 0)
3455 {
3456 emsg(_(e_cannot_use_flatten_in_vim9_script));
3457 goto theend;
3458 }
3459
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003460 if (STRCMP(name, "add") == 0 && argcount == 2)
3461 {
3462 garray_T *stack = &cctx->ctx_type_stack;
3463 type_T *type = ((type_T **)stack->ga_data)[
3464 stack->ga_len - 2];
3465
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003466 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003467 if (type->tt_type == VAR_LIST)
3468 {
3469 // inline "add(list, item)" so that the type can be checked
3470 res = generate_LISTAPPEND(cctx);
3471 idx = -1;
3472 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003473 else if (type->tt_type == VAR_BLOB)
3474 {
3475 // inline "add(blob, nr)" so that the type can be checked
3476 res = generate_BLOBAPPEND(cctx);
3477 idx = -1;
3478 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003479 }
3480
3481 if (idx >= 0)
3482 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3483 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003484 else
3485 semsg(_(e_unknownfunc), namebuf);
3486 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 }
3488
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003489 // An argument or local variable can be a function reference, this
3490 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003491 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003492 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003493 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003494 // If we can find the function by name generate the right call.
3495 // Skip global functions here, a local funcref takes precedence.
3496 ufunc = find_func(name, FALSE, cctx);
3497 if (ufunc != NULL && !func_is_global(ufunc))
3498 {
3499 res = generate_CALL(cctx, ufunc, argcount);
3500 goto theend;
3501 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503
3504 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003505 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003506 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003508 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003509 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003510 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003511 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003512 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003513
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003514 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003515 goto theend;
3516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517
Bram Moolenaar0f769812020-09-12 18:32:34 +02003518 // If we can find a global function by name generate the right call.
3519 if (ufunc != NULL)
3520 {
3521 res = generate_CALL(cctx, ufunc, argcount);
3522 goto theend;
3523 }
3524
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003525 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003526 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003527 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003528 res = generate_UCALL(cctx, name, argcount);
3529 else
3530 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003531
3532theend:
3533 vim_free(tofree);
3534 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535}
3536
3537// like NAMESPACE_CHAR but with 'a' and 'l'.
3538#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3539
3540/*
3541 * Find the end of a variable or function name. Unlike find_name_end() this
3542 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003543 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 * Return a pointer to just after the name. Equal to "arg" if there is no
3545 * valid name.
3546 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003547 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003548to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549{
3550 char_u *p;
3551
3552 // Quick check for valid starting character.
3553 if (!eval_isnamec1(*arg))
3554 return arg;
3555
3556 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3557 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3558 // and can be used in slice "[n:]".
3559 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003560 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3562 break;
3563 return p;
3564}
3565
3566/*
3567 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003568 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003569 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 */
3571 char_u *
3572to_name_const_end(char_u *arg)
3573{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003574 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 typval_T rettv;
3576
Bram Moolenaar678b2072021-07-26 21:10:11 +02003577 if (STRNCMP(p, "<SNR>", 5) == 0)
3578 p = skipdigits(p + 5);
3579 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 if (p == arg && *arg == '[')
3581 {
3582
3583 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003584 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 p = arg;
3586 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 return p;
3588}
3589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590/*
3591 * parse a list: [expr, expr]
3592 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003593 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 */
3595 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003596compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597{
3598 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003599 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003601 int is_const;
3602 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003604 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003606 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003607 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003608 semsg(_(e_list_end), *arg);
3609 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003610 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003611 if (*p == ',')
3612 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003613 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003614 return FAIL;
3615 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003616 if (*p == ']')
3617 {
3618 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003619 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003620 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003621 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003622 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003623 if (!is_const)
3624 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 ++count;
3626 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003627 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003629 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3630 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003631 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003632 return FAIL;
3633 }
3634 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003635 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 p = skipwhite(p);
3637 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003638 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003640 ppconst->pp_is_const = is_all_const;
3641 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642}
3643
3644/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003645 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003646 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003647 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 */
3649 static int
3650compile_lambda(char_u **arg, cctx_T *cctx)
3651{
Bram Moolenaare462f522020-12-27 14:43:30 +01003652 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 typval_T rettv;
3654 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003655 evalarg_T evalarg;
3656
3657 CLEAR_FIELD(evalarg);
3658 evalarg.eval_flags = EVAL_EVALUATE;
3659 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660
3661 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003662 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3663 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003664 {
3665 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003666 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003667 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003668
Bram Moolenaar65c44152020-12-24 15:14:01 +01003669 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003671 ++ufunc->uf_refcount;
3672 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673
Bram Moolenaara9931532021-06-12 15:58:16 +02003674 // Compile it here to get the return type. The return type is optional,
3675 // when it's missing use t_unknown. This is recognized in
3676 // compile_return().
3677 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3678 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003679 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680
Bram Moolenaar648594e2021-07-11 17:55:01 +02003681#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003682 // When the outer function is compiled for profiling, the lambda may be
3683 // called without profiling. Compile it here in the right context.
3684 if (cctx->ctx_compile_type == CT_PROFILE)
3685 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003686#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003687
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003688 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3689 // points into it. Point to the original line to avoid a dangling pointer.
3690 if (evalarg.eval_tofree_cmdline != NULL)
3691 {
3692 size_t off = *arg - evalarg.eval_tofree_cmdline;
3693
3694 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3695 + off;
3696 }
3697
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003698 clear_evalarg(&evalarg, NULL);
3699
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003700 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003701 {
3702 // The return type will now be known.
3703 set_function_type(ufunc);
3704
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003705 // The function reference count will be 1. When the ISN_FUNCREF
3706 // instruction is deleted the reference count is decremented and the
3707 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003708 return generate_FUNCREF(cctx, ufunc);
3709 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003710
3711 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 return FAIL;
3713}
3714
3715/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003716 * Get a lambda and compile it. Uses Vim9 syntax.
3717 */
3718 int
3719get_lambda_tv_and_compile(
3720 char_u **arg,
3721 typval_T *rettv,
3722 int types_optional,
3723 evalarg_T *evalarg)
3724{
3725 int r;
3726 ufunc_T *ufunc;
3727 int save_sc_version = current_sctx.sc_version;
3728
3729 // Get the funcref in "rettv".
3730 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3731 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3732 current_sctx.sc_version = save_sc_version;
3733 if (r != OK)
3734 return r;
3735
3736 // "rettv" will now be a partial referencing the function.
3737 ufunc = rettv->vval.v_partial->pt_func;
3738
3739 // Compile it here to get the return type. The return type is optional,
3740 // when it's missing use t_unknown. This is recognized in
3741 // compile_return().
3742 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3743 ufunc->uf_ret_type = &t_unknown;
3744 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3745
3746 if (ufunc->uf_def_status == UF_COMPILED)
3747 {
3748 // The return type will now be known.
3749 set_function_type(ufunc);
3750 return OK;
3751 }
3752 clear_tv(rettv);
3753 return FAIL;
3754}
3755
3756/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003757 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003759 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 */
3761 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003762compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763{
3764 garray_T *instr = &cctx->ctx_instr;
3765 int count = 0;
3766 dict_T *d = dict_alloc();
3767 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003768 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003769 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003770 int is_const;
3771 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772
3773 if (d == NULL)
3774 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003775 if (generate_ppconst(cctx, ppconst) == FAIL)
3776 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003777 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003779 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780
Bram Moolenaar23c55272020-06-21 16:58:13 +02003781 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003782 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003783 *arg = NULL;
3784 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003785 }
3786
3787 if (**arg == '}')
3788 break;
3789
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003790 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003792 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003794 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003795 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003796 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003799 if (isn->isn_type == ISN_PUSHNR)
3800 {
3801 char buf[NUMBUFLEN];
3802
3803 // Convert to string at compile time.
3804 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3805 isn->isn_type = ISN_PUSHS;
3806 isn->isn_arg.string = vim_strsave((char_u *)buf);
3807 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 if (isn->isn_type == ISN_PUSHS)
3809 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003810 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003811 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003812 *arg = skipwhite(*arg);
3813 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003814 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003815 emsg(_(e_missing_matching_bracket_after_dict_key));
3816 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003817 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003818 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003820 else
3821 {
3822 // {"name": value},
3823 // {'name': value},
3824 // {name: value} use "name" as a literal key
3825 key = get_literal_key(arg);
3826 if (key == NULL)
3827 return FAIL;
3828 if (generate_PUSHS(cctx, key) == FAIL)
3829 return FAIL;
3830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831
3832 // Check for duplicate keys, if using string keys.
3833 if (key != NULL)
3834 {
3835 item = dict_find(d, key, -1);
3836 if (item != NULL)
3837 {
3838 semsg(_(e_duplicate_key), key);
3839 goto failret;
3840 }
3841 item = dictitem_alloc(key);
3842 if (item != NULL)
3843 {
3844 item->di_tv.v_type = VAR_UNKNOWN;
3845 item->di_tv.v_lock = 0;
3846 if (dict_add(d, item) == FAIL)
3847 dictitem_free(item);
3848 }
3849 }
3850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 if (**arg != ':')
3852 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003853 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003854 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003855 else
3856 semsg(_(e_missing_dict_colon), *arg);
3857 return FAIL;
3858 }
3859 whitep = *arg + 1;
3860 if (!IS_WHITE_OR_NUL(*whitep))
3861 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003862 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 return FAIL;
3864 }
3865
Bram Moolenaar23c55272020-06-21 16:58:13 +02003866 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003867 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003868 *arg = NULL;
3869 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003870 }
3871
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003872 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003874 if (!is_const)
3875 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 ++count;
3877
Bram Moolenaar2c330432020-04-13 14:41:35 +02003878 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003879 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003880 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003881 *arg = NULL;
3882 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 if (**arg == '}')
3885 break;
3886 if (**arg != ',')
3887 {
3888 semsg(_(e_missing_dict_comma), *arg);
3889 goto failret;
3890 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003891 if (IS_WHITE_OR_NUL(*whitep))
3892 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003893 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003894 return FAIL;
3895 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003896 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003897 if (!IS_WHITE_OR_NUL(*whitep))
3898 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003899 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003900 return FAIL;
3901 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003902 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 }
3904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 *arg = *arg + 1;
3906
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003907 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003908 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003909 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003910 *arg += STRLEN(*arg);
3911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003913 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 return generate_NEWDICT(cctx, count);
3915
3916failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003917 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003918 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003919 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003920 *arg = (char_u *)"";
3921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 dict_unref(d);
3923 return FAIL;
3924}
3925
3926/*
3927 * Compile "&option".
3928 */
3929 static int
3930compile_get_option(char_u **arg, cctx_T *cctx)
3931{
3932 typval_T rettv;
3933 char_u *start = *arg;
3934 int ret;
3935
3936 // parse the option and get the current value to get the type.
3937 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003938 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 if (ret == OK)
3940 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003941 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003942 char_u *name = vim_strnsave(start, *arg - start);
3943 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3944 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945
3946 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3947 vim_free(name);
3948 }
3949 clear_tv(&rettv);
3950
3951 return ret;
3952}
3953
3954/*
3955 * Compile "$VAR".
3956 */
3957 static int
3958compile_get_env(char_u **arg, cctx_T *cctx)
3959{
3960 char_u *start = *arg;
3961 int len;
3962 int ret;
3963 char_u *name;
3964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 ++*arg;
3966 len = get_env_len(arg);
3967 if (len == 0)
3968 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003969 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 return FAIL;
3971 }
3972
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003973 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 name = vim_strnsave(start, len + 1);
3975 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3976 vim_free(name);
3977 return ret;
3978}
3979
3980/*
3981 * Compile "@r".
3982 */
3983 static int
3984compile_get_register(char_u **arg, cctx_T *cctx)
3985{
3986 int ret;
3987
3988 ++*arg;
3989 if (**arg == NUL)
3990 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003991 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 return FAIL;
3993 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003994 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 {
3996 emsg_invreg(**arg);
3997 return FAIL;
3998 }
3999 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4000 ++*arg;
4001 return ret;
4002}
4003
4004/*
4005 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004006 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 */
4008 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004009apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004011 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012
4013 // this works from end to start
4014 while (p > start)
4015 {
4016 --p;
4017 if (*p == '-' || *p == '+')
4018 {
4019 // only '-' has an effect, for '+' we only check the type
4020#ifdef FEAT_FLOAT
4021 if (rettv->v_type == VAR_FLOAT)
4022 {
4023 if (*p == '-')
4024 rettv->vval.v_float = -rettv->vval.v_float;
4025 }
4026 else
4027#endif
4028 {
4029 varnumber_T val;
4030 int error = FALSE;
4031
4032 // tv_get_number_chk() accepts a string, but we don't want that
4033 // here
4034 if (check_not_string(rettv) == FAIL)
4035 return FAIL;
4036 val = tv_get_number_chk(rettv, &error);
4037 clear_tv(rettv);
4038 if (error)
4039 return FAIL;
4040 if (*p == '-')
4041 val = -val;
4042 rettv->v_type = VAR_NUMBER;
4043 rettv->vval.v_number = val;
4044 }
4045 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004046 else if (numeric_only)
4047 {
4048 ++p;
4049 break;
4050 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004051 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 {
4053 int v = tv2bool(rettv);
4054
4055 // '!' is permissive in the type.
4056 clear_tv(rettv);
4057 rettv->v_type = VAR_BOOL;
4058 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4059 }
4060 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004061 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 return OK;
4063}
4064
4065/*
4066 * Recognize v: variables that are constants and set "rettv".
4067 */
4068 static void
4069get_vim_constant(char_u **arg, typval_T *rettv)
4070{
4071 if (STRNCMP(*arg, "v:true", 6) == 0)
4072 {
4073 rettv->v_type = VAR_BOOL;
4074 rettv->vval.v_number = VVAL_TRUE;
4075 *arg += 6;
4076 }
4077 else if (STRNCMP(*arg, "v:false", 7) == 0)
4078 {
4079 rettv->v_type = VAR_BOOL;
4080 rettv->vval.v_number = VVAL_FALSE;
4081 *arg += 7;
4082 }
4083 else if (STRNCMP(*arg, "v:null", 6) == 0)
4084 {
4085 rettv->v_type = VAR_SPECIAL;
4086 rettv->vval.v_number = VVAL_NULL;
4087 *arg += 6;
4088 }
4089 else if (STRNCMP(*arg, "v:none", 6) == 0)
4090 {
4091 rettv->v_type = VAR_SPECIAL;
4092 rettv->vval.v_number = VVAL_NONE;
4093 *arg += 6;
4094 }
4095}
4096
Bram Moolenaar657137c2021-01-09 15:45:23 +01004097 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004098get_compare_type(char_u *p, int *len, int *type_is)
4099{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004100 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004101 int i;
4102
4103 switch (p[0])
4104 {
4105 case '=': if (p[1] == '=')
4106 type = EXPR_EQUAL;
4107 else if (p[1] == '~')
4108 type = EXPR_MATCH;
4109 break;
4110 case '!': if (p[1] == '=')
4111 type = EXPR_NEQUAL;
4112 else if (p[1] == '~')
4113 type = EXPR_NOMATCH;
4114 break;
4115 case '>': if (p[1] != '=')
4116 {
4117 type = EXPR_GREATER;
4118 *len = 1;
4119 }
4120 else
4121 type = EXPR_GEQUAL;
4122 break;
4123 case '<': if (p[1] != '=')
4124 {
4125 type = EXPR_SMALLER;
4126 *len = 1;
4127 }
4128 else
4129 type = EXPR_SEQUAL;
4130 break;
4131 case 'i': if (p[1] == 's')
4132 {
4133 // "is" and "isnot"; but not a prefix of a name
4134 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4135 *len = 5;
4136 i = p[*len];
4137 if (!isalnum(i) && i != '_')
4138 {
4139 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4140 *type_is = TRUE;
4141 }
4142 }
4143 break;
4144 }
4145 return type;
4146}
4147
4148/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004149 * Skip over an expression, ignoring most errors.
4150 */
4151 static void
4152skip_expr_cctx(char_u **arg, cctx_T *cctx)
4153{
4154 evalarg_T evalarg;
4155
4156 CLEAR_FIELD(evalarg);
4157 evalarg.eval_cctx = cctx;
4158 skip_expr(arg, &evalarg);
4159}
4160
4161/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004163 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 */
4165 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004166compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004168 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169
4170 // this works from end to start
4171 while (p > start)
4172 {
4173 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004174 while (VIM_ISWHITE(*p))
4175 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 if (*p == '-' || *p == '+')
4177 {
4178 int negate = *p == '-';
4179 isn_T *isn;
4180
4181 // TODO: check type
4182 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4183 {
4184 --p;
4185 if (*p == '-')
4186 negate = !negate;
4187 }
4188 // only '-' has an effect, for '+' we only check the type
4189 if (negate)
4190 isn = generate_instr(cctx, ISN_NEGATENR);
4191 else
4192 isn = generate_instr(cctx, ISN_CHECKNR);
4193 if (isn == NULL)
4194 return FAIL;
4195 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004196 else if (numeric_only)
4197 {
4198 ++p;
4199 break;
4200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 else
4202 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004203 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004205 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004207 if (p[-1] == '!')
4208 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004211 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 return FAIL;
4213 }
4214 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004215 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 return OK;
4217}
4218
4219/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004220 * Compile "(expression)": recursive!
4221 * Return FAIL/OK.
4222 */
4223 static int
4224compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4225{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004226 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004227 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004228
Bram Moolenaar24156692021-01-14 20:35:49 +01004229 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4230 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004231 if (ppconst->pp_used <= PPSIZE - 10)
4232 {
4233 ret = compile_expr1(arg, cctx, ppconst);
4234 }
4235 else
4236 {
4237 // Not enough space in ppconst, flush constants.
4238 if (generate_ppconst(cctx, ppconst) == FAIL)
4239 return FAIL;
4240 ret = compile_expr0(arg, cctx);
4241 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004242 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4243 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004244 if (**arg == ')')
4245 ++*arg;
4246 else if (ret == OK)
4247 {
4248 emsg(_(e_missing_close));
4249 ret = FAIL;
4250 }
4251 return ret;
4252}
4253
4254/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004256 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 */
4258 static int
4259compile_subscript(
4260 char_u **arg,
4261 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004262 char_u *start_leader,
4263 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004264 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004266 char_u *name_start = *end_leader;
4267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 for (;;)
4269 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004270 char_u *p = skipwhite(*arg);
4271
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004272 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004273 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004274 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004275
4276 // If a following line starts with "->{" or "->X" advance to that
4277 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004278 // Also if a following line starts with ".x".
4279 if (next != NULL &&
4280 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004281 && (next[2] == '{'
4282 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004283 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004284 {
4285 next = next_line_from_context(cctx, TRUE);
4286 if (next == NULL)
4287 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004288 *arg = next;
4289 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004290 }
4291 }
4292
Bram Moolenaarcd268012021-07-22 19:11:08 +02004293 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4294 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004295 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004297 garray_T *stack = &cctx->ctx_type_stack;
4298 type_T *type;
4299 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004301 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004302 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004303 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004306 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4307
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004308 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004309 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004311 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 return FAIL;
4313 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004314 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004316 char_u *pstart = p;
4317
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004318 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004319 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004320 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 // something->method()
4323 // Apply the '!', '-' and '+' first:
4324 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004325 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004328 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004329 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004330 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004331 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004332 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004333 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004334 garray_T *stack = &cctx->ctx_type_stack;
4335 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004336 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004337 int expr_isn_start = cctx->ctx_instr.ga_len;
4338 int expr_isn_end;
4339 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004340
4341 // Funcref call: list->(Refs[2])(arg)
4342 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004343 //
4344 // Fist compile the function expression.
4345 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004346 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004347
4348 // Remember the next instruction index, where the instructions
4349 // for arguments are being written.
4350 expr_isn_end = cctx->ctx_instr.ga_len;
4351
4352 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004353 if (**arg != '(')
4354 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004355 if (*skipwhite(*arg) == '(')
4356 emsg(_(e_nowhitespace));
4357 else
4358 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004359 return FAIL;
4360 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004361 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004362 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004363 return FAIL;
4364
Bram Moolenaar2927c072021-04-05 19:41:21 +02004365 // Move the instructions for the arguments to before the
4366 // instructions of the expression and move the type of the
4367 // expression after the argument types. This is what ISN_PCALL
4368 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004369 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004370 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4371 if (arg_isn_count > 0)
4372 {
4373 int expr_isn_count = expr_isn_end - expr_isn_start;
4374 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4375
4376 if (isn == NULL)
4377 return FAIL;
4378 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4379 + expr_isn_start,
4380 sizeof(isn_T) * expr_isn_count);
4381 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4382 + expr_isn_start,
4383 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4384 sizeof(isn_T) * arg_isn_count);
4385 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4386 + expr_isn_start + arg_isn_count,
4387 isn, sizeof(isn_T) * expr_isn_count);
4388 vim_free(isn);
4389
4390 type = ((type_T **)stack->ga_data)[type_idx_start];
4391 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4392 ((type_T **)stack->ga_data) + type_idx_start + 1,
4393 sizeof(type_T *)
4394 * (stack->ga_len - type_idx_start - 1));
4395 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4396 }
4397
Bram Moolenaar7e368202020-12-25 21:56:57 +01004398 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004399 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 return FAIL;
4401 }
4402 else
4403 {
4404 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004405 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004406 if (!eval_isnamec1(*p))
4407 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004408 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004409 return FAIL;
4410 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004411 if (ASCII_ISALPHA(*p) && p[1] == ':')
4412 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004413 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 ;
4415 if (*p != '(')
4416 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004417 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 return FAIL;
4419 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004420 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 return FAIL;
4422 }
4423 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004424 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004426 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004427
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004428 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004429 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004430 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004431 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004432 // TODO: more arguments
4433 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004434 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004435 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004436 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004437
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004438 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004439 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004440 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004441 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004442 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004443 // missing first index is equal to zero
4444 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004445 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004446 else
4447 {
4448 if (compile_expr0(arg, cctx) == FAIL)
4449 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004450 if (**arg == ':')
4451 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004452 semsg(_(e_white_space_required_before_and_after_str_at_str),
4453 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004454 return FAIL;
4455 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004456 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004457 return FAIL;
4458 *arg = skipwhite(*arg);
4459 }
4460 if (**arg == ':')
4461 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004462 is_slice = TRUE;
4463 ++*arg;
4464 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4465 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004466 semsg(_(e_white_space_required_before_and_after_str_at_str),
4467 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004468 return FAIL;
4469 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004470 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004471 return FAIL;
4472 if (**arg == ']')
4473 // missing second index is equal to end of string
4474 generate_PUSHNR(cctx, -1);
4475 else
4476 {
4477 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004478 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004479 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004480 return FAIL;
4481 *arg = skipwhite(*arg);
4482 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484
4485 if (**arg != ']')
4486 {
4487 emsg(_(e_missbrac));
4488 return FAIL;
4489 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004490 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491
Bram Moolenaare42939a2021-04-05 17:11:17 +02004492 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004493 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004495 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004497 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004498 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004499 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004500 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004501
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004502 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004503 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004504 {
4505 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004506 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004507 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004508 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004509 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 while (eval_isnamec(*p))
4511 MB_PTR_ADV(p);
4512 if (p == *arg)
4513 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004514 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 return FAIL;
4516 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004517 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 return FAIL;
4519 *arg = p;
4520 }
4521 else
4522 break;
4523 }
4524
4525 // TODO - see handle_subscript():
4526 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4527 // Don't do this when "Func" is already a partial that was bound
4528 // explicitly (pt_auto is FALSE).
4529
4530 return OK;
4531}
4532
4533/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004534 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4535 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004537 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004538 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004539 *
4540 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 */
4542
4543/*
4544 * number number constant
4545 * 0zFFFFFFFF Blob constant
4546 * "string" string constant
4547 * 'string' literal string constant
4548 * &option-name option value
4549 * @r register contents
4550 * identifier variable value
4551 * function() function call
4552 * $VAR environment variable
4553 * (expression) nested expression
4554 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004555 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556 *
4557 * Also handle:
4558 * ! in front logical NOT
4559 * - in front unary minus
4560 * + in front unary plus (ignored)
4561 * trailing (arg) funcref/partial call
4562 * trailing [] subscript in String or List
4563 * trailing .name entry in Dictionary
4564 * trailing ->name() method call
4565 */
4566 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004567compile_expr7(
4568 char_u **arg,
4569 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004570 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 char_u *start_leader, *end_leader;
4573 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004574 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004575 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004577 ppconst->pp_is_const = FALSE;
4578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 /*
4580 * Skip '!', '-' and '+' characters. They are handled later.
4581 */
4582 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004583 if (eval_leader(arg, TRUE) == FAIL)
4584 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 end_leader = *arg;
4586
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004587 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 switch (**arg)
4589 {
4590 /*
4591 * Number constant.
4592 */
4593 case '0': // also for blob starting with 0z
4594 case '1':
4595 case '2':
4596 case '3':
4597 case '4':
4598 case '5':
4599 case '6':
4600 case '7':
4601 case '8':
4602 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004603 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004605 // Apply "-" and "+" just before the number now, right to
4606 // left. Matters especially when "->" follows. Stops at
4607 // '!'.
4608 if (apply_leader(rettv, TRUE,
4609 start_leader, &end_leader) == FAIL)
4610 {
4611 clear_tv(rettv);
4612 return FAIL;
4613 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 break;
4615
4616 /*
4617 * String constant: "string".
4618 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004619 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 return FAIL;
4621 break;
4622
4623 /*
4624 * Literal string constant: 'str''ing'.
4625 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004626 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 return FAIL;
4628 break;
4629
4630 /*
4631 * Constant Vim variable.
4632 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004633 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 ret = NOTDONE;
4635 break;
4636
4637 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004638 * "true" constant
4639 */
4640 case 't': if (STRNCMP(*arg, "true", 4) == 0
4641 && !eval_isnamec((*arg)[4]))
4642 {
4643 *arg += 4;
4644 rettv->v_type = VAR_BOOL;
4645 rettv->vval.v_number = VVAL_TRUE;
4646 }
4647 else
4648 ret = NOTDONE;
4649 break;
4650
4651 /*
4652 * "false" constant
4653 */
4654 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4655 && !eval_isnamec((*arg)[5]))
4656 {
4657 *arg += 5;
4658 rettv->v_type = VAR_BOOL;
4659 rettv->vval.v_number = VVAL_FALSE;
4660 }
4661 else
4662 ret = NOTDONE;
4663 break;
4664
4665 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004666 * "null" constant
4667 */
4668 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004669 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004670 {
4671 *arg += 4;
4672 rettv->v_type = VAR_SPECIAL;
4673 rettv->vval.v_number = VVAL_NULL;
4674 }
4675 else
4676 ret = NOTDONE;
4677 break;
4678
4679 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 * List: [expr, expr]
4681 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004682 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4683 return FAIL;
4684 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 break;
4686
4687 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 * Dictionary: {'key': val, 'key': val}
4689 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004690 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4691 return FAIL;
4692 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 break;
4694
4695 /*
4696 * Option value: &name
4697 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004698 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4699 return FAIL;
4700 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701 break;
4702
4703 /*
4704 * Environment variable: $VAR.
4705 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004706 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4707 return FAIL;
4708 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 break;
4710
4711 /*
4712 * Register contents: @r.
4713 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004714 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4715 return FAIL;
4716 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 break;
4718 /*
4719 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004720 * lambda: (arg, arg) => expr
4721 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004723 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4724 ret = compile_lambda(arg, cctx);
4725 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004726 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 break;
4728
4729 default: ret = NOTDONE;
4730 break;
4731 }
4732 if (ret == FAIL)
4733 return FAIL;
4734
Bram Moolenaar1c747212020-05-09 18:28:34 +02004735 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004737 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004738 clear_tv(rettv);
4739 else
4740 // A constant expression can possibly be handled compile time,
4741 // return the value instead of generating code.
4742 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 }
4744 else if (ret == NOTDONE)
4745 {
4746 char_u *p;
4747 int r;
4748
4749 if (!eval_isnamec1(**arg))
4750 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004751 if (!vim9_bad_comment(*arg))
4752 {
4753 if (ends_excmd(*skipwhite(*arg)))
4754 semsg(_(e_empty_expression_str), *arg);
4755 else
4756 semsg(_(e_name_expected_str), *arg);
4757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 return FAIL;
4759 }
4760
4761 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004762 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004763 if (p - *arg == (size_t)1 && **arg == '_')
4764 {
4765 emsg(_(e_cannot_use_underscore_here));
4766 return FAIL;
4767 }
4768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004770 {
4771 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4772 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004774 {
4775 if (generate_ppconst(cctx, ppconst) == FAIL)
4776 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004777 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004778 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 if (r == FAIL)
4780 return FAIL;
4781 }
4782
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004783 // Handle following "[]", ".member", etc.
4784 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004785 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004786 ppconst) == FAIL)
4787 return FAIL;
4788 if (ppconst->pp_used > 0)
4789 {
4790 // apply the '!', '-' and '+' before the constant
4791 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004792 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004793 return FAIL;
4794 return OK;
4795 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004796 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004798 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799}
4800
4801/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004802 * Give the "white on both sides" error, taking the operator from "p[len]".
4803 */
4804 void
4805error_white_both(char_u *op, int len)
4806{
4807 char_u buf[10];
4808
4809 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004810 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004811}
4812
4813/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004814 * <type>expr7: runtime type check / conversion
4815 */
4816 static int
4817compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4818{
4819 type_T *want_type = NULL;
4820
4821 // Recognize <type>
4822 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4823 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004824 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004825 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4826 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004827 return FAIL;
4828
4829 if (**arg != '>')
4830 {
4831 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004832 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004833 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004834 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004835 return FAIL;
4836 }
4837 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004838 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004839 return FAIL;
4840 }
4841
4842 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4843 return FAIL;
4844
4845 if (want_type != NULL)
4846 {
4847 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004848 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004849 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004850
Bram Moolenaard1103582020-08-14 22:44:25 +02004851 generate_ppconst(cctx, ppconst);
4852 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004853 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004854 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004855 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004856 return FAIL;
4857 }
4858 }
4859
4860 return OK;
4861}
4862
4863/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 * * number multiplication
4865 * / number division
4866 * % number modulo
4867 */
4868 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004869compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870{
4871 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004872 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004873 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004875 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004876 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 return FAIL;
4878
4879 /*
4880 * Repeat computing, until no "*", "/" or "%" is following.
4881 */
4882 for (;;)
4883 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004884 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 if (*op != '*' && *op != '/' && *op != '%')
4886 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004887 if (next != NULL)
4888 {
4889 *arg = next_line_from_context(cctx, TRUE);
4890 op = skipwhite(*arg);
4891 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004892
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004893 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004895 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004896 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004898 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004899 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004901 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004902 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004904
4905 if (ppconst->pp_used == ppconst_used + 2
4906 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4907 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004908 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004909 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4910 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4911 varnumber_T res = 0;
4912 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004914 // both are numbers: compute the result
4915 switch (*op)
4916 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004917 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004918 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004919 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004920 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004921 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004922 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004923 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004924 break;
4925 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004926 if (failed)
4927 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004928 tv1->vval.v_number = res;
4929 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004930 }
4931 else
4932 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004933 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004934 generate_two_op(cctx, op);
4935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 }
4937
4938 return OK;
4939}
4940
4941/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004942 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 * - number subtraction
4944 * .. string concatenation
4945 */
4946 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004947compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948{
4949 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004950 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004952 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953
4954 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004955 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 return FAIL;
4957
4958 /*
4959 * Repeat computing, until no "+", "-" or ".." is following.
4960 */
4961 for (;;)
4962 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004963 op = may_peek_next_line(cctx, *arg, &next);
4964 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004966 if (op[0] == op[1] && *op != '.' && next)
4967 // Finding "++" or "--" on the next line is a separate command.
4968 // But ".." is concatenation.
4969 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004971 if (next != NULL)
4972 {
4973 *arg = next_line_from_context(cctx, TRUE);
4974 op = skipwhite(*arg);
4975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004977 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004979 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004980 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 }
4982
Bram Moolenaare0de1712020-12-02 17:36:54 +01004983 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004984 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004986 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004987 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 return FAIL;
4989
Bram Moolenaara5565e42020-05-09 15:44:01 +02004990 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004991 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004992 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4993 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4994 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4995 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004997 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4998 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004999
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005000 // concat/subtract/add constant numbers
5001 if (*op == '+')
5002 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5003 else if (*op == '-')
5004 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5005 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005006 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005007 // concatenate constant strings
5008 char_u *s1 = tv1->vval.v_string;
5009 char_u *s2 = tv2->vval.v_string;
5010 size_t len1 = STRLEN(s1);
5011
5012 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5013 if (tv1->vval.v_string == NULL)
5014 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005015 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005016 return FAIL;
5017 }
5018 mch_memmove(tv1->vval.v_string, s1, len1);
5019 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005020 vim_free(s1);
5021 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005022 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005023 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024 }
5025 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005026 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005027 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005028 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005029 if (*op == '.')
5030 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005031 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5032 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005033 return FAIL;
5034 generate_instr_drop(cctx, ISN_CONCAT, 1);
5035 }
5036 else
5037 generate_two_op(cctx, op);
5038 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 }
5040
5041 return OK;
5042}
5043
5044/*
5045 * expr5a == expr5b
5046 * expr5a =~ expr5b
5047 * expr5a != expr5b
5048 * expr5a !~ expr5b
5049 * expr5a > expr5b
5050 * expr5a >= expr5b
5051 * expr5a < expr5b
5052 * expr5a <= expr5b
5053 * expr5a is expr5b
5054 * expr5a isnot expr5b
5055 *
5056 * Produces instructions:
5057 * EVAL expr5a Push result of "expr5a"
5058 * EVAL expr5b Push result of "expr5b"
5059 * COMPARE one of the compare instructions
5060 */
5061 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005062compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005064 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005066 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005069 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070
5071 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005072 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 return FAIL;
5074
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005075 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005076 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077
5078 /*
5079 * If there is a comparative operator, use it.
5080 */
5081 if (type != EXPR_UNKNOWN)
5082 {
5083 int ic = FALSE; // Default: do not ignore case
5084
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005085 if (next != NULL)
5086 {
5087 *arg = next_line_from_context(cctx, TRUE);
5088 p = skipwhite(*arg);
5089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 if (type_is && (p[len] == '?' || p[len] == '#'))
5091 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005092 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 return FAIL;
5094 }
5095 // extra question mark appended: ignore case
5096 if (p[len] == '?')
5097 {
5098 ic = TRUE;
5099 ++len;
5100 }
5101 // extra '#' appended: match case (ignored)
5102 else if (p[len] == '#')
5103 ++len;
5104 // nothing appended: match case
5105
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005106 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005108 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005109 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 }
5111
5112 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005113 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005114 return FAIL;
5115
Bram Moolenaara5565e42020-05-09 15:44:01 +02005116 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117 return FAIL;
5118
Bram Moolenaara5565e42020-05-09 15:44:01 +02005119 if (ppconst->pp_used == ppconst_used + 2)
5120 {
5121 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5122 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5123 int ret;
5124
5125 // Both sides are a constant, compute the result now.
5126 // First check for a valid combination of types, this is more
5127 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005128 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005129 ret = FAIL;
5130 else
5131 {
5132 ret = typval_compare(tv1, tv2, type, ic);
5133 tv1->v_type = VAR_BOOL;
5134 tv1->vval.v_number = tv1->vval.v_number
5135 ? VVAL_TRUE : VVAL_FALSE;
5136 clear_tv(tv2);
5137 --ppconst->pp_used;
5138 }
5139 return ret;
5140 }
5141
5142 generate_ppconst(cctx, ppconst);
5143 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 }
5145
5146 return OK;
5147}
5148
Bram Moolenaar7f141552020-05-09 17:35:53 +02005149static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5150
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151/*
5152 * Compile || or &&.
5153 */
5154 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005155compile_and_or(
5156 char_u **arg,
5157 cctx_T *cctx,
5158 char *op,
5159 ppconst_T *ppconst,
5160 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005162 char_u *next;
5163 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164 int opchar = *op;
5165
5166 if (p[0] == opchar && p[1] == opchar)
5167 {
5168 garray_T *instr = &cctx->ctx_instr;
5169 garray_T end_ga;
5170
5171 /*
5172 * Repeat until there is no following "||" or "&&"
5173 */
5174 ga_init2(&end_ga, sizeof(int), 10);
5175 while (p[0] == opchar && p[1] == opchar)
5176 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005177 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005178 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005179 int start_ctx_lnum = cctx->ctx_lnum;
5180 int save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005181 int status;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005182
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005183 if (next != NULL)
5184 {
5185 *arg = next_line_from_context(cctx, TRUE);
5186 p = skipwhite(*arg);
5187 }
5188
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005189 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5190 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005191 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005192 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005193 return FAIL;
5194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005196 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005197 SOURCING_LNUM = start_lnum;
5198 save_lnum = cctx->ctx_lnum;
5199 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005200
5201 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005202 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005203 {
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005204 // TODO: use ppconst if the value is a constant
5205 generate_ppconst(cctx, ppconst);
5206
5207 // Every part must evaluate to a bool.
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005208 status = bool_on_stack(cctx);
5209 if (status != FAIL)
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005210 status = ga_grow(&end_ga, 1);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005211 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005212 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005213 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 {
5215 ga_clear(&end_ga);
5216 return FAIL;
5217 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5220 ++end_ga.ga_len;
5221 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005222 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223
5224 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005225 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005226 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005227 {
5228 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005229 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005230 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005231
Bram Moolenaara5565e42020-05-09 15:44:01 +02005232 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5233 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234 {
5235 ga_clear(&end_ga);
5236 return FAIL;
5237 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005238
5239 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005241
5242 if (check_ppconst_bool(ppconst) == FAIL)
5243 {
5244 ga_clear(&end_ga);
5245 return FAIL;
5246 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005247 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005249 // Every part must evaluate to a bool.
5250 if (bool_on_stack(cctx) == FAIL)
5251 {
5252 ga_clear(&end_ga);
5253 return FAIL;
5254 }
5255
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 // Fill in the end label in all jumps.
5257 while (end_ga.ga_len > 0)
5258 {
5259 isn_T *isn;
5260
5261 --end_ga.ga_len;
5262 isn = ((isn_T *)instr->ga_data)
5263 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5264 isn->isn_arg.jump.jump_where = instr->ga_len;
5265 }
5266 ga_clear(&end_ga);
5267 }
5268
5269 return OK;
5270}
5271
5272/*
5273 * expr4a && expr4a && expr4a logical AND
5274 *
5275 * Produces instructions:
5276 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005277 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005278 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005280 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005281 * EVAL expr4c Push result of "expr4c"
5282 * end:
5283 */
5284 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005285compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005287 int ppconst_used = ppconst->pp_used;
5288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005290 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291 return FAIL;
5292
5293 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005294 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295}
5296
5297/*
5298 * expr3a || expr3b || expr3c logical OR
5299 *
5300 * Produces instructions:
5301 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005302 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005303 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005305 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306 * EVAL expr3c Push result of "expr3c"
5307 * end:
5308 */
5309 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005310compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005311{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005312 int ppconst_used = ppconst->pp_used;
5313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005314 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005315 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005316 return FAIL;
5317
5318 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005319 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320}
5321
5322/*
5323 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005325 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005326 * JUMP_IF_FALSE alt jump if false
5327 * EVAL expr1a
5328 * JUMP_ALWAYS end
5329 * alt: EVAL expr1b
5330 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005331 *
5332 * Toplevel expression: expr2 ?? expr1
5333 * Produces instructions:
5334 * EVAL expr2 Push result of "expr2"
5335 * JUMP_AND_KEEP_IF_TRUE end jump if true
5336 * EVAL expr1
5337 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338 */
5339 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005340compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341{
5342 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005343 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005344 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345
Bram Moolenaar3988f642020-08-27 22:43:03 +02005346 // Ignore all kinds of errors when not producing code.
5347 if (cctx->ctx_skip == SKIP_YES)
5348 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005349 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005350 return OK;
5351 }
5352
Bram Moolenaar61a89812020-05-07 16:58:17 +02005353 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005354 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355 return FAIL;
5356
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005357 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005358 if (*p == '?')
5359 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005360 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 garray_T *instr = &cctx->ctx_instr;
5362 garray_T *stack = &cctx->ctx_type_stack;
5363 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005364 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005366 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005367 int has_const_expr = FALSE;
5368 int const_value = FALSE;
5369 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005371 if (next != NULL)
5372 {
5373 *arg = next_line_from_context(cctx, TRUE);
5374 p = skipwhite(*arg);
5375 }
5376
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005377 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005378 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005379 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005380 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005381 return FAIL;
5382 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005383
Bram Moolenaara5565e42020-05-09 15:44:01 +02005384 if (ppconst->pp_used == ppconst_used + 1)
5385 {
5386 // the condition is a constant, we know whether the ? or the :
5387 // expression is to be evaluated.
5388 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005389 if (op_falsy)
5390 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5391 else
5392 {
5393 int error = FALSE;
5394
5395 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5396 &error);
5397 if (error)
5398 return FAIL;
5399 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005400 cctx->ctx_skip = save_skip == SKIP_YES ||
5401 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5402
5403 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5404 // "left ?? right" and "left" is truthy: produce "left"
5405 generate_ppconst(cctx, ppconst);
5406 else
5407 {
5408 clear_tv(&ppconst->pp_tv[ppconst_used]);
5409 --ppconst->pp_used;
5410 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005411 }
5412 else
5413 {
5414 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005415 if (op_falsy)
5416 end_idx = instr->ga_len;
5417 generate_JUMP(cctx, op_falsy
5418 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5419 if (op_falsy)
5420 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422
5423 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005424 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005425 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005426 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005427 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428
Bram Moolenaara5565e42020-05-09 15:44:01 +02005429 if (!has_const_expr)
5430 {
5431 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005432
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005433 if (!op_falsy)
5434 {
5435 // remember the type and drop it
5436 --stack->ga_len;
5437 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005438
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005439 end_idx = instr->ga_len;
5440 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005441
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005442 // jump here from JUMP_IF_FALSE
5443 isn = ((isn_T *)instr->ga_data) + alt_idx;
5444 isn->isn_arg.jump.jump_where = instr->ga_len;
5445 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005446 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005448 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005450 // Check for the ":".
5451 p = may_peek_next_line(cctx, *arg, &next);
5452 if (*p != ':')
5453 {
5454 emsg(_(e_missing_colon));
5455 return FAIL;
5456 }
5457 if (next != NULL)
5458 {
5459 *arg = next_line_from_context(cctx, TRUE);
5460 p = skipwhite(*arg);
5461 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005462
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005463 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5464 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005465 semsg(_(e_white_space_required_before_and_after_str_at_str),
5466 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005467 return FAIL;
5468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005469
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005470 // evaluate the third expression
5471 if (has_const_expr)
5472 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005473 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005474 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005475 return FAIL;
5476 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5477 return FAIL;
5478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005479
Bram Moolenaara5565e42020-05-09 15:44:01 +02005480 if (!has_const_expr)
5481 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005482 type_T **typep;
5483
Bram Moolenaara5565e42020-05-09 15:44:01 +02005484 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005485
Bram Moolenaara5565e42020-05-09 15:44:01 +02005486 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005487 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5488 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005489
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005490 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005491 isn = ((isn_T *)instr->ga_data) + end_idx;
5492 isn->isn_arg.jump.jump_where = instr->ga_len;
5493 }
5494
5495 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496 }
5497 return OK;
5498}
5499
5500/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005501 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005502 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5503 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005504 */
5505 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005506compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005507{
5508 ppconst_T ppconst;
5509
5510 CLEAR_FIELD(ppconst);
5511 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5512 {
5513 clear_ppconst(&ppconst);
5514 return FAIL;
5515 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005516 if (is_const != NULL)
5517 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005518 if (generate_ppconst(cctx, &ppconst) == FAIL)
5519 return FAIL;
5520 return OK;
5521}
5522
5523/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005524 * Toplevel expression.
5525 */
5526 static int
5527compile_expr0(char_u **arg, cctx_T *cctx)
5528{
5529 return compile_expr0_ext(arg, cctx, NULL);
5530}
5531
5532/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005533 * Compile "return [expr]".
5534 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535 */
5536 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005537compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005538{
5539 char_u *p = arg;
5540 garray_T *stack = &cctx->ctx_type_stack;
5541 type_T *stack_type;
5542
5543 if (*p != NUL && *p != '|' && *p != '\n')
5544 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005545 if (legacy)
5546 {
5547 int save_flags = cmdmod.cmod_flags;
5548
5549 generate_LEGACY_EVAL(cctx, p);
5550 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5551 0, cctx, FALSE, FALSE) == FAIL)
5552 return NULL;
5553 cmdmod.cmod_flags |= CMOD_LEGACY;
5554 (void)skip_expr(&p, NULL);
5555 cmdmod.cmod_flags = save_flags;
5556 }
5557 else
5558 {
5559 // compile return argument into instructions
5560 if (compile_expr0(&p, cctx) == FAIL)
5561 return NULL;
5562 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005563
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005564 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005565 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005566 // "check_return_type" with uf_ret_type set to &t_unknown is used
5567 // for an inline function without a specified return type. Set the
5568 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005569 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005570 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005571 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5572 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005573 || (!check_return_type
5574 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005575 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005576 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005577 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005578 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005579 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005580 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5581 && stack_type->tt_type != VAR_VOID
5582 && stack_type->tt_type != VAR_UNKNOWN)
5583 {
5584 emsg(_(e_returning_value_in_function_without_return_type));
5585 return NULL;
5586 }
5587 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005588 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005589 return NULL;
5590 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005591 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592 }
5593 else
5594 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005595 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005596 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005597 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5598 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005599 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005600 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005601 return NULL;
5602 }
5603
5604 // No argument, return zero.
5605 generate_PUSHNR(cctx, 0);
5606 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005607
5608 // Undo any command modifiers.
5609 generate_undo_cmdmods(cctx);
5610
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005611 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612 return NULL;
5613
5614 // "return val | endif" is possible
5615 return skipwhite(p);
5616}
5617
5618/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005619 * Get a line from the compilation context, compatible with exarg_T getline().
5620 * Return a pointer to the line in allocated memory.
5621 * Return NULL for end-of-file or some error.
5622 */
5623 static char_u *
5624exarg_getline(
5625 int c UNUSED,
5626 void *cookie,
5627 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005628 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005629{
5630 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005631 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005632
Bram Moolenaar66250c92020-08-20 15:02:42 +02005633 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005634 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005635 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005636 return NULL;
5637 ++cctx->ctx_lnum;
5638 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5639 // Comment lines result in NULL pointers, skip them.
5640 if (p != NULL)
5641 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005642 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005643}
5644
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005645 void
5646fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5647{
5648 eap->getline = exarg_getline;
5649 eap->cookie = cctx;
5650}
5651
Bram Moolenaar04b12692020-05-04 23:24:44 +02005652/*
5653 * Compile a nested :def command.
5654 */
5655 static char_u *
5656compile_nested_function(exarg_T *eap, cctx_T *cctx)
5657{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005658 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005659 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005660 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005661 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005662 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005663 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005664 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005665
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005666 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005667 {
5668 emsg(_(e_cannot_use_bang_with_nested_def));
5669 return NULL;
5670 }
5671
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005672 if (*name_start == '/')
5673 {
5674 name_end = skip_regexp(name_start + 1, '/', TRUE);
5675 if (*name_end == '/')
5676 ++name_end;
5677 eap->nextcmd = check_nextcmd(name_end);
5678 }
5679 if (name_end == name_start || *skipwhite(name_end) != '(')
5680 {
5681 if (!ends_excmd2(name_start, name_end))
5682 {
5683 semsg(_(e_invalid_command_str), eap->cmd);
5684 return NULL;
5685 }
5686
5687 // "def" or "def Name": list functions
5688 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5689 return NULL;
5690 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5691 }
5692
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005693 // Only g:Func() can use a namespace.
5694 if (name_start[1] == ':' && !is_global)
5695 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005696 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005697 return NULL;
5698 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005699 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005700 return NULL;
5701
Bram Moolenaar04b12692020-05-04 23:24:44 +02005702 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005703 fill_exarg_from_cctx(eap, cctx);
5704
Bram Moolenaar04b12692020-05-04 23:24:44 +02005705 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005706 lambda_name = vim_strsave(get_lambda_name());
5707 if (lambda_name == NULL)
5708 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005709 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005710
Bram Moolenaar822ba242020-05-24 23:00:18 +02005711 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005712 {
5713 r = eap->skip ? OK : FAIL;
5714 goto theend;
5715 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005716
5717 // copy over the block scope IDs before compiling
5718 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5719 {
5720 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5721
5722 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5723 if (ufunc->uf_block_ids != NULL)
5724 {
5725 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5726 sizeof(int) * block_depth);
5727 ufunc->uf_block_depth = block_depth;
5728 }
5729 }
5730
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005731 compile_type = COMPILE_TYPE(ufunc);
5732#ifdef FEAT_PROFILE
5733 // If the outer function is profiled, also compile the nested function for
5734 // profiling.
5735 if (cctx->ctx_compile_type == CT_PROFILE)
5736 compile_type = CT_PROFILE;
5737#endif
5738 if (func_needs_compiling(ufunc, compile_type)
5739 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005740 {
5741 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005742 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005743 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005744
Bram Moolenaar648594e2021-07-11 17:55:01 +02005745#ifdef FEAT_PROFILE
5746 // When the outer function is compiled for profiling, the nested function
5747 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005748 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005749 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5750#endif
5751
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005752 if (is_global)
5753 {
5754 char_u *func_name = vim_strnsave(name_start + 2,
5755 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005756
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005757 if (func_name == NULL)
5758 r = FAIL;
5759 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005760 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005761 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005762 lambda_name = NULL;
5763 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005764 }
5765 else
5766 {
5767 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005768 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005769 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005770
Bram Moolenaareef21022020-08-01 22:16:43 +02005771 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005772 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005773 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005774 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005775 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5776 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005777 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005778
5779theend:
5780 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005781 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005782}
5783
5784/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005785 * Return the length of an assignment operator, or zero if there isn't one.
5786 */
5787 int
5788assignment_len(char_u *p, int *heredoc)
5789{
5790 if (*p == '=')
5791 {
5792 if (p[1] == '<' && p[2] == '<')
5793 {
5794 *heredoc = TRUE;
5795 return 3;
5796 }
5797 return 1;
5798 }
5799 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5800 return 2;
5801 if (STRNCMP(p, "..=", 3) == 0)
5802 return 3;
5803 return 0;
5804}
5805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005806/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005807 * Generate the load instruction for "name".
5808 */
5809 static void
5810generate_loadvar(
5811 cctx_T *cctx,
5812 assign_dest_T dest,
5813 char_u *name,
5814 lvar_T *lvar,
5815 type_T *type)
5816{
5817 switch (dest)
5818 {
5819 case dest_option:
5820 // TODO: check the option exists
5821 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5822 break;
5823 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005824 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5825 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5826 else
5827 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005828 break;
5829 case dest_buffer:
5830 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5831 break;
5832 case dest_window:
5833 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5834 break;
5835 case dest_tab:
5836 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5837 break;
5838 case dest_script:
5839 compile_load_scriptvar(cctx,
5840 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5841 break;
5842 case dest_env:
5843 // Include $ in the name here
5844 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5845 break;
5846 case dest_reg:
5847 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5848 break;
5849 case dest_vimvar:
5850 generate_LOADV(cctx, name + 2, TRUE);
5851 break;
5852 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005853 if (lvar->lv_from_outer > 0)
5854 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5855 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005856 else
5857 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5858 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005859 case dest_expr:
5860 // list or dict value should already be on the stack.
5861 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005862 }
5863}
5864
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005865/*
5866 * Skip over "[expr]" or ".member".
5867 * Does not check for any errors.
5868 */
5869 static char_u *
5870skip_index(char_u *start)
5871{
5872 char_u *p = start;
5873
5874 if (*p == '[')
5875 {
5876 p = skipwhite(p + 1);
5877 (void)skip_expr(&p, NULL);
5878 p = skipwhite(p);
5879 if (*p == ']')
5880 return p + 1;
5881 return p;
5882 }
5883 // if (*p == '.')
5884 return to_name_end(p + 1, TRUE);
5885}
5886
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005887 void
5888vim9_declare_error(char_u *name)
5889{
5890 char *scope = "";
5891
5892 switch (*name)
5893 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005894 case 'g': scope = _("global"); break;
5895 case 'b': scope = _("buffer"); break;
5896 case 'w': scope = _("window"); break;
5897 case 't': scope = _("tab"); break;
5898 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005899 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005900 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005901 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005902 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005903 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005904 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005905 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005906 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005907 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005908}
5909
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005910/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005911 * For one assignment figure out the type of destination. Return it in "dest".
5912 * When not recognized "dest" is not set.
5913 * For an option "opt_flags" is set.
5914 * For a v:var "vimvaridx" is set.
5915 * "type" is set to the destination type if known, unchanted otherwise.
5916 * Return FAIL if an error message was given.
5917 */
5918 static int
5919get_var_dest(
5920 char_u *name,
5921 assign_dest_T *dest,
5922 int cmdidx,
5923 int *opt_flags,
5924 int *vimvaridx,
5925 type_T **type,
5926 cctx_T *cctx)
5927{
5928 char_u *p;
5929
5930 if (*name == '&')
5931 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005932 int cc;
5933 long numval;
5934 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005935
5936 *dest = dest_option;
5937 if (cmdidx == CMD_final || cmdidx == CMD_const)
5938 {
5939 emsg(_(e_const_option));
5940 return FAIL;
5941 }
5942 p = name;
5943 p = find_option_end(&p, opt_flags);
5944 if (p == NULL)
5945 {
5946 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005947 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005948 return FAIL;
5949 }
5950 cc = *p;
5951 *p = NUL;
5952 opt_type = get_option_value(skip_option_env_lead(name),
5953 &numval, NULL, *opt_flags);
5954 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005955 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005956 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005957 case gov_unknown:
5958 semsg(_(e_unknown_option), name);
5959 return FAIL;
5960 case gov_string:
5961 case gov_hidden_string:
5962 *type = &t_string;
5963 break;
5964 case gov_bool:
5965 case gov_hidden_bool:
5966 *type = &t_bool;
5967 break;
5968 case gov_number:
5969 case gov_hidden_number:
5970 *type = &t_number;
5971 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005972 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005973 }
5974 else if (*name == '$')
5975 {
5976 *dest = dest_env;
5977 *type = &t_string;
5978 }
5979 else if (*name == '@')
5980 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005981 if (name[1] != '@'
5982 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005983 {
5984 emsg_invreg(name[1]);
5985 return FAIL;
5986 }
5987 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005988 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005989 }
5990 else if (STRNCMP(name, "g:", 2) == 0)
5991 {
5992 *dest = dest_global;
5993 }
5994 else if (STRNCMP(name, "b:", 2) == 0)
5995 {
5996 *dest = dest_buffer;
5997 }
5998 else if (STRNCMP(name, "w:", 2) == 0)
5999 {
6000 *dest = dest_window;
6001 }
6002 else if (STRNCMP(name, "t:", 2) == 0)
6003 {
6004 *dest = dest_tab;
6005 }
6006 else if (STRNCMP(name, "v:", 2) == 0)
6007 {
6008 typval_T *vtv;
6009 int di_flags;
6010
6011 *vimvaridx = find_vim_var(name + 2, &di_flags);
6012 if (*vimvaridx < 0)
6013 {
6014 semsg(_(e_variable_not_found_str), name);
6015 return FAIL;
6016 }
6017 // We use the current value of "sandbox" here, is that OK?
6018 if (var_check_ro(di_flags, name, FALSE))
6019 return FAIL;
6020 *dest = dest_vimvar;
6021 vtv = get_vim_var_tv(*vimvaridx);
6022 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6023 }
6024 return OK;
6025}
6026
6027/*
6028 * Generate a STORE instruction for "dest", not being "dest_local".
6029 * Return FAIL when out of memory.
6030 */
6031 static int
6032generate_store_var(
6033 cctx_T *cctx,
6034 assign_dest_T dest,
6035 int opt_flags,
6036 int vimvaridx,
6037 int scriptvar_idx,
6038 int scriptvar_sid,
6039 type_T *type,
6040 char_u *name)
6041{
6042 switch (dest)
6043 {
6044 case dest_option:
6045 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6046 opt_flags);
6047 case dest_global:
6048 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006049 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6050 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006051 case dest_buffer:
6052 // include b: with the name, easier to execute that way
6053 return generate_STORE(cctx, ISN_STOREB, 0, name);
6054 case dest_window:
6055 // include w: with the name, easier to execute that way
6056 return generate_STORE(cctx, ISN_STOREW, 0, name);
6057 case dest_tab:
6058 // include t: with the name, easier to execute that way
6059 return generate_STORE(cctx, ISN_STORET, 0, name);
6060 case dest_env:
6061 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6062 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006063 return generate_STORE(cctx, ISN_STOREREG,
6064 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006065 case dest_vimvar:
6066 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6067 case dest_script:
6068 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006069 // "s:" may be included in the name.
6070 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006071 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006072 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6073 scriptvar_sid, scriptvar_idx, type);
6074 case dest_local:
6075 case dest_expr:
6076 // cannot happen
6077 break;
6078 }
6079 return FAIL;
6080}
6081
Bram Moolenaar752fc692021-01-04 21:57:11 +01006082 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006083generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6084{
6085 if (lhs->lhs_dest != dest_local)
6086 return generate_store_var(cctx, lhs->lhs_dest,
6087 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6088 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6089 lhs->lhs_type, lhs->lhs_name);
6090
6091 if (lhs->lhs_lvar != NULL)
6092 {
6093 garray_T *instr = &cctx->ctx_instr;
6094 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6095
6096 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6097 // ISN_STORENR
6098 if (lhs->lhs_lvar->lv_from_outer == 0
6099 && instr->ga_len == instr_count + 1
6100 && isn->isn_type == ISN_PUSHNR)
6101 {
6102 varnumber_T val = isn->isn_arg.number;
6103 garray_T *stack = &cctx->ctx_type_stack;
6104
6105 isn->isn_type = ISN_STORENR;
6106 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6107 isn->isn_arg.storenr.stnr_val = val;
6108 if (stack->ga_len > 0)
6109 --stack->ga_len;
6110 }
6111 else if (lhs->lhs_lvar->lv_from_outer > 0)
6112 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6113 lhs->lhs_lvar->lv_from_outer);
6114 else
6115 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6116 }
6117 return OK;
6118}
6119
6120 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006121is_decl_command(int cmdidx)
6122{
6123 return cmdidx == CMD_let || cmdidx == CMD_var
6124 || cmdidx == CMD_final || cmdidx == CMD_const;
6125}
6126
6127/*
6128 * Figure out the LHS type and other properties for an assignment or one item
6129 * of ":unlet" with an index.
6130 * Returns OK or FAIL.
6131 */
6132 static int
6133compile_lhs(
6134 char_u *var_start,
6135 lhs_T *lhs,
6136 int cmdidx,
6137 int heredoc,
6138 int oplen,
6139 cctx_T *cctx)
6140{
6141 char_u *var_end;
6142 int is_decl = is_decl_command(cmdidx);
6143
6144 CLEAR_POINTER(lhs);
6145 lhs->lhs_dest = dest_local;
6146 lhs->lhs_vimvaridx = -1;
6147 lhs->lhs_scriptvar_idx = -1;
6148
6149 // "dest_end" is the end of the destination, including "[expr]" or
6150 // ".name".
6151 // "var_end" is the end of the variable/option/etc. name.
6152 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6153 if (*var_start == '@')
6154 var_end = var_start + 2;
6155 else
6156 {
6157 // skip over the leading "&", "&l:", "&g:" and "$"
6158 var_end = skip_option_env_lead(var_start);
6159 var_end = to_name_end(var_end, TRUE);
6160 }
6161
6162 // "a: type" is declaring variable "a" with a type, not dict "a:".
6163 if (is_decl && lhs->lhs_dest_end == var_start + 2
6164 && lhs->lhs_dest_end[-1] == ':')
6165 --lhs->lhs_dest_end;
6166 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6167 --var_end;
6168
6169 // compute the length of the destination without "[expr]" or ".name"
6170 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006171 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006172 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6173 if (lhs->lhs_name == NULL)
6174 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006175
6176 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6177 // Something follows after the variable: "var[idx]" or "var.key".
6178 lhs->lhs_has_index = TRUE;
6179
Bram Moolenaar752fc692021-01-04 21:57:11 +01006180 if (heredoc)
6181 lhs->lhs_type = &t_list_string;
6182 else
6183 lhs->lhs_type = &t_any;
6184
6185 if (cctx->ctx_skip != SKIP_YES)
6186 {
6187 int declare_error = FALSE;
6188
6189 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6190 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6191 &lhs->lhs_type, cctx) == FAIL)
6192 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006193 if (lhs->lhs_dest != dest_local
6194 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006195 {
6196 // Specific kind of variable recognized.
6197 declare_error = is_decl;
6198 }
6199 else
6200 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006201 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006202 if (check_reserved_name(lhs->lhs_name) == FAIL)
6203 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006204
6205 if (lookup_local(var_start, lhs->lhs_varlen,
6206 &lhs->lhs_local_lvar, cctx) == OK)
6207 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6208 else
6209 {
6210 CLEAR_FIELD(lhs->lhs_arg_lvar);
6211 if (arg_exists(var_start, lhs->lhs_varlen,
6212 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6213 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6214 {
6215 if (is_decl)
6216 {
6217 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6218 return FAIL;
6219 }
6220 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6221 }
6222 }
6223 if (lhs->lhs_lvar != NULL)
6224 {
6225 if (is_decl)
6226 {
6227 semsg(_(e_variable_already_declared), lhs->lhs_name);
6228 return FAIL;
6229 }
6230 }
6231 else
6232 {
6233 int script_namespace = lhs->lhs_varlen > 1
6234 && STRNCMP(var_start, "s:", 2) == 0;
6235 int script_var = (script_namespace
6236 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006237 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006238 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006239 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006240 imported_T *import =
6241 find_imported(var_start, lhs->lhs_varlen, cctx);
6242
6243 if (script_namespace || script_var || import != NULL)
6244 {
6245 char_u *rawname = lhs->lhs_name
6246 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6247
6248 if (is_decl)
6249 {
6250 if (script_namespace)
6251 semsg(_(e_cannot_declare_script_variable_in_function),
6252 lhs->lhs_name);
6253 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006254 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006255 lhs->lhs_name);
6256 return FAIL;
6257 }
6258 else if (cctx->ctx_ufunc->uf_script_ctx_version
6259 == SCRIPT_VERSION_VIM9
6260 && script_namespace
6261 && !script_var && import == NULL)
6262 {
6263 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6264 return FAIL;
6265 }
6266
6267 lhs->lhs_dest = dest_script;
6268
6269 // existing script-local variables should have a type
6270 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6271 if (import != NULL)
6272 lhs->lhs_scriptvar_sid = import->imp_sid;
6273 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6274 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006275 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006276 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006277 lhs->lhs_scriptvar_sid, rawname,
6278 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6279 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006280 if (lhs->lhs_scriptvar_idx >= 0)
6281 {
6282 scriptitem_T *si = SCRIPT_ITEM(
6283 lhs->lhs_scriptvar_sid);
6284 svar_T *sv =
6285 ((svar_T *)si->sn_var_vals.ga_data)
6286 + lhs->lhs_scriptvar_idx;
6287 lhs->lhs_type = sv->sv_type;
6288 }
6289 }
6290 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006291 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006292 == FAIL)
6293 return FAIL;
6294 }
6295 }
6296
6297 if (declare_error)
6298 {
6299 vim9_declare_error(lhs->lhs_name);
6300 return FAIL;
6301 }
6302 }
6303
6304 // handle "a:name" as a name, not index "name" on "a"
6305 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6306 var_end = lhs->lhs_dest_end;
6307
6308 if (lhs->lhs_dest != dest_option)
6309 {
6310 if (is_decl && *var_end == ':')
6311 {
6312 char_u *p;
6313
6314 // parse optional type: "let var: type = expr"
6315 if (!VIM_ISWHITE(var_end[1]))
6316 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006317 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006318 return FAIL;
6319 }
6320 p = skipwhite(var_end + 1);
6321 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6322 if (lhs->lhs_type == NULL)
6323 return FAIL;
6324 lhs->lhs_has_type = TRUE;
6325 }
6326 else if (lhs->lhs_lvar != NULL)
6327 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6328 }
6329
Bram Moolenaare42939a2021-04-05 17:11:17 +02006330 if (oplen == 3 && !heredoc
6331 && lhs->lhs_dest != dest_global
6332 && !lhs->lhs_has_index
6333 && lhs->lhs_type->tt_type != VAR_STRING
6334 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006335 {
6336 emsg(_(e_can_only_concatenate_to_string));
6337 return FAIL;
6338 }
6339
6340 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6341 && cctx->ctx_skip != SKIP_YES)
6342 {
6343 if (oplen > 1 && !heredoc)
6344 {
6345 // +=, /=, etc. require an existing variable
6346 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6347 return FAIL;
6348 }
6349 if (!is_decl)
6350 {
6351 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6352 return FAIL;
6353 }
6354
Bram Moolenaar3f327882021-03-17 20:56:38 +01006355 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006356 if ((lhs->lhs_type->tt_type == VAR_FUNC
6357 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006358 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006359 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006360
6361 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006362 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6363 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6364 if (lhs->lhs_lvar == NULL)
6365 return FAIL;
6366 lhs->lhs_new_local = TRUE;
6367 }
6368
6369 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006370 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006371 {
6372 // Something follows after the variable: "var[idx]" or "var.key".
6373 // TODO: should we also handle "->func()" here?
6374 if (is_decl)
6375 {
6376 emsg(_(e_cannot_use_index_when_declaring_variable));
6377 return FAIL;
6378 }
6379
6380 if (var_start[lhs->lhs_varlen] == '['
6381 || var_start[lhs->lhs_varlen] == '.')
6382 {
6383 char_u *after = var_start + lhs->lhs_varlen;
6384 char_u *p;
6385
6386 // Only the last index is used below, if there are others
6387 // before it generate code for the expression. Thus for
6388 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6389 for (;;)
6390 {
6391 p = skip_index(after);
6392 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006393 {
6394 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006395 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006396 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006397 after = p;
6398 }
6399 if (after > var_start + lhs->lhs_varlen)
6400 {
6401 lhs->lhs_varlen = after - var_start;
6402 lhs->lhs_dest = dest_expr;
6403 // We don't know the type before evaluating the expression,
6404 // use "any" until then.
6405 lhs->lhs_type = &t_any;
6406 }
6407
Bram Moolenaar752fc692021-01-04 21:57:11 +01006408 if (lhs->lhs_type->tt_member == NULL)
6409 lhs->lhs_member_type = &t_any;
6410 else
6411 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6412 }
6413 else
6414 {
6415 semsg("Not supported yet: %s", var_start);
6416 return FAIL;
6417 }
6418 }
6419 return OK;
6420}
6421
6422/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006423 * Figure out the LHS and check a few errors.
6424 */
6425 static int
6426compile_assign_lhs(
6427 char_u *var_start,
6428 lhs_T *lhs,
6429 int cmdidx,
6430 int is_decl,
6431 int heredoc,
6432 int oplen,
6433 cctx_T *cctx)
6434{
6435 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6436 return FAIL;
6437
6438 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6439 {
6440 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6441 return FAIL;
6442 }
6443 if (!is_decl && lhs->lhs_lvar != NULL
6444 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6445 {
6446 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6447 return FAIL;
6448 }
6449 return OK;
6450}
6451
6452/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006453 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6454 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006455 */
6456 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006457compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006458 char_u *var_start,
6459 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006460 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006461 cctx_T *cctx)
6462{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006463 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006464 char_u *p;
6465 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006466 int need_white_before = TRUE;
6467 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006468
Bram Moolenaar752fc692021-01-04 21:57:11 +01006469 p = var_start + varlen;
6470 if (*p == '[')
6471 {
6472 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006473 if (*p == ':')
6474 {
6475 // empty first index, push zero
6476 r = generate_PUSHNR(cctx, 0);
6477 need_white_before = FALSE;
6478 }
6479 else
6480 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006481
6482 if (r == OK && *skipwhite(p) == ':')
6483 {
6484 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006485 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006486 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006487 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006488 empty_second = *skipwhite(p + 1) == ']';
6489 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6490 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006491 {
6492 semsg(_(e_white_space_required_before_and_after_str_at_str),
6493 ":", p);
6494 return FAIL;
6495 }
6496 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006497 if (*p == ']')
6498 // empty second index, push "none"
6499 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6500 else
6501 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006502 }
6503
Bram Moolenaar752fc692021-01-04 21:57:11 +01006504 if (r == OK && *skipwhite(p) != ']')
6505 {
6506 // this should not happen
6507 emsg(_(e_missbrac));
6508 r = FAIL;
6509 }
6510 }
6511 else // if (*p == '.')
6512 {
6513 char_u *key_end = to_name_end(p + 1, TRUE);
6514 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6515
6516 r = generate_PUSHS(cctx, key);
6517 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006518 return r;
6519}
6520
6521/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006522 * For a LHS with an index, load the variable to be indexed.
6523 */
6524 static int
6525compile_load_lhs(
6526 lhs_T *lhs,
6527 char_u *var_start,
6528 type_T *rhs_type,
6529 cctx_T *cctx)
6530{
6531 if (lhs->lhs_dest == dest_expr)
6532 {
6533 size_t varlen = lhs->lhs_varlen;
6534 int c = var_start[varlen];
6535 char_u *p = var_start;
6536 garray_T *stack = &cctx->ctx_type_stack;
6537
6538 // Evaluate "ll[expr]" of "ll[expr][idx]"
6539 var_start[varlen] = NUL;
6540 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6541 {
6542 // this should not happen
6543 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006544 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006545 return FAIL;
6546 }
6547 var_start[varlen] = c;
6548
6549 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6550 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6551 // now we can properly check the type
6552 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6553 && rhs_type != &t_void
6554 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6555 FALSE, FALSE) == FAIL)
6556 return FAIL;
6557 }
6558 else
6559 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6560 lhs->lhs_lvar, lhs->lhs_type);
6561 return OK;
6562}
6563
6564/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006565 * Produce code for loading "lhs" and also take care of an index.
6566 * Return OK/FAIL.
6567 */
6568 static int
6569compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6570{
6571 compile_load_lhs(lhs, var_start, NULL, cctx);
6572
6573 if (lhs->lhs_has_index)
6574 {
6575 int range = FALSE;
6576
6577 // Get member from list or dict. First compile the
6578 // index value.
6579 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6580 return FAIL;
6581 if (range)
6582 {
6583 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6584 var_start);
6585 return FAIL;
6586 }
6587
6588 // Get the member.
6589 if (compile_member(FALSE, cctx) == FAIL)
6590 return FAIL;
6591 }
6592 return OK;
6593}
6594
6595/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006596 * Assignment to a list or dict member, or ":unlet" for the item, using the
6597 * information in "lhs".
6598 * Returns OK or FAIL.
6599 */
6600 static int
6601compile_assign_unlet(
6602 char_u *var_start,
6603 lhs_T *lhs,
6604 int is_assign,
6605 type_T *rhs_type,
6606 cctx_T *cctx)
6607{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006608 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006609 garray_T *stack = &cctx->ctx_type_stack;
6610 int range = FALSE;
6611
Bram Moolenaar68452172021-04-12 21:21:02 +02006612 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006613 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006614 if (is_assign && range && lhs->lhs_type != &t_blob
6615 && lhs->lhs_type != &t_any)
6616 {
6617 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6618 return FAIL;
6619 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006620
6621 if (lhs->lhs_type == &t_any)
6622 {
6623 // Index on variable of unknown type: check at runtime.
6624 dest_type = VAR_ANY;
6625 }
6626 else
6627 {
6628 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006629 if (dest_type == VAR_DICT && range)
6630 {
6631 emsg(e_cannot_use_range_with_dictionary);
6632 return FAIL;
6633 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006634 if (dest_type == VAR_DICT
6635 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006636 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006637 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006638 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006639 type_T *type;
6640
6641 if (range)
6642 {
6643 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6644 if (need_type(type, &t_number,
6645 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006646 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006647 }
6648 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006649 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006650 && need_type(type, &t_number,
6651 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006652 return FAIL;
6653 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006654 }
6655
6656 // Load the dict or list. On the stack we then have:
6657 // - value (for assignment, not for :unlet)
6658 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006659 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006660 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006661 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6662 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006663
Bram Moolenaar68452172021-04-12 21:21:02 +02006664 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6665 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006666 {
6667 if (is_assign)
6668 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006669 if (range)
6670 {
6671 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6672 return FAIL;
6673 }
6674 else
6675 {
6676 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006677
Bram Moolenaar68452172021-04-12 21:21:02 +02006678 if (isn == NULL)
6679 return FAIL;
6680 isn->isn_arg.vartype = dest_type;
6681 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006682 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006683 else if (range)
6684 {
6685 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6686 return FAIL;
6687 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006688 else
6689 {
6690 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6691 return FAIL;
6692 }
6693 }
6694 else
6695 {
6696 emsg(_(e_indexable_type_required));
6697 return FAIL;
6698 }
6699
6700 return OK;
6701}
6702
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006703/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006704 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006705 * "let name"
6706 * "var name = expr"
6707 * "final name = expr"
6708 * "const name = expr"
6709 * "name = expr"
6710 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006711 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006712 * Return NULL for an error.
6713 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 */
6715 static char_u *
6716compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6717{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006718 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006720 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721 char_u *ret = NULL;
6722 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006723 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006724 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006725 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006727 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729 int oplen = 0;
6730 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006731 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006732 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006733 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006734 int is_decl = is_decl_command(cmdidx);
6735 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006736 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006738 // Skip over the "var" or "[var, var]" to get to any "=".
6739 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6740 if (p == NULL)
6741 return *arg == '[' ? arg : NULL;
6742
6743 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006745 // TODO: should we allow this, and figure out type inference from list
6746 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006747 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006748 return NULL;
6749 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006750 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 sp = p;
6753 p = skipwhite(p);
6754 op = p;
6755 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006756
6757 if (var_count > 0 && oplen == 0)
6758 // can be something like "[1, 2]->func()"
6759 return arg;
6760
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006761 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006763 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006764 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006765 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006766 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6767 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006768 if (VIM_ISWHITE(eap->cmd[2]))
6769 {
6770 semsg(_(e_no_white_space_allowed_after_str_str),
6771 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6772 return NULL;
6773 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006774 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6775 oplen = 2;
6776 incdec = TRUE;
6777 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779 if (heredoc)
6780 {
6781 list_T *l;
6782 listitem_T *li;
6783
6784 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006785 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006787 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006788 if (l == NULL)
6789 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790
Bram Moolenaar078269b2020-09-21 20:35:55 +02006791 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006793 // Push each line and the create the list.
6794 FOR_ALL_LIST_ITEMS(l, li)
6795 {
6796 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6797 li->li_tv.vval.v_string = NULL;
6798 }
6799 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801 list_free(l);
6802 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006803 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006805 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006807 char_u *wp;
6808
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006809 // for "[var, var] = expr" evaluate the expression here, loop over the
6810 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006811 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006812
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006813 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006814 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006815 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006816 if (compile_expr0(&p, cctx) == FAIL)
6817 return NULL;
6818 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006820 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006821 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006822 type_T *stacktype;
6823
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006824 stacktype = stack->ga_len == 0 ? &t_void
6825 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006826 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006827 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006828 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006829 goto theend;
6830 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006831 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006832 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006833 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006834 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006835 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6836 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006837 if (stacktype->tt_member != NULL)
6838 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006839 }
6840 }
6841
6842 /*
6843 * Loop over variables in "[var, var] = expr".
6844 * For "var = expr" and "let var: type" this is done only once.
6845 */
6846 if (var_count > 0)
6847 var_start = skipwhite(arg + 1); // skip over the "["
6848 else
6849 var_start = arg;
6850 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6851 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006852 int instr_count = -1;
6853 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006854
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006855 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6856 {
6857 // Ignore underscore in "[a, _, b] = list".
6858 if (var_count > 0)
6859 {
6860 var_start = skipwhite(var_start + 2);
6861 continue;
6862 }
6863 emsg(_(e_cannot_use_underscore_here));
6864 goto theend;
6865 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006866 vim_free(lhs.lhs_name);
6867
6868 /*
6869 * Figure out the LHS type and other properties.
6870 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006871 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6872 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006873 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006874 if (!heredoc)
6875 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006876 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006877 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006878 if (oplen > 0 && var_count == 0)
6879 {
6880 // skip over the "=" and the expression
6881 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006882 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006883 }
6884 }
6885 else if (oplen > 0)
6886 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006887 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006888 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006889
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006890 // for "+=", "*=", "..=" etc. first load the current value
6891 if (*op != '='
6892 && compile_load_lhs_with_index(&lhs, var_start,
6893 cctx) == FAIL)
6894 goto theend;
6895
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006896 // For "var = expr" evaluate the expression.
6897 if (var_count == 0)
6898 {
6899 int r;
6900
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006901 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006902 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006903 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006904 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006905 r = generate_PUSHNR(cctx, 1);
6906 }
6907 else
6908 {
6909 // Temporarily hide the new local variable here, it is
6910 // not available to this expression.
6911 if (lhs.lhs_new_local)
6912 --cctx->ctx_locals.ga_len;
6913 wp = op + oplen;
6914 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6915 {
6916 if (lhs.lhs_new_local)
6917 ++cctx->ctx_locals.ga_len;
6918 goto theend;
6919 }
6920 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006921 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006922 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006923 if (r == FAIL)
6924 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006925 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006926 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006927 else if (semicolon && var_idx == var_count - 1)
6928 {
6929 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006930 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006931 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6932 goto theend;
6933 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006934 else
6935 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006936 // For "[var, var] = expr" get the "var_idx" item from the
6937 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006938 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006939 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006940 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006941
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006942 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006943 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006944 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006945 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006946 if ((rhs_type->tt_type == VAR_FUNC
6947 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006948 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006949 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006950 goto theend;
6951
Bram Moolenaar752fc692021-01-04 21:57:11 +01006952 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006953 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006954 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006955 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006956 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006957 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006958 }
6959 else
6960 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006961 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006962 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006963 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006964 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006965 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006966 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006967 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006968 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006969 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006970 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006971 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006972 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006973 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006974 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006975 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006976
Bram Moolenaar77709b12021-04-03 21:01:01 +02006977 // Without operator check type here, otherwise below.
6978 // Use the line number of the assignment.
6979 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006980 if (lhs.lhs_has_index)
6981 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006982 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006983 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006984 goto theend;
6985 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006986 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006987 else
6988 {
6989 type_T *lhs_type = lhs.lhs_member_type;
6990
6991 // Special case: assigning to @# can use a number or a
6992 // string.
6993 if (lhs_type == &t_number_or_string
6994 && rhs_type->tt_type == VAR_NUMBER)
6995 lhs_type = &t_number;
6996 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006997 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006998 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006999 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007001 else if (cmdidx == CMD_final)
7002 {
7003 emsg(_(e_final_requires_a_value));
7004 goto theend;
7005 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007006 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007008 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007009 goto theend;
7010 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007011 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007012 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007013 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007014 goto theend;
7015 }
7016 else
7017 {
7018 // variables are always initialized
7019 if (ga_grow(instr, 1) == FAIL)
7020 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007021 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007022 {
7023 case VAR_BOOL:
7024 generate_PUSHBOOL(cctx, VVAL_FALSE);
7025 break;
7026 case VAR_FLOAT:
7027#ifdef FEAT_FLOAT
7028 generate_PUSHF(cctx, 0.0);
7029#endif
7030 break;
7031 case VAR_STRING:
7032 generate_PUSHS(cctx, NULL);
7033 break;
7034 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007035 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007036 break;
7037 case VAR_FUNC:
7038 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7039 break;
7040 case VAR_LIST:
7041 generate_NEWLIST(cctx, 0);
7042 break;
7043 case VAR_DICT:
7044 generate_NEWDICT(cctx, 0);
7045 break;
7046 case VAR_JOB:
7047 generate_PUSHJOB(cctx, NULL);
7048 break;
7049 case VAR_CHANNEL:
7050 generate_PUSHCHANNEL(cctx, NULL);
7051 break;
7052 case VAR_NUMBER:
7053 case VAR_UNKNOWN:
7054 case VAR_ANY:
7055 case VAR_PARTIAL:
7056 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007057 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007058 case VAR_SPECIAL: // cannot happen
7059 generate_PUSHNR(cctx, 0);
7060 break;
7061 }
7062 }
7063 if (var_count == 0)
7064 end = p;
7065 }
7066
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007067 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007068 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007069 break;
7070
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007071 if (oplen > 0 && *op != '=')
7072 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007073 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007074 type_T *stacktype;
7075
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007076 if (*op == '.')
7077 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007078 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007079 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007080 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007081 if (
7082#ifdef FEAT_FLOAT
7083 // If variable is float operation with number is OK.
7084 !(expected == &t_float && stacktype == &t_number) &&
7085#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007086 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007087 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007088 goto theend;
7089
7090 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007091 {
7092 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7093 goto theend;
7094 }
7095 else if (*op == '+')
7096 {
7097 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007098 operator_type(lhs.lhs_member_type, stacktype),
7099 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007100 goto theend;
7101 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007102 else if (generate_two_op(cctx, op) == FAIL)
7103 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007106 // Use the line number of the assignment for store instruction.
7107 save_lnum = cctx->ctx_lnum;
7108 cctx->ctx_lnum = start_lnum - 1;
7109
Bram Moolenaar752fc692021-01-04 21:57:11 +01007110 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007111 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007112 // Use the info in "lhs" to store the value at the index in the
7113 // list or dict.
7114 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7115 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007116 {
7117 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007118 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007119 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007120 }
7121 else
7122 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007123 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007124 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007125 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007126 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007127 generate_LOCKCONST(cctx);
7128
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007129 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007130 && (lhs.lhs_type->tt_type == VAR_DICT
7131 || lhs.lhs_type->tt_type == VAR_LIST)
7132 && lhs.lhs_type->tt_member != NULL
7133 && lhs.lhs_type->tt_member != &t_any
7134 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007135 // Set the type in the list or dict, so that it can be checked,
7136 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007137 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007138
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007139 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007140 {
7141 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007142 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007143 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007144 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007145 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007146
7147 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007148 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007150
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007151 // For "[var, var] = expr" drop the "expr" value.
7152 // Also for "[var, var; _] = expr".
7153 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007154 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007155 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007156 goto theend;
7157 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007158
Bram Moolenaarb2097502020-07-19 17:17:02 +02007159 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007160
7161theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007162 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 return ret;
7164}
7165
7166/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007167 * Check for an assignment at "eap->cmd", compile it if found.
7168 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7169 */
7170 static int
7171may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7172{
7173 char_u *pskip;
7174 char_u *p;
7175
7176 // Assuming the command starts with a variable or function name,
7177 // find what follows.
7178 // Skip over "var.member", "var[idx]" and the like.
7179 // Also "&opt = val", "$ENV = val" and "@r = val".
7180 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7181 ? eap->cmd + 1 : eap->cmd;
7182 p = to_name_end(pskip, TRUE);
7183 if (p > eap->cmd && *p != NUL)
7184 {
7185 char_u *var_end;
7186 int oplen;
7187 int heredoc;
7188
7189 if (eap->cmd[0] == '@')
7190 var_end = eap->cmd + 2;
7191 else
7192 var_end = find_name_end(pskip, NULL, NULL,
7193 FNE_CHECK_START | FNE_INCL_BR);
7194 oplen = assignment_len(skipwhite(var_end), &heredoc);
7195 if (oplen > 0)
7196 {
7197 size_t len = p - eap->cmd;
7198
7199 // Recognize an assignment if we recognize the variable
7200 // name:
7201 // "g:var = expr"
7202 // "local = expr" where "local" is a local var.
7203 // "script = expr" where "script" is a script-local var.
7204 // "import = expr" where "import" is an imported var
7205 // "&opt = expr"
7206 // "$ENV = expr"
7207 // "@r = expr"
7208 if (*eap->cmd == '&'
7209 || *eap->cmd == '$'
7210 || *eap->cmd == '@'
7211 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007212 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007213 {
7214 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7215 if (*line == NULL || *line == eap->cmd)
7216 return FAIL;
7217 return OK;
7218 }
7219 }
7220 }
7221
7222 if (*eap->cmd == '[')
7223 {
7224 // [var, var] = expr
7225 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7226 if (*line == NULL)
7227 return FAIL;
7228 if (*line != eap->cmd)
7229 return OK;
7230 }
7231 return NOTDONE;
7232}
7233
7234/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007235 * Check if "name" can be "unlet".
7236 */
7237 int
7238check_vim9_unlet(char_u *name)
7239{
7240 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7241 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007242 // "unlet s:var" is allowed in legacy script.
7243 if (*name == 's' && !script_is_vim9())
7244 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007245 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007246 return FAIL;
7247 }
7248 return OK;
7249}
7250
7251/*
7252 * Callback passed to ex_unletlock().
7253 */
7254 static int
7255compile_unlet(
7256 lval_T *lvp,
7257 char_u *name_end,
7258 exarg_T *eap,
7259 int deep UNUSED,
7260 void *coookie)
7261{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007262 cctx_T *cctx = coookie;
7263 char_u *p = lvp->ll_name;
7264 int cc = *name_end;
7265 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007266
Bram Moolenaar752fc692021-01-04 21:57:11 +01007267 if (cctx->ctx_skip == SKIP_YES)
7268 return OK;
7269
7270 *name_end = NUL;
7271 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007272 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007273 // :unlet $ENV_VAR
7274 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7275 }
7276 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7277 {
7278 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007279
Bram Moolenaar752fc692021-01-04 21:57:11 +01007280 // This is similar to assigning: lookup the list/dict, compile the
7281 // idx/key. Then instead of storing the value unlet the item.
7282 // unlet {list}[idx]
7283 // unlet {dict}[key] dict.key
7284 //
7285 // Figure out the LHS type and other properties.
7286 //
7287 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7288
7289 // : unlet an indexed item
7290 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007291 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007292 iemsg("called compile_lhs() without an index");
7293 ret = FAIL;
7294 }
7295 else
7296 {
7297 // Use the info in "lhs" to unlet the item at the index in the
7298 // list or dict.
7299 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007300 }
7301
Bram Moolenaar752fc692021-01-04 21:57:11 +01007302 vim_free(lhs.lhs_name);
7303 }
7304 else if (check_vim9_unlet(p) == FAIL)
7305 {
7306 ret = FAIL;
7307 }
7308 else
7309 {
7310 // Normal name. Only supports g:, w:, t: and b: namespaces.
7311 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007312 }
7313
Bram Moolenaar752fc692021-01-04 21:57:11 +01007314 *name_end = cc;
7315 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007316}
7317
7318/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007319 * Callback passed to ex_unletlock().
7320 */
7321 static int
7322compile_lock_unlock(
7323 lval_T *lvp,
7324 char_u *name_end,
7325 exarg_T *eap,
7326 int deep UNUSED,
7327 void *coookie)
7328{
7329 cctx_T *cctx = coookie;
7330 int cc = *name_end;
7331 char_u *p = lvp->ll_name;
7332 int ret = OK;
7333 size_t len;
7334 char_u *buf;
7335
7336 if (cctx->ctx_skip == SKIP_YES)
7337 return OK;
7338
7339 // Cannot use :lockvar and :unlockvar on local variables.
7340 if (p[1] != ':')
7341 {
7342 char_u *end = skip_var_one(p, FALSE);
7343
7344 if (lookup_local(p, end - p, NULL, cctx) == OK)
7345 {
7346 emsg(_(e_cannot_lock_unlock_local_variable));
7347 return FAIL;
7348 }
7349 }
7350
7351 // Checking is done at runtime.
7352 *name_end = NUL;
7353 len = name_end - p + 20;
7354 buf = alloc(len);
7355 if (buf == NULL)
7356 ret = FAIL;
7357 else
7358 {
7359 vim_snprintf((char *)buf, len, "%s %s",
7360 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7361 p);
7362 ret = generate_EXEC(cctx, buf);
7363
7364 vim_free(buf);
7365 *name_end = cc;
7366 }
7367 return ret;
7368}
7369
7370/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007371 * compile "unlet var", "lock var" and "unlock var"
7372 * "arg" points to "var".
7373 */
7374 static char_u *
7375compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7376{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007377 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7378 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7379 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007380 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7381}
7382
7383/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7385 */
7386 static int
7387compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7388{
7389 garray_T *instr = &cctx->ctx_instr;
7390 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7391
7392 if (endlabel == NULL)
7393 return FAIL;
7394 endlabel->el_next = *el;
7395 *el = endlabel;
7396 endlabel->el_end_label = instr->ga_len;
7397
7398 generate_JUMP(cctx, when, 0);
7399 return OK;
7400}
7401
7402 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007403compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404{
7405 garray_T *instr = &cctx->ctx_instr;
7406
7407 while (*el != NULL)
7408 {
7409 endlabel_T *cur = (*el);
7410 isn_T *isn;
7411
7412 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007413 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414 *el = cur->el_next;
7415 vim_free(cur);
7416 }
7417}
7418
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007419 static void
7420compile_free_jump_to_end(endlabel_T **el)
7421{
7422 while (*el != NULL)
7423 {
7424 endlabel_T *cur = (*el);
7425
7426 *el = cur->el_next;
7427 vim_free(cur);
7428 }
7429}
7430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431/*
7432 * Create a new scope and set up the generic items.
7433 */
7434 static scope_T *
7435new_scope(cctx_T *cctx, scopetype_T type)
7436{
7437 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7438
7439 if (scope == NULL)
7440 return NULL;
7441 scope->se_outer = cctx->ctx_scope;
7442 cctx->ctx_scope = scope;
7443 scope->se_type = type;
7444 scope->se_local_count = cctx->ctx_locals.ga_len;
7445 return scope;
7446}
7447
7448/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007449 * Free the current scope and go back to the outer scope.
7450 */
7451 static void
7452drop_scope(cctx_T *cctx)
7453{
7454 scope_T *scope = cctx->ctx_scope;
7455
7456 if (scope == NULL)
7457 {
7458 iemsg("calling drop_scope() without a scope");
7459 return;
7460 }
7461 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007462 switch (scope->se_type)
7463 {
7464 case IF_SCOPE:
7465 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7466 case FOR_SCOPE:
7467 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7468 case WHILE_SCOPE:
7469 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7470 case TRY_SCOPE:
7471 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7472 case NO_SCOPE:
7473 case BLOCK_SCOPE:
7474 break;
7475 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007476 vim_free(scope);
7477}
7478
7479/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 * compile "if expr"
7481 *
7482 * "if expr" Produces instructions:
7483 * EVAL expr Push result of "expr"
7484 * JUMP_IF_FALSE end
7485 * ... body ...
7486 * end:
7487 *
7488 * "if expr | else" Produces instructions:
7489 * EVAL expr Push result of "expr"
7490 * JUMP_IF_FALSE else
7491 * ... body ...
7492 * JUMP_ALWAYS end
7493 * else:
7494 * ... body ...
7495 * end:
7496 *
7497 * "if expr1 | elseif expr2 | else" Produces instructions:
7498 * EVAL expr Push result of "expr"
7499 * JUMP_IF_FALSE elseif
7500 * ... body ...
7501 * JUMP_ALWAYS end
7502 * elseif:
7503 * EVAL expr Push result of "expr"
7504 * JUMP_IF_FALSE else
7505 * ... body ...
7506 * JUMP_ALWAYS end
7507 * else:
7508 * ... body ...
7509 * end:
7510 */
7511 static char_u *
7512compile_if(char_u *arg, cctx_T *cctx)
7513{
7514 char_u *p = arg;
7515 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007516 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007518 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007519 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007520
Bram Moolenaara5565e42020-05-09 15:44:01 +02007521 CLEAR_FIELD(ppconst);
7522 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007523 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007524 clear_ppconst(&ppconst);
7525 return NULL;
7526 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007527 if (!ends_excmd2(arg, skipwhite(p)))
7528 {
7529 semsg(_(e_trailing_arg), p);
7530 return NULL;
7531 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007532 if (cctx->ctx_skip == SKIP_YES)
7533 clear_ppconst(&ppconst);
7534 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007535 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007536 int error = FALSE;
7537 int v;
7538
Bram Moolenaara5565e42020-05-09 15:44:01 +02007539 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007540 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007541 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007542 if (error)
7543 return NULL;
7544 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007545 }
7546 else
7547 {
7548 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007549 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007550 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007551 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007552 if (bool_on_stack(cctx) == FAIL)
7553 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007555
Bram Moolenaara91a7132021-03-25 21:12:15 +01007556 // CMDMOD_REV must come before the jump
7557 generate_undo_cmdmods(cctx);
7558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007559 scope = new_scope(cctx, IF_SCOPE);
7560 if (scope == NULL)
7561 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007562 scope->se_skip_save = skip_save;
7563 // "is_had_return" will be reset if any block does not end in :return
7564 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007566 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007567 {
7568 // "where" is set when ":elseif", "else" or ":endif" is found
7569 scope->se_u.se_if.is_if_label = instr->ga_len;
7570 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7571 }
7572 else
7573 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574
Bram Moolenaarced68a02021-01-24 17:53:47 +01007575#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007576 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007577 && skip_save != SKIP_YES)
7578 {
7579 // generated a profile start, need to generate a profile end, since it
7580 // won't be done after returning
7581 cctx->ctx_skip = SKIP_NOT;
7582 generate_instr(cctx, ISN_PROF_END);
7583 cctx->ctx_skip = SKIP_YES;
7584 }
7585#endif
7586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007587 return p;
7588}
7589
7590 static char_u *
7591compile_elseif(char_u *arg, cctx_T *cctx)
7592{
7593 char_u *p = arg;
7594 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007595 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007596 isn_T *isn;
7597 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007598 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007599 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007600
7601 if (scope == NULL || scope->se_type != IF_SCOPE)
7602 {
7603 emsg(_(e_elseif_without_if));
7604 return NULL;
7605 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007606 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007607 if (!cctx->ctx_had_return)
7608 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609
Bram Moolenaarced68a02021-01-24 17:53:47 +01007610 if (cctx->ctx_skip == SKIP_NOT)
7611 {
7612 // previous block was executed, this one and following will not
7613 cctx->ctx_skip = SKIP_YES;
7614 scope->se_u.se_if.is_seen_skip_not = TRUE;
7615 }
7616 if (scope->se_u.se_if.is_seen_skip_not)
7617 {
7618 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007619 // Do not count the "elseif" for profiling and cmdmod
7620 instr->ga_len = current_instr_idx(cctx);
7621
Bram Moolenaarced68a02021-01-24 17:53:47 +01007622 skip_expr_cctx(&p, cctx);
7623 return p;
7624 }
7625
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007626 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007627 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007628 int moved_cmdmod = FALSE;
7629
7630 // Move any CMDMOD instruction to after the jump
7631 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7632 {
7633 if (ga_grow(instr, 1) == FAIL)
7634 return NULL;
7635 ((isn_T *)instr->ga_data)[instr->ga_len] =
7636 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7637 --instr->ga_len;
7638 moved_cmdmod = TRUE;
7639 }
7640
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007641 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007643 return NULL;
7644 // previous "if" or "elseif" jumps here
7645 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7646 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007647 if (moved_cmdmod)
7648 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007650
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007651 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007652 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007653 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007654 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007655 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007656#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007657 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007658 {
7659 // the previous block was skipped, need to profile this line
7660 generate_instr(cctx, ISN_PROF_START);
7661 instr_count = instr->ga_len;
7662 }
7663#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007664 if (cctx->ctx_compile_type == CT_DEBUG)
7665 {
7666 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007667 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007668 instr_count = instr->ga_len;
7669 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007670 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007671 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007672 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007673 clear_ppconst(&ppconst);
7674 return NULL;
7675 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007676 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007677 if (!ends_excmd2(arg, skipwhite(p)))
7678 {
7679 semsg(_(e_trailing_arg), p);
7680 return NULL;
7681 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007682 if (scope->se_skip_save == SKIP_YES)
7683 clear_ppconst(&ppconst);
7684 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007685 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007686 int error = FALSE;
7687 int v;
7688
Bram Moolenaar7f141552020-05-09 17:35:53 +02007689 // The expression results in a constant.
7690 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007691 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7692 if (error)
7693 return NULL;
7694 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007695 clear_ppconst(&ppconst);
7696 scope->se_u.se_if.is_if_label = -1;
7697 }
7698 else
7699 {
7700 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007701 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007702 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007703 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007704 if (bool_on_stack(cctx) == FAIL)
7705 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007706
Bram Moolenaara91a7132021-03-25 21:12:15 +01007707 // CMDMOD_REV must come before the jump
7708 generate_undo_cmdmods(cctx);
7709
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007710 // "where" is set when ":elseif", "else" or ":endif" is found
7711 scope->se_u.se_if.is_if_label = instr->ga_len;
7712 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714
7715 return p;
7716}
7717
7718 static char_u *
7719compile_else(char_u *arg, cctx_T *cctx)
7720{
7721 char_u *p = arg;
7722 garray_T *instr = &cctx->ctx_instr;
7723 isn_T *isn;
7724 scope_T *scope = cctx->ctx_scope;
7725
7726 if (scope == NULL || scope->se_type != IF_SCOPE)
7727 {
7728 emsg(_(e_else_without_if));
7729 return NULL;
7730 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007731 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007732 if (!cctx->ctx_had_return)
7733 scope->se_u.se_if.is_had_return = FALSE;
7734 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735
Bram Moolenaarced68a02021-01-24 17:53:47 +01007736#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007737 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007738 {
7739 if (cctx->ctx_skip == SKIP_NOT
7740 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7741 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007742 // the previous block was executed, do not count "else" for
7743 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007744 --instr->ga_len;
7745 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7746 {
7747 // the previous block was not executed, this one will, do count the
7748 // "else" for profiling
7749 cctx->ctx_skip = SKIP_NOT;
7750 generate_instr(cctx, ISN_PROF_END);
7751 generate_instr(cctx, ISN_PROF_START);
7752 cctx->ctx_skip = SKIP_YES;
7753 }
7754 }
7755#endif
7756
7757 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007758 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007759 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007760 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007761 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007762 if (!cctx->ctx_had_return
7763 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7764 JUMP_ALWAYS, cctx) == FAIL)
7765 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007766 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007767
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007768 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007769 {
7770 if (scope->se_u.se_if.is_if_label >= 0)
7771 {
7772 // previous "if" or "elseif" jumps here
7773 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7774 isn->isn_arg.jump.jump_where = instr->ga_len;
7775 scope->se_u.se_if.is_if_label = -1;
7776 }
7777 }
7778
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007779 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007780 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007782
7783 return p;
7784}
7785
7786 static char_u *
7787compile_endif(char_u *arg, cctx_T *cctx)
7788{
7789 scope_T *scope = cctx->ctx_scope;
7790 ifscope_T *ifscope;
7791 garray_T *instr = &cctx->ctx_instr;
7792 isn_T *isn;
7793
Bram Moolenaarfa984412021-03-25 22:15:28 +01007794 if (misplaced_cmdmod(cctx))
7795 return NULL;
7796
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007797 if (scope == NULL || scope->se_type != IF_SCOPE)
7798 {
7799 emsg(_(e_endif_without_if));
7800 return NULL;
7801 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007802 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007803 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007804 if (!cctx->ctx_had_return)
7805 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007806
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007807 if (scope->se_u.se_if.is_if_label >= 0)
7808 {
7809 // previous "if" or "elseif" jumps here
7810 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7811 isn->isn_arg.jump.jump_where = instr->ga_len;
7812 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007813 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007814 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007815
7816#ifdef FEAT_PROFILE
7817 // even when skipping we count the endif as executed, unless the block it's
7818 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007819 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007820 && scope->se_skip_save != SKIP_YES)
7821 {
7822 cctx->ctx_skip = SKIP_NOT;
7823 generate_instr(cctx, ISN_PROF_START);
7824 }
7825#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007826 cctx->ctx_skip = scope->se_skip_save;
7827
7828 // If all the blocks end in :return and there is an :else then the
7829 // had_return flag is set.
7830 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007831
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007832 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007833 return arg;
7834}
7835
7836/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007837 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007838 *
7839 * Produces instructions:
7840 * PUSHNR -1
7841 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007842 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007843 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7844 * - if beyond end, jump to "end"
7845 * - otherwise get item from list and push it
7846 * STORE var Store item in "var"
7847 * ... body ...
7848 * JUMP top Jump back to repeat
7849 * end: DROP Drop the result of "expr"
7850 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007851 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7852 * UNPACK 2 Split item in 2
7853 * STORE var1 Store item in "var1"
7854 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007855 */
7856 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007857compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007858{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007859 char_u *arg;
7860 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007861 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007863 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007864 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007865 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007866 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007867 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007869 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007871 lvar_T *loop_lvar; // loop iteration variable
7872 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007874 type_T *item_type = &t_any;
7875 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007876 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007877
Bram Moolenaar792f7862020-11-23 08:31:18 +01007878 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007879 if (p == NULL)
7880 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007881 if (var_count == 0)
7882 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007883 else
7884 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007885
7886 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007887 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007888 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7889 return NULL;
7890 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007891 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007892 if (*p == ':' && wp != p)
7893 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7894 else
7895 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007896 return NULL;
7897 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007898 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007899 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7900 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007901
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007902 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7903 // instruction.
7904 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7905 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7906 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007907 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007908 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007909 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7910 .isn_arg.debug.dbg_break_lnum;
7911 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007913 scope = new_scope(cctx, FOR_SCOPE);
7914 if (scope == NULL)
7915 return NULL;
7916
Bram Moolenaar792f7862020-11-23 08:31:18 +01007917 // Reserve a variable to store the loop iteration counter and initialize it
7918 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007919 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7920 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007921 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007922 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007923 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007924 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007925 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007926 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007927
7928 // compile "expr", it remains on the stack until "endfor"
7929 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007930 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007931 {
7932 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007933 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007934 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007935 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007936
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007937 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007938 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007939 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007940 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007941 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007942 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007943 semsg(_(e_for_loop_on_str_not_supported),
7944 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007945 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007946 return NULL;
7947 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007948
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007949 if (vartype->tt_type == VAR_STRING)
7950 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007951 else if (vartype->tt_type == VAR_BLOB)
7952 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007953 else if (vartype->tt_type == VAR_LIST
7954 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007955 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007956 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007957 item_type = vartype->tt_member;
7958 else if (vartype->tt_member->tt_type == VAR_LIST
7959 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007960 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007961 item_type = vartype->tt_member->tt_member;
7962 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007963
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007964 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007965 generate_undo_cmdmods(cctx);
7966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007967 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007968 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007969
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007970 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007971
Bram Moolenaar792f7862020-11-23 08:31:18 +01007972 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007973 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007974 {
7975 generate_UNPACK(cctx, var_count, semicolon);
7976 arg = skipwhite(arg + 1); // skip white after '['
7977
7978 // the list item is replaced by a number of items
7979 if (ga_grow(stack, var_count - 1) == FAIL)
7980 {
7981 drop_scope(cctx);
7982 return NULL;
7983 }
7984 --stack->ga_len;
7985 for (idx = 0; idx < var_count; ++idx)
7986 {
7987 ((type_T **)stack->ga_data)[stack->ga_len] =
7988 (semicolon && idx == 0) ? vartype : item_type;
7989 ++stack->ga_len;
7990 }
7991 }
7992
7993 for (idx = 0; idx < var_count; ++idx)
7994 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007995 assign_dest_T dest = dest_local;
7996 int opt_flags = 0;
7997 int vimvaridx = -1;
7998 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007999 type_T *lhs_type = &t_any;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02008000 where_T where = WHERE_INIT;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008001
8002 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008003 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008004 name = vim_strnsave(arg, varlen);
8005 if (name == NULL)
8006 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008007 if (*p == ':')
8008 {
8009 p = skipwhite(p + 1);
8010 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8011 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008012
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008013 // TODO: script var not supported?
8014 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
8015 &vimvaridx, &type, cctx) == FAIL)
8016 goto failed;
8017 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008018 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008019 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
8020 0, 0, type, name) == FAIL)
8021 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008022 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02008023 else if (varlen == 1 && *arg == '_')
8024 {
8025 // Assigning to "_": drop the value.
8026 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8027 goto failed;
8028 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008029 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008030 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01008031 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008032 {
8033 semsg(_(e_variable_already_declared), arg);
8034 goto failed;
8035 }
8036
Bram Moolenaarea870692020-12-02 14:24:30 +01008037 if (STRNCMP(name, "s:", 2) == 0)
8038 {
8039 semsg(_(e_cannot_declare_script_variable_in_function), name);
8040 goto failed;
8041 }
8042
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008043 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02008044 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008045 where.wt_variable = TRUE;
8046 if (lhs_type == &t_any)
8047 lhs_type = item_type;
8048 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02008049 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02008050 ? need_type(item_type, lhs_type,
8051 -1, 0, cctx, FALSE, FALSE)
8052 : check_type(lhs_type, item_type, TRUE, where))
8053 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008054 goto failed;
8055 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008056 if (var_lvar == NULL)
8057 // out of memory or used as an argument
8058 goto failed;
8059
8060 if (semicolon && idx == var_count - 1)
8061 var_lvar->lv_type = vartype;
8062 else
8063 var_lvar->lv_type = item_type;
8064 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8065 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008066
8067 if (*p == ',' || *p == ';')
8068 ++p;
8069 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008070 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008071 }
8072
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008073 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008074 {
8075 int save_prev_lnum = cctx->ctx_prev_lnum;
8076
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008077 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008078 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8079 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008080 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008081 cctx->ctx_prev_lnum = save_prev_lnum;
8082 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008083
Bram Moolenaar792f7862020-11-23 08:31:18 +01008084 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008085
8086failed:
8087 vim_free(name);
8088 drop_scope(cctx);
8089 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008090}
8091
8092/*
8093 * compile "endfor"
8094 */
8095 static char_u *
8096compile_endfor(char_u *arg, cctx_T *cctx)
8097{
8098 garray_T *instr = &cctx->ctx_instr;
8099 scope_T *scope = cctx->ctx_scope;
8100 forscope_T *forscope;
8101 isn_T *isn;
8102
Bram Moolenaarfa984412021-03-25 22:15:28 +01008103 if (misplaced_cmdmod(cctx))
8104 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008106 if (scope == NULL || scope->se_type != FOR_SCOPE)
8107 {
8108 emsg(_(e_for));
8109 return NULL;
8110 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008111 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008112 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008113 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008114
8115 // At end of ":for" scope jump back to the FOR instruction.
8116 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8117
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008118 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008119 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8120 isn->isn_arg.forloop.for_end = instr->ga_len;
8121
8122 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008123 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008124
8125 // Below the ":for" scope drop the "expr" list from the stack.
8126 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8127 return NULL;
8128
8129 vim_free(scope);
8130
8131 return arg;
8132}
8133
8134/*
8135 * compile "while expr"
8136 *
8137 * Produces instructions:
8138 * top: EVAL expr Push result of "expr"
8139 * JUMP_IF_FALSE end jump if false
8140 * ... body ...
8141 * JUMP top Jump back to repeat
8142 * end:
8143 *
8144 */
8145 static char_u *
8146compile_while(char_u *arg, cctx_T *cctx)
8147{
8148 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008149 scope_T *scope;
8150
8151 scope = new_scope(cctx, WHILE_SCOPE);
8152 if (scope == NULL)
8153 return NULL;
8154
Bram Moolenaara91a7132021-03-25 21:12:15 +01008155 // "endwhile" jumps back here, one before when profiling or using cmdmods
8156 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008157
8158 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008159 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008160 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008161 if (!ends_excmd2(arg, skipwhite(p)))
8162 {
8163 semsg(_(e_trailing_arg), p);
8164 return NULL;
8165 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166
Bram Moolenaar13106602020-10-04 16:06:05 +02008167 if (bool_on_stack(cctx) == FAIL)
8168 return FAIL;
8169
Bram Moolenaara91a7132021-03-25 21:12:15 +01008170 // CMDMOD_REV must come before the jump
8171 generate_undo_cmdmods(cctx);
8172
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008173 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008174 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008175 JUMP_IF_FALSE, cctx) == FAIL)
8176 return FAIL;
8177
8178 return p;
8179}
8180
8181/*
8182 * compile "endwhile"
8183 */
8184 static char_u *
8185compile_endwhile(char_u *arg, cctx_T *cctx)
8186{
8187 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008188 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189
Bram Moolenaarfa984412021-03-25 22:15:28 +01008190 if (misplaced_cmdmod(cctx))
8191 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008192 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8193 {
8194 emsg(_(e_while));
8195 return NULL;
8196 }
8197 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008198 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008199
Bram Moolenaarf002a412021-01-24 13:34:18 +01008200#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008201 // count the endwhile before jumping
8202 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008203#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008205 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008206 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008207
8208 // Fill in the "end" label in the WHILE statement so it can jump here.
8209 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008210 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8211 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008212
8213 vim_free(scope);
8214
8215 return arg;
8216}
8217
8218/*
8219 * compile "continue"
8220 */
8221 static char_u *
8222compile_continue(char_u *arg, cctx_T *cctx)
8223{
8224 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008225 int try_scopes = 0;
8226 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008227
8228 for (;;)
8229 {
8230 if (scope == NULL)
8231 {
8232 emsg(_(e_continue));
8233 return NULL;
8234 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008235 if (scope->se_type == FOR_SCOPE)
8236 {
8237 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008238 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008239 }
8240 if (scope->se_type == WHILE_SCOPE)
8241 {
8242 loop_label = scope->se_u.se_while.ws_top_label;
8243 break;
8244 }
8245 if (scope->se_type == TRY_SCOPE)
8246 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008247 scope = scope->se_outer;
8248 }
8249
Bram Moolenaarc150c092021-02-13 15:02:46 +01008250 if (try_scopes > 0)
8251 // Inside one or more try/catch blocks we first need to jump to the
8252 // "finally" or "endtry" to cleanup.
8253 generate_TRYCONT(cctx, try_scopes, loop_label);
8254 else
8255 // Jump back to the FOR or WHILE instruction.
8256 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008258 return arg;
8259}
8260
8261/*
8262 * compile "break"
8263 */
8264 static char_u *
8265compile_break(char_u *arg, cctx_T *cctx)
8266{
8267 scope_T *scope = cctx->ctx_scope;
8268 endlabel_T **el;
8269
8270 for (;;)
8271 {
8272 if (scope == NULL)
8273 {
8274 emsg(_(e_break));
8275 return NULL;
8276 }
8277 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8278 break;
8279 scope = scope->se_outer;
8280 }
8281
8282 // Jump to the end of the FOR or WHILE loop.
8283 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008284 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008285 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008286 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008287 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8288 return FAIL;
8289
8290 return arg;
8291}
8292
8293/*
8294 * compile "{" start of block
8295 */
8296 static char_u *
8297compile_block(char_u *arg, cctx_T *cctx)
8298{
8299 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8300 return NULL;
8301 return skipwhite(arg + 1);
8302}
8303
8304/*
8305 * compile end of block: drop one scope
8306 */
8307 static void
8308compile_endblock(cctx_T *cctx)
8309{
8310 scope_T *scope = cctx->ctx_scope;
8311
8312 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008313 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008314 vim_free(scope);
8315}
8316
8317/*
8318 * compile "try"
8319 * Creates a new scope for the try-endtry, pointing to the first catch and
8320 * finally.
8321 * Creates another scope for the "try" block itself.
8322 * TRY instruction sets up exception handling at runtime.
8323 *
8324 * "try"
8325 * TRY -> catch1, -> finally push trystack entry
8326 * ... try block
8327 * "throw {exception}"
8328 * EVAL {exception}
8329 * THROW create exception
8330 * ... try block
8331 * " catch {expr}"
8332 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008333 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008334 * EVAL {expr}
8335 * MATCH
8336 * JUMP nomatch -> catch2
8337 * CATCH remove exception
8338 * ... catch block
8339 * " catch"
8340 * JUMP -> finally
8341 * catch2: CATCH remove exception
8342 * ... catch block
8343 * " finally"
8344 * finally:
8345 * ... finally block
8346 * " endtry"
8347 * ENDTRY pop trystack entry, may rethrow
8348 */
8349 static char_u *
8350compile_try(char_u *arg, cctx_T *cctx)
8351{
8352 garray_T *instr = &cctx->ctx_instr;
8353 scope_T *try_scope;
8354 scope_T *scope;
8355
Bram Moolenaarfa984412021-03-25 22:15:28 +01008356 if (misplaced_cmdmod(cctx))
8357 return NULL;
8358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008359 // scope that holds the jumps that go to catch/finally/endtry
8360 try_scope = new_scope(cctx, TRY_SCOPE);
8361 if (try_scope == NULL)
8362 return NULL;
8363
Bram Moolenaar69f70502021-01-01 16:10:46 +01008364 if (cctx->ctx_skip != SKIP_YES)
8365 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008366 isn_T *isn;
8367
8368 // "try_catch" is set when the first ":catch" is found or when no catch
8369 // is found and ":finally" is found.
8370 // "try_finally" is set when ":finally" is found
8371 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008372 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008373 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8374 return NULL;
8375 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8376 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008377 return NULL;
8378 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008379
8380 // scope for the try block itself
8381 scope = new_scope(cctx, BLOCK_SCOPE);
8382 if (scope == NULL)
8383 return NULL;
8384
8385 return arg;
8386}
8387
8388/*
8389 * compile "catch {expr}"
8390 */
8391 static char_u *
8392compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8393{
8394 scope_T *scope = cctx->ctx_scope;
8395 garray_T *instr = &cctx->ctx_instr;
8396 char_u *p;
8397 isn_T *isn;
8398
Bram Moolenaarfa984412021-03-25 22:15:28 +01008399 if (misplaced_cmdmod(cctx))
8400 return NULL;
8401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008402 // end block scope from :try or :catch
8403 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8404 compile_endblock(cctx);
8405 scope = cctx->ctx_scope;
8406
8407 // Error if not in a :try scope
8408 if (scope == NULL || scope->se_type != TRY_SCOPE)
8409 {
8410 emsg(_(e_catch));
8411 return NULL;
8412 }
8413
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008414 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008415 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008416 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008417 return NULL;
8418 }
8419
Bram Moolenaar69f70502021-01-01 16:10:46 +01008420 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008421 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008422#ifdef FEAT_PROFILE
8423 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008424 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008425 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008426 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008427 .isn_type == ISN_PROF_START)
8428 --instr->ga_len;
8429#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008430 // Jump from end of previous block to :finally or :endtry
8431 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8432 JUMP_ALWAYS, cctx) == FAIL)
8433 return NULL;
8434
8435 // End :try or :catch scope: set value in ISN_TRY instruction
8436 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008437 if (isn->isn_arg.try.try_ref->try_catch == 0)
8438 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008439 if (scope->se_u.se_try.ts_catch_label != 0)
8440 {
8441 // Previous catch without match jumps here
8442 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8443 isn->isn_arg.jump.jump_where = instr->ga_len;
8444 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008445#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008446 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008447 {
8448 // a "throw" that jumps here needs to be counted
8449 generate_instr(cctx, ISN_PROF_END);
8450 // the "catch" is also counted
8451 generate_instr(cctx, ISN_PROF_START);
8452 }
8453#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008454 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008455 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008456 }
8457
8458 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008459 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008460 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008461 scope->se_u.se_try.ts_caught_all = TRUE;
8462 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008463 }
8464 else
8465 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008466 char_u *end;
8467 char_u *pat;
8468 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008469 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008470 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008472 // Push v:exception, push {expr} and MATCH
8473 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8474
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008475 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008476 if (*end != *p)
8477 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008478 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008479 vim_free(tofree);
8480 return FAIL;
8481 }
8482 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008483 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008484 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008485 len = (int)(end - tofree);
8486 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008487 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008488 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008489 if (pat == NULL)
8490 return FAIL;
8491 if (generate_PUSHS(cctx, pat) == FAIL)
8492 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008494 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8495 return NULL;
8496
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008497 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008498 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8499 return NULL;
8500 }
8501
Bram Moolenaar69f70502021-01-01 16:10:46 +01008502 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008503 return NULL;
8504
8505 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8506 return NULL;
8507 return p;
8508}
8509
8510 static char_u *
8511compile_finally(char_u *arg, cctx_T *cctx)
8512{
8513 scope_T *scope = cctx->ctx_scope;
8514 garray_T *instr = &cctx->ctx_instr;
8515 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008516 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008517
Bram Moolenaarfa984412021-03-25 22:15:28 +01008518 if (misplaced_cmdmod(cctx))
8519 return NULL;
8520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008521 // end block scope from :try or :catch
8522 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8523 compile_endblock(cctx);
8524 scope = cctx->ctx_scope;
8525
8526 // Error if not in a :try scope
8527 if (scope == NULL || scope->se_type != TRY_SCOPE)
8528 {
8529 emsg(_(e_finally));
8530 return NULL;
8531 }
8532
8533 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008534 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008535 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008536 {
8537 emsg(_(e_finally_dup));
8538 return NULL;
8539 }
8540
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008541 this_instr = instr->ga_len;
8542#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008543 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008544 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008545 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008546 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008547 // jump to the profile start of the "finally"
8548 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008549
8550 // jump to the profile end above it
8551 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8552 .isn_type == ISN_PROF_END)
8553 --this_instr;
8554 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008555#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008556
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008557 // Fill in the "end" label in jumps at the end of the blocks.
8558 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8559 this_instr, cctx);
8560
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008561 // If there is no :catch then an exception jumps to :finally.
8562 if (isn->isn_arg.try.try_ref->try_catch == 0)
8563 isn->isn_arg.try.try_ref->try_catch = this_instr;
8564 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008565 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008566 {
8567 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008568 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008569 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008570 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008571 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008572 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8573 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008575 // TODO: set index in ts_finally_label jumps
8576
8577 return arg;
8578}
8579
8580 static char_u *
8581compile_endtry(char_u *arg, cctx_T *cctx)
8582{
8583 scope_T *scope = cctx->ctx_scope;
8584 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008585 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008586
Bram Moolenaarfa984412021-03-25 22:15:28 +01008587 if (misplaced_cmdmod(cctx))
8588 return NULL;
8589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008590 // end block scope from :catch or :finally
8591 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8592 compile_endblock(cctx);
8593 scope = cctx->ctx_scope;
8594
8595 // Error if not in a :try scope
8596 if (scope == NULL || scope->se_type != TRY_SCOPE)
8597 {
8598 if (scope == NULL)
8599 emsg(_(e_no_endtry));
8600 else if (scope->se_type == WHILE_SCOPE)
8601 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008602 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008603 emsg(_(e_endfor));
8604 else
8605 emsg(_(e_endif));
8606 return NULL;
8607 }
8608
Bram Moolenaarc150c092021-02-13 15:02:46 +01008609 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008610 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008611 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008612 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8613 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008614 {
8615 emsg(_(e_missing_catch_or_finally));
8616 return NULL;
8617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008618
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008619#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008620 if (cctx->ctx_compile_type == CT_PROFILE
8621 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008622 .isn_type == ISN_PROF_START)
8623 // move the profile start after "endtry" so that it's not counted when
8624 // the exception is rethrown.
8625 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008626#endif
8627
Bram Moolenaar69f70502021-01-01 16:10:46 +01008628 // Fill in the "end" label in jumps at the end of the blocks, if not
8629 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008630 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8631 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008632
Bram Moolenaar69f70502021-01-01 16:10:46 +01008633 if (scope->se_u.se_try.ts_catch_label != 0)
8634 {
8635 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008636 isn_T *isn = ((isn_T *)instr->ga_data)
8637 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008638 isn->isn_arg.jump.jump_where = instr->ga_len;
8639 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008640 }
8641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008642 compile_endblock(cctx);
8643
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008644 if (cctx->ctx_skip != SKIP_YES)
8645 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008646 // End :catch or :finally scope: set instruction index in ISN_TRY
8647 // instruction
8648 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008649 if (cctx->ctx_skip != SKIP_YES
8650 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8651 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008652#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008653 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008654 generate_instr(cctx, ISN_PROF_START);
8655#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008657 return arg;
8658}
8659
8660/*
8661 * compile "throw {expr}"
8662 */
8663 static char_u *
8664compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8665{
8666 char_u *p = skipwhite(arg);
8667
Bram Moolenaara5565e42020-05-09 15:44:01 +02008668 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008669 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008670 if (cctx->ctx_skip == SKIP_YES)
8671 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008672 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008673 return NULL;
8674 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8675 return NULL;
8676
8677 return p;
8678}
8679
Bram Moolenaarc3235272021-07-10 19:42:03 +02008680 static char_u *
8681compile_eval(char_u *arg, cctx_T *cctx)
8682{
8683 char_u *p = arg;
8684 int name_only;
8685 char_u *alias;
8686 long lnum = SOURCING_LNUM;
8687
8688 // find_ex_command() will consider a variable name an expression, assuming
8689 // that something follows on the next line. Check that something actually
8690 // follows, otherwise it's probably a misplaced command.
8691 get_name_len(&p, &alias, FALSE, FALSE);
8692 name_only = ends_excmd2(arg, skipwhite(p));
8693 vim_free(alias);
8694
8695 p = arg;
8696 if (compile_expr0(&p, cctx) == FAIL)
8697 return NULL;
8698
8699 if (name_only && lnum == SOURCING_LNUM)
8700 {
8701 semsg(_(e_expression_without_effect_str), arg);
8702 return NULL;
8703 }
8704
8705 // drop the result
8706 generate_instr_drop(cctx, ISN_DROP, 1);
8707
8708 return skipwhite(p);
8709}
8710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008711/*
8712 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008713 * compile "echomsg expr"
8714 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008715 * compile "execute expr"
8716 */
8717 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008718compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008719{
8720 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008721 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008722 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008723 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008724 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008725 garray_T *stack = &cctx->ctx_type_stack;
8726 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008727
8728 for (;;)
8729 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008730 if (ends_excmd2(prev, p))
8731 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008732 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008733 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008734 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008735
8736 if (cctx->ctx_skip != SKIP_YES)
8737 {
8738 // check for non-void type
8739 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8740 if (type->tt_type == VAR_VOID)
8741 {
8742 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8743 return NULL;
8744 }
8745 }
8746
Bram Moolenaarad39c092020-02-26 18:23:43 +01008747 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008748 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008749 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008750 }
8751
Bram Moolenaare4984292020-12-13 14:19:25 +01008752 if (count > 0)
8753 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008754 long save_lnum = cctx->ctx_lnum;
8755
8756 // Use the line number where the command started.
8757 cctx->ctx_lnum = start_ctx_lnum;
8758
Bram Moolenaare4984292020-12-13 14:19:25 +01008759 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8760 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8761 else if (cmdidx == CMD_execute)
8762 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8763 else if (cmdidx == CMD_echomsg)
8764 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8765 else
8766 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008767
8768 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008770 return p;
8771}
8772
8773/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008774 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008775 * instruction to compute it and return OK.
8776 * Otherwise return FAIL, the caller must deal with any range.
8777 */
8778 static int
8779compile_variable_range(exarg_T *eap, cctx_T *cctx)
8780{
8781 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8782 char_u *p = skipdigits(eap->cmd);
8783
8784 if (p == range_end)
8785 return FAIL;
8786 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8787}
8788
8789/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008790 * :put r
8791 * :put ={expr}
8792 */
8793 static char_u *
8794compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8795{
8796 char_u *line = arg;
8797 linenr_T lnum;
8798 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008799 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008800
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008801 eap->regname = *line;
8802
8803 if (eap->regname == '=')
8804 {
8805 char_u *p = line + 1;
8806
8807 if (compile_expr0(&p, cctx) == FAIL)
8808 return NULL;
8809 line = p;
8810 }
8811 else if (eap->regname != NUL)
8812 ++line;
8813
Bram Moolenaar08597872020-12-10 19:43:40 +01008814 if (compile_variable_range(eap, cctx) == OK)
8815 {
8816 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8817 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008818 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008819 {
8820 // Either no range or a number.
8821 // "errormsg" will not be set because the range is ADDR_LINES.
8822 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008823 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008824 return NULL;
8825 if (eap->addr_count == 0)
8826 lnum = -1;
8827 else
8828 lnum = eap->line2;
8829 if (above)
8830 --lnum;
8831 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008832
8833 generate_PUT(cctx, eap->regname, lnum);
8834 return line;
8835}
8836
8837/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008838 * A command that is not compiled, execute with legacy code.
8839 */
8840 static char_u *
8841compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8842{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008843 char_u *p;
8844 int has_expr = FALSE;
8845 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008846
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008847 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008848 goto theend;
8849
Bram Moolenaare729ce22021-06-06 21:38:09 +02008850 // If there was a prececing command modifier, drop it and include it in the
8851 // EXEC command.
8852 if (cctx->ctx_has_cmdmod)
8853 {
8854 garray_T *instr = &cctx->ctx_instr;
8855 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8856
8857 if (isn->isn_type == ISN_CMDMOD)
8858 {
8859 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8860 ->cmod_filter_regmatch.regprog);
8861 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8862 --instr->ga_len;
8863 cctx->ctx_has_cmdmod = FALSE;
8864 }
8865 }
8866
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008867 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008868 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008869 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008870 int usefilter = FALSE;
8871
8872 has_expr = argt & (EX_XFILE | EX_EXPAND);
8873
8874 // If the command can be followed by a bar, find the bar and truncate
8875 // it, so that the following command can be compiled.
8876 // The '|' is overwritten with a NUL, it is put back below.
8877 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8878 && *eap->arg == '!')
8879 // :w !filter or :r !filter or :r! filter
8880 usefilter = TRUE;
8881 if ((argt & EX_TRLBAR) && !usefilter)
8882 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008883 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008884 separate_nextcmd(eap);
8885 if (eap->nextcmd != NULL)
8886 nextcmd = eap->nextcmd;
8887 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008888 else if (eap->cmdidx == CMD_wincmd)
8889 {
8890 p = eap->arg;
8891 if (*p != NUL)
8892 ++p;
8893 if (*p == 'g' || *p == Ctrl_G)
8894 ++p;
8895 p = skipwhite(p);
8896 if (*p == '|')
8897 {
8898 *p = NUL;
8899 nextcmd = p + 1;
8900 }
8901 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008902 }
8903
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008904 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8905 {
8906 // expand filename in "syntax include [@group] filename"
8907 has_expr = TRUE;
8908 eap->arg = skipwhite(eap->arg + 7);
8909 if (*eap->arg == '@')
8910 eap->arg = skiptowhite(eap->arg);
8911 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008912
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008913 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8914 && STRLEN(eap->arg) > 4)
8915 {
8916 int delim = *eap->arg;
8917
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008918 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008919 if (*p == delim)
8920 {
8921 eap->arg = p + 1;
8922 has_expr = TRUE;
8923 }
8924 }
8925
Bram Moolenaarecac5912021-01-05 19:23:28 +01008926 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8927 {
8928 // TODO: should only expand when appropriate for the command
8929 eap->arg = skiptowhite(eap->arg);
8930 has_expr = TRUE;
8931 }
8932
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008933 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008934 {
8935 int count = 0;
8936 char_u *start = skipwhite(line);
8937
8938 // :cmd xxx`=expr1`yyy`=expr2`zzz
8939 // PUSHS ":cmd xxx"
8940 // eval expr1
8941 // PUSHS "yyy"
8942 // eval expr2
8943 // PUSHS "zzz"
8944 // EXECCONCAT 5
8945 for (;;)
8946 {
8947 if (p > start)
8948 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008949 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008950 ++count;
8951 }
8952 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008953 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008954 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008955 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008956 ++count;
8957 p = skipwhite(p);
8958 if (*p != '`')
8959 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008960 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008961 return NULL;
8962 }
8963 start = p + 1;
8964
8965 p = (char_u *)strstr((char *)start, "`=");
8966 if (p == NULL)
8967 {
8968 if (*skipwhite(start) != NUL)
8969 {
8970 generate_PUSHS(cctx, vim_strsave(start));
8971 ++count;
8972 }
8973 break;
8974 }
8975 }
8976 generate_EXECCONCAT(cctx, count);
8977 }
8978 else
8979 generate_EXEC(cctx, line);
8980
8981theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008982 if (*nextcmd != NUL)
8983 {
8984 // the parser expects a pointer to the bar, put it back
8985 --nextcmd;
8986 *nextcmd = '|';
8987 }
8988
8989 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008990}
8991
Bram Moolenaar20677332021-06-06 17:02:53 +02008992/*
8993 * A script command with heredoc, e.g.
8994 * ruby << EOF
8995 * command
8996 * EOF
8997 * Has been turned into one long line with NL characters by
8998 * get_function_body():
8999 * ruby << EOF<NL> command<NL>EOF
9000 */
9001 static char_u *
9002compile_script(char_u *line, cctx_T *cctx)
9003{
9004 if (cctx->ctx_skip != SKIP_YES)
9005 {
9006 isn_T *isn;
9007
9008 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9009 return NULL;
9010 isn->isn_arg.string = vim_strsave(line);
9011 }
9012 return (char_u *)"";
9013}
9014
Bram Moolenaar8238f082021-04-20 21:10:48 +02009015
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009016/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009017 * :s/pat/repl/
9018 */
9019 static char_u *
9020compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9021{
9022 char_u *cmd = eap->arg;
9023 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9024
9025 if (expr != NULL)
9026 {
9027 int delimiter = *cmd++;
9028
9029 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009030 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009031 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9032 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009033 garray_T save_ga = cctx->ctx_instr;
9034 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009035 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009036 int trailing_error;
9037 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009038 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009039 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009040
9041 cmd += 3;
9042 end = skip_substitute(cmd, delimiter);
9043
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009044 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009045 // labels are correct.
9046 cctx->ctx_instr.ga_len = 0;
9047 cctx->ctx_instr.ga_maxlen = 0;
9048 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009049 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009050 if (end[-1] == NUL)
9051 end[-1] = delimiter;
9052 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009053 trailing_error = *cmd != delimiter && *cmd != NUL;
9054
Bram Moolenaar169502f2021-04-21 12:19:35 +02009055 if (expr_res == FAIL || trailing_error
9056 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02009057 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009058 if (trailing_error)
9059 semsg(_(e_trailing_arg), cmd);
9060 clear_instr_ga(&cctx->ctx_instr);
9061 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009062 return NULL;
9063 }
9064
Bram Moolenaar8238f082021-04-20 21:10:48 +02009065 // Move the generated instructions into the ISN_SUBSTITUTE
9066 // instructions, then restore the list of instructions before
9067 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009068 instr_count = cctx->ctx_instr.ga_len;
9069 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009070 instr[instr_count].isn_type = ISN_FINISH;
9071
9072 cctx->ctx_instr = save_ga;
9073 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9074 {
9075 int idx;
9076
9077 for (idx = 0; idx < instr_count; ++idx)
9078 delete_instr(instr + idx);
9079 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009080 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009081 }
9082 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9083 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009084
9085 // skip over flags
9086 if (*end == '&')
9087 ++end;
9088 while (ASCII_ISALPHA(*end) || *end == '#')
9089 ++end;
9090 return end;
9091 }
9092 }
9093
9094 return compile_exec(arg, eap, cctx);
9095}
9096
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009097 static char_u *
9098compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9099{
9100 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009101 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009102
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009103 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009104 {
9105 if (STRNCMP(arg, "END", 3) == 0)
9106 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009107 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009108 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009109 // First load the current variable value.
9110 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9111 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009112 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009113 }
9114
9115 // Gets the redirected text and put it on the stack, then store it
9116 // in the variable.
9117 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9118
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009119 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009120 generate_instr_drop(cctx, ISN_CONCAT, 1);
9121
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009122 if (lhs->lhs_has_index)
9123 {
9124 // Use the info in "lhs" to store the value at the index in the
9125 // list or dict.
9126 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9127 &t_string, cctx) == FAIL)
9128 return NULL;
9129 }
9130 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009131 return NULL;
9132
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009133 VIM_CLEAR(lhs->lhs_name);
9134 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009135 return arg + 3;
9136 }
9137 emsg(_(e_cannot_nest_redir));
9138 return NULL;
9139 }
9140
9141 if (arg[0] == '=' && arg[1] == '>')
9142 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009143 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009144
9145 // redirect to a variable is compiled
9146 arg += 2;
9147 if (*arg == '>')
9148 {
9149 ++arg;
9150 append = TRUE;
9151 }
9152 arg = skipwhite(arg);
9153
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009154 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009155 FALSE, FALSE, 1, cctx) == FAIL)
9156 return NULL;
9157 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009158 lhs->lhs_append = append;
9159 if (lhs->lhs_has_index)
9160 {
9161 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9162 if (lhs->lhs_whole == NULL)
9163 return NULL;
9164 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009165
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009166 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009167 }
9168
9169 // other redirects are handled like at script level
9170 return compile_exec(line, eap, cctx);
9171}
9172
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009173#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009174 static char_u *
9175compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9176{
9177 isn_T *isn;
9178 char_u *p;
9179
9180 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9181 if (isn == NULL)
9182 return NULL;
9183 isn->isn_arg.number = eap->cmdidx;
9184
9185 p = eap->arg;
9186 if (compile_expr0(&p, cctx) == FAIL)
9187 return NULL;
9188
9189 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9190 if (isn == NULL)
9191 return NULL;
9192 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9193 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9194 return NULL;
9195 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9196 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9197 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9198
9199 return p;
9200}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009201#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009202
Bram Moolenaar4c137212021-04-19 16:48:48 +02009203/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009204 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009205 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009206 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009207 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009208add_def_function(ufunc_T *ufunc)
9209{
9210 dfunc_T *dfunc;
9211
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009212 if (def_functions.ga_len == 0)
9213 {
9214 // The first position is not used, so that a zero uf_dfunc_idx means it
9215 // wasn't set.
9216 if (ga_grow(&def_functions, 1) == FAIL)
9217 return FAIL;
9218 ++def_functions.ga_len;
9219 }
9220
Bram Moolenaar09689a02020-05-09 22:50:08 +02009221 // Add the function to "def_functions".
9222 if (ga_grow(&def_functions, 1) == FAIL)
9223 return FAIL;
9224 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9225 CLEAR_POINTER(dfunc);
9226 dfunc->df_idx = def_functions.ga_len;
9227 ufunc->uf_dfunc_idx = dfunc->df_idx;
9228 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009229 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009230 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009231 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009232 ++def_functions.ga_len;
9233 return OK;
9234}
9235
9236/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009237 * After ex_function() has collected all the function lines: parse and compile
9238 * the lines into instructions.
9239 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009240 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9241 * the return statement (used for lambda). When uf_ret_type is already set
9242 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009243 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009244 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009245 * This can be used recursively through compile_lambda(), which may reallocate
9246 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009247 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009248 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009249 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009250compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009251 ufunc_T *ufunc,
9252 int check_return_type,
9253 compiletype_T compile_type,
9254 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009255{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009256 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009257 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009258 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009259 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009260 cctx_T cctx;
9261 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009262 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009263 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009264 int ret = FAIL;
9265 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009266 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009267 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009268 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009269 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009270#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009271 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009272#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009273 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009274
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009275 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009276 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009277 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009278 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009279 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9280 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009281 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009282
9283 switch (compile_type)
9284 {
9285 case CT_PROFILE:
9286#ifdef FEAT_PROFILE
9287 instr_dest = dfunc->df_instr_prof; break;
9288#endif
9289 case CT_NONE: instr_dest = dfunc->df_instr; break;
9290 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9291 }
9292 if (instr_dest != NULL)
9293 // Was compiled in this mode before: Free old instructions.
9294 delete_def_function_contents(dfunc, FALSE);
9295 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009296 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009297 else
9298 {
9299 if (add_def_function(ufunc) == FAIL)
9300 return FAIL;
9301 new_def_function = TRUE;
9302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009303
Bram Moolenaar985116a2020-07-12 17:31:09 +02009304 ufunc->uf_def_status = UF_COMPILING;
9305
Bram Moolenaara80faa82020-04-12 19:37:17 +02009306 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009307
Bram Moolenaare99d4222021-06-13 14:01:26 +02009308 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009309 cctx.ctx_ufunc = ufunc;
9310 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009311 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009312 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9313 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9314 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9315 cctx.ctx_type_list = &ufunc->uf_type_list;
9316 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9317 instr = &cctx.ctx_instr;
9318
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009319 // Set the context to the function, it may be compiled when called from
9320 // another script. Set the script version to the most modern one.
9321 // The line number will be set in next_line_from_context().
9322 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009323 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9324
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009325 // Don't use the flag from ":legacy" here.
9326 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9327
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009328 // Make sure error messages are OK.
9329 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9330 if (do_estack_push)
9331 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009332 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009333
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009334 if (ufunc->uf_def_args.ga_len > 0)
9335 {
9336 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009337 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009338 int i;
9339 char_u *arg;
9340 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009341 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009342
9343 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009344 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009345 for (i = 0; i < count; ++i)
9346 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009347 garray_T *stack = &cctx.ctx_type_stack;
9348 type_T *val_type;
9349 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009350 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009351 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009352 int jump_instr_idx = instr->ga_len;
9353 isn_T *isn;
9354
9355 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9356 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9357 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009358
9359 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009360 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009361
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009362 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009363 r = compile_expr0(&arg, &cctx);
9364
Bram Moolenaar12bce952021-03-11 20:04:04 +01009365 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009366 goto erret;
9367
9368 // If no type specified use the type of the default value.
9369 // Otherwise check that the default value type matches the
9370 // specified type.
9371 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009372 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009373 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009374 {
9375 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009376 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009377 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009378 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009379 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009380 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009381
9382 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009383 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009384
9385 // set instruction index in JUMP_IF_ARG_SET to here
9386 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9387 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009388 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009389
9390 if (did_set_arg_type)
9391 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009392 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009393 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009394
9395 /*
9396 * Loop over all the lines of the function and generate instructions.
9397 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009398 for (;;)
9399 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009400 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009401 int starts_with_colon = FALSE;
9402 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009403 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009404
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009405 // Bail out on the first error to avoid a flood of errors and report
9406 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009407 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009408 goto erret;
9409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009410 if (line != NULL && *line == '|')
9411 // the line continues after a '|'
9412 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009413 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009414 && !(*line == '#' && (line == cctx.ctx_line_start
9415 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009416 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009417 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009418 goto erret;
9419 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009420 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9421 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009422 else
9423 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009424 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009425 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009426 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009427 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009428#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009429 if (cctx.ctx_skip != SKIP_YES)
9430 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009431#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009432 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009433 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009434 // Make a copy, splitting off nextcmd and removing trailing spaces
9435 // may change it.
9436 if (line != NULL)
9437 {
9438 line = vim_strsave(line);
9439 vim_free(line_to_free);
9440 line_to_free = line;
9441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009442 }
9443
Bram Moolenaara80faa82020-04-12 19:37:17 +02009444 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009445 ea.cmdlinep = &line;
9446 ea.cmd = skipwhite(line);
9447
Bram Moolenaarb2049902021-01-24 12:53:53 +01009448 if (*ea.cmd == '#')
9449 {
9450 // "#" starts a comment
9451 line = (char_u *)"";
9452 continue;
9453 }
9454
Bram Moolenaarf002a412021-01-24 13:34:18 +01009455#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009456 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9457 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009458 {
9459 may_generate_prof_end(&cctx, prof_lnum);
9460
9461 prof_lnum = cctx.ctx_lnum;
9462 generate_instr(&cctx, ISN_PROF_START);
9463 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009464#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009465 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9466 && cctx.ctx_skip != SKIP_YES)
9467 {
9468 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009469 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009470 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009471 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009472
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009473 // Some things can be recognized by the first character.
9474 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009475 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009476 case '}':
9477 {
9478 // "}" ends a block scope
9479 scopetype_T stype = cctx.ctx_scope == NULL
9480 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009481
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009482 if (stype == BLOCK_SCOPE)
9483 {
9484 compile_endblock(&cctx);
9485 line = ea.cmd;
9486 }
9487 else
9488 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009489 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009490 goto erret;
9491 }
9492 if (line != NULL)
9493 line = skipwhite(ea.cmd + 1);
9494 continue;
9495 }
9496
9497 case '{':
9498 // "{" starts a block scope
9499 // "{'a': 1}->func() is something else
9500 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9501 {
9502 line = compile_block(ea.cmd, &cctx);
9503 continue;
9504 }
9505 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009506 }
9507
9508 /*
9509 * COMMAND MODIFIERS
9510 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009511 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009512 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9513 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009514 {
9515 if (errormsg != NULL)
9516 goto erret;
9517 // empty line or comment
9518 line = (char_u *)"";
9519 continue;
9520 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009521 generate_cmdmods(&cctx, &local_cmdmod);
9522 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009523
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009524 // Check if there was a colon after the last command modifier or before
9525 // the current position.
9526 for (p = ea.cmd; p >= line; --p)
9527 {
9528 if (*p == ':')
9529 starts_with_colon = TRUE;
9530 if (p < ea.cmd && !VIM_ISWHITE(*p))
9531 break;
9532 }
9533
Bram Moolenaarce024c32021-06-26 13:00:49 +02009534 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009535 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009536 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009537 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009538 if (checkforcmd(&ea.cmd, "call", 3))
9539 {
9540 if (*ea.cmd == '(')
9541 // not for "call()"
9542 ea.cmd = p;
9543 else
9544 ea.cmd = skipwhite(ea.cmd);
9545 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009546
Bram Moolenaarce024c32021-06-26 13:00:49 +02009547 if (!starts_with_colon)
9548 {
9549 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009550
Bram Moolenaarce024c32021-06-26 13:00:49 +02009551 // Check for assignment after command modifiers.
9552 assign = may_compile_assignment(&ea, &line, &cctx);
9553 if (assign == OK)
9554 goto nextline;
9555 if (assign == FAIL)
9556 goto erret;
9557 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009558 }
9559
9560 /*
9561 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009562 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009563 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009564 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009565 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009566 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9567 && (starts_with_colon || !(*cmd == '\''
9568 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009569 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009570 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009571 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009572 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009573 if (!starts_with_colon)
9574 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009575 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009576 goto erret;
9577 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009578 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009579 if (ends_excmd2(line, ea.cmd))
9580 {
9581 // A range without a command: jump to the line.
9582 // TODO: compile to a more efficient command, possibly
9583 // calling parse_cmd_address().
9584 ea.cmdidx = CMD_SIZE;
9585 line = compile_exec(line, &ea, &cctx);
9586 goto nextline;
9587 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009588 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009589 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009590 p = find_ex_command(&ea, NULL,
9591 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009592 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009593
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009594 if (p == NULL)
9595 {
9596 if (cctx.ctx_skip != SKIP_YES)
9597 emsg(_(e_ambiguous_use_of_user_defined_command));
9598 goto erret;
9599 }
9600
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009601 // When using ":legacy cmd" always use compile_exec().
9602 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009603 {
9604 char_u *start = ea.cmd;
9605
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009606 switch (ea.cmdidx)
9607 {
9608 case CMD_if:
9609 case CMD_elseif:
9610 case CMD_else:
9611 case CMD_endif:
9612 case CMD_for:
9613 case CMD_endfor:
9614 case CMD_continue:
9615 case CMD_break:
9616 case CMD_while:
9617 case CMD_endwhile:
9618 case CMD_try:
9619 case CMD_catch:
9620 case CMD_finally:
9621 case CMD_endtry:
9622 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9623 goto erret;
9624 default: break;
9625 }
9626
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009627 // ":legacy return expr" needs to be handled differently.
9628 if (checkforcmd(&start, "return", 4))
9629 ea.cmdidx = CMD_return;
9630 else
9631 ea.cmdidx = CMD_legacy;
9632 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009634 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9635 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009636 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009637 {
9638 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009639 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009640 }
9641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009642 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009643 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009644 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009645 // CMD_var cannot happen, compile_assignment() above would be
9646 // used. Most likely an assignment to a non-existing variable.
9647 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009648 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009650 }
9651
Bram Moolenaar3988f642020-08-27 22:43:03 +02009652 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009653 && ea.cmdidx != CMD_elseif
9654 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009655 && ea.cmdidx != CMD_endif
9656 && ea.cmdidx != CMD_endfor
9657 && ea.cmdidx != CMD_endwhile
9658 && ea.cmdidx != CMD_catch
9659 && ea.cmdidx != CMD_finally
9660 && ea.cmdidx != CMD_endtry)
9661 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009662 emsg(_(e_unreachable_code_after_return));
9663 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009664 }
9665
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009666 p = skipwhite(p);
9667 if (ea.cmdidx != CMD_SIZE
9668 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9669 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009670 if (ea.cmdidx >= 0)
9671 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009672 if ((ea.argt & EX_BANG) && *p == '!')
9673 {
9674 ea.forceit = TRUE;
9675 p = skipwhite(p + 1);
9676 }
9677 }
9678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009679 switch (ea.cmdidx)
9680 {
9681 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009682 ea.arg = p;
9683 line = compile_nested_function(&ea, &cctx);
9684 break;
9685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009686 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009687 // TODO: should we allow this, e.g. to declare a global
9688 // function?
9689 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009690 goto erret;
9691
9692 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009693 line = compile_return(p, check_return_type,
9694 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009695 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009696 break;
9697
9698 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009699 emsg(_(e_cannot_use_let_in_vim9_script));
9700 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009701 case CMD_var:
9702 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009703 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009704 case CMD_increment:
9705 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009706 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009707 if (line == p)
9708 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009709 break;
9710
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009711 case CMD_unlet:
9712 case CMD_unlockvar:
9713 case CMD_lockvar:
9714 line = compile_unletlock(p, &ea, &cctx);
9715 break;
9716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009717 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009718 emsg(_(e_import_can_only_be_used_in_script));
9719 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009720 break;
9721
9722 case CMD_if:
9723 line = compile_if(p, &cctx);
9724 break;
9725 case CMD_elseif:
9726 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009727 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009728 break;
9729 case CMD_else:
9730 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009731 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009732 break;
9733 case CMD_endif:
9734 line = compile_endif(p, &cctx);
9735 break;
9736
9737 case CMD_while:
9738 line = compile_while(p, &cctx);
9739 break;
9740 case CMD_endwhile:
9741 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009742 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009743 break;
9744
9745 case CMD_for:
9746 line = compile_for(p, &cctx);
9747 break;
9748 case CMD_endfor:
9749 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009750 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009751 break;
9752 case CMD_continue:
9753 line = compile_continue(p, &cctx);
9754 break;
9755 case CMD_break:
9756 line = compile_break(p, &cctx);
9757 break;
9758
9759 case CMD_try:
9760 line = compile_try(p, &cctx);
9761 break;
9762 case CMD_catch:
9763 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009764 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009765 break;
9766 case CMD_finally:
9767 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009768 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009769 break;
9770 case CMD_endtry:
9771 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009772 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009773 break;
9774 case CMD_throw:
9775 line = compile_throw(p, &cctx);
9776 break;
9777
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009778 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009779 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009780 break;
9781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009782 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009783 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009784 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009785 case CMD_echomsg:
9786 case CMD_echoerr:
9787 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009788 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009789
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009790 case CMD_put:
9791 ea.cmd = cmd;
9792 line = compile_put(p, &ea, &cctx);
9793 break;
9794
Bram Moolenaar4c137212021-04-19 16:48:48 +02009795 case CMD_substitute:
9796 if (cctx.ctx_skip == SKIP_YES)
9797 line = (char_u *)"";
9798 else
9799 {
9800 ea.arg = p;
9801 line = compile_substitute(line, &ea, &cctx);
9802 }
9803 break;
9804
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009805 case CMD_redir:
9806 ea.arg = p;
9807 line = compile_redir(line, &ea, &cctx);
9808 break;
9809
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009810 case CMD_cexpr:
9811 case CMD_lexpr:
9812 case CMD_caddexpr:
9813 case CMD_laddexpr:
9814 case CMD_cgetexpr:
9815 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009816#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009817 ea.arg = p;
9818 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009819#else
9820 ex_ni(&ea);
9821 line = NULL;
9822#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009823 break;
9824
Bram Moolenaar3988f642020-08-27 22:43:03 +02009825 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009826
Bram Moolenaarae616492020-07-28 20:07:27 +02009827 case CMD_append:
9828 case CMD_change:
9829 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009830 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009831 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009832 case CMD_xit:
9833 not_in_vim9(&ea);
9834 goto erret;
9835
Bram Moolenaar002262f2020-07-08 17:47:57 +02009836 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009837 if (cctx.ctx_skip != SKIP_YES)
9838 {
9839 semsg(_(e_invalid_command_str), ea.cmd);
9840 goto erret;
9841 }
9842 // We don't check for a next command here.
9843 line = (char_u *)"";
9844 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009845
Bram Moolenaar20677332021-06-06 17:02:53 +02009846 case CMD_lua:
9847 case CMD_mzscheme:
9848 case CMD_perl:
9849 case CMD_py3:
9850 case CMD_python3:
9851 case CMD_python:
9852 case CMD_pythonx:
9853 case CMD_ruby:
9854 case CMD_tcl:
9855 ea.arg = p;
9856 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009857 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009858 else
9859 // heredoc lines have been concatenated with NL
9860 // characters in get_function_body()
9861 line = compile_script(line, &cctx);
9862 break;
9863
9864 default:
9865 // Not recognized, execute with do_cmdline_cmd().
9866 ea.arg = p;
9867 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009868 break;
9869 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009870nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009871 if (line == NULL)
9872 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009873 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009874
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009875 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009876 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009878 if (cctx.ctx_type_stack.ga_len < 0)
9879 {
9880 iemsg("Type stack underflow");
9881 goto erret;
9882 }
9883 }
9884
9885 if (cctx.ctx_scope != NULL)
9886 {
9887 if (cctx.ctx_scope->se_type == IF_SCOPE)
9888 emsg(_(e_endif));
9889 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9890 emsg(_(e_endwhile));
9891 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9892 emsg(_(e_endfor));
9893 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009894 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009895 goto erret;
9896 }
9897
Bram Moolenaarefd88552020-06-18 20:50:10 +02009898 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009899 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009900 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9901 ufunc->uf_ret_type = &t_void;
9902 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009903 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009904 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009905 goto erret;
9906 }
9907
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009908 // Return void if there is no return at the end.
9909 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009910 }
9911
Bram Moolenaar599410c2021-04-10 14:03:43 +02009912 // When compiled with ":silent!" and there was an error don't consider the
9913 // function compiled.
9914 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009915 {
9916 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9917 + ufunc->uf_dfunc_idx;
9918 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009919 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009920#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009921 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009922 {
9923 dfunc->df_instr_prof = instr->ga_data;
9924 dfunc->df_instr_prof_count = instr->ga_len;
9925 }
9926 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009927#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009928 if (cctx.ctx_compile_type == CT_DEBUG)
9929 {
9930 dfunc->df_instr_debug = instr->ga_data;
9931 dfunc->df_instr_debug_count = instr->ga_len;
9932 }
9933 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009934 {
9935 dfunc->df_instr = instr->ga_data;
9936 dfunc->df_instr_count = instr->ga_len;
9937 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009938 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009939 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009940 if (cctx.ctx_outer_used)
9941 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009942 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009944
9945 ret = OK;
9946
9947erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009948 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009949 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009950 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9951 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009952
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009953 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009954 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009955 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009956 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009957
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009958 // If using the last entry in the table and it was added above, we
9959 // might as well remove it.
9960 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009961 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009962 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009963 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009964 ufunc->uf_dfunc_idx = 0;
9965 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009966 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009967
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009968 while (cctx.ctx_scope != NULL)
9969 drop_scope(&cctx);
9970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009971 if (errormsg != NULL)
9972 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009973 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009974 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009975 }
9976
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009977 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9978 {
9979 if (ret == OK)
9980 {
9981 emsg(_(e_missing_redir_end));
9982 ret = FAIL;
9983 }
9984 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009985 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009986 }
9987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009988 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009989 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009990 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009991 if (do_estack_push)
9992 estack_pop();
9993
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009994 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009995 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009996 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009997 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009998 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009999}
10000
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010001 void
10002set_function_type(ufunc_T *ufunc)
10003{
10004 int varargs = ufunc->uf_va_name != NULL;
10005 int argcount = ufunc->uf_args.ga_len;
10006
10007 // Create a type for the function, with the return type and any
10008 // argument types.
10009 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10010 // The type is included in "tt_args".
10011 if (argcount > 0 || varargs)
10012 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010013 if (ufunc->uf_type_list.ga_itemsize == 0)
10014 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010015 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10016 argcount, &ufunc->uf_type_list);
10017 // Add argument types to the function type.
10018 if (func_type_add_arg_types(ufunc->uf_func_type,
10019 argcount + varargs,
10020 &ufunc->uf_type_list) == FAIL)
10021 return;
10022 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10023 ufunc->uf_func_type->tt_min_argcount =
10024 argcount - ufunc->uf_def_args.ga_len;
10025 if (ufunc->uf_arg_types == NULL)
10026 {
10027 int i;
10028
10029 // lambda does not have argument types.
10030 for (i = 0; i < argcount; ++i)
10031 ufunc->uf_func_type->tt_args[i] = &t_any;
10032 }
10033 else
10034 mch_memmove(ufunc->uf_func_type->tt_args,
10035 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10036 if (varargs)
10037 {
10038 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010039 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010040 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10041 }
10042 }
10043 else
10044 // No arguments, can use a predefined type.
10045 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10046 argcount, &ufunc->uf_type_list);
10047}
10048
10049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010050/*
10051 * Delete an instruction, free what it contains.
10052 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010053 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010054delete_instr(isn_T *isn)
10055{
10056 switch (isn->isn_type)
10057 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010058 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010059 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010060 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010061 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010062 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010063 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010064 case ISN_LOADENV:
10065 case ISN_LOADG:
10066 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010067 case ISN_LOADT:
10068 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010069 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010070 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010071 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010072 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010073 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010074 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010075 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010076 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010077 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010078 case ISN_STOREW:
10079 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010080 vim_free(isn->isn_arg.string);
10081 break;
10082
Bram Moolenaar4c137212021-04-19 16:48:48 +020010083 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010084 {
10085 int idx;
10086 isn_T *list = isn->isn_arg.subs.subs_instr;
10087
10088 vim_free(isn->isn_arg.subs.subs_cmd);
10089 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10090 delete_instr(list + idx);
10091 vim_free(list);
10092 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010093 break;
10094
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010095 case ISN_INSTR:
10096 {
10097 int idx;
10098 isn_T *list = isn->isn_arg.instr;
10099
10100 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10101 delete_instr(list + idx);
10102 vim_free(list);
10103 }
10104 break;
10105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010106 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010107 case ISN_STORES:
10108 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010109 break;
10110
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010111 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010112 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010113 vim_free(isn->isn_arg.unlet.ul_name);
10114 break;
10115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010116 case ISN_STOREOPT:
10117 vim_free(isn->isn_arg.storeopt.so_name);
10118 break;
10119
10120 case ISN_PUSHBLOB: // push blob isn_arg.blob
10121 blob_unref(isn->isn_arg.blob);
10122 break;
10123
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010124 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010125#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010126 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010127#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010128 break;
10129
10130 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010131#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010132 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010133#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010134 break;
10135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010136 case ISN_UCALL:
10137 vim_free(isn->isn_arg.ufunc.cuf_name);
10138 break;
10139
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010140 case ISN_FUNCREF:
10141 {
10142 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10143 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010144 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010145
Bram Moolenaar077a4232020-12-22 18:33:27 +010010146 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10147 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010148 }
10149 break;
10150
10151 case ISN_DCALL:
10152 {
10153 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10154 + isn->isn_arg.dfunc.cdf_idx;
10155
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010156 if (dfunc->df_ufunc != NULL
10157 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010158 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010159 }
10160 break;
10161
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010162 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010163 {
10164 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10165 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10166
10167 if (ufunc != NULL)
10168 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010169 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010170 func_ptr_unref(ufunc);
10171 }
10172
10173 vim_free(lambda);
10174 vim_free(isn->isn_arg.newfunc.nf_global);
10175 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010176 break;
10177
Bram Moolenaar5e654232020-09-16 15:22:00 +020010178 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010179 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010180 free_type(isn->isn_arg.type.ct_type);
10181 break;
10182
Bram Moolenaar02194d22020-10-24 23:08:38 +020010183 case ISN_CMDMOD:
10184 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10185 ->cmod_filter_regmatch.regprog);
10186 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10187 break;
10188
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010189 case ISN_LOADSCRIPT:
10190 case ISN_STORESCRIPT:
10191 vim_free(isn->isn_arg.script.scriptref);
10192 break;
10193
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010194 case ISN_TRY:
10195 vim_free(isn->isn_arg.try.try_ref);
10196 break;
10197
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010198 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010199 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010200 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10201 break;
10202
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010203 case ISN_2BOOL:
10204 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010205 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010206 case ISN_ADDBLOB:
10207 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010208 case ISN_ANYINDEX:
10209 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010210 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010211 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010212 case ISN_BLOBINDEX:
10213 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010214 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010215 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010216 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010217 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010218 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010219 case ISN_COMPAREANY:
10220 case ISN_COMPAREBLOB:
10221 case ISN_COMPAREBOOL:
10222 case ISN_COMPAREDICT:
10223 case ISN_COMPAREFLOAT:
10224 case ISN_COMPAREFUNC:
10225 case ISN_COMPARELIST:
10226 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010227 case ISN_COMPARESPECIAL:
10228 case ISN_COMPARESTRING:
10229 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010230 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010231 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010232 case ISN_DROP:
10233 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010234 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010235 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010236 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010237 case ISN_EXECCONCAT:
10238 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010239 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010240 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010241 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010242 case ISN_GETITEM:
10243 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010244 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010245 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010246 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010247 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010248 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010249 case ISN_LOADBDICT:
10250 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010251 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010252 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010253 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010254 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010255 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010256 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010257 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010258 case ISN_NEGATENR:
10259 case ISN_NEWDICT:
10260 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010261 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010262 case ISN_OPFLOAT:
10263 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010264 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010265 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010266 case ISN_PROF_END:
10267 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010268 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010269 case ISN_PUSHF:
10270 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010271 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010272 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010273 case ISN_REDIREND:
10274 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010275 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010276 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010277 case ISN_SHUFFLE:
10278 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010279 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010280 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010281 case ISN_STORENR:
10282 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010283 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010284 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010285 case ISN_STOREV:
10286 case ISN_STRINDEX:
10287 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010288 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010289 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010290 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010291 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010292 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010293 // nothing allocated
10294 break;
10295 }
10296}
10297
10298/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010299 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010300 */
10301 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010302delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010303{
10304 int idx;
10305
10306 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010307 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010308
10309 if (dfunc->df_instr != NULL)
10310 {
10311 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10312 delete_instr(dfunc->df_instr + idx);
10313 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010314 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010315 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010316 if (dfunc->df_instr_debug != NULL)
10317 {
10318 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10319 delete_instr(dfunc->df_instr_debug + idx);
10320 VIM_CLEAR(dfunc->df_instr_debug);
10321 dfunc->df_instr_debug = NULL;
10322 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010323#ifdef FEAT_PROFILE
10324 if (dfunc->df_instr_prof != NULL)
10325 {
10326 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10327 delete_instr(dfunc->df_instr_prof + idx);
10328 VIM_CLEAR(dfunc->df_instr_prof);
10329 dfunc->df_instr_prof = NULL;
10330 }
10331#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010332
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010333 if (mark_deleted)
10334 dfunc->df_deleted = TRUE;
10335 if (dfunc->df_ufunc != NULL)
10336 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010337}
10338
10339/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010340 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010341 * function, unless another user function still uses it.
10342 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010343 */
10344 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010345unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010346{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010347 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010348 {
10349 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10350 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010351
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010352 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010353 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010354 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010355 ufunc->uf_dfunc_idx = 0;
10356 if (dfunc->df_ufunc == ufunc)
10357 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010358 }
10359}
10360
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010361/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010362 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010363 */
10364 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010365link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010366{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010367 if (ufunc->uf_dfunc_idx > 0)
10368 {
10369 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10370 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010371
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010372 ++dfunc->df_refcount;
10373 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010374}
10375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010376#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010377/*
10378 * Free all functions defined with ":def".
10379 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010380 void
10381free_def_functions(void)
10382{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010383 int idx;
10384
10385 for (idx = 0; idx < def_functions.ga_len; ++idx)
10386 {
10387 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10388
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010389 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010390 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010391 }
10392
10393 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010394}
10395#endif
10396
10397
10398#endif // FEAT_EVAL