blob: b04e750ef564e8172ec32d31ab8e00f3611477c8 [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 Moolenaar648594e2021-07-11 17:55:01 +02003627#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003628 // When the outer function is compiled for profiling, the lambda may be
3629 // called without profiling. Compile it here in the right context.
3630 if (cctx->ctx_compile_type == CT_PROFILE)
3631 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003632#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003633
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003634 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3635 // points into it. Point to the original line to avoid a dangling pointer.
3636 if (evalarg.eval_tofree_cmdline != NULL)
3637 {
3638 size_t off = *arg - evalarg.eval_tofree_cmdline;
3639
3640 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3641 + off;
3642 }
3643
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003644 clear_evalarg(&evalarg, NULL);
3645
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003646 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003647 {
3648 // The return type will now be known.
3649 set_function_type(ufunc);
3650
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003651 // The function reference count will be 1. When the ISN_FUNCREF
3652 // instruction is deleted the reference count is decremented and the
3653 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003654 return generate_FUNCREF(cctx, ufunc);
3655 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003656
3657 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 return FAIL;
3659}
3660
3661/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003662 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003664 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 */
3666 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003667compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668{
3669 garray_T *instr = &cctx->ctx_instr;
3670 int count = 0;
3671 dict_T *d = dict_alloc();
3672 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003673 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003674 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003675 int is_const;
3676 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677
3678 if (d == NULL)
3679 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003680 if (generate_ppconst(cctx, ppconst) == FAIL)
3681 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003682 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003684 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685
Bram Moolenaar23c55272020-06-21 16:58:13 +02003686 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003687 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003688 *arg = NULL;
3689 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003690 }
3691
3692 if (**arg == '}')
3693 break;
3694
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003695 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003697 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003699 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003700 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003701 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003704 if (isn->isn_type == ISN_PUSHNR)
3705 {
3706 char buf[NUMBUFLEN];
3707
3708 // Convert to string at compile time.
3709 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3710 isn->isn_type = ISN_PUSHS;
3711 isn->isn_arg.string = vim_strsave((char_u *)buf);
3712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 if (isn->isn_type == ISN_PUSHS)
3714 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003715 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003716 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003717 *arg = skipwhite(*arg);
3718 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003719 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003720 emsg(_(e_missing_matching_bracket_after_dict_key));
3721 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003722 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003723 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003725 else
3726 {
3727 // {"name": value},
3728 // {'name': value},
3729 // {name: value} use "name" as a literal key
3730 key = get_literal_key(arg);
3731 if (key == NULL)
3732 return FAIL;
3733 if (generate_PUSHS(cctx, key) == FAIL)
3734 return FAIL;
3735 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736
3737 // Check for duplicate keys, if using string keys.
3738 if (key != NULL)
3739 {
3740 item = dict_find(d, key, -1);
3741 if (item != NULL)
3742 {
3743 semsg(_(e_duplicate_key), key);
3744 goto failret;
3745 }
3746 item = dictitem_alloc(key);
3747 if (item != NULL)
3748 {
3749 item->di_tv.v_type = VAR_UNKNOWN;
3750 item->di_tv.v_lock = 0;
3751 if (dict_add(d, item) == FAIL)
3752 dictitem_free(item);
3753 }
3754 }
3755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 if (**arg != ':')
3757 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003758 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003759 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003760 else
3761 semsg(_(e_missing_dict_colon), *arg);
3762 return FAIL;
3763 }
3764 whitep = *arg + 1;
3765 if (!IS_WHITE_OR_NUL(*whitep))
3766 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003767 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 return FAIL;
3769 }
3770
Bram Moolenaar23c55272020-06-21 16:58:13 +02003771 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003772 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003773 *arg = NULL;
3774 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003775 }
3776
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003777 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003779 if (!is_const)
3780 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 ++count;
3782
Bram Moolenaar2c330432020-04-13 14:41:35 +02003783 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003784 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003785 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003786 *arg = NULL;
3787 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 if (**arg == '}')
3790 break;
3791 if (**arg != ',')
3792 {
3793 semsg(_(e_missing_dict_comma), *arg);
3794 goto failret;
3795 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003796 if (IS_WHITE_OR_NUL(*whitep))
3797 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003798 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003799 return FAIL;
3800 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003801 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003802 if (!IS_WHITE_OR_NUL(*whitep))
3803 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003804 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003805 return FAIL;
3806 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003807 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 }
3809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 *arg = *arg + 1;
3811
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003812 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003813 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003814 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003815 *arg += STRLEN(*arg);
3816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003818 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 return generate_NEWDICT(cctx, count);
3820
3821failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003822 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003823 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003824 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003825 *arg = (char_u *)"";
3826 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 dict_unref(d);
3828 return FAIL;
3829}
3830
3831/*
3832 * Compile "&option".
3833 */
3834 static int
3835compile_get_option(char_u **arg, cctx_T *cctx)
3836{
3837 typval_T rettv;
3838 char_u *start = *arg;
3839 int ret;
3840
3841 // parse the option and get the current value to get the type.
3842 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003843 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 if (ret == OK)
3845 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003846 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003847 char_u *name = vim_strnsave(start, *arg - start);
3848 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3849 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850
3851 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3852 vim_free(name);
3853 }
3854 clear_tv(&rettv);
3855
3856 return ret;
3857}
3858
3859/*
3860 * Compile "$VAR".
3861 */
3862 static int
3863compile_get_env(char_u **arg, cctx_T *cctx)
3864{
3865 char_u *start = *arg;
3866 int len;
3867 int ret;
3868 char_u *name;
3869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 ++*arg;
3871 len = get_env_len(arg);
3872 if (len == 0)
3873 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003874 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 return FAIL;
3876 }
3877
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003878 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 name = vim_strnsave(start, len + 1);
3880 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3881 vim_free(name);
3882 return ret;
3883}
3884
3885/*
3886 * Compile "@r".
3887 */
3888 static int
3889compile_get_register(char_u **arg, cctx_T *cctx)
3890{
3891 int ret;
3892
3893 ++*arg;
3894 if (**arg == NUL)
3895 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003896 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 return FAIL;
3898 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003899 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 {
3901 emsg_invreg(**arg);
3902 return FAIL;
3903 }
3904 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3905 ++*arg;
3906 return ret;
3907}
3908
3909/*
3910 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003911 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 */
3913 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003914apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003916 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917
3918 // this works from end to start
3919 while (p > start)
3920 {
3921 --p;
3922 if (*p == '-' || *p == '+')
3923 {
3924 // only '-' has an effect, for '+' we only check the type
3925#ifdef FEAT_FLOAT
3926 if (rettv->v_type == VAR_FLOAT)
3927 {
3928 if (*p == '-')
3929 rettv->vval.v_float = -rettv->vval.v_float;
3930 }
3931 else
3932#endif
3933 {
3934 varnumber_T val;
3935 int error = FALSE;
3936
3937 // tv_get_number_chk() accepts a string, but we don't want that
3938 // here
3939 if (check_not_string(rettv) == FAIL)
3940 return FAIL;
3941 val = tv_get_number_chk(rettv, &error);
3942 clear_tv(rettv);
3943 if (error)
3944 return FAIL;
3945 if (*p == '-')
3946 val = -val;
3947 rettv->v_type = VAR_NUMBER;
3948 rettv->vval.v_number = val;
3949 }
3950 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003951 else if (numeric_only)
3952 {
3953 ++p;
3954 break;
3955 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003956 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 {
3958 int v = tv2bool(rettv);
3959
3960 // '!' is permissive in the type.
3961 clear_tv(rettv);
3962 rettv->v_type = VAR_BOOL;
3963 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3964 }
3965 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003966 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 return OK;
3968}
3969
3970/*
3971 * Recognize v: variables that are constants and set "rettv".
3972 */
3973 static void
3974get_vim_constant(char_u **arg, typval_T *rettv)
3975{
3976 if (STRNCMP(*arg, "v:true", 6) == 0)
3977 {
3978 rettv->v_type = VAR_BOOL;
3979 rettv->vval.v_number = VVAL_TRUE;
3980 *arg += 6;
3981 }
3982 else if (STRNCMP(*arg, "v:false", 7) == 0)
3983 {
3984 rettv->v_type = VAR_BOOL;
3985 rettv->vval.v_number = VVAL_FALSE;
3986 *arg += 7;
3987 }
3988 else if (STRNCMP(*arg, "v:null", 6) == 0)
3989 {
3990 rettv->v_type = VAR_SPECIAL;
3991 rettv->vval.v_number = VVAL_NULL;
3992 *arg += 6;
3993 }
3994 else if (STRNCMP(*arg, "v:none", 6) == 0)
3995 {
3996 rettv->v_type = VAR_SPECIAL;
3997 rettv->vval.v_number = VVAL_NONE;
3998 *arg += 6;
3999 }
4000}
4001
Bram Moolenaar657137c2021-01-09 15:45:23 +01004002 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004003get_compare_type(char_u *p, int *len, int *type_is)
4004{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004005 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004006 int i;
4007
4008 switch (p[0])
4009 {
4010 case '=': if (p[1] == '=')
4011 type = EXPR_EQUAL;
4012 else if (p[1] == '~')
4013 type = EXPR_MATCH;
4014 break;
4015 case '!': if (p[1] == '=')
4016 type = EXPR_NEQUAL;
4017 else if (p[1] == '~')
4018 type = EXPR_NOMATCH;
4019 break;
4020 case '>': if (p[1] != '=')
4021 {
4022 type = EXPR_GREATER;
4023 *len = 1;
4024 }
4025 else
4026 type = EXPR_GEQUAL;
4027 break;
4028 case '<': if (p[1] != '=')
4029 {
4030 type = EXPR_SMALLER;
4031 *len = 1;
4032 }
4033 else
4034 type = EXPR_SEQUAL;
4035 break;
4036 case 'i': if (p[1] == 's')
4037 {
4038 // "is" and "isnot"; but not a prefix of a name
4039 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4040 *len = 5;
4041 i = p[*len];
4042 if (!isalnum(i) && i != '_')
4043 {
4044 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4045 *type_is = TRUE;
4046 }
4047 }
4048 break;
4049 }
4050 return type;
4051}
4052
4053/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004054 * Skip over an expression, ignoring most errors.
4055 */
4056 static void
4057skip_expr_cctx(char_u **arg, cctx_T *cctx)
4058{
4059 evalarg_T evalarg;
4060
4061 CLEAR_FIELD(evalarg);
4062 evalarg.eval_cctx = cctx;
4063 skip_expr(arg, &evalarg);
4064}
4065
4066/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004068 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 */
4070 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004071compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004073 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074
4075 // this works from end to start
4076 while (p > start)
4077 {
4078 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004079 while (VIM_ISWHITE(*p))
4080 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 if (*p == '-' || *p == '+')
4082 {
4083 int negate = *p == '-';
4084 isn_T *isn;
4085
4086 // TODO: check type
4087 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4088 {
4089 --p;
4090 if (*p == '-')
4091 negate = !negate;
4092 }
4093 // only '-' has an effect, for '+' we only check the type
4094 if (negate)
4095 isn = generate_instr(cctx, ISN_NEGATENR);
4096 else
4097 isn = generate_instr(cctx, ISN_CHECKNR);
4098 if (isn == NULL)
4099 return FAIL;
4100 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004101 else if (numeric_only)
4102 {
4103 ++p;
4104 break;
4105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 else
4107 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004108 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004110 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004112 if (p[-1] == '!')
4113 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004116 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 return FAIL;
4118 }
4119 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004120 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 return OK;
4122}
4123
4124/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004125 * Compile "(expression)": recursive!
4126 * Return FAIL/OK.
4127 */
4128 static int
4129compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4130{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004131 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004132 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004133
Bram Moolenaar24156692021-01-14 20:35:49 +01004134 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4135 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004136 if (ppconst->pp_used <= PPSIZE - 10)
4137 {
4138 ret = compile_expr1(arg, cctx, ppconst);
4139 }
4140 else
4141 {
4142 // Not enough space in ppconst, flush constants.
4143 if (generate_ppconst(cctx, ppconst) == FAIL)
4144 return FAIL;
4145 ret = compile_expr0(arg, cctx);
4146 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004147 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4148 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004149 if (**arg == ')')
4150 ++*arg;
4151 else if (ret == OK)
4152 {
4153 emsg(_(e_missing_close));
4154 ret = FAIL;
4155 }
4156 return ret;
4157}
4158
4159/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004161 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 */
4163 static int
4164compile_subscript(
4165 char_u **arg,
4166 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004167 char_u *start_leader,
4168 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004169 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004171 char_u *name_start = *end_leader;
4172
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 for (;;)
4174 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004175 char_u *p = skipwhite(*arg);
4176
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004177 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004178 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004179 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004180
4181 // If a following line starts with "->{" or "->X" advance to that
4182 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004183 // Also if a following line starts with ".x".
4184 if (next != NULL &&
4185 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004186 && (next[2] == '{'
4187 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004188 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004189 {
4190 next = next_line_from_context(cctx, TRUE);
4191 if (next == NULL)
4192 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004193 *arg = next;
4194 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004195 }
4196 }
4197
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004198 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004199 // not a function call.
4200 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004202 garray_T *stack = &cctx->ctx_type_stack;
4203 type_T *type;
4204 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004206 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004207 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004208 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004211 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4212
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004213 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004214 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004216 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 return FAIL;
4218 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004219 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004221 char_u *pstart = p;
4222
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004223 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004224 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004225 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004226
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 // something->method()
4228 // Apply the '!', '-' and '+' first:
4229 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004230 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004233 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004234 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004235 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004236 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004237 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004238 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004239 garray_T *stack = &cctx->ctx_type_stack;
4240 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004241 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004242 int expr_isn_start = cctx->ctx_instr.ga_len;
4243 int expr_isn_end;
4244 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004245
4246 // Funcref call: list->(Refs[2])(arg)
4247 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004248 //
4249 // Fist compile the function expression.
4250 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004251 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004252
4253 // Remember the next instruction index, where the instructions
4254 // for arguments are being written.
4255 expr_isn_end = cctx->ctx_instr.ga_len;
4256
4257 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004258 if (**arg != '(')
4259 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004260 if (*skipwhite(*arg) == '(')
4261 emsg(_(e_nowhitespace));
4262 else
4263 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004264 return FAIL;
4265 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004266 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004267 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004268 return FAIL;
4269
Bram Moolenaar2927c072021-04-05 19:41:21 +02004270 // Move the instructions for the arguments to before the
4271 // instructions of the expression and move the type of the
4272 // expression after the argument types. This is what ISN_PCALL
4273 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004274 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004275 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4276 if (arg_isn_count > 0)
4277 {
4278 int expr_isn_count = expr_isn_end - expr_isn_start;
4279 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4280
4281 if (isn == NULL)
4282 return FAIL;
4283 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4284 + expr_isn_start,
4285 sizeof(isn_T) * expr_isn_count);
4286 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4287 + expr_isn_start,
4288 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4289 sizeof(isn_T) * arg_isn_count);
4290 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4291 + expr_isn_start + arg_isn_count,
4292 isn, sizeof(isn_T) * expr_isn_count);
4293 vim_free(isn);
4294
4295 type = ((type_T **)stack->ga_data)[type_idx_start];
4296 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4297 ((type_T **)stack->ga_data) + type_idx_start + 1,
4298 sizeof(type_T *)
4299 * (stack->ga_len - type_idx_start - 1));
4300 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4301 }
4302
Bram Moolenaar7e368202020-12-25 21:56:57 +01004303 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4304 if (generate_PCALL(cctx, argcount,
4305 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 return FAIL;
4307 }
4308 else
4309 {
4310 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004311 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004312 if (!eval_isnamec1(*p))
4313 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004314 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004315 return FAIL;
4316 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004317 if (ASCII_ISALPHA(*p) && p[1] == ':')
4318 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004319 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 ;
4321 if (*p != '(')
4322 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004323 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 return FAIL;
4325 }
4326 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004327 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 return FAIL;
4329 }
4330 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004331 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004333 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004334
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004335 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004336 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004337 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004338 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004339 // TODO: more arguments
4340 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004341 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004342 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004343 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004344
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004345 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004346 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004347 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004348 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004349 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004350 // missing first index is equal to zero
4351 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004352 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004353 else
4354 {
4355 if (compile_expr0(arg, cctx) == FAIL)
4356 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004357 if (**arg == ':')
4358 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004359 semsg(_(e_white_space_required_before_and_after_str_at_str),
4360 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004361 return FAIL;
4362 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004363 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004364 return FAIL;
4365 *arg = skipwhite(*arg);
4366 }
4367 if (**arg == ':')
4368 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004369 is_slice = TRUE;
4370 ++*arg;
4371 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4372 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004373 semsg(_(e_white_space_required_before_and_after_str_at_str),
4374 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004375 return FAIL;
4376 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004377 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004378 return FAIL;
4379 if (**arg == ']')
4380 // missing second index is equal to end of string
4381 generate_PUSHNR(cctx, -1);
4382 else
4383 {
4384 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004385 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004386 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004387 return FAIL;
4388 *arg = skipwhite(*arg);
4389 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004390 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391
4392 if (**arg != ']')
4393 {
4394 emsg(_(e_missbrac));
4395 return FAIL;
4396 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004397 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398
Bram Moolenaare42939a2021-04-05 17:11:17 +02004399 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004400 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004402 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004404 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004405 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004406 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004407 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004408
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004409 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004410 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004411 {
4412 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004413 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004414 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004415 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004416 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 while (eval_isnamec(*p))
4418 MB_PTR_ADV(p);
4419 if (p == *arg)
4420 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004421 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 return FAIL;
4423 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004424 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 return FAIL;
4426 *arg = p;
4427 }
4428 else
4429 break;
4430 }
4431
4432 // TODO - see handle_subscript():
4433 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4434 // Don't do this when "Func" is already a partial that was bound
4435 // explicitly (pt_auto is FALSE).
4436
4437 return OK;
4438}
4439
4440/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004441 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4442 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004444 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004445 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004446 *
4447 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 */
4449
4450/*
4451 * number number constant
4452 * 0zFFFFFFFF Blob constant
4453 * "string" string constant
4454 * 'string' literal string constant
4455 * &option-name option value
4456 * @r register contents
4457 * identifier variable value
4458 * function() function call
4459 * $VAR environment variable
4460 * (expression) nested expression
4461 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004462 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 *
4464 * Also handle:
4465 * ! in front logical NOT
4466 * - in front unary minus
4467 * + in front unary plus (ignored)
4468 * trailing (arg) funcref/partial call
4469 * trailing [] subscript in String or List
4470 * trailing .name entry in Dictionary
4471 * trailing ->name() method call
4472 */
4473 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004474compile_expr7(
4475 char_u **arg,
4476 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004477 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 char_u *start_leader, *end_leader;
4480 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004481 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004482 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004484 ppconst->pp_is_const = FALSE;
4485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 /*
4487 * Skip '!', '-' and '+' characters. They are handled later.
4488 */
4489 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004490 if (eval_leader(arg, TRUE) == FAIL)
4491 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 end_leader = *arg;
4493
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004494 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 switch (**arg)
4496 {
4497 /*
4498 * Number constant.
4499 */
4500 case '0': // also for blob starting with 0z
4501 case '1':
4502 case '2':
4503 case '3':
4504 case '4':
4505 case '5':
4506 case '6':
4507 case '7':
4508 case '8':
4509 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004510 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004512 // Apply "-" and "+" just before the number now, right to
4513 // left. Matters especially when "->" follows. Stops at
4514 // '!'.
4515 if (apply_leader(rettv, TRUE,
4516 start_leader, &end_leader) == FAIL)
4517 {
4518 clear_tv(rettv);
4519 return FAIL;
4520 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 break;
4522
4523 /*
4524 * String constant: "string".
4525 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004526 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 return FAIL;
4528 break;
4529
4530 /*
4531 * Literal string constant: 'str''ing'.
4532 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004533 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 return FAIL;
4535 break;
4536
4537 /*
4538 * Constant Vim variable.
4539 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004540 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 ret = NOTDONE;
4542 break;
4543
4544 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004545 * "true" constant
4546 */
4547 case 't': if (STRNCMP(*arg, "true", 4) == 0
4548 && !eval_isnamec((*arg)[4]))
4549 {
4550 *arg += 4;
4551 rettv->v_type = VAR_BOOL;
4552 rettv->vval.v_number = VVAL_TRUE;
4553 }
4554 else
4555 ret = NOTDONE;
4556 break;
4557
4558 /*
4559 * "false" constant
4560 */
4561 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4562 && !eval_isnamec((*arg)[5]))
4563 {
4564 *arg += 5;
4565 rettv->v_type = VAR_BOOL;
4566 rettv->vval.v_number = VVAL_FALSE;
4567 }
4568 else
4569 ret = NOTDONE;
4570 break;
4571
4572 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004573 * "null" constant
4574 */
4575 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004576 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004577 {
4578 *arg += 4;
4579 rettv->v_type = VAR_SPECIAL;
4580 rettv->vval.v_number = VVAL_NULL;
4581 }
4582 else
4583 ret = NOTDONE;
4584 break;
4585
4586 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 * List: [expr, expr]
4588 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004589 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4590 return FAIL;
4591 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 break;
4593
4594 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 * Dictionary: {'key': val, 'key': val}
4596 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004597 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4598 return FAIL;
4599 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 break;
4601
4602 /*
4603 * Option value: &name
4604 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004605 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4606 return FAIL;
4607 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 break;
4609
4610 /*
4611 * Environment variable: $VAR.
4612 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004613 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4614 return FAIL;
4615 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 break;
4617
4618 /*
4619 * Register contents: @r.
4620 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004621 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4622 return FAIL;
4623 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 break;
4625 /*
4626 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004627 * lambda: (arg, arg) => expr
4628 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004630 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4631 ret = compile_lambda(arg, cctx);
4632 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004633 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 break;
4635
4636 default: ret = NOTDONE;
4637 break;
4638 }
4639 if (ret == FAIL)
4640 return FAIL;
4641
Bram Moolenaar1c747212020-05-09 18:28:34 +02004642 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004644 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004645 clear_tv(rettv);
4646 else
4647 // A constant expression can possibly be handled compile time,
4648 // return the value instead of generating code.
4649 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 }
4651 else if (ret == NOTDONE)
4652 {
4653 char_u *p;
4654 int r;
4655
4656 if (!eval_isnamec1(**arg))
4657 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004658 if (!vim9_bad_comment(*arg))
4659 {
4660 if (ends_excmd(*skipwhite(*arg)))
4661 semsg(_(e_empty_expression_str), *arg);
4662 else
4663 semsg(_(e_name_expected_str), *arg);
4664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 return FAIL;
4666 }
4667
4668 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004669 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004670 if (p - *arg == (size_t)1 && **arg == '_')
4671 {
4672 emsg(_(e_cannot_use_underscore_here));
4673 return FAIL;
4674 }
4675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004677 {
4678 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4679 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004681 {
4682 if (generate_ppconst(cctx, ppconst) == FAIL)
4683 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004684 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 if (r == FAIL)
4687 return FAIL;
4688 }
4689
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004690 // Handle following "[]", ".member", etc.
4691 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004692 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004693 ppconst) == FAIL)
4694 return FAIL;
4695 if (ppconst->pp_used > 0)
4696 {
4697 // apply the '!', '-' and '+' before the constant
4698 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004699 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004700 return FAIL;
4701 return OK;
4702 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004703 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004705 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706}
4707
4708/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004709 * Give the "white on both sides" error, taking the operator from "p[len]".
4710 */
4711 void
4712error_white_both(char_u *op, int len)
4713{
4714 char_u buf[10];
4715
4716 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004717 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004718}
4719
4720/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004721 * <type>expr7: runtime type check / conversion
4722 */
4723 static int
4724compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4725{
4726 type_T *want_type = NULL;
4727
4728 // Recognize <type>
4729 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4730 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004731 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004732 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4733 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004734 return FAIL;
4735
4736 if (**arg != '>')
4737 {
4738 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004739 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004740 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004741 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004742 return FAIL;
4743 }
4744 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004745 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004746 return FAIL;
4747 }
4748
4749 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4750 return FAIL;
4751
4752 if (want_type != NULL)
4753 {
4754 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004755 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004756 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004757
Bram Moolenaard1103582020-08-14 22:44:25 +02004758 generate_ppconst(cctx, ppconst);
4759 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004760 where.wt_index = 0;
4761 where.wt_variable = FALSE;
4762 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004763 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004764 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004765 return FAIL;
4766 }
4767 }
4768
4769 return OK;
4770}
4771
4772/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 * * number multiplication
4774 * / number division
4775 * % number modulo
4776 */
4777 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004778compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779{
4780 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004781 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004782 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004784 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004785 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 return FAIL;
4787
4788 /*
4789 * Repeat computing, until no "*", "/" or "%" is following.
4790 */
4791 for (;;)
4792 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004793 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 if (*op != '*' && *op != '/' && *op != '%')
4795 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004796 if (next != NULL)
4797 {
4798 *arg = next_line_from_context(cctx, TRUE);
4799 op = skipwhite(*arg);
4800 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004801
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004802 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004804 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004805 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004807 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004808 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004810 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004811 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004813
4814 if (ppconst->pp_used == ppconst_used + 2
4815 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4816 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004817 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004818 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4819 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4820 varnumber_T res = 0;
4821 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004823 // both are numbers: compute the result
4824 switch (*op)
4825 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004826 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004827 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004828 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004829 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004830 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004831 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004832 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004833 break;
4834 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004835 if (failed)
4836 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004837 tv1->vval.v_number = res;
4838 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004839 }
4840 else
4841 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004842 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004843 generate_two_op(cctx, op);
4844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 }
4846
4847 return OK;
4848}
4849
4850/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004851 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 * - number subtraction
4853 * .. string concatenation
4854 */
4855 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004856compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857{
4858 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004859 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004861 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862
4863 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004864 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 return FAIL;
4866
4867 /*
4868 * Repeat computing, until no "+", "-" or ".." is following.
4869 */
4870 for (;;)
4871 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004872 op = may_peek_next_line(cctx, *arg, &next);
4873 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004875 if (op[0] == op[1] && *op != '.' && next)
4876 // Finding "++" or "--" on the next line is a separate command.
4877 // But ".." is concatenation.
4878 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004880 if (next != NULL)
4881 {
4882 *arg = next_line_from_context(cctx, TRUE);
4883 op = skipwhite(*arg);
4884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004886 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004888 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004889 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890 }
4891
Bram Moolenaare0de1712020-12-02 17:36:54 +01004892 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004893 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004895 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004896 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 return FAIL;
4898
Bram Moolenaara5565e42020-05-09 15:44:01 +02004899 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004900 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004901 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4902 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4903 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4904 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004906 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4907 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004908
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004909 // concat/subtract/add constant numbers
4910 if (*op == '+')
4911 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4912 else if (*op == '-')
4913 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4914 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004915 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004916 // concatenate constant strings
4917 char_u *s1 = tv1->vval.v_string;
4918 char_u *s2 = tv2->vval.v_string;
4919 size_t len1 = STRLEN(s1);
4920
4921 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4922 if (tv1->vval.v_string == NULL)
4923 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004924 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004925 return FAIL;
4926 }
4927 mch_memmove(tv1->vval.v_string, s1, len1);
4928 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004929 vim_free(s1);
4930 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004931 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004932 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 }
4934 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004935 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004936 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004937 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004938 if (*op == '.')
4939 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004940 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4941 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004942 return FAIL;
4943 generate_instr_drop(cctx, ISN_CONCAT, 1);
4944 }
4945 else
4946 generate_two_op(cctx, op);
4947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 }
4949
4950 return OK;
4951}
4952
4953/*
4954 * expr5a == expr5b
4955 * expr5a =~ expr5b
4956 * expr5a != expr5b
4957 * expr5a !~ expr5b
4958 * expr5a > expr5b
4959 * expr5a >= expr5b
4960 * expr5a < expr5b
4961 * expr5a <= expr5b
4962 * expr5a is expr5b
4963 * expr5a isnot expr5b
4964 *
4965 * Produces instructions:
4966 * EVAL expr5a Push result of "expr5a"
4967 * EVAL expr5b Push result of "expr5b"
4968 * COMPARE one of the compare instructions
4969 */
4970 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004971compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004973 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004975 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004978 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979
4980 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004981 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 return FAIL;
4983
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004984 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004985 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986
4987 /*
4988 * If there is a comparative operator, use it.
4989 */
4990 if (type != EXPR_UNKNOWN)
4991 {
4992 int ic = FALSE; // Default: do not ignore case
4993
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004994 if (next != NULL)
4995 {
4996 *arg = next_line_from_context(cctx, TRUE);
4997 p = skipwhite(*arg);
4998 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 if (type_is && (p[len] == '?' || p[len] == '#'))
5000 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005001 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 return FAIL;
5003 }
5004 // extra question mark appended: ignore case
5005 if (p[len] == '?')
5006 {
5007 ic = TRUE;
5008 ++len;
5009 }
5010 // extra '#' appended: match case (ignored)
5011 else if (p[len] == '#')
5012 ++len;
5013 // nothing appended: match case
5014
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005015 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005017 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005018 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 }
5020
5021 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005022 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005023 return FAIL;
5024
Bram Moolenaara5565e42020-05-09 15:44:01 +02005025 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026 return FAIL;
5027
Bram Moolenaara5565e42020-05-09 15:44:01 +02005028 if (ppconst->pp_used == ppconst_used + 2)
5029 {
5030 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5031 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5032 int ret;
5033
5034 // Both sides are a constant, compute the result now.
5035 // First check for a valid combination of types, this is more
5036 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005037 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005038 ret = FAIL;
5039 else
5040 {
5041 ret = typval_compare(tv1, tv2, type, ic);
5042 tv1->v_type = VAR_BOOL;
5043 tv1->vval.v_number = tv1->vval.v_number
5044 ? VVAL_TRUE : VVAL_FALSE;
5045 clear_tv(tv2);
5046 --ppconst->pp_used;
5047 }
5048 return ret;
5049 }
5050
5051 generate_ppconst(cctx, ppconst);
5052 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005053 }
5054
5055 return OK;
5056}
5057
Bram Moolenaar7f141552020-05-09 17:35:53 +02005058static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060/*
5061 * Compile || or &&.
5062 */
5063 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005064compile_and_or(
5065 char_u **arg,
5066 cctx_T *cctx,
5067 char *op,
5068 ppconst_T *ppconst,
5069 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005071 char_u *next;
5072 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 int opchar = *op;
5074
5075 if (p[0] == opchar && p[1] == opchar)
5076 {
5077 garray_T *instr = &cctx->ctx_instr;
5078 garray_T end_ga;
5079
5080 /*
5081 * Repeat until there is no following "||" or "&&"
5082 */
5083 ga_init2(&end_ga, sizeof(int), 10);
5084 while (p[0] == opchar && p[1] == opchar)
5085 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005086 long start_lnum = SOURCING_LNUM;
5087 int start_ctx_lnum = cctx->ctx_lnum;
5088 int save_lnum;
5089
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005090 if (next != NULL)
5091 {
5092 *arg = next_line_from_context(cctx, TRUE);
5093 p = skipwhite(*arg);
5094 }
5095
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005096 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5097 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005098 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005099 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005100 return FAIL;
5101 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005103 // TODO: use ppconst if the value is a constant and check
5104 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005105 generate_ppconst(cctx, ppconst);
5106
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005107 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005108 SOURCING_LNUM = start_lnum;
5109 save_lnum = cctx->ctx_lnum;
5110 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005111 if (bool_on_stack(cctx) == FAIL)
5112 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005113 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005114 ga_clear(&end_ga);
5115 return FAIL;
5116 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005117 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 if (ga_grow(&end_ga, 1) == FAIL)
5120 {
5121 ga_clear(&end_ga);
5122 return FAIL;
5123 }
5124 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5125 ++end_ga.ga_len;
5126 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005127 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128
5129 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005130 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005131 {
5132 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005133 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005134 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005135
Bram Moolenaara5565e42020-05-09 15:44:01 +02005136 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5137 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 {
5139 ga_clear(&end_ga);
5140 return FAIL;
5141 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005142
5143 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005145 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005147 // Every part must evaluate to a bool.
5148 if (bool_on_stack(cctx) == FAIL)
5149 {
5150 ga_clear(&end_ga);
5151 return FAIL;
5152 }
5153
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005154 // Fill in the end label in all jumps.
5155 while (end_ga.ga_len > 0)
5156 {
5157 isn_T *isn;
5158
5159 --end_ga.ga_len;
5160 isn = ((isn_T *)instr->ga_data)
5161 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5162 isn->isn_arg.jump.jump_where = instr->ga_len;
5163 }
5164 ga_clear(&end_ga);
5165 }
5166
5167 return OK;
5168}
5169
5170/*
5171 * expr4a && expr4a && expr4a logical AND
5172 *
5173 * Produces instructions:
5174 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005175 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005176 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005178 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 * EVAL expr4c Push result of "expr4c"
5180 * end:
5181 */
5182 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005183compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005185 int ppconst_used = ppconst->pp_used;
5186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005187 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005188 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 return FAIL;
5190
5191 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005192 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193}
5194
5195/*
5196 * expr3a || expr3b || expr3c logical OR
5197 *
5198 * Produces instructions:
5199 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005200 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005201 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005203 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 * EVAL expr3c Push result of "expr3c"
5205 * end:
5206 */
5207 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005208compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005210 int ppconst_used = ppconst->pp_used;
5211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005213 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 return FAIL;
5215
5216 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005217 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218}
5219
5220/*
5221 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005223 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005224 * JUMP_IF_FALSE alt jump if false
5225 * EVAL expr1a
5226 * JUMP_ALWAYS end
5227 * alt: EVAL expr1b
5228 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005229 *
5230 * Toplevel expression: expr2 ?? expr1
5231 * Produces instructions:
5232 * EVAL expr2 Push result of "expr2"
5233 * JUMP_AND_KEEP_IF_TRUE end jump if true
5234 * EVAL expr1
5235 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236 */
5237 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005238compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239{
5240 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005241 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005242 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243
Bram Moolenaar3988f642020-08-27 22:43:03 +02005244 // Ignore all kinds of errors when not producing code.
5245 if (cctx->ctx_skip == SKIP_YES)
5246 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005247 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005248 return OK;
5249 }
5250
Bram Moolenaar61a89812020-05-07 16:58:17 +02005251 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005252 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253 return FAIL;
5254
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005255 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 if (*p == '?')
5257 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005258 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259 garray_T *instr = &cctx->ctx_instr;
5260 garray_T *stack = &cctx->ctx_type_stack;
5261 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005262 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005264 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005265 int has_const_expr = FALSE;
5266 int const_value = FALSE;
5267 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005268
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005269 if (next != NULL)
5270 {
5271 *arg = next_line_from_context(cctx, TRUE);
5272 p = skipwhite(*arg);
5273 }
5274
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005275 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005276 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005277 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005278 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005279 return FAIL;
5280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005281
Bram Moolenaara5565e42020-05-09 15:44:01 +02005282 if (ppconst->pp_used == ppconst_used + 1)
5283 {
5284 // the condition is a constant, we know whether the ? or the :
5285 // expression is to be evaluated.
5286 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005287 if (op_falsy)
5288 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5289 else
5290 {
5291 int error = FALSE;
5292
5293 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5294 &error);
5295 if (error)
5296 return FAIL;
5297 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005298 cctx->ctx_skip = save_skip == SKIP_YES ||
5299 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5300
5301 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5302 // "left ?? right" and "left" is truthy: produce "left"
5303 generate_ppconst(cctx, ppconst);
5304 else
5305 {
5306 clear_tv(&ppconst->pp_tv[ppconst_used]);
5307 --ppconst->pp_used;
5308 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005309 }
5310 else
5311 {
5312 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005313 if (op_falsy)
5314 end_idx = instr->ga_len;
5315 generate_JUMP(cctx, op_falsy
5316 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5317 if (op_falsy)
5318 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320
5321 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005322 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005323 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005324 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005325 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005326
Bram Moolenaara5565e42020-05-09 15:44:01 +02005327 if (!has_const_expr)
5328 {
5329 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005330
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005331 if (!op_falsy)
5332 {
5333 // remember the type and drop it
5334 --stack->ga_len;
5335 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005337 end_idx = instr->ga_len;
5338 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005339
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005340 // jump here from JUMP_IF_FALSE
5341 isn = ((isn_T *)instr->ga_data) + alt_idx;
5342 isn->isn_arg.jump.jump_where = instr->ga_len;
5343 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005344 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005346 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005347 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005348 // Check for the ":".
5349 p = may_peek_next_line(cctx, *arg, &next);
5350 if (*p != ':')
5351 {
5352 emsg(_(e_missing_colon));
5353 return FAIL;
5354 }
5355 if (next != NULL)
5356 {
5357 *arg = next_line_from_context(cctx, TRUE);
5358 p = skipwhite(*arg);
5359 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005360
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005361 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5362 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005363 semsg(_(e_white_space_required_before_and_after_str_at_str),
5364 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005365 return FAIL;
5366 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005368 // evaluate the third expression
5369 if (has_const_expr)
5370 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005371 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005372 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005373 return FAIL;
5374 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5375 return FAIL;
5376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377
Bram Moolenaara5565e42020-05-09 15:44:01 +02005378 if (!has_const_expr)
5379 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005380 type_T **typep;
5381
Bram Moolenaara5565e42020-05-09 15:44:01 +02005382 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005383
Bram Moolenaara5565e42020-05-09 15:44:01 +02005384 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005385 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5386 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005387
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005388 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005389 isn = ((isn_T *)instr->ga_data) + end_idx;
5390 isn->isn_arg.jump.jump_where = instr->ga_len;
5391 }
5392
5393 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394 }
5395 return OK;
5396}
5397
5398/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005399 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005400 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5401 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005402 */
5403 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005404compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005405{
5406 ppconst_T ppconst;
5407
5408 CLEAR_FIELD(ppconst);
5409 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5410 {
5411 clear_ppconst(&ppconst);
5412 return FAIL;
5413 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005414 if (is_const != NULL)
5415 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005416 if (generate_ppconst(cctx, &ppconst) == FAIL)
5417 return FAIL;
5418 return OK;
5419}
5420
5421/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005422 * Toplevel expression.
5423 */
5424 static int
5425compile_expr0(char_u **arg, cctx_T *cctx)
5426{
5427 return compile_expr0_ext(arg, cctx, NULL);
5428}
5429
5430/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005431 * Compile "return [expr]".
5432 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005433 */
5434 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005435compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005436{
5437 char_u *p = arg;
5438 garray_T *stack = &cctx->ctx_type_stack;
5439 type_T *stack_type;
5440
5441 if (*p != NUL && *p != '|' && *p != '\n')
5442 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005443 if (legacy)
5444 {
5445 int save_flags = cmdmod.cmod_flags;
5446
5447 generate_LEGACY_EVAL(cctx, p);
5448 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5449 0, cctx, FALSE, FALSE) == FAIL)
5450 return NULL;
5451 cmdmod.cmod_flags |= CMOD_LEGACY;
5452 (void)skip_expr(&p, NULL);
5453 cmdmod.cmod_flags = save_flags;
5454 }
5455 else
5456 {
5457 // compile return argument into instructions
5458 if (compile_expr0(&p, cctx) == FAIL)
5459 return NULL;
5460 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005461
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005462 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005463 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005464 // "check_return_type" with uf_ret_type set to &t_unknown is used
5465 // for an inline function without a specified return type. Set the
5466 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005467 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005468 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005469 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5470 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005471 || (!check_return_type
5472 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005473 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005474 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005475 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005476 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005477 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005478 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5479 && stack_type->tt_type != VAR_VOID
5480 && stack_type->tt_type != VAR_UNKNOWN)
5481 {
5482 emsg(_(e_returning_value_in_function_without_return_type));
5483 return NULL;
5484 }
5485 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005486 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005487 return NULL;
5488 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005489 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490 }
5491 else
5492 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005493 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005494 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005495 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5496 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005498 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005499 return NULL;
5500 }
5501
5502 // No argument, return zero.
5503 generate_PUSHNR(cctx, 0);
5504 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005505
5506 // Undo any command modifiers.
5507 generate_undo_cmdmods(cctx);
5508
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005509 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005510 return NULL;
5511
5512 // "return val | endif" is possible
5513 return skipwhite(p);
5514}
5515
5516/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005517 * Get a line from the compilation context, compatible with exarg_T getline().
5518 * Return a pointer to the line in allocated memory.
5519 * Return NULL for end-of-file or some error.
5520 */
5521 static char_u *
5522exarg_getline(
5523 int c UNUSED,
5524 void *cookie,
5525 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005526 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005527{
5528 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005529 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005530
Bram Moolenaar66250c92020-08-20 15:02:42 +02005531 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005532 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005533 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005534 return NULL;
5535 ++cctx->ctx_lnum;
5536 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5537 // Comment lines result in NULL pointers, skip them.
5538 if (p != NULL)
5539 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005540 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005541}
5542
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005543 void
5544fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5545{
5546 eap->getline = exarg_getline;
5547 eap->cookie = cctx;
5548}
5549
Bram Moolenaar04b12692020-05-04 23:24:44 +02005550/*
5551 * Compile a nested :def command.
5552 */
5553 static char_u *
5554compile_nested_function(exarg_T *eap, cctx_T *cctx)
5555{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005556 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005557 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005558 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005559 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005560 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005561 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005562
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005563 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005564 {
5565 emsg(_(e_cannot_use_bang_with_nested_def));
5566 return NULL;
5567 }
5568
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005569 if (*name_start == '/')
5570 {
5571 name_end = skip_regexp(name_start + 1, '/', TRUE);
5572 if (*name_end == '/')
5573 ++name_end;
5574 eap->nextcmd = check_nextcmd(name_end);
5575 }
5576 if (name_end == name_start || *skipwhite(name_end) != '(')
5577 {
5578 if (!ends_excmd2(name_start, name_end))
5579 {
5580 semsg(_(e_invalid_command_str), eap->cmd);
5581 return NULL;
5582 }
5583
5584 // "def" or "def Name": list functions
5585 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5586 return NULL;
5587 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5588 }
5589
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005590 // Only g:Func() can use a namespace.
5591 if (name_start[1] == ':' && !is_global)
5592 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005593 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005594 return NULL;
5595 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005596 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005597 return NULL;
5598
Bram Moolenaar04b12692020-05-04 23:24:44 +02005599 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005600 fill_exarg_from_cctx(eap, cctx);
5601
Bram Moolenaar04b12692020-05-04 23:24:44 +02005602 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005603 lambda_name = vim_strsave(get_lambda_name());
5604 if (lambda_name == NULL)
5605 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005606 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005607
Bram Moolenaar822ba242020-05-24 23:00:18 +02005608 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005609 {
5610 r = eap->skip ? OK : FAIL;
5611 goto theend;
5612 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005613
5614 // copy over the block scope IDs before compiling
5615 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5616 {
5617 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5618
5619 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5620 if (ufunc->uf_block_ids != NULL)
5621 {
5622 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5623 sizeof(int) * block_depth);
5624 ufunc->uf_block_depth = block_depth;
5625 }
5626 }
5627
Bram Moolenaare99d4222021-06-13 14:01:26 +02005628 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
5629 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005630 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005631 {
5632 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005633 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005634 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005635
Bram Moolenaar648594e2021-07-11 17:55:01 +02005636#ifdef FEAT_PROFILE
5637 // When the outer function is compiled for profiling, the nested function
5638 // may be called without profiling. Compile it here in the right context.
5639 if (COMPILE_TYPE(ufunc) == CT_PROFILE
5640 && func_needs_compiling(ufunc, CT_NONE))
5641 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5642#endif
5643
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005644 if (is_global)
5645 {
5646 char_u *func_name = vim_strnsave(name_start + 2,
5647 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005648
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005649 if (func_name == NULL)
5650 r = FAIL;
5651 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005652 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005653 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005654 lambda_name = NULL;
5655 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005656 }
5657 else
5658 {
5659 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005660 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005661 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005662
Bram Moolenaareef21022020-08-01 22:16:43 +02005663 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005664 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005665 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005666 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005667 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5668 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005669 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005670
5671theend:
5672 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005673 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005674}
5675
5676/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005677 * Return the length of an assignment operator, or zero if there isn't one.
5678 */
5679 int
5680assignment_len(char_u *p, int *heredoc)
5681{
5682 if (*p == '=')
5683 {
5684 if (p[1] == '<' && p[2] == '<')
5685 {
5686 *heredoc = TRUE;
5687 return 3;
5688 }
5689 return 1;
5690 }
5691 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5692 return 2;
5693 if (STRNCMP(p, "..=", 3) == 0)
5694 return 3;
5695 return 0;
5696}
5697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005698/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005699 * Generate the load instruction for "name".
5700 */
5701 static void
5702generate_loadvar(
5703 cctx_T *cctx,
5704 assign_dest_T dest,
5705 char_u *name,
5706 lvar_T *lvar,
5707 type_T *type)
5708{
5709 switch (dest)
5710 {
5711 case dest_option:
5712 // TODO: check the option exists
5713 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5714 break;
5715 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005716 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5717 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5718 else
5719 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005720 break;
5721 case dest_buffer:
5722 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5723 break;
5724 case dest_window:
5725 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5726 break;
5727 case dest_tab:
5728 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5729 break;
5730 case dest_script:
5731 compile_load_scriptvar(cctx,
5732 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5733 break;
5734 case dest_env:
5735 // Include $ in the name here
5736 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5737 break;
5738 case dest_reg:
5739 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5740 break;
5741 case dest_vimvar:
5742 generate_LOADV(cctx, name + 2, TRUE);
5743 break;
5744 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005745 if (lvar->lv_from_outer > 0)
5746 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5747 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005748 else
5749 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5750 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005751 case dest_expr:
5752 // list or dict value should already be on the stack.
5753 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005754 }
5755}
5756
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005757/*
5758 * Skip over "[expr]" or ".member".
5759 * Does not check for any errors.
5760 */
5761 static char_u *
5762skip_index(char_u *start)
5763{
5764 char_u *p = start;
5765
5766 if (*p == '[')
5767 {
5768 p = skipwhite(p + 1);
5769 (void)skip_expr(&p, NULL);
5770 p = skipwhite(p);
5771 if (*p == ']')
5772 return p + 1;
5773 return p;
5774 }
5775 // if (*p == '.')
5776 return to_name_end(p + 1, TRUE);
5777}
5778
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005779 void
5780vim9_declare_error(char_u *name)
5781{
5782 char *scope = "";
5783
5784 switch (*name)
5785 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005786 case 'g': scope = _("global"); break;
5787 case 'b': scope = _("buffer"); break;
5788 case 'w': scope = _("window"); break;
5789 case 't': scope = _("tab"); break;
5790 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005791 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005792 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005793 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005794 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005795 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005796 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005797 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005798 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005799 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005800}
5801
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005802/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005803 * For one assignment figure out the type of destination. Return it in "dest".
5804 * When not recognized "dest" is not set.
5805 * For an option "opt_flags" is set.
5806 * For a v:var "vimvaridx" is set.
5807 * "type" is set to the destination type if known, unchanted otherwise.
5808 * Return FAIL if an error message was given.
5809 */
5810 static int
5811get_var_dest(
5812 char_u *name,
5813 assign_dest_T *dest,
5814 int cmdidx,
5815 int *opt_flags,
5816 int *vimvaridx,
5817 type_T **type,
5818 cctx_T *cctx)
5819{
5820 char_u *p;
5821
5822 if (*name == '&')
5823 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005824 int cc;
5825 long numval;
5826 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005827
5828 *dest = dest_option;
5829 if (cmdidx == CMD_final || cmdidx == CMD_const)
5830 {
5831 emsg(_(e_const_option));
5832 return FAIL;
5833 }
5834 p = name;
5835 p = find_option_end(&p, opt_flags);
5836 if (p == NULL)
5837 {
5838 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005839 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005840 return FAIL;
5841 }
5842 cc = *p;
5843 *p = NUL;
5844 opt_type = get_option_value(skip_option_env_lead(name),
5845 &numval, NULL, *opt_flags);
5846 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005847 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005848 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005849 case gov_unknown:
5850 semsg(_(e_unknown_option), name);
5851 return FAIL;
5852 case gov_string:
5853 case gov_hidden_string:
5854 *type = &t_string;
5855 break;
5856 case gov_bool:
5857 case gov_hidden_bool:
5858 *type = &t_bool;
5859 break;
5860 case gov_number:
5861 case gov_hidden_number:
5862 *type = &t_number;
5863 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005864 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005865 }
5866 else if (*name == '$')
5867 {
5868 *dest = dest_env;
5869 *type = &t_string;
5870 }
5871 else if (*name == '@')
5872 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005873 if (name[1] != '@'
5874 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005875 {
5876 emsg_invreg(name[1]);
5877 return FAIL;
5878 }
5879 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005880 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005881 }
5882 else if (STRNCMP(name, "g:", 2) == 0)
5883 {
5884 *dest = dest_global;
5885 }
5886 else if (STRNCMP(name, "b:", 2) == 0)
5887 {
5888 *dest = dest_buffer;
5889 }
5890 else if (STRNCMP(name, "w:", 2) == 0)
5891 {
5892 *dest = dest_window;
5893 }
5894 else if (STRNCMP(name, "t:", 2) == 0)
5895 {
5896 *dest = dest_tab;
5897 }
5898 else if (STRNCMP(name, "v:", 2) == 0)
5899 {
5900 typval_T *vtv;
5901 int di_flags;
5902
5903 *vimvaridx = find_vim_var(name + 2, &di_flags);
5904 if (*vimvaridx < 0)
5905 {
5906 semsg(_(e_variable_not_found_str), name);
5907 return FAIL;
5908 }
5909 // We use the current value of "sandbox" here, is that OK?
5910 if (var_check_ro(di_flags, name, FALSE))
5911 return FAIL;
5912 *dest = dest_vimvar;
5913 vtv = get_vim_var_tv(*vimvaridx);
5914 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5915 }
5916 return OK;
5917}
5918
5919/*
5920 * Generate a STORE instruction for "dest", not being "dest_local".
5921 * Return FAIL when out of memory.
5922 */
5923 static int
5924generate_store_var(
5925 cctx_T *cctx,
5926 assign_dest_T dest,
5927 int opt_flags,
5928 int vimvaridx,
5929 int scriptvar_idx,
5930 int scriptvar_sid,
5931 type_T *type,
5932 char_u *name)
5933{
5934 switch (dest)
5935 {
5936 case dest_option:
5937 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5938 opt_flags);
5939 case dest_global:
5940 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005941 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5942 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005943 case dest_buffer:
5944 // include b: with the name, easier to execute that way
5945 return generate_STORE(cctx, ISN_STOREB, 0, name);
5946 case dest_window:
5947 // include w: with the name, easier to execute that way
5948 return generate_STORE(cctx, ISN_STOREW, 0, name);
5949 case dest_tab:
5950 // include t: with the name, easier to execute that way
5951 return generate_STORE(cctx, ISN_STORET, 0, name);
5952 case dest_env:
5953 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5954 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005955 return generate_STORE(cctx, ISN_STOREREG,
5956 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005957 case dest_vimvar:
5958 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5959 case dest_script:
5960 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005961 // "s:" may be included in the name.
5962 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005963 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005964 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5965 scriptvar_sid, scriptvar_idx, type);
5966 case dest_local:
5967 case dest_expr:
5968 // cannot happen
5969 break;
5970 }
5971 return FAIL;
5972}
5973
Bram Moolenaar752fc692021-01-04 21:57:11 +01005974 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005975generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5976{
5977 if (lhs->lhs_dest != dest_local)
5978 return generate_store_var(cctx, lhs->lhs_dest,
5979 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5980 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5981 lhs->lhs_type, lhs->lhs_name);
5982
5983 if (lhs->lhs_lvar != NULL)
5984 {
5985 garray_T *instr = &cctx->ctx_instr;
5986 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5987
5988 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5989 // ISN_STORENR
5990 if (lhs->lhs_lvar->lv_from_outer == 0
5991 && instr->ga_len == instr_count + 1
5992 && isn->isn_type == ISN_PUSHNR)
5993 {
5994 varnumber_T val = isn->isn_arg.number;
5995 garray_T *stack = &cctx->ctx_type_stack;
5996
5997 isn->isn_type = ISN_STORENR;
5998 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5999 isn->isn_arg.storenr.stnr_val = val;
6000 if (stack->ga_len > 0)
6001 --stack->ga_len;
6002 }
6003 else if (lhs->lhs_lvar->lv_from_outer > 0)
6004 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6005 lhs->lhs_lvar->lv_from_outer);
6006 else
6007 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6008 }
6009 return OK;
6010}
6011
6012 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006013is_decl_command(int cmdidx)
6014{
6015 return cmdidx == CMD_let || cmdidx == CMD_var
6016 || cmdidx == CMD_final || cmdidx == CMD_const;
6017}
6018
6019/*
6020 * Figure out the LHS type and other properties for an assignment or one item
6021 * of ":unlet" with an index.
6022 * Returns OK or FAIL.
6023 */
6024 static int
6025compile_lhs(
6026 char_u *var_start,
6027 lhs_T *lhs,
6028 int cmdidx,
6029 int heredoc,
6030 int oplen,
6031 cctx_T *cctx)
6032{
6033 char_u *var_end;
6034 int is_decl = is_decl_command(cmdidx);
6035
6036 CLEAR_POINTER(lhs);
6037 lhs->lhs_dest = dest_local;
6038 lhs->lhs_vimvaridx = -1;
6039 lhs->lhs_scriptvar_idx = -1;
6040
6041 // "dest_end" is the end of the destination, including "[expr]" or
6042 // ".name".
6043 // "var_end" is the end of the variable/option/etc. name.
6044 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6045 if (*var_start == '@')
6046 var_end = var_start + 2;
6047 else
6048 {
6049 // skip over the leading "&", "&l:", "&g:" and "$"
6050 var_end = skip_option_env_lead(var_start);
6051 var_end = to_name_end(var_end, TRUE);
6052 }
6053
6054 // "a: type" is declaring variable "a" with a type, not dict "a:".
6055 if (is_decl && lhs->lhs_dest_end == var_start + 2
6056 && lhs->lhs_dest_end[-1] == ':')
6057 --lhs->lhs_dest_end;
6058 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6059 --var_end;
6060
6061 // compute the length of the destination without "[expr]" or ".name"
6062 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006063 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006064 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6065 if (lhs->lhs_name == NULL)
6066 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006067
6068 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6069 // Something follows after the variable: "var[idx]" or "var.key".
6070 lhs->lhs_has_index = TRUE;
6071
Bram Moolenaar752fc692021-01-04 21:57:11 +01006072 if (heredoc)
6073 lhs->lhs_type = &t_list_string;
6074 else
6075 lhs->lhs_type = &t_any;
6076
6077 if (cctx->ctx_skip != SKIP_YES)
6078 {
6079 int declare_error = FALSE;
6080
6081 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6082 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6083 &lhs->lhs_type, cctx) == FAIL)
6084 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006085 if (lhs->lhs_dest != dest_local
6086 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006087 {
6088 // Specific kind of variable recognized.
6089 declare_error = is_decl;
6090 }
6091 else
6092 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006093 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006094 if (check_reserved_name(lhs->lhs_name) == FAIL)
6095 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006096
6097 if (lookup_local(var_start, lhs->lhs_varlen,
6098 &lhs->lhs_local_lvar, cctx) == OK)
6099 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6100 else
6101 {
6102 CLEAR_FIELD(lhs->lhs_arg_lvar);
6103 if (arg_exists(var_start, lhs->lhs_varlen,
6104 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6105 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6106 {
6107 if (is_decl)
6108 {
6109 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6110 return FAIL;
6111 }
6112 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6113 }
6114 }
6115 if (lhs->lhs_lvar != NULL)
6116 {
6117 if (is_decl)
6118 {
6119 semsg(_(e_variable_already_declared), lhs->lhs_name);
6120 return FAIL;
6121 }
6122 }
6123 else
6124 {
6125 int script_namespace = lhs->lhs_varlen > 1
6126 && STRNCMP(var_start, "s:", 2) == 0;
6127 int script_var = (script_namespace
6128 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006129 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006130 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006131 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006132 imported_T *import =
6133 find_imported(var_start, lhs->lhs_varlen, cctx);
6134
6135 if (script_namespace || script_var || import != NULL)
6136 {
6137 char_u *rawname = lhs->lhs_name
6138 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6139
6140 if (is_decl)
6141 {
6142 if (script_namespace)
6143 semsg(_(e_cannot_declare_script_variable_in_function),
6144 lhs->lhs_name);
6145 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006146 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006147 lhs->lhs_name);
6148 return FAIL;
6149 }
6150 else if (cctx->ctx_ufunc->uf_script_ctx_version
6151 == SCRIPT_VERSION_VIM9
6152 && script_namespace
6153 && !script_var && import == NULL)
6154 {
6155 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6156 return FAIL;
6157 }
6158
6159 lhs->lhs_dest = dest_script;
6160
6161 // existing script-local variables should have a type
6162 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6163 if (import != NULL)
6164 lhs->lhs_scriptvar_sid = import->imp_sid;
6165 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6166 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006167 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006168 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006169 lhs->lhs_scriptvar_sid, rawname,
6170 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6171 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006172 if (lhs->lhs_scriptvar_idx >= 0)
6173 {
6174 scriptitem_T *si = SCRIPT_ITEM(
6175 lhs->lhs_scriptvar_sid);
6176 svar_T *sv =
6177 ((svar_T *)si->sn_var_vals.ga_data)
6178 + lhs->lhs_scriptvar_idx;
6179 lhs->lhs_type = sv->sv_type;
6180 }
6181 }
6182 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006183 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006184 == FAIL)
6185 return FAIL;
6186 }
6187 }
6188
6189 if (declare_error)
6190 {
6191 vim9_declare_error(lhs->lhs_name);
6192 return FAIL;
6193 }
6194 }
6195
6196 // handle "a:name" as a name, not index "name" on "a"
6197 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6198 var_end = lhs->lhs_dest_end;
6199
6200 if (lhs->lhs_dest != dest_option)
6201 {
6202 if (is_decl && *var_end == ':')
6203 {
6204 char_u *p;
6205
6206 // parse optional type: "let var: type = expr"
6207 if (!VIM_ISWHITE(var_end[1]))
6208 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006209 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006210 return FAIL;
6211 }
6212 p = skipwhite(var_end + 1);
6213 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6214 if (lhs->lhs_type == NULL)
6215 return FAIL;
6216 lhs->lhs_has_type = TRUE;
6217 }
6218 else if (lhs->lhs_lvar != NULL)
6219 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6220 }
6221
Bram Moolenaare42939a2021-04-05 17:11:17 +02006222 if (oplen == 3 && !heredoc
6223 && lhs->lhs_dest != dest_global
6224 && !lhs->lhs_has_index
6225 && lhs->lhs_type->tt_type != VAR_STRING
6226 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006227 {
6228 emsg(_(e_can_only_concatenate_to_string));
6229 return FAIL;
6230 }
6231
6232 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6233 && cctx->ctx_skip != SKIP_YES)
6234 {
6235 if (oplen > 1 && !heredoc)
6236 {
6237 // +=, /=, etc. require an existing variable
6238 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6239 return FAIL;
6240 }
6241 if (!is_decl)
6242 {
6243 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6244 return FAIL;
6245 }
6246
Bram Moolenaar3f327882021-03-17 20:56:38 +01006247 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006248 if ((lhs->lhs_type->tt_type == VAR_FUNC
6249 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006250 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006251 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006252
6253 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006254 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6255 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6256 if (lhs->lhs_lvar == NULL)
6257 return FAIL;
6258 lhs->lhs_new_local = TRUE;
6259 }
6260
6261 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006262 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006263 {
6264 // Something follows after the variable: "var[idx]" or "var.key".
6265 // TODO: should we also handle "->func()" here?
6266 if (is_decl)
6267 {
6268 emsg(_(e_cannot_use_index_when_declaring_variable));
6269 return FAIL;
6270 }
6271
6272 if (var_start[lhs->lhs_varlen] == '['
6273 || var_start[lhs->lhs_varlen] == '.')
6274 {
6275 char_u *after = var_start + lhs->lhs_varlen;
6276 char_u *p;
6277
6278 // Only the last index is used below, if there are others
6279 // before it generate code for the expression. Thus for
6280 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6281 for (;;)
6282 {
6283 p = skip_index(after);
6284 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006285 {
6286 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006287 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006288 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006289 after = p;
6290 }
6291 if (after > var_start + lhs->lhs_varlen)
6292 {
6293 lhs->lhs_varlen = after - var_start;
6294 lhs->lhs_dest = dest_expr;
6295 // We don't know the type before evaluating the expression,
6296 // use "any" until then.
6297 lhs->lhs_type = &t_any;
6298 }
6299
Bram Moolenaar752fc692021-01-04 21:57:11 +01006300 if (lhs->lhs_type->tt_member == NULL)
6301 lhs->lhs_member_type = &t_any;
6302 else
6303 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6304 }
6305 else
6306 {
6307 semsg("Not supported yet: %s", var_start);
6308 return FAIL;
6309 }
6310 }
6311 return OK;
6312}
6313
6314/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006315 * Figure out the LHS and check a few errors.
6316 */
6317 static int
6318compile_assign_lhs(
6319 char_u *var_start,
6320 lhs_T *lhs,
6321 int cmdidx,
6322 int is_decl,
6323 int heredoc,
6324 int oplen,
6325 cctx_T *cctx)
6326{
6327 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6328 return FAIL;
6329
6330 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6331 {
6332 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6333 return FAIL;
6334 }
6335 if (!is_decl && lhs->lhs_lvar != NULL
6336 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6337 {
6338 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6339 return FAIL;
6340 }
6341 return OK;
6342}
6343
6344/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006345 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6346 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006347 */
6348 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006349compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006350 char_u *var_start,
6351 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006352 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006353 cctx_T *cctx)
6354{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006355 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006356 char_u *p;
6357 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006358 int need_white_before = TRUE;
6359 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006360
Bram Moolenaar752fc692021-01-04 21:57:11 +01006361 p = var_start + varlen;
6362 if (*p == '[')
6363 {
6364 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006365 if (*p == ':')
6366 {
6367 // empty first index, push zero
6368 r = generate_PUSHNR(cctx, 0);
6369 need_white_before = FALSE;
6370 }
6371 else
6372 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006373
6374 if (r == OK && *skipwhite(p) == ':')
6375 {
6376 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006377 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006378 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006379 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006380 empty_second = *skipwhite(p + 1) == ']';
6381 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6382 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006383 {
6384 semsg(_(e_white_space_required_before_and_after_str_at_str),
6385 ":", p);
6386 return FAIL;
6387 }
6388 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006389 if (*p == ']')
6390 // empty second index, push "none"
6391 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6392 else
6393 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006394 }
6395
Bram Moolenaar752fc692021-01-04 21:57:11 +01006396 if (r == OK && *skipwhite(p) != ']')
6397 {
6398 // this should not happen
6399 emsg(_(e_missbrac));
6400 r = FAIL;
6401 }
6402 }
6403 else // if (*p == '.')
6404 {
6405 char_u *key_end = to_name_end(p + 1, TRUE);
6406 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6407
6408 r = generate_PUSHS(cctx, key);
6409 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006410 return r;
6411}
6412
6413/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006414 * For a LHS with an index, load the variable to be indexed.
6415 */
6416 static int
6417compile_load_lhs(
6418 lhs_T *lhs,
6419 char_u *var_start,
6420 type_T *rhs_type,
6421 cctx_T *cctx)
6422{
6423 if (lhs->lhs_dest == dest_expr)
6424 {
6425 size_t varlen = lhs->lhs_varlen;
6426 int c = var_start[varlen];
6427 char_u *p = var_start;
6428 garray_T *stack = &cctx->ctx_type_stack;
6429
6430 // Evaluate "ll[expr]" of "ll[expr][idx]"
6431 var_start[varlen] = NUL;
6432 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6433 {
6434 // this should not happen
6435 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006436 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006437 return FAIL;
6438 }
6439 var_start[varlen] = c;
6440
6441 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6442 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6443 // now we can properly check the type
6444 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6445 && rhs_type != &t_void
6446 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6447 FALSE, FALSE) == FAIL)
6448 return FAIL;
6449 }
6450 else
6451 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6452 lhs->lhs_lvar, lhs->lhs_type);
6453 return OK;
6454}
6455
6456/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006457 * Produce code for loading "lhs" and also take care of an index.
6458 * Return OK/FAIL.
6459 */
6460 static int
6461compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6462{
6463 compile_load_lhs(lhs, var_start, NULL, cctx);
6464
6465 if (lhs->lhs_has_index)
6466 {
6467 int range = FALSE;
6468
6469 // Get member from list or dict. First compile the
6470 // index value.
6471 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6472 return FAIL;
6473 if (range)
6474 {
6475 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6476 var_start);
6477 return FAIL;
6478 }
6479
6480 // Get the member.
6481 if (compile_member(FALSE, cctx) == FAIL)
6482 return FAIL;
6483 }
6484 return OK;
6485}
6486
6487/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006488 * Assignment to a list or dict member, or ":unlet" for the item, using the
6489 * information in "lhs".
6490 * Returns OK or FAIL.
6491 */
6492 static int
6493compile_assign_unlet(
6494 char_u *var_start,
6495 lhs_T *lhs,
6496 int is_assign,
6497 type_T *rhs_type,
6498 cctx_T *cctx)
6499{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006500 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006501 garray_T *stack = &cctx->ctx_type_stack;
6502 int range = FALSE;
6503
Bram Moolenaar68452172021-04-12 21:21:02 +02006504 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006505 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006506 if (is_assign && range && lhs->lhs_type != &t_blob
6507 && lhs->lhs_type != &t_any)
6508 {
6509 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6510 return FAIL;
6511 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006512
6513 if (lhs->lhs_type == &t_any)
6514 {
6515 // Index on variable of unknown type: check at runtime.
6516 dest_type = VAR_ANY;
6517 }
6518 else
6519 {
6520 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006521 if (dest_type == VAR_DICT && range)
6522 {
6523 emsg(e_cannot_use_range_with_dictionary);
6524 return FAIL;
6525 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006526 if (dest_type == VAR_DICT
6527 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006528 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006529 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006530 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006531 type_T *type;
6532
6533 if (range)
6534 {
6535 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6536 if (need_type(type, &t_number,
6537 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006538 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006539 }
6540 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6541 if ((dest_type != VAR_BLOB || type != &t_special)
6542 && need_type(type, &t_number,
6543 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006544 return FAIL;
6545 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006546 }
6547
6548 // Load the dict or list. On the stack we then have:
6549 // - value (for assignment, not for :unlet)
6550 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006551 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006552 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006553 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6554 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006555
Bram Moolenaar68452172021-04-12 21:21:02 +02006556 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6557 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006558 {
6559 if (is_assign)
6560 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006561 if (range)
6562 {
6563 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6564 return FAIL;
6565 }
6566 else
6567 {
6568 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006569
Bram Moolenaar68452172021-04-12 21:21:02 +02006570 if (isn == NULL)
6571 return FAIL;
6572 isn->isn_arg.vartype = dest_type;
6573 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006574 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006575 else if (range)
6576 {
6577 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6578 return FAIL;
6579 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006580 else
6581 {
6582 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6583 return FAIL;
6584 }
6585 }
6586 else
6587 {
6588 emsg(_(e_indexable_type_required));
6589 return FAIL;
6590 }
6591
6592 return OK;
6593}
6594
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006595/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006596 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006597 * "let name"
6598 * "var name = expr"
6599 * "final name = expr"
6600 * "const name = expr"
6601 * "name = expr"
6602 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006603 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006604 * Return NULL for an error.
6605 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 */
6607 static char_u *
6608compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6609{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006610 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006612 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 char_u *ret = NULL;
6614 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006615 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006617 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006618 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006619 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621 int oplen = 0;
6622 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006623 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006624 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006626 int is_decl = is_decl_command(cmdidx);
6627 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006628 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006630 // Skip over the "var" or "[var, var]" to get to any "=".
6631 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6632 if (p == NULL)
6633 return *arg == '[' ? arg : NULL;
6634
6635 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006637 // TODO: should we allow this, and figure out type inference from list
6638 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006639 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006640 return NULL;
6641 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006642 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006644 sp = p;
6645 p = skipwhite(p);
6646 op = p;
6647 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006648
6649 if (var_count > 0 && oplen == 0)
6650 // can be something like "[1, 2]->func()"
6651 return arg;
6652
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006653 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006655 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006656 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006657 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006658 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6659 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006660 if (VIM_ISWHITE(eap->cmd[2]))
6661 {
6662 semsg(_(e_no_white_space_allowed_after_str_str),
6663 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6664 return NULL;
6665 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006666 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6667 oplen = 2;
6668 incdec = TRUE;
6669 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006671 if (heredoc)
6672 {
6673 list_T *l;
6674 listitem_T *li;
6675
6676 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006677 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006679 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006680 if (l == NULL)
6681 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682
Bram Moolenaar078269b2020-09-21 20:35:55 +02006683 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006684 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006685 // Push each line and the create the list.
6686 FOR_ALL_LIST_ITEMS(l, li)
6687 {
6688 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6689 li->li_tv.vval.v_string = NULL;
6690 }
6691 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693 list_free(l);
6694 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006695 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006697 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006699 char_u *wp;
6700
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006701 // for "[var, var] = expr" evaluate the expression here, loop over the
6702 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006703 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006704
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006705 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006706 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006707 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006708 if (compile_expr0(&p, cctx) == FAIL)
6709 return NULL;
6710 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006711
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006712 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006714 type_T *stacktype;
6715
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006716 stacktype = stack->ga_len == 0 ? &t_void
6717 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006718 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006720 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006721 goto theend;
6722 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006723 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006724 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006725 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006726 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006727 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6728 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006729 if (stacktype->tt_member != NULL)
6730 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006731 }
6732 }
6733
6734 /*
6735 * Loop over variables in "[var, var] = expr".
6736 * For "var = expr" and "let var: type" this is done only once.
6737 */
6738 if (var_count > 0)
6739 var_start = skipwhite(arg + 1); // skip over the "["
6740 else
6741 var_start = arg;
6742 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6743 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006744 int instr_count = -1;
6745 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006746
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006747 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6748 {
6749 // Ignore underscore in "[a, _, b] = list".
6750 if (var_count > 0)
6751 {
6752 var_start = skipwhite(var_start + 2);
6753 continue;
6754 }
6755 emsg(_(e_cannot_use_underscore_here));
6756 goto theend;
6757 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006758 vim_free(lhs.lhs_name);
6759
6760 /*
6761 * Figure out the LHS type and other properties.
6762 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006763 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6764 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006765 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006766 if (!heredoc)
6767 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006768 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006769 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006770 if (oplen > 0 && var_count == 0)
6771 {
6772 // skip over the "=" and the expression
6773 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006774 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006775 }
6776 }
6777 else if (oplen > 0)
6778 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006779 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006780 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006781
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006782 // for "+=", "*=", "..=" etc. first load the current value
6783 if (*op != '='
6784 && compile_load_lhs_with_index(&lhs, var_start,
6785 cctx) == FAIL)
6786 goto theend;
6787
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006788 // For "var = expr" evaluate the expression.
6789 if (var_count == 0)
6790 {
6791 int r;
6792
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006793 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006794 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006795 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006796 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006797 r = generate_PUSHNR(cctx, 1);
6798 }
6799 else
6800 {
6801 // Temporarily hide the new local variable here, it is
6802 // not available to this expression.
6803 if (lhs.lhs_new_local)
6804 --cctx->ctx_locals.ga_len;
6805 wp = op + oplen;
6806 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6807 {
6808 if (lhs.lhs_new_local)
6809 ++cctx->ctx_locals.ga_len;
6810 goto theend;
6811 }
6812 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006813 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006814 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006815 if (r == FAIL)
6816 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006817 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006818 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006819 else if (semicolon && var_idx == var_count - 1)
6820 {
6821 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006822 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006823 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6824 goto theend;
6825 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006826 else
6827 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006828 // For "[var, var] = expr" get the "var_idx" item from the
6829 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006830 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006831 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006832 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006833
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006834 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006835 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006836 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006837 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006838 if ((rhs_type->tt_type == VAR_FUNC
6839 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006840 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006841 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006842 goto theend;
6843
Bram Moolenaar752fc692021-01-04 21:57:11 +01006844 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006845 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006846 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006847 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006848 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006849 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006850 }
6851 else
6852 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006853 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006854 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006855 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006856 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006857 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006858 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006859 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006860 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006861 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006862 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006863 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006864 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006865 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006866 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006867 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006868
Bram Moolenaar77709b12021-04-03 21:01:01 +02006869 // Without operator check type here, otherwise below.
6870 // Use the line number of the assignment.
6871 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006872 if (lhs.lhs_has_index)
6873 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006874 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006875 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006876 goto theend;
6877 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006878 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006879 else
6880 {
6881 type_T *lhs_type = lhs.lhs_member_type;
6882
6883 // Special case: assigning to @# can use a number or a
6884 // string.
6885 if (lhs_type == &t_number_or_string
6886 && rhs_type->tt_type == VAR_NUMBER)
6887 lhs_type = &t_number;
6888 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006889 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006890 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006893 else if (cmdidx == CMD_final)
6894 {
6895 emsg(_(e_final_requires_a_value));
6896 goto theend;
6897 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006898 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006899 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006900 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006901 goto theend;
6902 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006903 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006904 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006905 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006906 goto theend;
6907 }
6908 else
6909 {
6910 // variables are always initialized
6911 if (ga_grow(instr, 1) == FAIL)
6912 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006913 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006914 {
6915 case VAR_BOOL:
6916 generate_PUSHBOOL(cctx, VVAL_FALSE);
6917 break;
6918 case VAR_FLOAT:
6919#ifdef FEAT_FLOAT
6920 generate_PUSHF(cctx, 0.0);
6921#endif
6922 break;
6923 case VAR_STRING:
6924 generate_PUSHS(cctx, NULL);
6925 break;
6926 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006927 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006928 break;
6929 case VAR_FUNC:
6930 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6931 break;
6932 case VAR_LIST:
6933 generate_NEWLIST(cctx, 0);
6934 break;
6935 case VAR_DICT:
6936 generate_NEWDICT(cctx, 0);
6937 break;
6938 case VAR_JOB:
6939 generate_PUSHJOB(cctx, NULL);
6940 break;
6941 case VAR_CHANNEL:
6942 generate_PUSHCHANNEL(cctx, NULL);
6943 break;
6944 case VAR_NUMBER:
6945 case VAR_UNKNOWN:
6946 case VAR_ANY:
6947 case VAR_PARTIAL:
6948 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006949 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006950 case VAR_SPECIAL: // cannot happen
6951 generate_PUSHNR(cctx, 0);
6952 break;
6953 }
6954 }
6955 if (var_count == 0)
6956 end = p;
6957 }
6958
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006959 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006960 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006961 break;
6962
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006963 if (oplen > 0 && *op != '=')
6964 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006965 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006966 type_T *stacktype;
6967
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006968 if (*op == '.')
6969 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006970 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006971 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006972 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006973 if (
6974#ifdef FEAT_FLOAT
6975 // If variable is float operation with number is OK.
6976 !(expected == &t_float && stacktype == &t_number) &&
6977#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006978 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006979 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006980 goto theend;
6981
6982 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006983 {
6984 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6985 goto theend;
6986 }
6987 else if (*op == '+')
6988 {
6989 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006990 operator_type(lhs.lhs_member_type, stacktype),
6991 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006992 goto theend;
6993 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006994 else if (generate_two_op(cctx, op) == FAIL)
6995 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006998 // Use the line number of the assignment for store instruction.
6999 save_lnum = cctx->ctx_lnum;
7000 cctx->ctx_lnum = start_lnum - 1;
7001
Bram Moolenaar752fc692021-01-04 21:57:11 +01007002 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007003 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007004 // Use the info in "lhs" to store the value at the index in the
7005 // list or dict.
7006 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7007 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007008 {
7009 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007010 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007011 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007012 }
7013 else
7014 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007015 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007016 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007017 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007018 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007019 generate_LOCKCONST(cctx);
7020
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007021 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007022 && (lhs.lhs_type->tt_type == VAR_DICT
7023 || lhs.lhs_type->tt_type == VAR_LIST)
7024 && lhs.lhs_type->tt_member != NULL
7025 && lhs.lhs_type->tt_member != &t_any
7026 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007027 // Set the type in the list or dict, so that it can be checked,
7028 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007029 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007030
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007031 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007032 {
7033 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007034 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007035 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007036 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007037 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007038
7039 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007040 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007042
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007043 // For "[var, var] = expr" drop the "expr" value.
7044 // Also for "[var, var; _] = expr".
7045 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007046 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007047 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007048 goto theend;
7049 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007050
Bram Moolenaarb2097502020-07-19 17:17:02 +02007051 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052
7053theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007054 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055 return ret;
7056}
7057
7058/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007059 * Check for an assignment at "eap->cmd", compile it if found.
7060 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7061 */
7062 static int
7063may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7064{
7065 char_u *pskip;
7066 char_u *p;
7067
7068 // Assuming the command starts with a variable or function name,
7069 // find what follows.
7070 // Skip over "var.member", "var[idx]" and the like.
7071 // Also "&opt = val", "$ENV = val" and "@r = val".
7072 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7073 ? eap->cmd + 1 : eap->cmd;
7074 p = to_name_end(pskip, TRUE);
7075 if (p > eap->cmd && *p != NUL)
7076 {
7077 char_u *var_end;
7078 int oplen;
7079 int heredoc;
7080
7081 if (eap->cmd[0] == '@')
7082 var_end = eap->cmd + 2;
7083 else
7084 var_end = find_name_end(pskip, NULL, NULL,
7085 FNE_CHECK_START | FNE_INCL_BR);
7086 oplen = assignment_len(skipwhite(var_end), &heredoc);
7087 if (oplen > 0)
7088 {
7089 size_t len = p - eap->cmd;
7090
7091 // Recognize an assignment if we recognize the variable
7092 // name:
7093 // "g:var = expr"
7094 // "local = expr" where "local" is a local var.
7095 // "script = expr" where "script" is a script-local var.
7096 // "import = expr" where "import" is an imported var
7097 // "&opt = expr"
7098 // "$ENV = expr"
7099 // "@r = expr"
7100 if (*eap->cmd == '&'
7101 || *eap->cmd == '$'
7102 || *eap->cmd == '@'
7103 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007104 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007105 {
7106 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7107 if (*line == NULL || *line == eap->cmd)
7108 return FAIL;
7109 return OK;
7110 }
7111 }
7112 }
7113
7114 if (*eap->cmd == '[')
7115 {
7116 // [var, var] = expr
7117 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7118 if (*line == NULL)
7119 return FAIL;
7120 if (*line != eap->cmd)
7121 return OK;
7122 }
7123 return NOTDONE;
7124}
7125
7126/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007127 * Check if "name" can be "unlet".
7128 */
7129 int
7130check_vim9_unlet(char_u *name)
7131{
7132 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7133 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007134 // "unlet s:var" is allowed in legacy script.
7135 if (*name == 's' && !script_is_vim9())
7136 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007137 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007138 return FAIL;
7139 }
7140 return OK;
7141}
7142
7143/*
7144 * Callback passed to ex_unletlock().
7145 */
7146 static int
7147compile_unlet(
7148 lval_T *lvp,
7149 char_u *name_end,
7150 exarg_T *eap,
7151 int deep UNUSED,
7152 void *coookie)
7153{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007154 cctx_T *cctx = coookie;
7155 char_u *p = lvp->ll_name;
7156 int cc = *name_end;
7157 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007158
Bram Moolenaar752fc692021-01-04 21:57:11 +01007159 if (cctx->ctx_skip == SKIP_YES)
7160 return OK;
7161
7162 *name_end = NUL;
7163 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007164 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007165 // :unlet $ENV_VAR
7166 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7167 }
7168 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7169 {
7170 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007171
Bram Moolenaar752fc692021-01-04 21:57:11 +01007172 // This is similar to assigning: lookup the list/dict, compile the
7173 // idx/key. Then instead of storing the value unlet the item.
7174 // unlet {list}[idx]
7175 // unlet {dict}[key] dict.key
7176 //
7177 // Figure out the LHS type and other properties.
7178 //
7179 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7180
7181 // : unlet an indexed item
7182 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007183 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007184 iemsg("called compile_lhs() without an index");
7185 ret = FAIL;
7186 }
7187 else
7188 {
7189 // Use the info in "lhs" to unlet the item at the index in the
7190 // list or dict.
7191 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007192 }
7193
Bram Moolenaar752fc692021-01-04 21:57:11 +01007194 vim_free(lhs.lhs_name);
7195 }
7196 else if (check_vim9_unlet(p) == FAIL)
7197 {
7198 ret = FAIL;
7199 }
7200 else
7201 {
7202 // Normal name. Only supports g:, w:, t: and b: namespaces.
7203 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007204 }
7205
Bram Moolenaar752fc692021-01-04 21:57:11 +01007206 *name_end = cc;
7207 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007208}
7209
7210/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007211 * Callback passed to ex_unletlock().
7212 */
7213 static int
7214compile_lock_unlock(
7215 lval_T *lvp,
7216 char_u *name_end,
7217 exarg_T *eap,
7218 int deep UNUSED,
7219 void *coookie)
7220{
7221 cctx_T *cctx = coookie;
7222 int cc = *name_end;
7223 char_u *p = lvp->ll_name;
7224 int ret = OK;
7225 size_t len;
7226 char_u *buf;
7227
7228 if (cctx->ctx_skip == SKIP_YES)
7229 return OK;
7230
7231 // Cannot use :lockvar and :unlockvar on local variables.
7232 if (p[1] != ':')
7233 {
7234 char_u *end = skip_var_one(p, FALSE);
7235
7236 if (lookup_local(p, end - p, NULL, cctx) == OK)
7237 {
7238 emsg(_(e_cannot_lock_unlock_local_variable));
7239 return FAIL;
7240 }
7241 }
7242
7243 // Checking is done at runtime.
7244 *name_end = NUL;
7245 len = name_end - p + 20;
7246 buf = alloc(len);
7247 if (buf == NULL)
7248 ret = FAIL;
7249 else
7250 {
7251 vim_snprintf((char *)buf, len, "%s %s",
7252 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7253 p);
7254 ret = generate_EXEC(cctx, buf);
7255
7256 vim_free(buf);
7257 *name_end = cc;
7258 }
7259 return ret;
7260}
7261
7262/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007263 * compile "unlet var", "lock var" and "unlock var"
7264 * "arg" points to "var".
7265 */
7266 static char_u *
7267compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7268{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007269 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7270 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7271 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007272 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7273}
7274
7275/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276 * Compile an :import command.
7277 */
7278 static char_u *
7279compile_import(char_u *arg, cctx_T *cctx)
7280{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007281 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007282}
7283
7284/*
7285 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7286 */
7287 static int
7288compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7289{
7290 garray_T *instr = &cctx->ctx_instr;
7291 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7292
7293 if (endlabel == NULL)
7294 return FAIL;
7295 endlabel->el_next = *el;
7296 *el = endlabel;
7297 endlabel->el_end_label = instr->ga_len;
7298
7299 generate_JUMP(cctx, when, 0);
7300 return OK;
7301}
7302
7303 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007304compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305{
7306 garray_T *instr = &cctx->ctx_instr;
7307
7308 while (*el != NULL)
7309 {
7310 endlabel_T *cur = (*el);
7311 isn_T *isn;
7312
7313 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007314 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315 *el = cur->el_next;
7316 vim_free(cur);
7317 }
7318}
7319
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007320 static void
7321compile_free_jump_to_end(endlabel_T **el)
7322{
7323 while (*el != NULL)
7324 {
7325 endlabel_T *cur = (*el);
7326
7327 *el = cur->el_next;
7328 vim_free(cur);
7329 }
7330}
7331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332/*
7333 * Create a new scope and set up the generic items.
7334 */
7335 static scope_T *
7336new_scope(cctx_T *cctx, scopetype_T type)
7337{
7338 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7339
7340 if (scope == NULL)
7341 return NULL;
7342 scope->se_outer = cctx->ctx_scope;
7343 cctx->ctx_scope = scope;
7344 scope->se_type = type;
7345 scope->se_local_count = cctx->ctx_locals.ga_len;
7346 return scope;
7347}
7348
7349/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007350 * Free the current scope and go back to the outer scope.
7351 */
7352 static void
7353drop_scope(cctx_T *cctx)
7354{
7355 scope_T *scope = cctx->ctx_scope;
7356
7357 if (scope == NULL)
7358 {
7359 iemsg("calling drop_scope() without a scope");
7360 return;
7361 }
7362 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007363 switch (scope->se_type)
7364 {
7365 case IF_SCOPE:
7366 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7367 case FOR_SCOPE:
7368 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7369 case WHILE_SCOPE:
7370 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7371 case TRY_SCOPE:
7372 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7373 case NO_SCOPE:
7374 case BLOCK_SCOPE:
7375 break;
7376 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007377 vim_free(scope);
7378}
7379
7380/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 * compile "if expr"
7382 *
7383 * "if expr" Produces instructions:
7384 * EVAL expr Push result of "expr"
7385 * JUMP_IF_FALSE end
7386 * ... body ...
7387 * end:
7388 *
7389 * "if expr | else" Produces instructions:
7390 * EVAL expr Push result of "expr"
7391 * JUMP_IF_FALSE else
7392 * ... body ...
7393 * JUMP_ALWAYS end
7394 * else:
7395 * ... body ...
7396 * end:
7397 *
7398 * "if expr1 | elseif expr2 | else" Produces instructions:
7399 * EVAL expr Push result of "expr"
7400 * JUMP_IF_FALSE elseif
7401 * ... body ...
7402 * JUMP_ALWAYS end
7403 * elseif:
7404 * EVAL expr Push result of "expr"
7405 * JUMP_IF_FALSE else
7406 * ... body ...
7407 * JUMP_ALWAYS end
7408 * else:
7409 * ... body ...
7410 * end:
7411 */
7412 static char_u *
7413compile_if(char_u *arg, cctx_T *cctx)
7414{
7415 char_u *p = arg;
7416 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007417 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007418 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007419 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007420 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421
Bram Moolenaara5565e42020-05-09 15:44:01 +02007422 CLEAR_FIELD(ppconst);
7423 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007424 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007425 clear_ppconst(&ppconst);
7426 return NULL;
7427 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007428 if (!ends_excmd2(arg, skipwhite(p)))
7429 {
7430 semsg(_(e_trailing_arg), p);
7431 return NULL;
7432 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007433 if (cctx->ctx_skip == SKIP_YES)
7434 clear_ppconst(&ppconst);
7435 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007436 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007437 int error = FALSE;
7438 int v;
7439
Bram Moolenaara5565e42020-05-09 15:44:01 +02007440 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007441 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007442 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007443 if (error)
7444 return NULL;
7445 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007446 }
7447 else
7448 {
7449 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007450 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007451 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007452 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007453 if (bool_on_stack(cctx) == FAIL)
7454 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456
Bram Moolenaara91a7132021-03-25 21:12:15 +01007457 // CMDMOD_REV must come before the jump
7458 generate_undo_cmdmods(cctx);
7459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 scope = new_scope(cctx, IF_SCOPE);
7461 if (scope == NULL)
7462 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007463 scope->se_skip_save = skip_save;
7464 // "is_had_return" will be reset if any block does not end in :return
7465 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007467 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007468 {
7469 // "where" is set when ":elseif", "else" or ":endif" is found
7470 scope->se_u.se_if.is_if_label = instr->ga_len;
7471 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7472 }
7473 else
7474 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475
Bram Moolenaarced68a02021-01-24 17:53:47 +01007476#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007477 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007478 && skip_save != SKIP_YES)
7479 {
7480 // generated a profile start, need to generate a profile end, since it
7481 // won't be done after returning
7482 cctx->ctx_skip = SKIP_NOT;
7483 generate_instr(cctx, ISN_PROF_END);
7484 cctx->ctx_skip = SKIP_YES;
7485 }
7486#endif
7487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 return p;
7489}
7490
7491 static char_u *
7492compile_elseif(char_u *arg, cctx_T *cctx)
7493{
7494 char_u *p = arg;
7495 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007496 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007497 isn_T *isn;
7498 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007499 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007500 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007501
7502 if (scope == NULL || scope->se_type != IF_SCOPE)
7503 {
7504 emsg(_(e_elseif_without_if));
7505 return NULL;
7506 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007507 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007508 if (!cctx->ctx_had_return)
7509 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510
Bram Moolenaarced68a02021-01-24 17:53:47 +01007511 if (cctx->ctx_skip == SKIP_NOT)
7512 {
7513 // previous block was executed, this one and following will not
7514 cctx->ctx_skip = SKIP_YES;
7515 scope->se_u.se_if.is_seen_skip_not = TRUE;
7516 }
7517 if (scope->se_u.se_if.is_seen_skip_not)
7518 {
7519 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007520 // Do not count the "elseif" for profiling and cmdmod
7521 instr->ga_len = current_instr_idx(cctx);
7522
Bram Moolenaarced68a02021-01-24 17:53:47 +01007523 skip_expr_cctx(&p, cctx);
7524 return p;
7525 }
7526
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007527 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007528 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007529 int moved_cmdmod = FALSE;
7530
7531 // Move any CMDMOD instruction to after the jump
7532 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7533 {
7534 if (ga_grow(instr, 1) == FAIL)
7535 return NULL;
7536 ((isn_T *)instr->ga_data)[instr->ga_len] =
7537 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7538 --instr->ga_len;
7539 moved_cmdmod = TRUE;
7540 }
7541
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007542 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007543 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007544 return NULL;
7545 // previous "if" or "elseif" jumps here
7546 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7547 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007548 if (moved_cmdmod)
7549 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007552 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007553 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007554 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007555 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007556 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007557#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007558 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007559 {
7560 // the previous block was skipped, need to profile this line
7561 generate_instr(cctx, ISN_PROF_START);
7562 instr_count = instr->ga_len;
7563 }
7564#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007565 if (cctx->ctx_compile_type == CT_DEBUG)
7566 {
7567 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007568 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007569 instr_count = instr->ga_len;
7570 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007571 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007572 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007573 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007574 clear_ppconst(&ppconst);
7575 return NULL;
7576 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007577 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007578 if (!ends_excmd2(arg, skipwhite(p)))
7579 {
7580 semsg(_(e_trailing_arg), p);
7581 return NULL;
7582 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007583 if (scope->se_skip_save == SKIP_YES)
7584 clear_ppconst(&ppconst);
7585 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007586 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007587 int error = FALSE;
7588 int v;
7589
Bram Moolenaar7f141552020-05-09 17:35:53 +02007590 // The expression results in a constant.
7591 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007592 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7593 if (error)
7594 return NULL;
7595 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007596 clear_ppconst(&ppconst);
7597 scope->se_u.se_if.is_if_label = -1;
7598 }
7599 else
7600 {
7601 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007602 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007603 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007604 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007605 if (bool_on_stack(cctx) == FAIL)
7606 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607
Bram Moolenaara91a7132021-03-25 21:12:15 +01007608 // CMDMOD_REV must come before the jump
7609 generate_undo_cmdmods(cctx);
7610
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007611 // "where" is set when ":elseif", "else" or ":endif" is found
7612 scope->se_u.se_if.is_if_label = instr->ga_len;
7613 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007615
7616 return p;
7617}
7618
7619 static char_u *
7620compile_else(char_u *arg, cctx_T *cctx)
7621{
7622 char_u *p = arg;
7623 garray_T *instr = &cctx->ctx_instr;
7624 isn_T *isn;
7625 scope_T *scope = cctx->ctx_scope;
7626
7627 if (scope == NULL || scope->se_type != IF_SCOPE)
7628 {
7629 emsg(_(e_else_without_if));
7630 return NULL;
7631 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007632 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007633 if (!cctx->ctx_had_return)
7634 scope->se_u.se_if.is_had_return = FALSE;
7635 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007636
Bram Moolenaarced68a02021-01-24 17:53:47 +01007637#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007638 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007639 {
7640 if (cctx->ctx_skip == SKIP_NOT
7641 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7642 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007643 // the previous block was executed, do not count "else" for
7644 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007645 --instr->ga_len;
7646 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7647 {
7648 // the previous block was not executed, this one will, do count the
7649 // "else" for profiling
7650 cctx->ctx_skip = SKIP_NOT;
7651 generate_instr(cctx, ISN_PROF_END);
7652 generate_instr(cctx, ISN_PROF_START);
7653 cctx->ctx_skip = SKIP_YES;
7654 }
7655 }
7656#endif
7657
7658 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007659 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007660 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007661 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007662 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007663 if (!cctx->ctx_had_return
7664 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7665 JUMP_ALWAYS, cctx) == FAIL)
7666 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007667 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007668
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007669 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007670 {
7671 if (scope->se_u.se_if.is_if_label >= 0)
7672 {
7673 // previous "if" or "elseif" jumps here
7674 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7675 isn->isn_arg.jump.jump_where = instr->ga_len;
7676 scope->se_u.se_if.is_if_label = -1;
7677 }
7678 }
7679
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007680 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007681 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7682 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683
7684 return p;
7685}
7686
7687 static char_u *
7688compile_endif(char_u *arg, cctx_T *cctx)
7689{
7690 scope_T *scope = cctx->ctx_scope;
7691 ifscope_T *ifscope;
7692 garray_T *instr = &cctx->ctx_instr;
7693 isn_T *isn;
7694
Bram Moolenaarfa984412021-03-25 22:15:28 +01007695 if (misplaced_cmdmod(cctx))
7696 return NULL;
7697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007698 if (scope == NULL || scope->se_type != IF_SCOPE)
7699 {
7700 emsg(_(e_endif_without_if));
7701 return NULL;
7702 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007703 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007704 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007705 if (!cctx->ctx_had_return)
7706 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007707
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007708 if (scope->se_u.se_if.is_if_label >= 0)
7709 {
7710 // previous "if" or "elseif" jumps here
7711 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7712 isn->isn_arg.jump.jump_where = instr->ga_len;
7713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007715 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007716
7717#ifdef FEAT_PROFILE
7718 // even when skipping we count the endif as executed, unless the block it's
7719 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007720 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007721 && scope->se_skip_save != SKIP_YES)
7722 {
7723 cctx->ctx_skip = SKIP_NOT;
7724 generate_instr(cctx, ISN_PROF_START);
7725 }
7726#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007727 cctx->ctx_skip = scope->se_skip_save;
7728
7729 // If all the blocks end in :return and there is an :else then the
7730 // had_return flag is set.
7731 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007732
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007733 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007734 return arg;
7735}
7736
7737/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007738 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007739 *
7740 * Produces instructions:
7741 * PUSHNR -1
7742 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007743 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007744 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7745 * - if beyond end, jump to "end"
7746 * - otherwise get item from list and push it
7747 * STORE var Store item in "var"
7748 * ... body ...
7749 * JUMP top Jump back to repeat
7750 * end: DROP Drop the result of "expr"
7751 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007752 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7753 * UNPACK 2 Split item in 2
7754 * STORE var1 Store item in "var1"
7755 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756 */
7757 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007758compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007760 char_u *arg;
7761 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007762 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007763 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007764 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007765 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007766 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007767 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007768 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007769 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007770 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007772 lvar_T *loop_lvar; // loop iteration variable
7773 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007774 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007775 type_T *item_type = &t_any;
7776 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007777 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007778
Bram Moolenaar792f7862020-11-23 08:31:18 +01007779 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007780 if (p == NULL)
7781 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007782 if (var_count == 0)
7783 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007784 else
7785 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007786
7787 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007788 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007789 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7790 return NULL;
7791 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007792 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007793 if (*p == ':' && wp != p)
7794 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7795 else
7796 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007797 return NULL;
7798 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007799 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007800 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7801 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007802
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007803 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7804 // instruction.
7805 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7806 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7807 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007808 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007809 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007810 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7811 .isn_arg.debug.dbg_break_lnum;
7812 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007813
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814 scope = new_scope(cctx, FOR_SCOPE);
7815 if (scope == NULL)
7816 return NULL;
7817
Bram Moolenaar792f7862020-11-23 08:31:18 +01007818 // Reserve a variable to store the loop iteration counter and initialize it
7819 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007820 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7821 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007822 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007823 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007824 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007826 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007827 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007828
7829 // compile "expr", it remains on the stack until "endfor"
7830 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007831 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007832 {
7833 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007834 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007835 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007836 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007837
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007838 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007839 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007840 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007841 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007842 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007843 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007844 semsg(_(e_for_loop_on_str_not_supported),
7845 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007846 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007847 return NULL;
7848 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007849
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007850 if (vartype->tt_type == VAR_STRING)
7851 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007852 else if (vartype->tt_type == VAR_BLOB)
7853 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007854 else if (vartype->tt_type == VAR_LIST
7855 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007856 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007857 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007858 item_type = vartype->tt_member;
7859 else if (vartype->tt_member->tt_type == VAR_LIST
7860 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007861 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007862 item_type = vartype->tt_member->tt_member;
7863 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007864
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007865 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007866 generate_undo_cmdmods(cctx);
7867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007869 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007870
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007871 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007872
Bram Moolenaar792f7862020-11-23 08:31:18 +01007873 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007874 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007875 {
7876 generate_UNPACK(cctx, var_count, semicolon);
7877 arg = skipwhite(arg + 1); // skip white after '['
7878
7879 // the list item is replaced by a number of items
7880 if (ga_grow(stack, var_count - 1) == FAIL)
7881 {
7882 drop_scope(cctx);
7883 return NULL;
7884 }
7885 --stack->ga_len;
7886 for (idx = 0; idx < var_count; ++idx)
7887 {
7888 ((type_T **)stack->ga_data)[stack->ga_len] =
7889 (semicolon && idx == 0) ? vartype : item_type;
7890 ++stack->ga_len;
7891 }
7892 }
7893
7894 for (idx = 0; idx < var_count; ++idx)
7895 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007896 assign_dest_T dest = dest_local;
7897 int opt_flags = 0;
7898 int vimvaridx = -1;
7899 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007900 type_T *lhs_type = &t_any;
7901 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007902
7903 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007904 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007905 name = vim_strnsave(arg, varlen);
7906 if (name == NULL)
7907 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007908 if (*p == ':')
7909 {
7910 p = skipwhite(p + 1);
7911 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7912 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007913
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007914 // TODO: script var not supported?
7915 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7916 &vimvaridx, &type, cctx) == FAIL)
7917 goto failed;
7918 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007919 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007920 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7921 0, 0, type, name) == FAIL)
7922 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007923 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007924 else if (varlen == 1 && *arg == '_')
7925 {
7926 // Assigning to "_": drop the value.
7927 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7928 goto failed;
7929 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007930 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007931 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007932 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007933 {
7934 semsg(_(e_variable_already_declared), arg);
7935 goto failed;
7936 }
7937
Bram Moolenaarea870692020-12-02 14:24:30 +01007938 if (STRNCMP(name, "s:", 2) == 0)
7939 {
7940 semsg(_(e_cannot_declare_script_variable_in_function), name);
7941 goto failed;
7942 }
7943
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007944 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02007945 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007946 where.wt_variable = TRUE;
7947 if (lhs_type == &t_any)
7948 lhs_type = item_type;
7949 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02007950 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02007951 ? need_type(item_type, lhs_type,
7952 -1, 0, cctx, FALSE, FALSE)
7953 : check_type(lhs_type, item_type, TRUE, where))
7954 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007955 goto failed;
7956 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007957 if (var_lvar == NULL)
7958 // out of memory or used as an argument
7959 goto failed;
7960
7961 if (semicolon && idx == var_count - 1)
7962 var_lvar->lv_type = vartype;
7963 else
7964 var_lvar->lv_type = item_type;
7965 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7966 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007967
7968 if (*p == ',' || *p == ';')
7969 ++p;
7970 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007971 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007972 }
7973
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007974 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007975 {
7976 int save_prev_lnum = cctx->ctx_prev_lnum;
7977
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007978 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007979 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
7980 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007981 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007982 cctx->ctx_prev_lnum = save_prev_lnum;
7983 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007984
Bram Moolenaar792f7862020-11-23 08:31:18 +01007985 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007986
7987failed:
7988 vim_free(name);
7989 drop_scope(cctx);
7990 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007991}
7992
7993/*
7994 * compile "endfor"
7995 */
7996 static char_u *
7997compile_endfor(char_u *arg, cctx_T *cctx)
7998{
7999 garray_T *instr = &cctx->ctx_instr;
8000 scope_T *scope = cctx->ctx_scope;
8001 forscope_T *forscope;
8002 isn_T *isn;
8003
Bram Moolenaarfa984412021-03-25 22:15:28 +01008004 if (misplaced_cmdmod(cctx))
8005 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008007 if (scope == NULL || scope->se_type != FOR_SCOPE)
8008 {
8009 emsg(_(e_for));
8010 return NULL;
8011 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008012 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008013 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008014 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008015
8016 // At end of ":for" scope jump back to the FOR instruction.
8017 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8018
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008019 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008020 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8021 isn->isn_arg.forloop.for_end = instr->ga_len;
8022
8023 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008024 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008025
8026 // Below the ":for" scope drop the "expr" list from the stack.
8027 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8028 return NULL;
8029
8030 vim_free(scope);
8031
8032 return arg;
8033}
8034
8035/*
8036 * compile "while expr"
8037 *
8038 * Produces instructions:
8039 * top: EVAL expr Push result of "expr"
8040 * JUMP_IF_FALSE end jump if false
8041 * ... body ...
8042 * JUMP top Jump back to repeat
8043 * end:
8044 *
8045 */
8046 static char_u *
8047compile_while(char_u *arg, cctx_T *cctx)
8048{
8049 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008050 scope_T *scope;
8051
8052 scope = new_scope(cctx, WHILE_SCOPE);
8053 if (scope == NULL)
8054 return NULL;
8055
Bram Moolenaara91a7132021-03-25 21:12:15 +01008056 // "endwhile" jumps back here, one before when profiling or using cmdmods
8057 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008058
8059 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008060 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008061 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008062 if (!ends_excmd2(arg, skipwhite(p)))
8063 {
8064 semsg(_(e_trailing_arg), p);
8065 return NULL;
8066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008067
Bram Moolenaar13106602020-10-04 16:06:05 +02008068 if (bool_on_stack(cctx) == FAIL)
8069 return FAIL;
8070
Bram Moolenaara91a7132021-03-25 21:12:15 +01008071 // CMDMOD_REV must come before the jump
8072 generate_undo_cmdmods(cctx);
8073
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008074 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008075 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008076 JUMP_IF_FALSE, cctx) == FAIL)
8077 return FAIL;
8078
8079 return p;
8080}
8081
8082/*
8083 * compile "endwhile"
8084 */
8085 static char_u *
8086compile_endwhile(char_u *arg, cctx_T *cctx)
8087{
8088 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008089 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008090
Bram Moolenaarfa984412021-03-25 22:15:28 +01008091 if (misplaced_cmdmod(cctx))
8092 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008093 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8094 {
8095 emsg(_(e_while));
8096 return NULL;
8097 }
8098 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008099 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008100
Bram Moolenaarf002a412021-01-24 13:34:18 +01008101#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008102 // count the endwhile before jumping
8103 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008104#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008106 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008107 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008108
8109 // Fill in the "end" label in the WHILE statement so it can jump here.
8110 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008111 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8112 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008113
8114 vim_free(scope);
8115
8116 return arg;
8117}
8118
8119/*
8120 * compile "continue"
8121 */
8122 static char_u *
8123compile_continue(char_u *arg, cctx_T *cctx)
8124{
8125 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008126 int try_scopes = 0;
8127 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008128
8129 for (;;)
8130 {
8131 if (scope == NULL)
8132 {
8133 emsg(_(e_continue));
8134 return NULL;
8135 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008136 if (scope->se_type == FOR_SCOPE)
8137 {
8138 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008139 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008140 }
8141 if (scope->se_type == WHILE_SCOPE)
8142 {
8143 loop_label = scope->se_u.se_while.ws_top_label;
8144 break;
8145 }
8146 if (scope->se_type == TRY_SCOPE)
8147 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008148 scope = scope->se_outer;
8149 }
8150
Bram Moolenaarc150c092021-02-13 15:02:46 +01008151 if (try_scopes > 0)
8152 // Inside one or more try/catch blocks we first need to jump to the
8153 // "finally" or "endtry" to cleanup.
8154 generate_TRYCONT(cctx, try_scopes, loop_label);
8155 else
8156 // Jump back to the FOR or WHILE instruction.
8157 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008159 return arg;
8160}
8161
8162/*
8163 * compile "break"
8164 */
8165 static char_u *
8166compile_break(char_u *arg, cctx_T *cctx)
8167{
8168 scope_T *scope = cctx->ctx_scope;
8169 endlabel_T **el;
8170
8171 for (;;)
8172 {
8173 if (scope == NULL)
8174 {
8175 emsg(_(e_break));
8176 return NULL;
8177 }
8178 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8179 break;
8180 scope = scope->se_outer;
8181 }
8182
8183 // Jump to the end of the FOR or WHILE loop.
8184 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008185 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008186 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008187 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8189 return FAIL;
8190
8191 return arg;
8192}
8193
8194/*
8195 * compile "{" start of block
8196 */
8197 static char_u *
8198compile_block(char_u *arg, cctx_T *cctx)
8199{
8200 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8201 return NULL;
8202 return skipwhite(arg + 1);
8203}
8204
8205/*
8206 * compile end of block: drop one scope
8207 */
8208 static void
8209compile_endblock(cctx_T *cctx)
8210{
8211 scope_T *scope = cctx->ctx_scope;
8212
8213 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008214 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008215 vim_free(scope);
8216}
8217
8218/*
8219 * compile "try"
8220 * Creates a new scope for the try-endtry, pointing to the first catch and
8221 * finally.
8222 * Creates another scope for the "try" block itself.
8223 * TRY instruction sets up exception handling at runtime.
8224 *
8225 * "try"
8226 * TRY -> catch1, -> finally push trystack entry
8227 * ... try block
8228 * "throw {exception}"
8229 * EVAL {exception}
8230 * THROW create exception
8231 * ... try block
8232 * " catch {expr}"
8233 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008234 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008235 * EVAL {expr}
8236 * MATCH
8237 * JUMP nomatch -> catch2
8238 * CATCH remove exception
8239 * ... catch block
8240 * " catch"
8241 * JUMP -> finally
8242 * catch2: CATCH remove exception
8243 * ... catch block
8244 * " finally"
8245 * finally:
8246 * ... finally block
8247 * " endtry"
8248 * ENDTRY pop trystack entry, may rethrow
8249 */
8250 static char_u *
8251compile_try(char_u *arg, cctx_T *cctx)
8252{
8253 garray_T *instr = &cctx->ctx_instr;
8254 scope_T *try_scope;
8255 scope_T *scope;
8256
Bram Moolenaarfa984412021-03-25 22:15:28 +01008257 if (misplaced_cmdmod(cctx))
8258 return NULL;
8259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008260 // scope that holds the jumps that go to catch/finally/endtry
8261 try_scope = new_scope(cctx, TRY_SCOPE);
8262 if (try_scope == NULL)
8263 return NULL;
8264
Bram Moolenaar69f70502021-01-01 16:10:46 +01008265 if (cctx->ctx_skip != SKIP_YES)
8266 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008267 isn_T *isn;
8268
8269 // "try_catch" is set when the first ":catch" is found or when no catch
8270 // is found and ":finally" is found.
8271 // "try_finally" is set when ":finally" is found
8272 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008273 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008274 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8275 return NULL;
8276 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8277 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008278 return NULL;
8279 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008280
8281 // scope for the try block itself
8282 scope = new_scope(cctx, BLOCK_SCOPE);
8283 if (scope == NULL)
8284 return NULL;
8285
8286 return arg;
8287}
8288
8289/*
8290 * compile "catch {expr}"
8291 */
8292 static char_u *
8293compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8294{
8295 scope_T *scope = cctx->ctx_scope;
8296 garray_T *instr = &cctx->ctx_instr;
8297 char_u *p;
8298 isn_T *isn;
8299
Bram Moolenaarfa984412021-03-25 22:15:28 +01008300 if (misplaced_cmdmod(cctx))
8301 return NULL;
8302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008303 // end block scope from :try or :catch
8304 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8305 compile_endblock(cctx);
8306 scope = cctx->ctx_scope;
8307
8308 // Error if not in a :try scope
8309 if (scope == NULL || scope->se_type != TRY_SCOPE)
8310 {
8311 emsg(_(e_catch));
8312 return NULL;
8313 }
8314
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008315 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008316 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008317 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008318 return NULL;
8319 }
8320
Bram Moolenaar69f70502021-01-01 16:10:46 +01008321 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008322 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008323#ifdef FEAT_PROFILE
8324 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008325 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008326 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008327 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008328 .isn_type == ISN_PROF_START)
8329 --instr->ga_len;
8330#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008331 // Jump from end of previous block to :finally or :endtry
8332 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8333 JUMP_ALWAYS, cctx) == FAIL)
8334 return NULL;
8335
8336 // End :try or :catch scope: set value in ISN_TRY instruction
8337 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008338 if (isn->isn_arg.try.try_ref->try_catch == 0)
8339 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008340 if (scope->se_u.se_try.ts_catch_label != 0)
8341 {
8342 // Previous catch without match jumps here
8343 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8344 isn->isn_arg.jump.jump_where = instr->ga_len;
8345 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008346#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008347 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008348 {
8349 // a "throw" that jumps here needs to be counted
8350 generate_instr(cctx, ISN_PROF_END);
8351 // the "catch" is also counted
8352 generate_instr(cctx, ISN_PROF_START);
8353 }
8354#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008355 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008356 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008357 }
8358
8359 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008360 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008361 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008362 scope->se_u.se_try.ts_caught_all = TRUE;
8363 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008364 }
8365 else
8366 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008367 char_u *end;
8368 char_u *pat;
8369 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008370 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008371 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008373 // Push v:exception, push {expr} and MATCH
8374 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8375
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008376 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008377 if (*end != *p)
8378 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008379 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008380 vim_free(tofree);
8381 return FAIL;
8382 }
8383 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008384 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008385 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008386 len = (int)(end - tofree);
8387 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008388 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008389 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008390 if (pat == NULL)
8391 return FAIL;
8392 if (generate_PUSHS(cctx, pat) == FAIL)
8393 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008395 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8396 return NULL;
8397
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008398 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008399 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8400 return NULL;
8401 }
8402
Bram Moolenaar69f70502021-01-01 16:10:46 +01008403 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008404 return NULL;
8405
8406 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8407 return NULL;
8408 return p;
8409}
8410
8411 static char_u *
8412compile_finally(char_u *arg, cctx_T *cctx)
8413{
8414 scope_T *scope = cctx->ctx_scope;
8415 garray_T *instr = &cctx->ctx_instr;
8416 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008417 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418
Bram Moolenaarfa984412021-03-25 22:15:28 +01008419 if (misplaced_cmdmod(cctx))
8420 return NULL;
8421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008422 // end block scope from :try or :catch
8423 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8424 compile_endblock(cctx);
8425 scope = cctx->ctx_scope;
8426
8427 // Error if not in a :try scope
8428 if (scope == NULL || scope->se_type != TRY_SCOPE)
8429 {
8430 emsg(_(e_finally));
8431 return NULL;
8432 }
8433
8434 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008435 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008436 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008437 {
8438 emsg(_(e_finally_dup));
8439 return NULL;
8440 }
8441
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008442 this_instr = instr->ga_len;
8443#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008444 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008445 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008446 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008447 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008448 // jump to the profile start of the "finally"
8449 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008450
8451 // jump to the profile end above it
8452 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8453 .isn_type == ISN_PROF_END)
8454 --this_instr;
8455 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008456#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008457
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008458 // Fill in the "end" label in jumps at the end of the blocks.
8459 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8460 this_instr, cctx);
8461
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008462 // If there is no :catch then an exception jumps to :finally.
8463 if (isn->isn_arg.try.try_ref->try_catch == 0)
8464 isn->isn_arg.try.try_ref->try_catch = this_instr;
8465 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008466 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008467 {
8468 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008469 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008470 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008471 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008472 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008473 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8474 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008476 // TODO: set index in ts_finally_label jumps
8477
8478 return arg;
8479}
8480
8481 static char_u *
8482compile_endtry(char_u *arg, cctx_T *cctx)
8483{
8484 scope_T *scope = cctx->ctx_scope;
8485 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008486 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008487
Bram Moolenaarfa984412021-03-25 22:15:28 +01008488 if (misplaced_cmdmod(cctx))
8489 return NULL;
8490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008491 // end block scope from :catch or :finally
8492 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8493 compile_endblock(cctx);
8494 scope = cctx->ctx_scope;
8495
8496 // Error if not in a :try scope
8497 if (scope == NULL || scope->se_type != TRY_SCOPE)
8498 {
8499 if (scope == NULL)
8500 emsg(_(e_no_endtry));
8501 else if (scope->se_type == WHILE_SCOPE)
8502 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008503 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008504 emsg(_(e_endfor));
8505 else
8506 emsg(_(e_endif));
8507 return NULL;
8508 }
8509
Bram Moolenaarc150c092021-02-13 15:02:46 +01008510 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008511 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008512 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008513 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8514 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008515 {
8516 emsg(_(e_missing_catch_or_finally));
8517 return NULL;
8518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008519
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008520#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008521 if (cctx->ctx_compile_type == CT_PROFILE
8522 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008523 .isn_type == ISN_PROF_START)
8524 // move the profile start after "endtry" so that it's not counted when
8525 // the exception is rethrown.
8526 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008527#endif
8528
Bram Moolenaar69f70502021-01-01 16:10:46 +01008529 // Fill in the "end" label in jumps at the end of the blocks, if not
8530 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008531 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8532 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008533
Bram Moolenaar69f70502021-01-01 16:10:46 +01008534 if (scope->se_u.se_try.ts_catch_label != 0)
8535 {
8536 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008537 isn_T *isn = ((isn_T *)instr->ga_data)
8538 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008539 isn->isn_arg.jump.jump_where = instr->ga_len;
8540 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008541 }
8542
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008543 compile_endblock(cctx);
8544
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008545 if (cctx->ctx_skip != SKIP_YES)
8546 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008547 // End :catch or :finally scope: set instruction index in ISN_TRY
8548 // instruction
8549 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008550 if (cctx->ctx_skip != SKIP_YES
8551 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8552 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008553#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008554 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008555 generate_instr(cctx, ISN_PROF_START);
8556#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008557 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008558 return arg;
8559}
8560
8561/*
8562 * compile "throw {expr}"
8563 */
8564 static char_u *
8565compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8566{
8567 char_u *p = skipwhite(arg);
8568
Bram Moolenaara5565e42020-05-09 15:44:01 +02008569 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008570 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008571 if (cctx->ctx_skip == SKIP_YES)
8572 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008573 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008574 return NULL;
8575 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8576 return NULL;
8577
8578 return p;
8579}
8580
Bram Moolenaarc3235272021-07-10 19:42:03 +02008581 static char_u *
8582compile_eval(char_u *arg, cctx_T *cctx)
8583{
8584 char_u *p = arg;
8585 int name_only;
8586 char_u *alias;
8587 long lnum = SOURCING_LNUM;
8588
8589 // find_ex_command() will consider a variable name an expression, assuming
8590 // that something follows on the next line. Check that something actually
8591 // follows, otherwise it's probably a misplaced command.
8592 get_name_len(&p, &alias, FALSE, FALSE);
8593 name_only = ends_excmd2(arg, skipwhite(p));
8594 vim_free(alias);
8595
8596 p = arg;
8597 if (compile_expr0(&p, cctx) == FAIL)
8598 return NULL;
8599
8600 if (name_only && lnum == SOURCING_LNUM)
8601 {
8602 semsg(_(e_expression_without_effect_str), arg);
8603 return NULL;
8604 }
8605
8606 // drop the result
8607 generate_instr_drop(cctx, ISN_DROP, 1);
8608
8609 return skipwhite(p);
8610}
8611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008612/*
8613 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008614 * compile "echomsg expr"
8615 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008616 * compile "execute expr"
8617 */
8618 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008619compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008620{
8621 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008622 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008623 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008624 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008625 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008626 garray_T *stack = &cctx->ctx_type_stack;
8627 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008628
8629 for (;;)
8630 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008631 if (ends_excmd2(prev, p))
8632 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008633 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008634 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008635 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008636
8637 if (cctx->ctx_skip != SKIP_YES)
8638 {
8639 // check for non-void type
8640 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8641 if (type->tt_type == VAR_VOID)
8642 {
8643 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8644 return NULL;
8645 }
8646 }
8647
Bram Moolenaarad39c092020-02-26 18:23:43 +01008648 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008649 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008650 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008651 }
8652
Bram Moolenaare4984292020-12-13 14:19:25 +01008653 if (count > 0)
8654 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008655 long save_lnum = cctx->ctx_lnum;
8656
8657 // Use the line number where the command started.
8658 cctx->ctx_lnum = start_ctx_lnum;
8659
Bram Moolenaare4984292020-12-13 14:19:25 +01008660 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8661 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8662 else if (cmdidx == CMD_execute)
8663 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8664 else if (cmdidx == CMD_echomsg)
8665 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8666 else
8667 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008668
8669 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008671 return p;
8672}
8673
8674/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008675 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008676 * instruction to compute it and return OK.
8677 * Otherwise return FAIL, the caller must deal with any range.
8678 */
8679 static int
8680compile_variable_range(exarg_T *eap, cctx_T *cctx)
8681{
8682 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8683 char_u *p = skipdigits(eap->cmd);
8684
8685 if (p == range_end)
8686 return FAIL;
8687 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8688}
8689
8690/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008691 * :put r
8692 * :put ={expr}
8693 */
8694 static char_u *
8695compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8696{
8697 char_u *line = arg;
8698 linenr_T lnum;
8699 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008700 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008701
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008702 eap->regname = *line;
8703
8704 if (eap->regname == '=')
8705 {
8706 char_u *p = line + 1;
8707
8708 if (compile_expr0(&p, cctx) == FAIL)
8709 return NULL;
8710 line = p;
8711 }
8712 else if (eap->regname != NUL)
8713 ++line;
8714
Bram Moolenaar08597872020-12-10 19:43:40 +01008715 if (compile_variable_range(eap, cctx) == OK)
8716 {
8717 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8718 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008719 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008720 {
8721 // Either no range or a number.
8722 // "errormsg" will not be set because the range is ADDR_LINES.
8723 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008724 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008725 return NULL;
8726 if (eap->addr_count == 0)
8727 lnum = -1;
8728 else
8729 lnum = eap->line2;
8730 if (above)
8731 --lnum;
8732 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008733
8734 generate_PUT(cctx, eap->regname, lnum);
8735 return line;
8736}
8737
8738/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008739 * A command that is not compiled, execute with legacy code.
8740 */
8741 static char_u *
8742compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8743{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008744 char_u *p;
8745 int has_expr = FALSE;
8746 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008747
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008748 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008749 goto theend;
8750
Bram Moolenaare729ce22021-06-06 21:38:09 +02008751 // If there was a prececing command modifier, drop it and include it in the
8752 // EXEC command.
8753 if (cctx->ctx_has_cmdmod)
8754 {
8755 garray_T *instr = &cctx->ctx_instr;
8756 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8757
8758 if (isn->isn_type == ISN_CMDMOD)
8759 {
8760 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8761 ->cmod_filter_regmatch.regprog);
8762 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8763 --instr->ga_len;
8764 cctx->ctx_has_cmdmod = FALSE;
8765 }
8766 }
8767
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008768 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008769 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008770 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008771 int usefilter = FALSE;
8772
8773 has_expr = argt & (EX_XFILE | EX_EXPAND);
8774
8775 // If the command can be followed by a bar, find the bar and truncate
8776 // it, so that the following command can be compiled.
8777 // The '|' is overwritten with a NUL, it is put back below.
8778 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8779 && *eap->arg == '!')
8780 // :w !filter or :r !filter or :r! filter
8781 usefilter = TRUE;
8782 if ((argt & EX_TRLBAR) && !usefilter)
8783 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008784 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008785 separate_nextcmd(eap);
8786 if (eap->nextcmd != NULL)
8787 nextcmd = eap->nextcmd;
8788 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008789 else if (eap->cmdidx == CMD_wincmd)
8790 {
8791 p = eap->arg;
8792 if (*p != NUL)
8793 ++p;
8794 if (*p == 'g' || *p == Ctrl_G)
8795 ++p;
8796 p = skipwhite(p);
8797 if (*p == '|')
8798 {
8799 *p = NUL;
8800 nextcmd = p + 1;
8801 }
8802 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008803 }
8804
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008805 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8806 {
8807 // expand filename in "syntax include [@group] filename"
8808 has_expr = TRUE;
8809 eap->arg = skipwhite(eap->arg + 7);
8810 if (*eap->arg == '@')
8811 eap->arg = skiptowhite(eap->arg);
8812 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008813
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008814 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8815 && STRLEN(eap->arg) > 4)
8816 {
8817 int delim = *eap->arg;
8818
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008819 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008820 if (*p == delim)
8821 {
8822 eap->arg = p + 1;
8823 has_expr = TRUE;
8824 }
8825 }
8826
Bram Moolenaarecac5912021-01-05 19:23:28 +01008827 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8828 {
8829 // TODO: should only expand when appropriate for the command
8830 eap->arg = skiptowhite(eap->arg);
8831 has_expr = TRUE;
8832 }
8833
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008834 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008835 {
8836 int count = 0;
8837 char_u *start = skipwhite(line);
8838
8839 // :cmd xxx`=expr1`yyy`=expr2`zzz
8840 // PUSHS ":cmd xxx"
8841 // eval expr1
8842 // PUSHS "yyy"
8843 // eval expr2
8844 // PUSHS "zzz"
8845 // EXECCONCAT 5
8846 for (;;)
8847 {
8848 if (p > start)
8849 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008850 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008851 ++count;
8852 }
8853 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008854 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008855 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008856 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008857 ++count;
8858 p = skipwhite(p);
8859 if (*p != '`')
8860 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008861 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008862 return NULL;
8863 }
8864 start = p + 1;
8865
8866 p = (char_u *)strstr((char *)start, "`=");
8867 if (p == NULL)
8868 {
8869 if (*skipwhite(start) != NUL)
8870 {
8871 generate_PUSHS(cctx, vim_strsave(start));
8872 ++count;
8873 }
8874 break;
8875 }
8876 }
8877 generate_EXECCONCAT(cctx, count);
8878 }
8879 else
8880 generate_EXEC(cctx, line);
8881
8882theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008883 if (*nextcmd != NUL)
8884 {
8885 // the parser expects a pointer to the bar, put it back
8886 --nextcmd;
8887 *nextcmd = '|';
8888 }
8889
8890 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008891}
8892
Bram Moolenaar20677332021-06-06 17:02:53 +02008893/*
8894 * A script command with heredoc, e.g.
8895 * ruby << EOF
8896 * command
8897 * EOF
8898 * Has been turned into one long line with NL characters by
8899 * get_function_body():
8900 * ruby << EOF<NL> command<NL>EOF
8901 */
8902 static char_u *
8903compile_script(char_u *line, cctx_T *cctx)
8904{
8905 if (cctx->ctx_skip != SKIP_YES)
8906 {
8907 isn_T *isn;
8908
8909 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8910 return NULL;
8911 isn->isn_arg.string = vim_strsave(line);
8912 }
8913 return (char_u *)"";
8914}
8915
Bram Moolenaar8238f082021-04-20 21:10:48 +02008916
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008917/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008918 * :s/pat/repl/
8919 */
8920 static char_u *
8921compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8922{
8923 char_u *cmd = eap->arg;
8924 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8925
8926 if (expr != NULL)
8927 {
8928 int delimiter = *cmd++;
8929
8930 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008931 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008932 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8933 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008934 garray_T save_ga = cctx->ctx_instr;
8935 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008936 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008937 int trailing_error;
8938 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008939 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008940 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008941
8942 cmd += 3;
8943 end = skip_substitute(cmd, delimiter);
8944
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008945 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008946 // labels are correct.
8947 cctx->ctx_instr.ga_len = 0;
8948 cctx->ctx_instr.ga_maxlen = 0;
8949 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008950 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008951 if (end[-1] == NUL)
8952 end[-1] = delimiter;
8953 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008954 trailing_error = *cmd != delimiter && *cmd != NUL;
8955
Bram Moolenaar169502f2021-04-21 12:19:35 +02008956 if (expr_res == FAIL || trailing_error
8957 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008958 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008959 if (trailing_error)
8960 semsg(_(e_trailing_arg), cmd);
8961 clear_instr_ga(&cctx->ctx_instr);
8962 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008963 return NULL;
8964 }
8965
Bram Moolenaar8238f082021-04-20 21:10:48 +02008966 // Move the generated instructions into the ISN_SUBSTITUTE
8967 // instructions, then restore the list of instructions before
8968 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008969 instr_count = cctx->ctx_instr.ga_len;
8970 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008971 instr[instr_count].isn_type = ISN_FINISH;
8972
8973 cctx->ctx_instr = save_ga;
8974 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8975 {
8976 int idx;
8977
8978 for (idx = 0; idx < instr_count; ++idx)
8979 delete_instr(instr + idx);
8980 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008981 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008982 }
8983 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8984 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008985
8986 // skip over flags
8987 if (*end == '&')
8988 ++end;
8989 while (ASCII_ISALPHA(*end) || *end == '#')
8990 ++end;
8991 return end;
8992 }
8993 }
8994
8995 return compile_exec(arg, eap, cctx);
8996}
8997
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008998 static char_u *
8999compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9000{
9001 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009002 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009003
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009004 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009005 {
9006 if (STRNCMP(arg, "END", 3) == 0)
9007 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009008 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009009 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009010 // First load the current variable value.
9011 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9012 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009013 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009014 }
9015
9016 // Gets the redirected text and put it on the stack, then store it
9017 // in the variable.
9018 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9019
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009020 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009021 generate_instr_drop(cctx, ISN_CONCAT, 1);
9022
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009023 if (lhs->lhs_has_index)
9024 {
9025 // Use the info in "lhs" to store the value at the index in the
9026 // list or dict.
9027 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9028 &t_string, cctx) == FAIL)
9029 return NULL;
9030 }
9031 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009032 return NULL;
9033
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009034 VIM_CLEAR(lhs->lhs_name);
9035 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009036 return arg + 3;
9037 }
9038 emsg(_(e_cannot_nest_redir));
9039 return NULL;
9040 }
9041
9042 if (arg[0] == '=' && arg[1] == '>')
9043 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009044 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009045
9046 // redirect to a variable is compiled
9047 arg += 2;
9048 if (*arg == '>')
9049 {
9050 ++arg;
9051 append = TRUE;
9052 }
9053 arg = skipwhite(arg);
9054
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009055 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009056 FALSE, FALSE, 1, cctx) == FAIL)
9057 return NULL;
9058 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009059 lhs->lhs_append = append;
9060 if (lhs->lhs_has_index)
9061 {
9062 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9063 if (lhs->lhs_whole == NULL)
9064 return NULL;
9065 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009066
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009067 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009068 }
9069
9070 // other redirects are handled like at script level
9071 return compile_exec(line, eap, cctx);
9072}
9073
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009074#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009075 static char_u *
9076compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9077{
9078 isn_T *isn;
9079 char_u *p;
9080
9081 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9082 if (isn == NULL)
9083 return NULL;
9084 isn->isn_arg.number = eap->cmdidx;
9085
9086 p = eap->arg;
9087 if (compile_expr0(&p, cctx) == FAIL)
9088 return NULL;
9089
9090 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9091 if (isn == NULL)
9092 return NULL;
9093 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9094 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9095 return NULL;
9096 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9097 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9098 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9099
9100 return p;
9101}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009102#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009103
Bram Moolenaar4c137212021-04-19 16:48:48 +02009104/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009105 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009106 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009107 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009108 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009109add_def_function(ufunc_T *ufunc)
9110{
9111 dfunc_T *dfunc;
9112
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009113 if (def_functions.ga_len == 0)
9114 {
9115 // The first position is not used, so that a zero uf_dfunc_idx means it
9116 // wasn't set.
9117 if (ga_grow(&def_functions, 1) == FAIL)
9118 return FAIL;
9119 ++def_functions.ga_len;
9120 }
9121
Bram Moolenaar09689a02020-05-09 22:50:08 +02009122 // Add the function to "def_functions".
9123 if (ga_grow(&def_functions, 1) == FAIL)
9124 return FAIL;
9125 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9126 CLEAR_POINTER(dfunc);
9127 dfunc->df_idx = def_functions.ga_len;
9128 ufunc->uf_dfunc_idx = dfunc->df_idx;
9129 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009130 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009131 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009132 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009133 ++def_functions.ga_len;
9134 return OK;
9135}
9136
9137/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009138 * After ex_function() has collected all the function lines: parse and compile
9139 * the lines into instructions.
9140 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009141 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9142 * the return statement (used for lambda). When uf_ret_type is already set
9143 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009144 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009145 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009146 * This can be used recursively through compile_lambda(), which may reallocate
9147 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009148 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009149 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009150 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009151compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009152 ufunc_T *ufunc,
9153 int check_return_type,
9154 compiletype_T compile_type,
9155 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009156{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009157 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009158 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009159 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009160 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009161 cctx_T cctx;
9162 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009163 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009164 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009165 int ret = FAIL;
9166 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009167 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009168 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009169 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009170 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009171#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009172 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009173#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009174 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009175
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009176 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009177 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009178 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009179 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009180 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9181 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009182 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009183
9184 switch (compile_type)
9185 {
9186 case CT_PROFILE:
9187#ifdef FEAT_PROFILE
9188 instr_dest = dfunc->df_instr_prof; break;
9189#endif
9190 case CT_NONE: instr_dest = dfunc->df_instr; break;
9191 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9192 }
9193 if (instr_dest != NULL)
9194 // Was compiled in this mode before: Free old instructions.
9195 delete_def_function_contents(dfunc, FALSE);
9196 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009197 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009198 else
9199 {
9200 if (add_def_function(ufunc) == FAIL)
9201 return FAIL;
9202 new_def_function = TRUE;
9203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009204
Bram Moolenaar985116a2020-07-12 17:31:09 +02009205 ufunc->uf_def_status = UF_COMPILING;
9206
Bram Moolenaara80faa82020-04-12 19:37:17 +02009207 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009208
Bram Moolenaare99d4222021-06-13 14:01:26 +02009209 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009210 cctx.ctx_ufunc = ufunc;
9211 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009212 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009213 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9214 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9215 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9216 cctx.ctx_type_list = &ufunc->uf_type_list;
9217 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9218 instr = &cctx.ctx_instr;
9219
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009220 // Set the context to the function, it may be compiled when called from
9221 // another script. Set the script version to the most modern one.
9222 // The line number will be set in next_line_from_context().
9223 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009224 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9225
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009226 // Don't use the flag from ":legacy" here.
9227 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9228
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009229 // Make sure error messages are OK.
9230 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9231 if (do_estack_push)
9232 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009233 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009234
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009235 if (ufunc->uf_def_args.ga_len > 0)
9236 {
9237 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009238 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009239 int i;
9240 char_u *arg;
9241 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009242 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009243
9244 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009245 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009246 for (i = 0; i < count; ++i)
9247 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009248 garray_T *stack = &cctx.ctx_type_stack;
9249 type_T *val_type;
9250 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009251 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009252 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009253 int jump_instr_idx = instr->ga_len;
9254 isn_T *isn;
9255
9256 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9257 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9258 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009259
9260 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009261 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009262
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009263 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009264 r = compile_expr0(&arg, &cctx);
9265
Bram Moolenaar12bce952021-03-11 20:04:04 +01009266 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009267 goto erret;
9268
9269 // If no type specified use the type of the default value.
9270 // Otherwise check that the default value type matches the
9271 // specified type.
9272 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009273 where.wt_index = arg_idx + 1;
9274 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009275 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009276 {
9277 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009278 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009279 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009280 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009281 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009282 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009283
9284 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009285 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009286
9287 // set instruction index in JUMP_IF_ARG_SET to here
9288 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9289 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009290 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009291
9292 if (did_set_arg_type)
9293 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009294 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009295 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009296
9297 /*
9298 * Loop over all the lines of the function and generate instructions.
9299 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009300 for (;;)
9301 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009302 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009303 int starts_with_colon = FALSE;
9304 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009305 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009306
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009307 // Bail out on the first error to avoid a flood of errors and report
9308 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009309 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009310 goto erret;
9311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009312 if (line != NULL && *line == '|')
9313 // the line continues after a '|'
9314 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009315 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009316 && !(*line == '#' && (line == cctx.ctx_line_start
9317 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009318 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009319 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009320 goto erret;
9321 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009322 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9323 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009324 else
9325 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009326 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009327 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009328 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009329 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009330#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009331 if (cctx.ctx_skip != SKIP_YES)
9332 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009333#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009334 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009335 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009336 // Make a copy, splitting off nextcmd and removing trailing spaces
9337 // may change it.
9338 if (line != NULL)
9339 {
9340 line = vim_strsave(line);
9341 vim_free(line_to_free);
9342 line_to_free = line;
9343 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009344 }
9345
Bram Moolenaara80faa82020-04-12 19:37:17 +02009346 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009347 ea.cmdlinep = &line;
9348 ea.cmd = skipwhite(line);
9349
Bram Moolenaarb2049902021-01-24 12:53:53 +01009350 if (*ea.cmd == '#')
9351 {
9352 // "#" starts a comment
9353 line = (char_u *)"";
9354 continue;
9355 }
9356
Bram Moolenaarf002a412021-01-24 13:34:18 +01009357#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009358 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9359 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009360 {
9361 may_generate_prof_end(&cctx, prof_lnum);
9362
9363 prof_lnum = cctx.ctx_lnum;
9364 generate_instr(&cctx, ISN_PROF_START);
9365 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009366#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009367 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9368 && cctx.ctx_skip != SKIP_YES)
9369 {
9370 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009371 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009372 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009373 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009374
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009375 // Some things can be recognized by the first character.
9376 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009377 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009378 case '}':
9379 {
9380 // "}" ends a block scope
9381 scopetype_T stype = cctx.ctx_scope == NULL
9382 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009383
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009384 if (stype == BLOCK_SCOPE)
9385 {
9386 compile_endblock(&cctx);
9387 line = ea.cmd;
9388 }
9389 else
9390 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009391 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009392 goto erret;
9393 }
9394 if (line != NULL)
9395 line = skipwhite(ea.cmd + 1);
9396 continue;
9397 }
9398
9399 case '{':
9400 // "{" starts a block scope
9401 // "{'a': 1}->func() is something else
9402 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9403 {
9404 line = compile_block(ea.cmd, &cctx);
9405 continue;
9406 }
9407 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009408 }
9409
9410 /*
9411 * COMMAND MODIFIERS
9412 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009413 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009414 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9415 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009416 {
9417 if (errormsg != NULL)
9418 goto erret;
9419 // empty line or comment
9420 line = (char_u *)"";
9421 continue;
9422 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009423 generate_cmdmods(&cctx, &local_cmdmod);
9424 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009425
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009426 // Check if there was a colon after the last command modifier or before
9427 // the current position.
9428 for (p = ea.cmd; p >= line; --p)
9429 {
9430 if (*p == ':')
9431 starts_with_colon = TRUE;
9432 if (p < ea.cmd && !VIM_ISWHITE(*p))
9433 break;
9434 }
9435
Bram Moolenaarce024c32021-06-26 13:00:49 +02009436 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009437 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009438 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009439 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009440 if (checkforcmd(&ea.cmd, "call", 3))
9441 {
9442 if (*ea.cmd == '(')
9443 // not for "call()"
9444 ea.cmd = p;
9445 else
9446 ea.cmd = skipwhite(ea.cmd);
9447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009448
Bram Moolenaarce024c32021-06-26 13:00:49 +02009449 if (!starts_with_colon)
9450 {
9451 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009452
Bram Moolenaarce024c32021-06-26 13:00:49 +02009453 // Check for assignment after command modifiers.
9454 assign = may_compile_assignment(&ea, &line, &cctx);
9455 if (assign == OK)
9456 goto nextline;
9457 if (assign == FAIL)
9458 goto erret;
9459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009460 }
9461
9462 /*
9463 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009464 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009465 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009466 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009467 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009468 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9469 && (starts_with_colon || !(*cmd == '\''
9470 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009471 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009472 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009473 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009474 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009475 if (!starts_with_colon)
9476 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009477 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009478 goto erret;
9479 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009480 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009481 if (ends_excmd2(line, ea.cmd))
9482 {
9483 // A range without a command: jump to the line.
9484 // TODO: compile to a more efficient command, possibly
9485 // calling parse_cmd_address().
9486 ea.cmdidx = CMD_SIZE;
9487 line = compile_exec(line, &ea, &cctx);
9488 goto nextline;
9489 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009490 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009491 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009492 p = find_ex_command(&ea, NULL, starts_with_colon
9493 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009494
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009495 if (p == NULL)
9496 {
9497 if (cctx.ctx_skip != SKIP_YES)
9498 emsg(_(e_ambiguous_use_of_user_defined_command));
9499 goto erret;
9500 }
9501
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009502 // When using ":legacy cmd" always use compile_exec().
9503 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009504 {
9505 char_u *start = ea.cmd;
9506
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009507 switch (ea.cmdidx)
9508 {
9509 case CMD_if:
9510 case CMD_elseif:
9511 case CMD_else:
9512 case CMD_endif:
9513 case CMD_for:
9514 case CMD_endfor:
9515 case CMD_continue:
9516 case CMD_break:
9517 case CMD_while:
9518 case CMD_endwhile:
9519 case CMD_try:
9520 case CMD_catch:
9521 case CMD_finally:
9522 case CMD_endtry:
9523 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9524 goto erret;
9525 default: break;
9526 }
9527
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009528 // ":legacy return expr" needs to be handled differently.
9529 if (checkforcmd(&start, "return", 4))
9530 ea.cmdidx = CMD_return;
9531 else
9532 ea.cmdidx = CMD_legacy;
9533 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009535 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9536 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009537 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009538 {
9539 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009540 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009541 }
9542
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009543 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009544 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009545 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009546 // CMD_var cannot happen, compile_assignment() above would be
9547 // used. Most likely an assignment to a non-existing variable.
9548 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009549 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009551 }
9552
Bram Moolenaar3988f642020-08-27 22:43:03 +02009553 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009554 && ea.cmdidx != CMD_elseif
9555 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009556 && ea.cmdidx != CMD_endif
9557 && ea.cmdidx != CMD_endfor
9558 && ea.cmdidx != CMD_endwhile
9559 && ea.cmdidx != CMD_catch
9560 && ea.cmdidx != CMD_finally
9561 && ea.cmdidx != CMD_endtry)
9562 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009563 emsg(_(e_unreachable_code_after_return));
9564 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009565 }
9566
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009567 p = skipwhite(p);
9568 if (ea.cmdidx != CMD_SIZE
9569 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9570 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009571 if (ea.cmdidx >= 0)
9572 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009573 if ((ea.argt & EX_BANG) && *p == '!')
9574 {
9575 ea.forceit = TRUE;
9576 p = skipwhite(p + 1);
9577 }
9578 }
9579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009580 switch (ea.cmdidx)
9581 {
9582 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009583 ea.arg = p;
9584 line = compile_nested_function(&ea, &cctx);
9585 break;
9586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009587 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009588 // TODO: should we allow this, e.g. to declare a global
9589 // function?
9590 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009591 goto erret;
9592
9593 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009594 line = compile_return(p, check_return_type,
9595 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009596 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009597 break;
9598
9599 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009600 emsg(_(e_cannot_use_let_in_vim9_script));
9601 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009602 case CMD_var:
9603 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009604 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009605 case CMD_increment:
9606 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009607 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009608 if (line == p)
9609 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009610 break;
9611
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009612 case CMD_unlet:
9613 case CMD_unlockvar:
9614 case CMD_lockvar:
9615 line = compile_unletlock(p, &ea, &cctx);
9616 break;
9617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009618 case CMD_import:
9619 line = compile_import(p, &cctx);
9620 break;
9621
9622 case CMD_if:
9623 line = compile_if(p, &cctx);
9624 break;
9625 case CMD_elseif:
9626 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009627 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009628 break;
9629 case CMD_else:
9630 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009631 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009632 break;
9633 case CMD_endif:
9634 line = compile_endif(p, &cctx);
9635 break;
9636
9637 case CMD_while:
9638 line = compile_while(p, &cctx);
9639 break;
9640 case CMD_endwhile:
9641 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009642 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009643 break;
9644
9645 case CMD_for:
9646 line = compile_for(p, &cctx);
9647 break;
9648 case CMD_endfor:
9649 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009650 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009651 break;
9652 case CMD_continue:
9653 line = compile_continue(p, &cctx);
9654 break;
9655 case CMD_break:
9656 line = compile_break(p, &cctx);
9657 break;
9658
9659 case CMD_try:
9660 line = compile_try(p, &cctx);
9661 break;
9662 case CMD_catch:
9663 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009664 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009665 break;
9666 case CMD_finally:
9667 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009668 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009669 break;
9670 case CMD_endtry:
9671 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009672 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009673 break;
9674 case CMD_throw:
9675 line = compile_throw(p, &cctx);
9676 break;
9677
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009678 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009679 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009680 break;
9681
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009682 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009683 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009684 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009685 case CMD_echomsg:
9686 case CMD_echoerr:
9687 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009688 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009689
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009690 case CMD_put:
9691 ea.cmd = cmd;
9692 line = compile_put(p, &ea, &cctx);
9693 break;
9694
Bram Moolenaar4c137212021-04-19 16:48:48 +02009695 case CMD_substitute:
9696 if (cctx.ctx_skip == SKIP_YES)
9697 line = (char_u *)"";
9698 else
9699 {
9700 ea.arg = p;
9701 line = compile_substitute(line, &ea, &cctx);
9702 }
9703 break;
9704
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009705 case CMD_redir:
9706 ea.arg = p;
9707 line = compile_redir(line, &ea, &cctx);
9708 break;
9709
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009710 case CMD_cexpr:
9711 case CMD_lexpr:
9712 case CMD_caddexpr:
9713 case CMD_laddexpr:
9714 case CMD_cgetexpr:
9715 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009716#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009717 ea.arg = p;
9718 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009719#else
9720 ex_ni(&ea);
9721 line = NULL;
9722#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009723 break;
9724
Bram Moolenaar3988f642020-08-27 22:43:03 +02009725 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009726
Bram Moolenaarae616492020-07-28 20:07:27 +02009727 case CMD_append:
9728 case CMD_change:
9729 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009730 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009731 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009732 case CMD_xit:
9733 not_in_vim9(&ea);
9734 goto erret;
9735
Bram Moolenaar002262f2020-07-08 17:47:57 +02009736 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009737 if (cctx.ctx_skip != SKIP_YES)
9738 {
9739 semsg(_(e_invalid_command_str), ea.cmd);
9740 goto erret;
9741 }
9742 // We don't check for a next command here.
9743 line = (char_u *)"";
9744 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009745
Bram Moolenaar20677332021-06-06 17:02:53 +02009746 case CMD_lua:
9747 case CMD_mzscheme:
9748 case CMD_perl:
9749 case CMD_py3:
9750 case CMD_python3:
9751 case CMD_python:
9752 case CMD_pythonx:
9753 case CMD_ruby:
9754 case CMD_tcl:
9755 ea.arg = p;
9756 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009757 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009758 else
9759 // heredoc lines have been concatenated with NL
9760 // characters in get_function_body()
9761 line = compile_script(line, &cctx);
9762 break;
9763
9764 default:
9765 // Not recognized, execute with do_cmdline_cmd().
9766 ea.arg = p;
9767 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009768 break;
9769 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009770nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009771 if (line == NULL)
9772 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009773 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009774
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009775 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009776 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009778 if (cctx.ctx_type_stack.ga_len < 0)
9779 {
9780 iemsg("Type stack underflow");
9781 goto erret;
9782 }
9783 }
9784
9785 if (cctx.ctx_scope != NULL)
9786 {
9787 if (cctx.ctx_scope->se_type == IF_SCOPE)
9788 emsg(_(e_endif));
9789 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9790 emsg(_(e_endwhile));
9791 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9792 emsg(_(e_endfor));
9793 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009794 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009795 goto erret;
9796 }
9797
Bram Moolenaarefd88552020-06-18 20:50:10 +02009798 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009799 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009800 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9801 ufunc->uf_ret_type = &t_void;
9802 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009803 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009804 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009805 goto erret;
9806 }
9807
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009808 // Return void if there is no return at the end.
9809 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009810 }
9811
Bram Moolenaar599410c2021-04-10 14:03:43 +02009812 // When compiled with ":silent!" and there was an error don't consider the
9813 // function compiled.
9814 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009815 {
9816 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9817 + ufunc->uf_dfunc_idx;
9818 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009819 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009820#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009821 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009822 {
9823 dfunc->df_instr_prof = instr->ga_data;
9824 dfunc->df_instr_prof_count = instr->ga_len;
9825 }
9826 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009827#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009828 if (cctx.ctx_compile_type == CT_DEBUG)
9829 {
9830 dfunc->df_instr_debug = instr->ga_data;
9831 dfunc->df_instr_debug_count = instr->ga_len;
9832 }
9833 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009834 {
9835 dfunc->df_instr = instr->ga_data;
9836 dfunc->df_instr_count = instr->ga_len;
9837 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009838 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009839 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009840 if (cctx.ctx_outer_used)
9841 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009842 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009843 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009844
9845 ret = OK;
9846
9847erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009848 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009849 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009850 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9851 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009852
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009853 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009854 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009855 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009856 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009857
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009858 // If using the last entry in the table and it was added above, we
9859 // might as well remove it.
9860 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009861 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009862 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009863 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009864 ufunc->uf_dfunc_idx = 0;
9865 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009866 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009867
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009868 while (cctx.ctx_scope != NULL)
9869 drop_scope(&cctx);
9870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009871 if (errormsg != NULL)
9872 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009873 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009874 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009875 }
9876
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009877 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9878 {
9879 if (ret == OK)
9880 {
9881 emsg(_(e_missing_redir_end));
9882 ret = FAIL;
9883 }
9884 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009885 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009886 }
9887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009888 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009889 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009890 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009891 if (do_estack_push)
9892 estack_pop();
9893
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009894 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009895 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009896 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009897 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009898 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009899}
9900
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009901 void
9902set_function_type(ufunc_T *ufunc)
9903{
9904 int varargs = ufunc->uf_va_name != NULL;
9905 int argcount = ufunc->uf_args.ga_len;
9906
9907 // Create a type for the function, with the return type and any
9908 // argument types.
9909 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9910 // The type is included in "tt_args".
9911 if (argcount > 0 || varargs)
9912 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009913 if (ufunc->uf_type_list.ga_itemsize == 0)
9914 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009915 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9916 argcount, &ufunc->uf_type_list);
9917 // Add argument types to the function type.
9918 if (func_type_add_arg_types(ufunc->uf_func_type,
9919 argcount + varargs,
9920 &ufunc->uf_type_list) == FAIL)
9921 return;
9922 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9923 ufunc->uf_func_type->tt_min_argcount =
9924 argcount - ufunc->uf_def_args.ga_len;
9925 if (ufunc->uf_arg_types == NULL)
9926 {
9927 int i;
9928
9929 // lambda does not have argument types.
9930 for (i = 0; i < argcount; ++i)
9931 ufunc->uf_func_type->tt_args[i] = &t_any;
9932 }
9933 else
9934 mch_memmove(ufunc->uf_func_type->tt_args,
9935 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9936 if (varargs)
9937 {
9938 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009939 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009940 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9941 }
9942 }
9943 else
9944 // No arguments, can use a predefined type.
9945 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9946 argcount, &ufunc->uf_type_list);
9947}
9948
9949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009950/*
9951 * Delete an instruction, free what it contains.
9952 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009953 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009954delete_instr(isn_T *isn)
9955{
9956 switch (isn->isn_type)
9957 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009958 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009959 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009960 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009961 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009962 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009963 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009964 case ISN_LOADENV:
9965 case ISN_LOADG:
9966 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009967 case ISN_LOADT:
9968 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009969 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009970 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009971 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009972 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009973 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009974 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009975 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009976 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009977 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009978 case ISN_STOREW:
9979 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009980 vim_free(isn->isn_arg.string);
9981 break;
9982
Bram Moolenaar4c137212021-04-19 16:48:48 +02009983 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009984 {
9985 int idx;
9986 isn_T *list = isn->isn_arg.subs.subs_instr;
9987
9988 vim_free(isn->isn_arg.subs.subs_cmd);
9989 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9990 delete_instr(list + idx);
9991 vim_free(list);
9992 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009993 break;
9994
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009995 case ISN_INSTR:
9996 {
9997 int idx;
9998 isn_T *list = isn->isn_arg.instr;
9999
10000 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10001 delete_instr(list + idx);
10002 vim_free(list);
10003 }
10004 break;
10005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010006 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010007 case ISN_STORES:
10008 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010009 break;
10010
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010011 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010012 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010013 vim_free(isn->isn_arg.unlet.ul_name);
10014 break;
10015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010016 case ISN_STOREOPT:
10017 vim_free(isn->isn_arg.storeopt.so_name);
10018 break;
10019
10020 case ISN_PUSHBLOB: // push blob isn_arg.blob
10021 blob_unref(isn->isn_arg.blob);
10022 break;
10023
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010024 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010025#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010026 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010027#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010028 break;
10029
10030 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010031#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010032 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010033#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010034 break;
10035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010036 case ISN_UCALL:
10037 vim_free(isn->isn_arg.ufunc.cuf_name);
10038 break;
10039
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010040 case ISN_FUNCREF:
10041 {
10042 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10043 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010044 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010045
Bram Moolenaar077a4232020-12-22 18:33:27 +010010046 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10047 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010048 }
10049 break;
10050
10051 case ISN_DCALL:
10052 {
10053 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10054 + isn->isn_arg.dfunc.cdf_idx;
10055
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010056 if (dfunc->df_ufunc != NULL
10057 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010058 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010059 }
10060 break;
10061
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010062 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010063 {
10064 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10065 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10066
10067 if (ufunc != NULL)
10068 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010069 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010070 func_ptr_unref(ufunc);
10071 }
10072
10073 vim_free(lambda);
10074 vim_free(isn->isn_arg.newfunc.nf_global);
10075 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010076 break;
10077
Bram Moolenaar5e654232020-09-16 15:22:00 +020010078 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010079 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010080 free_type(isn->isn_arg.type.ct_type);
10081 break;
10082
Bram Moolenaar02194d22020-10-24 23:08:38 +020010083 case ISN_CMDMOD:
10084 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10085 ->cmod_filter_regmatch.regprog);
10086 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10087 break;
10088
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010089 case ISN_LOADSCRIPT:
10090 case ISN_STORESCRIPT:
10091 vim_free(isn->isn_arg.script.scriptref);
10092 break;
10093
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010094 case ISN_TRY:
10095 vim_free(isn->isn_arg.try.try_ref);
10096 break;
10097
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010098 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010099 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010100 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10101 break;
10102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010103 case ISN_2BOOL:
10104 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010105 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010106 case ISN_ADDBLOB:
10107 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010108 case ISN_ANYINDEX:
10109 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010110 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010111 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010112 case ISN_BLOBINDEX:
10113 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010114 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010115 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010116 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010117 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010118 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010119 case ISN_COMPAREANY:
10120 case ISN_COMPAREBLOB:
10121 case ISN_COMPAREBOOL:
10122 case ISN_COMPAREDICT:
10123 case ISN_COMPAREFLOAT:
10124 case ISN_COMPAREFUNC:
10125 case ISN_COMPARELIST:
10126 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010127 case ISN_COMPARESPECIAL:
10128 case ISN_COMPARESTRING:
10129 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010130 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010131 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010132 case ISN_DROP:
10133 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010134 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010135 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010136 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010137 case ISN_EXECCONCAT:
10138 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010139 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010140 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010141 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010142 case ISN_GETITEM:
10143 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010144 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010145 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010146 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010147 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010148 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010149 case ISN_LOADBDICT:
10150 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010151 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010152 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010153 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010154 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010155 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010156 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010157 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010158 case ISN_NEGATENR:
10159 case ISN_NEWDICT:
10160 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010161 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010162 case ISN_OPFLOAT:
10163 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010164 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010165 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010166 case ISN_PROF_END:
10167 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010168 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010169 case ISN_PUSHF:
10170 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010171 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010172 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010173 case ISN_REDIREND:
10174 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010175 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010176 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010177 case ISN_SHUFFLE:
10178 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010179 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010180 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010181 case ISN_STORENR:
10182 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010183 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010184 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010185 case ISN_STOREV:
10186 case ISN_STRINDEX:
10187 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010188 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010189 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010190 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010191 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010192 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010193 // nothing allocated
10194 break;
10195 }
10196}
10197
10198/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010199 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010200 */
10201 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010202delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010203{
10204 int idx;
10205
10206 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010207 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010208
10209 if (dfunc->df_instr != NULL)
10210 {
10211 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10212 delete_instr(dfunc->df_instr + idx);
10213 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010214 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010215 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010216 if (dfunc->df_instr_debug != NULL)
10217 {
10218 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10219 delete_instr(dfunc->df_instr_debug + idx);
10220 VIM_CLEAR(dfunc->df_instr_debug);
10221 dfunc->df_instr_debug = NULL;
10222 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010223#ifdef FEAT_PROFILE
10224 if (dfunc->df_instr_prof != NULL)
10225 {
10226 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10227 delete_instr(dfunc->df_instr_prof + idx);
10228 VIM_CLEAR(dfunc->df_instr_prof);
10229 dfunc->df_instr_prof = NULL;
10230 }
10231#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010232
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010233 if (mark_deleted)
10234 dfunc->df_deleted = TRUE;
10235 if (dfunc->df_ufunc != NULL)
10236 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010237}
10238
10239/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010240 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010241 * function, unless another user function still uses it.
10242 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010243 */
10244 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010245unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010246{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010247 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010248 {
10249 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10250 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010251
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010252 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010253 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010254 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010255 ufunc->uf_dfunc_idx = 0;
10256 if (dfunc->df_ufunc == ufunc)
10257 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010258 }
10259}
10260
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010261/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010262 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010263 */
10264 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010265link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010266{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010267 if (ufunc->uf_dfunc_idx > 0)
10268 {
10269 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10270 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010271
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010272 ++dfunc->df_refcount;
10273 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010274}
10275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010276#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010277/*
10278 * Free all functions defined with ":def".
10279 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010280 void
10281free_def_functions(void)
10282{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010283 int idx;
10284
10285 for (idx = 0; idx < def_functions.ga_len; ++idx)
10286 {
10287 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10288
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010289 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010290 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010291 }
10292
10293 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010294}
10295#endif
10296
10297
10298#endif // FEAT_EVAL