blob: 9aa11f6210af10edb186f7987045e968b761783f [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;
342 ufunc_T *ufunc;
343
344 // Find the list of all script variables with the right name.
345 if (len > 0)
346 {
347 cc = name[len];
348 name[len] = NUL;
349 }
350 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
351 if (len > 0)
352 name[len] = cc;
353 if (HASHITEM_EMPTY(hi))
354 return NULL;
355
356 sav = HI2SAV(hi);
357 if (sav->sav_block_id == 0 || cctx == NULL)
358 // variable defined in the script scope or not in a function.
359 return sav;
360
361 // Go over the variables with this name and find one that was visible
362 // from the function.
363 ufunc = cctx->ctx_ufunc;
364 while (sav != NULL)
365 {
366 int idx;
367
368 // Go over the blocks that this function was defined in. If the
369 // variable block ID matches it was visible to the function.
370 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
371 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
372 return sav;
373 sav = sav->sav_next;
374 }
375
376 return NULL;
377}
378
379/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100380 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200381 */
382 static int
383script_is_vim9()
384{
385 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
386}
387
388/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200389 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200390 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 * Returns OK or FAIL.
392 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200393 static int
394script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200396 if (current_sctx.sc_sid <= 0)
397 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200398 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200399 {
400 // Check script variables that were visible where the function was
401 // defined.
402 if (find_script_var(name, len, cctx) != NULL)
403 return OK;
404 }
405 else
406 {
407 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
408 dictitem_T *di;
409 int cc;
410
411 // Check script variables that are currently visible
412 cc = name[len];
413 name[len] = NUL;
414 di = find_var_in_ht(ht, 0, name, TRUE);
415 name[len] = cc;
416 if (di != NULL)
417 return OK;
418 }
419
420 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100421}
422
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100423/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100424 * Return TRUE if "name" is a local variable, argument, script variable or
425 * imported.
426 */
427 static int
428variable_exists(char_u *name, size_t len, cctx_T *cctx)
429{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100430 return (cctx != NULL
431 && (lookup_local(name, len, NULL, cctx) == OK
432 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200433 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100434 || find_imported(name, len, cctx) != NULL;
435}
436
437/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100438 * Return TRUE if "name" is a local variable, argument, script variable,
439 * imported or function.
440 */
441 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100442item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100443{
444 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100445 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100446
447 if (variable_exists(name, len, cctx))
448 return TRUE;
449
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100450 // This is similar to what is in lookup_scriptitem():
451 // Find a function, so that a following "->" works.
452 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
453 // a function call.
454 p = skipwhite(name + len);
455
456 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
457 {
458 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200459 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100460 // Skip "g:" before a function name.
461 is_global = (name[0] == 'g' && name[1] == ':');
462 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
463 }
464 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100465}
466
467/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100468 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200469 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200470 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100471 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100472 * Return FAIL and give an error if it defined.
473 */
474 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100475check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100476{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200477 int c = p[len];
478 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200479
Bram Moolenaarda479c72021-04-10 21:01:38 +0200480 // underscore argument is OK
481 if (len == 1 && *p == '_')
482 return OK;
483
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200484 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100485 {
486 if (is_arg)
487 semsg(_(e_argument_already_declared_in_script_str), p);
488 else
489 semsg(_(e_variable_already_declared_in_script_str), p);
490 return FAIL;
491 }
492
Bram Moolenaarad486a02020-08-01 23:22:18 +0200493 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100494 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100495 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200496 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200497 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200498 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100499 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200500 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200501 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
502 && (!func_is_global(ufunc)
503 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200504 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100505 if (is_arg)
506 semsg(_(e_argument_name_shadows_existing_variable_str), p);
507 else
508 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200509 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200510 return FAIL;
511 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100512 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200513 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100514 return OK;
515}
516
Bram Moolenaar65b95452020-07-19 14:03:09 +0200517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518/////////////////////////////////////////////////////////////////////
519// Following generate_ functions expect the caller to call ga_grow().
520
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200521#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
522#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524/*
525 * Generate an instruction without arguments.
526 * Returns a pointer to the new instruction, NULL if failed.
527 */
528 static isn_T *
529generate_instr(cctx_T *cctx, isntype_T isn_type)
530{
531 garray_T *instr = &cctx->ctx_instr;
532 isn_T *isn;
533
Bram Moolenaar080457c2020-03-03 21:53:32 +0100534 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 if (ga_grow(instr, 1) == FAIL)
536 return NULL;
537 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
538 isn->isn_type = isn_type;
539 isn->isn_lnum = cctx->ctx_lnum + 1;
540 ++instr->ga_len;
541
542 return isn;
543}
544
545/*
546 * Generate an instruction without arguments.
547 * "drop" will be removed from the stack.
548 * Returns a pointer to the new instruction, NULL if failed.
549 */
550 static isn_T *
551generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
552{
553 garray_T *stack = &cctx->ctx_type_stack;
554
Bram Moolenaar080457c2020-03-03 21:53:32 +0100555 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 stack->ga_len -= drop;
557 return generate_instr(cctx, isn_type);
558}
559
560/*
561 * Generate instruction "isn_type" and put "type" on the type stack.
562 */
563 static isn_T *
564generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
565{
566 isn_T *isn;
567 garray_T *stack = &cctx->ctx_type_stack;
568
569 if ((isn = generate_instr(cctx, isn_type)) == NULL)
570 return NULL;
571
572 if (ga_grow(stack, 1) == FAIL)
573 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200574 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 ++stack->ga_len;
576
577 return isn;
578}
579
580/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200581 * Generate an ISN_DEBUG instruction.
582 */
583 static isn_T *
584generate_instr_debug(cctx_T *cctx)
585{
586 isn_T *isn;
587 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
588 + cctx->ctx_ufunc->uf_dfunc_idx;
589
590 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
591 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200592 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
593 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200594 return isn;
595}
596
597/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200599 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200600 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 */
602 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200603may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604{
605 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200606 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100608 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100610 RETURN_OK_IF_SKIP(cctx);
611 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200612 switch ((*type)->tt_type)
613 {
614 // nothing to be done
615 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200617 // conversion possible
618 case VAR_SPECIAL:
619 case VAR_BOOL:
620 case VAR_NUMBER:
621 case VAR_FLOAT:
622 break;
623
624 // conversion possible (with runtime check)
625 case VAR_ANY:
626 case VAR_UNKNOWN:
627 isntype = ISN_2STRING_ANY;
628 break;
629
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200630 // conversion possible when tolerant
631 case VAR_LIST:
632 if (tolerant)
633 {
634 isntype = ISN_2STRING_ANY;
635 break;
636 }
637 // FALLTHROUGH
638
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200639 // conversion not possible
640 case VAR_VOID:
641 case VAR_BLOB:
642 case VAR_FUNC:
643 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200644 case VAR_DICT:
645 case VAR_JOB:
646 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200647 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200648 to_string_error((*type)->tt_type);
649 return FAIL;
650 }
651
652 *type = &t_string;
653 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200655 isn->isn_arg.tostring.offset = offset;
656 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657
658 return OK;
659}
660
661 static int
662check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
663{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200664 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200666 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667 {
668 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200669 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200671 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 return FAIL;
673 }
674 return OK;
675}
676
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200677 static int
678generate_add_instr(
679 cctx_T *cctx,
680 vartype_T vartype,
681 type_T *type1,
682 type_T *type2)
683{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100684 garray_T *stack = &cctx->ctx_type_stack;
685 isn_T *isn = generate_instr_drop(cctx,
686 vartype == VAR_NUMBER ? ISN_OPNR
687 : vartype == VAR_LIST ? ISN_ADDLIST
688 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200689#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100690 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200691#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100692 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200693
694 if (vartype != VAR_LIST && vartype != VAR_BLOB
695 && type1->tt_type != VAR_ANY
696 && type2->tt_type != VAR_ANY
697 && check_number_or_float(
698 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
699 return FAIL;
700
701 if (isn != NULL)
702 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100703
704 // When concatenating two lists with different member types the member type
705 // becomes "any".
706 if (vartype == VAR_LIST
707 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
708 && type1->tt_member != type2->tt_member)
709 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
710
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200711 return isn == NULL ? FAIL : OK;
712}
713
714/*
715 * Get the type to use for an instruction for an operation on "type1" and
716 * "type2". If they are matching use a type-specific instruction. Otherwise
717 * fall back to runtime type checking.
718 */
719 static vartype_T
720operator_type(type_T *type1, type_T *type2)
721{
722 if (type1->tt_type == type2->tt_type
723 && (type1->tt_type == VAR_NUMBER
724 || type1->tt_type == VAR_LIST
725#ifdef FEAT_FLOAT
726 || type1->tt_type == VAR_FLOAT
727#endif
728 || type1->tt_type == VAR_BLOB))
729 return type1->tt_type;
730 return VAR_ANY;
731}
732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733/*
734 * Generate an instruction with two arguments. The instruction depends on the
735 * type of the arguments.
736 */
737 static int
738generate_two_op(cctx_T *cctx, char_u *op)
739{
740 garray_T *stack = &cctx->ctx_type_stack;
741 type_T *type1;
742 type_T *type2;
743 vartype_T vartype;
744 isn_T *isn;
745
Bram Moolenaar080457c2020-03-03 21:53:32 +0100746 RETURN_OK_IF_SKIP(cctx);
747
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200748 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
750 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200751 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752
753 switch (*op)
754 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200755 case '+':
756 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 break;
759
760 case '-':
761 case '*':
762 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
763 op) == FAIL)
764 return FAIL;
765 if (vartype == VAR_NUMBER)
766 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
767#ifdef FEAT_FLOAT
768 else if (vartype == VAR_FLOAT)
769 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
770#endif
771 else
772 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
773 if (isn != NULL)
774 isn->isn_arg.op.op_type = *op == '*'
775 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
776 break;
777
Bram Moolenaar4c683752020-04-05 21:38:23 +0200778 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200780 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 && type2->tt_type != VAR_NUMBER))
782 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200783 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 return FAIL;
785 }
786 isn = generate_instr_drop(cctx,
787 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
788 if (isn != NULL)
789 isn->isn_arg.op.op_type = EXPR_REM;
790 break;
791 }
792
793 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200794 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795 {
796 type_T *type = &t_any;
797
798#ifdef FEAT_FLOAT
799 // float+number and number+float results in float
800 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
801 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
802 type = &t_float;
803#endif
804 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
805 }
806
807 return OK;
808}
809
810/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200811 * Get the instruction to use for comparing "type1" with "type2"
812 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200814 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100815get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816{
817 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818
Bram Moolenaar4c683752020-04-05 21:38:23 +0200819 if (type1 == VAR_UNKNOWN)
820 type1 = VAR_ANY;
821 if (type2 == VAR_UNKNOWN)
822 type2 = VAR_ANY;
823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 if (type1 == type2)
825 {
826 switch (type1)
827 {
828 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
829 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
830 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
831 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
832 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
833 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
834 case VAR_LIST: isntype = ISN_COMPARELIST; break;
835 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
836 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 default: isntype = ISN_COMPAREANY; break;
838 }
839 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200840 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100842 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 isntype = ISN_COMPAREANY;
844
Bram Moolenaar657137c2021-01-09 15:45:23 +0100845 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 && (isntype == ISN_COMPAREBOOL
847 || isntype == ISN_COMPARESPECIAL
848 || isntype == ISN_COMPARENR
849 || isntype == ISN_COMPAREFLOAT))
850 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200851 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100852 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200853 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854 }
855 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100856 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
858 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100859 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
860 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 && (type1 == VAR_BLOB || type2 == VAR_BLOB
862 || type1 == VAR_LIST || type2 == VAR_LIST))))
863 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200864 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200866 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200868 return isntype;
869}
870
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200871 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100872check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200873{
874 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
875 return FAIL;
876 return OK;
877}
878
Bram Moolenaara5565e42020-05-09 15:44:01 +0200879/*
880 * Generate an ISN_COMPARE* instruction with a boolean result.
881 */
882 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100883generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200884{
885 isntype_T isntype;
886 isn_T *isn;
887 garray_T *stack = &cctx->ctx_type_stack;
888 vartype_T type1;
889 vartype_T type2;
890
891 RETURN_OK_IF_SKIP(cctx);
892
893 // Get the known type of the two items on the stack. If they are matching
894 // use a type-specific instruction. Otherwise fall back to runtime type
895 // checking.
896 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
897 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100898 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200899 if (isntype == ISN_DROP)
900 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901
902 if ((isn = generate_instr(cctx, isntype)) == NULL)
903 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100904 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 isn->isn_arg.op.op_ic = ic;
906
907 // takes two arguments, puts one bool back
908 if (stack->ga_len >= 2)
909 {
910 --stack->ga_len;
911 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
912 }
913
914 return OK;
915}
916
917/*
918 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200919 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 */
921 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200922generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923{
924 isn_T *isn;
925 garray_T *stack = &cctx->ctx_type_stack;
926
Bram Moolenaar080457c2020-03-03 21:53:32 +0100927 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
929 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200930 isn->isn_arg.tobool.invert = invert;
931 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932
933 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200934 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935
936 return OK;
937}
938
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200939/*
940 * Generate an ISN_COND2BOOL instruction.
941 */
942 static int
943generate_COND2BOOL(cctx_T *cctx)
944{
945 isn_T *isn;
946 garray_T *stack = &cctx->ctx_type_stack;
947
948 RETURN_OK_IF_SKIP(cctx);
949 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
950 return FAIL;
951
952 // type becomes bool
953 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
954
955 return OK;
956}
957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200959generate_TYPECHECK(
960 cctx_T *cctx,
961 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100962 int offset,
963 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964{
965 isn_T *isn;
966 garray_T *stack = &cctx->ctx_type_stack;
967
Bram Moolenaar080457c2020-03-03 21:53:32 +0100968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
970 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200971 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100972 isn->isn_arg.type.ct_off = (int8_T)offset;
973 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974
Bram Moolenaar5e654232020-09-16 15:22:00 +0200975 // type becomes expected
976 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977
978 return OK;
979}
980
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100981 static int
982generate_SETTYPE(
983 cctx_T *cctx,
984 type_T *expected)
985{
986 isn_T *isn;
987
988 RETURN_OK_IF_SKIP(cctx);
989 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
990 return FAIL;
991 isn->isn_arg.type.ct_type = alloc_type(expected);
992 return OK;
993}
994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200996 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
997 * used. Return FALSE if the types will never match.
998 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100999 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001000use_typecheck(type_T *actual, type_T *expected)
1001{
1002 if (actual->tt_type == VAR_ANY
1003 || actual->tt_type == VAR_UNKNOWN
1004 || (actual->tt_type == VAR_FUNC
1005 && (expected->tt_type == VAR_FUNC
1006 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001007 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1008 && ((actual->tt_member == &t_void)
1009 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001010 return TRUE;
1011 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1012 && actual->tt_type == expected->tt_type)
1013 // This takes care of a nested list or dict.
1014 return use_typecheck(actual->tt_member, expected->tt_member);
1015 return FALSE;
1016}
1017
1018/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001019 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001020 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001021 * - "actual" is a type that can be "expected" type: add a runtime check; or
1022 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001023 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1024 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001025 */
Bram Moolenaar351ead02021-01-16 16:07:01 +01001026 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001027need_type(
1028 type_T *actual,
1029 type_T *expected,
1030 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001031 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001032 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001033 int silent,
1034 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001035{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001036 where_T where;
1037
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001038 if (expected == &t_bool && actual != &t_bool
1039 && (actual->tt_flags & TTFLAG_BOOL_OK))
1040 {
1041 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1042 // boolean is OK but requires a conversion.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001043 generate_2BOOL(cctx, FALSE, offset);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001044 return OK;
1045 }
1046
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001047 where.wt_index = arg_idx;
1048 where.wt_variable = FALSE;
1049 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001050 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001051
1052 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001053 // If it's a constant a runtime check makes no sense.
1054 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001055 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001056 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001057 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001058 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001059
1060 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001061 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001062 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001063}
1064
1065/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001066 * Check that the top of the type stack has a type that can be used as a
1067 * condition. Give an error and return FAIL if not.
1068 */
1069 static int
1070bool_on_stack(cctx_T *cctx)
1071{
1072 garray_T *stack = &cctx->ctx_type_stack;
1073 type_T *type;
1074
1075 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1076 if (type == &t_bool)
1077 return OK;
1078
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001079 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001080 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1081 // This requires a runtime type check.
1082 return generate_COND2BOOL(cctx);
1083
Bram Moolenaar351ead02021-01-16 16:07:01 +01001084 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001085}
1086
1087/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 * Generate an ISN_PUSHNR instruction.
1089 */
1090 static int
1091generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1092{
1093 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001094 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095
Bram Moolenaar080457c2020-03-03 21:53:32 +01001096 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1098 return FAIL;
1099 isn->isn_arg.number = number;
1100
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001101 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001102 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001103 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 return OK;
1105}
1106
1107/*
1108 * Generate an ISN_PUSHBOOL instruction.
1109 */
1110 static int
1111generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1112{
1113 isn_T *isn;
1114
Bram Moolenaar080457c2020-03-03 21:53:32 +01001115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1117 return FAIL;
1118 isn->isn_arg.number = number;
1119
1120 return OK;
1121}
1122
1123/*
1124 * Generate an ISN_PUSHSPEC instruction.
1125 */
1126 static int
1127generate_PUSHSPEC(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_PUSHSPEC, &t_special)) == NULL)
1133 return FAIL;
1134 isn->isn_arg.number = number;
1135
1136 return OK;
1137}
1138
1139#ifdef FEAT_FLOAT
1140/*
1141 * Generate an ISN_PUSHF instruction.
1142 */
1143 static int
1144generate_PUSHF(cctx_T *cctx, float_T fnumber)
1145{
1146 isn_T *isn;
1147
Bram Moolenaar080457c2020-03-03 21:53:32 +01001148 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1150 return FAIL;
1151 isn->isn_arg.fnumber = fnumber;
1152
1153 return OK;
1154}
1155#endif
1156
1157/*
1158 * Generate an ISN_PUSHS instruction.
1159 * Consumes "str".
1160 */
1161 static int
1162generate_PUSHS(cctx_T *cctx, char_u *str)
1163{
1164 isn_T *isn;
1165
Bram Moolenaar508b5612021-01-02 18:17:26 +01001166 if (cctx->ctx_skip == SKIP_YES)
1167 {
1168 vim_free(str);
1169 return OK;
1170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1172 return FAIL;
1173 isn->isn_arg.string = str;
1174
1175 return OK;
1176}
1177
1178/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001179 * Generate an ISN_PUSHCHANNEL instruction.
1180 * Consumes "channel".
1181 */
1182 static int
1183generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1184{
1185 isn_T *isn;
1186
Bram Moolenaar080457c2020-03-03 21:53:32 +01001187 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001188 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1189 return FAIL;
1190 isn->isn_arg.channel = channel;
1191
1192 return OK;
1193}
1194
1195/*
1196 * Generate an ISN_PUSHJOB instruction.
1197 * Consumes "job".
1198 */
1199 static int
1200generate_PUSHJOB(cctx_T *cctx, job_T *job)
1201{
1202 isn_T *isn;
1203
Bram Moolenaar080457c2020-03-03 21:53:32 +01001204 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001205 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001206 return FAIL;
1207 isn->isn_arg.job = job;
1208
1209 return OK;
1210}
1211
1212/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 * Generate an ISN_PUSHBLOB instruction.
1214 * Consumes "blob".
1215 */
1216 static int
1217generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1218{
1219 isn_T *isn;
1220
Bram Moolenaar080457c2020-03-03 21:53:32 +01001221 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1223 return FAIL;
1224 isn->isn_arg.blob = blob;
1225
1226 return OK;
1227}
1228
1229/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001230 * Generate an ISN_PUSHFUNC instruction with name "name".
1231 * Consumes "name".
1232 */
1233 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001234generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001235{
1236 isn_T *isn;
1237
Bram Moolenaar080457c2020-03-03 21:53:32 +01001238 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001239 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001240 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001241 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001242
1243 return OK;
1244}
1245
1246/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001247 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001248 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1249 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001250 */
1251 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001252generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001253{
1254 isn_T *isn;
1255 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001256 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1257 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001258 type_T *item_type = &t_any;
1259
1260 RETURN_OK_IF_SKIP(cctx);
1261
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001262 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001263 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001264 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001265 emsg(_(e_listreq));
1266 return FAIL;
1267 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001268 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001269 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1270 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001271 isn->isn_arg.getitem.gi_index = index;
1272 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001273
1274 // add the item type to the type stack
1275 if (ga_grow(stack, 1) == FAIL)
1276 return FAIL;
1277 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1278 ++stack->ga_len;
1279 return OK;
1280}
1281
1282/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001283 * Generate an ISN_SLICE instruction with "count".
1284 */
1285 static int
1286generate_SLICE(cctx_T *cctx, int count)
1287{
1288 isn_T *isn;
1289
1290 RETURN_OK_IF_SKIP(cctx);
1291 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1292 return FAIL;
1293 isn->isn_arg.number = count;
1294 return OK;
1295}
1296
1297/*
1298 * Generate an ISN_CHECKLEN instruction with "min_len".
1299 */
1300 static int
1301generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1302{
1303 isn_T *isn;
1304
1305 RETURN_OK_IF_SKIP(cctx);
1306
1307 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.checklen.cl_min_len = min_len;
1310 isn->isn_arg.checklen.cl_more_OK = more_OK;
1311
1312 return OK;
1313}
1314
1315/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 * Generate an ISN_STORE instruction.
1317 */
1318 static int
1319generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1320{
1321 isn_T *isn;
1322
Bram Moolenaar080457c2020-03-03 21:53:32 +01001323 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1325 return FAIL;
1326 if (name != NULL)
1327 isn->isn_arg.string = vim_strsave(name);
1328 else
1329 isn->isn_arg.number = idx;
1330
1331 return OK;
1332}
1333
1334/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001335 * Generate an ISN_STOREOUTER instruction.
1336 */
1337 static int
1338generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1339{
1340 isn_T *isn;
1341
1342 RETURN_OK_IF_SKIP(cctx);
1343 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1344 return FAIL;
1345 isn->isn_arg.outer.outer_idx = idx;
1346 isn->isn_arg.outer.outer_depth = level;
1347
1348 return OK;
1349}
1350
1351/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1353 */
1354 static int
1355generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1356{
1357 isn_T *isn;
1358
Bram Moolenaar080457c2020-03-03 21:53:32 +01001359 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1361 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001362 isn->isn_arg.storenr.stnr_idx = idx;
1363 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364
1365 return OK;
1366}
1367
1368/*
1369 * Generate an ISN_STOREOPT instruction
1370 */
1371 static int
1372generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1373{
1374 isn_T *isn;
1375
Bram Moolenaar080457c2020-03-03 21:53:32 +01001376 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001377 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 return FAIL;
1379 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1380 isn->isn_arg.storeopt.so_flags = opt_flags;
1381
1382 return OK;
1383}
1384
1385/*
1386 * Generate an ISN_LOAD or similar instruction.
1387 */
1388 static int
1389generate_LOAD(
1390 cctx_T *cctx,
1391 isntype_T isn_type,
1392 int idx,
1393 char_u *name,
1394 type_T *type)
1395{
1396 isn_T *isn;
1397
Bram Moolenaar080457c2020-03-03 21:53:32 +01001398 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1400 return FAIL;
1401 if (name != NULL)
1402 isn->isn_arg.string = vim_strsave(name);
1403 else
1404 isn->isn_arg.number = idx;
1405
1406 return OK;
1407}
1408
1409/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001410 * Generate an ISN_LOADOUTER instruction
1411 */
1412 static int
1413generate_LOADOUTER(
1414 cctx_T *cctx,
1415 int idx,
1416 int nesting,
1417 type_T *type)
1418{
1419 isn_T *isn;
1420
1421 RETURN_OK_IF_SKIP(cctx);
1422 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1423 return FAIL;
1424 isn->isn_arg.outer.outer_idx = idx;
1425 isn->isn_arg.outer.outer_depth = nesting;
1426
1427 return OK;
1428}
1429
1430/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001431 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001432 */
1433 static int
1434generate_LOADV(
1435 cctx_T *cctx,
1436 char_u *name,
1437 int error)
1438{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001439 int di_flags;
1440 int vidx = find_vim_var(name, &di_flags);
1441 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001442
Bram Moolenaar080457c2020-03-03 21:53:32 +01001443 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001444 if (vidx < 0)
1445 {
1446 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001447 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001448 return FAIL;
1449 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001450 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001451
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001452 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001453}
1454
1455/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001456 * Generate an ISN_UNLET instruction.
1457 */
1458 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001459generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001460{
1461 isn_T *isn;
1462
1463 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001464 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001465 return FAIL;
1466 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1467 isn->isn_arg.unlet.ul_forceit = forceit;
1468
1469 return OK;
1470}
1471
1472/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001473 * Generate an ISN_LOCKCONST instruction.
1474 */
1475 static int
1476generate_LOCKCONST(cctx_T *cctx)
1477{
1478 isn_T *isn;
1479
1480 RETURN_OK_IF_SKIP(cctx);
1481 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1482 return FAIL;
1483 return OK;
1484}
1485
1486/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 * Generate an ISN_LOADS instruction.
1488 */
1489 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001490generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001492 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001494 int sid,
1495 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496{
1497 isn_T *isn;
1498
Bram Moolenaar080457c2020-03-03 21:53:32 +01001499 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001500 if (isn_type == ISN_LOADS)
1501 isn = generate_instr_type(cctx, isn_type, type);
1502 else
1503 isn = generate_instr_drop(cctx, isn_type, 1);
1504 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001506 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1507 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508
1509 return OK;
1510}
1511
1512/*
1513 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1514 */
1515 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001516generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517 cctx_T *cctx,
1518 isntype_T isn_type,
1519 int sid,
1520 int idx,
1521 type_T *type)
1522{
1523 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001524 scriptref_T *sref;
1525 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526
Bram Moolenaar080457c2020-03-03 21:53:32 +01001527 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 if (isn_type == ISN_LOADSCRIPT)
1529 isn = generate_instr_type(cctx, isn_type, type);
1530 else
1531 isn = generate_instr_drop(cctx, isn_type, 1);
1532 if (isn == NULL)
1533 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001534
1535 // This requires three arguments, which doesn't fit in an instruction, thus
1536 // we need to allocate a struct for this.
1537 sref = ALLOC_ONE(scriptref_T);
1538 if (sref == NULL)
1539 return FAIL;
1540 isn->isn_arg.script.scriptref = sref;
1541 sref->sref_sid = sid;
1542 sref->sref_idx = idx;
1543 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001544 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 return OK;
1546}
1547
1548/*
1549 * Generate an ISN_NEWLIST instruction.
1550 */
1551 static int
1552generate_NEWLIST(cctx_T *cctx, int count)
1553{
1554 isn_T *isn;
1555 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 type_T *type;
1557 type_T *member;
1558
Bram Moolenaar080457c2020-03-03 21:53:32 +01001559 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1561 return FAIL;
1562 isn->isn_arg.number = count;
1563
Bram Moolenaar127542b2020-08-09 17:22:04 +02001564 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001565 if (count == 0)
1566 member = &t_void;
1567 else
1568 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001569 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1570 cctx->ctx_type_list);
1571 type = get_list_type(member, cctx->ctx_type_list);
1572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 // drop the value types
1574 stack->ga_len -= count;
1575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 // add the list type to the type stack
1577 if (ga_grow(stack, 1) == FAIL)
1578 return FAIL;
1579 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1580 ++stack->ga_len;
1581
1582 return OK;
1583}
1584
1585/*
1586 * Generate an ISN_NEWDICT instruction.
1587 */
1588 static int
1589generate_NEWDICT(cctx_T *cctx, int count)
1590{
1591 isn_T *isn;
1592 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 type_T *type;
1594 type_T *member;
1595
Bram Moolenaar080457c2020-03-03 21:53:32 +01001596 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1598 return FAIL;
1599 isn->isn_arg.number = count;
1600
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001601 if (count == 0)
1602 member = &t_void;
1603 else
1604 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001605 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1606 cctx->ctx_type_list);
1607 type = get_dict_type(member, cctx->ctx_type_list);
1608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 // drop the key and value types
1610 stack->ga_len -= 2 * count;
1611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 // add the dict type to the type stack
1613 if (ga_grow(stack, 1) == FAIL)
1614 return FAIL;
1615 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1616 ++stack->ga_len;
1617
1618 return OK;
1619}
1620
1621/*
1622 * Generate an ISN_FUNCREF instruction.
1623 */
1624 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001625generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626{
1627 isn_T *isn;
1628 garray_T *stack = &cctx->ctx_type_stack;
1629
Bram Moolenaar080457c2020-03-03 21:53:32 +01001630 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1632 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001633 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001634 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001636 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001637 // the nested context, including this one.
1638 if (ufunc->uf_flags & FC_CLOSURE)
1639 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 if (ga_grow(stack, 1) == FAIL)
1642 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001643 ((type_T **)stack->ga_data)[stack->ga_len] =
1644 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 ++stack->ga_len;
1646
1647 return OK;
1648}
1649
1650/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001651 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001652 * "lambda_name" and "func_name" must be in allocated memory and will be
1653 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001654 */
1655 static int
1656generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1657{
1658 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001659
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001660 if (cctx->ctx_skip == SKIP_YES)
1661 {
1662 vim_free(lambda_name);
1663 vim_free(func_name);
1664 return OK;
1665 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001666 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001667 {
1668 vim_free(lambda_name);
1669 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001670 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001671 }
1672 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001673 isn->isn_arg.newfunc.nf_global = func_name;
1674
1675 return OK;
1676}
1677
1678/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001679 * Generate an ISN_DEF instruction: list functions
1680 */
1681 static int
1682generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1683{
1684 isn_T *isn;
1685
1686 RETURN_OK_IF_SKIP(cctx);
1687 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1688 return FAIL;
1689 if (len > 0)
1690 {
1691 isn->isn_arg.string = vim_strnsave(name, len);
1692 if (isn->isn_arg.string == NULL)
1693 return FAIL;
1694 }
1695 return OK;
1696}
1697
1698/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 * Generate an ISN_JUMP instruction.
1700 */
1701 static int
1702generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1703{
1704 isn_T *isn;
1705 garray_T *stack = &cctx->ctx_type_stack;
1706
Bram Moolenaar080457c2020-03-03 21:53:32 +01001707 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1709 return FAIL;
1710 isn->isn_arg.jump.jump_when = when;
1711 isn->isn_arg.jump.jump_where = where;
1712
1713 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1714 --stack->ga_len;
1715
1716 return OK;
1717}
1718
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001719/*
1720 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1721 */
1722 static int
1723generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1724{
1725 isn_T *isn;
1726
1727 RETURN_OK_IF_SKIP(cctx);
1728 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1729 return FAIL;
1730 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1731 // jump_where is set later
1732 return OK;
1733}
1734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 static int
1736generate_FOR(cctx_T *cctx, int loop_idx)
1737{
1738 isn_T *isn;
1739 garray_T *stack = &cctx->ctx_type_stack;
1740
Bram Moolenaar080457c2020-03-03 21:53:32 +01001741 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1743 return FAIL;
1744 isn->isn_arg.forloop.for_idx = loop_idx;
1745
1746 if (ga_grow(stack, 1) == FAIL)
1747 return FAIL;
1748 // type doesn't matter, will be stored next
1749 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1750 ++stack->ga_len;
1751
1752 return OK;
1753}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001754/*
1755 * Generate an ISN_TRYCONT instruction.
1756 */
1757 static int
1758generate_TRYCONT(cctx_T *cctx, int levels, int where)
1759{
1760 isn_T *isn;
1761
1762 RETURN_OK_IF_SKIP(cctx);
1763 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1764 return FAIL;
1765 isn->isn_arg.trycont.tct_levels = levels;
1766 isn->isn_arg.trycont.tct_where = where;
1767
1768 return OK;
1769}
1770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771
1772/*
1773 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001774 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 * Return FAIL if the number of arguments is wrong.
1776 */
1777 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001778generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779{
1780 isn_T *isn;
1781 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001782 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001783 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001784 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785
Bram Moolenaar080457c2020-03-03 21:53:32 +01001786 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001787 argoff = check_internal_func(func_idx, argcount);
1788 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 return FAIL;
1790
Bram Moolenaar389df252020-07-09 21:20:47 +02001791 if (method_call && argoff > 1)
1792 {
1793 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1794 return FAIL;
1795 isn->isn_arg.shuffle.shfl_item = argcount;
1796 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1797 }
1798
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001799 if (argcount > 0)
1800 {
1801 // Check the types of the arguments.
1802 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001803 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1804 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001805 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001806 if (internal_func_is_map(func_idx))
1807 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001808 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1811 return FAIL;
1812 isn->isn_arg.bfunc.cbf_idx = func_idx;
1813 isn->isn_arg.bfunc.cbf_argcount = argcount;
1814
Bram Moolenaar94738d82020-10-21 14:25:07 +02001815 // Drop the argument types and push the return type.
1816 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 if (ga_grow(stack, 1) == FAIL)
1818 return FAIL;
1819 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001820 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001821 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001823 if (maptype != NULL && maptype->tt_member != NULL
1824 && maptype->tt_member != &t_any)
1825 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001826 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 return OK;
1829}
1830
1831/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001832 * Generate an ISN_LISTAPPEND instruction. Works like add().
1833 * Argument count is already checked.
1834 */
1835 static int
1836generate_LISTAPPEND(cctx_T *cctx)
1837{
1838 garray_T *stack = &cctx->ctx_type_stack;
1839 type_T *list_type;
1840 type_T *item_type;
1841 type_T *expected;
1842
1843 // Caller already checked that list_type is a list.
1844 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1845 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1846 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001847 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001848 return FAIL;
1849
1850 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1851 return FAIL;
1852
1853 --stack->ga_len; // drop the argument
1854 return OK;
1855}
1856
1857/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001858 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1859 * Argument count is already checked.
1860 */
1861 static int
1862generate_BLOBAPPEND(cctx_T *cctx)
1863{
1864 garray_T *stack = &cctx->ctx_type_stack;
1865 type_T *item_type;
1866
1867 // Caller already checked that blob_type is a blob.
1868 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001869 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001870 return FAIL;
1871
1872 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1873 return FAIL;
1874
1875 --stack->ga_len; // drop the argument
1876 return OK;
1877}
1878
1879/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001880 * Return TRUE if "ufunc" should be compiled, taking into account whether
1881 * "profile" indicates profiling is to be done.
1882 */
1883 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001884func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001885{
1886 switch (ufunc->uf_def_status)
1887 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001888 case UF_TO_BE_COMPILED:
1889 return TRUE;
1890
Bram Moolenaarb2049902021-01-24 12:53:53 +01001891 case UF_COMPILED:
1892 {
1893 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1894 + ufunc->uf_dfunc_idx;
1895
Bram Moolenaare99d4222021-06-13 14:01:26 +02001896 switch (compile_type)
1897 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001898 case CT_PROFILE:
1899#ifdef FEAT_PROFILE
1900 return dfunc->df_instr_prof == NULL;
1901#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001902 case CT_NONE:
1903 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001904 case CT_DEBUG:
1905 return dfunc->df_instr_debug == NULL;
1906 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001907 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001908
1909 case UF_NOT_COMPILED:
1910 case UF_COMPILE_ERROR:
1911 case UF_COMPILING:
1912 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001913 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001914 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001915}
1916
1917/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 * Generate an ISN_DCALL or ISN_UCALL instruction.
1919 * Return FAIL if the number of arguments is wrong.
1920 */
1921 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001922generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923{
1924 isn_T *isn;
1925 garray_T *stack = &cctx->ctx_type_stack;
1926 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001927 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928
Bram Moolenaar080457c2020-03-03 21:53:32 +01001929 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 if (argcount > regular_args && !has_varargs(ufunc))
1931 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001932 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 return FAIL;
1934 }
1935 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1936 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001937 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 return FAIL;
1939 }
1940
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001941 if (ufunc->uf_def_status != UF_NOT_COMPILED
1942 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001943 {
1944 int i;
1945
1946 for (i = 0; i < argcount; ++i)
1947 {
1948 type_T *expected;
1949 type_T *actual;
1950
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001951 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1952 if (actual == &t_special
1953 && i >= regular_args - ufunc->uf_def_args.ga_len)
1954 {
1955 // assume v:none used for default argument value
1956 continue;
1957 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001958 if (i < regular_args)
1959 {
1960 if (ufunc->uf_arg_types == NULL)
1961 continue;
1962 expected = ufunc->uf_arg_types[i];
1963 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001964 else if (ufunc->uf_va_type == NULL
1965 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001966 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001967 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001968 else
1969 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001970 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001971 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001972 {
1973 arg_type_mismatch(expected, actual, i + 1);
1974 return FAIL;
1975 }
1976 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02001977 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001978 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02001979 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001980 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001981 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02001982 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1983 {
1984 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1985 ufunc->uf_name);
1986 return FAIL;
1987 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001990 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001991 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001993 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 {
1995 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1996 isn->isn_arg.dfunc.cdf_argcount = argcount;
1997 }
1998 else
1999 {
2000 // A user function may be deleted and redefined later, can't use the
2001 // ufunc pointer, need to look it up again at runtime.
2002 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2003 isn->isn_arg.ufunc.cuf_argcount = argcount;
2004 }
2005
2006 stack->ga_len -= argcount; // drop the arguments
2007 if (ga_grow(stack, 1) == FAIL)
2008 return FAIL;
2009 // add return value
2010 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2011 ++stack->ga_len;
2012
2013 return OK;
2014}
2015
2016/*
2017 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2018 */
2019 static int
2020generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2021{
2022 isn_T *isn;
2023 garray_T *stack = &cctx->ctx_type_stack;
2024
Bram Moolenaar080457c2020-03-03 21:53:32 +01002025 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2027 return FAIL;
2028 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2029 isn->isn_arg.ufunc.cuf_argcount = argcount;
2030
2031 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002032 if (ga_grow(stack, 1) == FAIL)
2033 return FAIL;
2034 // add return value
2035 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2036 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037
2038 return OK;
2039}
2040
2041/*
2042 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002043 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 */
2045 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002046generate_PCALL(
2047 cctx_T *cctx,
2048 int argcount,
2049 char_u *name,
2050 type_T *type,
2051 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052{
2053 isn_T *isn;
2054 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002055 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056
Bram Moolenaar080457c2020-03-03 21:53:32 +01002057 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002058
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002059 if (type->tt_type == VAR_ANY)
2060 ret_type = &t_any;
2061 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002062 {
2063 if (type->tt_argcount != -1)
2064 {
2065 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2066
2067 if (argcount < type->tt_min_argcount - varargs)
2068 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002069 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002070 return FAIL;
2071 }
2072 if (!varargs && argcount > type->tt_argcount)
2073 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002074 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002075 return FAIL;
2076 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002077 if (type->tt_args != NULL)
2078 {
2079 int i;
2080
2081 for (i = 0; i < argcount; ++i)
2082 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002083 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002084 type_T *actual = ((type_T **)stack->ga_data)[
2085 stack->ga_len + offset];
2086 type_T *expected;
2087
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002088 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002089 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002090 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002091 else if (i >= type->tt_min_argcount
2092 && actual == &t_special)
2093 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002094 else
2095 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002096 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002097 cctx, TRUE, FALSE) == FAIL)
2098 {
2099 arg_type_mismatch(expected, actual, i + 1);
2100 return FAIL;
2101 }
2102 }
2103 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002104 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002105 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002106 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002107 else
2108 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002109 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002110 return FAIL;
2111 }
2112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2114 return FAIL;
2115 isn->isn_arg.pfunc.cpf_top = at_top;
2116 isn->isn_arg.pfunc.cpf_argcount = argcount;
2117
2118 stack->ga_len -= argcount; // drop the arguments
2119
2120 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002121 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002123 // If partial is above the arguments it must be cleared and replaced with
2124 // the return value.
2125 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2126 return FAIL;
2127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 return OK;
2129}
2130
2131/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002132 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 */
2134 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002135generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136{
2137 isn_T *isn;
2138 garray_T *stack = &cctx->ctx_type_stack;
2139 type_T *type;
2140
Bram Moolenaar080457c2020-03-03 21:53:32 +01002141 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002142 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002144 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002146 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002148 if (type->tt_type != VAR_DICT && type != &t_any)
2149 {
2150 emsg(_(e_dictreq));
2151 return FAIL;
2152 }
2153 // change dict type to dict member type
2154 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002155 {
2156 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2157 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159
2160 return OK;
2161}
2162
2163/*
2164 * Generate an ISN_ECHO instruction.
2165 */
2166 static int
2167generate_ECHO(cctx_T *cctx, int with_white, int count)
2168{
2169 isn_T *isn;
2170
Bram Moolenaar080457c2020-03-03 21:53:32 +01002171 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2173 return FAIL;
2174 isn->isn_arg.echo.echo_with_white = with_white;
2175 isn->isn_arg.echo.echo_count = count;
2176
2177 return OK;
2178}
2179
Bram Moolenaarad39c092020-02-26 18:23:43 +01002180/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002181 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002182 */
2183 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002184generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002185{
2186 isn_T *isn;
2187
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002188 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002189 return FAIL;
2190 isn->isn_arg.number = count;
2191
2192 return OK;
2193}
2194
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002195/*
2196 * Generate an ISN_PUT instruction.
2197 */
2198 static int
2199generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2200{
2201 isn_T *isn;
2202
2203 RETURN_OK_IF_SKIP(cctx);
2204 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2205 return FAIL;
2206 isn->isn_arg.put.put_regname = regname;
2207 isn->isn_arg.put.put_lnum = lnum;
2208 return OK;
2209}
2210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 static int
2212generate_EXEC(cctx_T *cctx, char_u *line)
2213{
2214 isn_T *isn;
2215
Bram Moolenaar080457c2020-03-03 21:53:32 +01002216 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2218 return FAIL;
2219 isn->isn_arg.string = vim_strsave(line);
2220 return OK;
2221}
2222
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002223 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002224generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2225{
2226 isn_T *isn;
2227 garray_T *stack = &cctx->ctx_type_stack;
2228
2229 RETURN_OK_IF_SKIP(cctx);
2230 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2231 return FAIL;
2232 isn->isn_arg.string = vim_strsave(line);
2233
2234 if (ga_grow(stack, 1) == FAIL)
2235 return FAIL;
2236 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2237 ++stack->ga_len;
2238
2239 return OK;
2240}
2241
2242 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002243generate_EXECCONCAT(cctx_T *cctx, int count)
2244{
2245 isn_T *isn;
2246
2247 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2248 return FAIL;
2249 isn->isn_arg.number = count;
2250 return OK;
2251}
2252
Bram Moolenaar08597872020-12-10 19:43:40 +01002253/*
2254 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2255 */
2256 static int
2257generate_RANGE(cctx_T *cctx, char_u *range)
2258{
2259 isn_T *isn;
2260 garray_T *stack = &cctx->ctx_type_stack;
2261
2262 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2263 return FAIL;
2264 isn->isn_arg.string = range;
2265
2266 if (ga_grow(stack, 1) == FAIL)
2267 return FAIL;
2268 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2269 ++stack->ga_len;
2270 return OK;
2271}
2272
Bram Moolenaar792f7862020-11-23 08:31:18 +01002273 static int
2274generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2275{
2276 isn_T *isn;
2277
2278 RETURN_OK_IF_SKIP(cctx);
2279 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2280 return FAIL;
2281 isn->isn_arg.unpack.unp_count = var_count;
2282 isn->isn_arg.unpack.unp_semicolon = semicolon;
2283 return OK;
2284}
2285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002287 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002288 */
2289 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002290generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002291{
2292 isn_T *isn;
2293
Bram Moolenaarfa984412021-03-25 22:15:28 +01002294 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002295 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002296 cctx->ctx_has_cmdmod = TRUE;
2297
2298 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002299 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002300 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2301 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2302 return FAIL;
2303 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002304 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002305 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002306 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002307
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002308 return OK;
2309}
2310
2311 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002312generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002313{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002314 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2315 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002316 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002317 return OK;
2318}
2319
Bram Moolenaarfa984412021-03-25 22:15:28 +01002320 static int
2321misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002322{
2323 garray_T *instr = &cctx->ctx_instr;
2324
Bram Moolenaara91a7132021-03-25 21:12:15 +01002325 if (cctx->ctx_has_cmdmod
2326 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2327 == ISN_CMDMOD)
2328 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002329 emsg(_(e_misplaced_command_modifier));
2330 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002331 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002332 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002333}
2334
2335/*
2336 * Get the index of the current instruction.
2337 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2338 */
2339 static int
2340current_instr_idx(cctx_T *cctx)
2341{
2342 garray_T *instr = &cctx->ctx_instr;
2343 int idx = instr->ga_len;
2344
Bram Moolenaare99d4222021-06-13 14:01:26 +02002345 while (idx > 0)
2346 {
2347 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002348 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002349 {
2350 --idx;
2351 continue;
2352 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002353#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002354 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2355 {
2356 --idx;
2357 continue;
2358 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002359#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002360 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2361 {
2362 --idx;
2363 continue;
2364 }
2365 break;
2366 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002367 return idx;
2368}
2369
Bram Moolenaarf002a412021-01-24 13:34:18 +01002370#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002371 static void
2372may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2373{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002374 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002375 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002376}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002377#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002378
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002379/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002381 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002383 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002384reserve_local(
2385 cctx_T *cctx,
2386 char_u *name,
2387 size_t len,
2388 int isConst,
2389 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002392 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002394 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002396 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002397 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 }
2399
2400 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002401 return NULL;
2402 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002403 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002405 // Every local variable uses the next entry on the stack. We could re-use
2406 // the last ones when leaving a scope, but then variables used in a closure
2407 // might get overwritten. To keep things simple do not re-use stack
2408 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002409 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2410 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002411
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002412 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 lvar->lv_const = isConst;
2414 lvar->lv_type = type;
2415
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002416 // Remember the name for debugging.
2417 if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2418 return NULL;
2419 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2420 vim_strsave(lvar->lv_name);
2421 ++dfunc->df_var_names.ga_len;
2422
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002423 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424}
2425
2426/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002427 * Remove local variables above "new_top".
2428 */
2429 static void
2430unwind_locals(cctx_T *cctx, int new_top)
2431{
2432 if (cctx->ctx_locals.ga_len > new_top)
2433 {
2434 int idx;
2435 lvar_T *lvar;
2436
2437 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2438 {
2439 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2440 vim_free(lvar->lv_name);
2441 }
2442 }
2443 cctx->ctx_locals.ga_len = new_top;
2444}
2445
2446/*
2447 * Free all local variables.
2448 */
2449 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002450free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002451{
2452 unwind_locals(cctx, 0);
2453 ga_clear(&cctx->ctx_locals);
2454}
2455
2456/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002457 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2458 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2459 * error if the variable was defined with :const.
2460 */
2461 static int
2462check_item_writable(svar_T *sv, int check_writable, char_u *name)
2463{
2464 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2465 || (check_writable == ASSIGN_FINAL
2466 && sv->sv_const == ASSIGN_CONST))
2467 {
2468 semsg(_(e_readonlyvar), name);
2469 return FAIL;
2470 }
2471 return OK;
2472}
2473
2474/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002476 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 * Returns the index in "sn_var_vals" if found.
2478 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002479 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 */
2481 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002482get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483{
2484 hashtab_T *ht;
2485 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002486 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002487 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 int idx;
2489
Bram Moolenaare3d46852020-08-29 13:39:17 +02002490 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002492 if (sid == current_sctx.sc_sid)
2493 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002494 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002495
2496 if (sav == NULL)
2497 return -2;
2498 idx = sav->sav_var_vals_idx;
2499 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002500 if (check_item_writable(sv, check_writable, name) == FAIL)
2501 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002502 return idx;
2503 }
2504
2505 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 ht = &SCRIPT_VARS(sid);
2507 di = find_var_in_ht(ht, 0, name, TRUE);
2508 if (di == NULL)
2509 return -2;
2510
2511 // Now find the svar_T index in sn_var_vals.
2512 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2513 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002514 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 if (sv->sv_tv == &di->di_tv)
2516 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002517 if (check_item_writable(sv, check_writable, name) == FAIL)
2518 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 return idx;
2520 }
2521 }
2522 return -1;
2523}
2524
2525/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002526 * Find "name" in imported items of the current script or in "cctx" if not
2527 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 */
2529 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002530find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 int idx;
2533
Bram Moolenaare3d46852020-08-29 13:39:17 +02002534 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002535 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 if (cctx != NULL)
2537 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2538 {
2539 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2540 + idx;
2541
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002542 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2543 : STRLEN(import->imp_name) == len
2544 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 return import;
2546 }
2547
Bram Moolenaarefa94442020-08-08 22:16:00 +02002548 return find_imported_in_script(name, len, current_sctx.sc_sid);
2549}
2550
2551 imported_T *
2552find_imported_in_script(char_u *name, size_t len, int sid)
2553{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002554 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002555 int idx;
2556
Bram Moolenaare3d46852020-08-29 13:39:17 +02002557 if (!SCRIPT_ID_VALID(sid))
2558 return NULL;
2559 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2561 {
2562 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2563
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002564 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2565 : STRLEN(import->imp_name) == len
2566 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 return import;
2568 }
2569 return NULL;
2570}
2571
2572/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002573 * Free all imported variables.
2574 */
2575 static void
2576free_imported(cctx_T *cctx)
2577{
2578 int idx;
2579
2580 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2581 {
2582 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2583
2584 vim_free(import->imp_name);
2585 }
2586 ga_clear(&cctx->ctx_imports);
2587}
2588
2589/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002590 * Return a pointer to the next line that isn't empty or only contains a
2591 * comment. Skips over white space.
2592 * Returns NULL if there is none.
2593 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002594 char_u *
2595peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002596{
2597 int lnum = cctx->ctx_lnum;
2598
2599 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2600 {
2601 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002602 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002603
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002604 // ignore NULLs inserted for continuation lines
2605 if (line != NULL)
2606 {
2607 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002608 if (vim9_bad_comment(p))
2609 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002610 if (*p != NUL && !vim9_comment_start(p))
2611 return p;
2612 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002613 }
2614 return NULL;
2615}
2616
2617/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002618 * Called when checking for a following operator at "arg". When the rest of
2619 * the line is empty or only a comment, peek the next line. If there is a next
2620 * line return a pointer to it and set "nextp".
2621 * Otherwise skip over white space.
2622 */
2623 static char_u *
2624may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2625{
2626 char_u *p = skipwhite(arg);
2627
2628 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002629 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002630 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002631 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002632 if (*nextp != NULL)
2633 return *nextp;
2634 }
2635 return p;
2636}
2637
2638/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002639 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002640 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002641 * Returns NULL when at the end.
2642 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002643 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002644next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002645{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002646 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002647
2648 do
2649 {
2650 ++cctx->ctx_lnum;
2651 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002652 {
2653 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002654 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002655 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002656 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002657 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002658 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002659 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002660 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002661 return line;
2662}
2663
2664/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002665 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002666 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002667 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002668 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2669 */
2670 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002671may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002672{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002673 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002674 if (vim9_bad_comment(*arg))
2675 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002676 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002677 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002678 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002679
2680 if (next == NULL)
2681 return FAIL;
2682 *arg = skipwhite(next);
2683 }
2684 return OK;
2685}
2686
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002687/*
2688 * Idem, and give an error when failed.
2689 */
2690 static int
2691may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2692{
2693 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2694 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002695 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002696 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002697 return FAIL;
2698 }
2699 return OK;
2700}
2701
2702
Bram Moolenaara5565e42020-05-09 15:44:01 +02002703// Structure passed between the compile_expr* functions to keep track of
2704// constants that have been parsed but for which no code was produced yet. If
2705// possible expressions on these constants are applied at compile time. If
2706// that is not possible, the code to push the constants needs to be generated
2707// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002708// Using 50 should be more than enough of 5 levels of ().
2709#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002710typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002711 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002712 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002713 int pp_is_const; // all generated code was constants, used for a
2714 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002715} ppconst_T;
2716
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002717static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002718static int compile_expr0(char_u **arg, cctx_T *cctx);
2719static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2720
Bram Moolenaara5565e42020-05-09 15:44:01 +02002721/*
2722 * Generate a PUSH instruction for "tv".
2723 * "tv" will be consumed or cleared.
2724 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2725 */
2726 static int
2727generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2728{
2729 if (tv != NULL)
2730 {
2731 switch (tv->v_type)
2732 {
2733 case VAR_UNKNOWN:
2734 break;
2735 case VAR_BOOL:
2736 generate_PUSHBOOL(cctx, tv->vval.v_number);
2737 break;
2738 case VAR_SPECIAL:
2739 generate_PUSHSPEC(cctx, tv->vval.v_number);
2740 break;
2741 case VAR_NUMBER:
2742 generate_PUSHNR(cctx, tv->vval.v_number);
2743 break;
2744#ifdef FEAT_FLOAT
2745 case VAR_FLOAT:
2746 generate_PUSHF(cctx, tv->vval.v_float);
2747 break;
2748#endif
2749 case VAR_BLOB:
2750 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2751 tv->vval.v_blob = NULL;
2752 break;
2753 case VAR_STRING:
2754 generate_PUSHS(cctx, tv->vval.v_string);
2755 tv->vval.v_string = NULL;
2756 break;
2757 default:
2758 iemsg("constant type not supported");
2759 clear_tv(tv);
2760 return FAIL;
2761 }
2762 tv->v_type = VAR_UNKNOWN;
2763 }
2764 return OK;
2765}
2766
2767/*
2768 * Generate code for any ppconst entries.
2769 */
2770 static int
2771generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2772{
2773 int i;
2774 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002775 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002776
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002777 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002778 for (i = 0; i < ppconst->pp_used; ++i)
2779 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2780 ret = FAIL;
2781 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002782 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002783 return ret;
2784}
2785
2786/*
2787 * Clear ppconst constants. Used when failing.
2788 */
2789 static void
2790clear_ppconst(ppconst_T *ppconst)
2791{
2792 int i;
2793
2794 for (i = 0; i < ppconst->pp_used; ++i)
2795 clear_tv(&ppconst->pp_tv[i]);
2796 ppconst->pp_used = 0;
2797}
2798
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002799/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002800 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002801 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002802 */
2803 static int
2804compile_member(int is_slice, cctx_T *cctx)
2805{
2806 type_T **typep;
2807 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002808 vartype_T vartype;
2809 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002810
Bram Moolenaar261417b2021-05-06 21:04:55 +02002811 // We can index a list, dict and blob. If we don't know the type
2812 // we can use the index value type. If we still don't know use an "ANY"
2813 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002814 typep = ((type_T **)stack->ga_data) + stack->ga_len
2815 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002816 vartype = (*typep)->tt_type;
2817 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002818 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002819 if (*typep == &t_any && idxtype == &t_string)
2820 vartype = VAR_DICT;
2821 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002822 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002823 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002824 return FAIL;
2825 if (is_slice)
2826 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002827 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2828 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002829 FALSE, FALSE) == FAIL)
2830 return FAIL;
2831 }
2832 }
2833
Bram Moolenaar261417b2021-05-06 21:04:55 +02002834 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002835 {
2836 if (is_slice)
2837 {
2838 emsg(_(e_cannot_slice_dictionary));
2839 return FAIL;
2840 }
2841 if ((*typep)->tt_type == VAR_DICT)
2842 {
2843 *typep = (*typep)->tt_member;
2844 if (*typep == &t_unknown)
2845 // empty dict was used
2846 *typep = &t_any;
2847 }
2848 else
2849 {
2850 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2851 FALSE, FALSE) == FAIL)
2852 return FAIL;
2853 *typep = &t_any;
2854 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002855 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002856 return FAIL;
2857 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2858 return FAIL;
2859 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002860 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002861 {
2862 *typep = &t_string;
2863 if ((is_slice
2864 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2865 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2866 return FAIL;
2867 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002868 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002869 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002870 if (is_slice)
2871 {
2872 *typep = &t_blob;
2873 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2874 return FAIL;
2875 }
2876 else
2877 {
2878 *typep = &t_number;
2879 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2880 return FAIL;
2881 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002882 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002883 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002884 {
2885 if (is_slice)
2886 {
2887 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002888 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002889 2) == FAIL)
2890 return FAIL;
2891 }
2892 else
2893 {
2894 if ((*typep)->tt_type == VAR_LIST)
2895 {
2896 *typep = (*typep)->tt_member;
2897 if (*typep == &t_unknown)
2898 // empty list was used
2899 *typep = &t_any;
2900 }
2901 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002902 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2903 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002904 return FAIL;
2905 }
2906 }
2907 else
2908 {
2909 emsg(_(e_string_list_dict_or_blob_required));
2910 return FAIL;
2911 }
2912 return OK;
2913}
2914
2915/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002916 * Generate an instruction to load script-local variable "name", without the
2917 * leading "s:".
2918 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 */
2920 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002921compile_load_scriptvar(
2922 cctx_T *cctx,
2923 char_u *name, // variable NUL terminated
2924 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002925 char_u **end, // end of variable
2926 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002928 scriptitem_T *si;
2929 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 imported_T *import;
2931
Bram Moolenaare3d46852020-08-29 13:39:17 +02002932 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2933 return FAIL;
2934 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002935 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002936 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002938 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002939 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2940 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 }
2942 if (idx >= 0)
2943 {
2944 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2945
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002946 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 current_sctx.sc_sid, idx, sv->sv_type);
2948 return OK;
2949 }
2950
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002951 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 if (import != NULL)
2953 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002954 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002955 {
2956 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002957 char_u *exp_name;
2958 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002959 ufunc_T *ufunc;
2960 type_T *type;
2961
2962 // Used "import * as Name", need to lookup the member.
2963 if (*p != '.')
2964 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002965 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002966 return FAIL;
2967 }
2968 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002969 if (VIM_ISWHITE(*p))
2970 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002971 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002972 return FAIL;
2973 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002974
Bram Moolenaar1c991142020-07-04 13:15:31 +02002975 // isolate one name
2976 exp_name = p;
2977 while (eval_isnamec(*p))
2978 ++p;
2979 cc = *p;
2980 *p = NUL;
2981
Bram Moolenaaredba7072021-03-13 21:14:18 +01002982 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2983 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002984 *p = cc;
2985 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002986 *end = p;
2987
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002988 if (idx < 0)
2989 {
2990 if (*p == '(' && ufunc != NULL)
2991 {
2992 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2993 return OK;
2994 }
2995 return FAIL;
2996 }
2997
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002998 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2999 import->imp_sid,
3000 idx,
3001 type);
3002 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003003 else if (import->imp_funcname != NULL)
3004 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003005 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003006 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3007 import->imp_sid,
3008 import->imp_var_vals_idx,
3009 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 return OK;
3011 }
3012
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003013 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003014 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 return FAIL;
3016}
3017
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003018 static int
3019generate_funcref(cctx_T *cctx, char_u *name)
3020{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003021 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003022
3023 if (ufunc == NULL)
3024 return FAIL;
3025
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003026 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003027 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3028 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003029 == FAIL)
3030 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003031 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003032}
3033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034/*
3035 * Compile a variable name into a load instruction.
3036 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003037 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 * When "error" is FALSE do not give an error when not found.
3039 */
3040 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003041compile_load(
3042 char_u **arg,
3043 char_u *end_arg,
3044 cctx_T *cctx,
3045 int is_expr,
3046 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047{
3048 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003049 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003050 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003052 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053
3054 if (*(*arg + 1) == ':')
3055 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003056 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003057 {
3058 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059
Bram Moolenaarfa596382021-04-07 21:58:16 +02003060 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003061 switch (**arg)
3062 {
3063 case 'g': isn_type = ISN_LOADGDICT; break;
3064 case 'w': isn_type = ISN_LOADWDICT; break;
3065 case 't': isn_type = ISN_LOADTDICT; break;
3066 case 'b': isn_type = ISN_LOADBDICT; break;
3067 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003068 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003069 goto theend;
3070 }
3071 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3072 goto theend;
3073 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 else
3076 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003077 isntype_T isn_type = ISN_DROP;
3078
Bram Moolenaarfa596382021-04-07 21:58:16 +02003079 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003080 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3081 if (name == NULL)
3082 return FAIL;
3083
3084 switch (**arg)
3085 {
3086 case 'v': res = generate_LOADV(cctx, name, error);
3087 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003088 case 's': if (is_expr && ASCII_ISUPPER(*name)
3089 && find_func(name, FALSE, cctx) != NULL)
3090 res = generate_funcref(cctx, name);
3091 else
3092 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003093 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003094 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003095 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003096 {
3097 if (is_expr && ASCII_ISUPPER(*name)
3098 && find_func(name, FALSE, cctx) != NULL)
3099 res = generate_funcref(cctx, name);
3100 else
3101 isn_type = ISN_LOADG;
3102 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003103 else
3104 {
3105 isn_type = ISN_LOADAUTO;
3106 vim_free(name);
3107 name = vim_strnsave(*arg, end - *arg);
3108 if (name == NULL)
3109 return FAIL;
3110 }
3111 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003112 case 'w': isn_type = ISN_LOADW; break;
3113 case 't': isn_type = ISN_LOADT; break;
3114 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003115 default: // cannot happen, just in case
3116 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003117 goto theend;
3118 }
3119 if (isn_type != ISN_DROP)
3120 {
3121 // Global, Buffer-local, Window-local and Tabpage-local
3122 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003123 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003124 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 }
3127 }
3128 else
3129 {
3130 size_t len = end - *arg;
3131 int idx;
3132 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003133 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134
3135 name = vim_strnsave(*arg, end - *arg);
3136 if (name == NULL)
3137 return FAIL;
3138
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003139 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3140 {
3141 script_autoload(name, FALSE);
3142 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3143 }
3144 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3145 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003147 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003148 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 }
3150 else
3151 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003152 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003153
Bram Moolenaar709664c2020-12-12 14:33:41 +01003154 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003156 type = lvar.lv_type;
3157 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003158 if (lvar.lv_from_outer != 0)
3159 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003160 else
3161 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 }
3163 else
3164 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003165 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003166 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003167 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003168 || find_imported(name, 0, cctx) != NULL)
3169 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003170
Bram Moolenaar0f769812020-09-12 18:32:34 +02003171 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003172 // uppercase letter it can be a user defined function.
3173 // generate_funcref() will fail if the function can't be found.
3174 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003175 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 }
3177 }
3178 if (gen_load)
3179 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003180 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003181 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003182 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003183 cctx->ctx_outer_used = TRUE;
3184 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 }
3186
3187 *arg = end;
3188
3189theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003190 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003191 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 vim_free(name);
3193 return res;
3194}
3195
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003196 static void
3197clear_instr_ga(garray_T *gap)
3198{
3199 int idx;
3200
3201 for (idx = 0; idx < gap->ga_len; ++idx)
3202 delete_instr(((isn_T *)gap->ga_data) + idx);
3203 ga_clear(gap);
3204}
3205
3206/*
3207 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3208 * Returns FAIL if compilation fails.
3209 */
3210 static int
3211compile_string(isn_T *isn, cctx_T *cctx)
3212{
3213 char_u *s = isn->isn_arg.string;
3214 garray_T save_ga = cctx->ctx_instr;
3215 int expr_res;
3216 int trailing_error;
3217 int instr_count;
3218 isn_T *instr = NULL;
3219
3220 // Temporarily reset the list of instructions so that the jump labels are
3221 // correct.
3222 cctx->ctx_instr.ga_len = 0;
3223 cctx->ctx_instr.ga_maxlen = 0;
3224 cctx->ctx_instr.ga_data = NULL;
3225 expr_res = compile_expr0(&s, cctx);
3226 s = skipwhite(s);
3227 trailing_error = *s != NUL;
3228
Bram Moolenaarff652882021-05-16 15:24:49 +02003229 if (expr_res == FAIL || trailing_error
3230 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003231 {
3232 if (trailing_error)
3233 semsg(_(e_trailing_arg), s);
3234 clear_instr_ga(&cctx->ctx_instr);
3235 cctx->ctx_instr = save_ga;
3236 return FAIL;
3237 }
3238
3239 // Move the generated instructions into the ISN_INSTR instruction, then
3240 // restore the list of instructions.
3241 instr_count = cctx->ctx_instr.ga_len;
3242 instr = cctx->ctx_instr.ga_data;
3243 instr[instr_count].isn_type = ISN_FINISH;
3244
3245 cctx->ctx_instr = save_ga;
3246 vim_free(isn->isn_arg.string);
3247 isn->isn_type = ISN_INSTR;
3248 isn->isn_arg.instr = instr;
3249 return OK;
3250}
3251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252/*
3253 * Compile the argument expressions.
3254 * "arg" points to just after the "(" and is advanced to after the ")"
3255 */
3256 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003257compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003259 char_u *p = *arg;
3260 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003261 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003262 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263
Bram Moolenaare6085c52020-04-12 20:19:16 +02003264 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003266 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3267 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003268 if (*p == ')')
3269 {
3270 *arg = p + 1;
3271 return OK;
3272 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003273 if (must_end)
3274 {
3275 semsg(_(e_missing_comma_before_argument_str), p);
3276 return FAIL;
3277 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003278
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003279 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003280 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 return FAIL;
3282 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003283
Bram Moolenaarff652882021-05-16 15:24:49 +02003284 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003285 && cctx->ctx_instr.ga_len == instr_count + 1)
3286 {
3287 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3288
3289 // {skip} argument of searchpair() can be compiled if not empty
3290 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3291 compile_string(isn, cctx);
3292 }
3293
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003294 if (*p != ',' && *skipwhite(p) == ',')
3295 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003296 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003297 p = skipwhite(p);
3298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003300 {
3301 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003302 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003303 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003304 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003305 else
3306 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003307 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003308 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003310failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003311 emsg(_(e_missing_close));
3312 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313}
3314
3315/*
3316 * Compile a function call: name(arg1, arg2)
3317 * "arg" points to "name", "arg + varlen" to the "(".
3318 * "argcount_init" is 1 for "value->method()"
3319 * Instructions:
3320 * EVAL arg1
3321 * EVAL arg2
3322 * BCALL / DCALL / UCALL
3323 */
3324 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003325compile_call(
3326 char_u **arg,
3327 size_t varlen,
3328 cctx_T *cctx,
3329 ppconst_T *ppconst,
3330 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331{
3332 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003333 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 int argcount = argcount_init;
3335 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003336 char_u fname_buf[FLEN_FIXED + 1];
3337 char_u *tofree = NULL;
3338 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003339 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003340 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003341 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003342 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343
Bram Moolenaara5565e42020-05-09 15:44:01 +02003344 // we can evaluate "has('name')" at compile time
3345 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3346 {
3347 char_u *s = skipwhite(*arg + varlen + 1);
3348 typval_T argvars[2];
3349
3350 argvars[0].v_type = VAR_UNKNOWN;
3351 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003352 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003353 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003354 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003355 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003356 if (*s == ')' && argvars[0].v_type == VAR_STRING
3357 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003358 {
3359 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3360
3361 *arg = s + 1;
3362 argvars[1].v_type = VAR_UNKNOWN;
3363 tv->v_type = VAR_NUMBER;
3364 tv->vval.v_number = 0;
3365 f_has(argvars, tv);
3366 clear_tv(&argvars[0]);
3367 ++ppconst->pp_used;
3368 return OK;
3369 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003370 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003371 }
3372
3373 if (generate_ppconst(cctx, ppconst) == FAIL)
3374 return FAIL;
3375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 if (varlen >= sizeof(namebuf))
3377 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003378 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 return FAIL;
3380 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003381 vim_strncpy(namebuf, *arg, varlen);
3382 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003384 // We handle the "skip" argument of searchpair() and searchpairpos()
3385 // differently.
3386 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3387 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3388 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3389 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003390
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003392 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003393 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394
Bram Moolenaar03290b82020-12-19 16:30:44 +01003395 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003396 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 {
3398 int idx;
3399
3400 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003401 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003403 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003404 if (STRCMP(name, "flatten") == 0)
3405 {
3406 emsg(_(e_cannot_use_flatten_in_vim9_script));
3407 goto theend;
3408 }
3409
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003410 if (STRCMP(name, "add") == 0 && argcount == 2)
3411 {
3412 garray_T *stack = &cctx->ctx_type_stack;
3413 type_T *type = ((type_T **)stack->ga_data)[
3414 stack->ga_len - 2];
3415
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003416 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003417 if (type->tt_type == VAR_LIST)
3418 {
3419 // inline "add(list, item)" so that the type can be checked
3420 res = generate_LISTAPPEND(cctx);
3421 idx = -1;
3422 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003423 else if (type->tt_type == VAR_BLOB)
3424 {
3425 // inline "add(blob, nr)" so that the type can be checked
3426 res = generate_BLOBAPPEND(cctx);
3427 idx = -1;
3428 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003429 }
3430
3431 if (idx >= 0)
3432 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3433 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003434 else
3435 semsg(_(e_unknownfunc), namebuf);
3436 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 }
3438
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003439 // An argument or local variable can be a function reference, this
3440 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003441 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003442 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003443 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003444 // If we can find the function by name generate the right call.
3445 // Skip global functions here, a local funcref takes precedence.
3446 ufunc = find_func(name, FALSE, cctx);
3447 if (ufunc != NULL && !func_is_global(ufunc))
3448 {
3449 res = generate_CALL(cctx, ufunc, argcount);
3450 goto theend;
3451 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453
3454 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003455 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003456 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003458 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003459 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003460 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003461 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003462 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003463
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003464 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003465 goto theend;
3466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467
Bram Moolenaar0f769812020-09-12 18:32:34 +02003468 // If we can find a global function by name generate the right call.
3469 if (ufunc != NULL)
3470 {
3471 res = generate_CALL(cctx, ufunc, argcount);
3472 goto theend;
3473 }
3474
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003475 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003476 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003477 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003478 res = generate_UCALL(cctx, name, argcount);
3479 else
3480 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003481
3482theend:
3483 vim_free(tofree);
3484 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485}
3486
3487// like NAMESPACE_CHAR but with 'a' and 'l'.
3488#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3489
3490/*
3491 * Find the end of a variable or function name. Unlike find_name_end() this
3492 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003493 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 * Return a pointer to just after the name. Equal to "arg" if there is no
3495 * valid name.
3496 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003497 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003498to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499{
3500 char_u *p;
3501
3502 // Quick check for valid starting character.
3503 if (!eval_isnamec1(*arg))
3504 return arg;
3505
3506 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3507 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3508 // and can be used in slice "[n:]".
3509 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003510 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3512 break;
3513 return p;
3514}
3515
3516/*
3517 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003518 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 */
3520 char_u *
3521to_name_const_end(char_u *arg)
3522{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003523 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 typval_T rettv;
3525
3526 if (p == arg && *arg == '[')
3527 {
3528
3529 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003530 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 p = arg;
3532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 return p;
3534}
3535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536/*
3537 * parse a list: [expr, expr]
3538 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003539 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 */
3541 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003542compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543{
3544 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003545 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003547 int is_const;
3548 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003550 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003552 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003553 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003554 semsg(_(e_list_end), *arg);
3555 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003556 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003557 if (*p == ',')
3558 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003559 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003560 return FAIL;
3561 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003562 if (*p == ']')
3563 {
3564 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003565 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003566 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003567 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003568 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003569 if (!is_const)
3570 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 ++count;
3572 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003573 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003575 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3576 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003577 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003578 return FAIL;
3579 }
3580 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003581 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 p = skipwhite(p);
3583 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003584 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003586 ppconst->pp_is_const = is_all_const;
3587 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588}
3589
3590/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003591 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003592 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003593 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 */
3595 static int
3596compile_lambda(char_u **arg, cctx_T *cctx)
3597{
Bram Moolenaare462f522020-12-27 14:43:30 +01003598 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 typval_T rettv;
3600 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003601 evalarg_T evalarg;
3602
3603 CLEAR_FIELD(evalarg);
3604 evalarg.eval_flags = EVAL_EVALUATE;
3605 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606
3607 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003608 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3609 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003610 {
3611 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003612 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003613 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003614
Bram Moolenaar65c44152020-12-24 15:14:01 +01003615 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003617 ++ufunc->uf_refcount;
3618 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619
Bram Moolenaara9931532021-06-12 15:58:16 +02003620 // Compile it here to get the return type. The return type is optional,
3621 // when it's missing use t_unknown. This is recognized in
3622 // compile_return().
3623 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3624 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003625 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626
Bram Moolenaard9162552021-07-11 15:26:13 +02003627 // When the outer function is compiled for profiling, the lambda may be
3628 // called without profiling. Compile it here in the right context.
3629 if (cctx->ctx_compile_type == CT_PROFILE)
3630 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
3631
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003632 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3633 // points into it. Point to the original line to avoid a dangling pointer.
3634 if (evalarg.eval_tofree_cmdline != NULL)
3635 {
3636 size_t off = *arg - evalarg.eval_tofree_cmdline;
3637
3638 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3639 + off;
3640 }
3641
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003642 clear_evalarg(&evalarg, NULL);
3643
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003644 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003645 {
3646 // The return type will now be known.
3647 set_function_type(ufunc);
3648
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003649 // The function reference count will be 1. When the ISN_FUNCREF
3650 // instruction is deleted the reference count is decremented and the
3651 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003652 return generate_FUNCREF(cctx, ufunc);
3653 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003654
3655 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 return FAIL;
3657}
3658
3659/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003660 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003662 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 */
3664 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003665compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666{
3667 garray_T *instr = &cctx->ctx_instr;
3668 int count = 0;
3669 dict_T *d = dict_alloc();
3670 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003671 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003672 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003673 int is_const;
3674 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675
3676 if (d == NULL)
3677 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003678 if (generate_ppconst(cctx, ppconst) == FAIL)
3679 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003680 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003682 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683
Bram Moolenaar23c55272020-06-21 16:58:13 +02003684 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003685 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003686 *arg = NULL;
3687 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003688 }
3689
3690 if (**arg == '}')
3691 break;
3692
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003693 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003695 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003697 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003698 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003699 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003702 if (isn->isn_type == ISN_PUSHNR)
3703 {
3704 char buf[NUMBUFLEN];
3705
3706 // Convert to string at compile time.
3707 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3708 isn->isn_type = ISN_PUSHS;
3709 isn->isn_arg.string = vim_strsave((char_u *)buf);
3710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 if (isn->isn_type == ISN_PUSHS)
3712 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003713 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003714 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003715 *arg = skipwhite(*arg);
3716 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003717 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003718 emsg(_(e_missing_matching_bracket_after_dict_key));
3719 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003720 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003721 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003723 else
3724 {
3725 // {"name": value},
3726 // {'name': value},
3727 // {name: value} use "name" as a literal key
3728 key = get_literal_key(arg);
3729 if (key == NULL)
3730 return FAIL;
3731 if (generate_PUSHS(cctx, key) == FAIL)
3732 return FAIL;
3733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734
3735 // Check for duplicate keys, if using string keys.
3736 if (key != NULL)
3737 {
3738 item = dict_find(d, key, -1);
3739 if (item != NULL)
3740 {
3741 semsg(_(e_duplicate_key), key);
3742 goto failret;
3743 }
3744 item = dictitem_alloc(key);
3745 if (item != NULL)
3746 {
3747 item->di_tv.v_type = VAR_UNKNOWN;
3748 item->di_tv.v_lock = 0;
3749 if (dict_add(d, item) == FAIL)
3750 dictitem_free(item);
3751 }
3752 }
3753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 if (**arg != ':')
3755 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003756 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003757 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003758 else
3759 semsg(_(e_missing_dict_colon), *arg);
3760 return FAIL;
3761 }
3762 whitep = *arg + 1;
3763 if (!IS_WHITE_OR_NUL(*whitep))
3764 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003765 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 return FAIL;
3767 }
3768
Bram Moolenaar23c55272020-06-21 16:58:13 +02003769 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003770 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003771 *arg = NULL;
3772 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003773 }
3774
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003775 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003777 if (!is_const)
3778 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 ++count;
3780
Bram Moolenaar2c330432020-04-13 14:41:35 +02003781 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003782 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003783 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003784 *arg = NULL;
3785 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 if (**arg == '}')
3788 break;
3789 if (**arg != ',')
3790 {
3791 semsg(_(e_missing_dict_comma), *arg);
3792 goto failret;
3793 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003794 if (IS_WHITE_OR_NUL(*whitep))
3795 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003796 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003797 return FAIL;
3798 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003799 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003800 if (!IS_WHITE_OR_NUL(*whitep))
3801 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003802 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003803 return FAIL;
3804 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003805 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 }
3807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 *arg = *arg + 1;
3809
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003810 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003811 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003812 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003813 *arg += STRLEN(*arg);
3814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003816 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 return generate_NEWDICT(cctx, count);
3818
3819failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003820 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003821 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003822 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003823 *arg = (char_u *)"";
3824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 dict_unref(d);
3826 return FAIL;
3827}
3828
3829/*
3830 * Compile "&option".
3831 */
3832 static int
3833compile_get_option(char_u **arg, cctx_T *cctx)
3834{
3835 typval_T rettv;
3836 char_u *start = *arg;
3837 int ret;
3838
3839 // parse the option and get the current value to get the type.
3840 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003841 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 if (ret == OK)
3843 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003844 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003845 char_u *name = vim_strnsave(start, *arg - start);
3846 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3847 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848
3849 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3850 vim_free(name);
3851 }
3852 clear_tv(&rettv);
3853
3854 return ret;
3855}
3856
3857/*
3858 * Compile "$VAR".
3859 */
3860 static int
3861compile_get_env(char_u **arg, cctx_T *cctx)
3862{
3863 char_u *start = *arg;
3864 int len;
3865 int ret;
3866 char_u *name;
3867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 ++*arg;
3869 len = get_env_len(arg);
3870 if (len == 0)
3871 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003872 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 return FAIL;
3874 }
3875
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003876 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 name = vim_strnsave(start, len + 1);
3878 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3879 vim_free(name);
3880 return ret;
3881}
3882
3883/*
3884 * Compile "@r".
3885 */
3886 static int
3887compile_get_register(char_u **arg, cctx_T *cctx)
3888{
3889 int ret;
3890
3891 ++*arg;
3892 if (**arg == NUL)
3893 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003894 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 return FAIL;
3896 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003897 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 {
3899 emsg_invreg(**arg);
3900 return FAIL;
3901 }
3902 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3903 ++*arg;
3904 return ret;
3905}
3906
3907/*
3908 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003909 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 */
3911 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003912apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003914 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915
3916 // this works from end to start
3917 while (p > start)
3918 {
3919 --p;
3920 if (*p == '-' || *p == '+')
3921 {
3922 // only '-' has an effect, for '+' we only check the type
3923#ifdef FEAT_FLOAT
3924 if (rettv->v_type == VAR_FLOAT)
3925 {
3926 if (*p == '-')
3927 rettv->vval.v_float = -rettv->vval.v_float;
3928 }
3929 else
3930#endif
3931 {
3932 varnumber_T val;
3933 int error = FALSE;
3934
3935 // tv_get_number_chk() accepts a string, but we don't want that
3936 // here
3937 if (check_not_string(rettv) == FAIL)
3938 return FAIL;
3939 val = tv_get_number_chk(rettv, &error);
3940 clear_tv(rettv);
3941 if (error)
3942 return FAIL;
3943 if (*p == '-')
3944 val = -val;
3945 rettv->v_type = VAR_NUMBER;
3946 rettv->vval.v_number = val;
3947 }
3948 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003949 else if (numeric_only)
3950 {
3951 ++p;
3952 break;
3953 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003954 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 {
3956 int v = tv2bool(rettv);
3957
3958 // '!' is permissive in the type.
3959 clear_tv(rettv);
3960 rettv->v_type = VAR_BOOL;
3961 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3962 }
3963 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003964 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 return OK;
3966}
3967
3968/*
3969 * Recognize v: variables that are constants and set "rettv".
3970 */
3971 static void
3972get_vim_constant(char_u **arg, typval_T *rettv)
3973{
3974 if (STRNCMP(*arg, "v:true", 6) == 0)
3975 {
3976 rettv->v_type = VAR_BOOL;
3977 rettv->vval.v_number = VVAL_TRUE;
3978 *arg += 6;
3979 }
3980 else if (STRNCMP(*arg, "v:false", 7) == 0)
3981 {
3982 rettv->v_type = VAR_BOOL;
3983 rettv->vval.v_number = VVAL_FALSE;
3984 *arg += 7;
3985 }
3986 else if (STRNCMP(*arg, "v:null", 6) == 0)
3987 {
3988 rettv->v_type = VAR_SPECIAL;
3989 rettv->vval.v_number = VVAL_NULL;
3990 *arg += 6;
3991 }
3992 else if (STRNCMP(*arg, "v:none", 6) == 0)
3993 {
3994 rettv->v_type = VAR_SPECIAL;
3995 rettv->vval.v_number = VVAL_NONE;
3996 *arg += 6;
3997 }
3998}
3999
Bram Moolenaar657137c2021-01-09 15:45:23 +01004000 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004001get_compare_type(char_u *p, int *len, int *type_is)
4002{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004003 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004004 int i;
4005
4006 switch (p[0])
4007 {
4008 case '=': if (p[1] == '=')
4009 type = EXPR_EQUAL;
4010 else if (p[1] == '~')
4011 type = EXPR_MATCH;
4012 break;
4013 case '!': if (p[1] == '=')
4014 type = EXPR_NEQUAL;
4015 else if (p[1] == '~')
4016 type = EXPR_NOMATCH;
4017 break;
4018 case '>': if (p[1] != '=')
4019 {
4020 type = EXPR_GREATER;
4021 *len = 1;
4022 }
4023 else
4024 type = EXPR_GEQUAL;
4025 break;
4026 case '<': if (p[1] != '=')
4027 {
4028 type = EXPR_SMALLER;
4029 *len = 1;
4030 }
4031 else
4032 type = EXPR_SEQUAL;
4033 break;
4034 case 'i': if (p[1] == 's')
4035 {
4036 // "is" and "isnot"; but not a prefix of a name
4037 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4038 *len = 5;
4039 i = p[*len];
4040 if (!isalnum(i) && i != '_')
4041 {
4042 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4043 *type_is = TRUE;
4044 }
4045 }
4046 break;
4047 }
4048 return type;
4049}
4050
4051/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004052 * Skip over an expression, ignoring most errors.
4053 */
4054 static void
4055skip_expr_cctx(char_u **arg, cctx_T *cctx)
4056{
4057 evalarg_T evalarg;
4058
4059 CLEAR_FIELD(evalarg);
4060 evalarg.eval_cctx = cctx;
4061 skip_expr(arg, &evalarg);
4062}
4063
4064/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004066 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 */
4068 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004069compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004071 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072
4073 // this works from end to start
4074 while (p > start)
4075 {
4076 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004077 while (VIM_ISWHITE(*p))
4078 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 if (*p == '-' || *p == '+')
4080 {
4081 int negate = *p == '-';
4082 isn_T *isn;
4083
4084 // TODO: check type
4085 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4086 {
4087 --p;
4088 if (*p == '-')
4089 negate = !negate;
4090 }
4091 // only '-' has an effect, for '+' we only check the type
4092 if (negate)
4093 isn = generate_instr(cctx, ISN_NEGATENR);
4094 else
4095 isn = generate_instr(cctx, ISN_CHECKNR);
4096 if (isn == NULL)
4097 return FAIL;
4098 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004099 else if (numeric_only)
4100 {
4101 ++p;
4102 break;
4103 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 else
4105 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004106 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004108 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004110 if (p[-1] == '!')
4111 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004114 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 return FAIL;
4116 }
4117 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004118 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 return OK;
4120}
4121
4122/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004123 * Compile "(expression)": recursive!
4124 * Return FAIL/OK.
4125 */
4126 static int
4127compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4128{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004129 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004130 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004131
Bram Moolenaar24156692021-01-14 20:35:49 +01004132 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4133 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004134 if (ppconst->pp_used <= PPSIZE - 10)
4135 {
4136 ret = compile_expr1(arg, cctx, ppconst);
4137 }
4138 else
4139 {
4140 // Not enough space in ppconst, flush constants.
4141 if (generate_ppconst(cctx, ppconst) == FAIL)
4142 return FAIL;
4143 ret = compile_expr0(arg, cctx);
4144 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004145 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4146 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004147 if (**arg == ')')
4148 ++*arg;
4149 else if (ret == OK)
4150 {
4151 emsg(_(e_missing_close));
4152 ret = FAIL;
4153 }
4154 return ret;
4155}
4156
4157/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004159 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 */
4161 static int
4162compile_subscript(
4163 char_u **arg,
4164 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004165 char_u *start_leader,
4166 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004167 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004169 char_u *name_start = *end_leader;
4170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 for (;;)
4172 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004173 char_u *p = skipwhite(*arg);
4174
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004175 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004176 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004177 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004178
4179 // If a following line starts with "->{" or "->X" advance to that
4180 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004181 // Also if a following line starts with ".x".
4182 if (next != NULL &&
4183 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004184 && (next[2] == '{'
4185 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004186 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004187 {
4188 next = next_line_from_context(cctx, TRUE);
4189 if (next == NULL)
4190 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004191 *arg = next;
4192 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004193 }
4194 }
4195
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004196 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004197 // not a function call.
4198 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004200 garray_T *stack = &cctx->ctx_type_stack;
4201 type_T *type;
4202 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004204 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004205 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004206 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004209 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4210
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004211 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004212 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004214 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 return FAIL;
4216 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004217 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004219 char_u *pstart = p;
4220
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004221 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004222 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004223 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 // something->method()
4226 // Apply the '!', '-' and '+' first:
4227 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004228 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004231 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004232 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004233 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004234 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004235 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004236 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004237 garray_T *stack = &cctx->ctx_type_stack;
4238 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004239 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004240 int expr_isn_start = cctx->ctx_instr.ga_len;
4241 int expr_isn_end;
4242 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004243
4244 // Funcref call: list->(Refs[2])(arg)
4245 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004246 //
4247 // Fist compile the function expression.
4248 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004249 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004250
4251 // Remember the next instruction index, where the instructions
4252 // for arguments are being written.
4253 expr_isn_end = cctx->ctx_instr.ga_len;
4254
4255 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004256 if (**arg != '(')
4257 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004258 if (*skipwhite(*arg) == '(')
4259 emsg(_(e_nowhitespace));
4260 else
4261 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004262 return FAIL;
4263 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004264 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004265 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004266 return FAIL;
4267
Bram Moolenaar2927c072021-04-05 19:41:21 +02004268 // Move the instructions for the arguments to before the
4269 // instructions of the expression and move the type of the
4270 // expression after the argument types. This is what ISN_PCALL
4271 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004272 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004273 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4274 if (arg_isn_count > 0)
4275 {
4276 int expr_isn_count = expr_isn_end - expr_isn_start;
4277 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4278
4279 if (isn == NULL)
4280 return FAIL;
4281 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4282 + expr_isn_start,
4283 sizeof(isn_T) * expr_isn_count);
4284 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4285 + expr_isn_start,
4286 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4287 sizeof(isn_T) * arg_isn_count);
4288 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4289 + expr_isn_start + arg_isn_count,
4290 isn, sizeof(isn_T) * expr_isn_count);
4291 vim_free(isn);
4292
4293 type = ((type_T **)stack->ga_data)[type_idx_start];
4294 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4295 ((type_T **)stack->ga_data) + type_idx_start + 1,
4296 sizeof(type_T *)
4297 * (stack->ga_len - type_idx_start - 1));
4298 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4299 }
4300
Bram Moolenaar7e368202020-12-25 21:56:57 +01004301 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4302 if (generate_PCALL(cctx, argcount,
4303 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 return FAIL;
4305 }
4306 else
4307 {
4308 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004309 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004310 if (!eval_isnamec1(*p))
4311 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004312 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004313 return FAIL;
4314 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004315 if (ASCII_ISALPHA(*p) && p[1] == ':')
4316 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004317 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 ;
4319 if (*p != '(')
4320 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004321 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 return FAIL;
4323 }
4324 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004325 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 return FAIL;
4327 }
4328 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004329 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004331 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004332
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004333 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004334 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004335 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004336 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004337 // TODO: more arguments
4338 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004339 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004340 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004341 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004342
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004343 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004344 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004345 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004346 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004347 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004348 // missing first index is equal to zero
4349 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004350 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004351 else
4352 {
4353 if (compile_expr0(arg, cctx) == FAIL)
4354 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004355 if (**arg == ':')
4356 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004357 semsg(_(e_white_space_required_before_and_after_str_at_str),
4358 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004359 return FAIL;
4360 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004361 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004362 return FAIL;
4363 *arg = skipwhite(*arg);
4364 }
4365 if (**arg == ':')
4366 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004367 is_slice = TRUE;
4368 ++*arg;
4369 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4370 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004371 semsg(_(e_white_space_required_before_and_after_str_at_str),
4372 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004373 return FAIL;
4374 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004375 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004376 return FAIL;
4377 if (**arg == ']')
4378 // missing second index is equal to end of string
4379 generate_PUSHNR(cctx, -1);
4380 else
4381 {
4382 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004383 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004384 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004385 return FAIL;
4386 *arg = skipwhite(*arg);
4387 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389
4390 if (**arg != ']')
4391 {
4392 emsg(_(e_missbrac));
4393 return FAIL;
4394 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004395 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396
Bram Moolenaare42939a2021-04-05 17:11:17 +02004397 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004398 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004400 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004402 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004403 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004404 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004405 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004406
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004407 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004408 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004409 {
4410 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004411 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004412 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004413 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004414 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 while (eval_isnamec(*p))
4416 MB_PTR_ADV(p);
4417 if (p == *arg)
4418 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004419 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 return FAIL;
4421 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004422 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 return FAIL;
4424 *arg = p;
4425 }
4426 else
4427 break;
4428 }
4429
4430 // TODO - see handle_subscript():
4431 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4432 // Don't do this when "Func" is already a partial that was bound
4433 // explicitly (pt_auto is FALSE).
4434
4435 return OK;
4436}
4437
4438/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004439 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4440 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004442 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004443 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004444 *
4445 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 */
4447
4448/*
4449 * number number constant
4450 * 0zFFFFFFFF Blob constant
4451 * "string" string constant
4452 * 'string' literal string constant
4453 * &option-name option value
4454 * @r register contents
4455 * identifier variable value
4456 * function() function call
4457 * $VAR environment variable
4458 * (expression) nested expression
4459 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004460 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 *
4462 * Also handle:
4463 * ! in front logical NOT
4464 * - in front unary minus
4465 * + in front unary plus (ignored)
4466 * trailing (arg) funcref/partial call
4467 * trailing [] subscript in String or List
4468 * trailing .name entry in Dictionary
4469 * trailing ->name() method call
4470 */
4471 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004472compile_expr7(
4473 char_u **arg,
4474 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004475 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477 char_u *start_leader, *end_leader;
4478 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004479 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004480 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004482 ppconst->pp_is_const = FALSE;
4483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 /*
4485 * Skip '!', '-' and '+' characters. They are handled later.
4486 */
4487 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004488 if (eval_leader(arg, TRUE) == FAIL)
4489 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 end_leader = *arg;
4491
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004492 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 switch (**arg)
4494 {
4495 /*
4496 * Number constant.
4497 */
4498 case '0': // also for blob starting with 0z
4499 case '1':
4500 case '2':
4501 case '3':
4502 case '4':
4503 case '5':
4504 case '6':
4505 case '7':
4506 case '8':
4507 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004508 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004510 // Apply "-" and "+" just before the number now, right to
4511 // left. Matters especially when "->" follows. Stops at
4512 // '!'.
4513 if (apply_leader(rettv, TRUE,
4514 start_leader, &end_leader) == FAIL)
4515 {
4516 clear_tv(rettv);
4517 return FAIL;
4518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 break;
4520
4521 /*
4522 * String constant: "string".
4523 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004524 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 return FAIL;
4526 break;
4527
4528 /*
4529 * Literal string constant: 'str''ing'.
4530 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004531 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 return FAIL;
4533 break;
4534
4535 /*
4536 * Constant Vim variable.
4537 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004538 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 ret = NOTDONE;
4540 break;
4541
4542 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004543 * "true" constant
4544 */
4545 case 't': if (STRNCMP(*arg, "true", 4) == 0
4546 && !eval_isnamec((*arg)[4]))
4547 {
4548 *arg += 4;
4549 rettv->v_type = VAR_BOOL;
4550 rettv->vval.v_number = VVAL_TRUE;
4551 }
4552 else
4553 ret = NOTDONE;
4554 break;
4555
4556 /*
4557 * "false" constant
4558 */
4559 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4560 && !eval_isnamec((*arg)[5]))
4561 {
4562 *arg += 5;
4563 rettv->v_type = VAR_BOOL;
4564 rettv->vval.v_number = VVAL_FALSE;
4565 }
4566 else
4567 ret = NOTDONE;
4568 break;
4569
4570 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004571 * "null" constant
4572 */
4573 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004574 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004575 {
4576 *arg += 4;
4577 rettv->v_type = VAR_SPECIAL;
4578 rettv->vval.v_number = VVAL_NULL;
4579 }
4580 else
4581 ret = NOTDONE;
4582 break;
4583
4584 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 * List: [expr, expr]
4586 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004587 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4588 return FAIL;
4589 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 break;
4591
4592 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593 * Dictionary: {'key': val, 'key': val}
4594 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004595 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4596 return FAIL;
4597 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 break;
4599
4600 /*
4601 * Option value: &name
4602 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004603 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4604 return FAIL;
4605 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 break;
4607
4608 /*
4609 * Environment variable: $VAR.
4610 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004611 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4612 return FAIL;
4613 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 break;
4615
4616 /*
4617 * Register contents: @r.
4618 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004619 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4620 return FAIL;
4621 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 break;
4623 /*
4624 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004625 * lambda: (arg, arg) => expr
4626 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004628 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4629 ret = compile_lambda(arg, cctx);
4630 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004631 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 break;
4633
4634 default: ret = NOTDONE;
4635 break;
4636 }
4637 if (ret == FAIL)
4638 return FAIL;
4639
Bram Moolenaar1c747212020-05-09 18:28:34 +02004640 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004642 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004643 clear_tv(rettv);
4644 else
4645 // A constant expression can possibly be handled compile time,
4646 // return the value instead of generating code.
4647 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 }
4649 else if (ret == NOTDONE)
4650 {
4651 char_u *p;
4652 int r;
4653
4654 if (!eval_isnamec1(**arg))
4655 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004656 if (!vim9_bad_comment(*arg))
4657 {
4658 if (ends_excmd(*skipwhite(*arg)))
4659 semsg(_(e_empty_expression_str), *arg);
4660 else
4661 semsg(_(e_name_expected_str), *arg);
4662 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 return FAIL;
4664 }
4665
4666 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004667 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004668 if (p - *arg == (size_t)1 && **arg == '_')
4669 {
4670 emsg(_(e_cannot_use_underscore_here));
4671 return FAIL;
4672 }
4673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004675 {
4676 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4677 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004679 {
4680 if (generate_ppconst(cctx, ppconst) == FAIL)
4681 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004682 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004683 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 if (r == FAIL)
4685 return FAIL;
4686 }
4687
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004688 // Handle following "[]", ".member", etc.
4689 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004690 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004691 ppconst) == FAIL)
4692 return FAIL;
4693 if (ppconst->pp_used > 0)
4694 {
4695 // apply the '!', '-' and '+' before the constant
4696 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004697 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004698 return FAIL;
4699 return OK;
4700 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004701 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004703 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704}
4705
4706/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004707 * Give the "white on both sides" error, taking the operator from "p[len]".
4708 */
4709 void
4710error_white_both(char_u *op, int len)
4711{
4712 char_u buf[10];
4713
4714 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004715 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004716}
4717
4718/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004719 * <type>expr7: runtime type check / conversion
4720 */
4721 static int
4722compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4723{
4724 type_T *want_type = NULL;
4725
4726 // Recognize <type>
4727 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4728 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004729 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004730 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4731 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004732 return FAIL;
4733
4734 if (**arg != '>')
4735 {
4736 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004737 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004738 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004739 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004740 return FAIL;
4741 }
4742 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004743 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004744 return FAIL;
4745 }
4746
4747 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4748 return FAIL;
4749
4750 if (want_type != NULL)
4751 {
4752 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004753 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004754 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004755
Bram Moolenaard1103582020-08-14 22:44:25 +02004756 generate_ppconst(cctx, ppconst);
4757 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004758 where.wt_index = 0;
4759 where.wt_variable = FALSE;
4760 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004761 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004762 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004763 return FAIL;
4764 }
4765 }
4766
4767 return OK;
4768}
4769
4770/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 * * number multiplication
4772 * / number division
4773 * % number modulo
4774 */
4775 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004776compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777{
4778 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004779 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004780 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004782 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004783 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 return FAIL;
4785
4786 /*
4787 * Repeat computing, until no "*", "/" or "%" is following.
4788 */
4789 for (;;)
4790 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004791 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 if (*op != '*' && *op != '/' && *op != '%')
4793 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004794 if (next != NULL)
4795 {
4796 *arg = next_line_from_context(cctx, TRUE);
4797 op = skipwhite(*arg);
4798 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004799
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004800 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004802 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004803 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004805 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004806 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004808 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004809 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004811
4812 if (ppconst->pp_used == ppconst_used + 2
4813 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4814 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004815 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004816 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4817 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4818 varnumber_T res = 0;
4819 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004821 // both are numbers: compute the result
4822 switch (*op)
4823 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004824 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004825 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004826 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004827 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004828 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004829 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004830 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004831 break;
4832 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004833 if (failed)
4834 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004835 tv1->vval.v_number = res;
4836 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004837 }
4838 else
4839 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004840 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004841 generate_two_op(cctx, op);
4842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 }
4844
4845 return OK;
4846}
4847
4848/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004849 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 * - number subtraction
4851 * .. string concatenation
4852 */
4853 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004854compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855{
4856 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004857 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004859 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860
4861 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004862 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 return FAIL;
4864
4865 /*
4866 * Repeat computing, until no "+", "-" or ".." is following.
4867 */
4868 for (;;)
4869 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004870 op = may_peek_next_line(cctx, *arg, &next);
4871 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004873 if (op[0] == op[1] && *op != '.' && next)
4874 // Finding "++" or "--" on the next line is a separate command.
4875 // But ".." is concatenation.
4876 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004878 if (next != NULL)
4879 {
4880 *arg = next_line_from_context(cctx, TRUE);
4881 op = skipwhite(*arg);
4882 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004884 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004886 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004887 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 }
4889
Bram Moolenaare0de1712020-12-02 17:36:54 +01004890 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004891 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004893 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004894 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 return FAIL;
4896
Bram Moolenaara5565e42020-05-09 15:44:01 +02004897 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004898 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004899 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4900 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4901 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4902 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004904 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4905 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004906
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004907 // concat/subtract/add constant numbers
4908 if (*op == '+')
4909 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4910 else if (*op == '-')
4911 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4912 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004913 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004914 // concatenate constant strings
4915 char_u *s1 = tv1->vval.v_string;
4916 char_u *s2 = tv2->vval.v_string;
4917 size_t len1 = STRLEN(s1);
4918
4919 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4920 if (tv1->vval.v_string == NULL)
4921 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004922 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004923 return FAIL;
4924 }
4925 mch_memmove(tv1->vval.v_string, s1, len1);
4926 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004927 vim_free(s1);
4928 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004929 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004930 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 }
4932 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004933 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004934 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004935 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004936 if (*op == '.')
4937 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004938 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4939 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004940 return FAIL;
4941 generate_instr_drop(cctx, ISN_CONCAT, 1);
4942 }
4943 else
4944 generate_two_op(cctx, op);
4945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946 }
4947
4948 return OK;
4949}
4950
4951/*
4952 * expr5a == expr5b
4953 * expr5a =~ expr5b
4954 * expr5a != expr5b
4955 * expr5a !~ expr5b
4956 * expr5a > expr5b
4957 * expr5a >= expr5b
4958 * expr5a < expr5b
4959 * expr5a <= expr5b
4960 * expr5a is expr5b
4961 * expr5a isnot expr5b
4962 *
4963 * Produces instructions:
4964 * EVAL expr5a Push result of "expr5a"
4965 * EVAL expr5b Push result of "expr5b"
4966 * COMPARE one of the compare instructions
4967 */
4968 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004969compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004971 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004973 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004976 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977
4978 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004979 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980 return FAIL;
4981
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004982 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004983 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984
4985 /*
4986 * If there is a comparative operator, use it.
4987 */
4988 if (type != EXPR_UNKNOWN)
4989 {
4990 int ic = FALSE; // Default: do not ignore case
4991
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004992 if (next != NULL)
4993 {
4994 *arg = next_line_from_context(cctx, TRUE);
4995 p = skipwhite(*arg);
4996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997 if (type_is && (p[len] == '?' || p[len] == '#'))
4998 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004999 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 return FAIL;
5001 }
5002 // extra question mark appended: ignore case
5003 if (p[len] == '?')
5004 {
5005 ic = TRUE;
5006 ++len;
5007 }
5008 // extra '#' appended: match case (ignored)
5009 else if (p[len] == '#')
5010 ++len;
5011 // nothing appended: match case
5012
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005013 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005015 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005016 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 }
5018
5019 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005020 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005021 return FAIL;
5022
Bram Moolenaara5565e42020-05-09 15:44:01 +02005023 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024 return FAIL;
5025
Bram Moolenaara5565e42020-05-09 15:44:01 +02005026 if (ppconst->pp_used == ppconst_used + 2)
5027 {
5028 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5029 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5030 int ret;
5031
5032 // Both sides are a constant, compute the result now.
5033 // First check for a valid combination of types, this is more
5034 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005035 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005036 ret = FAIL;
5037 else
5038 {
5039 ret = typval_compare(tv1, tv2, type, ic);
5040 tv1->v_type = VAR_BOOL;
5041 tv1->vval.v_number = tv1->vval.v_number
5042 ? VVAL_TRUE : VVAL_FALSE;
5043 clear_tv(tv2);
5044 --ppconst->pp_used;
5045 }
5046 return ret;
5047 }
5048
5049 generate_ppconst(cctx, ppconst);
5050 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 }
5052
5053 return OK;
5054}
5055
Bram Moolenaar7f141552020-05-09 17:35:53 +02005056static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058/*
5059 * Compile || or &&.
5060 */
5061 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005062compile_and_or(
5063 char_u **arg,
5064 cctx_T *cctx,
5065 char *op,
5066 ppconst_T *ppconst,
5067 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005069 char_u *next;
5070 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071 int opchar = *op;
5072
5073 if (p[0] == opchar && p[1] == opchar)
5074 {
5075 garray_T *instr = &cctx->ctx_instr;
5076 garray_T end_ga;
5077
5078 /*
5079 * Repeat until there is no following "||" or "&&"
5080 */
5081 ga_init2(&end_ga, sizeof(int), 10);
5082 while (p[0] == opchar && p[1] == opchar)
5083 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005084 long start_lnum = SOURCING_LNUM;
5085 int start_ctx_lnum = cctx->ctx_lnum;
5086 int save_lnum;
5087
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005088 if (next != NULL)
5089 {
5090 *arg = next_line_from_context(cctx, TRUE);
5091 p = skipwhite(*arg);
5092 }
5093
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005094 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5095 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005096 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005097 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005098 return FAIL;
5099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005101 // TODO: use ppconst if the value is a constant and check
5102 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005103 generate_ppconst(cctx, ppconst);
5104
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005105 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005106 SOURCING_LNUM = start_lnum;
5107 save_lnum = cctx->ctx_lnum;
5108 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005109 if (bool_on_stack(cctx) == FAIL)
5110 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005111 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005112 ga_clear(&end_ga);
5113 return FAIL;
5114 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005115 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117 if (ga_grow(&end_ga, 1) == FAIL)
5118 {
5119 ga_clear(&end_ga);
5120 return FAIL;
5121 }
5122 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5123 ++end_ga.ga_len;
5124 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005125 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126
5127 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005128 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005129 {
5130 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005131 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005132 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005133
Bram Moolenaara5565e42020-05-09 15:44:01 +02005134 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5135 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005136 {
5137 ga_clear(&end_ga);
5138 return FAIL;
5139 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005140
5141 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005143 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005145 // Every part must evaluate to a bool.
5146 if (bool_on_stack(cctx) == FAIL)
5147 {
5148 ga_clear(&end_ga);
5149 return FAIL;
5150 }
5151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152 // Fill in the end label in all jumps.
5153 while (end_ga.ga_len > 0)
5154 {
5155 isn_T *isn;
5156
5157 --end_ga.ga_len;
5158 isn = ((isn_T *)instr->ga_data)
5159 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5160 isn->isn_arg.jump.jump_where = instr->ga_len;
5161 }
5162 ga_clear(&end_ga);
5163 }
5164
5165 return OK;
5166}
5167
5168/*
5169 * expr4a && expr4a && expr4a logical AND
5170 *
5171 * Produces instructions:
5172 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005173 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005174 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005175 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005176 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 * EVAL expr4c Push result of "expr4c"
5178 * end:
5179 */
5180 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005181compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005183 int ppconst_used = ppconst->pp_used;
5184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005185 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005186 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005187 return FAIL;
5188
5189 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005190 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005191}
5192
5193/*
5194 * expr3a || expr3b || expr3c logical OR
5195 *
5196 * Produces instructions:
5197 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005198 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005199 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005201 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 * EVAL expr3c Push result of "expr3c"
5203 * end:
5204 */
5205 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005206compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005208 int ppconst_used = ppconst->pp_used;
5209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005211 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 return FAIL;
5213
5214 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005215 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216}
5217
5218/*
5219 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005221 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 * JUMP_IF_FALSE alt jump if false
5223 * EVAL expr1a
5224 * JUMP_ALWAYS end
5225 * alt: EVAL expr1b
5226 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005227 *
5228 * Toplevel expression: expr2 ?? expr1
5229 * Produces instructions:
5230 * EVAL expr2 Push result of "expr2"
5231 * JUMP_AND_KEEP_IF_TRUE end jump if true
5232 * EVAL expr1
5233 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234 */
5235 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005236compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237{
5238 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005239 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005240 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241
Bram Moolenaar3988f642020-08-27 22:43:03 +02005242 // Ignore all kinds of errors when not producing code.
5243 if (cctx->ctx_skip == SKIP_YES)
5244 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005245 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005246 return OK;
5247 }
5248
Bram Moolenaar61a89812020-05-07 16:58:17 +02005249 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005250 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251 return FAIL;
5252
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005253 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254 if (*p == '?')
5255 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005256 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005257 garray_T *instr = &cctx->ctx_instr;
5258 garray_T *stack = &cctx->ctx_type_stack;
5259 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005260 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005262 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005263 int has_const_expr = FALSE;
5264 int const_value = FALSE;
5265 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005266
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005267 if (next != NULL)
5268 {
5269 *arg = next_line_from_context(cctx, TRUE);
5270 p = skipwhite(*arg);
5271 }
5272
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005273 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005274 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005275 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005276 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005277 return FAIL;
5278 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279
Bram Moolenaara5565e42020-05-09 15:44:01 +02005280 if (ppconst->pp_used == ppconst_used + 1)
5281 {
5282 // the condition is a constant, we know whether the ? or the :
5283 // expression is to be evaluated.
5284 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005285 if (op_falsy)
5286 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5287 else
5288 {
5289 int error = FALSE;
5290
5291 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5292 &error);
5293 if (error)
5294 return FAIL;
5295 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005296 cctx->ctx_skip = save_skip == SKIP_YES ||
5297 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5298
5299 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5300 // "left ?? right" and "left" is truthy: produce "left"
5301 generate_ppconst(cctx, ppconst);
5302 else
5303 {
5304 clear_tv(&ppconst->pp_tv[ppconst_used]);
5305 --ppconst->pp_used;
5306 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005307 }
5308 else
5309 {
5310 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005311 if (op_falsy)
5312 end_idx = instr->ga_len;
5313 generate_JUMP(cctx, op_falsy
5314 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5315 if (op_falsy)
5316 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318
5319 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005320 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005321 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005322 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005323 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324
Bram Moolenaara5565e42020-05-09 15:44:01 +02005325 if (!has_const_expr)
5326 {
5327 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005329 if (!op_falsy)
5330 {
5331 // remember the type and drop it
5332 --stack->ga_len;
5333 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005334
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005335 end_idx = instr->ga_len;
5336 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005337
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005338 // jump here from JUMP_IF_FALSE
5339 isn = ((isn_T *)instr->ga_data) + alt_idx;
5340 isn->isn_arg.jump.jump_where = instr->ga_len;
5341 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005342 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005343
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005344 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005346 // Check for the ":".
5347 p = may_peek_next_line(cctx, *arg, &next);
5348 if (*p != ':')
5349 {
5350 emsg(_(e_missing_colon));
5351 return FAIL;
5352 }
5353 if (next != NULL)
5354 {
5355 *arg = next_line_from_context(cctx, TRUE);
5356 p = skipwhite(*arg);
5357 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005358
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005359 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5360 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005361 semsg(_(e_white_space_required_before_and_after_str_at_str),
5362 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005363 return FAIL;
5364 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005366 // evaluate the third expression
5367 if (has_const_expr)
5368 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005369 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005370 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005371 return FAIL;
5372 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5373 return FAIL;
5374 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005375
Bram Moolenaara5565e42020-05-09 15:44:01 +02005376 if (!has_const_expr)
5377 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005378 type_T **typep;
5379
Bram Moolenaara5565e42020-05-09 15:44:01 +02005380 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005381
Bram Moolenaara5565e42020-05-09 15:44:01 +02005382 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005383 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5384 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005385
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005386 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005387 isn = ((isn_T *)instr->ga_data) + end_idx;
5388 isn->isn_arg.jump.jump_where = instr->ga_len;
5389 }
5390
5391 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005392 }
5393 return OK;
5394}
5395
5396/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005397 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005398 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5399 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005400 */
5401 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005402compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005403{
5404 ppconst_T ppconst;
5405
5406 CLEAR_FIELD(ppconst);
5407 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5408 {
5409 clear_ppconst(&ppconst);
5410 return FAIL;
5411 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005412 if (is_const != NULL)
5413 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005414 if (generate_ppconst(cctx, &ppconst) == FAIL)
5415 return FAIL;
5416 return OK;
5417}
5418
5419/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005420 * Toplevel expression.
5421 */
5422 static int
5423compile_expr0(char_u **arg, cctx_T *cctx)
5424{
5425 return compile_expr0_ext(arg, cctx, NULL);
5426}
5427
5428/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005429 * Compile "return [expr]".
5430 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 */
5432 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005433compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434{
5435 char_u *p = arg;
5436 garray_T *stack = &cctx->ctx_type_stack;
5437 type_T *stack_type;
5438
5439 if (*p != NUL && *p != '|' && *p != '\n')
5440 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005441 if (legacy)
5442 {
5443 int save_flags = cmdmod.cmod_flags;
5444
5445 generate_LEGACY_EVAL(cctx, p);
5446 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5447 0, cctx, FALSE, FALSE) == FAIL)
5448 return NULL;
5449 cmdmod.cmod_flags |= CMOD_LEGACY;
5450 (void)skip_expr(&p, NULL);
5451 cmdmod.cmod_flags = save_flags;
5452 }
5453 else
5454 {
5455 // compile return argument into instructions
5456 if (compile_expr0(&p, cctx) == FAIL)
5457 return NULL;
5458 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005460 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005461 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005462 // "check_return_type" with uf_ret_type set to &t_unknown is used
5463 // for an inline function without a specified return type. Set the
5464 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005465 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005466 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005467 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5468 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005469 || (!check_return_type
5470 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005471 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005472 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005473 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005474 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005475 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005476 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5477 && stack_type->tt_type != VAR_VOID
5478 && stack_type->tt_type != VAR_UNKNOWN)
5479 {
5480 emsg(_(e_returning_value_in_function_without_return_type));
5481 return NULL;
5482 }
5483 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005484 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005485 return NULL;
5486 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005488 }
5489 else
5490 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005491 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005492 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005493 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5494 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005495 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005496 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 return NULL;
5498 }
5499
5500 // No argument, return zero.
5501 generate_PUSHNR(cctx, 0);
5502 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005503
5504 // Undo any command modifiers.
5505 generate_undo_cmdmods(cctx);
5506
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005507 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005508 return NULL;
5509
5510 // "return val | endif" is possible
5511 return skipwhite(p);
5512}
5513
5514/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005515 * Get a line from the compilation context, compatible with exarg_T getline().
5516 * Return a pointer to the line in allocated memory.
5517 * Return NULL for end-of-file or some error.
5518 */
5519 static char_u *
5520exarg_getline(
5521 int c UNUSED,
5522 void *cookie,
5523 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005524 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005525{
5526 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005527 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005528
Bram Moolenaar66250c92020-08-20 15:02:42 +02005529 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005530 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005531 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005532 return NULL;
5533 ++cctx->ctx_lnum;
5534 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5535 // Comment lines result in NULL pointers, skip them.
5536 if (p != NULL)
5537 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005538 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005539}
5540
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005541 void
5542fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5543{
5544 eap->getline = exarg_getline;
5545 eap->cookie = cctx;
5546}
5547
Bram Moolenaar04b12692020-05-04 23:24:44 +02005548/*
5549 * Compile a nested :def command.
5550 */
5551 static char_u *
5552compile_nested_function(exarg_T *eap, cctx_T *cctx)
5553{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005554 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005555 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005556 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005557 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005558 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005559 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005560
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005561 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005562 {
5563 emsg(_(e_cannot_use_bang_with_nested_def));
5564 return NULL;
5565 }
5566
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005567 if (*name_start == '/')
5568 {
5569 name_end = skip_regexp(name_start + 1, '/', TRUE);
5570 if (*name_end == '/')
5571 ++name_end;
5572 eap->nextcmd = check_nextcmd(name_end);
5573 }
5574 if (name_end == name_start || *skipwhite(name_end) != '(')
5575 {
5576 if (!ends_excmd2(name_start, name_end))
5577 {
5578 semsg(_(e_invalid_command_str), eap->cmd);
5579 return NULL;
5580 }
5581
5582 // "def" or "def Name": list functions
5583 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5584 return NULL;
5585 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5586 }
5587
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005588 // Only g:Func() can use a namespace.
5589 if (name_start[1] == ':' && !is_global)
5590 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005591 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005592 return NULL;
5593 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005594 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005595 return NULL;
5596
Bram Moolenaar04b12692020-05-04 23:24:44 +02005597 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005598 fill_exarg_from_cctx(eap, cctx);
5599
Bram Moolenaar04b12692020-05-04 23:24:44 +02005600 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005601 lambda_name = vim_strsave(get_lambda_name());
5602 if (lambda_name == NULL)
5603 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005604 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005605
Bram Moolenaar822ba242020-05-24 23:00:18 +02005606 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005607 {
5608 r = eap->skip ? OK : FAIL;
5609 goto theend;
5610 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005611
5612 // copy over the block scope IDs before compiling
5613 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5614 {
5615 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5616
5617 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5618 if (ufunc->uf_block_ids != NULL)
5619 {
5620 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5621 sizeof(int) * block_depth);
5622 ufunc->uf_block_depth = block_depth;
5623 }
5624 }
5625
Bram Moolenaare99d4222021-06-13 14:01:26 +02005626 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
5627 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005628 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005629 {
5630 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005631 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005632 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005633
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005634 if (is_global)
5635 {
5636 char_u *func_name = vim_strnsave(name_start + 2,
5637 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005638
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005639 if (func_name == NULL)
5640 r = FAIL;
5641 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005642 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005643 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005644 lambda_name = NULL;
5645 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005646 }
5647 else
5648 {
5649 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005650 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005651 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005652
Bram Moolenaareef21022020-08-01 22:16:43 +02005653 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005654 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005655 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005656 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005657 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5658 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005659 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005660
5661theend:
5662 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005663 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005664}
5665
5666/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005667 * Return the length of an assignment operator, or zero if there isn't one.
5668 */
5669 int
5670assignment_len(char_u *p, int *heredoc)
5671{
5672 if (*p == '=')
5673 {
5674 if (p[1] == '<' && p[2] == '<')
5675 {
5676 *heredoc = TRUE;
5677 return 3;
5678 }
5679 return 1;
5680 }
5681 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5682 return 2;
5683 if (STRNCMP(p, "..=", 3) == 0)
5684 return 3;
5685 return 0;
5686}
5687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005688/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005689 * Generate the load instruction for "name".
5690 */
5691 static void
5692generate_loadvar(
5693 cctx_T *cctx,
5694 assign_dest_T dest,
5695 char_u *name,
5696 lvar_T *lvar,
5697 type_T *type)
5698{
5699 switch (dest)
5700 {
5701 case dest_option:
5702 // TODO: check the option exists
5703 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5704 break;
5705 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005706 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5707 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5708 else
5709 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005710 break;
5711 case dest_buffer:
5712 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5713 break;
5714 case dest_window:
5715 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5716 break;
5717 case dest_tab:
5718 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5719 break;
5720 case dest_script:
5721 compile_load_scriptvar(cctx,
5722 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5723 break;
5724 case dest_env:
5725 // Include $ in the name here
5726 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5727 break;
5728 case dest_reg:
5729 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5730 break;
5731 case dest_vimvar:
5732 generate_LOADV(cctx, name + 2, TRUE);
5733 break;
5734 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005735 if (lvar->lv_from_outer > 0)
5736 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5737 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005738 else
5739 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5740 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005741 case dest_expr:
5742 // list or dict value should already be on the stack.
5743 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005744 }
5745}
5746
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005747/*
5748 * Skip over "[expr]" or ".member".
5749 * Does not check for any errors.
5750 */
5751 static char_u *
5752skip_index(char_u *start)
5753{
5754 char_u *p = start;
5755
5756 if (*p == '[')
5757 {
5758 p = skipwhite(p + 1);
5759 (void)skip_expr(&p, NULL);
5760 p = skipwhite(p);
5761 if (*p == ']')
5762 return p + 1;
5763 return p;
5764 }
5765 // if (*p == '.')
5766 return to_name_end(p + 1, TRUE);
5767}
5768
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005769 void
5770vim9_declare_error(char_u *name)
5771{
5772 char *scope = "";
5773
5774 switch (*name)
5775 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005776 case 'g': scope = _("global"); break;
5777 case 'b': scope = _("buffer"); break;
5778 case 'w': scope = _("window"); break;
5779 case 't': scope = _("tab"); break;
5780 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005781 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005782 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005783 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005784 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005785 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005786 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005787 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005788 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005789 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005790}
5791
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005792/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005793 * For one assignment figure out the type of destination. Return it in "dest".
5794 * When not recognized "dest" is not set.
5795 * For an option "opt_flags" is set.
5796 * For a v:var "vimvaridx" is set.
5797 * "type" is set to the destination type if known, unchanted otherwise.
5798 * Return FAIL if an error message was given.
5799 */
5800 static int
5801get_var_dest(
5802 char_u *name,
5803 assign_dest_T *dest,
5804 int cmdidx,
5805 int *opt_flags,
5806 int *vimvaridx,
5807 type_T **type,
5808 cctx_T *cctx)
5809{
5810 char_u *p;
5811
5812 if (*name == '&')
5813 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005814 int cc;
5815 long numval;
5816 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005817
5818 *dest = dest_option;
5819 if (cmdidx == CMD_final || cmdidx == CMD_const)
5820 {
5821 emsg(_(e_const_option));
5822 return FAIL;
5823 }
5824 p = name;
5825 p = find_option_end(&p, opt_flags);
5826 if (p == NULL)
5827 {
5828 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005829 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005830 return FAIL;
5831 }
5832 cc = *p;
5833 *p = NUL;
5834 opt_type = get_option_value(skip_option_env_lead(name),
5835 &numval, NULL, *opt_flags);
5836 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005837 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005838 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005839 case gov_unknown:
5840 semsg(_(e_unknown_option), name);
5841 return FAIL;
5842 case gov_string:
5843 case gov_hidden_string:
5844 *type = &t_string;
5845 break;
5846 case gov_bool:
5847 case gov_hidden_bool:
5848 *type = &t_bool;
5849 break;
5850 case gov_number:
5851 case gov_hidden_number:
5852 *type = &t_number;
5853 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005854 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005855 }
5856 else if (*name == '$')
5857 {
5858 *dest = dest_env;
5859 *type = &t_string;
5860 }
5861 else if (*name == '@')
5862 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005863 if (name[1] != '@'
5864 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005865 {
5866 emsg_invreg(name[1]);
5867 return FAIL;
5868 }
5869 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005870 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005871 }
5872 else if (STRNCMP(name, "g:", 2) == 0)
5873 {
5874 *dest = dest_global;
5875 }
5876 else if (STRNCMP(name, "b:", 2) == 0)
5877 {
5878 *dest = dest_buffer;
5879 }
5880 else if (STRNCMP(name, "w:", 2) == 0)
5881 {
5882 *dest = dest_window;
5883 }
5884 else if (STRNCMP(name, "t:", 2) == 0)
5885 {
5886 *dest = dest_tab;
5887 }
5888 else if (STRNCMP(name, "v:", 2) == 0)
5889 {
5890 typval_T *vtv;
5891 int di_flags;
5892
5893 *vimvaridx = find_vim_var(name + 2, &di_flags);
5894 if (*vimvaridx < 0)
5895 {
5896 semsg(_(e_variable_not_found_str), name);
5897 return FAIL;
5898 }
5899 // We use the current value of "sandbox" here, is that OK?
5900 if (var_check_ro(di_flags, name, FALSE))
5901 return FAIL;
5902 *dest = dest_vimvar;
5903 vtv = get_vim_var_tv(*vimvaridx);
5904 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5905 }
5906 return OK;
5907}
5908
5909/*
5910 * Generate a STORE instruction for "dest", not being "dest_local".
5911 * Return FAIL when out of memory.
5912 */
5913 static int
5914generate_store_var(
5915 cctx_T *cctx,
5916 assign_dest_T dest,
5917 int opt_flags,
5918 int vimvaridx,
5919 int scriptvar_idx,
5920 int scriptvar_sid,
5921 type_T *type,
5922 char_u *name)
5923{
5924 switch (dest)
5925 {
5926 case dest_option:
5927 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5928 opt_flags);
5929 case dest_global:
5930 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005931 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5932 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005933 case dest_buffer:
5934 // include b: with the name, easier to execute that way
5935 return generate_STORE(cctx, ISN_STOREB, 0, name);
5936 case dest_window:
5937 // include w: with the name, easier to execute that way
5938 return generate_STORE(cctx, ISN_STOREW, 0, name);
5939 case dest_tab:
5940 // include t: with the name, easier to execute that way
5941 return generate_STORE(cctx, ISN_STORET, 0, name);
5942 case dest_env:
5943 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5944 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005945 return generate_STORE(cctx, ISN_STOREREG,
5946 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005947 case dest_vimvar:
5948 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5949 case dest_script:
5950 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005951 // "s:" may be included in the name.
5952 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005953 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005954 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5955 scriptvar_sid, scriptvar_idx, type);
5956 case dest_local:
5957 case dest_expr:
5958 // cannot happen
5959 break;
5960 }
5961 return FAIL;
5962}
5963
Bram Moolenaar752fc692021-01-04 21:57:11 +01005964 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005965generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5966{
5967 if (lhs->lhs_dest != dest_local)
5968 return generate_store_var(cctx, lhs->lhs_dest,
5969 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5970 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5971 lhs->lhs_type, lhs->lhs_name);
5972
5973 if (lhs->lhs_lvar != NULL)
5974 {
5975 garray_T *instr = &cctx->ctx_instr;
5976 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5977
5978 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5979 // ISN_STORENR
5980 if (lhs->lhs_lvar->lv_from_outer == 0
5981 && instr->ga_len == instr_count + 1
5982 && isn->isn_type == ISN_PUSHNR)
5983 {
5984 varnumber_T val = isn->isn_arg.number;
5985 garray_T *stack = &cctx->ctx_type_stack;
5986
5987 isn->isn_type = ISN_STORENR;
5988 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5989 isn->isn_arg.storenr.stnr_val = val;
5990 if (stack->ga_len > 0)
5991 --stack->ga_len;
5992 }
5993 else if (lhs->lhs_lvar->lv_from_outer > 0)
5994 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5995 lhs->lhs_lvar->lv_from_outer);
5996 else
5997 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5998 }
5999 return OK;
6000}
6001
6002 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006003is_decl_command(int cmdidx)
6004{
6005 return cmdidx == CMD_let || cmdidx == CMD_var
6006 || cmdidx == CMD_final || cmdidx == CMD_const;
6007}
6008
6009/*
6010 * Figure out the LHS type and other properties for an assignment or one item
6011 * of ":unlet" with an index.
6012 * Returns OK or FAIL.
6013 */
6014 static int
6015compile_lhs(
6016 char_u *var_start,
6017 lhs_T *lhs,
6018 int cmdidx,
6019 int heredoc,
6020 int oplen,
6021 cctx_T *cctx)
6022{
6023 char_u *var_end;
6024 int is_decl = is_decl_command(cmdidx);
6025
6026 CLEAR_POINTER(lhs);
6027 lhs->lhs_dest = dest_local;
6028 lhs->lhs_vimvaridx = -1;
6029 lhs->lhs_scriptvar_idx = -1;
6030
6031 // "dest_end" is the end of the destination, including "[expr]" or
6032 // ".name".
6033 // "var_end" is the end of the variable/option/etc. name.
6034 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6035 if (*var_start == '@')
6036 var_end = var_start + 2;
6037 else
6038 {
6039 // skip over the leading "&", "&l:", "&g:" and "$"
6040 var_end = skip_option_env_lead(var_start);
6041 var_end = to_name_end(var_end, TRUE);
6042 }
6043
6044 // "a: type" is declaring variable "a" with a type, not dict "a:".
6045 if (is_decl && lhs->lhs_dest_end == var_start + 2
6046 && lhs->lhs_dest_end[-1] == ':')
6047 --lhs->lhs_dest_end;
6048 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6049 --var_end;
6050
6051 // compute the length of the destination without "[expr]" or ".name"
6052 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006053 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006054 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6055 if (lhs->lhs_name == NULL)
6056 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006057
6058 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6059 // Something follows after the variable: "var[idx]" or "var.key".
6060 lhs->lhs_has_index = TRUE;
6061
Bram Moolenaar752fc692021-01-04 21:57:11 +01006062 if (heredoc)
6063 lhs->lhs_type = &t_list_string;
6064 else
6065 lhs->lhs_type = &t_any;
6066
6067 if (cctx->ctx_skip != SKIP_YES)
6068 {
6069 int declare_error = FALSE;
6070
6071 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6072 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6073 &lhs->lhs_type, cctx) == FAIL)
6074 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006075 if (lhs->lhs_dest != dest_local
6076 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006077 {
6078 // Specific kind of variable recognized.
6079 declare_error = is_decl;
6080 }
6081 else
6082 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006083 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006084 if (check_reserved_name(lhs->lhs_name) == FAIL)
6085 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006086
6087 if (lookup_local(var_start, lhs->lhs_varlen,
6088 &lhs->lhs_local_lvar, cctx) == OK)
6089 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6090 else
6091 {
6092 CLEAR_FIELD(lhs->lhs_arg_lvar);
6093 if (arg_exists(var_start, lhs->lhs_varlen,
6094 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6095 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6096 {
6097 if (is_decl)
6098 {
6099 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6100 return FAIL;
6101 }
6102 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6103 }
6104 }
6105 if (lhs->lhs_lvar != NULL)
6106 {
6107 if (is_decl)
6108 {
6109 semsg(_(e_variable_already_declared), lhs->lhs_name);
6110 return FAIL;
6111 }
6112 }
6113 else
6114 {
6115 int script_namespace = lhs->lhs_varlen > 1
6116 && STRNCMP(var_start, "s:", 2) == 0;
6117 int script_var = (script_namespace
6118 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006119 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006120 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006121 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006122 imported_T *import =
6123 find_imported(var_start, lhs->lhs_varlen, cctx);
6124
6125 if (script_namespace || script_var || import != NULL)
6126 {
6127 char_u *rawname = lhs->lhs_name
6128 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6129
6130 if (is_decl)
6131 {
6132 if (script_namespace)
6133 semsg(_(e_cannot_declare_script_variable_in_function),
6134 lhs->lhs_name);
6135 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006136 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006137 lhs->lhs_name);
6138 return FAIL;
6139 }
6140 else if (cctx->ctx_ufunc->uf_script_ctx_version
6141 == SCRIPT_VERSION_VIM9
6142 && script_namespace
6143 && !script_var && import == NULL)
6144 {
6145 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6146 return FAIL;
6147 }
6148
6149 lhs->lhs_dest = dest_script;
6150
6151 // existing script-local variables should have a type
6152 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6153 if (import != NULL)
6154 lhs->lhs_scriptvar_sid = import->imp_sid;
6155 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6156 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006157 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006158 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006159 lhs->lhs_scriptvar_sid, rawname,
6160 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6161 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006162 if (lhs->lhs_scriptvar_idx >= 0)
6163 {
6164 scriptitem_T *si = SCRIPT_ITEM(
6165 lhs->lhs_scriptvar_sid);
6166 svar_T *sv =
6167 ((svar_T *)si->sn_var_vals.ga_data)
6168 + lhs->lhs_scriptvar_idx;
6169 lhs->lhs_type = sv->sv_type;
6170 }
6171 }
6172 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006173 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006174 == FAIL)
6175 return FAIL;
6176 }
6177 }
6178
6179 if (declare_error)
6180 {
6181 vim9_declare_error(lhs->lhs_name);
6182 return FAIL;
6183 }
6184 }
6185
6186 // handle "a:name" as a name, not index "name" on "a"
6187 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6188 var_end = lhs->lhs_dest_end;
6189
6190 if (lhs->lhs_dest != dest_option)
6191 {
6192 if (is_decl && *var_end == ':')
6193 {
6194 char_u *p;
6195
6196 // parse optional type: "let var: type = expr"
6197 if (!VIM_ISWHITE(var_end[1]))
6198 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006199 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006200 return FAIL;
6201 }
6202 p = skipwhite(var_end + 1);
6203 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6204 if (lhs->lhs_type == NULL)
6205 return FAIL;
6206 lhs->lhs_has_type = TRUE;
6207 }
6208 else if (lhs->lhs_lvar != NULL)
6209 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6210 }
6211
Bram Moolenaare42939a2021-04-05 17:11:17 +02006212 if (oplen == 3 && !heredoc
6213 && lhs->lhs_dest != dest_global
6214 && !lhs->lhs_has_index
6215 && lhs->lhs_type->tt_type != VAR_STRING
6216 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006217 {
6218 emsg(_(e_can_only_concatenate_to_string));
6219 return FAIL;
6220 }
6221
6222 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6223 && cctx->ctx_skip != SKIP_YES)
6224 {
6225 if (oplen > 1 && !heredoc)
6226 {
6227 // +=, /=, etc. require an existing variable
6228 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6229 return FAIL;
6230 }
6231 if (!is_decl)
6232 {
6233 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6234 return FAIL;
6235 }
6236
Bram Moolenaar3f327882021-03-17 20:56:38 +01006237 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006238 if ((lhs->lhs_type->tt_type == VAR_FUNC
6239 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006240 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006241 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006242
6243 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006244 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6245 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6246 if (lhs->lhs_lvar == NULL)
6247 return FAIL;
6248 lhs->lhs_new_local = TRUE;
6249 }
6250
6251 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006252 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006253 {
6254 // Something follows after the variable: "var[idx]" or "var.key".
6255 // TODO: should we also handle "->func()" here?
6256 if (is_decl)
6257 {
6258 emsg(_(e_cannot_use_index_when_declaring_variable));
6259 return FAIL;
6260 }
6261
6262 if (var_start[lhs->lhs_varlen] == '['
6263 || var_start[lhs->lhs_varlen] == '.')
6264 {
6265 char_u *after = var_start + lhs->lhs_varlen;
6266 char_u *p;
6267
6268 // Only the last index is used below, if there are others
6269 // before it generate code for the expression. Thus for
6270 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6271 for (;;)
6272 {
6273 p = skip_index(after);
6274 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006275 {
6276 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006277 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006278 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006279 after = p;
6280 }
6281 if (after > var_start + lhs->lhs_varlen)
6282 {
6283 lhs->lhs_varlen = after - var_start;
6284 lhs->lhs_dest = dest_expr;
6285 // We don't know the type before evaluating the expression,
6286 // use "any" until then.
6287 lhs->lhs_type = &t_any;
6288 }
6289
Bram Moolenaar752fc692021-01-04 21:57:11 +01006290 if (lhs->lhs_type->tt_member == NULL)
6291 lhs->lhs_member_type = &t_any;
6292 else
6293 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6294 }
6295 else
6296 {
6297 semsg("Not supported yet: %s", var_start);
6298 return FAIL;
6299 }
6300 }
6301 return OK;
6302}
6303
6304/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006305 * Figure out the LHS and check a few errors.
6306 */
6307 static int
6308compile_assign_lhs(
6309 char_u *var_start,
6310 lhs_T *lhs,
6311 int cmdidx,
6312 int is_decl,
6313 int heredoc,
6314 int oplen,
6315 cctx_T *cctx)
6316{
6317 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6318 return FAIL;
6319
6320 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6321 {
6322 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6323 return FAIL;
6324 }
6325 if (!is_decl && lhs->lhs_lvar != NULL
6326 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6327 {
6328 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6329 return FAIL;
6330 }
6331 return OK;
6332}
6333
6334/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006335 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6336 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006337 */
6338 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006339compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006340 char_u *var_start,
6341 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006342 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006343 cctx_T *cctx)
6344{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006345 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006346 char_u *p;
6347 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006348 int need_white_before = TRUE;
6349 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006350
Bram Moolenaar752fc692021-01-04 21:57:11 +01006351 p = var_start + varlen;
6352 if (*p == '[')
6353 {
6354 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006355 if (*p == ':')
6356 {
6357 // empty first index, push zero
6358 r = generate_PUSHNR(cctx, 0);
6359 need_white_before = FALSE;
6360 }
6361 else
6362 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006363
6364 if (r == OK && *skipwhite(p) == ':')
6365 {
6366 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006367 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006368 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006369 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006370 empty_second = *skipwhite(p + 1) == ']';
6371 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6372 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006373 {
6374 semsg(_(e_white_space_required_before_and_after_str_at_str),
6375 ":", p);
6376 return FAIL;
6377 }
6378 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006379 if (*p == ']')
6380 // empty second index, push "none"
6381 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6382 else
6383 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006384 }
6385
Bram Moolenaar752fc692021-01-04 21:57:11 +01006386 if (r == OK && *skipwhite(p) != ']')
6387 {
6388 // this should not happen
6389 emsg(_(e_missbrac));
6390 r = FAIL;
6391 }
6392 }
6393 else // if (*p == '.')
6394 {
6395 char_u *key_end = to_name_end(p + 1, TRUE);
6396 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6397
6398 r = generate_PUSHS(cctx, key);
6399 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006400 return r;
6401}
6402
6403/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006404 * For a LHS with an index, load the variable to be indexed.
6405 */
6406 static int
6407compile_load_lhs(
6408 lhs_T *lhs,
6409 char_u *var_start,
6410 type_T *rhs_type,
6411 cctx_T *cctx)
6412{
6413 if (lhs->lhs_dest == dest_expr)
6414 {
6415 size_t varlen = lhs->lhs_varlen;
6416 int c = var_start[varlen];
6417 char_u *p = var_start;
6418 garray_T *stack = &cctx->ctx_type_stack;
6419
6420 // Evaluate "ll[expr]" of "ll[expr][idx]"
6421 var_start[varlen] = NUL;
6422 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6423 {
6424 // this should not happen
6425 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006426 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006427 return FAIL;
6428 }
6429 var_start[varlen] = c;
6430
6431 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6432 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6433 // now we can properly check the type
6434 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6435 && rhs_type != &t_void
6436 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6437 FALSE, FALSE) == FAIL)
6438 return FAIL;
6439 }
6440 else
6441 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6442 lhs->lhs_lvar, lhs->lhs_type);
6443 return OK;
6444}
6445
6446/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006447 * Produce code for loading "lhs" and also take care of an index.
6448 * Return OK/FAIL.
6449 */
6450 static int
6451compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6452{
6453 compile_load_lhs(lhs, var_start, NULL, cctx);
6454
6455 if (lhs->lhs_has_index)
6456 {
6457 int range = FALSE;
6458
6459 // Get member from list or dict. First compile the
6460 // index value.
6461 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6462 return FAIL;
6463 if (range)
6464 {
6465 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6466 var_start);
6467 return FAIL;
6468 }
6469
6470 // Get the member.
6471 if (compile_member(FALSE, cctx) == FAIL)
6472 return FAIL;
6473 }
6474 return OK;
6475}
6476
6477/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006478 * Assignment to a list or dict member, or ":unlet" for the item, using the
6479 * information in "lhs".
6480 * Returns OK or FAIL.
6481 */
6482 static int
6483compile_assign_unlet(
6484 char_u *var_start,
6485 lhs_T *lhs,
6486 int is_assign,
6487 type_T *rhs_type,
6488 cctx_T *cctx)
6489{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006490 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006491 garray_T *stack = &cctx->ctx_type_stack;
6492 int range = FALSE;
6493
Bram Moolenaar68452172021-04-12 21:21:02 +02006494 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006495 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006496 if (is_assign && range && lhs->lhs_type != &t_blob
6497 && lhs->lhs_type != &t_any)
6498 {
6499 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6500 return FAIL;
6501 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006502
6503 if (lhs->lhs_type == &t_any)
6504 {
6505 // Index on variable of unknown type: check at runtime.
6506 dest_type = VAR_ANY;
6507 }
6508 else
6509 {
6510 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006511 if (dest_type == VAR_DICT && range)
6512 {
6513 emsg(e_cannot_use_range_with_dictionary);
6514 return FAIL;
6515 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006516 if (dest_type == VAR_DICT
6517 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006518 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006519 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006520 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006521 type_T *type;
6522
6523 if (range)
6524 {
6525 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6526 if (need_type(type, &t_number,
6527 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006528 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006529 }
6530 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6531 if ((dest_type != VAR_BLOB || type != &t_special)
6532 && need_type(type, &t_number,
6533 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006534 return FAIL;
6535 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006536 }
6537
6538 // Load the dict or list. On the stack we then have:
6539 // - value (for assignment, not for :unlet)
6540 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006541 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006542 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006543 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6544 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006545
Bram Moolenaar68452172021-04-12 21:21:02 +02006546 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6547 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006548 {
6549 if (is_assign)
6550 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006551 if (range)
6552 {
6553 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6554 return FAIL;
6555 }
6556 else
6557 {
6558 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006559
Bram Moolenaar68452172021-04-12 21:21:02 +02006560 if (isn == NULL)
6561 return FAIL;
6562 isn->isn_arg.vartype = dest_type;
6563 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006564 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006565 else if (range)
6566 {
6567 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6568 return FAIL;
6569 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006570 else
6571 {
6572 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6573 return FAIL;
6574 }
6575 }
6576 else
6577 {
6578 emsg(_(e_indexable_type_required));
6579 return FAIL;
6580 }
6581
6582 return OK;
6583}
6584
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006585/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006586 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006587 * "let name"
6588 * "var name = expr"
6589 * "final name = expr"
6590 * "const name = expr"
6591 * "name = expr"
6592 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006593 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006594 * Return NULL for an error.
6595 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596 */
6597 static char_u *
6598compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6599{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006600 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006602 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 char_u *ret = NULL;
6604 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006605 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006607 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006609 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006610 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 int oplen = 0;
6612 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006613 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006614 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006616 int is_decl = is_decl_command(cmdidx);
6617 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006618 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006620 // Skip over the "var" or "[var, var]" to get to any "=".
6621 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6622 if (p == NULL)
6623 return *arg == '[' ? arg : NULL;
6624
6625 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006627 // TODO: should we allow this, and figure out type inference from list
6628 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006629 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006630 return NULL;
6631 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006632 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006634 sp = p;
6635 p = skipwhite(p);
6636 op = p;
6637 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006638
6639 if (var_count > 0 && oplen == 0)
6640 // can be something like "[1, 2]->func()"
6641 return arg;
6642
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006643 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006644 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006645 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006646 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006647 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006648 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6649 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006650 if (VIM_ISWHITE(eap->cmd[2]))
6651 {
6652 semsg(_(e_no_white_space_allowed_after_str_str),
6653 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6654 return NULL;
6655 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006656 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6657 oplen = 2;
6658 incdec = TRUE;
6659 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006661 if (heredoc)
6662 {
6663 list_T *l;
6664 listitem_T *li;
6665
6666 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006667 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006668 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006669 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006670 if (l == NULL)
6671 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006672
Bram Moolenaar078269b2020-09-21 20:35:55 +02006673 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006675 // Push each line and the create the list.
6676 FOR_ALL_LIST_ITEMS(l, li)
6677 {
6678 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6679 li->li_tv.vval.v_string = NULL;
6680 }
6681 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 list_free(l);
6684 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006685 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006687 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006689 char_u *wp;
6690
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006691 // for "[var, var] = expr" evaluate the expression here, loop over the
6692 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006693 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006694
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006695 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006696 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006697 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006698 if (compile_expr0(&p, cctx) == FAIL)
6699 return NULL;
6700 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006702 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006704 type_T *stacktype;
6705
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006706 stacktype = stack->ga_len == 0 ? &t_void
6707 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006708 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006710 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006711 goto theend;
6712 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006713 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006714 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006715 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006716 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006717 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6718 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006719 if (stacktype->tt_member != NULL)
6720 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006721 }
6722 }
6723
6724 /*
6725 * Loop over variables in "[var, var] = expr".
6726 * For "var = expr" and "let var: type" this is done only once.
6727 */
6728 if (var_count > 0)
6729 var_start = skipwhite(arg + 1); // skip over the "["
6730 else
6731 var_start = arg;
6732 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6733 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006734 int instr_count = -1;
6735 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006736
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006737 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6738 {
6739 // Ignore underscore in "[a, _, b] = list".
6740 if (var_count > 0)
6741 {
6742 var_start = skipwhite(var_start + 2);
6743 continue;
6744 }
6745 emsg(_(e_cannot_use_underscore_here));
6746 goto theend;
6747 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006748 vim_free(lhs.lhs_name);
6749
6750 /*
6751 * Figure out the LHS type and other properties.
6752 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006753 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6754 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006755 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006756 if (!heredoc)
6757 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006758 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006759 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006760 if (oplen > 0 && var_count == 0)
6761 {
6762 // skip over the "=" and the expression
6763 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006764 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006765 }
6766 }
6767 else if (oplen > 0)
6768 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006769 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006770 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006771
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006772 // for "+=", "*=", "..=" etc. first load the current value
6773 if (*op != '='
6774 && compile_load_lhs_with_index(&lhs, var_start,
6775 cctx) == FAIL)
6776 goto theend;
6777
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006778 // For "var = expr" evaluate the expression.
6779 if (var_count == 0)
6780 {
6781 int r;
6782
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006783 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006784 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006785 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006786 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006787 r = generate_PUSHNR(cctx, 1);
6788 }
6789 else
6790 {
6791 // Temporarily hide the new local variable here, it is
6792 // not available to this expression.
6793 if (lhs.lhs_new_local)
6794 --cctx->ctx_locals.ga_len;
6795 wp = op + oplen;
6796 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6797 {
6798 if (lhs.lhs_new_local)
6799 ++cctx->ctx_locals.ga_len;
6800 goto theend;
6801 }
6802 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006803 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006804 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006805 if (r == FAIL)
6806 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006807 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006808 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006809 else if (semicolon && var_idx == var_count - 1)
6810 {
6811 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006812 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006813 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6814 goto theend;
6815 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006816 else
6817 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006818 // For "[var, var] = expr" get the "var_idx" item from the
6819 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006820 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006821 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006822 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006823
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006824 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006825 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006826 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006827 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006828 if ((rhs_type->tt_type == VAR_FUNC
6829 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006830 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006831 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006832 goto theend;
6833
Bram Moolenaar752fc692021-01-04 21:57:11 +01006834 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006835 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006836 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006837 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006838 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006839 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006840 }
6841 else
6842 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006843 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006844 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006845 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006846 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006847 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006848 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006849 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006850 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006851 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006852 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006853 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006854 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006855 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006856 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006857 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006858
Bram Moolenaar77709b12021-04-03 21:01:01 +02006859 // Without operator check type here, otherwise below.
6860 // Use the line number of the assignment.
6861 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006862 if (lhs.lhs_has_index)
6863 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006864 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006865 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006866 goto theend;
6867 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006868 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006869 else
6870 {
6871 type_T *lhs_type = lhs.lhs_member_type;
6872
6873 // Special case: assigning to @# can use a number or a
6874 // string.
6875 if (lhs_type == &t_number_or_string
6876 && rhs_type->tt_type == VAR_NUMBER)
6877 lhs_type = &t_number;
6878 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006879 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006880 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006883 else if (cmdidx == CMD_final)
6884 {
6885 emsg(_(e_final_requires_a_value));
6886 goto theend;
6887 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006888 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006890 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006891 goto theend;
6892 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006893 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006894 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006895 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006896 goto theend;
6897 }
6898 else
6899 {
6900 // variables are always initialized
6901 if (ga_grow(instr, 1) == FAIL)
6902 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006903 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006904 {
6905 case VAR_BOOL:
6906 generate_PUSHBOOL(cctx, VVAL_FALSE);
6907 break;
6908 case VAR_FLOAT:
6909#ifdef FEAT_FLOAT
6910 generate_PUSHF(cctx, 0.0);
6911#endif
6912 break;
6913 case VAR_STRING:
6914 generate_PUSHS(cctx, NULL);
6915 break;
6916 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006917 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006918 break;
6919 case VAR_FUNC:
6920 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6921 break;
6922 case VAR_LIST:
6923 generate_NEWLIST(cctx, 0);
6924 break;
6925 case VAR_DICT:
6926 generate_NEWDICT(cctx, 0);
6927 break;
6928 case VAR_JOB:
6929 generate_PUSHJOB(cctx, NULL);
6930 break;
6931 case VAR_CHANNEL:
6932 generate_PUSHCHANNEL(cctx, NULL);
6933 break;
6934 case VAR_NUMBER:
6935 case VAR_UNKNOWN:
6936 case VAR_ANY:
6937 case VAR_PARTIAL:
6938 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006939 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006940 case VAR_SPECIAL: // cannot happen
6941 generate_PUSHNR(cctx, 0);
6942 break;
6943 }
6944 }
6945 if (var_count == 0)
6946 end = p;
6947 }
6948
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006949 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006950 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006951 break;
6952
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006953 if (oplen > 0 && *op != '=')
6954 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006955 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006956 type_T *stacktype;
6957
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006958 if (*op == '.')
6959 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006960 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006961 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006962 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006963 if (
6964#ifdef FEAT_FLOAT
6965 // If variable is float operation with number is OK.
6966 !(expected == &t_float && stacktype == &t_number) &&
6967#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006968 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006969 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006970 goto theend;
6971
6972 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006973 {
6974 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6975 goto theend;
6976 }
6977 else if (*op == '+')
6978 {
6979 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006980 operator_type(lhs.lhs_member_type, stacktype),
6981 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006982 goto theend;
6983 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006984 else if (generate_two_op(cctx, op) == FAIL)
6985 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006988 // Use the line number of the assignment for store instruction.
6989 save_lnum = cctx->ctx_lnum;
6990 cctx->ctx_lnum = start_lnum - 1;
6991
Bram Moolenaar752fc692021-01-04 21:57:11 +01006992 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006993 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006994 // Use the info in "lhs" to store the value at the index in the
6995 // list or dict.
6996 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6997 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006998 {
6999 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007000 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007001 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007002 }
7003 else
7004 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007005 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007006 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007007 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007008 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007009 generate_LOCKCONST(cctx);
7010
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007011 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007012 && (lhs.lhs_type->tt_type == VAR_DICT
7013 || lhs.lhs_type->tt_type == VAR_LIST)
7014 && lhs.lhs_type->tt_member != NULL
7015 && lhs.lhs_type->tt_member != &t_any
7016 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007017 // Set the type in the list or dict, so that it can be checked,
7018 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007019 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007020
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007021 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007022 {
7023 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007024 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007025 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007026 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007027 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007028
7029 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007030 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007032
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007033 // For "[var, var] = expr" drop the "expr" value.
7034 // Also for "[var, var; _] = expr".
7035 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007036 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007037 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007038 goto theend;
7039 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007040
Bram Moolenaarb2097502020-07-19 17:17:02 +02007041 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042
7043theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007044 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045 return ret;
7046}
7047
7048/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007049 * Check for an assignment at "eap->cmd", compile it if found.
7050 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7051 */
7052 static int
7053may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7054{
7055 char_u *pskip;
7056 char_u *p;
7057
7058 // Assuming the command starts with a variable or function name,
7059 // find what follows.
7060 // Skip over "var.member", "var[idx]" and the like.
7061 // Also "&opt = val", "$ENV = val" and "@r = val".
7062 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7063 ? eap->cmd + 1 : eap->cmd;
7064 p = to_name_end(pskip, TRUE);
7065 if (p > eap->cmd && *p != NUL)
7066 {
7067 char_u *var_end;
7068 int oplen;
7069 int heredoc;
7070
7071 if (eap->cmd[0] == '@')
7072 var_end = eap->cmd + 2;
7073 else
7074 var_end = find_name_end(pskip, NULL, NULL,
7075 FNE_CHECK_START | FNE_INCL_BR);
7076 oplen = assignment_len(skipwhite(var_end), &heredoc);
7077 if (oplen > 0)
7078 {
7079 size_t len = p - eap->cmd;
7080
7081 // Recognize an assignment if we recognize the variable
7082 // name:
7083 // "g:var = expr"
7084 // "local = expr" where "local" is a local var.
7085 // "script = expr" where "script" is a script-local var.
7086 // "import = expr" where "import" is an imported var
7087 // "&opt = expr"
7088 // "$ENV = expr"
7089 // "@r = expr"
7090 if (*eap->cmd == '&'
7091 || *eap->cmd == '$'
7092 || *eap->cmd == '@'
7093 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007094 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007095 {
7096 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7097 if (*line == NULL || *line == eap->cmd)
7098 return FAIL;
7099 return OK;
7100 }
7101 }
7102 }
7103
7104 if (*eap->cmd == '[')
7105 {
7106 // [var, var] = expr
7107 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7108 if (*line == NULL)
7109 return FAIL;
7110 if (*line != eap->cmd)
7111 return OK;
7112 }
7113 return NOTDONE;
7114}
7115
7116/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007117 * Check if "name" can be "unlet".
7118 */
7119 int
7120check_vim9_unlet(char_u *name)
7121{
7122 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7123 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007124 // "unlet s:var" is allowed in legacy script.
7125 if (*name == 's' && !script_is_vim9())
7126 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007127 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007128 return FAIL;
7129 }
7130 return OK;
7131}
7132
7133/*
7134 * Callback passed to ex_unletlock().
7135 */
7136 static int
7137compile_unlet(
7138 lval_T *lvp,
7139 char_u *name_end,
7140 exarg_T *eap,
7141 int deep UNUSED,
7142 void *coookie)
7143{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007144 cctx_T *cctx = coookie;
7145 char_u *p = lvp->ll_name;
7146 int cc = *name_end;
7147 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007148
Bram Moolenaar752fc692021-01-04 21:57:11 +01007149 if (cctx->ctx_skip == SKIP_YES)
7150 return OK;
7151
7152 *name_end = NUL;
7153 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007154 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007155 // :unlet $ENV_VAR
7156 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7157 }
7158 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7159 {
7160 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007161
Bram Moolenaar752fc692021-01-04 21:57:11 +01007162 // This is similar to assigning: lookup the list/dict, compile the
7163 // idx/key. Then instead of storing the value unlet the item.
7164 // unlet {list}[idx]
7165 // unlet {dict}[key] dict.key
7166 //
7167 // Figure out the LHS type and other properties.
7168 //
7169 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7170
7171 // : unlet an indexed item
7172 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007173 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007174 iemsg("called compile_lhs() without an index");
7175 ret = FAIL;
7176 }
7177 else
7178 {
7179 // Use the info in "lhs" to unlet the item at the index in the
7180 // list or dict.
7181 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007182 }
7183
Bram Moolenaar752fc692021-01-04 21:57:11 +01007184 vim_free(lhs.lhs_name);
7185 }
7186 else if (check_vim9_unlet(p) == FAIL)
7187 {
7188 ret = FAIL;
7189 }
7190 else
7191 {
7192 // Normal name. Only supports g:, w:, t: and b: namespaces.
7193 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007194 }
7195
Bram Moolenaar752fc692021-01-04 21:57:11 +01007196 *name_end = cc;
7197 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007198}
7199
7200/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007201 * Callback passed to ex_unletlock().
7202 */
7203 static int
7204compile_lock_unlock(
7205 lval_T *lvp,
7206 char_u *name_end,
7207 exarg_T *eap,
7208 int deep UNUSED,
7209 void *coookie)
7210{
7211 cctx_T *cctx = coookie;
7212 int cc = *name_end;
7213 char_u *p = lvp->ll_name;
7214 int ret = OK;
7215 size_t len;
7216 char_u *buf;
7217
7218 if (cctx->ctx_skip == SKIP_YES)
7219 return OK;
7220
7221 // Cannot use :lockvar and :unlockvar on local variables.
7222 if (p[1] != ':')
7223 {
7224 char_u *end = skip_var_one(p, FALSE);
7225
7226 if (lookup_local(p, end - p, NULL, cctx) == OK)
7227 {
7228 emsg(_(e_cannot_lock_unlock_local_variable));
7229 return FAIL;
7230 }
7231 }
7232
7233 // Checking is done at runtime.
7234 *name_end = NUL;
7235 len = name_end - p + 20;
7236 buf = alloc(len);
7237 if (buf == NULL)
7238 ret = FAIL;
7239 else
7240 {
7241 vim_snprintf((char *)buf, len, "%s %s",
7242 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7243 p);
7244 ret = generate_EXEC(cctx, buf);
7245
7246 vim_free(buf);
7247 *name_end = cc;
7248 }
7249 return ret;
7250}
7251
7252/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007253 * compile "unlet var", "lock var" and "unlock var"
7254 * "arg" points to "var".
7255 */
7256 static char_u *
7257compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7258{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007259 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7260 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7261 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007262 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7263}
7264
7265/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007266 * Compile an :import command.
7267 */
7268 static char_u *
7269compile_import(char_u *arg, cctx_T *cctx)
7270{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007271 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007272}
7273
7274/*
7275 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7276 */
7277 static int
7278compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7279{
7280 garray_T *instr = &cctx->ctx_instr;
7281 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7282
7283 if (endlabel == NULL)
7284 return FAIL;
7285 endlabel->el_next = *el;
7286 *el = endlabel;
7287 endlabel->el_end_label = instr->ga_len;
7288
7289 generate_JUMP(cctx, when, 0);
7290 return OK;
7291}
7292
7293 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007294compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295{
7296 garray_T *instr = &cctx->ctx_instr;
7297
7298 while (*el != NULL)
7299 {
7300 endlabel_T *cur = (*el);
7301 isn_T *isn;
7302
7303 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007304 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 *el = cur->el_next;
7306 vim_free(cur);
7307 }
7308}
7309
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007310 static void
7311compile_free_jump_to_end(endlabel_T **el)
7312{
7313 while (*el != NULL)
7314 {
7315 endlabel_T *cur = (*el);
7316
7317 *el = cur->el_next;
7318 vim_free(cur);
7319 }
7320}
7321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322/*
7323 * Create a new scope and set up the generic items.
7324 */
7325 static scope_T *
7326new_scope(cctx_T *cctx, scopetype_T type)
7327{
7328 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7329
7330 if (scope == NULL)
7331 return NULL;
7332 scope->se_outer = cctx->ctx_scope;
7333 cctx->ctx_scope = scope;
7334 scope->se_type = type;
7335 scope->se_local_count = cctx->ctx_locals.ga_len;
7336 return scope;
7337}
7338
7339/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007340 * Free the current scope and go back to the outer scope.
7341 */
7342 static void
7343drop_scope(cctx_T *cctx)
7344{
7345 scope_T *scope = cctx->ctx_scope;
7346
7347 if (scope == NULL)
7348 {
7349 iemsg("calling drop_scope() without a scope");
7350 return;
7351 }
7352 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007353 switch (scope->se_type)
7354 {
7355 case IF_SCOPE:
7356 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7357 case FOR_SCOPE:
7358 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7359 case WHILE_SCOPE:
7360 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7361 case TRY_SCOPE:
7362 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7363 case NO_SCOPE:
7364 case BLOCK_SCOPE:
7365 break;
7366 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007367 vim_free(scope);
7368}
7369
7370/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007371 * compile "if expr"
7372 *
7373 * "if expr" Produces instructions:
7374 * EVAL expr Push result of "expr"
7375 * JUMP_IF_FALSE end
7376 * ... body ...
7377 * end:
7378 *
7379 * "if expr | else" Produces instructions:
7380 * EVAL expr Push result of "expr"
7381 * JUMP_IF_FALSE else
7382 * ... body ...
7383 * JUMP_ALWAYS end
7384 * else:
7385 * ... body ...
7386 * end:
7387 *
7388 * "if expr1 | elseif expr2 | else" Produces instructions:
7389 * EVAL expr Push result of "expr"
7390 * JUMP_IF_FALSE elseif
7391 * ... body ...
7392 * JUMP_ALWAYS end
7393 * elseif:
7394 * EVAL expr Push result of "expr"
7395 * JUMP_IF_FALSE else
7396 * ... body ...
7397 * JUMP_ALWAYS end
7398 * else:
7399 * ... body ...
7400 * end:
7401 */
7402 static char_u *
7403compile_if(char_u *arg, cctx_T *cctx)
7404{
7405 char_u *p = arg;
7406 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007407 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007408 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007409 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007410 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411
Bram Moolenaara5565e42020-05-09 15:44:01 +02007412 CLEAR_FIELD(ppconst);
7413 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007414 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007415 clear_ppconst(&ppconst);
7416 return NULL;
7417 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007418 if (!ends_excmd2(arg, skipwhite(p)))
7419 {
7420 semsg(_(e_trailing_arg), p);
7421 return NULL;
7422 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007423 if (cctx->ctx_skip == SKIP_YES)
7424 clear_ppconst(&ppconst);
7425 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007426 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007427 int error = FALSE;
7428 int v;
7429
Bram Moolenaara5565e42020-05-09 15:44:01 +02007430 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007431 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007432 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007433 if (error)
7434 return NULL;
7435 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007436 }
7437 else
7438 {
7439 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007440 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007441 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007442 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007443 if (bool_on_stack(cctx) == FAIL)
7444 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446
Bram Moolenaara91a7132021-03-25 21:12:15 +01007447 // CMDMOD_REV must come before the jump
7448 generate_undo_cmdmods(cctx);
7449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 scope = new_scope(cctx, IF_SCOPE);
7451 if (scope == NULL)
7452 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007453 scope->se_skip_save = skip_save;
7454 // "is_had_return" will be reset if any block does not end in :return
7455 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007457 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007458 {
7459 // "where" is set when ":elseif", "else" or ":endif" is found
7460 scope->se_u.se_if.is_if_label = instr->ga_len;
7461 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7462 }
7463 else
7464 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007465
Bram Moolenaarced68a02021-01-24 17:53:47 +01007466#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007467 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007468 && skip_save != SKIP_YES)
7469 {
7470 // generated a profile start, need to generate a profile end, since it
7471 // won't be done after returning
7472 cctx->ctx_skip = SKIP_NOT;
7473 generate_instr(cctx, ISN_PROF_END);
7474 cctx->ctx_skip = SKIP_YES;
7475 }
7476#endif
7477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478 return p;
7479}
7480
7481 static char_u *
7482compile_elseif(char_u *arg, cctx_T *cctx)
7483{
7484 char_u *p = arg;
7485 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007486 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 isn_T *isn;
7488 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007489 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007490 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007491
7492 if (scope == NULL || scope->se_type != IF_SCOPE)
7493 {
7494 emsg(_(e_elseif_without_if));
7495 return NULL;
7496 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007497 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007498 if (!cctx->ctx_had_return)
7499 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007500
Bram Moolenaarced68a02021-01-24 17:53:47 +01007501 if (cctx->ctx_skip == SKIP_NOT)
7502 {
7503 // previous block was executed, this one and following will not
7504 cctx->ctx_skip = SKIP_YES;
7505 scope->se_u.se_if.is_seen_skip_not = TRUE;
7506 }
7507 if (scope->se_u.se_if.is_seen_skip_not)
7508 {
7509 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007510 // Do not count the "elseif" for profiling and cmdmod
7511 instr->ga_len = current_instr_idx(cctx);
7512
Bram Moolenaarced68a02021-01-24 17:53:47 +01007513 skip_expr_cctx(&p, cctx);
7514 return p;
7515 }
7516
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007517 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007518 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007519 int moved_cmdmod = FALSE;
7520
7521 // Move any CMDMOD instruction to after the jump
7522 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7523 {
7524 if (ga_grow(instr, 1) == FAIL)
7525 return NULL;
7526 ((isn_T *)instr->ga_data)[instr->ga_len] =
7527 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7528 --instr->ga_len;
7529 moved_cmdmod = TRUE;
7530 }
7531
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007532 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007533 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007534 return NULL;
7535 // previous "if" or "elseif" jumps here
7536 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7537 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007538 if (moved_cmdmod)
7539 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007540 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007542 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007543 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007544 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007545 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007546 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007547#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007548 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007549 {
7550 // the previous block was skipped, need to profile this line
7551 generate_instr(cctx, ISN_PROF_START);
7552 instr_count = instr->ga_len;
7553 }
7554#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007555 if (cctx->ctx_compile_type == CT_DEBUG)
7556 {
7557 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007558 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007559 instr_count = instr->ga_len;
7560 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007561 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007562 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007563 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007564 clear_ppconst(&ppconst);
7565 return NULL;
7566 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007567 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007568 if (!ends_excmd2(arg, skipwhite(p)))
7569 {
7570 semsg(_(e_trailing_arg), p);
7571 return NULL;
7572 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007573 if (scope->se_skip_save == SKIP_YES)
7574 clear_ppconst(&ppconst);
7575 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007576 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007577 int error = FALSE;
7578 int v;
7579
Bram Moolenaar7f141552020-05-09 17:35:53 +02007580 // The expression results in a constant.
7581 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007582 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7583 if (error)
7584 return NULL;
7585 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007586 clear_ppconst(&ppconst);
7587 scope->se_u.se_if.is_if_label = -1;
7588 }
7589 else
7590 {
7591 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007592 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007593 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007594 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007595 if (bool_on_stack(cctx) == FAIL)
7596 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007597
Bram Moolenaara91a7132021-03-25 21:12:15 +01007598 // CMDMOD_REV must come before the jump
7599 generate_undo_cmdmods(cctx);
7600
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007601 // "where" is set when ":elseif", "else" or ":endif" is found
7602 scope->se_u.se_if.is_if_label = instr->ga_len;
7603 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7604 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007605
7606 return p;
7607}
7608
7609 static char_u *
7610compile_else(char_u *arg, cctx_T *cctx)
7611{
7612 char_u *p = arg;
7613 garray_T *instr = &cctx->ctx_instr;
7614 isn_T *isn;
7615 scope_T *scope = cctx->ctx_scope;
7616
7617 if (scope == NULL || scope->se_type != IF_SCOPE)
7618 {
7619 emsg(_(e_else_without_if));
7620 return NULL;
7621 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007622 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007623 if (!cctx->ctx_had_return)
7624 scope->se_u.se_if.is_had_return = FALSE;
7625 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626
Bram Moolenaarced68a02021-01-24 17:53:47 +01007627#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007628 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007629 {
7630 if (cctx->ctx_skip == SKIP_NOT
7631 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7632 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007633 // the previous block was executed, do not count "else" for
7634 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007635 --instr->ga_len;
7636 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7637 {
7638 // the previous block was not executed, this one will, do count the
7639 // "else" for profiling
7640 cctx->ctx_skip = SKIP_NOT;
7641 generate_instr(cctx, ISN_PROF_END);
7642 generate_instr(cctx, ISN_PROF_START);
7643 cctx->ctx_skip = SKIP_YES;
7644 }
7645 }
7646#endif
7647
7648 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007649 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007650 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007651 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007652 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007653 if (!cctx->ctx_had_return
7654 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7655 JUMP_ALWAYS, cctx) == FAIL)
7656 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007657 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007658
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007659 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007660 {
7661 if (scope->se_u.se_if.is_if_label >= 0)
7662 {
7663 // previous "if" or "elseif" jumps here
7664 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7665 isn->isn_arg.jump.jump_where = instr->ga_len;
7666 scope->se_u.se_if.is_if_label = -1;
7667 }
7668 }
7669
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007670 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007671 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007673
7674 return p;
7675}
7676
7677 static char_u *
7678compile_endif(char_u *arg, cctx_T *cctx)
7679{
7680 scope_T *scope = cctx->ctx_scope;
7681 ifscope_T *ifscope;
7682 garray_T *instr = &cctx->ctx_instr;
7683 isn_T *isn;
7684
Bram Moolenaarfa984412021-03-25 22:15:28 +01007685 if (misplaced_cmdmod(cctx))
7686 return NULL;
7687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007688 if (scope == NULL || scope->se_type != IF_SCOPE)
7689 {
7690 emsg(_(e_endif_without_if));
7691 return NULL;
7692 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007693 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007694 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007695 if (!cctx->ctx_had_return)
7696 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007697
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007698 if (scope->se_u.se_if.is_if_label >= 0)
7699 {
7700 // previous "if" or "elseif" jumps here
7701 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7702 isn->isn_arg.jump.jump_where = instr->ga_len;
7703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007705 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007706
7707#ifdef FEAT_PROFILE
7708 // even when skipping we count the endif as executed, unless the block it's
7709 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007710 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007711 && scope->se_skip_save != SKIP_YES)
7712 {
7713 cctx->ctx_skip = SKIP_NOT;
7714 generate_instr(cctx, ISN_PROF_START);
7715 }
7716#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007717 cctx->ctx_skip = scope->se_skip_save;
7718
7719 // If all the blocks end in :return and there is an :else then the
7720 // had_return flag is set.
7721 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007722
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007723 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007724 return arg;
7725}
7726
7727/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007728 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007729 *
7730 * Produces instructions:
7731 * PUSHNR -1
7732 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007733 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007734 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7735 * - if beyond end, jump to "end"
7736 * - otherwise get item from list and push it
7737 * STORE var Store item in "var"
7738 * ... body ...
7739 * JUMP top Jump back to repeat
7740 * end: DROP Drop the result of "expr"
7741 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007742 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7743 * UNPACK 2 Split item in 2
7744 * STORE var1 Store item in "var1"
7745 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007746 */
7747 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007748compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007749{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007750 char_u *arg;
7751 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007752 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007754 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007755 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007756 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007757 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007760 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007761 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007762 lvar_T *loop_lvar; // loop iteration variable
7763 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007764 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007765 type_T *item_type = &t_any;
7766 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007767 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007768
Bram Moolenaar792f7862020-11-23 08:31:18 +01007769 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007770 if (p == NULL)
7771 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007772 if (var_count == 0)
7773 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007774 else
7775 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007776
7777 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007778 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007779 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7780 return NULL;
7781 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007782 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007783 if (*p == ':' && wp != p)
7784 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7785 else
7786 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007787 return NULL;
7788 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007789 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007790 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7791 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007792
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007793 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7794 // instruction.
7795 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7796 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7797 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007798 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007799 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007800 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7801 .isn_arg.debug.dbg_break_lnum;
7802 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007804 scope = new_scope(cctx, FOR_SCOPE);
7805 if (scope == NULL)
7806 return NULL;
7807
Bram Moolenaar792f7862020-11-23 08:31:18 +01007808 // Reserve a variable to store the loop iteration counter and initialize it
7809 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007810 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7811 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007812 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007813 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007814 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007816 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007817 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818
7819 // compile "expr", it remains on the stack until "endfor"
7820 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007821 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007822 {
7823 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007825 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007826 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007827
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007828 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007829 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007831 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007832 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007833 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007834 semsg(_(e_for_loop_on_str_not_supported),
7835 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007836 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007837 return NULL;
7838 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007839
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007840 if (vartype->tt_type == VAR_STRING)
7841 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007842 else if (vartype->tt_type == VAR_BLOB)
7843 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007844 else if (vartype->tt_type == VAR_LIST
7845 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007846 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007847 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007848 item_type = vartype->tt_member;
7849 else if (vartype->tt_member->tt_type == VAR_LIST
7850 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007851 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007852 item_type = vartype->tt_member->tt_member;
7853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007854
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007855 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007856 generate_undo_cmdmods(cctx);
7857
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007858 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007859 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007860
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007861 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862
Bram Moolenaar792f7862020-11-23 08:31:18 +01007863 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007864 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007865 {
7866 generate_UNPACK(cctx, var_count, semicolon);
7867 arg = skipwhite(arg + 1); // skip white after '['
7868
7869 // the list item is replaced by a number of items
7870 if (ga_grow(stack, var_count - 1) == FAIL)
7871 {
7872 drop_scope(cctx);
7873 return NULL;
7874 }
7875 --stack->ga_len;
7876 for (idx = 0; idx < var_count; ++idx)
7877 {
7878 ((type_T **)stack->ga_data)[stack->ga_len] =
7879 (semicolon && idx == 0) ? vartype : item_type;
7880 ++stack->ga_len;
7881 }
7882 }
7883
7884 for (idx = 0; idx < var_count; ++idx)
7885 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007886 assign_dest_T dest = dest_local;
7887 int opt_flags = 0;
7888 int vimvaridx = -1;
7889 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007890 type_T *lhs_type = &t_any;
7891 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007892
7893 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007894 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007895 name = vim_strnsave(arg, varlen);
7896 if (name == NULL)
7897 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007898 if (*p == ':')
7899 {
7900 p = skipwhite(p + 1);
7901 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7902 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007903
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007904 // TODO: script var not supported?
7905 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7906 &vimvaridx, &type, cctx) == FAIL)
7907 goto failed;
7908 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007909 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007910 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7911 0, 0, type, name) == FAIL)
7912 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007913 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007914 else if (varlen == 1 && *arg == '_')
7915 {
7916 // Assigning to "_": drop the value.
7917 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7918 goto failed;
7919 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007920 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007921 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007922 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007923 {
7924 semsg(_(e_variable_already_declared), arg);
7925 goto failed;
7926 }
7927
Bram Moolenaarea870692020-12-02 14:24:30 +01007928 if (STRNCMP(name, "s:", 2) == 0)
7929 {
7930 semsg(_(e_cannot_declare_script_variable_in_function), name);
7931 goto failed;
7932 }
7933
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007934 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02007935 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007936 where.wt_variable = TRUE;
7937 if (lhs_type == &t_any)
7938 lhs_type = item_type;
7939 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02007940 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02007941 ? need_type(item_type, lhs_type,
7942 -1, 0, cctx, FALSE, FALSE)
7943 : check_type(lhs_type, item_type, TRUE, where))
7944 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007945 goto failed;
7946 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007947 if (var_lvar == NULL)
7948 // out of memory or used as an argument
7949 goto failed;
7950
7951 if (semicolon && idx == var_count - 1)
7952 var_lvar->lv_type = vartype;
7953 else
7954 var_lvar->lv_type = item_type;
7955 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7956 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007957
7958 if (*p == ',' || *p == ';')
7959 ++p;
7960 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007961 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007962 }
7963
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007964 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007965 {
7966 int save_prev_lnum = cctx->ctx_prev_lnum;
7967
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007968 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007969 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
7970 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007971 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007972 cctx->ctx_prev_lnum = save_prev_lnum;
7973 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007974
Bram Moolenaar792f7862020-11-23 08:31:18 +01007975 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007976
7977failed:
7978 vim_free(name);
7979 drop_scope(cctx);
7980 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007981}
7982
7983/*
7984 * compile "endfor"
7985 */
7986 static char_u *
7987compile_endfor(char_u *arg, cctx_T *cctx)
7988{
7989 garray_T *instr = &cctx->ctx_instr;
7990 scope_T *scope = cctx->ctx_scope;
7991 forscope_T *forscope;
7992 isn_T *isn;
7993
Bram Moolenaarfa984412021-03-25 22:15:28 +01007994 if (misplaced_cmdmod(cctx))
7995 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007997 if (scope == NULL || scope->se_type != FOR_SCOPE)
7998 {
7999 emsg(_(e_for));
8000 return NULL;
8001 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008002 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008003 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008004 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008005
8006 // At end of ":for" scope jump back to the FOR instruction.
8007 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8008
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008009 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008010 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8011 isn->isn_arg.forloop.for_end = instr->ga_len;
8012
8013 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008014 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008015
8016 // Below the ":for" scope drop the "expr" list from the stack.
8017 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8018 return NULL;
8019
8020 vim_free(scope);
8021
8022 return arg;
8023}
8024
8025/*
8026 * compile "while expr"
8027 *
8028 * Produces instructions:
8029 * top: EVAL expr Push result of "expr"
8030 * JUMP_IF_FALSE end jump if false
8031 * ... body ...
8032 * JUMP top Jump back to repeat
8033 * end:
8034 *
8035 */
8036 static char_u *
8037compile_while(char_u *arg, cctx_T *cctx)
8038{
8039 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008040 scope_T *scope;
8041
8042 scope = new_scope(cctx, WHILE_SCOPE);
8043 if (scope == NULL)
8044 return NULL;
8045
Bram Moolenaara91a7132021-03-25 21:12:15 +01008046 // "endwhile" jumps back here, one before when profiling or using cmdmods
8047 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008048
8049 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008050 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008051 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008052 if (!ends_excmd2(arg, skipwhite(p)))
8053 {
8054 semsg(_(e_trailing_arg), p);
8055 return NULL;
8056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008057
Bram Moolenaar13106602020-10-04 16:06:05 +02008058 if (bool_on_stack(cctx) == FAIL)
8059 return FAIL;
8060
Bram Moolenaara91a7132021-03-25 21:12:15 +01008061 // CMDMOD_REV must come before the jump
8062 generate_undo_cmdmods(cctx);
8063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008064 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008065 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008066 JUMP_IF_FALSE, cctx) == FAIL)
8067 return FAIL;
8068
8069 return p;
8070}
8071
8072/*
8073 * compile "endwhile"
8074 */
8075 static char_u *
8076compile_endwhile(char_u *arg, cctx_T *cctx)
8077{
8078 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008079 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008080
Bram Moolenaarfa984412021-03-25 22:15:28 +01008081 if (misplaced_cmdmod(cctx))
8082 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008083 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8084 {
8085 emsg(_(e_while));
8086 return NULL;
8087 }
8088 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008089 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008090
Bram Moolenaarf002a412021-01-24 13:34:18 +01008091#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008092 // count the endwhile before jumping
8093 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008094#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008096 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008097 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008098
8099 // Fill in the "end" label in the WHILE statement so it can jump here.
8100 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008101 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8102 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008103
8104 vim_free(scope);
8105
8106 return arg;
8107}
8108
8109/*
8110 * compile "continue"
8111 */
8112 static char_u *
8113compile_continue(char_u *arg, cctx_T *cctx)
8114{
8115 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008116 int try_scopes = 0;
8117 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008118
8119 for (;;)
8120 {
8121 if (scope == NULL)
8122 {
8123 emsg(_(e_continue));
8124 return NULL;
8125 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008126 if (scope->se_type == FOR_SCOPE)
8127 {
8128 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008130 }
8131 if (scope->se_type == WHILE_SCOPE)
8132 {
8133 loop_label = scope->se_u.se_while.ws_top_label;
8134 break;
8135 }
8136 if (scope->se_type == TRY_SCOPE)
8137 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008138 scope = scope->se_outer;
8139 }
8140
Bram Moolenaarc150c092021-02-13 15:02:46 +01008141 if (try_scopes > 0)
8142 // Inside one or more try/catch blocks we first need to jump to the
8143 // "finally" or "endtry" to cleanup.
8144 generate_TRYCONT(cctx, try_scopes, loop_label);
8145 else
8146 // Jump back to the FOR or WHILE instruction.
8147 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008149 return arg;
8150}
8151
8152/*
8153 * compile "break"
8154 */
8155 static char_u *
8156compile_break(char_u *arg, cctx_T *cctx)
8157{
8158 scope_T *scope = cctx->ctx_scope;
8159 endlabel_T **el;
8160
8161 for (;;)
8162 {
8163 if (scope == NULL)
8164 {
8165 emsg(_(e_break));
8166 return NULL;
8167 }
8168 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8169 break;
8170 scope = scope->se_outer;
8171 }
8172
8173 // Jump to the end of the FOR or WHILE loop.
8174 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008175 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008176 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008177 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008178 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8179 return FAIL;
8180
8181 return arg;
8182}
8183
8184/*
8185 * compile "{" start of block
8186 */
8187 static char_u *
8188compile_block(char_u *arg, cctx_T *cctx)
8189{
8190 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8191 return NULL;
8192 return skipwhite(arg + 1);
8193}
8194
8195/*
8196 * compile end of block: drop one scope
8197 */
8198 static void
8199compile_endblock(cctx_T *cctx)
8200{
8201 scope_T *scope = cctx->ctx_scope;
8202
8203 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008204 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008205 vim_free(scope);
8206}
8207
8208/*
8209 * compile "try"
8210 * Creates a new scope for the try-endtry, pointing to the first catch and
8211 * finally.
8212 * Creates another scope for the "try" block itself.
8213 * TRY instruction sets up exception handling at runtime.
8214 *
8215 * "try"
8216 * TRY -> catch1, -> finally push trystack entry
8217 * ... try block
8218 * "throw {exception}"
8219 * EVAL {exception}
8220 * THROW create exception
8221 * ... try block
8222 * " catch {expr}"
8223 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008224 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008225 * EVAL {expr}
8226 * MATCH
8227 * JUMP nomatch -> catch2
8228 * CATCH remove exception
8229 * ... catch block
8230 * " catch"
8231 * JUMP -> finally
8232 * catch2: CATCH remove exception
8233 * ... catch block
8234 * " finally"
8235 * finally:
8236 * ... finally block
8237 * " endtry"
8238 * ENDTRY pop trystack entry, may rethrow
8239 */
8240 static char_u *
8241compile_try(char_u *arg, cctx_T *cctx)
8242{
8243 garray_T *instr = &cctx->ctx_instr;
8244 scope_T *try_scope;
8245 scope_T *scope;
8246
Bram Moolenaarfa984412021-03-25 22:15:28 +01008247 if (misplaced_cmdmod(cctx))
8248 return NULL;
8249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008250 // scope that holds the jumps that go to catch/finally/endtry
8251 try_scope = new_scope(cctx, TRY_SCOPE);
8252 if (try_scope == NULL)
8253 return NULL;
8254
Bram Moolenaar69f70502021-01-01 16:10:46 +01008255 if (cctx->ctx_skip != SKIP_YES)
8256 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008257 isn_T *isn;
8258
8259 // "try_catch" is set when the first ":catch" is found or when no catch
8260 // is found and ":finally" is found.
8261 // "try_finally" is set when ":finally" is found
8262 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008263 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008264 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8265 return NULL;
8266 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8267 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008268 return NULL;
8269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008270
8271 // scope for the try block itself
8272 scope = new_scope(cctx, BLOCK_SCOPE);
8273 if (scope == NULL)
8274 return NULL;
8275
8276 return arg;
8277}
8278
8279/*
8280 * compile "catch {expr}"
8281 */
8282 static char_u *
8283compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8284{
8285 scope_T *scope = cctx->ctx_scope;
8286 garray_T *instr = &cctx->ctx_instr;
8287 char_u *p;
8288 isn_T *isn;
8289
Bram Moolenaarfa984412021-03-25 22:15:28 +01008290 if (misplaced_cmdmod(cctx))
8291 return NULL;
8292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008293 // end block scope from :try or :catch
8294 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8295 compile_endblock(cctx);
8296 scope = cctx->ctx_scope;
8297
8298 // Error if not in a :try scope
8299 if (scope == NULL || scope->se_type != TRY_SCOPE)
8300 {
8301 emsg(_(e_catch));
8302 return NULL;
8303 }
8304
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008305 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008306 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008307 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008308 return NULL;
8309 }
8310
Bram Moolenaar69f70502021-01-01 16:10:46 +01008311 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008312 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008313#ifdef FEAT_PROFILE
8314 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008315 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008316 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008317 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008318 .isn_type == ISN_PROF_START)
8319 --instr->ga_len;
8320#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008321 // Jump from end of previous block to :finally or :endtry
8322 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8323 JUMP_ALWAYS, cctx) == FAIL)
8324 return NULL;
8325
8326 // End :try or :catch scope: set value in ISN_TRY instruction
8327 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008328 if (isn->isn_arg.try.try_ref->try_catch == 0)
8329 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008330 if (scope->se_u.se_try.ts_catch_label != 0)
8331 {
8332 // Previous catch without match jumps here
8333 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8334 isn->isn_arg.jump.jump_where = instr->ga_len;
8335 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008336#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008337 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008338 {
8339 // a "throw" that jumps here needs to be counted
8340 generate_instr(cctx, ISN_PROF_END);
8341 // the "catch" is also counted
8342 generate_instr(cctx, ISN_PROF_START);
8343 }
8344#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008345 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008346 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008347 }
8348
8349 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008350 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008351 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008352 scope->se_u.se_try.ts_caught_all = TRUE;
8353 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008354 }
8355 else
8356 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008357 char_u *end;
8358 char_u *pat;
8359 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008360 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008361 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008363 // Push v:exception, push {expr} and MATCH
8364 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8365
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008366 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008367 if (*end != *p)
8368 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008369 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008370 vim_free(tofree);
8371 return FAIL;
8372 }
8373 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008374 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008375 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008376 len = (int)(end - tofree);
8377 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008378 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008379 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008380 if (pat == NULL)
8381 return FAIL;
8382 if (generate_PUSHS(cctx, pat) == FAIL)
8383 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008385 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8386 return NULL;
8387
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008388 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008389 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8390 return NULL;
8391 }
8392
Bram Moolenaar69f70502021-01-01 16:10:46 +01008393 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008394 return NULL;
8395
8396 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8397 return NULL;
8398 return p;
8399}
8400
8401 static char_u *
8402compile_finally(char_u *arg, cctx_T *cctx)
8403{
8404 scope_T *scope = cctx->ctx_scope;
8405 garray_T *instr = &cctx->ctx_instr;
8406 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008407 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008408
Bram Moolenaarfa984412021-03-25 22:15:28 +01008409 if (misplaced_cmdmod(cctx))
8410 return NULL;
8411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008412 // end block scope from :try or :catch
8413 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8414 compile_endblock(cctx);
8415 scope = cctx->ctx_scope;
8416
8417 // Error if not in a :try scope
8418 if (scope == NULL || scope->se_type != TRY_SCOPE)
8419 {
8420 emsg(_(e_finally));
8421 return NULL;
8422 }
8423
8424 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008425 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008426 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008427 {
8428 emsg(_(e_finally_dup));
8429 return NULL;
8430 }
8431
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008432 this_instr = instr->ga_len;
8433#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008434 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008435 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008436 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008437 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008438 // jump to the profile start of the "finally"
8439 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008440
8441 // jump to the profile end above it
8442 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8443 .isn_type == ISN_PROF_END)
8444 --this_instr;
8445 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008446#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008447
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008448 // Fill in the "end" label in jumps at the end of the blocks.
8449 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8450 this_instr, cctx);
8451
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008452 // If there is no :catch then an exception jumps to :finally.
8453 if (isn->isn_arg.try.try_ref->try_catch == 0)
8454 isn->isn_arg.try.try_ref->try_catch = this_instr;
8455 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008456 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008457 {
8458 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008459 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008460 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008461 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008462 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008463 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8464 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008466 // TODO: set index in ts_finally_label jumps
8467
8468 return arg;
8469}
8470
8471 static char_u *
8472compile_endtry(char_u *arg, cctx_T *cctx)
8473{
8474 scope_T *scope = cctx->ctx_scope;
8475 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008476 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008477
Bram Moolenaarfa984412021-03-25 22:15:28 +01008478 if (misplaced_cmdmod(cctx))
8479 return NULL;
8480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008481 // end block scope from :catch or :finally
8482 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8483 compile_endblock(cctx);
8484 scope = cctx->ctx_scope;
8485
8486 // Error if not in a :try scope
8487 if (scope == NULL || scope->se_type != TRY_SCOPE)
8488 {
8489 if (scope == NULL)
8490 emsg(_(e_no_endtry));
8491 else if (scope->se_type == WHILE_SCOPE)
8492 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008493 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008494 emsg(_(e_endfor));
8495 else
8496 emsg(_(e_endif));
8497 return NULL;
8498 }
8499
Bram Moolenaarc150c092021-02-13 15:02:46 +01008500 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008501 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008502 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008503 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8504 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008505 {
8506 emsg(_(e_missing_catch_or_finally));
8507 return NULL;
8508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008509
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008510#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008511 if (cctx->ctx_compile_type == CT_PROFILE
8512 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008513 .isn_type == ISN_PROF_START)
8514 // move the profile start after "endtry" so that it's not counted when
8515 // the exception is rethrown.
8516 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008517#endif
8518
Bram Moolenaar69f70502021-01-01 16:10:46 +01008519 // Fill in the "end" label in jumps at the end of the blocks, if not
8520 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008521 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8522 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008523
Bram Moolenaar69f70502021-01-01 16:10:46 +01008524 if (scope->se_u.se_try.ts_catch_label != 0)
8525 {
8526 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008527 isn_T *isn = ((isn_T *)instr->ga_data)
8528 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008529 isn->isn_arg.jump.jump_where = instr->ga_len;
8530 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008531 }
8532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008533 compile_endblock(cctx);
8534
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008535 if (cctx->ctx_skip != SKIP_YES)
8536 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008537 // End :catch or :finally scope: set instruction index in ISN_TRY
8538 // instruction
8539 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008540 if (cctx->ctx_skip != SKIP_YES
8541 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8542 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008543#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008544 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008545 generate_instr(cctx, ISN_PROF_START);
8546#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008548 return arg;
8549}
8550
8551/*
8552 * compile "throw {expr}"
8553 */
8554 static char_u *
8555compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8556{
8557 char_u *p = skipwhite(arg);
8558
Bram Moolenaara5565e42020-05-09 15:44:01 +02008559 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008560 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008561 if (cctx->ctx_skip == SKIP_YES)
8562 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008563 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008564 return NULL;
8565 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8566 return NULL;
8567
8568 return p;
8569}
8570
Bram Moolenaarc3235272021-07-10 19:42:03 +02008571 static char_u *
8572compile_eval(char_u *arg, cctx_T *cctx)
8573{
8574 char_u *p = arg;
8575 int name_only;
8576 char_u *alias;
8577 long lnum = SOURCING_LNUM;
8578
8579 // find_ex_command() will consider a variable name an expression, assuming
8580 // that something follows on the next line. Check that something actually
8581 // follows, otherwise it's probably a misplaced command.
8582 get_name_len(&p, &alias, FALSE, FALSE);
8583 name_only = ends_excmd2(arg, skipwhite(p));
8584 vim_free(alias);
8585
8586 p = arg;
8587 if (compile_expr0(&p, cctx) == FAIL)
8588 return NULL;
8589
8590 if (name_only && lnum == SOURCING_LNUM)
8591 {
8592 semsg(_(e_expression_without_effect_str), arg);
8593 return NULL;
8594 }
8595
8596 // drop the result
8597 generate_instr_drop(cctx, ISN_DROP, 1);
8598
8599 return skipwhite(p);
8600}
8601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008602/*
8603 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008604 * compile "echomsg expr"
8605 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008606 * compile "execute expr"
8607 */
8608 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008609compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008610{
8611 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008612 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008613 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008614 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008615 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008616 garray_T *stack = &cctx->ctx_type_stack;
8617 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008618
8619 for (;;)
8620 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008621 if (ends_excmd2(prev, p))
8622 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008623 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008624 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008625 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008626
8627 if (cctx->ctx_skip != SKIP_YES)
8628 {
8629 // check for non-void type
8630 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8631 if (type->tt_type == VAR_VOID)
8632 {
8633 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8634 return NULL;
8635 }
8636 }
8637
Bram Moolenaarad39c092020-02-26 18:23:43 +01008638 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008639 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008640 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008641 }
8642
Bram Moolenaare4984292020-12-13 14:19:25 +01008643 if (count > 0)
8644 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008645 long save_lnum = cctx->ctx_lnum;
8646
8647 // Use the line number where the command started.
8648 cctx->ctx_lnum = start_ctx_lnum;
8649
Bram Moolenaare4984292020-12-13 14:19:25 +01008650 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8651 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8652 else if (cmdidx == CMD_execute)
8653 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8654 else if (cmdidx == CMD_echomsg)
8655 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8656 else
8657 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008658
8659 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008660 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008661 return p;
8662}
8663
8664/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008665 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008666 * instruction to compute it and return OK.
8667 * Otherwise return FAIL, the caller must deal with any range.
8668 */
8669 static int
8670compile_variable_range(exarg_T *eap, cctx_T *cctx)
8671{
8672 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8673 char_u *p = skipdigits(eap->cmd);
8674
8675 if (p == range_end)
8676 return FAIL;
8677 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8678}
8679
8680/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008681 * :put r
8682 * :put ={expr}
8683 */
8684 static char_u *
8685compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8686{
8687 char_u *line = arg;
8688 linenr_T lnum;
8689 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008690 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008691
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008692 eap->regname = *line;
8693
8694 if (eap->regname == '=')
8695 {
8696 char_u *p = line + 1;
8697
8698 if (compile_expr0(&p, cctx) == FAIL)
8699 return NULL;
8700 line = p;
8701 }
8702 else if (eap->regname != NUL)
8703 ++line;
8704
Bram Moolenaar08597872020-12-10 19:43:40 +01008705 if (compile_variable_range(eap, cctx) == OK)
8706 {
8707 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8708 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008709 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008710 {
8711 // Either no range or a number.
8712 // "errormsg" will not be set because the range is ADDR_LINES.
8713 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008714 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008715 return NULL;
8716 if (eap->addr_count == 0)
8717 lnum = -1;
8718 else
8719 lnum = eap->line2;
8720 if (above)
8721 --lnum;
8722 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008723
8724 generate_PUT(cctx, eap->regname, lnum);
8725 return line;
8726}
8727
8728/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008729 * A command that is not compiled, execute with legacy code.
8730 */
8731 static char_u *
8732compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8733{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008734 char_u *p;
8735 int has_expr = FALSE;
8736 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008737
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008738 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008739 goto theend;
8740
Bram Moolenaare729ce22021-06-06 21:38:09 +02008741 // If there was a prececing command modifier, drop it and include it in the
8742 // EXEC command.
8743 if (cctx->ctx_has_cmdmod)
8744 {
8745 garray_T *instr = &cctx->ctx_instr;
8746 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8747
8748 if (isn->isn_type == ISN_CMDMOD)
8749 {
8750 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8751 ->cmod_filter_regmatch.regprog);
8752 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8753 --instr->ga_len;
8754 cctx->ctx_has_cmdmod = FALSE;
8755 }
8756 }
8757
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008758 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008759 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008760 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008761 int usefilter = FALSE;
8762
8763 has_expr = argt & (EX_XFILE | EX_EXPAND);
8764
8765 // If the command can be followed by a bar, find the bar and truncate
8766 // it, so that the following command can be compiled.
8767 // The '|' is overwritten with a NUL, it is put back below.
8768 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8769 && *eap->arg == '!')
8770 // :w !filter or :r !filter or :r! filter
8771 usefilter = TRUE;
8772 if ((argt & EX_TRLBAR) && !usefilter)
8773 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008774 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008775 separate_nextcmd(eap);
8776 if (eap->nextcmd != NULL)
8777 nextcmd = eap->nextcmd;
8778 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008779 else if (eap->cmdidx == CMD_wincmd)
8780 {
8781 p = eap->arg;
8782 if (*p != NUL)
8783 ++p;
8784 if (*p == 'g' || *p == Ctrl_G)
8785 ++p;
8786 p = skipwhite(p);
8787 if (*p == '|')
8788 {
8789 *p = NUL;
8790 nextcmd = p + 1;
8791 }
8792 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008793 }
8794
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008795 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8796 {
8797 // expand filename in "syntax include [@group] filename"
8798 has_expr = TRUE;
8799 eap->arg = skipwhite(eap->arg + 7);
8800 if (*eap->arg == '@')
8801 eap->arg = skiptowhite(eap->arg);
8802 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008803
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008804 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8805 && STRLEN(eap->arg) > 4)
8806 {
8807 int delim = *eap->arg;
8808
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008809 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008810 if (*p == delim)
8811 {
8812 eap->arg = p + 1;
8813 has_expr = TRUE;
8814 }
8815 }
8816
Bram Moolenaarecac5912021-01-05 19:23:28 +01008817 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8818 {
8819 // TODO: should only expand when appropriate for the command
8820 eap->arg = skiptowhite(eap->arg);
8821 has_expr = TRUE;
8822 }
8823
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008824 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008825 {
8826 int count = 0;
8827 char_u *start = skipwhite(line);
8828
8829 // :cmd xxx`=expr1`yyy`=expr2`zzz
8830 // PUSHS ":cmd xxx"
8831 // eval expr1
8832 // PUSHS "yyy"
8833 // eval expr2
8834 // PUSHS "zzz"
8835 // EXECCONCAT 5
8836 for (;;)
8837 {
8838 if (p > start)
8839 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008840 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008841 ++count;
8842 }
8843 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008844 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008845 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008846 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008847 ++count;
8848 p = skipwhite(p);
8849 if (*p != '`')
8850 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008851 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008852 return NULL;
8853 }
8854 start = p + 1;
8855
8856 p = (char_u *)strstr((char *)start, "`=");
8857 if (p == NULL)
8858 {
8859 if (*skipwhite(start) != NUL)
8860 {
8861 generate_PUSHS(cctx, vim_strsave(start));
8862 ++count;
8863 }
8864 break;
8865 }
8866 }
8867 generate_EXECCONCAT(cctx, count);
8868 }
8869 else
8870 generate_EXEC(cctx, line);
8871
8872theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008873 if (*nextcmd != NUL)
8874 {
8875 // the parser expects a pointer to the bar, put it back
8876 --nextcmd;
8877 *nextcmd = '|';
8878 }
8879
8880 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008881}
8882
Bram Moolenaar20677332021-06-06 17:02:53 +02008883/*
8884 * A script command with heredoc, e.g.
8885 * ruby << EOF
8886 * command
8887 * EOF
8888 * Has been turned into one long line with NL characters by
8889 * get_function_body():
8890 * ruby << EOF<NL> command<NL>EOF
8891 */
8892 static char_u *
8893compile_script(char_u *line, cctx_T *cctx)
8894{
8895 if (cctx->ctx_skip != SKIP_YES)
8896 {
8897 isn_T *isn;
8898
8899 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8900 return NULL;
8901 isn->isn_arg.string = vim_strsave(line);
8902 }
8903 return (char_u *)"";
8904}
8905
Bram Moolenaar8238f082021-04-20 21:10:48 +02008906
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008907/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008908 * :s/pat/repl/
8909 */
8910 static char_u *
8911compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8912{
8913 char_u *cmd = eap->arg;
8914 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8915
8916 if (expr != NULL)
8917 {
8918 int delimiter = *cmd++;
8919
8920 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008921 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008922 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8923 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008924 garray_T save_ga = cctx->ctx_instr;
8925 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008926 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008927 int trailing_error;
8928 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008929 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008930 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008931
8932 cmd += 3;
8933 end = skip_substitute(cmd, delimiter);
8934
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008935 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008936 // labels are correct.
8937 cctx->ctx_instr.ga_len = 0;
8938 cctx->ctx_instr.ga_maxlen = 0;
8939 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008940 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008941 if (end[-1] == NUL)
8942 end[-1] = delimiter;
8943 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008944 trailing_error = *cmd != delimiter && *cmd != NUL;
8945
Bram Moolenaar169502f2021-04-21 12:19:35 +02008946 if (expr_res == FAIL || trailing_error
8947 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008948 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008949 if (trailing_error)
8950 semsg(_(e_trailing_arg), cmd);
8951 clear_instr_ga(&cctx->ctx_instr);
8952 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008953 return NULL;
8954 }
8955
Bram Moolenaar8238f082021-04-20 21:10:48 +02008956 // Move the generated instructions into the ISN_SUBSTITUTE
8957 // instructions, then restore the list of instructions before
8958 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008959 instr_count = cctx->ctx_instr.ga_len;
8960 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008961 instr[instr_count].isn_type = ISN_FINISH;
8962
8963 cctx->ctx_instr = save_ga;
8964 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8965 {
8966 int idx;
8967
8968 for (idx = 0; idx < instr_count; ++idx)
8969 delete_instr(instr + idx);
8970 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008971 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008972 }
8973 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8974 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008975
8976 // skip over flags
8977 if (*end == '&')
8978 ++end;
8979 while (ASCII_ISALPHA(*end) || *end == '#')
8980 ++end;
8981 return end;
8982 }
8983 }
8984
8985 return compile_exec(arg, eap, cctx);
8986}
8987
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008988 static char_u *
8989compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8990{
8991 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008992 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008993
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008994 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008995 {
8996 if (STRNCMP(arg, "END", 3) == 0)
8997 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008998 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008999 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009000 // First load the current variable value.
9001 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9002 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009003 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009004 }
9005
9006 // Gets the redirected text and put it on the stack, then store it
9007 // in the variable.
9008 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9009
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009010 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009011 generate_instr_drop(cctx, ISN_CONCAT, 1);
9012
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009013 if (lhs->lhs_has_index)
9014 {
9015 // Use the info in "lhs" to store the value at the index in the
9016 // list or dict.
9017 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9018 &t_string, cctx) == FAIL)
9019 return NULL;
9020 }
9021 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009022 return NULL;
9023
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009024 VIM_CLEAR(lhs->lhs_name);
9025 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009026 return arg + 3;
9027 }
9028 emsg(_(e_cannot_nest_redir));
9029 return NULL;
9030 }
9031
9032 if (arg[0] == '=' && arg[1] == '>')
9033 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009034 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009035
9036 // redirect to a variable is compiled
9037 arg += 2;
9038 if (*arg == '>')
9039 {
9040 ++arg;
9041 append = TRUE;
9042 }
9043 arg = skipwhite(arg);
9044
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009045 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009046 FALSE, FALSE, 1, cctx) == FAIL)
9047 return NULL;
9048 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009049 lhs->lhs_append = append;
9050 if (lhs->lhs_has_index)
9051 {
9052 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9053 if (lhs->lhs_whole == NULL)
9054 return NULL;
9055 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009056
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009057 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009058 }
9059
9060 // other redirects are handled like at script level
9061 return compile_exec(line, eap, cctx);
9062}
9063
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009064#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009065 static char_u *
9066compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9067{
9068 isn_T *isn;
9069 char_u *p;
9070
9071 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9072 if (isn == NULL)
9073 return NULL;
9074 isn->isn_arg.number = eap->cmdidx;
9075
9076 p = eap->arg;
9077 if (compile_expr0(&p, cctx) == FAIL)
9078 return NULL;
9079
9080 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9081 if (isn == NULL)
9082 return NULL;
9083 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9084 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9085 return NULL;
9086 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9087 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9088 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9089
9090 return p;
9091}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009092#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009093
Bram Moolenaar4c137212021-04-19 16:48:48 +02009094/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009095 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009096 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009097 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009098 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009099add_def_function(ufunc_T *ufunc)
9100{
9101 dfunc_T *dfunc;
9102
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009103 if (def_functions.ga_len == 0)
9104 {
9105 // The first position is not used, so that a zero uf_dfunc_idx means it
9106 // wasn't set.
9107 if (ga_grow(&def_functions, 1) == FAIL)
9108 return FAIL;
9109 ++def_functions.ga_len;
9110 }
9111
Bram Moolenaar09689a02020-05-09 22:50:08 +02009112 // Add the function to "def_functions".
9113 if (ga_grow(&def_functions, 1) == FAIL)
9114 return FAIL;
9115 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9116 CLEAR_POINTER(dfunc);
9117 dfunc->df_idx = def_functions.ga_len;
9118 ufunc->uf_dfunc_idx = dfunc->df_idx;
9119 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009120 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009121 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009122 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009123 ++def_functions.ga_len;
9124 return OK;
9125}
9126
9127/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009128 * After ex_function() has collected all the function lines: parse and compile
9129 * the lines into instructions.
9130 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009131 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9132 * the return statement (used for lambda). When uf_ret_type is already set
9133 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009134 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009135 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009136 * This can be used recursively through compile_lambda(), which may reallocate
9137 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009138 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009139 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009140 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009141compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009142 ufunc_T *ufunc,
9143 int check_return_type,
9144 compiletype_T compile_type,
9145 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009146{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009147 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009148 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009149 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009150 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009151 cctx_T cctx;
9152 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009153 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009154 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009155 int ret = FAIL;
9156 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009157 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009158 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009159 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009160 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009161#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009162 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009163#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009164 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009165
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009166 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009167 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009168 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009169 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009170 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9171 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009172 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009173
9174 switch (compile_type)
9175 {
9176 case CT_PROFILE:
9177#ifdef FEAT_PROFILE
9178 instr_dest = dfunc->df_instr_prof; break;
9179#endif
9180 case CT_NONE: instr_dest = dfunc->df_instr; break;
9181 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9182 }
9183 if (instr_dest != NULL)
9184 // Was compiled in this mode before: Free old instructions.
9185 delete_def_function_contents(dfunc, FALSE);
9186 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009187 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009188 else
9189 {
9190 if (add_def_function(ufunc) == FAIL)
9191 return FAIL;
9192 new_def_function = TRUE;
9193 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009194
Bram Moolenaar985116a2020-07-12 17:31:09 +02009195 ufunc->uf_def_status = UF_COMPILING;
9196
Bram Moolenaara80faa82020-04-12 19:37:17 +02009197 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009198
Bram Moolenaare99d4222021-06-13 14:01:26 +02009199 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009200 cctx.ctx_ufunc = ufunc;
9201 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009202 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009203 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9204 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9205 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9206 cctx.ctx_type_list = &ufunc->uf_type_list;
9207 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9208 instr = &cctx.ctx_instr;
9209
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009210 // Set the context to the function, it may be compiled when called from
9211 // another script. Set the script version to the most modern one.
9212 // The line number will be set in next_line_from_context().
9213 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009214 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9215
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009216 // Don't use the flag from ":legacy" here.
9217 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9218
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009219 // Make sure error messages are OK.
9220 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9221 if (do_estack_push)
9222 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009223 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009224
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009225 if (ufunc->uf_def_args.ga_len > 0)
9226 {
9227 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009228 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009229 int i;
9230 char_u *arg;
9231 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009232 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009233
9234 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009235 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009236 for (i = 0; i < count; ++i)
9237 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009238 garray_T *stack = &cctx.ctx_type_stack;
9239 type_T *val_type;
9240 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009241 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009242 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009243 int jump_instr_idx = instr->ga_len;
9244 isn_T *isn;
9245
9246 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9247 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9248 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009249
9250 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009251 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009252
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009253 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009254 r = compile_expr0(&arg, &cctx);
9255
Bram Moolenaar12bce952021-03-11 20:04:04 +01009256 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009257 goto erret;
9258
9259 // If no type specified use the type of the default value.
9260 // Otherwise check that the default value type matches the
9261 // specified type.
9262 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009263 where.wt_index = arg_idx + 1;
9264 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009265 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009266 {
9267 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009268 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009269 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009270 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009271 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009272 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009273
9274 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009275 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009276
9277 // set instruction index in JUMP_IF_ARG_SET to here
9278 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9279 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009280 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009281
9282 if (did_set_arg_type)
9283 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009284 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009285 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009286
9287 /*
9288 * Loop over all the lines of the function and generate instructions.
9289 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009290 for (;;)
9291 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009292 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009293 int starts_with_colon = FALSE;
9294 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009295 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009296
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009297 // Bail out on the first error to avoid a flood of errors and report
9298 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009299 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009300 goto erret;
9301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009302 if (line != NULL && *line == '|')
9303 // the line continues after a '|'
9304 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009305 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009306 && !(*line == '#' && (line == cctx.ctx_line_start
9307 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009308 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009309 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009310 goto erret;
9311 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009312 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9313 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009314 else
9315 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009316 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009317 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009318 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009319 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009320#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009321 if (cctx.ctx_skip != SKIP_YES)
9322 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009323#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009324 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009325 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009326 // Make a copy, splitting off nextcmd and removing trailing spaces
9327 // may change it.
9328 if (line != NULL)
9329 {
9330 line = vim_strsave(line);
9331 vim_free(line_to_free);
9332 line_to_free = line;
9333 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009334 }
9335
Bram Moolenaara80faa82020-04-12 19:37:17 +02009336 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009337 ea.cmdlinep = &line;
9338 ea.cmd = skipwhite(line);
9339
Bram Moolenaarb2049902021-01-24 12:53:53 +01009340 if (*ea.cmd == '#')
9341 {
9342 // "#" starts a comment
9343 line = (char_u *)"";
9344 continue;
9345 }
9346
Bram Moolenaarf002a412021-01-24 13:34:18 +01009347#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009348 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9349 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009350 {
9351 may_generate_prof_end(&cctx, prof_lnum);
9352
9353 prof_lnum = cctx.ctx_lnum;
9354 generate_instr(&cctx, ISN_PROF_START);
9355 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009356#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009357 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9358 && cctx.ctx_skip != SKIP_YES)
9359 {
9360 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009361 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009362 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009363 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009364
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009365 // Some things can be recognized by the first character.
9366 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009367 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009368 case '}':
9369 {
9370 // "}" ends a block scope
9371 scopetype_T stype = cctx.ctx_scope == NULL
9372 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009373
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009374 if (stype == BLOCK_SCOPE)
9375 {
9376 compile_endblock(&cctx);
9377 line = ea.cmd;
9378 }
9379 else
9380 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009381 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009382 goto erret;
9383 }
9384 if (line != NULL)
9385 line = skipwhite(ea.cmd + 1);
9386 continue;
9387 }
9388
9389 case '{':
9390 // "{" starts a block scope
9391 // "{'a': 1}->func() is something else
9392 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9393 {
9394 line = compile_block(ea.cmd, &cctx);
9395 continue;
9396 }
9397 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009398 }
9399
9400 /*
9401 * COMMAND MODIFIERS
9402 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009403 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009404 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9405 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009406 {
9407 if (errormsg != NULL)
9408 goto erret;
9409 // empty line or comment
9410 line = (char_u *)"";
9411 continue;
9412 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009413 generate_cmdmods(&cctx, &local_cmdmod);
9414 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009415
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009416 // Check if there was a colon after the last command modifier or before
9417 // the current position.
9418 for (p = ea.cmd; p >= line; --p)
9419 {
9420 if (*p == ':')
9421 starts_with_colon = TRUE;
9422 if (p < ea.cmd && !VIM_ISWHITE(*p))
9423 break;
9424 }
9425
Bram Moolenaarce024c32021-06-26 13:00:49 +02009426 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009427 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009428 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009429 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009430 if (checkforcmd(&ea.cmd, "call", 3))
9431 {
9432 if (*ea.cmd == '(')
9433 // not for "call()"
9434 ea.cmd = p;
9435 else
9436 ea.cmd = skipwhite(ea.cmd);
9437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009438
Bram Moolenaarce024c32021-06-26 13:00:49 +02009439 if (!starts_with_colon)
9440 {
9441 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009442
Bram Moolenaarce024c32021-06-26 13:00:49 +02009443 // Check for assignment after command modifiers.
9444 assign = may_compile_assignment(&ea, &line, &cctx);
9445 if (assign == OK)
9446 goto nextline;
9447 if (assign == FAIL)
9448 goto erret;
9449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009450 }
9451
9452 /*
9453 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009454 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009455 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009456 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009457 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009458 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9459 && (starts_with_colon || !(*cmd == '\''
9460 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009461 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009462 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009463 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009464 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009465 if (!starts_with_colon)
9466 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009467 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009468 goto erret;
9469 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009470 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009471 if (ends_excmd2(line, ea.cmd))
9472 {
9473 // A range without a command: jump to the line.
9474 // TODO: compile to a more efficient command, possibly
9475 // calling parse_cmd_address().
9476 ea.cmdidx = CMD_SIZE;
9477 line = compile_exec(line, &ea, &cctx);
9478 goto nextline;
9479 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009480 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009481 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009482 p = find_ex_command(&ea, NULL, starts_with_colon
9483 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009484
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009485 if (p == NULL)
9486 {
9487 if (cctx.ctx_skip != SKIP_YES)
9488 emsg(_(e_ambiguous_use_of_user_defined_command));
9489 goto erret;
9490 }
9491
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009492 // When using ":legacy cmd" always use compile_exec().
9493 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009494 {
9495 char_u *start = ea.cmd;
9496
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009497 switch (ea.cmdidx)
9498 {
9499 case CMD_if:
9500 case CMD_elseif:
9501 case CMD_else:
9502 case CMD_endif:
9503 case CMD_for:
9504 case CMD_endfor:
9505 case CMD_continue:
9506 case CMD_break:
9507 case CMD_while:
9508 case CMD_endwhile:
9509 case CMD_try:
9510 case CMD_catch:
9511 case CMD_finally:
9512 case CMD_endtry:
9513 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9514 goto erret;
9515 default: break;
9516 }
9517
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009518 // ":legacy return expr" needs to be handled differently.
9519 if (checkforcmd(&start, "return", 4))
9520 ea.cmdidx = CMD_return;
9521 else
9522 ea.cmdidx = CMD_legacy;
9523 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009525 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9526 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009527 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009528 {
9529 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009530 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009531 }
9532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009533 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009534 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009535 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009536 // CMD_var cannot happen, compile_assignment() above would be
9537 // used. Most likely an assignment to a non-existing variable.
9538 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009539 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009540 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009541 }
9542
Bram Moolenaar3988f642020-08-27 22:43:03 +02009543 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009544 && ea.cmdidx != CMD_elseif
9545 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009546 && ea.cmdidx != CMD_endif
9547 && ea.cmdidx != CMD_endfor
9548 && ea.cmdidx != CMD_endwhile
9549 && ea.cmdidx != CMD_catch
9550 && ea.cmdidx != CMD_finally
9551 && ea.cmdidx != CMD_endtry)
9552 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009553 emsg(_(e_unreachable_code_after_return));
9554 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009555 }
9556
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009557 p = skipwhite(p);
9558 if (ea.cmdidx != CMD_SIZE
9559 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9560 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009561 if (ea.cmdidx >= 0)
9562 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009563 if ((ea.argt & EX_BANG) && *p == '!')
9564 {
9565 ea.forceit = TRUE;
9566 p = skipwhite(p + 1);
9567 }
9568 }
9569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009570 switch (ea.cmdidx)
9571 {
9572 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009573 ea.arg = p;
9574 line = compile_nested_function(&ea, &cctx);
9575 break;
9576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009577 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009578 // TODO: should we allow this, e.g. to declare a global
9579 // function?
9580 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009581 goto erret;
9582
9583 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009584 line = compile_return(p, check_return_type,
9585 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009586 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009587 break;
9588
9589 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009590 emsg(_(e_cannot_use_let_in_vim9_script));
9591 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009592 case CMD_var:
9593 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009594 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009595 case CMD_increment:
9596 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009597 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009598 if (line == p)
9599 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009600 break;
9601
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009602 case CMD_unlet:
9603 case CMD_unlockvar:
9604 case CMD_lockvar:
9605 line = compile_unletlock(p, &ea, &cctx);
9606 break;
9607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009608 case CMD_import:
9609 line = compile_import(p, &cctx);
9610 break;
9611
9612 case CMD_if:
9613 line = compile_if(p, &cctx);
9614 break;
9615 case CMD_elseif:
9616 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009617 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009618 break;
9619 case CMD_else:
9620 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009621 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009622 break;
9623 case CMD_endif:
9624 line = compile_endif(p, &cctx);
9625 break;
9626
9627 case CMD_while:
9628 line = compile_while(p, &cctx);
9629 break;
9630 case CMD_endwhile:
9631 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009632 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009633 break;
9634
9635 case CMD_for:
9636 line = compile_for(p, &cctx);
9637 break;
9638 case CMD_endfor:
9639 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009640 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009641 break;
9642 case CMD_continue:
9643 line = compile_continue(p, &cctx);
9644 break;
9645 case CMD_break:
9646 line = compile_break(p, &cctx);
9647 break;
9648
9649 case CMD_try:
9650 line = compile_try(p, &cctx);
9651 break;
9652 case CMD_catch:
9653 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009654 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009655 break;
9656 case CMD_finally:
9657 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009658 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009659 break;
9660 case CMD_endtry:
9661 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009662 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009663 break;
9664 case CMD_throw:
9665 line = compile_throw(p, &cctx);
9666 break;
9667
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009668 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009669 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009670 break;
9671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009672 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009673 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009674 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009675 case CMD_echomsg:
9676 case CMD_echoerr:
9677 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009678 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009679
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009680 case CMD_put:
9681 ea.cmd = cmd;
9682 line = compile_put(p, &ea, &cctx);
9683 break;
9684
Bram Moolenaar4c137212021-04-19 16:48:48 +02009685 case CMD_substitute:
9686 if (cctx.ctx_skip == SKIP_YES)
9687 line = (char_u *)"";
9688 else
9689 {
9690 ea.arg = p;
9691 line = compile_substitute(line, &ea, &cctx);
9692 }
9693 break;
9694
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009695 case CMD_redir:
9696 ea.arg = p;
9697 line = compile_redir(line, &ea, &cctx);
9698 break;
9699
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009700 case CMD_cexpr:
9701 case CMD_lexpr:
9702 case CMD_caddexpr:
9703 case CMD_laddexpr:
9704 case CMD_cgetexpr:
9705 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009706#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009707 ea.arg = p;
9708 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009709#else
9710 ex_ni(&ea);
9711 line = NULL;
9712#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009713 break;
9714
Bram Moolenaar3988f642020-08-27 22:43:03 +02009715 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009716
Bram Moolenaarae616492020-07-28 20:07:27 +02009717 case CMD_append:
9718 case CMD_change:
9719 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009720 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009721 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009722 case CMD_xit:
9723 not_in_vim9(&ea);
9724 goto erret;
9725
Bram Moolenaar002262f2020-07-08 17:47:57 +02009726 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009727 if (cctx.ctx_skip != SKIP_YES)
9728 {
9729 semsg(_(e_invalid_command_str), ea.cmd);
9730 goto erret;
9731 }
9732 // We don't check for a next command here.
9733 line = (char_u *)"";
9734 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009735
Bram Moolenaar20677332021-06-06 17:02:53 +02009736 case CMD_lua:
9737 case CMD_mzscheme:
9738 case CMD_perl:
9739 case CMD_py3:
9740 case CMD_python3:
9741 case CMD_python:
9742 case CMD_pythonx:
9743 case CMD_ruby:
9744 case CMD_tcl:
9745 ea.arg = p;
9746 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009747 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009748 else
9749 // heredoc lines have been concatenated with NL
9750 // characters in get_function_body()
9751 line = compile_script(line, &cctx);
9752 break;
9753
9754 default:
9755 // Not recognized, execute with do_cmdline_cmd().
9756 ea.arg = p;
9757 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009758 break;
9759 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009760nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009761 if (line == NULL)
9762 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009763 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009764
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009765 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009766 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009768 if (cctx.ctx_type_stack.ga_len < 0)
9769 {
9770 iemsg("Type stack underflow");
9771 goto erret;
9772 }
9773 }
9774
9775 if (cctx.ctx_scope != NULL)
9776 {
9777 if (cctx.ctx_scope->se_type == IF_SCOPE)
9778 emsg(_(e_endif));
9779 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9780 emsg(_(e_endwhile));
9781 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9782 emsg(_(e_endfor));
9783 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009784 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009785 goto erret;
9786 }
9787
Bram Moolenaarefd88552020-06-18 20:50:10 +02009788 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009789 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009790 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9791 ufunc->uf_ret_type = &t_void;
9792 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009793 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009794 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009795 goto erret;
9796 }
9797
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009798 // Return void if there is no return at the end.
9799 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009800 }
9801
Bram Moolenaar599410c2021-04-10 14:03:43 +02009802 // When compiled with ":silent!" and there was an error don't consider the
9803 // function compiled.
9804 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009805 {
9806 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9807 + ufunc->uf_dfunc_idx;
9808 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009809 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009810#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009811 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009812 {
9813 dfunc->df_instr_prof = instr->ga_data;
9814 dfunc->df_instr_prof_count = instr->ga_len;
9815 }
9816 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009817#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009818 if (cctx.ctx_compile_type == CT_DEBUG)
9819 {
9820 dfunc->df_instr_debug = instr->ga_data;
9821 dfunc->df_instr_debug_count = instr->ga_len;
9822 }
9823 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009824 {
9825 dfunc->df_instr = instr->ga_data;
9826 dfunc->df_instr_count = instr->ga_len;
9827 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009828 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009829 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009830 if (cctx.ctx_outer_used)
9831 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009832 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009834
9835 ret = OK;
9836
9837erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009838 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009839 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009840 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9841 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009842
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009843 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009844 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009845 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009846 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009847
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009848 // If using the last entry in the table and it was added above, we
9849 // might as well remove it.
9850 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009851 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009852 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009853 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009854 ufunc->uf_dfunc_idx = 0;
9855 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009856 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009857
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009858 while (cctx.ctx_scope != NULL)
9859 drop_scope(&cctx);
9860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009861 if (errormsg != NULL)
9862 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009863 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009864 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009865 }
9866
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009867 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9868 {
9869 if (ret == OK)
9870 {
9871 emsg(_(e_missing_redir_end));
9872 ret = FAIL;
9873 }
9874 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009875 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009876 }
9877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009878 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009879 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009880 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009881 if (do_estack_push)
9882 estack_pop();
9883
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009884 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009885 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009886 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009887 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009888 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009889}
9890
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009891 void
9892set_function_type(ufunc_T *ufunc)
9893{
9894 int varargs = ufunc->uf_va_name != NULL;
9895 int argcount = ufunc->uf_args.ga_len;
9896
9897 // Create a type for the function, with the return type and any
9898 // argument types.
9899 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9900 // The type is included in "tt_args".
9901 if (argcount > 0 || varargs)
9902 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009903 if (ufunc->uf_type_list.ga_itemsize == 0)
9904 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009905 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9906 argcount, &ufunc->uf_type_list);
9907 // Add argument types to the function type.
9908 if (func_type_add_arg_types(ufunc->uf_func_type,
9909 argcount + varargs,
9910 &ufunc->uf_type_list) == FAIL)
9911 return;
9912 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9913 ufunc->uf_func_type->tt_min_argcount =
9914 argcount - ufunc->uf_def_args.ga_len;
9915 if (ufunc->uf_arg_types == NULL)
9916 {
9917 int i;
9918
9919 // lambda does not have argument types.
9920 for (i = 0; i < argcount; ++i)
9921 ufunc->uf_func_type->tt_args[i] = &t_any;
9922 }
9923 else
9924 mch_memmove(ufunc->uf_func_type->tt_args,
9925 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9926 if (varargs)
9927 {
9928 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009929 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009930 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9931 }
9932 }
9933 else
9934 // No arguments, can use a predefined type.
9935 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9936 argcount, &ufunc->uf_type_list);
9937}
9938
9939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009940/*
9941 * Delete an instruction, free what it contains.
9942 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009943 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009944delete_instr(isn_T *isn)
9945{
9946 switch (isn->isn_type)
9947 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009948 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009949 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009950 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009951 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009952 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009953 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009954 case ISN_LOADENV:
9955 case ISN_LOADG:
9956 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009957 case ISN_LOADT:
9958 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009959 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009960 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009961 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009962 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009963 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009964 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009965 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009966 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009967 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009968 case ISN_STOREW:
9969 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009970 vim_free(isn->isn_arg.string);
9971 break;
9972
Bram Moolenaar4c137212021-04-19 16:48:48 +02009973 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009974 {
9975 int idx;
9976 isn_T *list = isn->isn_arg.subs.subs_instr;
9977
9978 vim_free(isn->isn_arg.subs.subs_cmd);
9979 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9980 delete_instr(list + idx);
9981 vim_free(list);
9982 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009983 break;
9984
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009985 case ISN_INSTR:
9986 {
9987 int idx;
9988 isn_T *list = isn->isn_arg.instr;
9989
9990 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9991 delete_instr(list + idx);
9992 vim_free(list);
9993 }
9994 break;
9995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009996 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009997 case ISN_STORES:
9998 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009999 break;
10000
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010001 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010002 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010003 vim_free(isn->isn_arg.unlet.ul_name);
10004 break;
10005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010006 case ISN_STOREOPT:
10007 vim_free(isn->isn_arg.storeopt.so_name);
10008 break;
10009
10010 case ISN_PUSHBLOB: // push blob isn_arg.blob
10011 blob_unref(isn->isn_arg.blob);
10012 break;
10013
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010014 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010015#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010016 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010017#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010018 break;
10019
10020 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010021#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010022 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010023#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010024 break;
10025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010026 case ISN_UCALL:
10027 vim_free(isn->isn_arg.ufunc.cuf_name);
10028 break;
10029
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010030 case ISN_FUNCREF:
10031 {
10032 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10033 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010034 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010035
Bram Moolenaar077a4232020-12-22 18:33:27 +010010036 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10037 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010038 }
10039 break;
10040
10041 case ISN_DCALL:
10042 {
10043 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10044 + isn->isn_arg.dfunc.cdf_idx;
10045
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010046 if (dfunc->df_ufunc != NULL
10047 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010048 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010049 }
10050 break;
10051
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010052 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010053 {
10054 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10055 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10056
10057 if (ufunc != NULL)
10058 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010059 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010060 func_ptr_unref(ufunc);
10061 }
10062
10063 vim_free(lambda);
10064 vim_free(isn->isn_arg.newfunc.nf_global);
10065 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010066 break;
10067
Bram Moolenaar5e654232020-09-16 15:22:00 +020010068 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010069 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010070 free_type(isn->isn_arg.type.ct_type);
10071 break;
10072
Bram Moolenaar02194d22020-10-24 23:08:38 +020010073 case ISN_CMDMOD:
10074 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10075 ->cmod_filter_regmatch.regprog);
10076 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10077 break;
10078
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010079 case ISN_LOADSCRIPT:
10080 case ISN_STORESCRIPT:
10081 vim_free(isn->isn_arg.script.scriptref);
10082 break;
10083
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010084 case ISN_TRY:
10085 vim_free(isn->isn_arg.try.try_ref);
10086 break;
10087
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010088 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010089 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010090 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10091 break;
10092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010093 case ISN_2BOOL:
10094 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010095 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010096 case ISN_ADDBLOB:
10097 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010098 case ISN_ANYINDEX:
10099 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010100 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010101 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010102 case ISN_BLOBINDEX:
10103 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010104 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010105 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010106 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010107 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010108 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010109 case ISN_COMPAREANY:
10110 case ISN_COMPAREBLOB:
10111 case ISN_COMPAREBOOL:
10112 case ISN_COMPAREDICT:
10113 case ISN_COMPAREFLOAT:
10114 case ISN_COMPAREFUNC:
10115 case ISN_COMPARELIST:
10116 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010117 case ISN_COMPARESPECIAL:
10118 case ISN_COMPARESTRING:
10119 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010120 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010121 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010122 case ISN_DROP:
10123 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010124 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010125 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010126 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010127 case ISN_EXECCONCAT:
10128 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010129 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010130 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010131 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010132 case ISN_GETITEM:
10133 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010134 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010135 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010136 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010137 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010138 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010139 case ISN_LOADBDICT:
10140 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010141 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010142 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010143 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010144 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010145 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010146 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010147 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010148 case ISN_NEGATENR:
10149 case ISN_NEWDICT:
10150 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010151 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010152 case ISN_OPFLOAT:
10153 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010154 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010155 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010156 case ISN_PROF_END:
10157 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010158 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010159 case ISN_PUSHF:
10160 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010161 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010162 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010163 case ISN_REDIREND:
10164 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010165 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010166 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010167 case ISN_SHUFFLE:
10168 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010169 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010170 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010171 case ISN_STORENR:
10172 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010173 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010174 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010175 case ISN_STOREV:
10176 case ISN_STRINDEX:
10177 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010178 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010179 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010180 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010181 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010182 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010183 // nothing allocated
10184 break;
10185 }
10186}
10187
10188/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010189 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010190 */
10191 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010192delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010193{
10194 int idx;
10195
10196 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010197 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010198
10199 if (dfunc->df_instr != NULL)
10200 {
10201 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10202 delete_instr(dfunc->df_instr + idx);
10203 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010204 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010205 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010206 if (dfunc->df_instr_debug != NULL)
10207 {
10208 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10209 delete_instr(dfunc->df_instr_debug + idx);
10210 VIM_CLEAR(dfunc->df_instr_debug);
10211 dfunc->df_instr_debug = NULL;
10212 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010213#ifdef FEAT_PROFILE
10214 if (dfunc->df_instr_prof != NULL)
10215 {
10216 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10217 delete_instr(dfunc->df_instr_prof + idx);
10218 VIM_CLEAR(dfunc->df_instr_prof);
10219 dfunc->df_instr_prof = NULL;
10220 }
10221#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010222
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010223 if (mark_deleted)
10224 dfunc->df_deleted = TRUE;
10225 if (dfunc->df_ufunc != NULL)
10226 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010227}
10228
10229/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010230 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010231 * function, unless another user function still uses it.
10232 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010233 */
10234 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010235unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010236{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010237 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010238 {
10239 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10240 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010241
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010242 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010243 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010244 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010245 ufunc->uf_dfunc_idx = 0;
10246 if (dfunc->df_ufunc == ufunc)
10247 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010248 }
10249}
10250
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010251/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010252 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010253 */
10254 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010255link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010256{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010257 if (ufunc->uf_dfunc_idx > 0)
10258 {
10259 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10260 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010261
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010262 ++dfunc->df_refcount;
10263 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010264}
10265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010266#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010267/*
10268 * Free all functions defined with ":def".
10269 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010270 void
10271free_def_functions(void)
10272{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010273 int idx;
10274
10275 for (idx = 0; idx < def_functions.ga_len; ++idx)
10276 {
10277 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10278
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010279 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010280 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010281 }
10282
10283 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010284}
10285#endif
10286
10287
10288#endif // FEAT_EVAL