blob: dd3f3aec320d0aa5ae3665c74ab6ae9c090cb4af [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100277 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
278 {
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.
501 if (ufunc == NULL || !func_is_global(ufunc)
502 || (p[0] == 'g' && p[1] == ':'))
503 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100504 if (is_arg)
505 semsg(_(e_argument_name_shadows_existing_variable_str), p);
506 else
507 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200508 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200509 return FAIL;
510 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100511 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200512 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100513 return OK;
514}
515
Bram Moolenaar65b95452020-07-19 14:03:09 +0200516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100517/////////////////////////////////////////////////////////////////////
518// Following generate_ functions expect the caller to call ga_grow().
519
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200520#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
521#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523/*
524 * Generate an instruction without arguments.
525 * Returns a pointer to the new instruction, NULL if failed.
526 */
527 static isn_T *
528generate_instr(cctx_T *cctx, isntype_T isn_type)
529{
530 garray_T *instr = &cctx->ctx_instr;
531 isn_T *isn;
532
Bram Moolenaar080457c2020-03-03 21:53:32 +0100533 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534 if (ga_grow(instr, 1) == FAIL)
535 return NULL;
536 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
537 isn->isn_type = isn_type;
538 isn->isn_lnum = cctx->ctx_lnum + 1;
539 ++instr->ga_len;
540
541 return isn;
542}
543
544/*
545 * Generate an instruction without arguments.
546 * "drop" will be removed from the stack.
547 * Returns a pointer to the new instruction, NULL if failed.
548 */
549 static isn_T *
550generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
551{
552 garray_T *stack = &cctx->ctx_type_stack;
553
Bram Moolenaar080457c2020-03-03 21:53:32 +0100554 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 stack->ga_len -= drop;
556 return generate_instr(cctx, isn_type);
557}
558
559/*
560 * Generate instruction "isn_type" and put "type" on the type stack.
561 */
562 static isn_T *
563generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
564{
565 isn_T *isn;
566 garray_T *stack = &cctx->ctx_type_stack;
567
568 if ((isn = generate_instr(cctx, isn_type)) == NULL)
569 return NULL;
570
571 if (ga_grow(stack, 1) == FAIL)
572 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200573 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 ++stack->ga_len;
575
576 return isn;
577}
578
579/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200580 * Generate an ISN_DEBUG instruction.
581 */
582 static isn_T *
583generate_instr_debug(cctx_T *cctx)
584{
585 isn_T *isn;
586 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
587 + cctx->ctx_ufunc->uf_dfunc_idx;
588
589 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
590 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200591 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
592 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200593 return isn;
594}
595
596/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200598 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200599 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 */
601 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200602may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603{
604 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200605 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100607 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100609 RETURN_OK_IF_SKIP(cctx);
610 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200611 switch ((*type)->tt_type)
612 {
613 // nothing to be done
614 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200616 // conversion possible
617 case VAR_SPECIAL:
618 case VAR_BOOL:
619 case VAR_NUMBER:
620 case VAR_FLOAT:
621 break;
622
623 // conversion possible (with runtime check)
624 case VAR_ANY:
625 case VAR_UNKNOWN:
626 isntype = ISN_2STRING_ANY;
627 break;
628
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200629 // conversion possible when tolerant
630 case VAR_LIST:
631 if (tolerant)
632 {
633 isntype = ISN_2STRING_ANY;
634 break;
635 }
636 // FALLTHROUGH
637
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200638 // conversion not possible
639 case VAR_VOID:
640 case VAR_BLOB:
641 case VAR_FUNC:
642 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200643 case VAR_DICT:
644 case VAR_JOB:
645 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200646 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200647 to_string_error((*type)->tt_type);
648 return FAIL;
649 }
650
651 *type = &t_string;
652 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200654 isn->isn_arg.tostring.offset = offset;
655 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656
657 return OK;
658}
659
660 static int
661check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
662{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200663 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200665 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 {
667 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200668 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200670 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671 return FAIL;
672 }
673 return OK;
674}
675
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200676 static int
677generate_add_instr(
678 cctx_T *cctx,
679 vartype_T vartype,
680 type_T *type1,
681 type_T *type2)
682{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100683 garray_T *stack = &cctx->ctx_type_stack;
684 isn_T *isn = generate_instr_drop(cctx,
685 vartype == VAR_NUMBER ? ISN_OPNR
686 : vartype == VAR_LIST ? ISN_ADDLIST
687 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200688#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100689 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200690#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100691 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200692
693 if (vartype != VAR_LIST && vartype != VAR_BLOB
694 && type1->tt_type != VAR_ANY
695 && type2->tt_type != VAR_ANY
696 && check_number_or_float(
697 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
698 return FAIL;
699
700 if (isn != NULL)
701 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100702
703 // When concatenating two lists with different member types the member type
704 // becomes "any".
705 if (vartype == VAR_LIST
706 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
707 && type1->tt_member != type2->tt_member)
708 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
709
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200710 return isn == NULL ? FAIL : OK;
711}
712
713/*
714 * Get the type to use for an instruction for an operation on "type1" and
715 * "type2". If they are matching use a type-specific instruction. Otherwise
716 * fall back to runtime type checking.
717 */
718 static vartype_T
719operator_type(type_T *type1, type_T *type2)
720{
721 if (type1->tt_type == type2->tt_type
722 && (type1->tt_type == VAR_NUMBER
723 || type1->tt_type == VAR_LIST
724#ifdef FEAT_FLOAT
725 || type1->tt_type == VAR_FLOAT
726#endif
727 || type1->tt_type == VAR_BLOB))
728 return type1->tt_type;
729 return VAR_ANY;
730}
731
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732/*
733 * Generate an instruction with two arguments. The instruction depends on the
734 * type of the arguments.
735 */
736 static int
737generate_two_op(cctx_T *cctx, char_u *op)
738{
739 garray_T *stack = &cctx->ctx_type_stack;
740 type_T *type1;
741 type_T *type2;
742 vartype_T vartype;
743 isn_T *isn;
744
Bram Moolenaar080457c2020-03-03 21:53:32 +0100745 RETURN_OK_IF_SKIP(cctx);
746
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200747 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
749 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200750 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751
752 switch (*op)
753 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200754 case '+':
755 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 break;
758
759 case '-':
760 case '*':
761 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
762 op) == FAIL)
763 return FAIL;
764 if (vartype == VAR_NUMBER)
765 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
766#ifdef FEAT_FLOAT
767 else if (vartype == VAR_FLOAT)
768 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
769#endif
770 else
771 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
772 if (isn != NULL)
773 isn->isn_arg.op.op_type = *op == '*'
774 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
775 break;
776
Bram Moolenaar4c683752020-04-05 21:38:23 +0200777 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200779 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100780 && type2->tt_type != VAR_NUMBER))
781 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200782 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 return FAIL;
784 }
785 isn = generate_instr_drop(cctx,
786 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
787 if (isn != NULL)
788 isn->isn_arg.op.op_type = EXPR_REM;
789 break;
790 }
791
792 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200793 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 {
795 type_T *type = &t_any;
796
797#ifdef FEAT_FLOAT
798 // float+number and number+float results in float
799 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
800 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
801 type = &t_float;
802#endif
803 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
804 }
805
806 return OK;
807}
808
809/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200810 * Get the instruction to use for comparing "type1" with "type2"
811 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200813 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100814get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815{
816 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817
Bram Moolenaar4c683752020-04-05 21:38:23 +0200818 if (type1 == VAR_UNKNOWN)
819 type1 = VAR_ANY;
820 if (type2 == VAR_UNKNOWN)
821 type2 = VAR_ANY;
822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if (type1 == type2)
824 {
825 switch (type1)
826 {
827 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
828 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
829 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
830 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
831 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
832 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
833 case VAR_LIST: isntype = ISN_COMPARELIST; break;
834 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
835 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 default: isntype = ISN_COMPAREANY; break;
837 }
838 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200839 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100841 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842 isntype = ISN_COMPAREANY;
843
Bram Moolenaar657137c2021-01-09 15:45:23 +0100844 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 && (isntype == ISN_COMPAREBOOL
846 || isntype == ISN_COMPARESPECIAL
847 || isntype == ISN_COMPARENR
848 || isntype == ISN_COMPAREFLOAT))
849 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200850 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100851 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200852 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 }
854 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100855 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
857 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100858 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
859 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 && (type1 == VAR_BLOB || type2 == VAR_BLOB
861 || type1 == VAR_LIST || type2 == VAR_LIST))))
862 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200863 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200865 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200867 return isntype;
868}
869
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200870 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100871check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200872{
873 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
874 return FAIL;
875 return OK;
876}
877
Bram Moolenaara5565e42020-05-09 15:44:01 +0200878/*
879 * Generate an ISN_COMPARE* instruction with a boolean result.
880 */
881 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100882generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200883{
884 isntype_T isntype;
885 isn_T *isn;
886 garray_T *stack = &cctx->ctx_type_stack;
887 vartype_T type1;
888 vartype_T type2;
889
890 RETURN_OK_IF_SKIP(cctx);
891
892 // Get the known type of the two items on the stack. If they are matching
893 // use a type-specific instruction. Otherwise fall back to runtime type
894 // checking.
895 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
896 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100897 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200898 if (isntype == ISN_DROP)
899 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900
901 if ((isn = generate_instr(cctx, isntype)) == NULL)
902 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100903 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 isn->isn_arg.op.op_ic = ic;
905
906 // takes two arguments, puts one bool back
907 if (stack->ga_len >= 2)
908 {
909 --stack->ga_len;
910 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
911 }
912
913 return OK;
914}
915
916/*
917 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200918 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919 */
920 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200921generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922{
923 isn_T *isn;
924 garray_T *stack = &cctx->ctx_type_stack;
925
Bram Moolenaar080457c2020-03-03 21:53:32 +0100926 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
928 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200929 isn->isn_arg.tobool.invert = invert;
930 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931
932 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200933 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934
935 return OK;
936}
937
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200938/*
939 * Generate an ISN_COND2BOOL instruction.
940 */
941 static int
942generate_COND2BOOL(cctx_T *cctx)
943{
944 isn_T *isn;
945 garray_T *stack = &cctx->ctx_type_stack;
946
947 RETURN_OK_IF_SKIP(cctx);
948 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
949 return FAIL;
950
951 // type becomes bool
952 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
953
954 return OK;
955}
956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200958generate_TYPECHECK(
959 cctx_T *cctx,
960 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100961 int offset,
962 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963{
964 isn_T *isn;
965 garray_T *stack = &cctx->ctx_type_stack;
966
Bram Moolenaar080457c2020-03-03 21:53:32 +0100967 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
969 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200970 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100971 isn->isn_arg.type.ct_off = (int8_T)offset;
972 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973
Bram Moolenaar5e654232020-09-16 15:22:00 +0200974 // type becomes expected
975 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976
977 return OK;
978}
979
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100980 static int
981generate_SETTYPE(
982 cctx_T *cctx,
983 type_T *expected)
984{
985 isn_T *isn;
986
987 RETURN_OK_IF_SKIP(cctx);
988 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
989 return FAIL;
990 isn->isn_arg.type.ct_type = alloc_type(expected);
991 return OK;
992}
993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200995 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
996 * used. Return FALSE if the types will never match.
997 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100998 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200999use_typecheck(type_T *actual, type_T *expected)
1000{
1001 if (actual->tt_type == VAR_ANY
1002 || actual->tt_type == VAR_UNKNOWN
1003 || (actual->tt_type == VAR_FUNC
1004 && (expected->tt_type == VAR_FUNC
1005 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001006 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1007 && ((actual->tt_member == &t_void)
1008 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001009 return TRUE;
1010 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1011 && actual->tt_type == expected->tt_type)
1012 // This takes care of a nested list or dict.
1013 return use_typecheck(actual->tt_member, expected->tt_member);
1014 return FALSE;
1015}
1016
1017/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001018 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001019 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001020 * - "actual" is a type that can be "expected" type: add a runtime check; or
1021 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001022 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1023 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001024 */
Bram Moolenaar351ead02021-01-16 16:07:01 +01001025 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001026need_type(
1027 type_T *actual,
1028 type_T *expected,
1029 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001030 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001031 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001032 int silent,
1033 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001034{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001035 where_T where;
1036
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001037 if (expected == &t_bool && actual != &t_bool
1038 && (actual->tt_flags & TTFLAG_BOOL_OK))
1039 {
1040 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1041 // boolean is OK but requires a conversion.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001042 generate_2BOOL(cctx, FALSE, offset);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001043 return OK;
1044 }
1045
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001046 where.wt_index = arg_idx;
1047 where.wt_variable = FALSE;
1048 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001049 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001050
1051 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001052 // If it's a constant a runtime check makes no sense.
1053 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001054 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001055 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001056 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001057 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001058
1059 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001060 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001061 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001062}
1063
1064/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001065 * Check that the top of the type stack has a type that can be used as a
1066 * condition. Give an error and return FAIL if not.
1067 */
1068 static int
1069bool_on_stack(cctx_T *cctx)
1070{
1071 garray_T *stack = &cctx->ctx_type_stack;
1072 type_T *type;
1073
1074 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1075 if (type == &t_bool)
1076 return OK;
1077
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001078 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001079 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1080 // This requires a runtime type check.
1081 return generate_COND2BOOL(cctx);
1082
Bram Moolenaar351ead02021-01-16 16:07:01 +01001083 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001084}
1085
1086/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 * Generate an ISN_PUSHNR instruction.
1088 */
1089 static int
1090generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1091{
1092 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001093 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094
Bram Moolenaar080457c2020-03-03 21:53:32 +01001095 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1097 return FAIL;
1098 isn->isn_arg.number = number;
1099
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001100 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001101 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001102 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103 return OK;
1104}
1105
1106/*
1107 * Generate an ISN_PUSHBOOL instruction.
1108 */
1109 static int
1110generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1111{
1112 isn_T *isn;
1113
Bram Moolenaar080457c2020-03-03 21:53:32 +01001114 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1116 return FAIL;
1117 isn->isn_arg.number = number;
1118
1119 return OK;
1120}
1121
1122/*
1123 * Generate an ISN_PUSHSPEC instruction.
1124 */
1125 static int
1126generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1127{
1128 isn_T *isn;
1129
Bram Moolenaar080457c2020-03-03 21:53:32 +01001130 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1132 return FAIL;
1133 isn->isn_arg.number = number;
1134
1135 return OK;
1136}
1137
1138#ifdef FEAT_FLOAT
1139/*
1140 * Generate an ISN_PUSHF instruction.
1141 */
1142 static int
1143generate_PUSHF(cctx_T *cctx, float_T fnumber)
1144{
1145 isn_T *isn;
1146
Bram Moolenaar080457c2020-03-03 21:53:32 +01001147 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1149 return FAIL;
1150 isn->isn_arg.fnumber = fnumber;
1151
1152 return OK;
1153}
1154#endif
1155
1156/*
1157 * Generate an ISN_PUSHS instruction.
1158 * Consumes "str".
1159 */
1160 static int
1161generate_PUSHS(cctx_T *cctx, char_u *str)
1162{
1163 isn_T *isn;
1164
Bram Moolenaar508b5612021-01-02 18:17:26 +01001165 if (cctx->ctx_skip == SKIP_YES)
1166 {
1167 vim_free(str);
1168 return OK;
1169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1171 return FAIL;
1172 isn->isn_arg.string = str;
1173
1174 return OK;
1175}
1176
1177/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001178 * Generate an ISN_PUSHCHANNEL instruction.
1179 * Consumes "channel".
1180 */
1181 static int
1182generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1183{
1184 isn_T *isn;
1185
Bram Moolenaar080457c2020-03-03 21:53:32 +01001186 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001187 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1188 return FAIL;
1189 isn->isn_arg.channel = channel;
1190
1191 return OK;
1192}
1193
1194/*
1195 * Generate an ISN_PUSHJOB instruction.
1196 * Consumes "job".
1197 */
1198 static int
1199generate_PUSHJOB(cctx_T *cctx, job_T *job)
1200{
1201 isn_T *isn;
1202
Bram Moolenaar080457c2020-03-03 21:53:32 +01001203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001204 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001205 return FAIL;
1206 isn->isn_arg.job = job;
1207
1208 return OK;
1209}
1210
1211/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 * Generate an ISN_PUSHBLOB instruction.
1213 * Consumes "blob".
1214 */
1215 static int
1216generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1217{
1218 isn_T *isn;
1219
Bram Moolenaar080457c2020-03-03 21:53:32 +01001220 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1222 return FAIL;
1223 isn->isn_arg.blob = blob;
1224
1225 return OK;
1226}
1227
1228/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001229 * Generate an ISN_PUSHFUNC instruction with name "name".
1230 * Consumes "name".
1231 */
1232 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001233generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001234{
1235 isn_T *isn;
1236
Bram Moolenaar080457c2020-03-03 21:53:32 +01001237 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001238 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001239 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001240 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001241
1242 return OK;
1243}
1244
1245/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001246 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001247 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1248 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001249 */
1250 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001251generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001252{
1253 isn_T *isn;
1254 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001255 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1256 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001257 type_T *item_type = &t_any;
1258
1259 RETURN_OK_IF_SKIP(cctx);
1260
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001261 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001262 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001263 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001264 emsg(_(e_listreq));
1265 return FAIL;
1266 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001267 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001268 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1269 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001270 isn->isn_arg.getitem.gi_index = index;
1271 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001272
1273 // add the item type to the type stack
1274 if (ga_grow(stack, 1) == FAIL)
1275 return FAIL;
1276 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1277 ++stack->ga_len;
1278 return OK;
1279}
1280
1281/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001282 * Generate an ISN_SLICE instruction with "count".
1283 */
1284 static int
1285generate_SLICE(cctx_T *cctx, int count)
1286{
1287 isn_T *isn;
1288
1289 RETURN_OK_IF_SKIP(cctx);
1290 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1291 return FAIL;
1292 isn->isn_arg.number = count;
1293 return OK;
1294}
1295
1296/*
1297 * Generate an ISN_CHECKLEN instruction with "min_len".
1298 */
1299 static int
1300generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1301{
1302 isn_T *isn;
1303
1304 RETURN_OK_IF_SKIP(cctx);
1305
1306 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1307 return FAIL;
1308 isn->isn_arg.checklen.cl_min_len = min_len;
1309 isn->isn_arg.checklen.cl_more_OK = more_OK;
1310
1311 return OK;
1312}
1313
1314/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 * Generate an ISN_STORE instruction.
1316 */
1317 static int
1318generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1319{
1320 isn_T *isn;
1321
Bram Moolenaar080457c2020-03-03 21:53:32 +01001322 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1324 return FAIL;
1325 if (name != NULL)
1326 isn->isn_arg.string = vim_strsave(name);
1327 else
1328 isn->isn_arg.number = idx;
1329
1330 return OK;
1331}
1332
1333/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001334 * Generate an ISN_STOREOUTER instruction.
1335 */
1336 static int
1337generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1338{
1339 isn_T *isn;
1340
1341 RETURN_OK_IF_SKIP(cctx);
1342 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1343 return FAIL;
1344 isn->isn_arg.outer.outer_idx = idx;
1345 isn->isn_arg.outer.outer_depth = level;
1346
1347 return OK;
1348}
1349
1350/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1352 */
1353 static int
1354generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1355{
1356 isn_T *isn;
1357
Bram Moolenaar080457c2020-03-03 21:53:32 +01001358 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1360 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001361 isn->isn_arg.storenr.stnr_idx = idx;
1362 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363
1364 return OK;
1365}
1366
1367/*
1368 * Generate an ISN_STOREOPT instruction
1369 */
1370 static int
1371generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1372{
1373 isn_T *isn;
1374
Bram Moolenaar080457c2020-03-03 21:53:32 +01001375 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001376 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 return FAIL;
1378 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1379 isn->isn_arg.storeopt.so_flags = opt_flags;
1380
1381 return OK;
1382}
1383
1384/*
1385 * Generate an ISN_LOAD or similar instruction.
1386 */
1387 static int
1388generate_LOAD(
1389 cctx_T *cctx,
1390 isntype_T isn_type,
1391 int idx,
1392 char_u *name,
1393 type_T *type)
1394{
1395 isn_T *isn;
1396
Bram Moolenaar080457c2020-03-03 21:53:32 +01001397 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1399 return FAIL;
1400 if (name != NULL)
1401 isn->isn_arg.string = vim_strsave(name);
1402 else
1403 isn->isn_arg.number = idx;
1404
1405 return OK;
1406}
1407
1408/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001409 * Generate an ISN_LOADOUTER instruction
1410 */
1411 static int
1412generate_LOADOUTER(
1413 cctx_T *cctx,
1414 int idx,
1415 int nesting,
1416 type_T *type)
1417{
1418 isn_T *isn;
1419
1420 RETURN_OK_IF_SKIP(cctx);
1421 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1422 return FAIL;
1423 isn->isn_arg.outer.outer_idx = idx;
1424 isn->isn_arg.outer.outer_depth = nesting;
1425
1426 return OK;
1427}
1428
1429/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001430 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001431 */
1432 static int
1433generate_LOADV(
1434 cctx_T *cctx,
1435 char_u *name,
1436 int error)
1437{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001438 int di_flags;
1439 int vidx = find_vim_var(name, &di_flags);
1440 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001441
Bram Moolenaar080457c2020-03-03 21:53:32 +01001442 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001443 if (vidx < 0)
1444 {
1445 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001446 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001447 return FAIL;
1448 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001449 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001450
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001451 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001452}
1453
1454/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001455 * Generate an ISN_UNLET instruction.
1456 */
1457 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001458generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001459{
1460 isn_T *isn;
1461
1462 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001463 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001464 return FAIL;
1465 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1466 isn->isn_arg.unlet.ul_forceit = forceit;
1467
1468 return OK;
1469}
1470
1471/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001472 * Generate an ISN_LOCKCONST instruction.
1473 */
1474 static int
1475generate_LOCKCONST(cctx_T *cctx)
1476{
1477 isn_T *isn;
1478
1479 RETURN_OK_IF_SKIP(cctx);
1480 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1481 return FAIL;
1482 return OK;
1483}
1484
1485/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 * Generate an ISN_LOADS instruction.
1487 */
1488 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001489generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001491 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001493 int sid,
1494 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495{
1496 isn_T *isn;
1497
Bram Moolenaar080457c2020-03-03 21:53:32 +01001498 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001499 if (isn_type == ISN_LOADS)
1500 isn = generate_instr_type(cctx, isn_type, type);
1501 else
1502 isn = generate_instr_drop(cctx, isn_type, 1);
1503 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001505 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1506 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507
1508 return OK;
1509}
1510
1511/*
1512 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1513 */
1514 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001515generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516 cctx_T *cctx,
1517 isntype_T isn_type,
1518 int sid,
1519 int idx,
1520 type_T *type)
1521{
1522 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001523 scriptref_T *sref;
1524 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525
Bram Moolenaar080457c2020-03-03 21:53:32 +01001526 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 if (isn_type == ISN_LOADSCRIPT)
1528 isn = generate_instr_type(cctx, isn_type, type);
1529 else
1530 isn = generate_instr_drop(cctx, isn_type, 1);
1531 if (isn == NULL)
1532 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001533
1534 // This requires three arguments, which doesn't fit in an instruction, thus
1535 // we need to allocate a struct for this.
1536 sref = ALLOC_ONE(scriptref_T);
1537 if (sref == NULL)
1538 return FAIL;
1539 isn->isn_arg.script.scriptref = sref;
1540 sref->sref_sid = sid;
1541 sref->sref_idx = idx;
1542 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001543 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 return OK;
1545}
1546
1547/*
1548 * Generate an ISN_NEWLIST instruction.
1549 */
1550 static int
1551generate_NEWLIST(cctx_T *cctx, int count)
1552{
1553 isn_T *isn;
1554 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 type_T *type;
1556 type_T *member;
1557
Bram Moolenaar080457c2020-03-03 21:53:32 +01001558 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1560 return FAIL;
1561 isn->isn_arg.number = count;
1562
Bram Moolenaar127542b2020-08-09 17:22:04 +02001563 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001564 if (count == 0)
1565 member = &t_void;
1566 else
1567 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001568 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1569 cctx->ctx_type_list);
1570 type = get_list_type(member, cctx->ctx_type_list);
1571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 // drop the value types
1573 stack->ga_len -= count;
1574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 // add the list type to the type stack
1576 if (ga_grow(stack, 1) == FAIL)
1577 return FAIL;
1578 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1579 ++stack->ga_len;
1580
1581 return OK;
1582}
1583
1584/*
1585 * Generate an ISN_NEWDICT instruction.
1586 */
1587 static int
1588generate_NEWDICT(cctx_T *cctx, int count)
1589{
1590 isn_T *isn;
1591 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 type_T *type;
1593 type_T *member;
1594
Bram Moolenaar080457c2020-03-03 21:53:32 +01001595 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1597 return FAIL;
1598 isn->isn_arg.number = count;
1599
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001600 if (count == 0)
1601 member = &t_void;
1602 else
1603 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001604 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1605 cctx->ctx_type_list);
1606 type = get_dict_type(member, cctx->ctx_type_list);
1607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 // drop the key and value types
1609 stack->ga_len -= 2 * count;
1610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 // add the dict type to the type stack
1612 if (ga_grow(stack, 1) == FAIL)
1613 return FAIL;
1614 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1615 ++stack->ga_len;
1616
1617 return OK;
1618}
1619
1620/*
1621 * Generate an ISN_FUNCREF instruction.
1622 */
1623 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001624generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625{
1626 isn_T *isn;
1627 garray_T *stack = &cctx->ctx_type_stack;
1628
Bram Moolenaar080457c2020-03-03 21:53:32 +01001629 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1631 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001632 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001633 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001635 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001636 // the nested context, including this one.
1637 if (ufunc->uf_flags & FC_CLOSURE)
1638 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 if (ga_grow(stack, 1) == FAIL)
1641 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001642 ((type_T **)stack->ga_data)[stack->ga_len] =
1643 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 ++stack->ga_len;
1645
1646 return OK;
1647}
1648
1649/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001650 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001651 * "lambda_name" and "func_name" must be in allocated memory and will be
1652 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001653 */
1654 static int
1655generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1656{
1657 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001658
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001659 if (cctx->ctx_skip == SKIP_YES)
1660 {
1661 vim_free(lambda_name);
1662 vim_free(func_name);
1663 return OK;
1664 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001665 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001666 {
1667 vim_free(lambda_name);
1668 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001669 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001670 }
1671 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001672 isn->isn_arg.newfunc.nf_global = func_name;
1673
1674 return OK;
1675}
1676
1677/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001678 * Generate an ISN_DEF instruction: list functions
1679 */
1680 static int
1681generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1682{
1683 isn_T *isn;
1684
1685 RETURN_OK_IF_SKIP(cctx);
1686 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1687 return FAIL;
1688 if (len > 0)
1689 {
1690 isn->isn_arg.string = vim_strnsave(name, len);
1691 if (isn->isn_arg.string == NULL)
1692 return FAIL;
1693 }
1694 return OK;
1695}
1696
1697/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 * Generate an ISN_JUMP instruction.
1699 */
1700 static int
1701generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1702{
1703 isn_T *isn;
1704 garray_T *stack = &cctx->ctx_type_stack;
1705
Bram Moolenaar080457c2020-03-03 21:53:32 +01001706 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1708 return FAIL;
1709 isn->isn_arg.jump.jump_when = when;
1710 isn->isn_arg.jump.jump_where = where;
1711
1712 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1713 --stack->ga_len;
1714
1715 return OK;
1716}
1717
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001718/*
1719 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1720 */
1721 static int
1722generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1723{
1724 isn_T *isn;
1725
1726 RETURN_OK_IF_SKIP(cctx);
1727 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1728 return FAIL;
1729 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1730 // jump_where is set later
1731 return OK;
1732}
1733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 static int
1735generate_FOR(cctx_T *cctx, int loop_idx)
1736{
1737 isn_T *isn;
1738 garray_T *stack = &cctx->ctx_type_stack;
1739
Bram Moolenaar080457c2020-03-03 21:53:32 +01001740 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1742 return FAIL;
1743 isn->isn_arg.forloop.for_idx = loop_idx;
1744
1745 if (ga_grow(stack, 1) == FAIL)
1746 return FAIL;
1747 // type doesn't matter, will be stored next
1748 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1749 ++stack->ga_len;
1750
1751 return OK;
1752}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001753/*
1754 * Generate an ISN_TRYCONT instruction.
1755 */
1756 static int
1757generate_TRYCONT(cctx_T *cctx, int levels, int where)
1758{
1759 isn_T *isn;
1760
1761 RETURN_OK_IF_SKIP(cctx);
1762 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1763 return FAIL;
1764 isn->isn_arg.trycont.tct_levels = levels;
1765 isn->isn_arg.trycont.tct_where = where;
1766
1767 return OK;
1768}
1769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770
1771/*
1772 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001773 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 * Return FAIL if the number of arguments is wrong.
1775 */
1776 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001777generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778{
1779 isn_T *isn;
1780 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001781 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001782 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001783 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784
Bram Moolenaar080457c2020-03-03 21:53:32 +01001785 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001786 argoff = check_internal_func(func_idx, argcount);
1787 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 return FAIL;
1789
Bram Moolenaar389df252020-07-09 21:20:47 +02001790 if (method_call && argoff > 1)
1791 {
1792 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1793 return FAIL;
1794 isn->isn_arg.shuffle.shfl_item = argcount;
1795 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1796 }
1797
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001798 if (argcount > 0)
1799 {
1800 // Check the types of the arguments.
1801 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001802 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1803 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001804 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001805 if (internal_func_is_map(func_idx))
1806 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001807 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1810 return FAIL;
1811 isn->isn_arg.bfunc.cbf_idx = func_idx;
1812 isn->isn_arg.bfunc.cbf_argcount = argcount;
1813
Bram Moolenaar94738d82020-10-21 14:25:07 +02001814 // Drop the argument types and push the return type.
1815 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 if (ga_grow(stack, 1) == FAIL)
1817 return FAIL;
1818 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001819 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001820 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001822 if (maptype != NULL && maptype->tt_member != NULL
1823 && maptype->tt_member != &t_any)
1824 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001825 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 return OK;
1828}
1829
1830/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001831 * Generate an ISN_LISTAPPEND instruction. Works like add().
1832 * Argument count is already checked.
1833 */
1834 static int
1835generate_LISTAPPEND(cctx_T *cctx)
1836{
1837 garray_T *stack = &cctx->ctx_type_stack;
1838 type_T *list_type;
1839 type_T *item_type;
1840 type_T *expected;
1841
1842 // Caller already checked that list_type is a list.
1843 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1844 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1845 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001846 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001847 return FAIL;
1848
1849 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1850 return FAIL;
1851
1852 --stack->ga_len; // drop the argument
1853 return OK;
1854}
1855
1856/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001857 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1858 * Argument count is already checked.
1859 */
1860 static int
1861generate_BLOBAPPEND(cctx_T *cctx)
1862{
1863 garray_T *stack = &cctx->ctx_type_stack;
1864 type_T *item_type;
1865
1866 // Caller already checked that blob_type is a blob.
1867 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001868 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001869 return FAIL;
1870
1871 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1872 return FAIL;
1873
1874 --stack->ga_len; // drop the argument
1875 return OK;
1876}
1877
1878/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001879 * Return TRUE if "ufunc" should be compiled, taking into account whether
1880 * "profile" indicates profiling is to be done.
1881 */
1882 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001883func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001884{
1885 switch (ufunc->uf_def_status)
1886 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001887 case UF_TO_BE_COMPILED:
1888 return TRUE;
1889
Bram Moolenaarb2049902021-01-24 12:53:53 +01001890 case UF_COMPILED:
1891 {
1892 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1893 + ufunc->uf_dfunc_idx;
1894
Bram Moolenaare99d4222021-06-13 14:01:26 +02001895 switch (compile_type)
1896 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001897 case CT_PROFILE:
1898#ifdef FEAT_PROFILE
1899 return dfunc->df_instr_prof == NULL;
1900#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001901 case CT_NONE:
1902 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001903 case CT_DEBUG:
1904 return dfunc->df_instr_debug == NULL;
1905 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001906 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001907
1908 case UF_NOT_COMPILED:
1909 case UF_COMPILE_ERROR:
1910 case UF_COMPILING:
1911 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001912 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001913 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001914}
1915
1916/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 * Generate an ISN_DCALL or ISN_UCALL instruction.
1918 * Return FAIL if the number of arguments is wrong.
1919 */
1920 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001921generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922{
1923 isn_T *isn;
1924 garray_T *stack = &cctx->ctx_type_stack;
1925 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001926 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927
Bram Moolenaar080457c2020-03-03 21:53:32 +01001928 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 if (argcount > regular_args && !has_varargs(ufunc))
1930 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001931 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 return FAIL;
1933 }
1934 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1935 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001936 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 return FAIL;
1938 }
1939
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001940 if (ufunc->uf_def_status != UF_NOT_COMPILED
1941 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001942 {
1943 int i;
1944
1945 for (i = 0; i < argcount; ++i)
1946 {
1947 type_T *expected;
1948 type_T *actual;
1949
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001950 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1951 if (actual == &t_special
1952 && i >= regular_args - ufunc->uf_def_args.ga_len)
1953 {
1954 // assume v:none used for default argument value
1955 continue;
1956 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001957 if (i < regular_args)
1958 {
1959 if (ufunc->uf_arg_types == NULL)
1960 continue;
1961 expected = ufunc->uf_arg_types[i];
1962 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001963 else if (ufunc->uf_va_type == NULL
1964 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001965 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001966 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001967 else
1968 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001969 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001970 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001971 {
1972 arg_type_mismatch(expected, actual, i + 1);
1973 return FAIL;
1974 }
1975 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02001976 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001977 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02001978 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001979 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001980 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02001981 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1982 {
1983 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1984 ufunc->uf_name);
1985 return FAIL;
1986 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001989 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001990 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001992 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 {
1994 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1995 isn->isn_arg.dfunc.cdf_argcount = argcount;
1996 }
1997 else
1998 {
1999 // A user function may be deleted and redefined later, can't use the
2000 // ufunc pointer, need to look it up again at runtime.
2001 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2002 isn->isn_arg.ufunc.cuf_argcount = argcount;
2003 }
2004
2005 stack->ga_len -= argcount; // drop the arguments
2006 if (ga_grow(stack, 1) == FAIL)
2007 return FAIL;
2008 // add return value
2009 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2010 ++stack->ga_len;
2011
2012 return OK;
2013}
2014
2015/*
2016 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2017 */
2018 static int
2019generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2020{
2021 isn_T *isn;
2022 garray_T *stack = &cctx->ctx_type_stack;
2023
Bram Moolenaar080457c2020-03-03 21:53:32 +01002024 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2026 return FAIL;
2027 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2028 isn->isn_arg.ufunc.cuf_argcount = argcount;
2029
2030 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002031 if (ga_grow(stack, 1) == FAIL)
2032 return FAIL;
2033 // add return value
2034 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2035 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036
2037 return OK;
2038}
2039
2040/*
2041 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002042 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 */
2044 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002045generate_PCALL(
2046 cctx_T *cctx,
2047 int argcount,
2048 char_u *name,
2049 type_T *type,
2050 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051{
2052 isn_T *isn;
2053 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002054 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055
Bram Moolenaar080457c2020-03-03 21:53:32 +01002056 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002057
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002058 if (type->tt_type == VAR_ANY)
2059 ret_type = &t_any;
2060 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002061 {
2062 if (type->tt_argcount != -1)
2063 {
2064 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2065
2066 if (argcount < type->tt_min_argcount - varargs)
2067 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002068 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002069 return FAIL;
2070 }
2071 if (!varargs && argcount > type->tt_argcount)
2072 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002073 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002074 return FAIL;
2075 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002076 if (type->tt_args != NULL)
2077 {
2078 int i;
2079
2080 for (i = 0; i < argcount; ++i)
2081 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002082 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002083 type_T *actual = ((type_T **)stack->ga_data)[
2084 stack->ga_len + offset];
2085 type_T *expected;
2086
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002087 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002088 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002089 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002090 else if (i >= type->tt_min_argcount
2091 && actual == &t_special)
2092 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002093 else
2094 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002095 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002096 cctx, TRUE, FALSE) == FAIL)
2097 {
2098 arg_type_mismatch(expected, actual, i + 1);
2099 return FAIL;
2100 }
2101 }
2102 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002103 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002104 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002105 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002106 else
2107 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002108 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002109 return FAIL;
2110 }
2111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2113 return FAIL;
2114 isn->isn_arg.pfunc.cpf_top = at_top;
2115 isn->isn_arg.pfunc.cpf_argcount = argcount;
2116
2117 stack->ga_len -= argcount; // drop the arguments
2118
2119 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002120 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002122 // If partial is above the arguments it must be cleared and replaced with
2123 // the return value.
2124 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2125 return FAIL;
2126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 return OK;
2128}
2129
2130/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002131 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 */
2133 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002134generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135{
2136 isn_T *isn;
2137 garray_T *stack = &cctx->ctx_type_stack;
2138 type_T *type;
2139
Bram Moolenaar080457c2020-03-03 21:53:32 +01002140 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002141 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002143 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002145 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002147 if (type->tt_type != VAR_DICT && type != &t_any)
2148 {
2149 emsg(_(e_dictreq));
2150 return FAIL;
2151 }
2152 // change dict type to dict member type
2153 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002154 {
2155 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2156 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158
2159 return OK;
2160}
2161
2162/*
2163 * Generate an ISN_ECHO instruction.
2164 */
2165 static int
2166generate_ECHO(cctx_T *cctx, int with_white, int count)
2167{
2168 isn_T *isn;
2169
Bram Moolenaar080457c2020-03-03 21:53:32 +01002170 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2172 return FAIL;
2173 isn->isn_arg.echo.echo_with_white = with_white;
2174 isn->isn_arg.echo.echo_count = count;
2175
2176 return OK;
2177}
2178
Bram Moolenaarad39c092020-02-26 18:23:43 +01002179/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002180 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002181 */
2182 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002183generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002184{
2185 isn_T *isn;
2186
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002187 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002188 return FAIL;
2189 isn->isn_arg.number = count;
2190
2191 return OK;
2192}
2193
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002194/*
2195 * Generate an ISN_PUT instruction.
2196 */
2197 static int
2198generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2199{
2200 isn_T *isn;
2201
2202 RETURN_OK_IF_SKIP(cctx);
2203 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2204 return FAIL;
2205 isn->isn_arg.put.put_regname = regname;
2206 isn->isn_arg.put.put_lnum = lnum;
2207 return OK;
2208}
2209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 static int
2211generate_EXEC(cctx_T *cctx, char_u *line)
2212{
2213 isn_T *isn;
2214
Bram Moolenaar080457c2020-03-03 21:53:32 +01002215 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2217 return FAIL;
2218 isn->isn_arg.string = vim_strsave(line);
2219 return OK;
2220}
2221
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002222 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002223generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2224{
2225 isn_T *isn;
2226 garray_T *stack = &cctx->ctx_type_stack;
2227
2228 RETURN_OK_IF_SKIP(cctx);
2229 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2230 return FAIL;
2231 isn->isn_arg.string = vim_strsave(line);
2232
2233 if (ga_grow(stack, 1) == FAIL)
2234 return FAIL;
2235 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2236 ++stack->ga_len;
2237
2238 return OK;
2239}
2240
2241 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002242generate_EXECCONCAT(cctx_T *cctx, int count)
2243{
2244 isn_T *isn;
2245
2246 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2247 return FAIL;
2248 isn->isn_arg.number = count;
2249 return OK;
2250}
2251
Bram Moolenaar08597872020-12-10 19:43:40 +01002252/*
2253 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2254 */
2255 static int
2256generate_RANGE(cctx_T *cctx, char_u *range)
2257{
2258 isn_T *isn;
2259 garray_T *stack = &cctx->ctx_type_stack;
2260
2261 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2262 return FAIL;
2263 isn->isn_arg.string = range;
2264
2265 if (ga_grow(stack, 1) == FAIL)
2266 return FAIL;
2267 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2268 ++stack->ga_len;
2269 return OK;
2270}
2271
Bram Moolenaar792f7862020-11-23 08:31:18 +01002272 static int
2273generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2274{
2275 isn_T *isn;
2276
2277 RETURN_OK_IF_SKIP(cctx);
2278 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2279 return FAIL;
2280 isn->isn_arg.unpack.unp_count = var_count;
2281 isn->isn_arg.unpack.unp_semicolon = semicolon;
2282 return OK;
2283}
2284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002286 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002287 */
2288 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002289generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002290{
2291 isn_T *isn;
2292
Bram Moolenaarfa984412021-03-25 22:15:28 +01002293 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002294 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002295 cctx->ctx_has_cmdmod = TRUE;
2296
2297 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002298 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002299 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2300 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2301 return FAIL;
2302 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002303 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002304 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002305 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002306
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002307 return OK;
2308}
2309
2310 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002311generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002312{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002313 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2314 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002315 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002316 return OK;
2317}
2318
Bram Moolenaarfa984412021-03-25 22:15:28 +01002319 static int
2320misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002321{
2322 garray_T *instr = &cctx->ctx_instr;
2323
Bram Moolenaara91a7132021-03-25 21:12:15 +01002324 if (cctx->ctx_has_cmdmod
2325 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2326 == ISN_CMDMOD)
2327 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002328 emsg(_(e_misplaced_command_modifier));
2329 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002330 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002331 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002332}
2333
2334/*
2335 * Get the index of the current instruction.
2336 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2337 */
2338 static int
2339current_instr_idx(cctx_T *cctx)
2340{
2341 garray_T *instr = &cctx->ctx_instr;
2342 int idx = instr->ga_len;
2343
Bram Moolenaare99d4222021-06-13 14:01:26 +02002344 while (idx > 0)
2345 {
2346 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002347 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002348 {
2349 --idx;
2350 continue;
2351 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002352#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002353 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2354 {
2355 --idx;
2356 continue;
2357 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002358#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002359 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2360 {
2361 --idx;
2362 continue;
2363 }
2364 break;
2365 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002366 return idx;
2367}
2368
Bram Moolenaarf002a412021-01-24 13:34:18 +01002369#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002370 static void
2371may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2372{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002373 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002374 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002375}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002376#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002377
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002378/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002380 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002382 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002383reserve_local(
2384 cctx_T *cctx,
2385 char_u *name,
2386 size_t len,
2387 int isConst,
2388 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002391 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002393 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002395 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002396 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 }
2398
2399 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002400 return NULL;
2401 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002402 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002404 // Every local variable uses the next entry on the stack. We could re-use
2405 // the last ones when leaving a scope, but then variables used in a closure
2406 // might get overwritten. To keep things simple do not re-use stack
2407 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002408 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2409 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002410
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002411 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 lvar->lv_const = isConst;
2413 lvar->lv_type = type;
2414
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002415 // Remember the name for debugging.
2416 if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2417 return NULL;
2418 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2419 vim_strsave(lvar->lv_name);
2420 ++dfunc->df_var_names.ga_len;
2421
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002422 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423}
2424
2425/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002426 * Remove local variables above "new_top".
2427 */
2428 static void
2429unwind_locals(cctx_T *cctx, int new_top)
2430{
2431 if (cctx->ctx_locals.ga_len > new_top)
2432 {
2433 int idx;
2434 lvar_T *lvar;
2435
2436 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2437 {
2438 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2439 vim_free(lvar->lv_name);
2440 }
2441 }
2442 cctx->ctx_locals.ga_len = new_top;
2443}
2444
2445/*
2446 * Free all local variables.
2447 */
2448 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002449free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002450{
2451 unwind_locals(cctx, 0);
2452 ga_clear(&cctx->ctx_locals);
2453}
2454
2455/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002456 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2457 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2458 * error if the variable was defined with :const.
2459 */
2460 static int
2461check_item_writable(svar_T *sv, int check_writable, char_u *name)
2462{
2463 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2464 || (check_writable == ASSIGN_FINAL
2465 && sv->sv_const == ASSIGN_CONST))
2466 {
2467 semsg(_(e_readonlyvar), name);
2468 return FAIL;
2469 }
2470 return OK;
2471}
2472
2473/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002475 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 * Returns the index in "sn_var_vals" if found.
2477 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002478 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 */
2480 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002481get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482{
2483 hashtab_T *ht;
2484 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002485 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002486 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 int idx;
2488
Bram Moolenaare3d46852020-08-29 13:39:17 +02002489 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002491 if (sid == current_sctx.sc_sid)
2492 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002493 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002494
2495 if (sav == NULL)
2496 return -2;
2497 idx = sav->sav_var_vals_idx;
2498 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002499 if (check_item_writable(sv, check_writable, name) == FAIL)
2500 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002501 return idx;
2502 }
2503
2504 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 ht = &SCRIPT_VARS(sid);
2506 di = find_var_in_ht(ht, 0, name, TRUE);
2507 if (di == NULL)
2508 return -2;
2509
2510 // Now find the svar_T index in sn_var_vals.
2511 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2512 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002513 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 if (sv->sv_tv == &di->di_tv)
2515 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002516 if (check_item_writable(sv, check_writable, name) == FAIL)
2517 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 return idx;
2519 }
2520 }
2521 return -1;
2522}
2523
2524/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002525 * Find "name" in imported items of the current script or in "cctx" if not
2526 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 */
2528 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002529find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 int idx;
2532
Bram Moolenaare3d46852020-08-29 13:39:17 +02002533 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002534 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 if (cctx != NULL)
2536 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2537 {
2538 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2539 + idx;
2540
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002541 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2542 : STRLEN(import->imp_name) == len
2543 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 return import;
2545 }
2546
Bram Moolenaarefa94442020-08-08 22:16:00 +02002547 return find_imported_in_script(name, len, current_sctx.sc_sid);
2548}
2549
2550 imported_T *
2551find_imported_in_script(char_u *name, size_t len, int sid)
2552{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002553 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002554 int idx;
2555
Bram Moolenaare3d46852020-08-29 13:39:17 +02002556 if (!SCRIPT_ID_VALID(sid))
2557 return NULL;
2558 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2560 {
2561 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2562
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002563 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2564 : STRLEN(import->imp_name) == len
2565 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 return import;
2567 }
2568 return NULL;
2569}
2570
2571/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002572 * Free all imported variables.
2573 */
2574 static void
2575free_imported(cctx_T *cctx)
2576{
2577 int idx;
2578
2579 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2580 {
2581 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2582
2583 vim_free(import->imp_name);
2584 }
2585 ga_clear(&cctx->ctx_imports);
2586}
2587
2588/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002589 * Return a pointer to the next line that isn't empty or only contains a
2590 * comment. Skips over white space.
2591 * Returns NULL if there is none.
2592 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002593 char_u *
2594peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002595{
2596 int lnum = cctx->ctx_lnum;
2597
2598 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2599 {
2600 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002601 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002602
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002603 // ignore NULLs inserted for continuation lines
2604 if (line != NULL)
2605 {
2606 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002607 if (vim9_bad_comment(p))
2608 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002609 if (*p != NUL && !vim9_comment_start(p))
2610 return p;
2611 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002612 }
2613 return NULL;
2614}
2615
2616/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002617 * Called when checking for a following operator at "arg". When the rest of
2618 * the line is empty or only a comment, peek the next line. If there is a next
2619 * line return a pointer to it and set "nextp".
2620 * Otherwise skip over white space.
2621 */
2622 static char_u *
2623may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2624{
2625 char_u *p = skipwhite(arg);
2626
2627 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002628 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002629 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002630 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002631 if (*nextp != NULL)
2632 return *nextp;
2633 }
2634 return p;
2635}
2636
2637/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002638 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002639 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002640 * Returns NULL when at the end.
2641 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002642 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002643next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002644{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002645 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002646
2647 do
2648 {
2649 ++cctx->ctx_lnum;
2650 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002651 {
2652 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002653 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002654 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002655 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002656 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002657 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002658 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002659 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002660 return line;
2661}
2662
2663/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002664 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002665 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002666 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002667 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2668 */
2669 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002670may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002671{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002672 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002673 if (vim9_bad_comment(*arg))
2674 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002675 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002676 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002677 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002678
2679 if (next == NULL)
2680 return FAIL;
2681 *arg = skipwhite(next);
2682 }
2683 return OK;
2684}
2685
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002686/*
2687 * Idem, and give an error when failed.
2688 */
2689 static int
2690may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2691{
2692 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2693 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002694 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002695 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002696 return FAIL;
2697 }
2698 return OK;
2699}
2700
2701
Bram Moolenaara5565e42020-05-09 15:44:01 +02002702// Structure passed between the compile_expr* functions to keep track of
2703// constants that have been parsed but for which no code was produced yet. If
2704// possible expressions on these constants are applied at compile time. If
2705// that is not possible, the code to push the constants needs to be generated
2706// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002707// Using 50 should be more than enough of 5 levels of ().
2708#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002709typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002710 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002711 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002712 int pp_is_const; // all generated code was constants, used for a
2713 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002714} ppconst_T;
2715
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002716static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002717static int compile_expr0(char_u **arg, cctx_T *cctx);
2718static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2719
Bram Moolenaara5565e42020-05-09 15:44:01 +02002720/*
2721 * Generate a PUSH instruction for "tv".
2722 * "tv" will be consumed or cleared.
2723 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2724 */
2725 static int
2726generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2727{
2728 if (tv != NULL)
2729 {
2730 switch (tv->v_type)
2731 {
2732 case VAR_UNKNOWN:
2733 break;
2734 case VAR_BOOL:
2735 generate_PUSHBOOL(cctx, tv->vval.v_number);
2736 break;
2737 case VAR_SPECIAL:
2738 generate_PUSHSPEC(cctx, tv->vval.v_number);
2739 break;
2740 case VAR_NUMBER:
2741 generate_PUSHNR(cctx, tv->vval.v_number);
2742 break;
2743#ifdef FEAT_FLOAT
2744 case VAR_FLOAT:
2745 generate_PUSHF(cctx, tv->vval.v_float);
2746 break;
2747#endif
2748 case VAR_BLOB:
2749 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2750 tv->vval.v_blob = NULL;
2751 break;
2752 case VAR_STRING:
2753 generate_PUSHS(cctx, tv->vval.v_string);
2754 tv->vval.v_string = NULL;
2755 break;
2756 default:
2757 iemsg("constant type not supported");
2758 clear_tv(tv);
2759 return FAIL;
2760 }
2761 tv->v_type = VAR_UNKNOWN;
2762 }
2763 return OK;
2764}
2765
2766/*
2767 * Generate code for any ppconst entries.
2768 */
2769 static int
2770generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2771{
2772 int i;
2773 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002774 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002775
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002776 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002777 for (i = 0; i < ppconst->pp_used; ++i)
2778 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2779 ret = FAIL;
2780 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002781 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002782 return ret;
2783}
2784
2785/*
2786 * Clear ppconst constants. Used when failing.
2787 */
2788 static void
2789clear_ppconst(ppconst_T *ppconst)
2790{
2791 int i;
2792
2793 for (i = 0; i < ppconst->pp_used; ++i)
2794 clear_tv(&ppconst->pp_tv[i]);
2795 ppconst->pp_used = 0;
2796}
2797
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002798/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002799 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002800 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002801 */
2802 static int
2803compile_member(int is_slice, cctx_T *cctx)
2804{
2805 type_T **typep;
2806 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002807 vartype_T vartype;
2808 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002809
Bram Moolenaar261417b2021-05-06 21:04:55 +02002810 // We can index a list, dict and blob. If we don't know the type
2811 // we can use the index value type. If we still don't know use an "ANY"
2812 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002813 typep = ((type_T **)stack->ga_data) + stack->ga_len
2814 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002815 vartype = (*typep)->tt_type;
2816 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002817 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002818 if (*typep == &t_any && idxtype == &t_string)
2819 vartype = VAR_DICT;
2820 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002821 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002822 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002823 return FAIL;
2824 if (is_slice)
2825 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002826 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2827 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002828 FALSE, FALSE) == FAIL)
2829 return FAIL;
2830 }
2831 }
2832
Bram Moolenaar261417b2021-05-06 21:04:55 +02002833 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002834 {
2835 if (is_slice)
2836 {
2837 emsg(_(e_cannot_slice_dictionary));
2838 return FAIL;
2839 }
2840 if ((*typep)->tt_type == VAR_DICT)
2841 {
2842 *typep = (*typep)->tt_member;
2843 if (*typep == &t_unknown)
2844 // empty dict was used
2845 *typep = &t_any;
2846 }
2847 else
2848 {
2849 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2850 FALSE, FALSE) == FAIL)
2851 return FAIL;
2852 *typep = &t_any;
2853 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002854 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002855 return FAIL;
2856 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2857 return FAIL;
2858 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002859 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002860 {
2861 *typep = &t_string;
2862 if ((is_slice
2863 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2864 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2865 return FAIL;
2866 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002867 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002868 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002869 if (is_slice)
2870 {
2871 *typep = &t_blob;
2872 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2873 return FAIL;
2874 }
2875 else
2876 {
2877 *typep = &t_number;
2878 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2879 return FAIL;
2880 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002881 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002882 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002883 {
2884 if (is_slice)
2885 {
2886 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002887 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002888 2) == FAIL)
2889 return FAIL;
2890 }
2891 else
2892 {
2893 if ((*typep)->tt_type == VAR_LIST)
2894 {
2895 *typep = (*typep)->tt_member;
2896 if (*typep == &t_unknown)
2897 // empty list was used
2898 *typep = &t_any;
2899 }
2900 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002901 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2902 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002903 return FAIL;
2904 }
2905 }
2906 else
2907 {
2908 emsg(_(e_string_list_dict_or_blob_required));
2909 return FAIL;
2910 }
2911 return OK;
2912}
2913
2914/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002915 * Generate an instruction to load script-local variable "name", without the
2916 * leading "s:".
2917 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 */
2919 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002920compile_load_scriptvar(
2921 cctx_T *cctx,
2922 char_u *name, // variable NUL terminated
2923 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002924 char_u **end, // end of variable
2925 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002927 scriptitem_T *si;
2928 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 imported_T *import;
2930
Bram Moolenaare3d46852020-08-29 13:39:17 +02002931 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2932 return FAIL;
2933 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002934 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002935 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002937 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002938 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2939 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 }
2941 if (idx >= 0)
2942 {
2943 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2944
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002945 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 current_sctx.sc_sid, idx, sv->sv_type);
2947 return OK;
2948 }
2949
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002950 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 if (import != NULL)
2952 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002953 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002954 {
2955 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002956 char_u *exp_name;
2957 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002958 ufunc_T *ufunc;
2959 type_T *type;
2960
2961 // Used "import * as Name", need to lookup the member.
2962 if (*p != '.')
2963 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002964 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002965 return FAIL;
2966 }
2967 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002968 if (VIM_ISWHITE(*p))
2969 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002970 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002971 return FAIL;
2972 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002973
Bram Moolenaar1c991142020-07-04 13:15:31 +02002974 // isolate one name
2975 exp_name = p;
2976 while (eval_isnamec(*p))
2977 ++p;
2978 cc = *p;
2979 *p = NUL;
2980
Bram Moolenaaredba7072021-03-13 21:14:18 +01002981 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2982 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002983 *p = cc;
2984 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002985 *end = p;
2986
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002987 if (idx < 0)
2988 {
2989 if (*p == '(' && ufunc != NULL)
2990 {
2991 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2992 return OK;
2993 }
2994 return FAIL;
2995 }
2996
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002997 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2998 import->imp_sid,
2999 idx,
3000 type);
3001 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003002 else if (import->imp_funcname != NULL)
3003 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003004 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003005 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3006 import->imp_sid,
3007 import->imp_var_vals_idx,
3008 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 return OK;
3010 }
3011
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003012 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003013 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 return FAIL;
3015}
3016
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003017 static int
3018generate_funcref(cctx_T *cctx, char_u *name)
3019{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003020 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003021
3022 if (ufunc == NULL)
3023 return FAIL;
3024
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003025 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003026 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3027 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003028 == FAIL)
3029 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003030 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003031}
3032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033/*
3034 * Compile a variable name into a load instruction.
3035 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003036 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 * When "error" is FALSE do not give an error when not found.
3038 */
3039 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003040compile_load(
3041 char_u **arg,
3042 char_u *end_arg,
3043 cctx_T *cctx,
3044 int is_expr,
3045 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046{
3047 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003048 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003049 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003051 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052
3053 if (*(*arg + 1) == ':')
3054 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003055 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003056 {
3057 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058
Bram Moolenaarfa596382021-04-07 21:58:16 +02003059 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003060 switch (**arg)
3061 {
3062 case 'g': isn_type = ISN_LOADGDICT; break;
3063 case 'w': isn_type = ISN_LOADWDICT; break;
3064 case 't': isn_type = ISN_LOADTDICT; break;
3065 case 'b': isn_type = ISN_LOADBDICT; break;
3066 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003067 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003068 goto theend;
3069 }
3070 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3071 goto theend;
3072 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 else
3075 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003076 isntype_T isn_type = ISN_DROP;
3077
Bram Moolenaarfa596382021-04-07 21:58:16 +02003078 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003079 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3080 if (name == NULL)
3081 return FAIL;
3082
3083 switch (**arg)
3084 {
3085 case 'v': res = generate_LOADV(cctx, name, error);
3086 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003087 case 's': if (is_expr && ASCII_ISUPPER(*name)
3088 && find_func(name, FALSE, cctx) != NULL)
3089 res = generate_funcref(cctx, name);
3090 else
3091 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003092 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003093 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003094 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003095 {
3096 if (is_expr && ASCII_ISUPPER(*name)
3097 && find_func(name, FALSE, cctx) != NULL)
3098 res = generate_funcref(cctx, name);
3099 else
3100 isn_type = ISN_LOADG;
3101 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003102 else
3103 {
3104 isn_type = ISN_LOADAUTO;
3105 vim_free(name);
3106 name = vim_strnsave(*arg, end - *arg);
3107 if (name == NULL)
3108 return FAIL;
3109 }
3110 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003111 case 'w': isn_type = ISN_LOADW; break;
3112 case 't': isn_type = ISN_LOADT; break;
3113 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003114 default: // cannot happen, just in case
3115 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003116 goto theend;
3117 }
3118 if (isn_type != ISN_DROP)
3119 {
3120 // Global, Buffer-local, Window-local and Tabpage-local
3121 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003122 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003123 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 }
3126 }
3127 else
3128 {
3129 size_t len = end - *arg;
3130 int idx;
3131 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003132 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133
3134 name = vim_strnsave(*arg, end - *arg);
3135 if (name == NULL)
3136 return FAIL;
3137
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003138 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3139 {
3140 script_autoload(name, FALSE);
3141 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3142 }
3143 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3144 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003146 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003147 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 }
3149 else
3150 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003151 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003152
Bram Moolenaar709664c2020-12-12 14:33:41 +01003153 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003155 type = lvar.lv_type;
3156 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003157 if (lvar.lv_from_outer != 0)
3158 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003159 else
3160 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 }
3162 else
3163 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003164 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003165 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003166 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003167 || find_imported(name, 0, cctx) != NULL)
3168 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003169
Bram Moolenaar0f769812020-09-12 18:32:34 +02003170 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003171 // uppercase letter it can be a user defined function.
3172 // generate_funcref() will fail if the function can't be found.
3173 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003174 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 }
3176 }
3177 if (gen_load)
3178 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003179 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003180 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003181 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003182 cctx->ctx_outer_used = TRUE;
3183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 }
3185
3186 *arg = end;
3187
3188theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003189 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003190 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 vim_free(name);
3192 return res;
3193}
3194
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003195 static void
3196clear_instr_ga(garray_T *gap)
3197{
3198 int idx;
3199
3200 for (idx = 0; idx < gap->ga_len; ++idx)
3201 delete_instr(((isn_T *)gap->ga_data) + idx);
3202 ga_clear(gap);
3203}
3204
3205/*
3206 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3207 * Returns FAIL if compilation fails.
3208 */
3209 static int
3210compile_string(isn_T *isn, cctx_T *cctx)
3211{
3212 char_u *s = isn->isn_arg.string;
3213 garray_T save_ga = cctx->ctx_instr;
3214 int expr_res;
3215 int trailing_error;
3216 int instr_count;
3217 isn_T *instr = NULL;
3218
3219 // Temporarily reset the list of instructions so that the jump labels are
3220 // correct.
3221 cctx->ctx_instr.ga_len = 0;
3222 cctx->ctx_instr.ga_maxlen = 0;
3223 cctx->ctx_instr.ga_data = NULL;
3224 expr_res = compile_expr0(&s, cctx);
3225 s = skipwhite(s);
3226 trailing_error = *s != NUL;
3227
Bram Moolenaarff652882021-05-16 15:24:49 +02003228 if (expr_res == FAIL || trailing_error
3229 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003230 {
3231 if (trailing_error)
3232 semsg(_(e_trailing_arg), s);
3233 clear_instr_ga(&cctx->ctx_instr);
3234 cctx->ctx_instr = save_ga;
3235 return FAIL;
3236 }
3237
3238 // Move the generated instructions into the ISN_INSTR instruction, then
3239 // restore the list of instructions.
3240 instr_count = cctx->ctx_instr.ga_len;
3241 instr = cctx->ctx_instr.ga_data;
3242 instr[instr_count].isn_type = ISN_FINISH;
3243
3244 cctx->ctx_instr = save_ga;
3245 vim_free(isn->isn_arg.string);
3246 isn->isn_type = ISN_INSTR;
3247 isn->isn_arg.instr = instr;
3248 return OK;
3249}
3250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251/*
3252 * Compile the argument expressions.
3253 * "arg" points to just after the "(" and is advanced to after the ")"
3254 */
3255 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003256compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003258 char_u *p = *arg;
3259 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003260 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003261 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262
Bram Moolenaare6085c52020-04-12 20:19:16 +02003263 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003265 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3266 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003267 if (*p == ')')
3268 {
3269 *arg = p + 1;
3270 return OK;
3271 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003272 if (must_end)
3273 {
3274 semsg(_(e_missing_comma_before_argument_str), p);
3275 return FAIL;
3276 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003277
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003278 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003279 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 return FAIL;
3281 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003282
Bram Moolenaarff652882021-05-16 15:24:49 +02003283 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003284 && cctx->ctx_instr.ga_len == instr_count + 1)
3285 {
3286 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3287
3288 // {skip} argument of searchpair() can be compiled if not empty
3289 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3290 compile_string(isn, cctx);
3291 }
3292
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003293 if (*p != ',' && *skipwhite(p) == ',')
3294 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003295 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003296 p = skipwhite(p);
3297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003299 {
3300 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003301 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003302 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003303 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003304 else
3305 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003306 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003307 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003309failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003310 emsg(_(e_missing_close));
3311 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312}
3313
3314/*
3315 * Compile a function call: name(arg1, arg2)
3316 * "arg" points to "name", "arg + varlen" to the "(".
3317 * "argcount_init" is 1 for "value->method()"
3318 * Instructions:
3319 * EVAL arg1
3320 * EVAL arg2
3321 * BCALL / DCALL / UCALL
3322 */
3323 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003324compile_call(
3325 char_u **arg,
3326 size_t varlen,
3327 cctx_T *cctx,
3328 ppconst_T *ppconst,
3329 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330{
3331 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003332 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 int argcount = argcount_init;
3334 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003335 char_u fname_buf[FLEN_FIXED + 1];
3336 char_u *tofree = NULL;
3337 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003338 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003339 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003340 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003341 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342
Bram Moolenaara5565e42020-05-09 15:44:01 +02003343 // we can evaluate "has('name')" at compile time
3344 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3345 {
3346 char_u *s = skipwhite(*arg + varlen + 1);
3347 typval_T argvars[2];
3348
3349 argvars[0].v_type = VAR_UNKNOWN;
3350 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003351 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003352 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003353 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003354 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003355 if (*s == ')' && argvars[0].v_type == VAR_STRING
3356 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003357 {
3358 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3359
3360 *arg = s + 1;
3361 argvars[1].v_type = VAR_UNKNOWN;
3362 tv->v_type = VAR_NUMBER;
3363 tv->vval.v_number = 0;
3364 f_has(argvars, tv);
3365 clear_tv(&argvars[0]);
3366 ++ppconst->pp_used;
3367 return OK;
3368 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003369 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003370 }
3371
3372 if (generate_ppconst(cctx, ppconst) == FAIL)
3373 return FAIL;
3374
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 if (varlen >= sizeof(namebuf))
3376 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003377 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 return FAIL;
3379 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003380 vim_strncpy(namebuf, *arg, varlen);
3381 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003383 // We handle the "skip" argument of searchpair() and searchpairpos()
3384 // differently.
3385 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3386 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3387 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3388 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003391 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003392 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393
Bram Moolenaar03290b82020-12-19 16:30:44 +01003394 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003395 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 {
3397 int idx;
3398
3399 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003400 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003402 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003403 if (STRCMP(name, "flatten") == 0)
3404 {
3405 emsg(_(e_cannot_use_flatten_in_vim9_script));
3406 goto theend;
3407 }
3408
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003409 if (STRCMP(name, "add") == 0 && argcount == 2)
3410 {
3411 garray_T *stack = &cctx->ctx_type_stack;
3412 type_T *type = ((type_T **)stack->ga_data)[
3413 stack->ga_len - 2];
3414
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003415 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003416 if (type->tt_type == VAR_LIST)
3417 {
3418 // inline "add(list, item)" so that the type can be checked
3419 res = generate_LISTAPPEND(cctx);
3420 idx = -1;
3421 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003422 else if (type->tt_type == VAR_BLOB)
3423 {
3424 // inline "add(blob, nr)" so that the type can be checked
3425 res = generate_BLOBAPPEND(cctx);
3426 idx = -1;
3427 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003428 }
3429
3430 if (idx >= 0)
3431 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3432 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003433 else
3434 semsg(_(e_unknownfunc), namebuf);
3435 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 }
3437
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003438 // An argument or local variable can be a function reference, this
3439 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003440 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003441 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003442 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003443 // If we can find the function by name generate the right call.
3444 // Skip global functions here, a local funcref takes precedence.
3445 ufunc = find_func(name, FALSE, cctx);
3446 if (ufunc != NULL && !func_is_global(ufunc))
3447 {
3448 res = generate_CALL(cctx, ufunc, argcount);
3449 goto theend;
3450 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003451 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452
3453 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003454 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003455 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003457 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003458 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003459 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003460 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003461 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003462
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003463 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003464 goto theend;
3465 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466
Bram Moolenaar0f769812020-09-12 18:32:34 +02003467 // If we can find a global function by name generate the right call.
3468 if (ufunc != NULL)
3469 {
3470 res = generate_CALL(cctx, ufunc, argcount);
3471 goto theend;
3472 }
3473
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003474 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003475 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003476 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003477 res = generate_UCALL(cctx, name, argcount);
3478 else
3479 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003480
3481theend:
3482 vim_free(tofree);
3483 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484}
3485
3486// like NAMESPACE_CHAR but with 'a' and 'l'.
3487#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3488
3489/*
3490 * Find the end of a variable or function name. Unlike find_name_end() this
3491 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003492 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 * Return a pointer to just after the name. Equal to "arg" if there is no
3494 * valid name.
3495 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003496 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003497to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498{
3499 char_u *p;
3500
3501 // Quick check for valid starting character.
3502 if (!eval_isnamec1(*arg))
3503 return arg;
3504
3505 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3506 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3507 // and can be used in slice "[n:]".
3508 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003509 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3511 break;
3512 return p;
3513}
3514
3515/*
3516 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003517 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 */
3519 char_u *
3520to_name_const_end(char_u *arg)
3521{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003522 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 typval_T rettv;
3524
3525 if (p == arg && *arg == '[')
3526 {
3527
3528 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003529 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 p = arg;
3531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 return p;
3533}
3534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535/*
3536 * parse a list: [expr, expr]
3537 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003538 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 */
3540 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003541compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542{
3543 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003544 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003546 int is_const;
3547 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003549 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003551 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003552 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003553 semsg(_(e_list_end), *arg);
3554 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003555 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003556 if (*p == ',')
3557 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003558 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003559 return FAIL;
3560 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003561 if (*p == ']')
3562 {
3563 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003564 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003565 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003566 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003567 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003568 if (!is_const)
3569 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 ++count;
3571 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003572 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003574 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3575 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003576 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003577 return FAIL;
3578 }
3579 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003580 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 p = skipwhite(p);
3582 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003583 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003585 ppconst->pp_is_const = is_all_const;
3586 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587}
3588
3589/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003590 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003591 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003592 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 */
3594 static int
3595compile_lambda(char_u **arg, cctx_T *cctx)
3596{
Bram Moolenaare462f522020-12-27 14:43:30 +01003597 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 typval_T rettv;
3599 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003600 evalarg_T evalarg;
3601
3602 CLEAR_FIELD(evalarg);
3603 evalarg.eval_flags = EVAL_EVALUATE;
3604 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605
3606 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003607 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3608 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003609 {
3610 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003611 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003612 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003613
Bram Moolenaar65c44152020-12-24 15:14:01 +01003614 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003616 ++ufunc->uf_refcount;
3617 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618
Bram Moolenaara9931532021-06-12 15:58:16 +02003619 // Compile it here to get the return type. The return type is optional,
3620 // when it's missing use t_unknown. This is recognized in
3621 // compile_return().
3622 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3623 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaare99d4222021-06-13 14:01:26 +02003624 compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003626 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3627 // points into it. Point to the original line to avoid a dangling pointer.
3628 if (evalarg.eval_tofree_cmdline != NULL)
3629 {
3630 size_t off = *arg - evalarg.eval_tofree_cmdline;
3631
3632 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3633 + off;
3634 }
3635
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003636 clear_evalarg(&evalarg, NULL);
3637
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003638 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003639 {
3640 // The return type will now be known.
3641 set_function_type(ufunc);
3642
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003643 // The function reference count will be 1. When the ISN_FUNCREF
3644 // instruction is deleted the reference count is decremented and the
3645 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003646 return generate_FUNCREF(cctx, ufunc);
3647 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003648
3649 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 return FAIL;
3651}
3652
3653/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003654 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003656 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 */
3658 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003659compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660{
3661 garray_T *instr = &cctx->ctx_instr;
3662 int count = 0;
3663 dict_T *d = dict_alloc();
3664 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003665 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003666 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003667 int is_const;
3668 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669
3670 if (d == NULL)
3671 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003672 if (generate_ppconst(cctx, ppconst) == FAIL)
3673 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003674 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003676 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677
Bram Moolenaar23c55272020-06-21 16:58:13 +02003678 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003679 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003680 *arg = NULL;
3681 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003682 }
3683
3684 if (**arg == '}')
3685 break;
3686
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003687 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003689 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003691 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003692 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003693 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003696 if (isn->isn_type == ISN_PUSHNR)
3697 {
3698 char buf[NUMBUFLEN];
3699
3700 // Convert to string at compile time.
3701 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3702 isn->isn_type = ISN_PUSHS;
3703 isn->isn_arg.string = vim_strsave((char_u *)buf);
3704 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 if (isn->isn_type == ISN_PUSHS)
3706 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003707 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003708 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003709 *arg = skipwhite(*arg);
3710 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003711 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003712 emsg(_(e_missing_matching_bracket_after_dict_key));
3713 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003714 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003715 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003717 else
3718 {
3719 // {"name": value},
3720 // {'name': value},
3721 // {name: value} use "name" as a literal key
3722 key = get_literal_key(arg);
3723 if (key == NULL)
3724 return FAIL;
3725 if (generate_PUSHS(cctx, key) == FAIL)
3726 return FAIL;
3727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728
3729 // Check for duplicate keys, if using string keys.
3730 if (key != NULL)
3731 {
3732 item = dict_find(d, key, -1);
3733 if (item != NULL)
3734 {
3735 semsg(_(e_duplicate_key), key);
3736 goto failret;
3737 }
3738 item = dictitem_alloc(key);
3739 if (item != NULL)
3740 {
3741 item->di_tv.v_type = VAR_UNKNOWN;
3742 item->di_tv.v_lock = 0;
3743 if (dict_add(d, item) == FAIL)
3744 dictitem_free(item);
3745 }
3746 }
3747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 if (**arg != ':')
3749 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003750 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003751 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003752 else
3753 semsg(_(e_missing_dict_colon), *arg);
3754 return FAIL;
3755 }
3756 whitep = *arg + 1;
3757 if (!IS_WHITE_OR_NUL(*whitep))
3758 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003759 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 return FAIL;
3761 }
3762
Bram Moolenaar23c55272020-06-21 16:58:13 +02003763 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003764 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003765 *arg = NULL;
3766 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003767 }
3768
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003769 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003771 if (!is_const)
3772 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 ++count;
3774
Bram Moolenaar2c330432020-04-13 14:41:35 +02003775 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003776 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003777 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003778 *arg = NULL;
3779 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 if (**arg == '}')
3782 break;
3783 if (**arg != ',')
3784 {
3785 semsg(_(e_missing_dict_comma), *arg);
3786 goto failret;
3787 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003788 if (IS_WHITE_OR_NUL(*whitep))
3789 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003790 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003791 return FAIL;
3792 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003793 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003794 if (!IS_WHITE_OR_NUL(*whitep))
3795 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003796 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003797 return FAIL;
3798 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003799 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 }
3801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 *arg = *arg + 1;
3803
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003804 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003805 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003806 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003807 *arg += STRLEN(*arg);
3808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003810 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 return generate_NEWDICT(cctx, count);
3812
3813failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003814 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003815 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003816 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003817 *arg = (char_u *)"";
3818 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 dict_unref(d);
3820 return FAIL;
3821}
3822
3823/*
3824 * Compile "&option".
3825 */
3826 static int
3827compile_get_option(char_u **arg, cctx_T *cctx)
3828{
3829 typval_T rettv;
3830 char_u *start = *arg;
3831 int ret;
3832
3833 // parse the option and get the current value to get the type.
3834 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003835 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 if (ret == OK)
3837 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003838 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003839 char_u *name = vim_strnsave(start, *arg - start);
3840 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3841 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842
3843 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3844 vim_free(name);
3845 }
3846 clear_tv(&rettv);
3847
3848 return ret;
3849}
3850
3851/*
3852 * Compile "$VAR".
3853 */
3854 static int
3855compile_get_env(char_u **arg, cctx_T *cctx)
3856{
3857 char_u *start = *arg;
3858 int len;
3859 int ret;
3860 char_u *name;
3861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 ++*arg;
3863 len = get_env_len(arg);
3864 if (len == 0)
3865 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003866 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 return FAIL;
3868 }
3869
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003870 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 name = vim_strnsave(start, len + 1);
3872 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3873 vim_free(name);
3874 return ret;
3875}
3876
3877/*
3878 * Compile "@r".
3879 */
3880 static int
3881compile_get_register(char_u **arg, cctx_T *cctx)
3882{
3883 int ret;
3884
3885 ++*arg;
3886 if (**arg == NUL)
3887 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003888 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 return FAIL;
3890 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003891 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 {
3893 emsg_invreg(**arg);
3894 return FAIL;
3895 }
3896 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3897 ++*arg;
3898 return ret;
3899}
3900
3901/*
3902 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003903 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 */
3905 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003906apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003908 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909
3910 // this works from end to start
3911 while (p > start)
3912 {
3913 --p;
3914 if (*p == '-' || *p == '+')
3915 {
3916 // only '-' has an effect, for '+' we only check the type
3917#ifdef FEAT_FLOAT
3918 if (rettv->v_type == VAR_FLOAT)
3919 {
3920 if (*p == '-')
3921 rettv->vval.v_float = -rettv->vval.v_float;
3922 }
3923 else
3924#endif
3925 {
3926 varnumber_T val;
3927 int error = FALSE;
3928
3929 // tv_get_number_chk() accepts a string, but we don't want that
3930 // here
3931 if (check_not_string(rettv) == FAIL)
3932 return FAIL;
3933 val = tv_get_number_chk(rettv, &error);
3934 clear_tv(rettv);
3935 if (error)
3936 return FAIL;
3937 if (*p == '-')
3938 val = -val;
3939 rettv->v_type = VAR_NUMBER;
3940 rettv->vval.v_number = val;
3941 }
3942 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003943 else if (numeric_only)
3944 {
3945 ++p;
3946 break;
3947 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003948 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 {
3950 int v = tv2bool(rettv);
3951
3952 // '!' is permissive in the type.
3953 clear_tv(rettv);
3954 rettv->v_type = VAR_BOOL;
3955 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3956 }
3957 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003958 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 return OK;
3960}
3961
3962/*
3963 * Recognize v: variables that are constants and set "rettv".
3964 */
3965 static void
3966get_vim_constant(char_u **arg, typval_T *rettv)
3967{
3968 if (STRNCMP(*arg, "v:true", 6) == 0)
3969 {
3970 rettv->v_type = VAR_BOOL;
3971 rettv->vval.v_number = VVAL_TRUE;
3972 *arg += 6;
3973 }
3974 else if (STRNCMP(*arg, "v:false", 7) == 0)
3975 {
3976 rettv->v_type = VAR_BOOL;
3977 rettv->vval.v_number = VVAL_FALSE;
3978 *arg += 7;
3979 }
3980 else if (STRNCMP(*arg, "v:null", 6) == 0)
3981 {
3982 rettv->v_type = VAR_SPECIAL;
3983 rettv->vval.v_number = VVAL_NULL;
3984 *arg += 6;
3985 }
3986 else if (STRNCMP(*arg, "v:none", 6) == 0)
3987 {
3988 rettv->v_type = VAR_SPECIAL;
3989 rettv->vval.v_number = VVAL_NONE;
3990 *arg += 6;
3991 }
3992}
3993
Bram Moolenaar657137c2021-01-09 15:45:23 +01003994 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003995get_compare_type(char_u *p, int *len, int *type_is)
3996{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003997 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003998 int i;
3999
4000 switch (p[0])
4001 {
4002 case '=': if (p[1] == '=')
4003 type = EXPR_EQUAL;
4004 else if (p[1] == '~')
4005 type = EXPR_MATCH;
4006 break;
4007 case '!': if (p[1] == '=')
4008 type = EXPR_NEQUAL;
4009 else if (p[1] == '~')
4010 type = EXPR_NOMATCH;
4011 break;
4012 case '>': if (p[1] != '=')
4013 {
4014 type = EXPR_GREATER;
4015 *len = 1;
4016 }
4017 else
4018 type = EXPR_GEQUAL;
4019 break;
4020 case '<': if (p[1] != '=')
4021 {
4022 type = EXPR_SMALLER;
4023 *len = 1;
4024 }
4025 else
4026 type = EXPR_SEQUAL;
4027 break;
4028 case 'i': if (p[1] == 's')
4029 {
4030 // "is" and "isnot"; but not a prefix of a name
4031 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4032 *len = 5;
4033 i = p[*len];
4034 if (!isalnum(i) && i != '_')
4035 {
4036 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4037 *type_is = TRUE;
4038 }
4039 }
4040 break;
4041 }
4042 return type;
4043}
4044
4045/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004046 * Skip over an expression, ignoring most errors.
4047 */
4048 static void
4049skip_expr_cctx(char_u **arg, cctx_T *cctx)
4050{
4051 evalarg_T evalarg;
4052
4053 CLEAR_FIELD(evalarg);
4054 evalarg.eval_cctx = cctx;
4055 skip_expr(arg, &evalarg);
4056}
4057
4058/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004060 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 */
4062 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004063compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004065 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066
4067 // this works from end to start
4068 while (p > start)
4069 {
4070 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004071 while (VIM_ISWHITE(*p))
4072 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 if (*p == '-' || *p == '+')
4074 {
4075 int negate = *p == '-';
4076 isn_T *isn;
4077
4078 // TODO: check type
4079 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4080 {
4081 --p;
4082 if (*p == '-')
4083 negate = !negate;
4084 }
4085 // only '-' has an effect, for '+' we only check the type
4086 if (negate)
4087 isn = generate_instr(cctx, ISN_NEGATENR);
4088 else
4089 isn = generate_instr(cctx, ISN_CHECKNR);
4090 if (isn == NULL)
4091 return FAIL;
4092 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004093 else if (numeric_only)
4094 {
4095 ++p;
4096 break;
4097 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 else
4099 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004100 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004102 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004104 if (p[-1] == '!')
4105 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004108 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 return FAIL;
4110 }
4111 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004112 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 return OK;
4114}
4115
4116/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004117 * Compile "(expression)": recursive!
4118 * Return FAIL/OK.
4119 */
4120 static int
4121compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4122{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004123 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004124 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004125
Bram Moolenaar24156692021-01-14 20:35:49 +01004126 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4127 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004128 if (ppconst->pp_used <= PPSIZE - 10)
4129 {
4130 ret = compile_expr1(arg, cctx, ppconst);
4131 }
4132 else
4133 {
4134 // Not enough space in ppconst, flush constants.
4135 if (generate_ppconst(cctx, ppconst) == FAIL)
4136 return FAIL;
4137 ret = compile_expr0(arg, cctx);
4138 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004139 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4140 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004141 if (**arg == ')')
4142 ++*arg;
4143 else if (ret == OK)
4144 {
4145 emsg(_(e_missing_close));
4146 ret = FAIL;
4147 }
4148 return ret;
4149}
4150
4151/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004153 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 */
4155 static int
4156compile_subscript(
4157 char_u **arg,
4158 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004159 char_u *start_leader,
4160 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004161 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004163 char_u *name_start = *end_leader;
4164
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 for (;;)
4166 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004167 char_u *p = skipwhite(*arg);
4168
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004169 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004170 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004171 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004172
4173 // If a following line starts with "->{" or "->X" advance to that
4174 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004175 // Also if a following line starts with ".x".
4176 if (next != NULL &&
4177 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004178 && (next[2] == '{'
4179 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004180 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004181 {
4182 next = next_line_from_context(cctx, TRUE);
4183 if (next == NULL)
4184 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004185 *arg = next;
4186 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004187 }
4188 }
4189
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004190 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004191 // not a function call.
4192 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004194 garray_T *stack = &cctx->ctx_type_stack;
4195 type_T *type;
4196 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004198 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004199 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004200 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004201
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004203 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4204
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004205 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004206 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004208 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 return FAIL;
4210 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004211 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004213 char_u *pstart = p;
4214
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004215 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004216 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004217 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 // something->method()
4220 // Apply the '!', '-' and '+' first:
4221 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004222 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004225 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004226 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004227 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004228 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004229 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004230 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004231 garray_T *stack = &cctx->ctx_type_stack;
4232 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004233 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004234 int expr_isn_start = cctx->ctx_instr.ga_len;
4235 int expr_isn_end;
4236 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004237
4238 // Funcref call: list->(Refs[2])(arg)
4239 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004240 //
4241 // Fist compile the function expression.
4242 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004243 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004244
4245 // Remember the next instruction index, where the instructions
4246 // for arguments are being written.
4247 expr_isn_end = cctx->ctx_instr.ga_len;
4248
4249 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004250 if (**arg != '(')
4251 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004252 if (*skipwhite(*arg) == '(')
4253 emsg(_(e_nowhitespace));
4254 else
4255 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004256 return FAIL;
4257 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004258 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004259 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004260 return FAIL;
4261
Bram Moolenaar2927c072021-04-05 19:41:21 +02004262 // Move the instructions for the arguments to before the
4263 // instructions of the expression and move the type of the
4264 // expression after the argument types. This is what ISN_PCALL
4265 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004266 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004267 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4268 if (arg_isn_count > 0)
4269 {
4270 int expr_isn_count = expr_isn_end - expr_isn_start;
4271 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4272
4273 if (isn == NULL)
4274 return FAIL;
4275 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4276 + expr_isn_start,
4277 sizeof(isn_T) * expr_isn_count);
4278 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4279 + expr_isn_start,
4280 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4281 sizeof(isn_T) * arg_isn_count);
4282 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4283 + expr_isn_start + arg_isn_count,
4284 isn, sizeof(isn_T) * expr_isn_count);
4285 vim_free(isn);
4286
4287 type = ((type_T **)stack->ga_data)[type_idx_start];
4288 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4289 ((type_T **)stack->ga_data) + type_idx_start + 1,
4290 sizeof(type_T *)
4291 * (stack->ga_len - type_idx_start - 1));
4292 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4293 }
4294
Bram Moolenaar7e368202020-12-25 21:56:57 +01004295 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4296 if (generate_PCALL(cctx, argcount,
4297 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 return FAIL;
4299 }
4300 else
4301 {
4302 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004303 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004304 if (!eval_isnamec1(*p))
4305 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004306 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004307 return FAIL;
4308 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004309 if (ASCII_ISALPHA(*p) && p[1] == ':')
4310 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004311 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 ;
4313 if (*p != '(')
4314 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004315 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 return FAIL;
4317 }
4318 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004319 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 return FAIL;
4321 }
4322 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004323 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004325 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004326
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004327 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004328 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004329 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004330 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004331 // TODO: more arguments
4332 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004333 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004334 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004335 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004336
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004337 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004338 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004339 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004340 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004341 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004342 // missing first index is equal to zero
4343 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004344 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004345 else
4346 {
4347 if (compile_expr0(arg, cctx) == FAIL)
4348 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004349 if (**arg == ':')
4350 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004351 semsg(_(e_white_space_required_before_and_after_str_at_str),
4352 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004353 return FAIL;
4354 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004355 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004356 return FAIL;
4357 *arg = skipwhite(*arg);
4358 }
4359 if (**arg == ':')
4360 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004361 is_slice = TRUE;
4362 ++*arg;
4363 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4364 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004365 semsg(_(e_white_space_required_before_and_after_str_at_str),
4366 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004367 return FAIL;
4368 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004369 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004370 return FAIL;
4371 if (**arg == ']')
4372 // missing second index is equal to end of string
4373 generate_PUSHNR(cctx, -1);
4374 else
4375 {
4376 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004377 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004378 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004379 return FAIL;
4380 *arg = skipwhite(*arg);
4381 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004382 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383
4384 if (**arg != ']')
4385 {
4386 emsg(_(e_missbrac));
4387 return FAIL;
4388 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004389 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390
Bram Moolenaare42939a2021-04-05 17:11:17 +02004391 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004392 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004394 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004396 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004397 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004398 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004399 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004400
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004401 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004402 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004403 {
4404 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004405 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004406 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004407 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004408 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 while (eval_isnamec(*p))
4410 MB_PTR_ADV(p);
4411 if (p == *arg)
4412 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004413 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 return FAIL;
4415 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004416 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 return FAIL;
4418 *arg = p;
4419 }
4420 else
4421 break;
4422 }
4423
4424 // TODO - see handle_subscript():
4425 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4426 // Don't do this when "Func" is already a partial that was bound
4427 // explicitly (pt_auto is FALSE).
4428
4429 return OK;
4430}
4431
4432/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004433 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4434 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004436 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004437 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004438 *
4439 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 */
4441
4442/*
4443 * number number constant
4444 * 0zFFFFFFFF Blob constant
4445 * "string" string constant
4446 * 'string' literal string constant
4447 * &option-name option value
4448 * @r register contents
4449 * identifier variable value
4450 * function() function call
4451 * $VAR environment variable
4452 * (expression) nested expression
4453 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004454 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 *
4456 * Also handle:
4457 * ! in front logical NOT
4458 * - in front unary minus
4459 * + in front unary plus (ignored)
4460 * trailing (arg) funcref/partial call
4461 * trailing [] subscript in String or List
4462 * trailing .name entry in Dictionary
4463 * trailing ->name() method call
4464 */
4465 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004466compile_expr7(
4467 char_u **arg,
4468 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004469 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471 char_u *start_leader, *end_leader;
4472 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004473 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004474 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004476 ppconst->pp_is_const = FALSE;
4477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 /*
4479 * Skip '!', '-' and '+' characters. They are handled later.
4480 */
4481 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004482 if (eval_leader(arg, TRUE) == FAIL)
4483 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 end_leader = *arg;
4485
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004486 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 switch (**arg)
4488 {
4489 /*
4490 * Number constant.
4491 */
4492 case '0': // also for blob starting with 0z
4493 case '1':
4494 case '2':
4495 case '3':
4496 case '4':
4497 case '5':
4498 case '6':
4499 case '7':
4500 case '8':
4501 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004502 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004504 // Apply "-" and "+" just before the number now, right to
4505 // left. Matters especially when "->" follows. Stops at
4506 // '!'.
4507 if (apply_leader(rettv, TRUE,
4508 start_leader, &end_leader) == FAIL)
4509 {
4510 clear_tv(rettv);
4511 return FAIL;
4512 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 break;
4514
4515 /*
4516 * String constant: "string".
4517 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004518 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 return FAIL;
4520 break;
4521
4522 /*
4523 * Literal string constant: 'str''ing'.
4524 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004525 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526 return FAIL;
4527 break;
4528
4529 /*
4530 * Constant Vim variable.
4531 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004532 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 ret = NOTDONE;
4534 break;
4535
4536 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004537 * "true" constant
4538 */
4539 case 't': if (STRNCMP(*arg, "true", 4) == 0
4540 && !eval_isnamec((*arg)[4]))
4541 {
4542 *arg += 4;
4543 rettv->v_type = VAR_BOOL;
4544 rettv->vval.v_number = VVAL_TRUE;
4545 }
4546 else
4547 ret = NOTDONE;
4548 break;
4549
4550 /*
4551 * "false" constant
4552 */
4553 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4554 && !eval_isnamec((*arg)[5]))
4555 {
4556 *arg += 5;
4557 rettv->v_type = VAR_BOOL;
4558 rettv->vval.v_number = VVAL_FALSE;
4559 }
4560 else
4561 ret = NOTDONE;
4562 break;
4563
4564 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004565 * "null" constant
4566 */
4567 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004568 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004569 {
4570 *arg += 4;
4571 rettv->v_type = VAR_SPECIAL;
4572 rettv->vval.v_number = VVAL_NULL;
4573 }
4574 else
4575 ret = NOTDONE;
4576 break;
4577
4578 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 * List: [expr, expr]
4580 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004581 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4582 return FAIL;
4583 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 break;
4585
4586 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 * Dictionary: {'key': val, 'key': val}
4588 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004589 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4590 return FAIL;
4591 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 break;
4593
4594 /*
4595 * Option value: &name
4596 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004597 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4598 return FAIL;
4599 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 break;
4601
4602 /*
4603 * Environment variable: $VAR.
4604 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004605 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4606 return FAIL;
4607 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 break;
4609
4610 /*
4611 * Register contents: @r.
4612 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004613 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4614 return FAIL;
4615 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 break;
4617 /*
4618 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004619 * lambda: (arg, arg) => expr
4620 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004622 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4623 ret = compile_lambda(arg, cctx);
4624 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004625 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 break;
4627
4628 default: ret = NOTDONE;
4629 break;
4630 }
4631 if (ret == FAIL)
4632 return FAIL;
4633
Bram Moolenaar1c747212020-05-09 18:28:34 +02004634 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004636 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004637 clear_tv(rettv);
4638 else
4639 // A constant expression can possibly be handled compile time,
4640 // return the value instead of generating code.
4641 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 }
4643 else if (ret == NOTDONE)
4644 {
4645 char_u *p;
4646 int r;
4647
4648 if (!eval_isnamec1(**arg))
4649 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004650 if (!vim9_bad_comment(*arg))
4651 {
4652 if (ends_excmd(*skipwhite(*arg)))
4653 semsg(_(e_empty_expression_str), *arg);
4654 else
4655 semsg(_(e_name_expected_str), *arg);
4656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 return FAIL;
4658 }
4659
4660 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004661 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004662 if (p - *arg == (size_t)1 && **arg == '_')
4663 {
4664 emsg(_(e_cannot_use_underscore_here));
4665 return FAIL;
4666 }
4667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 {
4670 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4671 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004673 {
4674 if (generate_ppconst(cctx, ppconst) == FAIL)
4675 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004676 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004677 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 if (r == FAIL)
4679 return FAIL;
4680 }
4681
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004682 // Handle following "[]", ".member", etc.
4683 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004684 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004685 ppconst) == FAIL)
4686 return FAIL;
4687 if (ppconst->pp_used > 0)
4688 {
4689 // apply the '!', '-' and '+' before the constant
4690 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004691 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004692 return FAIL;
4693 return OK;
4694 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004695 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004697 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698}
4699
4700/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004701 * Give the "white on both sides" error, taking the operator from "p[len]".
4702 */
4703 void
4704error_white_both(char_u *op, int len)
4705{
4706 char_u buf[10];
4707
4708 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004709 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004710}
4711
4712/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004713 * <type>expr7: runtime type check / conversion
4714 */
4715 static int
4716compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4717{
4718 type_T *want_type = NULL;
4719
4720 // Recognize <type>
4721 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4722 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004723 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004724 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4725 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004726 return FAIL;
4727
4728 if (**arg != '>')
4729 {
4730 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004731 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004732 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004733 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004734 return FAIL;
4735 }
4736 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004737 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004738 return FAIL;
4739 }
4740
4741 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4742 return FAIL;
4743
4744 if (want_type != NULL)
4745 {
4746 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004747 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004748 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004749
Bram Moolenaard1103582020-08-14 22:44:25 +02004750 generate_ppconst(cctx, ppconst);
4751 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004752 where.wt_index = 0;
4753 where.wt_variable = FALSE;
4754 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004755 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004756 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004757 return FAIL;
4758 }
4759 }
4760
4761 return OK;
4762}
4763
4764/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 * * number multiplication
4766 * / number division
4767 * % number modulo
4768 */
4769 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004770compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771{
4772 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004773 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004774 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004776 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004777 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 return FAIL;
4779
4780 /*
4781 * Repeat computing, until no "*", "/" or "%" is following.
4782 */
4783 for (;;)
4784 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004785 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 if (*op != '*' && *op != '/' && *op != '%')
4787 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004788 if (next != NULL)
4789 {
4790 *arg = next_line_from_context(cctx, TRUE);
4791 op = skipwhite(*arg);
4792 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004793
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004794 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004796 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004797 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004799 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004800 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004802 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004803 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004805
4806 if (ppconst->pp_used == ppconst_used + 2
4807 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4808 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004809 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004810 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4811 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4812 varnumber_T res = 0;
4813 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004815 // both are numbers: compute the result
4816 switch (*op)
4817 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004818 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004819 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004820 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004821 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004822 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004823 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004824 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004825 break;
4826 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004827 if (failed)
4828 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004829 tv1->vval.v_number = res;
4830 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004831 }
4832 else
4833 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004834 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004835 generate_two_op(cctx, op);
4836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 }
4838
4839 return OK;
4840}
4841
4842/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004843 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 * - number subtraction
4845 * .. string concatenation
4846 */
4847 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004848compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849{
4850 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004851 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004853 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854
4855 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004856 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 return FAIL;
4858
4859 /*
4860 * Repeat computing, until no "+", "-" or ".." is following.
4861 */
4862 for (;;)
4863 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004864 op = may_peek_next_line(cctx, *arg, &next);
4865 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004867 if (op[0] == op[1] && *op != '.' && next)
4868 // Finding "++" or "--" on the next line is a separate command.
4869 // But ".." is concatenation.
4870 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004872 if (next != NULL)
4873 {
4874 *arg = next_line_from_context(cctx, TRUE);
4875 op = skipwhite(*arg);
4876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004878 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004880 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004881 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882 }
4883
Bram Moolenaare0de1712020-12-02 17:36:54 +01004884 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004885 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004887 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004888 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 return FAIL;
4890
Bram Moolenaara5565e42020-05-09 15:44:01 +02004891 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004892 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004893 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4894 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4895 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4896 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004898 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4899 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004900
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004901 // concat/subtract/add constant numbers
4902 if (*op == '+')
4903 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4904 else if (*op == '-')
4905 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4906 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004907 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004908 // concatenate constant strings
4909 char_u *s1 = tv1->vval.v_string;
4910 char_u *s2 = tv2->vval.v_string;
4911 size_t len1 = STRLEN(s1);
4912
4913 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4914 if (tv1->vval.v_string == NULL)
4915 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004916 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004917 return FAIL;
4918 }
4919 mch_memmove(tv1->vval.v_string, s1, len1);
4920 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004921 vim_free(s1);
4922 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004923 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004924 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 }
4926 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004927 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004928 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004929 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004930 if (*op == '.')
4931 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004932 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4933 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004934 return FAIL;
4935 generate_instr_drop(cctx, ISN_CONCAT, 1);
4936 }
4937 else
4938 generate_two_op(cctx, op);
4939 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 }
4941
4942 return OK;
4943}
4944
4945/*
4946 * expr5a == expr5b
4947 * expr5a =~ expr5b
4948 * expr5a != expr5b
4949 * expr5a !~ expr5b
4950 * expr5a > expr5b
4951 * expr5a >= expr5b
4952 * expr5a < expr5b
4953 * expr5a <= expr5b
4954 * expr5a is expr5b
4955 * expr5a isnot expr5b
4956 *
4957 * Produces instructions:
4958 * EVAL expr5a Push result of "expr5a"
4959 * EVAL expr5b Push result of "expr5b"
4960 * COMPARE one of the compare instructions
4961 */
4962 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004963compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004965 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004967 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004970 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971
4972 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004973 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 return FAIL;
4975
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004976 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004977 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978
4979 /*
4980 * If there is a comparative operator, use it.
4981 */
4982 if (type != EXPR_UNKNOWN)
4983 {
4984 int ic = FALSE; // Default: do not ignore case
4985
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004986 if (next != NULL)
4987 {
4988 *arg = next_line_from_context(cctx, TRUE);
4989 p = skipwhite(*arg);
4990 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 if (type_is && (p[len] == '?' || p[len] == '#'))
4992 {
4993 semsg(_(e_invexpr2), *arg);
4994 return FAIL;
4995 }
4996 // extra question mark appended: ignore case
4997 if (p[len] == '?')
4998 {
4999 ic = TRUE;
5000 ++len;
5001 }
5002 // extra '#' appended: match case (ignored)
5003 else if (p[len] == '#')
5004 ++len;
5005 // nothing appended: match case
5006
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005007 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005009 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005010 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 }
5012
5013 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005014 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005015 return FAIL;
5016
Bram Moolenaara5565e42020-05-09 15:44:01 +02005017 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 return FAIL;
5019
Bram Moolenaara5565e42020-05-09 15:44:01 +02005020 if (ppconst->pp_used == ppconst_used + 2)
5021 {
5022 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5023 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5024 int ret;
5025
5026 // Both sides are a constant, compute the result now.
5027 // First check for a valid combination of types, this is more
5028 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005029 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005030 ret = FAIL;
5031 else
5032 {
5033 ret = typval_compare(tv1, tv2, type, ic);
5034 tv1->v_type = VAR_BOOL;
5035 tv1->vval.v_number = tv1->vval.v_number
5036 ? VVAL_TRUE : VVAL_FALSE;
5037 clear_tv(tv2);
5038 --ppconst->pp_used;
5039 }
5040 return ret;
5041 }
5042
5043 generate_ppconst(cctx, ppconst);
5044 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 }
5046
5047 return OK;
5048}
5049
Bram Moolenaar7f141552020-05-09 17:35:53 +02005050static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052/*
5053 * Compile || or &&.
5054 */
5055 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005056compile_and_or(
5057 char_u **arg,
5058 cctx_T *cctx,
5059 char *op,
5060 ppconst_T *ppconst,
5061 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005063 char_u *next;
5064 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 int opchar = *op;
5066
5067 if (p[0] == opchar && p[1] == opchar)
5068 {
5069 garray_T *instr = &cctx->ctx_instr;
5070 garray_T end_ga;
5071
5072 /*
5073 * Repeat until there is no following "||" or "&&"
5074 */
5075 ga_init2(&end_ga, sizeof(int), 10);
5076 while (p[0] == opchar && p[1] == opchar)
5077 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005078 long start_lnum = SOURCING_LNUM;
5079 int start_ctx_lnum = cctx->ctx_lnum;
5080 int save_lnum;
5081
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005082 if (next != NULL)
5083 {
5084 *arg = next_line_from_context(cctx, TRUE);
5085 p = skipwhite(*arg);
5086 }
5087
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005088 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5089 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005090 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005091 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005092 return FAIL;
5093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005095 // TODO: use ppconst if the value is a constant and check
5096 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005097 generate_ppconst(cctx, ppconst);
5098
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005099 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005100 SOURCING_LNUM = start_lnum;
5101 save_lnum = cctx->ctx_lnum;
5102 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005103 if (bool_on_stack(cctx) == FAIL)
5104 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005105 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005106 ga_clear(&end_ga);
5107 return FAIL;
5108 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005109 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 if (ga_grow(&end_ga, 1) == FAIL)
5112 {
5113 ga_clear(&end_ga);
5114 return FAIL;
5115 }
5116 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5117 ++end_ga.ga_len;
5118 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005119 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120
5121 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005122 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005123 {
5124 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005125 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005126 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005127
Bram Moolenaara5565e42020-05-09 15:44:01 +02005128 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5129 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 {
5131 ga_clear(&end_ga);
5132 return FAIL;
5133 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005134
5135 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005136 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005137 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005139 // Every part must evaluate to a bool.
5140 if (bool_on_stack(cctx) == FAIL)
5141 {
5142 ga_clear(&end_ga);
5143 return FAIL;
5144 }
5145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 // Fill in the end label in all jumps.
5147 while (end_ga.ga_len > 0)
5148 {
5149 isn_T *isn;
5150
5151 --end_ga.ga_len;
5152 isn = ((isn_T *)instr->ga_data)
5153 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5154 isn->isn_arg.jump.jump_where = instr->ga_len;
5155 }
5156 ga_clear(&end_ga);
5157 }
5158
5159 return OK;
5160}
5161
5162/*
5163 * expr4a && expr4a && expr4a logical AND
5164 *
5165 * Produces instructions:
5166 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005167 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005168 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005169 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005170 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171 * EVAL expr4c Push result of "expr4c"
5172 * end:
5173 */
5174 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005175compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005177 int ppconst_used = ppconst->pp_used;
5178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005180 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 return FAIL;
5182
5183 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005184 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005185}
5186
5187/*
5188 * expr3a || expr3b || expr3c logical OR
5189 *
5190 * Produces instructions:
5191 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005192 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005193 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005194 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005195 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196 * EVAL expr3c Push result of "expr3c"
5197 * end:
5198 */
5199 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005200compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005202 int ppconst_used = ppconst->pp_used;
5203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005205 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 return FAIL;
5207
5208 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005209 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210}
5211
5212/*
5213 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005215 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216 * JUMP_IF_FALSE alt jump if false
5217 * EVAL expr1a
5218 * JUMP_ALWAYS end
5219 * alt: EVAL expr1b
5220 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005221 *
5222 * Toplevel expression: expr2 ?? expr1
5223 * Produces instructions:
5224 * EVAL expr2 Push result of "expr2"
5225 * JUMP_AND_KEEP_IF_TRUE end jump if true
5226 * EVAL expr1
5227 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 */
5229 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005230compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231{
5232 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005233 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005234 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235
Bram Moolenaar3988f642020-08-27 22:43:03 +02005236 // Ignore all kinds of errors when not producing code.
5237 if (cctx->ctx_skip == SKIP_YES)
5238 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005239 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005240 return OK;
5241 }
5242
Bram Moolenaar61a89812020-05-07 16:58:17 +02005243 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005244 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 return FAIL;
5246
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005247 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248 if (*p == '?')
5249 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005250 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251 garray_T *instr = &cctx->ctx_instr;
5252 garray_T *stack = &cctx->ctx_type_stack;
5253 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005254 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005256 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005257 int has_const_expr = FALSE;
5258 int const_value = FALSE;
5259 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005260
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005261 if (next != NULL)
5262 {
5263 *arg = next_line_from_context(cctx, TRUE);
5264 p = skipwhite(*arg);
5265 }
5266
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005267 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005268 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005269 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005270 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005271 return FAIL;
5272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005273
Bram Moolenaara5565e42020-05-09 15:44:01 +02005274 if (ppconst->pp_used == ppconst_used + 1)
5275 {
5276 // the condition is a constant, we know whether the ? or the :
5277 // expression is to be evaluated.
5278 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005279 if (op_falsy)
5280 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5281 else
5282 {
5283 int error = FALSE;
5284
5285 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5286 &error);
5287 if (error)
5288 return FAIL;
5289 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005290 cctx->ctx_skip = save_skip == SKIP_YES ||
5291 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5292
5293 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5294 // "left ?? right" and "left" is truthy: produce "left"
5295 generate_ppconst(cctx, ppconst);
5296 else
5297 {
5298 clear_tv(&ppconst->pp_tv[ppconst_used]);
5299 --ppconst->pp_used;
5300 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005301 }
5302 else
5303 {
5304 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005305 if (op_falsy)
5306 end_idx = instr->ga_len;
5307 generate_JUMP(cctx, op_falsy
5308 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5309 if (op_falsy)
5310 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312
5313 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005314 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005315 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005316 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005317 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318
Bram Moolenaara5565e42020-05-09 15:44:01 +02005319 if (!has_const_expr)
5320 {
5321 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005322
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005323 if (!op_falsy)
5324 {
5325 // remember the type and drop it
5326 --stack->ga_len;
5327 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005329 end_idx = instr->ga_len;
5330 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005331
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005332 // jump here from JUMP_IF_FALSE
5333 isn = ((isn_T *)instr->ga_data) + alt_idx;
5334 isn->isn_arg.jump.jump_where = instr->ga_len;
5335 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005338 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005340 // Check for the ":".
5341 p = may_peek_next_line(cctx, *arg, &next);
5342 if (*p != ':')
5343 {
5344 emsg(_(e_missing_colon));
5345 return FAIL;
5346 }
5347 if (next != NULL)
5348 {
5349 *arg = next_line_from_context(cctx, TRUE);
5350 p = skipwhite(*arg);
5351 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005352
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005353 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5354 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005355 semsg(_(e_white_space_required_before_and_after_str_at_str),
5356 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005357 return FAIL;
5358 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005359
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005360 // evaluate the third expression
5361 if (has_const_expr)
5362 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005363 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005364 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005365 return FAIL;
5366 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5367 return FAIL;
5368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369
Bram Moolenaara5565e42020-05-09 15:44:01 +02005370 if (!has_const_expr)
5371 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005372 type_T **typep;
5373
Bram Moolenaara5565e42020-05-09 15:44:01 +02005374 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005375
Bram Moolenaara5565e42020-05-09 15:44:01 +02005376 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005377 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5378 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005379
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005380 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005381 isn = ((isn_T *)instr->ga_data) + end_idx;
5382 isn->isn_arg.jump.jump_where = instr->ga_len;
5383 }
5384
5385 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386 }
5387 return OK;
5388}
5389
5390/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005391 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005392 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5393 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005394 */
5395 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005396compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005397{
5398 ppconst_T ppconst;
5399
5400 CLEAR_FIELD(ppconst);
5401 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5402 {
5403 clear_ppconst(&ppconst);
5404 return FAIL;
5405 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005406 if (is_const != NULL)
5407 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005408 if (generate_ppconst(cctx, &ppconst) == FAIL)
5409 return FAIL;
5410 return OK;
5411}
5412
5413/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005414 * Toplevel expression.
5415 */
5416 static int
5417compile_expr0(char_u **arg, cctx_T *cctx)
5418{
5419 return compile_expr0_ext(arg, cctx, NULL);
5420}
5421
5422/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005423 * Compile "return [expr]".
5424 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005425 */
5426 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005427compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428{
5429 char_u *p = arg;
5430 garray_T *stack = &cctx->ctx_type_stack;
5431 type_T *stack_type;
5432
5433 if (*p != NUL && *p != '|' && *p != '\n')
5434 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005435 if (legacy)
5436 {
5437 int save_flags = cmdmod.cmod_flags;
5438
5439 generate_LEGACY_EVAL(cctx, p);
5440 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5441 0, cctx, FALSE, FALSE) == FAIL)
5442 return NULL;
5443 cmdmod.cmod_flags |= CMOD_LEGACY;
5444 (void)skip_expr(&p, NULL);
5445 cmdmod.cmod_flags = save_flags;
5446 }
5447 else
5448 {
5449 // compile return argument into instructions
5450 if (compile_expr0(&p, cctx) == FAIL)
5451 return NULL;
5452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005453
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005454 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005455 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005456 // "check_return_type" with uf_ret_type set to &t_unknown is used
5457 // for an inline function without a specified return type. Set the
5458 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005459 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005460 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005461 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5462 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005463 || (!check_return_type
5464 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005465 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005466 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005467 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005468 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005469 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005470 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5471 && stack_type->tt_type != VAR_VOID
5472 && stack_type->tt_type != VAR_UNKNOWN)
5473 {
5474 emsg(_(e_returning_value_in_function_without_return_type));
5475 return NULL;
5476 }
5477 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005478 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005479 return NULL;
5480 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482 }
5483 else
5484 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005485 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005486 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005487 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5488 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005490 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491 return NULL;
5492 }
5493
5494 // No argument, return zero.
5495 generate_PUSHNR(cctx, 0);
5496 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005497
5498 // Undo any command modifiers.
5499 generate_undo_cmdmods(cctx);
5500
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005501 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502 return NULL;
5503
5504 // "return val | endif" is possible
5505 return skipwhite(p);
5506}
5507
5508/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005509 * Get a line from the compilation context, compatible with exarg_T getline().
5510 * Return a pointer to the line in allocated memory.
5511 * Return NULL for end-of-file or some error.
5512 */
5513 static char_u *
5514exarg_getline(
5515 int c UNUSED,
5516 void *cookie,
5517 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005518 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005519{
5520 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005521 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005522
Bram Moolenaar66250c92020-08-20 15:02:42 +02005523 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005524 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005525 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005526 return NULL;
5527 ++cctx->ctx_lnum;
5528 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5529 // Comment lines result in NULL pointers, skip them.
5530 if (p != NULL)
5531 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005532 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005533}
5534
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005535 void
5536fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5537{
5538 eap->getline = exarg_getline;
5539 eap->cookie = cctx;
5540}
5541
Bram Moolenaar04b12692020-05-04 23:24:44 +02005542/*
5543 * Compile a nested :def command.
5544 */
5545 static char_u *
5546compile_nested_function(exarg_T *eap, cctx_T *cctx)
5547{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005548 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005549 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005550 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005551 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005552 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005553 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005554
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005555 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005556 {
5557 emsg(_(e_cannot_use_bang_with_nested_def));
5558 return NULL;
5559 }
5560
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005561 if (*name_start == '/')
5562 {
5563 name_end = skip_regexp(name_start + 1, '/', TRUE);
5564 if (*name_end == '/')
5565 ++name_end;
5566 eap->nextcmd = check_nextcmd(name_end);
5567 }
5568 if (name_end == name_start || *skipwhite(name_end) != '(')
5569 {
5570 if (!ends_excmd2(name_start, name_end))
5571 {
5572 semsg(_(e_invalid_command_str), eap->cmd);
5573 return NULL;
5574 }
5575
5576 // "def" or "def Name": list functions
5577 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5578 return NULL;
5579 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5580 }
5581
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005582 // Only g:Func() can use a namespace.
5583 if (name_start[1] == ':' && !is_global)
5584 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005585 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005586 return NULL;
5587 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005588 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005589 return NULL;
5590
Bram Moolenaar04b12692020-05-04 23:24:44 +02005591 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005592 fill_exarg_from_cctx(eap, cctx);
5593
Bram Moolenaar04b12692020-05-04 23:24:44 +02005594 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005595 lambda_name = vim_strsave(get_lambda_name());
5596 if (lambda_name == NULL)
5597 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005598 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005599
Bram Moolenaar822ba242020-05-24 23:00:18 +02005600 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005601 {
5602 r = eap->skip ? OK : FAIL;
5603 goto theend;
5604 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005605
5606 // copy over the block scope IDs before compiling
5607 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5608 {
5609 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5610
5611 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5612 if (ufunc->uf_block_ids != NULL)
5613 {
5614 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5615 sizeof(int) * block_depth);
5616 ufunc->uf_block_depth = block_depth;
5617 }
5618 }
5619
Bram Moolenaare99d4222021-06-13 14:01:26 +02005620 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
5621 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005622 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005623 {
5624 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005625 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005626 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005627
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005628 if (is_global)
5629 {
5630 char_u *func_name = vim_strnsave(name_start + 2,
5631 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005632
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005633 if (func_name == NULL)
5634 r = FAIL;
5635 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005636 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005637 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005638 lambda_name = NULL;
5639 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005640 }
5641 else
5642 {
5643 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005644 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005645 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005646
Bram Moolenaareef21022020-08-01 22:16:43 +02005647 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005648 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005649 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005650 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005651 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5652 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005653 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005654
5655theend:
5656 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005657 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005658}
5659
5660/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005661 * Return the length of an assignment operator, or zero if there isn't one.
5662 */
5663 int
5664assignment_len(char_u *p, int *heredoc)
5665{
5666 if (*p == '=')
5667 {
5668 if (p[1] == '<' && p[2] == '<')
5669 {
5670 *heredoc = TRUE;
5671 return 3;
5672 }
5673 return 1;
5674 }
5675 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5676 return 2;
5677 if (STRNCMP(p, "..=", 3) == 0)
5678 return 3;
5679 return 0;
5680}
5681
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005682/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005683 * Generate the load instruction for "name".
5684 */
5685 static void
5686generate_loadvar(
5687 cctx_T *cctx,
5688 assign_dest_T dest,
5689 char_u *name,
5690 lvar_T *lvar,
5691 type_T *type)
5692{
5693 switch (dest)
5694 {
5695 case dest_option:
5696 // TODO: check the option exists
5697 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5698 break;
5699 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005700 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5701 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5702 else
5703 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005704 break;
5705 case dest_buffer:
5706 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5707 break;
5708 case dest_window:
5709 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5710 break;
5711 case dest_tab:
5712 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5713 break;
5714 case dest_script:
5715 compile_load_scriptvar(cctx,
5716 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5717 break;
5718 case dest_env:
5719 // Include $ in the name here
5720 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5721 break;
5722 case dest_reg:
5723 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5724 break;
5725 case dest_vimvar:
5726 generate_LOADV(cctx, name + 2, TRUE);
5727 break;
5728 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005729 if (lvar->lv_from_outer > 0)
5730 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5731 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005732 else
5733 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5734 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005735 case dest_expr:
5736 // list or dict value should already be on the stack.
5737 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005738 }
5739}
5740
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005741/*
5742 * Skip over "[expr]" or ".member".
5743 * Does not check for any errors.
5744 */
5745 static char_u *
5746skip_index(char_u *start)
5747{
5748 char_u *p = start;
5749
5750 if (*p == '[')
5751 {
5752 p = skipwhite(p + 1);
5753 (void)skip_expr(&p, NULL);
5754 p = skipwhite(p);
5755 if (*p == ']')
5756 return p + 1;
5757 return p;
5758 }
5759 // if (*p == '.')
5760 return to_name_end(p + 1, TRUE);
5761}
5762
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005763 void
5764vim9_declare_error(char_u *name)
5765{
5766 char *scope = "";
5767
5768 switch (*name)
5769 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005770 case 'g': scope = _("global"); break;
5771 case 'b': scope = _("buffer"); break;
5772 case 'w': scope = _("window"); break;
5773 case 't': scope = _("tab"); break;
5774 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005775 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005776 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005777 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005778 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005779 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005780 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005781 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005782 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005783 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005784}
5785
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005786/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005787 * For one assignment figure out the type of destination. Return it in "dest".
5788 * When not recognized "dest" is not set.
5789 * For an option "opt_flags" is set.
5790 * For a v:var "vimvaridx" is set.
5791 * "type" is set to the destination type if known, unchanted otherwise.
5792 * Return FAIL if an error message was given.
5793 */
5794 static int
5795get_var_dest(
5796 char_u *name,
5797 assign_dest_T *dest,
5798 int cmdidx,
5799 int *opt_flags,
5800 int *vimvaridx,
5801 type_T **type,
5802 cctx_T *cctx)
5803{
5804 char_u *p;
5805
5806 if (*name == '&')
5807 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005808 int cc;
5809 long numval;
5810 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005811
5812 *dest = dest_option;
5813 if (cmdidx == CMD_final || cmdidx == CMD_const)
5814 {
5815 emsg(_(e_const_option));
5816 return FAIL;
5817 }
5818 p = name;
5819 p = find_option_end(&p, opt_flags);
5820 if (p == NULL)
5821 {
5822 // cannot happen?
5823 emsg(_(e_letunexp));
5824 return FAIL;
5825 }
5826 cc = *p;
5827 *p = NUL;
5828 opt_type = get_option_value(skip_option_env_lead(name),
5829 &numval, NULL, *opt_flags);
5830 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005831 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005832 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005833 case gov_unknown:
5834 semsg(_(e_unknown_option), name);
5835 return FAIL;
5836 case gov_string:
5837 case gov_hidden_string:
5838 *type = &t_string;
5839 break;
5840 case gov_bool:
5841 case gov_hidden_bool:
5842 *type = &t_bool;
5843 break;
5844 case gov_number:
5845 case gov_hidden_number:
5846 *type = &t_number;
5847 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005848 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005849 }
5850 else if (*name == '$')
5851 {
5852 *dest = dest_env;
5853 *type = &t_string;
5854 }
5855 else if (*name == '@')
5856 {
5857 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5858 {
5859 emsg_invreg(name[1]);
5860 return FAIL;
5861 }
5862 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005863 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005864 }
5865 else if (STRNCMP(name, "g:", 2) == 0)
5866 {
5867 *dest = dest_global;
5868 }
5869 else if (STRNCMP(name, "b:", 2) == 0)
5870 {
5871 *dest = dest_buffer;
5872 }
5873 else if (STRNCMP(name, "w:", 2) == 0)
5874 {
5875 *dest = dest_window;
5876 }
5877 else if (STRNCMP(name, "t:", 2) == 0)
5878 {
5879 *dest = dest_tab;
5880 }
5881 else if (STRNCMP(name, "v:", 2) == 0)
5882 {
5883 typval_T *vtv;
5884 int di_flags;
5885
5886 *vimvaridx = find_vim_var(name + 2, &di_flags);
5887 if (*vimvaridx < 0)
5888 {
5889 semsg(_(e_variable_not_found_str), name);
5890 return FAIL;
5891 }
5892 // We use the current value of "sandbox" here, is that OK?
5893 if (var_check_ro(di_flags, name, FALSE))
5894 return FAIL;
5895 *dest = dest_vimvar;
5896 vtv = get_vim_var_tv(*vimvaridx);
5897 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5898 }
5899 return OK;
5900}
5901
5902/*
5903 * Generate a STORE instruction for "dest", not being "dest_local".
5904 * Return FAIL when out of memory.
5905 */
5906 static int
5907generate_store_var(
5908 cctx_T *cctx,
5909 assign_dest_T dest,
5910 int opt_flags,
5911 int vimvaridx,
5912 int scriptvar_idx,
5913 int scriptvar_sid,
5914 type_T *type,
5915 char_u *name)
5916{
5917 switch (dest)
5918 {
5919 case dest_option:
5920 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5921 opt_flags);
5922 case dest_global:
5923 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005924 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5925 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005926 case dest_buffer:
5927 // include b: with the name, easier to execute that way
5928 return generate_STORE(cctx, ISN_STOREB, 0, name);
5929 case dest_window:
5930 // include w: with the name, easier to execute that way
5931 return generate_STORE(cctx, ISN_STOREW, 0, name);
5932 case dest_tab:
5933 // include t: with the name, easier to execute that way
5934 return generate_STORE(cctx, ISN_STORET, 0, name);
5935 case dest_env:
5936 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5937 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005938 return generate_STORE(cctx, ISN_STOREREG,
5939 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005940 case dest_vimvar:
5941 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5942 case dest_script:
5943 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005944 // "s:" may be included in the name.
5945 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005946 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005947 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5948 scriptvar_sid, scriptvar_idx, type);
5949 case dest_local:
5950 case dest_expr:
5951 // cannot happen
5952 break;
5953 }
5954 return FAIL;
5955}
5956
Bram Moolenaar752fc692021-01-04 21:57:11 +01005957 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005958generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5959{
5960 if (lhs->lhs_dest != dest_local)
5961 return generate_store_var(cctx, lhs->lhs_dest,
5962 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5963 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5964 lhs->lhs_type, lhs->lhs_name);
5965
5966 if (lhs->lhs_lvar != NULL)
5967 {
5968 garray_T *instr = &cctx->ctx_instr;
5969 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5970
5971 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5972 // ISN_STORENR
5973 if (lhs->lhs_lvar->lv_from_outer == 0
5974 && instr->ga_len == instr_count + 1
5975 && isn->isn_type == ISN_PUSHNR)
5976 {
5977 varnumber_T val = isn->isn_arg.number;
5978 garray_T *stack = &cctx->ctx_type_stack;
5979
5980 isn->isn_type = ISN_STORENR;
5981 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5982 isn->isn_arg.storenr.stnr_val = val;
5983 if (stack->ga_len > 0)
5984 --stack->ga_len;
5985 }
5986 else if (lhs->lhs_lvar->lv_from_outer > 0)
5987 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5988 lhs->lhs_lvar->lv_from_outer);
5989 else
5990 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5991 }
5992 return OK;
5993}
5994
5995 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005996is_decl_command(int cmdidx)
5997{
5998 return cmdidx == CMD_let || cmdidx == CMD_var
5999 || cmdidx == CMD_final || cmdidx == CMD_const;
6000}
6001
6002/*
6003 * Figure out the LHS type and other properties for an assignment or one item
6004 * of ":unlet" with an index.
6005 * Returns OK or FAIL.
6006 */
6007 static int
6008compile_lhs(
6009 char_u *var_start,
6010 lhs_T *lhs,
6011 int cmdidx,
6012 int heredoc,
6013 int oplen,
6014 cctx_T *cctx)
6015{
6016 char_u *var_end;
6017 int is_decl = is_decl_command(cmdidx);
6018
6019 CLEAR_POINTER(lhs);
6020 lhs->lhs_dest = dest_local;
6021 lhs->lhs_vimvaridx = -1;
6022 lhs->lhs_scriptvar_idx = -1;
6023
6024 // "dest_end" is the end of the destination, including "[expr]" or
6025 // ".name".
6026 // "var_end" is the end of the variable/option/etc. name.
6027 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6028 if (*var_start == '@')
6029 var_end = var_start + 2;
6030 else
6031 {
6032 // skip over the leading "&", "&l:", "&g:" and "$"
6033 var_end = skip_option_env_lead(var_start);
6034 var_end = to_name_end(var_end, TRUE);
6035 }
6036
6037 // "a: type" is declaring variable "a" with a type, not dict "a:".
6038 if (is_decl && lhs->lhs_dest_end == var_start + 2
6039 && lhs->lhs_dest_end[-1] == ':')
6040 --lhs->lhs_dest_end;
6041 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6042 --var_end;
6043
6044 // compute the length of the destination without "[expr]" or ".name"
6045 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006046 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006047 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6048 if (lhs->lhs_name == NULL)
6049 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006050
6051 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6052 // Something follows after the variable: "var[idx]" or "var.key".
6053 lhs->lhs_has_index = TRUE;
6054
Bram Moolenaar752fc692021-01-04 21:57:11 +01006055 if (heredoc)
6056 lhs->lhs_type = &t_list_string;
6057 else
6058 lhs->lhs_type = &t_any;
6059
6060 if (cctx->ctx_skip != SKIP_YES)
6061 {
6062 int declare_error = FALSE;
6063
6064 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6065 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6066 &lhs->lhs_type, cctx) == FAIL)
6067 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006068 if (lhs->lhs_dest != dest_local
6069 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006070 {
6071 // Specific kind of variable recognized.
6072 declare_error = is_decl;
6073 }
6074 else
6075 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006076 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006077 if (check_reserved_name(lhs->lhs_name) == FAIL)
6078 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006079
6080 if (lookup_local(var_start, lhs->lhs_varlen,
6081 &lhs->lhs_local_lvar, cctx) == OK)
6082 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6083 else
6084 {
6085 CLEAR_FIELD(lhs->lhs_arg_lvar);
6086 if (arg_exists(var_start, lhs->lhs_varlen,
6087 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6088 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6089 {
6090 if (is_decl)
6091 {
6092 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6093 return FAIL;
6094 }
6095 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6096 }
6097 }
6098 if (lhs->lhs_lvar != NULL)
6099 {
6100 if (is_decl)
6101 {
6102 semsg(_(e_variable_already_declared), lhs->lhs_name);
6103 return FAIL;
6104 }
6105 }
6106 else
6107 {
6108 int script_namespace = lhs->lhs_varlen > 1
6109 && STRNCMP(var_start, "s:", 2) == 0;
6110 int script_var = (script_namespace
6111 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006112 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006113 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006114 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006115 imported_T *import =
6116 find_imported(var_start, lhs->lhs_varlen, cctx);
6117
6118 if (script_namespace || script_var || import != NULL)
6119 {
6120 char_u *rawname = lhs->lhs_name
6121 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6122
6123 if (is_decl)
6124 {
6125 if (script_namespace)
6126 semsg(_(e_cannot_declare_script_variable_in_function),
6127 lhs->lhs_name);
6128 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006129 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006130 lhs->lhs_name);
6131 return FAIL;
6132 }
6133 else if (cctx->ctx_ufunc->uf_script_ctx_version
6134 == SCRIPT_VERSION_VIM9
6135 && script_namespace
6136 && !script_var && import == NULL)
6137 {
6138 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6139 return FAIL;
6140 }
6141
6142 lhs->lhs_dest = dest_script;
6143
6144 // existing script-local variables should have a type
6145 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6146 if (import != NULL)
6147 lhs->lhs_scriptvar_sid = import->imp_sid;
6148 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6149 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006150 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006151 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006152 lhs->lhs_scriptvar_sid, rawname,
6153 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6154 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006155 if (lhs->lhs_scriptvar_idx >= 0)
6156 {
6157 scriptitem_T *si = SCRIPT_ITEM(
6158 lhs->lhs_scriptvar_sid);
6159 svar_T *sv =
6160 ((svar_T *)si->sn_var_vals.ga_data)
6161 + lhs->lhs_scriptvar_idx;
6162 lhs->lhs_type = sv->sv_type;
6163 }
6164 }
6165 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006166 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006167 == FAIL)
6168 return FAIL;
6169 }
6170 }
6171
6172 if (declare_error)
6173 {
6174 vim9_declare_error(lhs->lhs_name);
6175 return FAIL;
6176 }
6177 }
6178
6179 // handle "a:name" as a name, not index "name" on "a"
6180 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6181 var_end = lhs->lhs_dest_end;
6182
6183 if (lhs->lhs_dest != dest_option)
6184 {
6185 if (is_decl && *var_end == ':')
6186 {
6187 char_u *p;
6188
6189 // parse optional type: "let var: type = expr"
6190 if (!VIM_ISWHITE(var_end[1]))
6191 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006192 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006193 return FAIL;
6194 }
6195 p = skipwhite(var_end + 1);
6196 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6197 if (lhs->lhs_type == NULL)
6198 return FAIL;
6199 lhs->lhs_has_type = TRUE;
6200 }
6201 else if (lhs->lhs_lvar != NULL)
6202 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6203 }
6204
Bram Moolenaare42939a2021-04-05 17:11:17 +02006205 if (oplen == 3 && !heredoc
6206 && lhs->lhs_dest != dest_global
6207 && !lhs->lhs_has_index
6208 && lhs->lhs_type->tt_type != VAR_STRING
6209 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006210 {
6211 emsg(_(e_can_only_concatenate_to_string));
6212 return FAIL;
6213 }
6214
6215 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6216 && cctx->ctx_skip != SKIP_YES)
6217 {
6218 if (oplen > 1 && !heredoc)
6219 {
6220 // +=, /=, etc. require an existing variable
6221 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6222 return FAIL;
6223 }
6224 if (!is_decl)
6225 {
6226 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6227 return FAIL;
6228 }
6229
Bram Moolenaar3f327882021-03-17 20:56:38 +01006230 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006231 if ((lhs->lhs_type->tt_type == VAR_FUNC
6232 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006233 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006234 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006235
6236 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006237 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6238 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6239 if (lhs->lhs_lvar == NULL)
6240 return FAIL;
6241 lhs->lhs_new_local = TRUE;
6242 }
6243
6244 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006245 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006246 {
6247 // Something follows after the variable: "var[idx]" or "var.key".
6248 // TODO: should we also handle "->func()" here?
6249 if (is_decl)
6250 {
6251 emsg(_(e_cannot_use_index_when_declaring_variable));
6252 return FAIL;
6253 }
6254
6255 if (var_start[lhs->lhs_varlen] == '['
6256 || var_start[lhs->lhs_varlen] == '.')
6257 {
6258 char_u *after = var_start + lhs->lhs_varlen;
6259 char_u *p;
6260
6261 // Only the last index is used below, if there are others
6262 // before it generate code for the expression. Thus for
6263 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6264 for (;;)
6265 {
6266 p = skip_index(after);
6267 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006268 {
6269 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006270 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006271 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006272 after = p;
6273 }
6274 if (after > var_start + lhs->lhs_varlen)
6275 {
6276 lhs->lhs_varlen = after - var_start;
6277 lhs->lhs_dest = dest_expr;
6278 // We don't know the type before evaluating the expression,
6279 // use "any" until then.
6280 lhs->lhs_type = &t_any;
6281 }
6282
Bram Moolenaar752fc692021-01-04 21:57:11 +01006283 if (lhs->lhs_type->tt_member == NULL)
6284 lhs->lhs_member_type = &t_any;
6285 else
6286 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6287 }
6288 else
6289 {
6290 semsg("Not supported yet: %s", var_start);
6291 return FAIL;
6292 }
6293 }
6294 return OK;
6295}
6296
6297/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006298 * Figure out the LHS and check a few errors.
6299 */
6300 static int
6301compile_assign_lhs(
6302 char_u *var_start,
6303 lhs_T *lhs,
6304 int cmdidx,
6305 int is_decl,
6306 int heredoc,
6307 int oplen,
6308 cctx_T *cctx)
6309{
6310 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6311 return FAIL;
6312
6313 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6314 {
6315 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6316 return FAIL;
6317 }
6318 if (!is_decl && lhs->lhs_lvar != NULL
6319 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6320 {
6321 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6322 return FAIL;
6323 }
6324 return OK;
6325}
6326
6327/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006328 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6329 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006330 */
6331 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006332compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006333 char_u *var_start,
6334 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006335 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006336 cctx_T *cctx)
6337{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006338 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006339 char_u *p;
6340 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006341 int need_white_before = TRUE;
6342 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006343
Bram Moolenaar752fc692021-01-04 21:57:11 +01006344 p = var_start + varlen;
6345 if (*p == '[')
6346 {
6347 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006348 if (*p == ':')
6349 {
6350 // empty first index, push zero
6351 r = generate_PUSHNR(cctx, 0);
6352 need_white_before = FALSE;
6353 }
6354 else
6355 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006356
6357 if (r == OK && *skipwhite(p) == ':')
6358 {
6359 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006360 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006361 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006362 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006363 empty_second = *skipwhite(p + 1) == ']';
6364 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6365 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006366 {
6367 semsg(_(e_white_space_required_before_and_after_str_at_str),
6368 ":", p);
6369 return FAIL;
6370 }
6371 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006372 if (*p == ']')
6373 // empty second index, push "none"
6374 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6375 else
6376 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006377 }
6378
Bram Moolenaar752fc692021-01-04 21:57:11 +01006379 if (r == OK && *skipwhite(p) != ']')
6380 {
6381 // this should not happen
6382 emsg(_(e_missbrac));
6383 r = FAIL;
6384 }
6385 }
6386 else // if (*p == '.')
6387 {
6388 char_u *key_end = to_name_end(p + 1, TRUE);
6389 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6390
6391 r = generate_PUSHS(cctx, key);
6392 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006393 return r;
6394}
6395
6396/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006397 * For a LHS with an index, load the variable to be indexed.
6398 */
6399 static int
6400compile_load_lhs(
6401 lhs_T *lhs,
6402 char_u *var_start,
6403 type_T *rhs_type,
6404 cctx_T *cctx)
6405{
6406 if (lhs->lhs_dest == dest_expr)
6407 {
6408 size_t varlen = lhs->lhs_varlen;
6409 int c = var_start[varlen];
6410 char_u *p = var_start;
6411 garray_T *stack = &cctx->ctx_type_stack;
6412
6413 // Evaluate "ll[expr]" of "ll[expr][idx]"
6414 var_start[varlen] = NUL;
6415 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6416 {
6417 // this should not happen
6418 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006419 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006420 return FAIL;
6421 }
6422 var_start[varlen] = c;
6423
6424 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6425 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6426 // now we can properly check the type
6427 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6428 && rhs_type != &t_void
6429 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6430 FALSE, FALSE) == FAIL)
6431 return FAIL;
6432 }
6433 else
6434 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6435 lhs->lhs_lvar, lhs->lhs_type);
6436 return OK;
6437}
6438
6439/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006440 * Produce code for loading "lhs" and also take care of an index.
6441 * Return OK/FAIL.
6442 */
6443 static int
6444compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6445{
6446 compile_load_lhs(lhs, var_start, NULL, cctx);
6447
6448 if (lhs->lhs_has_index)
6449 {
6450 int range = FALSE;
6451
6452 // Get member from list or dict. First compile the
6453 // index value.
6454 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6455 return FAIL;
6456 if (range)
6457 {
6458 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6459 var_start);
6460 return FAIL;
6461 }
6462
6463 // Get the member.
6464 if (compile_member(FALSE, cctx) == FAIL)
6465 return FAIL;
6466 }
6467 return OK;
6468}
6469
6470/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006471 * Assignment to a list or dict member, or ":unlet" for the item, using the
6472 * information in "lhs".
6473 * Returns OK or FAIL.
6474 */
6475 static int
6476compile_assign_unlet(
6477 char_u *var_start,
6478 lhs_T *lhs,
6479 int is_assign,
6480 type_T *rhs_type,
6481 cctx_T *cctx)
6482{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006483 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006484 garray_T *stack = &cctx->ctx_type_stack;
6485 int range = FALSE;
6486
Bram Moolenaar68452172021-04-12 21:21:02 +02006487 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006488 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006489 if (is_assign && range && lhs->lhs_type != &t_blob
6490 && lhs->lhs_type != &t_any)
6491 {
6492 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6493 return FAIL;
6494 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006495
6496 if (lhs->lhs_type == &t_any)
6497 {
6498 // Index on variable of unknown type: check at runtime.
6499 dest_type = VAR_ANY;
6500 }
6501 else
6502 {
6503 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006504 if (dest_type == VAR_DICT && range)
6505 {
6506 emsg(e_cannot_use_range_with_dictionary);
6507 return FAIL;
6508 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006509 if (dest_type == VAR_DICT
6510 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006511 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006512 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006513 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006514 type_T *type;
6515
6516 if (range)
6517 {
6518 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6519 if (need_type(type, &t_number,
6520 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006521 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006522 }
6523 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6524 if ((dest_type != VAR_BLOB || type != &t_special)
6525 && need_type(type, &t_number,
6526 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006527 return FAIL;
6528 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006529 }
6530
6531 // Load the dict or list. On the stack we then have:
6532 // - value (for assignment, not for :unlet)
6533 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006534 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006535 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006536 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6537 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006538
Bram Moolenaar68452172021-04-12 21:21:02 +02006539 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6540 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006541 {
6542 if (is_assign)
6543 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006544 if (range)
6545 {
6546 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6547 return FAIL;
6548 }
6549 else
6550 {
6551 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006552
Bram Moolenaar68452172021-04-12 21:21:02 +02006553 if (isn == NULL)
6554 return FAIL;
6555 isn->isn_arg.vartype = dest_type;
6556 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006557 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006558 else if (range)
6559 {
6560 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6561 return FAIL;
6562 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006563 else
6564 {
6565 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6566 return FAIL;
6567 }
6568 }
6569 else
6570 {
6571 emsg(_(e_indexable_type_required));
6572 return FAIL;
6573 }
6574
6575 return OK;
6576}
6577
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006578/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006579 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006580 * "let name"
6581 * "var name = expr"
6582 * "final name = expr"
6583 * "const name = expr"
6584 * "name = expr"
6585 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006586 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006587 * Return NULL for an error.
6588 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589 */
6590 static char_u *
6591compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6592{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006593 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006595 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596 char_u *ret = NULL;
6597 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006598 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006601 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 int oplen = 0;
6604 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006605 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006606 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006608 int is_decl = is_decl_command(cmdidx);
6609 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006610 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006612 // Skip over the "var" or "[var, var]" to get to any "=".
6613 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6614 if (p == NULL)
6615 return *arg == '[' ? arg : NULL;
6616
6617 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006618 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006619 // TODO: should we allow this, and figure out type inference from list
6620 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006621 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 return NULL;
6623 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006624 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626 sp = p;
6627 p = skipwhite(p);
6628 op = p;
6629 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006630
6631 if (var_count > 0 && oplen == 0)
6632 // can be something like "[1, 2]->func()"
6633 return arg;
6634
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006635 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006637 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006638 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006639 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006640 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6641 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006642 if (VIM_ISWHITE(eap->cmd[2]))
6643 {
6644 semsg(_(e_no_white_space_allowed_after_str_str),
6645 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6646 return NULL;
6647 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006648 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6649 oplen = 2;
6650 incdec = TRUE;
6651 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006653 if (heredoc)
6654 {
6655 list_T *l;
6656 listitem_T *li;
6657
6658 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006659 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006661 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006662 if (l == NULL)
6663 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664
Bram Moolenaar078269b2020-09-21 20:35:55 +02006665 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006666 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006667 // Push each line and the create the list.
6668 FOR_ALL_LIST_ITEMS(l, li)
6669 {
6670 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6671 li->li_tv.vval.v_string = NULL;
6672 }
6673 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675 list_free(l);
6676 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006677 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006679 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006680 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006681 char_u *wp;
6682
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006683 // for "[var, var] = expr" evaluate the expression here, loop over the
6684 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006685 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006686
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006687 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006688 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006689 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006690 if (compile_expr0(&p, cctx) == FAIL)
6691 return NULL;
6692 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006694 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006696 type_T *stacktype;
6697
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006698 stacktype = stack->ga_len == 0 ? &t_void
6699 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006700 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006702 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006703 goto theend;
6704 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006705 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006706 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006707 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006708 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006709 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6710 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006711 if (stacktype->tt_member != NULL)
6712 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006713 }
6714 }
6715
6716 /*
6717 * Loop over variables in "[var, var] = expr".
6718 * For "var = expr" and "let var: type" this is done only once.
6719 */
6720 if (var_count > 0)
6721 var_start = skipwhite(arg + 1); // skip over the "["
6722 else
6723 var_start = arg;
6724 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6725 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006726 int instr_count = -1;
6727
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006728 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6729 {
6730 // Ignore underscore in "[a, _, b] = list".
6731 if (var_count > 0)
6732 {
6733 var_start = skipwhite(var_start + 2);
6734 continue;
6735 }
6736 emsg(_(e_cannot_use_underscore_here));
6737 goto theend;
6738 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006739 vim_free(lhs.lhs_name);
6740
6741 /*
6742 * Figure out the LHS type and other properties.
6743 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006744 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6745 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006746 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006747 if (!heredoc)
6748 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006749 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006750 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006751 if (oplen > 0 && var_count == 0)
6752 {
6753 // skip over the "=" and the expression
6754 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006755 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006756 }
6757 }
6758 else if (oplen > 0)
6759 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006760 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006761 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006762
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006763 // for "+=", "*=", "..=" etc. first load the current value
6764 if (*op != '='
6765 && compile_load_lhs_with_index(&lhs, var_start,
6766 cctx) == FAIL)
6767 goto theend;
6768
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006769 // For "var = expr" evaluate the expression.
6770 if (var_count == 0)
6771 {
6772 int r;
6773
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006774 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006775 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006776 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006777 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006778 r = generate_PUSHNR(cctx, 1);
6779 }
6780 else
6781 {
6782 // Temporarily hide the new local variable here, it is
6783 // not available to this expression.
6784 if (lhs.lhs_new_local)
6785 --cctx->ctx_locals.ga_len;
6786 wp = op + oplen;
6787 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6788 {
6789 if (lhs.lhs_new_local)
6790 ++cctx->ctx_locals.ga_len;
6791 goto theend;
6792 }
6793 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006794 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006795 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006796 if (r == FAIL)
6797 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006798 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006799 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006800 else if (semicolon && var_idx == var_count - 1)
6801 {
6802 // For "[var; var] = expr" get the rest of the list
6803 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6804 goto theend;
6805 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006806 else
6807 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006808 // For "[var, var] = expr" get the "var_idx" item from the
6809 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006810 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006811 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006812 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006813
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006814 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006815 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006816 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006817 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006818 if ((rhs_type->tt_type == VAR_FUNC
6819 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006820 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006821 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006822 goto theend;
6823
Bram Moolenaar752fc692021-01-04 21:57:11 +01006824 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006825 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006826 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006827 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006828 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006829 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006830 }
6831 else
6832 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006833 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006834 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006835 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006836 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006837 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006838 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006839 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006840 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006841 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006842 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006843 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006844 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006845 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006846 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006847 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006848
Bram Moolenaar77709b12021-04-03 21:01:01 +02006849 // Without operator check type here, otherwise below.
6850 // Use the line number of the assignment.
6851 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006852 if (lhs.lhs_has_index)
6853 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006854 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006855 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006856 goto theend;
6857 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006858 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006859 else
6860 {
6861 type_T *lhs_type = lhs.lhs_member_type;
6862
6863 // Special case: assigning to @# can use a number or a
6864 // string.
6865 if (lhs_type == &t_number_or_string
6866 && rhs_type->tt_type == VAR_NUMBER)
6867 lhs_type = &t_number;
6868 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006869 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006870 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006871 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006872 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006873 else if (cmdidx == CMD_final)
6874 {
6875 emsg(_(e_final_requires_a_value));
6876 goto theend;
6877 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006878 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006880 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006881 goto theend;
6882 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006883 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006884 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006885 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006886 goto theend;
6887 }
6888 else
6889 {
6890 // variables are always initialized
6891 if (ga_grow(instr, 1) == FAIL)
6892 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006893 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006894 {
6895 case VAR_BOOL:
6896 generate_PUSHBOOL(cctx, VVAL_FALSE);
6897 break;
6898 case VAR_FLOAT:
6899#ifdef FEAT_FLOAT
6900 generate_PUSHF(cctx, 0.0);
6901#endif
6902 break;
6903 case VAR_STRING:
6904 generate_PUSHS(cctx, NULL);
6905 break;
6906 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006907 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006908 break;
6909 case VAR_FUNC:
6910 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6911 break;
6912 case VAR_LIST:
6913 generate_NEWLIST(cctx, 0);
6914 break;
6915 case VAR_DICT:
6916 generate_NEWDICT(cctx, 0);
6917 break;
6918 case VAR_JOB:
6919 generate_PUSHJOB(cctx, NULL);
6920 break;
6921 case VAR_CHANNEL:
6922 generate_PUSHCHANNEL(cctx, NULL);
6923 break;
6924 case VAR_NUMBER:
6925 case VAR_UNKNOWN:
6926 case VAR_ANY:
6927 case VAR_PARTIAL:
6928 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006929 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006930 case VAR_SPECIAL: // cannot happen
6931 generate_PUSHNR(cctx, 0);
6932 break;
6933 }
6934 }
6935 if (var_count == 0)
6936 end = p;
6937 }
6938
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006939 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006940 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006941 break;
6942
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006943 if (oplen > 0 && *op != '=')
6944 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006945 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006946 type_T *stacktype;
6947
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006948 if (*op == '.')
6949 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006950 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006951 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006952 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006953 if (
6954#ifdef FEAT_FLOAT
6955 // If variable is float operation with number is OK.
6956 !(expected == &t_float && stacktype == &t_number) &&
6957#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006958 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006959 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006960 goto theend;
6961
6962 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006963 {
6964 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6965 goto theend;
6966 }
6967 else if (*op == '+')
6968 {
6969 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006970 operator_type(lhs.lhs_member_type, stacktype),
6971 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006972 goto theend;
6973 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006974 else if (generate_two_op(cctx, op) == FAIL)
6975 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977
Bram Moolenaar752fc692021-01-04 21:57:11 +01006978 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006979 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006980 // Use the info in "lhs" to store the value at the index in the
6981 // list or dict.
6982 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6983 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006984 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006985 }
6986 else
6987 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006988 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006989 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006990 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006991 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006992 generate_LOCKCONST(cctx);
6993
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006994 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006995 && (lhs.lhs_type->tt_type == VAR_DICT
6996 || lhs.lhs_type->tt_type == VAR_LIST)
6997 && lhs.lhs_type->tt_member != NULL
6998 && lhs.lhs_type->tt_member != &t_any
6999 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007000 // Set the type in the list or dict, so that it can be checked,
7001 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007002 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007003
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007004 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
7005 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007006 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007007
7008 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007009 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007011
7012 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02007013 if (var_count > 0 && !semicolon)
7014 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007015 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007016 goto theend;
7017 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007018
Bram Moolenaarb2097502020-07-19 17:17:02 +02007019 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020
7021theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007022 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 return ret;
7024}
7025
7026/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007027 * Check for an assignment at "eap->cmd", compile it if found.
7028 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7029 */
7030 static int
7031may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7032{
7033 char_u *pskip;
7034 char_u *p;
7035
7036 // Assuming the command starts with a variable or function name,
7037 // find what follows.
7038 // Skip over "var.member", "var[idx]" and the like.
7039 // Also "&opt = val", "$ENV = val" and "@r = val".
7040 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7041 ? eap->cmd + 1 : eap->cmd;
7042 p = to_name_end(pskip, TRUE);
7043 if (p > eap->cmd && *p != NUL)
7044 {
7045 char_u *var_end;
7046 int oplen;
7047 int heredoc;
7048
7049 if (eap->cmd[0] == '@')
7050 var_end = eap->cmd + 2;
7051 else
7052 var_end = find_name_end(pskip, NULL, NULL,
7053 FNE_CHECK_START | FNE_INCL_BR);
7054 oplen = assignment_len(skipwhite(var_end), &heredoc);
7055 if (oplen > 0)
7056 {
7057 size_t len = p - eap->cmd;
7058
7059 // Recognize an assignment if we recognize the variable
7060 // name:
7061 // "g:var = expr"
7062 // "local = expr" where "local" is a local var.
7063 // "script = expr" where "script" is a script-local var.
7064 // "import = expr" where "import" is an imported var
7065 // "&opt = expr"
7066 // "$ENV = expr"
7067 // "@r = expr"
7068 if (*eap->cmd == '&'
7069 || *eap->cmd == '$'
7070 || *eap->cmd == '@'
7071 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007072 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007073 {
7074 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7075 if (*line == NULL || *line == eap->cmd)
7076 return FAIL;
7077 return OK;
7078 }
7079 }
7080 }
7081
7082 if (*eap->cmd == '[')
7083 {
7084 // [var, var] = expr
7085 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7086 if (*line == NULL)
7087 return FAIL;
7088 if (*line != eap->cmd)
7089 return OK;
7090 }
7091 return NOTDONE;
7092}
7093
7094/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007095 * Check if "name" can be "unlet".
7096 */
7097 int
7098check_vim9_unlet(char_u *name)
7099{
7100 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7101 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007102 // "unlet s:var" is allowed in legacy script.
7103 if (*name == 's' && !script_is_vim9())
7104 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007105 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007106 return FAIL;
7107 }
7108 return OK;
7109}
7110
7111/*
7112 * Callback passed to ex_unletlock().
7113 */
7114 static int
7115compile_unlet(
7116 lval_T *lvp,
7117 char_u *name_end,
7118 exarg_T *eap,
7119 int deep UNUSED,
7120 void *coookie)
7121{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007122 cctx_T *cctx = coookie;
7123 char_u *p = lvp->ll_name;
7124 int cc = *name_end;
7125 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007126
Bram Moolenaar752fc692021-01-04 21:57:11 +01007127 if (cctx->ctx_skip == SKIP_YES)
7128 return OK;
7129
7130 *name_end = NUL;
7131 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007132 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007133 // :unlet $ENV_VAR
7134 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7135 }
7136 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7137 {
7138 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007139
Bram Moolenaar752fc692021-01-04 21:57:11 +01007140 // This is similar to assigning: lookup the list/dict, compile the
7141 // idx/key. Then instead of storing the value unlet the item.
7142 // unlet {list}[idx]
7143 // unlet {dict}[key] dict.key
7144 //
7145 // Figure out the LHS type and other properties.
7146 //
7147 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7148
7149 // : unlet an indexed item
7150 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007151 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007152 iemsg("called compile_lhs() without an index");
7153 ret = FAIL;
7154 }
7155 else
7156 {
7157 // Use the info in "lhs" to unlet the item at the index in the
7158 // list or dict.
7159 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007160 }
7161
Bram Moolenaar752fc692021-01-04 21:57:11 +01007162 vim_free(lhs.lhs_name);
7163 }
7164 else if (check_vim9_unlet(p) == FAIL)
7165 {
7166 ret = FAIL;
7167 }
7168 else
7169 {
7170 // Normal name. Only supports g:, w:, t: and b: namespaces.
7171 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007172 }
7173
Bram Moolenaar752fc692021-01-04 21:57:11 +01007174 *name_end = cc;
7175 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007176}
7177
7178/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007179 * Callback passed to ex_unletlock().
7180 */
7181 static int
7182compile_lock_unlock(
7183 lval_T *lvp,
7184 char_u *name_end,
7185 exarg_T *eap,
7186 int deep UNUSED,
7187 void *coookie)
7188{
7189 cctx_T *cctx = coookie;
7190 int cc = *name_end;
7191 char_u *p = lvp->ll_name;
7192 int ret = OK;
7193 size_t len;
7194 char_u *buf;
7195
7196 if (cctx->ctx_skip == SKIP_YES)
7197 return OK;
7198
7199 // Cannot use :lockvar and :unlockvar on local variables.
7200 if (p[1] != ':')
7201 {
7202 char_u *end = skip_var_one(p, FALSE);
7203
7204 if (lookup_local(p, end - p, NULL, cctx) == OK)
7205 {
7206 emsg(_(e_cannot_lock_unlock_local_variable));
7207 return FAIL;
7208 }
7209 }
7210
7211 // Checking is done at runtime.
7212 *name_end = NUL;
7213 len = name_end - p + 20;
7214 buf = alloc(len);
7215 if (buf == NULL)
7216 ret = FAIL;
7217 else
7218 {
7219 vim_snprintf((char *)buf, len, "%s %s",
7220 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7221 p);
7222 ret = generate_EXEC(cctx, buf);
7223
7224 vim_free(buf);
7225 *name_end = cc;
7226 }
7227 return ret;
7228}
7229
7230/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007231 * compile "unlet var", "lock var" and "unlock var"
7232 * "arg" points to "var".
7233 */
7234 static char_u *
7235compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7236{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007237 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7238 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7239 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007240 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7241}
7242
7243/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244 * Compile an :import command.
7245 */
7246 static char_u *
7247compile_import(char_u *arg, cctx_T *cctx)
7248{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007249 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007250}
7251
7252/*
7253 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7254 */
7255 static int
7256compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7257{
7258 garray_T *instr = &cctx->ctx_instr;
7259 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7260
7261 if (endlabel == NULL)
7262 return FAIL;
7263 endlabel->el_next = *el;
7264 *el = endlabel;
7265 endlabel->el_end_label = instr->ga_len;
7266
7267 generate_JUMP(cctx, when, 0);
7268 return OK;
7269}
7270
7271 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007272compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273{
7274 garray_T *instr = &cctx->ctx_instr;
7275
7276 while (*el != NULL)
7277 {
7278 endlabel_T *cur = (*el);
7279 isn_T *isn;
7280
7281 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007282 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007283 *el = cur->el_next;
7284 vim_free(cur);
7285 }
7286}
7287
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007288 static void
7289compile_free_jump_to_end(endlabel_T **el)
7290{
7291 while (*el != NULL)
7292 {
7293 endlabel_T *cur = (*el);
7294
7295 *el = cur->el_next;
7296 vim_free(cur);
7297 }
7298}
7299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007300/*
7301 * Create a new scope and set up the generic items.
7302 */
7303 static scope_T *
7304new_scope(cctx_T *cctx, scopetype_T type)
7305{
7306 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7307
7308 if (scope == NULL)
7309 return NULL;
7310 scope->se_outer = cctx->ctx_scope;
7311 cctx->ctx_scope = scope;
7312 scope->se_type = type;
7313 scope->se_local_count = cctx->ctx_locals.ga_len;
7314 return scope;
7315}
7316
7317/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007318 * Free the current scope and go back to the outer scope.
7319 */
7320 static void
7321drop_scope(cctx_T *cctx)
7322{
7323 scope_T *scope = cctx->ctx_scope;
7324
7325 if (scope == NULL)
7326 {
7327 iemsg("calling drop_scope() without a scope");
7328 return;
7329 }
7330 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007331 switch (scope->se_type)
7332 {
7333 case IF_SCOPE:
7334 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7335 case FOR_SCOPE:
7336 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7337 case WHILE_SCOPE:
7338 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7339 case TRY_SCOPE:
7340 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7341 case NO_SCOPE:
7342 case BLOCK_SCOPE:
7343 break;
7344 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007345 vim_free(scope);
7346}
7347
7348/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349 * compile "if expr"
7350 *
7351 * "if expr" Produces instructions:
7352 * EVAL expr Push result of "expr"
7353 * JUMP_IF_FALSE end
7354 * ... body ...
7355 * end:
7356 *
7357 * "if expr | else" Produces instructions:
7358 * EVAL expr Push result of "expr"
7359 * JUMP_IF_FALSE else
7360 * ... body ...
7361 * JUMP_ALWAYS end
7362 * else:
7363 * ... body ...
7364 * end:
7365 *
7366 * "if expr1 | elseif expr2 | else" Produces instructions:
7367 * EVAL expr Push result of "expr"
7368 * JUMP_IF_FALSE elseif
7369 * ... body ...
7370 * JUMP_ALWAYS end
7371 * elseif:
7372 * EVAL expr Push result of "expr"
7373 * JUMP_IF_FALSE else
7374 * ... body ...
7375 * JUMP_ALWAYS end
7376 * else:
7377 * ... body ...
7378 * end:
7379 */
7380 static char_u *
7381compile_if(char_u *arg, cctx_T *cctx)
7382{
7383 char_u *p = arg;
7384 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007385 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007387 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007388 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007389
Bram Moolenaara5565e42020-05-09 15:44:01 +02007390 CLEAR_FIELD(ppconst);
7391 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007392 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007393 clear_ppconst(&ppconst);
7394 return NULL;
7395 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007396 if (!ends_excmd2(arg, skipwhite(p)))
7397 {
7398 semsg(_(e_trailing_arg), p);
7399 return NULL;
7400 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007401 if (cctx->ctx_skip == SKIP_YES)
7402 clear_ppconst(&ppconst);
7403 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007404 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007405 int error = FALSE;
7406 int v;
7407
Bram Moolenaara5565e42020-05-09 15:44:01 +02007408 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007409 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007410 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007411 if (error)
7412 return NULL;
7413 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007414 }
7415 else
7416 {
7417 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007418 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007419 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007420 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007421 if (bool_on_stack(cctx) == FAIL)
7422 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007423 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424
Bram Moolenaara91a7132021-03-25 21:12:15 +01007425 // CMDMOD_REV must come before the jump
7426 generate_undo_cmdmods(cctx);
7427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428 scope = new_scope(cctx, IF_SCOPE);
7429 if (scope == NULL)
7430 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007431 scope->se_skip_save = skip_save;
7432 // "is_had_return" will be reset if any block does not end in :return
7433 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007435 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007436 {
7437 // "where" is set when ":elseif", "else" or ":endif" is found
7438 scope->se_u.se_if.is_if_label = instr->ga_len;
7439 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7440 }
7441 else
7442 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443
Bram Moolenaarced68a02021-01-24 17:53:47 +01007444#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007445 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007446 && skip_save != SKIP_YES)
7447 {
7448 // generated a profile start, need to generate a profile end, since it
7449 // won't be done after returning
7450 cctx->ctx_skip = SKIP_NOT;
7451 generate_instr(cctx, ISN_PROF_END);
7452 cctx->ctx_skip = SKIP_YES;
7453 }
7454#endif
7455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456 return p;
7457}
7458
7459 static char_u *
7460compile_elseif(char_u *arg, cctx_T *cctx)
7461{
7462 char_u *p = arg;
7463 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007464 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007465 isn_T *isn;
7466 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007467 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007468 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469
7470 if (scope == NULL || scope->se_type != IF_SCOPE)
7471 {
7472 emsg(_(e_elseif_without_if));
7473 return NULL;
7474 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007475 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007476 if (!cctx->ctx_had_return)
7477 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478
Bram Moolenaarced68a02021-01-24 17:53:47 +01007479 if (cctx->ctx_skip == SKIP_NOT)
7480 {
7481 // previous block was executed, this one and following will not
7482 cctx->ctx_skip = SKIP_YES;
7483 scope->se_u.se_if.is_seen_skip_not = TRUE;
7484 }
7485 if (scope->se_u.se_if.is_seen_skip_not)
7486 {
7487 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007488 // Do not count the "elseif" for profiling and cmdmod
7489 instr->ga_len = current_instr_idx(cctx);
7490
Bram Moolenaarced68a02021-01-24 17:53:47 +01007491 skip_expr_cctx(&p, cctx);
7492 return p;
7493 }
7494
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007495 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007496 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007497 int moved_cmdmod = FALSE;
7498
7499 // Move any CMDMOD instruction to after the jump
7500 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7501 {
7502 if (ga_grow(instr, 1) == FAIL)
7503 return NULL;
7504 ((isn_T *)instr->ga_data)[instr->ga_len] =
7505 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7506 --instr->ga_len;
7507 moved_cmdmod = TRUE;
7508 }
7509
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007510 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007512 return NULL;
7513 // previous "if" or "elseif" jumps here
7514 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7515 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007516 if (moved_cmdmod)
7517 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007519
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007520 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007521 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007522 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007523 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007524 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007525#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007526 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007527 {
7528 // the previous block was skipped, need to profile this line
7529 generate_instr(cctx, ISN_PROF_START);
7530 instr_count = instr->ga_len;
7531 }
7532#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007533 if (cctx->ctx_compile_type == CT_DEBUG)
7534 {
7535 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007536 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007537 instr_count = instr->ga_len;
7538 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007539 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007540 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007541 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007542 clear_ppconst(&ppconst);
7543 return NULL;
7544 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007545 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007546 if (!ends_excmd2(arg, skipwhite(p)))
7547 {
7548 semsg(_(e_trailing_arg), p);
7549 return NULL;
7550 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007551 if (scope->se_skip_save == SKIP_YES)
7552 clear_ppconst(&ppconst);
7553 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007554 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007555 int error = FALSE;
7556 int v;
7557
Bram Moolenaar7f141552020-05-09 17:35:53 +02007558 // The expression results in a constant.
7559 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007560 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7561 if (error)
7562 return NULL;
7563 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007564 clear_ppconst(&ppconst);
7565 scope->se_u.se_if.is_if_label = -1;
7566 }
7567 else
7568 {
7569 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007570 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007571 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007572 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007573 if (bool_on_stack(cctx) == FAIL)
7574 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007575
Bram Moolenaara91a7132021-03-25 21:12:15 +01007576 // CMDMOD_REV must come before the jump
7577 generate_undo_cmdmods(cctx);
7578
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007579 // "where" is set when ":elseif", "else" or ":endif" is found
7580 scope->se_u.se_if.is_if_label = instr->ga_len;
7581 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007583
7584 return p;
7585}
7586
7587 static char_u *
7588compile_else(char_u *arg, cctx_T *cctx)
7589{
7590 char_u *p = arg;
7591 garray_T *instr = &cctx->ctx_instr;
7592 isn_T *isn;
7593 scope_T *scope = cctx->ctx_scope;
7594
7595 if (scope == NULL || scope->se_type != IF_SCOPE)
7596 {
7597 emsg(_(e_else_without_if));
7598 return NULL;
7599 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007600 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007601 if (!cctx->ctx_had_return)
7602 scope->se_u.se_if.is_had_return = FALSE;
7603 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604
Bram Moolenaarced68a02021-01-24 17:53:47 +01007605#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007606 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007607 {
7608 if (cctx->ctx_skip == SKIP_NOT
7609 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7610 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007611 // the previous block was executed, do not count "else" for
7612 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007613 --instr->ga_len;
7614 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7615 {
7616 // the previous block was not executed, this one will, do count the
7617 // "else" for profiling
7618 cctx->ctx_skip = SKIP_NOT;
7619 generate_instr(cctx, ISN_PROF_END);
7620 generate_instr(cctx, ISN_PROF_START);
7621 cctx->ctx_skip = SKIP_YES;
7622 }
7623 }
7624#endif
7625
7626 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007627 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007628 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007629 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007630 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007631 if (!cctx->ctx_had_return
7632 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7633 JUMP_ALWAYS, cctx) == FAIL)
7634 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007635 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007636
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007637 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007638 {
7639 if (scope->se_u.se_if.is_if_label >= 0)
7640 {
7641 // previous "if" or "elseif" jumps here
7642 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7643 isn->isn_arg.jump.jump_where = instr->ga_len;
7644 scope->se_u.se_if.is_if_label = -1;
7645 }
7646 }
7647
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007648 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007649 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7650 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651
7652 return p;
7653}
7654
7655 static char_u *
7656compile_endif(char_u *arg, cctx_T *cctx)
7657{
7658 scope_T *scope = cctx->ctx_scope;
7659 ifscope_T *ifscope;
7660 garray_T *instr = &cctx->ctx_instr;
7661 isn_T *isn;
7662
Bram Moolenaarfa984412021-03-25 22:15:28 +01007663 if (misplaced_cmdmod(cctx))
7664 return NULL;
7665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007666 if (scope == NULL || scope->se_type != IF_SCOPE)
7667 {
7668 emsg(_(e_endif_without_if));
7669 return NULL;
7670 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007671 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007672 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007673 if (!cctx->ctx_had_return)
7674 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007675
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007676 if (scope->se_u.se_if.is_if_label >= 0)
7677 {
7678 // previous "if" or "elseif" jumps here
7679 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7680 isn->isn_arg.jump.jump_where = instr->ga_len;
7681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007682 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007683 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007684
7685#ifdef FEAT_PROFILE
7686 // even when skipping we count the endif as executed, unless the block it's
7687 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007688 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007689 && scope->se_skip_save != SKIP_YES)
7690 {
7691 cctx->ctx_skip = SKIP_NOT;
7692 generate_instr(cctx, ISN_PROF_START);
7693 }
7694#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007695 cctx->ctx_skip = scope->se_skip_save;
7696
7697 // If all the blocks end in :return and there is an :else then the
7698 // had_return flag is set.
7699 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007701 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007702 return arg;
7703}
7704
7705/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007706 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007707 *
7708 * Produces instructions:
7709 * PUSHNR -1
7710 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007711 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007712 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7713 * - if beyond end, jump to "end"
7714 * - otherwise get item from list and push it
7715 * STORE var Store item in "var"
7716 * ... body ...
7717 * JUMP top Jump back to repeat
7718 * end: DROP Drop the result of "expr"
7719 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007720 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7721 * UNPACK 2 Split item in 2
7722 * STORE var1 Store item in "var1"
7723 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007724 */
7725 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007726compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007728 char_u *arg;
7729 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007730 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007732 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007733 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007734 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007735 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007736 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007738 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007739 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007740 lvar_T *loop_lvar; // loop iteration variable
7741 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007742 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007743 type_T *item_type = &t_any;
7744 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745
Bram Moolenaar792f7862020-11-23 08:31:18 +01007746 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007747 if (p == NULL)
7748 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007749 if (var_count == 0)
7750 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007751 else
7752 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753
7754 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007755 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007756 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7757 return NULL;
7758 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759 {
7760 emsg(_(e_missing_in));
7761 return NULL;
7762 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007763 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007764 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7765 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007766
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007767 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7768 // instruction.
7769 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7770 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7771 .isn_type == ISN_DEBUG)
7772 --instr->ga_len;
7773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007774 scope = new_scope(cctx, FOR_SCOPE);
7775 if (scope == NULL)
7776 return NULL;
7777
Bram Moolenaar792f7862020-11-23 08:31:18 +01007778 // Reserve a variable to store the loop iteration counter and initialize it
7779 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007780 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7781 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007782 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007783 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007784 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007786 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007787 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007788
7789 // compile "expr", it remains on the stack until "endfor"
7790 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007791 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007792 {
7793 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007794 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007795 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007796 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007797
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007798 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007799 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007800 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007801 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007802 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007804 semsg(_(e_for_loop_on_str_not_supported),
7805 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007806 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807 return NULL;
7808 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007809
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007810 if (vartype->tt_type == VAR_STRING)
7811 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007812 else if (vartype->tt_type == VAR_BLOB)
7813 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007814 else if (vartype->tt_type == VAR_LIST
7815 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007816 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007817 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007818 item_type = vartype->tt_member;
7819 else if (vartype->tt_member->tt_type == VAR_LIST
7820 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007821 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007822 item_type = vartype->tt_member->tt_member;
7823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007825 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007826 generate_undo_cmdmods(cctx);
7827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007828 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007829 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007830
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007831 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007832
Bram Moolenaar792f7862020-11-23 08:31:18 +01007833 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007834 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007835 {
7836 generate_UNPACK(cctx, var_count, semicolon);
7837 arg = skipwhite(arg + 1); // skip white after '['
7838
7839 // the list item is replaced by a number of items
7840 if (ga_grow(stack, var_count - 1) == FAIL)
7841 {
7842 drop_scope(cctx);
7843 return NULL;
7844 }
7845 --stack->ga_len;
7846 for (idx = 0; idx < var_count; ++idx)
7847 {
7848 ((type_T **)stack->ga_data)[stack->ga_len] =
7849 (semicolon && idx == 0) ? vartype : item_type;
7850 ++stack->ga_len;
7851 }
7852 }
7853
7854 for (idx = 0; idx < var_count; ++idx)
7855 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007856 assign_dest_T dest = dest_local;
7857 int opt_flags = 0;
7858 int vimvaridx = -1;
7859 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007860 type_T *lhs_type = &t_any;
7861 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007862
7863 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007864 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007865 name = vim_strnsave(arg, varlen);
7866 if (name == NULL)
7867 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007868 if (*p == ':')
7869 {
7870 p = skipwhite(p + 1);
7871 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7872 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007873
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007874 // TODO: script var not supported?
7875 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7876 &vimvaridx, &type, cctx) == FAIL)
7877 goto failed;
7878 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007879 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007880 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7881 0, 0, type, name) == FAIL)
7882 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007883 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007884 else if (varlen == 1 && *arg == '_')
7885 {
7886 // Assigning to "_": drop the value.
7887 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7888 goto failed;
7889 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007890 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007891 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007892 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007893 {
7894 semsg(_(e_variable_already_declared), arg);
7895 goto failed;
7896 }
7897
Bram Moolenaarea870692020-12-02 14:24:30 +01007898 if (STRNCMP(name, "s:", 2) == 0)
7899 {
7900 semsg(_(e_cannot_declare_script_variable_in_function), name);
7901 goto failed;
7902 }
7903
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007904 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02007905 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007906 where.wt_variable = TRUE;
7907 if (lhs_type == &t_any)
7908 lhs_type = item_type;
7909 else if (item_type != &t_unknown
Bram Moolenaar444d8782021-06-26 12:40:56 +02007910 && !(var_list && item_type == &t_any)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007911 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7912 goto failed;
7913 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007914 if (var_lvar == NULL)
7915 // out of memory or used as an argument
7916 goto failed;
7917
7918 if (semicolon && idx == var_count - 1)
7919 var_lvar->lv_type = vartype;
7920 else
7921 var_lvar->lv_type = item_type;
7922 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7923 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007924
7925 if (*p == ',' || *p == ';')
7926 ++p;
7927 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007928 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007929 }
7930
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007931 if (cctx->ctx_compile_type == CT_DEBUG)
7932 // Add ISN_DEBUG here, so that the loop variables can be inspected.
7933 generate_instr_debug(cctx);
7934
Bram Moolenaar792f7862020-11-23 08:31:18 +01007935 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007936
7937failed:
7938 vim_free(name);
7939 drop_scope(cctx);
7940 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007941}
7942
7943/*
7944 * compile "endfor"
7945 */
7946 static char_u *
7947compile_endfor(char_u *arg, cctx_T *cctx)
7948{
7949 garray_T *instr = &cctx->ctx_instr;
7950 scope_T *scope = cctx->ctx_scope;
7951 forscope_T *forscope;
7952 isn_T *isn;
7953
Bram Moolenaarfa984412021-03-25 22:15:28 +01007954 if (misplaced_cmdmod(cctx))
7955 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007957 if (scope == NULL || scope->se_type != FOR_SCOPE)
7958 {
7959 emsg(_(e_for));
7960 return NULL;
7961 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007962 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007963 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007964 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007965
7966 // At end of ":for" scope jump back to the FOR instruction.
7967 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7968
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007969 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007970 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7971 isn->isn_arg.forloop.for_end = instr->ga_len;
7972
7973 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007974 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007975
7976 // Below the ":for" scope drop the "expr" list from the stack.
7977 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7978 return NULL;
7979
7980 vim_free(scope);
7981
7982 return arg;
7983}
7984
7985/*
7986 * compile "while expr"
7987 *
7988 * Produces instructions:
7989 * top: EVAL expr Push result of "expr"
7990 * JUMP_IF_FALSE end jump if false
7991 * ... body ...
7992 * JUMP top Jump back to repeat
7993 * end:
7994 *
7995 */
7996 static char_u *
7997compile_while(char_u *arg, cctx_T *cctx)
7998{
7999 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008000 scope_T *scope;
8001
8002 scope = new_scope(cctx, WHILE_SCOPE);
8003 if (scope == NULL)
8004 return NULL;
8005
Bram Moolenaara91a7132021-03-25 21:12:15 +01008006 // "endwhile" jumps back here, one before when profiling or using cmdmods
8007 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008008
8009 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008010 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008012 if (!ends_excmd2(arg, skipwhite(p)))
8013 {
8014 semsg(_(e_trailing_arg), p);
8015 return NULL;
8016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008017
Bram Moolenaar13106602020-10-04 16:06:05 +02008018 if (bool_on_stack(cctx) == FAIL)
8019 return FAIL;
8020
Bram Moolenaara91a7132021-03-25 21:12:15 +01008021 // CMDMOD_REV must come before the jump
8022 generate_undo_cmdmods(cctx);
8023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008024 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008025 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008026 JUMP_IF_FALSE, cctx) == FAIL)
8027 return FAIL;
8028
8029 return p;
8030}
8031
8032/*
8033 * compile "endwhile"
8034 */
8035 static char_u *
8036compile_endwhile(char_u *arg, cctx_T *cctx)
8037{
8038 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008039 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008040
Bram Moolenaarfa984412021-03-25 22:15:28 +01008041 if (misplaced_cmdmod(cctx))
8042 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008043 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8044 {
8045 emsg(_(e_while));
8046 return NULL;
8047 }
8048 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008049 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008050
Bram Moolenaarf002a412021-01-24 13:34:18 +01008051#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008052 // count the endwhile before jumping
8053 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008054#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008055
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008056 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008057 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008058
8059 // Fill in the "end" label in the WHILE statement so it can jump here.
8060 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008061 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8062 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008063
8064 vim_free(scope);
8065
8066 return arg;
8067}
8068
8069/*
8070 * compile "continue"
8071 */
8072 static char_u *
8073compile_continue(char_u *arg, cctx_T *cctx)
8074{
8075 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008076 int try_scopes = 0;
8077 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008078
8079 for (;;)
8080 {
8081 if (scope == NULL)
8082 {
8083 emsg(_(e_continue));
8084 return NULL;
8085 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008086 if (scope->se_type == FOR_SCOPE)
8087 {
8088 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008089 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008090 }
8091 if (scope->se_type == WHILE_SCOPE)
8092 {
8093 loop_label = scope->se_u.se_while.ws_top_label;
8094 break;
8095 }
8096 if (scope->se_type == TRY_SCOPE)
8097 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008098 scope = scope->se_outer;
8099 }
8100
Bram Moolenaarc150c092021-02-13 15:02:46 +01008101 if (try_scopes > 0)
8102 // Inside one or more try/catch blocks we first need to jump to the
8103 // "finally" or "endtry" to cleanup.
8104 generate_TRYCONT(cctx, try_scopes, loop_label);
8105 else
8106 // Jump back to the FOR or WHILE instruction.
8107 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008109 return arg;
8110}
8111
8112/*
8113 * compile "break"
8114 */
8115 static char_u *
8116compile_break(char_u *arg, cctx_T *cctx)
8117{
8118 scope_T *scope = cctx->ctx_scope;
8119 endlabel_T **el;
8120
8121 for (;;)
8122 {
8123 if (scope == NULL)
8124 {
8125 emsg(_(e_break));
8126 return NULL;
8127 }
8128 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8129 break;
8130 scope = scope->se_outer;
8131 }
8132
8133 // Jump to the end of the FOR or WHILE loop.
8134 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008135 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008136 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008137 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008138 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8139 return FAIL;
8140
8141 return arg;
8142}
8143
8144/*
8145 * compile "{" start of block
8146 */
8147 static char_u *
8148compile_block(char_u *arg, cctx_T *cctx)
8149{
8150 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8151 return NULL;
8152 return skipwhite(arg + 1);
8153}
8154
8155/*
8156 * compile end of block: drop one scope
8157 */
8158 static void
8159compile_endblock(cctx_T *cctx)
8160{
8161 scope_T *scope = cctx->ctx_scope;
8162
8163 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008164 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008165 vim_free(scope);
8166}
8167
8168/*
8169 * compile "try"
8170 * Creates a new scope for the try-endtry, pointing to the first catch and
8171 * finally.
8172 * Creates another scope for the "try" block itself.
8173 * TRY instruction sets up exception handling at runtime.
8174 *
8175 * "try"
8176 * TRY -> catch1, -> finally push trystack entry
8177 * ... try block
8178 * "throw {exception}"
8179 * EVAL {exception}
8180 * THROW create exception
8181 * ... try block
8182 * " catch {expr}"
8183 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008184 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008185 * EVAL {expr}
8186 * MATCH
8187 * JUMP nomatch -> catch2
8188 * CATCH remove exception
8189 * ... catch block
8190 * " catch"
8191 * JUMP -> finally
8192 * catch2: CATCH remove exception
8193 * ... catch block
8194 * " finally"
8195 * finally:
8196 * ... finally block
8197 * " endtry"
8198 * ENDTRY pop trystack entry, may rethrow
8199 */
8200 static char_u *
8201compile_try(char_u *arg, cctx_T *cctx)
8202{
8203 garray_T *instr = &cctx->ctx_instr;
8204 scope_T *try_scope;
8205 scope_T *scope;
8206
Bram Moolenaarfa984412021-03-25 22:15:28 +01008207 if (misplaced_cmdmod(cctx))
8208 return NULL;
8209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008210 // scope that holds the jumps that go to catch/finally/endtry
8211 try_scope = new_scope(cctx, TRY_SCOPE);
8212 if (try_scope == NULL)
8213 return NULL;
8214
Bram Moolenaar69f70502021-01-01 16:10:46 +01008215 if (cctx->ctx_skip != SKIP_YES)
8216 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008217 isn_T *isn;
8218
8219 // "try_catch" is set when the first ":catch" is found or when no catch
8220 // is found and ":finally" is found.
8221 // "try_finally" is set when ":finally" is found
8222 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008223 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008224 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8225 return NULL;
8226 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8227 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008228 return NULL;
8229 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008230
8231 // scope for the try block itself
8232 scope = new_scope(cctx, BLOCK_SCOPE);
8233 if (scope == NULL)
8234 return NULL;
8235
8236 return arg;
8237}
8238
8239/*
8240 * compile "catch {expr}"
8241 */
8242 static char_u *
8243compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8244{
8245 scope_T *scope = cctx->ctx_scope;
8246 garray_T *instr = &cctx->ctx_instr;
8247 char_u *p;
8248 isn_T *isn;
8249
Bram Moolenaarfa984412021-03-25 22:15:28 +01008250 if (misplaced_cmdmod(cctx))
8251 return NULL;
8252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008253 // end block scope from :try or :catch
8254 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8255 compile_endblock(cctx);
8256 scope = cctx->ctx_scope;
8257
8258 // Error if not in a :try scope
8259 if (scope == NULL || scope->se_type != TRY_SCOPE)
8260 {
8261 emsg(_(e_catch));
8262 return NULL;
8263 }
8264
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008265 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008266 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008267 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008268 return NULL;
8269 }
8270
Bram Moolenaar69f70502021-01-01 16:10:46 +01008271 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008272 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008273#ifdef FEAT_PROFILE
8274 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008275 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008276 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008277 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008278 .isn_type == ISN_PROF_START)
8279 --instr->ga_len;
8280#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008281 // Jump from end of previous block to :finally or :endtry
8282 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8283 JUMP_ALWAYS, cctx) == FAIL)
8284 return NULL;
8285
8286 // End :try or :catch scope: set value in ISN_TRY instruction
8287 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008288 if (isn->isn_arg.try.try_ref->try_catch == 0)
8289 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008290 if (scope->se_u.se_try.ts_catch_label != 0)
8291 {
8292 // Previous catch without match jumps here
8293 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8294 isn->isn_arg.jump.jump_where = instr->ga_len;
8295 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008296#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008297 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008298 {
8299 // a "throw" that jumps here needs to be counted
8300 generate_instr(cctx, ISN_PROF_END);
8301 // the "catch" is also counted
8302 generate_instr(cctx, ISN_PROF_START);
8303 }
8304#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008305 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008306 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008307 }
8308
8309 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008310 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008311 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008312 scope->se_u.se_try.ts_caught_all = TRUE;
8313 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008314 }
8315 else
8316 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008317 char_u *end;
8318 char_u *pat;
8319 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008320 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008321 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008323 // Push v:exception, push {expr} and MATCH
8324 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8325
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008326 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008327 if (*end != *p)
8328 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008329 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008330 vim_free(tofree);
8331 return FAIL;
8332 }
8333 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008334 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008335 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008336 len = (int)(end - tofree);
8337 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008338 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008339 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008340 if (pat == NULL)
8341 return FAIL;
8342 if (generate_PUSHS(cctx, pat) == FAIL)
8343 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008345 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8346 return NULL;
8347
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008348 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008349 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8350 return NULL;
8351 }
8352
Bram Moolenaar69f70502021-01-01 16:10:46 +01008353 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008354 return NULL;
8355
8356 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8357 return NULL;
8358 return p;
8359}
8360
8361 static char_u *
8362compile_finally(char_u *arg, cctx_T *cctx)
8363{
8364 scope_T *scope = cctx->ctx_scope;
8365 garray_T *instr = &cctx->ctx_instr;
8366 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008367 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008368
Bram Moolenaarfa984412021-03-25 22:15:28 +01008369 if (misplaced_cmdmod(cctx))
8370 return NULL;
8371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008372 // end block scope from :try or :catch
8373 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8374 compile_endblock(cctx);
8375 scope = cctx->ctx_scope;
8376
8377 // Error if not in a :try scope
8378 if (scope == NULL || scope->se_type != TRY_SCOPE)
8379 {
8380 emsg(_(e_finally));
8381 return NULL;
8382 }
8383
8384 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008385 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008386 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008387 {
8388 emsg(_(e_finally_dup));
8389 return NULL;
8390 }
8391
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008392 this_instr = instr->ga_len;
8393#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008394 if (cctx->ctx_compile_type == CT_PROFILE
8395 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008396 .isn_type == ISN_PROF_START)
8397 // jump to the profile start of the "finally"
8398 --this_instr;
8399#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008400
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008401 // Fill in the "end" label in jumps at the end of the blocks.
8402 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8403 this_instr, cctx);
8404
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008405 // If there is no :catch then an exception jumps to :finally.
8406 if (isn->isn_arg.try.try_ref->try_catch == 0)
8407 isn->isn_arg.try.try_ref->try_catch = this_instr;
8408 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008409 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008410 {
8411 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008412 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008413 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008414 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008415 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008416 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8417 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008419 // TODO: set index in ts_finally_label jumps
8420
8421 return arg;
8422}
8423
8424 static char_u *
8425compile_endtry(char_u *arg, cctx_T *cctx)
8426{
8427 scope_T *scope = cctx->ctx_scope;
8428 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008429 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008430
Bram Moolenaarfa984412021-03-25 22:15:28 +01008431 if (misplaced_cmdmod(cctx))
8432 return NULL;
8433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008434 // end block scope from :catch or :finally
8435 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8436 compile_endblock(cctx);
8437 scope = cctx->ctx_scope;
8438
8439 // Error if not in a :try scope
8440 if (scope == NULL || scope->se_type != TRY_SCOPE)
8441 {
8442 if (scope == NULL)
8443 emsg(_(e_no_endtry));
8444 else if (scope->se_type == WHILE_SCOPE)
8445 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008446 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008447 emsg(_(e_endfor));
8448 else
8449 emsg(_(e_endif));
8450 return NULL;
8451 }
8452
Bram Moolenaarc150c092021-02-13 15:02:46 +01008453 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008454 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008455 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008456 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8457 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008458 {
8459 emsg(_(e_missing_catch_or_finally));
8460 return NULL;
8461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008462
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008463#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008464 if (cctx->ctx_compile_type == CT_PROFILE
8465 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008466 .isn_type == ISN_PROF_START)
8467 // move the profile start after "endtry" so that it's not counted when
8468 // the exception is rethrown.
8469 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008470#endif
8471
Bram Moolenaar69f70502021-01-01 16:10:46 +01008472 // Fill in the "end" label in jumps at the end of the blocks, if not
8473 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008474 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8475 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008476
Bram Moolenaar69f70502021-01-01 16:10:46 +01008477 if (scope->se_u.se_try.ts_catch_label != 0)
8478 {
8479 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008480 isn_T *isn = ((isn_T *)instr->ga_data)
8481 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008482 isn->isn_arg.jump.jump_where = instr->ga_len;
8483 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008484 }
8485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008486 compile_endblock(cctx);
8487
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008488 if (cctx->ctx_skip != SKIP_YES)
8489 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008490 // End :catch or :finally scope: set instruction index in ISN_TRY
8491 // instruction
8492 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008493 if (cctx->ctx_skip != SKIP_YES
8494 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8495 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008496#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008497 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008498 generate_instr(cctx, ISN_PROF_START);
8499#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008501 return arg;
8502}
8503
8504/*
8505 * compile "throw {expr}"
8506 */
8507 static char_u *
8508compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8509{
8510 char_u *p = skipwhite(arg);
8511
Bram Moolenaara5565e42020-05-09 15:44:01 +02008512 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008513 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008514 if (cctx->ctx_skip == SKIP_YES)
8515 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008516 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008517 return NULL;
8518 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8519 return NULL;
8520
8521 return p;
8522}
8523
8524/*
8525 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008526 * compile "echomsg expr"
8527 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008528 * compile "execute expr"
8529 */
8530 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008531compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008532{
8533 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008534 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008535 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008536 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008537 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008538 garray_T *stack = &cctx->ctx_type_stack;
8539 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008540
8541 for (;;)
8542 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008543 if (ends_excmd2(prev, p))
8544 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008545 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008546 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008547 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008548
8549 if (cctx->ctx_skip != SKIP_YES)
8550 {
8551 // check for non-void type
8552 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8553 if (type->tt_type == VAR_VOID)
8554 {
8555 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8556 return NULL;
8557 }
8558 }
8559
Bram Moolenaarad39c092020-02-26 18:23:43 +01008560 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008561 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008562 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008563 }
8564
Bram Moolenaare4984292020-12-13 14:19:25 +01008565 if (count > 0)
8566 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008567 long save_lnum = cctx->ctx_lnum;
8568
8569 // Use the line number where the command started.
8570 cctx->ctx_lnum = start_ctx_lnum;
8571
Bram Moolenaare4984292020-12-13 14:19:25 +01008572 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8573 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8574 else if (cmdidx == CMD_execute)
8575 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8576 else if (cmdidx == CMD_echomsg)
8577 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8578 else
8579 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008580
8581 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008583 return p;
8584}
8585
8586/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008587 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008588 * instruction to compute it and return OK.
8589 * Otherwise return FAIL, the caller must deal with any range.
8590 */
8591 static int
8592compile_variable_range(exarg_T *eap, cctx_T *cctx)
8593{
8594 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8595 char_u *p = skipdigits(eap->cmd);
8596
8597 if (p == range_end)
8598 return FAIL;
8599 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8600}
8601
8602/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008603 * :put r
8604 * :put ={expr}
8605 */
8606 static char_u *
8607compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8608{
8609 char_u *line = arg;
8610 linenr_T lnum;
8611 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008612 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008613
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008614 eap->regname = *line;
8615
8616 if (eap->regname == '=')
8617 {
8618 char_u *p = line + 1;
8619
8620 if (compile_expr0(&p, cctx) == FAIL)
8621 return NULL;
8622 line = p;
8623 }
8624 else if (eap->regname != NUL)
8625 ++line;
8626
Bram Moolenaar08597872020-12-10 19:43:40 +01008627 if (compile_variable_range(eap, cctx) == OK)
8628 {
8629 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8630 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008631 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008632 {
8633 // Either no range or a number.
8634 // "errormsg" will not be set because the range is ADDR_LINES.
8635 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008636 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008637 return NULL;
8638 if (eap->addr_count == 0)
8639 lnum = -1;
8640 else
8641 lnum = eap->line2;
8642 if (above)
8643 --lnum;
8644 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008645
8646 generate_PUT(cctx, eap->regname, lnum);
8647 return line;
8648}
8649
8650/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008651 * A command that is not compiled, execute with legacy code.
8652 */
8653 static char_u *
8654compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8655{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008656 char_u *p;
8657 int has_expr = FALSE;
8658 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008659
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008660 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008661 goto theend;
8662
Bram Moolenaare729ce22021-06-06 21:38:09 +02008663 // If there was a prececing command modifier, drop it and include it in the
8664 // EXEC command.
8665 if (cctx->ctx_has_cmdmod)
8666 {
8667 garray_T *instr = &cctx->ctx_instr;
8668 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8669
8670 if (isn->isn_type == ISN_CMDMOD)
8671 {
8672 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8673 ->cmod_filter_regmatch.regprog);
8674 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8675 --instr->ga_len;
8676 cctx->ctx_has_cmdmod = FALSE;
8677 }
8678 }
8679
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008680 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008681 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008682 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008683 int usefilter = FALSE;
8684
8685 has_expr = argt & (EX_XFILE | EX_EXPAND);
8686
8687 // If the command can be followed by a bar, find the bar and truncate
8688 // it, so that the following command can be compiled.
8689 // The '|' is overwritten with a NUL, it is put back below.
8690 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8691 && *eap->arg == '!')
8692 // :w !filter or :r !filter or :r! filter
8693 usefilter = TRUE;
8694 if ((argt & EX_TRLBAR) && !usefilter)
8695 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008696 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008697 separate_nextcmd(eap);
8698 if (eap->nextcmd != NULL)
8699 nextcmd = eap->nextcmd;
8700 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008701 else if (eap->cmdidx == CMD_wincmd)
8702 {
8703 p = eap->arg;
8704 if (*p != NUL)
8705 ++p;
8706 if (*p == 'g' || *p == Ctrl_G)
8707 ++p;
8708 p = skipwhite(p);
8709 if (*p == '|')
8710 {
8711 *p = NUL;
8712 nextcmd = p + 1;
8713 }
8714 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008715 }
8716
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008717 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8718 {
8719 // expand filename in "syntax include [@group] filename"
8720 has_expr = TRUE;
8721 eap->arg = skipwhite(eap->arg + 7);
8722 if (*eap->arg == '@')
8723 eap->arg = skiptowhite(eap->arg);
8724 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008725
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008726 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8727 && STRLEN(eap->arg) > 4)
8728 {
8729 int delim = *eap->arg;
8730
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008731 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008732 if (*p == delim)
8733 {
8734 eap->arg = p + 1;
8735 has_expr = TRUE;
8736 }
8737 }
8738
Bram Moolenaarecac5912021-01-05 19:23:28 +01008739 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8740 {
8741 // TODO: should only expand when appropriate for the command
8742 eap->arg = skiptowhite(eap->arg);
8743 has_expr = TRUE;
8744 }
8745
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008746 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008747 {
8748 int count = 0;
8749 char_u *start = skipwhite(line);
8750
8751 // :cmd xxx`=expr1`yyy`=expr2`zzz
8752 // PUSHS ":cmd xxx"
8753 // eval expr1
8754 // PUSHS "yyy"
8755 // eval expr2
8756 // PUSHS "zzz"
8757 // EXECCONCAT 5
8758 for (;;)
8759 {
8760 if (p > start)
8761 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008762 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008763 ++count;
8764 }
8765 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008766 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008767 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008768 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008769 ++count;
8770 p = skipwhite(p);
8771 if (*p != '`')
8772 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008773 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008774 return NULL;
8775 }
8776 start = p + 1;
8777
8778 p = (char_u *)strstr((char *)start, "`=");
8779 if (p == NULL)
8780 {
8781 if (*skipwhite(start) != NUL)
8782 {
8783 generate_PUSHS(cctx, vim_strsave(start));
8784 ++count;
8785 }
8786 break;
8787 }
8788 }
8789 generate_EXECCONCAT(cctx, count);
8790 }
8791 else
8792 generate_EXEC(cctx, line);
8793
8794theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008795 if (*nextcmd != NUL)
8796 {
8797 // the parser expects a pointer to the bar, put it back
8798 --nextcmd;
8799 *nextcmd = '|';
8800 }
8801
8802 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008803}
8804
Bram Moolenaar20677332021-06-06 17:02:53 +02008805/*
8806 * A script command with heredoc, e.g.
8807 * ruby << EOF
8808 * command
8809 * EOF
8810 * Has been turned into one long line with NL characters by
8811 * get_function_body():
8812 * ruby << EOF<NL> command<NL>EOF
8813 */
8814 static char_u *
8815compile_script(char_u *line, cctx_T *cctx)
8816{
8817 if (cctx->ctx_skip != SKIP_YES)
8818 {
8819 isn_T *isn;
8820
8821 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8822 return NULL;
8823 isn->isn_arg.string = vim_strsave(line);
8824 }
8825 return (char_u *)"";
8826}
8827
Bram Moolenaar8238f082021-04-20 21:10:48 +02008828
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008829/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008830 * :s/pat/repl/
8831 */
8832 static char_u *
8833compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8834{
8835 char_u *cmd = eap->arg;
8836 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8837
8838 if (expr != NULL)
8839 {
8840 int delimiter = *cmd++;
8841
8842 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008843 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008844 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8845 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008846 garray_T save_ga = cctx->ctx_instr;
8847 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008848 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008849 int trailing_error;
8850 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008851 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008852 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008853
8854 cmd += 3;
8855 end = skip_substitute(cmd, delimiter);
8856
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008857 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008858 // labels are correct.
8859 cctx->ctx_instr.ga_len = 0;
8860 cctx->ctx_instr.ga_maxlen = 0;
8861 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008862 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008863 if (end[-1] == NUL)
8864 end[-1] = delimiter;
8865 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008866 trailing_error = *cmd != delimiter && *cmd != NUL;
8867
Bram Moolenaar169502f2021-04-21 12:19:35 +02008868 if (expr_res == FAIL || trailing_error
8869 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008870 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008871 if (trailing_error)
8872 semsg(_(e_trailing_arg), cmd);
8873 clear_instr_ga(&cctx->ctx_instr);
8874 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008875 return NULL;
8876 }
8877
Bram Moolenaar8238f082021-04-20 21:10:48 +02008878 // Move the generated instructions into the ISN_SUBSTITUTE
8879 // instructions, then restore the list of instructions before
8880 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008881 instr_count = cctx->ctx_instr.ga_len;
8882 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008883 instr[instr_count].isn_type = ISN_FINISH;
8884
8885 cctx->ctx_instr = save_ga;
8886 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8887 {
8888 int idx;
8889
8890 for (idx = 0; idx < instr_count; ++idx)
8891 delete_instr(instr + idx);
8892 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008893 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008894 }
8895 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8896 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008897
8898 // skip over flags
8899 if (*end == '&')
8900 ++end;
8901 while (ASCII_ISALPHA(*end) || *end == '#')
8902 ++end;
8903 return end;
8904 }
8905 }
8906
8907 return compile_exec(arg, eap, cctx);
8908}
8909
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008910 static char_u *
8911compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8912{
8913 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008914 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008915
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008916 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008917 {
8918 if (STRNCMP(arg, "END", 3) == 0)
8919 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008920 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008921 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008922 // First load the current variable value.
8923 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8924 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008925 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008926 }
8927
8928 // Gets the redirected text and put it on the stack, then store it
8929 // in the variable.
8930 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8931
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008932 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008933 generate_instr_drop(cctx, ISN_CONCAT, 1);
8934
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008935 if (lhs->lhs_has_index)
8936 {
8937 // Use the info in "lhs" to store the value at the index in the
8938 // list or dict.
8939 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8940 &t_string, cctx) == FAIL)
8941 return NULL;
8942 }
8943 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008944 return NULL;
8945
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008946 VIM_CLEAR(lhs->lhs_name);
8947 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008948 return arg + 3;
8949 }
8950 emsg(_(e_cannot_nest_redir));
8951 return NULL;
8952 }
8953
8954 if (arg[0] == '=' && arg[1] == '>')
8955 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008956 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008957
8958 // redirect to a variable is compiled
8959 arg += 2;
8960 if (*arg == '>')
8961 {
8962 ++arg;
8963 append = TRUE;
8964 }
8965 arg = skipwhite(arg);
8966
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008967 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008968 FALSE, FALSE, 1, cctx) == FAIL)
8969 return NULL;
8970 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008971 lhs->lhs_append = append;
8972 if (lhs->lhs_has_index)
8973 {
8974 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8975 if (lhs->lhs_whole == NULL)
8976 return NULL;
8977 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008978
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008979 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008980 }
8981
8982 // other redirects are handled like at script level
8983 return compile_exec(line, eap, cctx);
8984}
8985
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008986#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008987 static char_u *
8988compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8989{
8990 isn_T *isn;
8991 char_u *p;
8992
8993 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8994 if (isn == NULL)
8995 return NULL;
8996 isn->isn_arg.number = eap->cmdidx;
8997
8998 p = eap->arg;
8999 if (compile_expr0(&p, cctx) == FAIL)
9000 return NULL;
9001
9002 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9003 if (isn == NULL)
9004 return NULL;
9005 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9006 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9007 return NULL;
9008 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9009 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9010 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9011
9012 return p;
9013}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009014#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009015
Bram Moolenaar4c137212021-04-19 16:48:48 +02009016/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009017 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009018 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009019 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009020 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009021add_def_function(ufunc_T *ufunc)
9022{
9023 dfunc_T *dfunc;
9024
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009025 if (def_functions.ga_len == 0)
9026 {
9027 // The first position is not used, so that a zero uf_dfunc_idx means it
9028 // wasn't set.
9029 if (ga_grow(&def_functions, 1) == FAIL)
9030 return FAIL;
9031 ++def_functions.ga_len;
9032 }
9033
Bram Moolenaar09689a02020-05-09 22:50:08 +02009034 // Add the function to "def_functions".
9035 if (ga_grow(&def_functions, 1) == FAIL)
9036 return FAIL;
9037 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9038 CLEAR_POINTER(dfunc);
9039 dfunc->df_idx = def_functions.ga_len;
9040 ufunc->uf_dfunc_idx = dfunc->df_idx;
9041 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009042 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009043 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009044 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009045 ++def_functions.ga_len;
9046 return OK;
9047}
9048
9049/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009050 * After ex_function() has collected all the function lines: parse and compile
9051 * the lines into instructions.
9052 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009053 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9054 * the return statement (used for lambda). When uf_ret_type is already set
9055 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009056 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009057 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009058 * This can be used recursively through compile_lambda(), which may reallocate
9059 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009060 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009061 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009062 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009063compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009064 ufunc_T *ufunc,
9065 int check_return_type,
9066 compiletype_T compile_type,
9067 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009068{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009069 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009070 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009071 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009072 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009073 cctx_T cctx;
9074 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009075 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009076 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009077 int ret = FAIL;
9078 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009079 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009080 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009081 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009082 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009083#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009084 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009085#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009086 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009087
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009088 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009089 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009090 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009091 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009092 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9093 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009094 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009095
9096 switch (compile_type)
9097 {
9098 case CT_PROFILE:
9099#ifdef FEAT_PROFILE
9100 instr_dest = dfunc->df_instr_prof; break;
9101#endif
9102 case CT_NONE: instr_dest = dfunc->df_instr; break;
9103 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9104 }
9105 if (instr_dest != NULL)
9106 // Was compiled in this mode before: Free old instructions.
9107 delete_def_function_contents(dfunc, FALSE);
9108 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009109 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009110 else
9111 {
9112 if (add_def_function(ufunc) == FAIL)
9113 return FAIL;
9114 new_def_function = TRUE;
9115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009116
Bram Moolenaar985116a2020-07-12 17:31:09 +02009117 ufunc->uf_def_status = UF_COMPILING;
9118
Bram Moolenaara80faa82020-04-12 19:37:17 +02009119 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009120
Bram Moolenaare99d4222021-06-13 14:01:26 +02009121 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009122 cctx.ctx_ufunc = ufunc;
9123 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009124 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009125 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9126 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9127 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9128 cctx.ctx_type_list = &ufunc->uf_type_list;
9129 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9130 instr = &cctx.ctx_instr;
9131
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009132 // Set the context to the function, it may be compiled when called from
9133 // another script. Set the script version to the most modern one.
9134 // The line number will be set in next_line_from_context().
9135 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009136 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9137
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009138 // Don't use the flag from ":legacy" here.
9139 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9140
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009141 // Make sure error messages are OK.
9142 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9143 if (do_estack_push)
9144 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009145 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009146
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009147 if (ufunc->uf_def_args.ga_len > 0)
9148 {
9149 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009150 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009151 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009152 int i;
9153 char_u *arg;
9154 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009155 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009156
9157 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009158 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009159 for (i = 0; i < count; ++i)
9160 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009161 garray_T *stack = &cctx.ctx_type_stack;
9162 type_T *val_type;
9163 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009164 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009165 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009166 int jump_instr_idx = instr->ga_len;
9167 isn_T *isn;
9168
9169 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9170 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9171 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009172
9173 // Make sure later arguments are not found.
9174 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009175
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009176 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009177 r = compile_expr0(&arg, &cctx);
9178
9179 ufunc->uf_args.ga_len = uf_args_len;
9180 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009181 goto erret;
9182
9183 // If no type specified use the type of the default value.
9184 // Otherwise check that the default value type matches the
9185 // specified type.
9186 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009187 where.wt_index = arg_idx + 1;
9188 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009189 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009190 {
9191 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009192 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009193 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009194 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009195 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009196 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009197
9198 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009199 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009200
9201 // set instruction index in JUMP_IF_ARG_SET to here
9202 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9203 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009204 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009205
9206 if (did_set_arg_type)
9207 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009208 }
9209
9210 /*
9211 * Loop over all the lines of the function and generate instructions.
9212 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009213 for (;;)
9214 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009215 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009216 int starts_with_colon = FALSE;
9217 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009218 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009219
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009220 // Bail out on the first error to avoid a flood of errors and report
9221 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009222 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009223 goto erret;
9224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009225 if (line != NULL && *line == '|')
9226 // the line continues after a '|'
9227 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009228 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009229 && !(*line == '#' && (line == cctx.ctx_line_start
9230 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009231 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009232 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009233 goto erret;
9234 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009235 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9236 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009237 else
9238 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009239 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009240 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009241 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009242 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009243#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009244 if (cctx.ctx_skip != SKIP_YES)
9245 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009246#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009247 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009248 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009249 // Make a copy, splitting off nextcmd and removing trailing spaces
9250 // may change it.
9251 if (line != NULL)
9252 {
9253 line = vim_strsave(line);
9254 vim_free(line_to_free);
9255 line_to_free = line;
9256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009257 }
9258
Bram Moolenaara80faa82020-04-12 19:37:17 +02009259 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009260 ea.cmdlinep = &line;
9261 ea.cmd = skipwhite(line);
9262
Bram Moolenaarb2049902021-01-24 12:53:53 +01009263 if (*ea.cmd == '#')
9264 {
9265 // "#" starts a comment
9266 line = (char_u *)"";
9267 continue;
9268 }
9269
Bram Moolenaarf002a412021-01-24 13:34:18 +01009270#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009271 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9272 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009273 {
9274 may_generate_prof_end(&cctx, prof_lnum);
9275
9276 prof_lnum = cctx.ctx_lnum;
9277 generate_instr(&cctx, ISN_PROF_START);
9278 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009279#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009280 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9281 && cctx.ctx_skip != SKIP_YES)
9282 {
9283 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009284 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009285 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009286 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009287
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009288 // Some things can be recognized by the first character.
9289 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009290 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009291 case '}':
9292 {
9293 // "}" ends a block scope
9294 scopetype_T stype = cctx.ctx_scope == NULL
9295 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009296
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009297 if (stype == BLOCK_SCOPE)
9298 {
9299 compile_endblock(&cctx);
9300 line = ea.cmd;
9301 }
9302 else
9303 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009304 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009305 goto erret;
9306 }
9307 if (line != NULL)
9308 line = skipwhite(ea.cmd + 1);
9309 continue;
9310 }
9311
9312 case '{':
9313 // "{" starts a block scope
9314 // "{'a': 1}->func() is something else
9315 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9316 {
9317 line = compile_block(ea.cmd, &cctx);
9318 continue;
9319 }
9320 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009321 }
9322
9323 /*
9324 * COMMAND MODIFIERS
9325 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009326 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009327 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9328 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009329 {
9330 if (errormsg != NULL)
9331 goto erret;
9332 // empty line or comment
9333 line = (char_u *)"";
9334 continue;
9335 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009336 generate_cmdmods(&cctx, &local_cmdmod);
9337 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009338
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009339 // Check if there was a colon after the last command modifier or before
9340 // the current position.
9341 for (p = ea.cmd; p >= line; --p)
9342 {
9343 if (*p == ':')
9344 starts_with_colon = TRUE;
9345 if (p < ea.cmd && !VIM_ISWHITE(*p))
9346 break;
9347 }
9348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009349 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009350 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009351 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009352 {
9353 if (*ea.cmd == '(')
9354 // not for "call()"
9355 ea.cmd = p;
9356 else
9357 ea.cmd = skipwhite(ea.cmd);
9358 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009359
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009360 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009361 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009362 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009363
Bram Moolenaar17126b12021-01-07 22:03:02 +01009364 // Check for assignment after command modifiers.
9365 assign = may_compile_assignment(&ea, &line, &cctx);
9366 if (assign == OK)
9367 goto nextline;
9368 if (assign == FAIL)
9369 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009370 }
9371
9372 /*
9373 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009374 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009375 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009376 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009377 cmd = ea.cmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009378 if (starts_with_colon || !(*cmd == '\''
9379 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009380 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009381 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009382 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009383 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009384 if (!starts_with_colon)
9385 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009386 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009387 goto erret;
9388 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009389 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009390 if (ends_excmd2(line, ea.cmd))
9391 {
9392 // A range without a command: jump to the line.
9393 // TODO: compile to a more efficient command, possibly
9394 // calling parse_cmd_address().
9395 ea.cmdidx = CMD_SIZE;
9396 line = compile_exec(line, &ea, &cctx);
9397 goto nextline;
9398 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009399 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009400 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009401 p = find_ex_command(&ea, NULL, starts_with_colon
9402 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009403
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009404 if (p == NULL)
9405 {
9406 if (cctx.ctx_skip != SKIP_YES)
9407 emsg(_(e_ambiguous_use_of_user_defined_command));
9408 goto erret;
9409 }
9410
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009411 // When using ":legacy cmd" always use compile_exec().
9412 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009413 {
9414 char_u *start = ea.cmd;
9415
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009416 switch (ea.cmdidx)
9417 {
9418 case CMD_if:
9419 case CMD_elseif:
9420 case CMD_else:
9421 case CMD_endif:
9422 case CMD_for:
9423 case CMD_endfor:
9424 case CMD_continue:
9425 case CMD_break:
9426 case CMD_while:
9427 case CMD_endwhile:
9428 case CMD_try:
9429 case CMD_catch:
9430 case CMD_finally:
9431 case CMD_endtry:
9432 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9433 goto erret;
9434 default: break;
9435 }
9436
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009437 // ":legacy return expr" needs to be handled differently.
9438 if (checkforcmd(&start, "return", 4))
9439 ea.cmdidx = CMD_return;
9440 else
9441 ea.cmdidx = CMD_legacy;
9442 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009443
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009444 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9445 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009446 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009447 {
9448 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009449 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009450 }
9451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009452 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009453 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009454 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009455 // CMD_var cannot happen, compile_assignment() above would be
9456 // used. Most likely an assignment to a non-existing variable.
9457 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009458 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009460 }
9461
Bram Moolenaar3988f642020-08-27 22:43:03 +02009462 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009463 && ea.cmdidx != CMD_elseif
9464 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009465 && ea.cmdidx != CMD_endif
9466 && ea.cmdidx != CMD_endfor
9467 && ea.cmdidx != CMD_endwhile
9468 && ea.cmdidx != CMD_catch
9469 && ea.cmdidx != CMD_finally
9470 && ea.cmdidx != CMD_endtry)
9471 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009472 emsg(_(e_unreachable_code_after_return));
9473 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009474 }
9475
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009476 p = skipwhite(p);
9477 if (ea.cmdidx != CMD_SIZE
9478 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9479 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009480 if (ea.cmdidx >= 0)
9481 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009482 if ((ea.argt & EX_BANG) && *p == '!')
9483 {
9484 ea.forceit = TRUE;
9485 p = skipwhite(p + 1);
9486 }
9487 }
9488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009489 switch (ea.cmdidx)
9490 {
9491 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009492 ea.arg = p;
9493 line = compile_nested_function(&ea, &cctx);
9494 break;
9495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009496 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009497 // TODO: should we allow this, e.g. to declare a global
9498 // function?
9499 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009500 goto erret;
9501
9502 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009503 line = compile_return(p, check_return_type,
9504 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009505 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009506 break;
9507
9508 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009509 emsg(_(e_cannot_use_let_in_vim9_script));
9510 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009511 case CMD_var:
9512 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009513 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009514 case CMD_increment:
9515 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009516 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009517 if (line == p)
9518 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009519 break;
9520
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009521 case CMD_unlet:
9522 case CMD_unlockvar:
9523 case CMD_lockvar:
9524 line = compile_unletlock(p, &ea, &cctx);
9525 break;
9526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009527 case CMD_import:
9528 line = compile_import(p, &cctx);
9529 break;
9530
9531 case CMD_if:
9532 line = compile_if(p, &cctx);
9533 break;
9534 case CMD_elseif:
9535 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009536 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009537 break;
9538 case CMD_else:
9539 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009540 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009541 break;
9542 case CMD_endif:
9543 line = compile_endif(p, &cctx);
9544 break;
9545
9546 case CMD_while:
9547 line = compile_while(p, &cctx);
9548 break;
9549 case CMD_endwhile:
9550 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009551 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009552 break;
9553
9554 case CMD_for:
9555 line = compile_for(p, &cctx);
9556 break;
9557 case CMD_endfor:
9558 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009559 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009560 break;
9561 case CMD_continue:
9562 line = compile_continue(p, &cctx);
9563 break;
9564 case CMD_break:
9565 line = compile_break(p, &cctx);
9566 break;
9567
9568 case CMD_try:
9569 line = compile_try(p, &cctx);
9570 break;
9571 case CMD_catch:
9572 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009573 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009574 break;
9575 case CMD_finally:
9576 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009577 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009578 break;
9579 case CMD_endtry:
9580 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009581 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009582 break;
9583 case CMD_throw:
9584 line = compile_throw(p, &cctx);
9585 break;
9586
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009587 case CMD_eval:
9588 if (compile_expr0(&p, &cctx) == FAIL)
9589 goto erret;
9590
Bram Moolenaar3988f642020-08-27 22:43:03 +02009591 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009592 generate_instr_drop(&cctx, ISN_DROP, 1);
9593
9594 line = skipwhite(p);
9595 break;
9596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009597 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009598 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009599 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009600 case CMD_echomsg:
9601 case CMD_echoerr:
9602 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009603 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009604
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009605 case CMD_put:
9606 ea.cmd = cmd;
9607 line = compile_put(p, &ea, &cctx);
9608 break;
9609
Bram Moolenaar4c137212021-04-19 16:48:48 +02009610 case CMD_substitute:
9611 if (cctx.ctx_skip == SKIP_YES)
9612 line = (char_u *)"";
9613 else
9614 {
9615 ea.arg = p;
9616 line = compile_substitute(line, &ea, &cctx);
9617 }
9618 break;
9619
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009620 case CMD_redir:
9621 ea.arg = p;
9622 line = compile_redir(line, &ea, &cctx);
9623 break;
9624
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009625 case CMD_cexpr:
9626 case CMD_lexpr:
9627 case CMD_caddexpr:
9628 case CMD_laddexpr:
9629 case CMD_cgetexpr:
9630 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009631#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009632 ea.arg = p;
9633 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009634#else
9635 ex_ni(&ea);
9636 line = NULL;
9637#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009638 break;
9639
Bram Moolenaar3988f642020-08-27 22:43:03 +02009640 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009641
Bram Moolenaarae616492020-07-28 20:07:27 +02009642 case CMD_append:
9643 case CMD_change:
9644 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009645 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009646 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009647 case CMD_xit:
9648 not_in_vim9(&ea);
9649 goto erret;
9650
Bram Moolenaar002262f2020-07-08 17:47:57 +02009651 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009652 if (cctx.ctx_skip != SKIP_YES)
9653 {
9654 semsg(_(e_invalid_command_str), ea.cmd);
9655 goto erret;
9656 }
9657 // We don't check for a next command here.
9658 line = (char_u *)"";
9659 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009660
Bram Moolenaar20677332021-06-06 17:02:53 +02009661 case CMD_lua:
9662 case CMD_mzscheme:
9663 case CMD_perl:
9664 case CMD_py3:
9665 case CMD_python3:
9666 case CMD_python:
9667 case CMD_pythonx:
9668 case CMD_ruby:
9669 case CMD_tcl:
9670 ea.arg = p;
9671 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009672 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009673 else
9674 // heredoc lines have been concatenated with NL
9675 // characters in get_function_body()
9676 line = compile_script(line, &cctx);
9677 break;
9678
9679 default:
9680 // Not recognized, execute with do_cmdline_cmd().
9681 ea.arg = p;
9682 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009683 break;
9684 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009685nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009686 if (line == NULL)
9687 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009688 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009689
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009690 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009691 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009693 if (cctx.ctx_type_stack.ga_len < 0)
9694 {
9695 iemsg("Type stack underflow");
9696 goto erret;
9697 }
9698 }
9699
9700 if (cctx.ctx_scope != NULL)
9701 {
9702 if (cctx.ctx_scope->se_type == IF_SCOPE)
9703 emsg(_(e_endif));
9704 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9705 emsg(_(e_endwhile));
9706 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9707 emsg(_(e_endfor));
9708 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009709 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009710 goto erret;
9711 }
9712
Bram Moolenaarefd88552020-06-18 20:50:10 +02009713 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009714 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009715 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9716 ufunc->uf_ret_type = &t_void;
9717 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009718 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009719 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009720 goto erret;
9721 }
9722
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009723 // Return void if there is no return at the end.
9724 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009725 }
9726
Bram Moolenaar599410c2021-04-10 14:03:43 +02009727 // When compiled with ":silent!" and there was an error don't consider the
9728 // function compiled.
9729 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009730 {
9731 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9732 + ufunc->uf_dfunc_idx;
9733 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009734 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009735#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009736 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009737 {
9738 dfunc->df_instr_prof = instr->ga_data;
9739 dfunc->df_instr_prof_count = instr->ga_len;
9740 }
9741 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009742#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009743 if (cctx.ctx_compile_type == CT_DEBUG)
9744 {
9745 dfunc->df_instr_debug = instr->ga_data;
9746 dfunc->df_instr_debug_count = instr->ga_len;
9747 }
9748 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009749 {
9750 dfunc->df_instr = instr->ga_data;
9751 dfunc->df_instr_count = instr->ga_len;
9752 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009753 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009754 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009755 if (cctx.ctx_outer_used)
9756 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009757 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009758 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009759
9760 ret = OK;
9761
9762erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009763 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009764 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009765 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9766 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009767
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009768 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009769 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009770 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009771 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009772
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009773 // If using the last entry in the table and it was added above, we
9774 // might as well remove it.
9775 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009776 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009777 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009778 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009779 ufunc->uf_dfunc_idx = 0;
9780 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009781 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009782
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009783 while (cctx.ctx_scope != NULL)
9784 drop_scope(&cctx);
9785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009786 if (errormsg != NULL)
9787 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009788 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009789 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009790 }
9791
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009792 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9793 {
9794 if (ret == OK)
9795 {
9796 emsg(_(e_missing_redir_end));
9797 ret = FAIL;
9798 }
9799 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009800 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009801 }
9802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009803 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009804 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009805 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009806 if (do_estack_push)
9807 estack_pop();
9808
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009809 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009810 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009811 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009812 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009813 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009814}
9815
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009816 void
9817set_function_type(ufunc_T *ufunc)
9818{
9819 int varargs = ufunc->uf_va_name != NULL;
9820 int argcount = ufunc->uf_args.ga_len;
9821
9822 // Create a type for the function, with the return type and any
9823 // argument types.
9824 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9825 // The type is included in "tt_args".
9826 if (argcount > 0 || varargs)
9827 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009828 if (ufunc->uf_type_list.ga_itemsize == 0)
9829 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009830 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9831 argcount, &ufunc->uf_type_list);
9832 // Add argument types to the function type.
9833 if (func_type_add_arg_types(ufunc->uf_func_type,
9834 argcount + varargs,
9835 &ufunc->uf_type_list) == FAIL)
9836 return;
9837 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9838 ufunc->uf_func_type->tt_min_argcount =
9839 argcount - ufunc->uf_def_args.ga_len;
9840 if (ufunc->uf_arg_types == NULL)
9841 {
9842 int i;
9843
9844 // lambda does not have argument types.
9845 for (i = 0; i < argcount; ++i)
9846 ufunc->uf_func_type->tt_args[i] = &t_any;
9847 }
9848 else
9849 mch_memmove(ufunc->uf_func_type->tt_args,
9850 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9851 if (varargs)
9852 {
9853 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009854 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009855 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9856 }
9857 }
9858 else
9859 // No arguments, can use a predefined type.
9860 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9861 argcount, &ufunc->uf_type_list);
9862}
9863
9864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009865/*
9866 * Delete an instruction, free what it contains.
9867 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009868 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009869delete_instr(isn_T *isn)
9870{
9871 switch (isn->isn_type)
9872 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009873 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009874 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009875 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009876 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009877 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009878 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009879 case ISN_LOADENV:
9880 case ISN_LOADG:
9881 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009882 case ISN_LOADT:
9883 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009884 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009885 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009886 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009887 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009888 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009889 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009890 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009891 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009892 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009893 case ISN_STOREW:
9894 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009895 vim_free(isn->isn_arg.string);
9896 break;
9897
Bram Moolenaar4c137212021-04-19 16:48:48 +02009898 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009899 {
9900 int idx;
9901 isn_T *list = isn->isn_arg.subs.subs_instr;
9902
9903 vim_free(isn->isn_arg.subs.subs_cmd);
9904 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9905 delete_instr(list + idx);
9906 vim_free(list);
9907 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009908 break;
9909
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009910 case ISN_INSTR:
9911 {
9912 int idx;
9913 isn_T *list = isn->isn_arg.instr;
9914
9915 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9916 delete_instr(list + idx);
9917 vim_free(list);
9918 }
9919 break;
9920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009921 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009922 case ISN_STORES:
9923 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009924 break;
9925
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009926 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009927 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009928 vim_free(isn->isn_arg.unlet.ul_name);
9929 break;
9930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009931 case ISN_STOREOPT:
9932 vim_free(isn->isn_arg.storeopt.so_name);
9933 break;
9934
9935 case ISN_PUSHBLOB: // push blob isn_arg.blob
9936 blob_unref(isn->isn_arg.blob);
9937 break;
9938
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009939 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009940#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009941 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009942#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009943 break;
9944
9945 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009946#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009947 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009948#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009949 break;
9950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009951 case ISN_UCALL:
9952 vim_free(isn->isn_arg.ufunc.cuf_name);
9953 break;
9954
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009955 case ISN_FUNCREF:
9956 {
9957 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9958 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009959 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009960
Bram Moolenaar077a4232020-12-22 18:33:27 +01009961 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9962 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009963 }
9964 break;
9965
9966 case ISN_DCALL:
9967 {
9968 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9969 + isn->isn_arg.dfunc.cdf_idx;
9970
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009971 if (dfunc->df_ufunc != NULL
9972 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009973 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009974 }
9975 break;
9976
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009977 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009978 {
9979 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9980 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9981
9982 if (ufunc != NULL)
9983 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009984 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009985 func_ptr_unref(ufunc);
9986 }
9987
9988 vim_free(lambda);
9989 vim_free(isn->isn_arg.newfunc.nf_global);
9990 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009991 break;
9992
Bram Moolenaar5e654232020-09-16 15:22:00 +02009993 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009994 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009995 free_type(isn->isn_arg.type.ct_type);
9996 break;
9997
Bram Moolenaar02194d22020-10-24 23:08:38 +02009998 case ISN_CMDMOD:
9999 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10000 ->cmod_filter_regmatch.regprog);
10001 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10002 break;
10003
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010004 case ISN_LOADSCRIPT:
10005 case ISN_STORESCRIPT:
10006 vim_free(isn->isn_arg.script.scriptref);
10007 break;
10008
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010009 case ISN_TRY:
10010 vim_free(isn->isn_arg.try.try_ref);
10011 break;
10012
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010013 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010014 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010015 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10016 break;
10017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010018 case ISN_2BOOL:
10019 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010020 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010021 case ISN_ADDBLOB:
10022 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010023 case ISN_ANYINDEX:
10024 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010025 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010026 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010027 case ISN_BLOBINDEX:
10028 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010029 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010030 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010031 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010032 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010033 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010034 case ISN_COMPAREANY:
10035 case ISN_COMPAREBLOB:
10036 case ISN_COMPAREBOOL:
10037 case ISN_COMPAREDICT:
10038 case ISN_COMPAREFLOAT:
10039 case ISN_COMPAREFUNC:
10040 case ISN_COMPARELIST:
10041 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010042 case ISN_COMPARESPECIAL:
10043 case ISN_COMPARESTRING:
10044 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010045 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010046 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010047 case ISN_DROP:
10048 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010049 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010050 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010051 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010052 case ISN_EXECCONCAT:
10053 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010054 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010055 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010056 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010057 case ISN_GETITEM:
10058 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010059 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010060 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010061 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010062 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010063 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010064 case ISN_LOADBDICT:
10065 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010066 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010067 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010068 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010069 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010070 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010071 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010072 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010073 case ISN_NEGATENR:
10074 case ISN_NEWDICT:
10075 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010076 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010077 case ISN_OPFLOAT:
10078 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010079 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010080 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010081 case ISN_PROF_END:
10082 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010083 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010084 case ISN_PUSHF:
10085 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010086 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010087 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010088 case ISN_REDIREND:
10089 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010090 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010091 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010092 case ISN_SHUFFLE:
10093 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010094 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010095 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010096 case ISN_STORENR:
10097 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010098 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010099 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010100 case ISN_STOREV:
10101 case ISN_STRINDEX:
10102 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010103 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010104 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010105 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010106 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010107 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010108 // nothing allocated
10109 break;
10110 }
10111}
10112
10113/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010114 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010115 */
10116 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010117delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010118{
10119 int idx;
10120
10121 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010122 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010123
10124 if (dfunc->df_instr != NULL)
10125 {
10126 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10127 delete_instr(dfunc->df_instr + idx);
10128 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010129 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010130 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010131 if (dfunc->df_instr_debug != NULL)
10132 {
10133 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10134 delete_instr(dfunc->df_instr_debug + idx);
10135 VIM_CLEAR(dfunc->df_instr_debug);
10136 dfunc->df_instr_debug = NULL;
10137 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010138#ifdef FEAT_PROFILE
10139 if (dfunc->df_instr_prof != NULL)
10140 {
10141 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10142 delete_instr(dfunc->df_instr_prof + idx);
10143 VIM_CLEAR(dfunc->df_instr_prof);
10144 dfunc->df_instr_prof = NULL;
10145 }
10146#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010147
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010148 if (mark_deleted)
10149 dfunc->df_deleted = TRUE;
10150 if (dfunc->df_ufunc != NULL)
10151 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010152}
10153
10154/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010155 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010156 * function, unless another user function still uses it.
10157 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010158 */
10159 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010160unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010161{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010162 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010163 {
10164 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10165 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010166
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010167 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010168 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010169 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010170 ufunc->uf_dfunc_idx = 0;
10171 if (dfunc->df_ufunc == ufunc)
10172 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010173 }
10174}
10175
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010176/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010177 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010178 */
10179 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010180link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010181{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010182 if (ufunc->uf_dfunc_idx > 0)
10183 {
10184 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10185 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010186
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010187 ++dfunc->df_refcount;
10188 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010189}
10190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010191#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010192/*
10193 * Free all functions defined with ":def".
10194 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010195 void
10196free_def_functions(void)
10197{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010198 int idx;
10199
10200 for (idx = 0; idx < def_functions.ga_len; ++idx)
10201 {
10202 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10203
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010204 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010205 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010206 }
10207
10208 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010209}
10210#endif
10211
10212
10213#endif // FEAT_EVAL