blob: c5a2c2dcf28711778679dfdb5557054cdd4644cb [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 Moolenaare99d4222021-06-13 14:01:26 +0200177 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100180
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200181 int ctx_has_closure; // set to one if a closures was created in
182 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100184 garray_T ctx_imports; // imported items
185
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200186 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100187 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200188 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100189
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200190 cctx_T *ctx_outer; // outer scope for lambda or nested
191 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200192 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100194 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200195 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200196
Bram Moolenaar02194d22020-10-24 23:08:38 +0200197 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200198
199 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
200 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100201};
202
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100203static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204
205/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100206 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100207 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100208 * If "lvar" is NULL only check if the variable can be found.
209 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100210 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100211 static int
212lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213{
214 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100215 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100217 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100218 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200219
220 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
222 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100223 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
224 if (STRNCMP(name, lvp->lv_name, len) == 0
225 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200226 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100227 if (lvar != NULL)
228 {
229 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100230 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100231 }
232 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200235
236 // Find local in outer function scope.
237 if (cctx->ctx_outer != NULL)
238 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100239 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200240 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100241 if (lvar != NULL)
242 {
243 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100244 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100245 }
246 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200247 }
248 }
249
Bram Moolenaar709664c2020-12-12 14:33:41 +0100250 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251}
252
253/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200254 * Lookup an argument in the current function and an enclosing function.
255 * Returns the argument index in "idxp"
256 * Returns the argument type in "type"
257 * Sets "gen_load_outer" to TRUE if found in outer scope.
258 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259 */
260 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200261arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200262 char_u *name,
263 size_t len,
264 int *idxp,
265 type_T **type,
266 int *gen_load_outer,
267 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268{
269 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200270 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100272 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200273 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
275 {
276 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
277
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200278 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
279 {
280 if (idxp != NULL)
281 {
282 // Arguments are located above the frame pointer. One further
283 // if there is a vararg argument
284 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
285 + STACK_FRAME_SIZE)
286 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
287
288 if (cctx->ctx_ufunc->uf_arg_types != NULL)
289 *type = cctx->ctx_ufunc->uf_arg_types[idx];
290 else
291 *type = &t_any;
292 }
293 return OK;
294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200297 va_name = cctx->ctx_ufunc->uf_va_name;
298 if (va_name != NULL
299 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
300 {
301 if (idxp != NULL)
302 {
303 // varargs is always the last argument
304 *idxp = -STACK_FRAME_SIZE - 1;
305 *type = cctx->ctx_ufunc->uf_va_type;
306 }
307 return OK;
308 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200310 if (cctx->ctx_outer != NULL)
311 {
312 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200313 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200314 == OK)
315 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100316 if (gen_load_outer != NULL)
317 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200318 return OK;
319 }
320 }
321
322 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323}
324
325/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200326 * Lookup a script-local variable in the current script, possibly defined in a
327 * block that contains the function "cctx->ctx_ufunc".
328 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100329 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200330 * Return NULL when not found.
331 */
332 static sallvar_T *
333find_script_var(char_u *name, size_t len, cctx_T *cctx)
334{
335 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
336 hashitem_T *hi;
337 int cc;
338 sallvar_T *sav;
339 ufunc_T *ufunc;
340
341 // Find the list of all script variables with the right name.
342 if (len > 0)
343 {
344 cc = name[len];
345 name[len] = NUL;
346 }
347 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
348 if (len > 0)
349 name[len] = cc;
350 if (HASHITEM_EMPTY(hi))
351 return NULL;
352
353 sav = HI2SAV(hi);
354 if (sav->sav_block_id == 0 || cctx == NULL)
355 // variable defined in the script scope or not in a function.
356 return sav;
357
358 // Go over the variables with this name and find one that was visible
359 // from the function.
360 ufunc = cctx->ctx_ufunc;
361 while (sav != NULL)
362 {
363 int idx;
364
365 // Go over the blocks that this function was defined in. If the
366 // variable block ID matches it was visible to the function.
367 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
368 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
369 return sav;
370 sav = sav->sav_next;
371 }
372
373 return NULL;
374}
375
376/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100377 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200378 */
379 static int
380script_is_vim9()
381{
382 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
383}
384
385/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200386 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100388 * Returns OK or FAIL.
389 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200390 static int
391script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200393 if (current_sctx.sc_sid <= 0)
394 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200395 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200396 {
397 // Check script variables that were visible where the function was
398 // defined.
399 if (find_script_var(name, len, cctx) != NULL)
400 return OK;
401 }
402 else
403 {
404 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
405 dictitem_T *di;
406 int cc;
407
408 // Check script variables that are currently visible
409 cc = name[len];
410 name[len] = NUL;
411 di = find_var_in_ht(ht, 0, name, TRUE);
412 name[len] = cc;
413 if (di != NULL)
414 return OK;
415 }
416
417 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418}
419
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100420/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100421 * Return TRUE if "name" is a local variable, argument, script variable or
422 * imported.
423 */
424 static int
425variable_exists(char_u *name, size_t len, cctx_T *cctx)
426{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100427 return (cctx != NULL
428 && (lookup_local(name, len, NULL, cctx) == OK
429 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200430 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100431 || find_imported(name, len, cctx) != NULL;
432}
433
434/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100435 * Return TRUE if "name" is a local variable, argument, script variable,
436 * imported or function.
437 */
438 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100439item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100440{
441 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100442 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100443
444 if (variable_exists(name, len, cctx))
445 return TRUE;
446
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100447 // This is similar to what is in lookup_scriptitem():
448 // Find a function, so that a following "->" works.
449 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
450 // a function call.
451 p = skipwhite(name + len);
452
453 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
454 {
455 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200456 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100457 // Skip "g:" before a function name.
458 is_global = (name[0] == 'g' && name[1] == ':');
459 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
460 }
461 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100462}
463
464/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100465 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200466 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200467 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100468 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100469 * Return FAIL and give an error if it defined.
470 */
471 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100472check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100473{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200474 int c = p[len];
475 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200476
Bram Moolenaarda479c72021-04-10 21:01:38 +0200477 // underscore argument is OK
478 if (len == 1 && *p == '_')
479 return OK;
480
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200481 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100482 {
483 if (is_arg)
484 semsg(_(e_argument_already_declared_in_script_str), p);
485 else
486 semsg(_(e_variable_already_declared_in_script_str), p);
487 return FAIL;
488 }
489
Bram Moolenaarad486a02020-08-01 23:22:18 +0200490 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100491 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100492 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200493 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200494 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200495 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100496 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200497 // A local or script-local function can shadow a global function.
498 if (ufunc == NULL || !func_is_global(ufunc)
499 || (p[0] == 'g' && p[1] == ':'))
500 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100501 if (is_arg)
502 semsg(_(e_argument_name_shadows_existing_variable_str), p);
503 else
504 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200505 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200506 return FAIL;
507 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100508 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200509 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100510 return OK;
511}
512
Bram Moolenaar65b95452020-07-19 14:03:09 +0200513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514/////////////////////////////////////////////////////////////////////
515// Following generate_ functions expect the caller to call ga_grow().
516
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200517#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
518#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100520/*
521 * Generate an instruction without arguments.
522 * Returns a pointer to the new instruction, NULL if failed.
523 */
524 static isn_T *
525generate_instr(cctx_T *cctx, isntype_T isn_type)
526{
527 garray_T *instr = &cctx->ctx_instr;
528 isn_T *isn;
529
Bram Moolenaar080457c2020-03-03 21:53:32 +0100530 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531 if (ga_grow(instr, 1) == FAIL)
532 return NULL;
533 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
534 isn->isn_type = isn_type;
535 isn->isn_lnum = cctx->ctx_lnum + 1;
536 ++instr->ga_len;
537
538 return isn;
539}
540
541/*
542 * Generate an instruction without arguments.
543 * "drop" will be removed from the stack.
544 * Returns a pointer to the new instruction, NULL if failed.
545 */
546 static isn_T *
547generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
548{
549 garray_T *stack = &cctx->ctx_type_stack;
550
Bram Moolenaar080457c2020-03-03 21:53:32 +0100551 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 stack->ga_len -= drop;
553 return generate_instr(cctx, isn_type);
554}
555
556/*
557 * Generate instruction "isn_type" and put "type" on the type stack.
558 */
559 static isn_T *
560generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
561{
562 isn_T *isn;
563 garray_T *stack = &cctx->ctx_type_stack;
564
565 if ((isn = generate_instr(cctx, isn_type)) == NULL)
566 return NULL;
567
568 if (ga_grow(stack, 1) == FAIL)
569 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200570 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 ++stack->ga_len;
572
573 return isn;
574}
575
576/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200577 * Generate an ISN_DEBUG instruction.
578 */
579 static isn_T *
580generate_instr_debug(cctx_T *cctx)
581{
582 isn_T *isn;
583 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
584 + cctx->ctx_ufunc->uf_dfunc_idx;
585
586 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
587 return NULL;
588 isn->isn_arg.number = dfunc->df_var_names.ga_len;
589 return isn;
590}
591
592/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200594 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200595 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 */
597 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200598may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599{
600 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200601 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100603 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100605 RETURN_OK_IF_SKIP(cctx);
606 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200607 switch ((*type)->tt_type)
608 {
609 // nothing to be done
610 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200612 // conversion possible
613 case VAR_SPECIAL:
614 case VAR_BOOL:
615 case VAR_NUMBER:
616 case VAR_FLOAT:
617 break;
618
619 // conversion possible (with runtime check)
620 case VAR_ANY:
621 case VAR_UNKNOWN:
622 isntype = ISN_2STRING_ANY;
623 break;
624
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200625 // conversion possible when tolerant
626 case VAR_LIST:
627 if (tolerant)
628 {
629 isntype = ISN_2STRING_ANY;
630 break;
631 }
632 // FALLTHROUGH
633
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200634 // conversion not possible
635 case VAR_VOID:
636 case VAR_BLOB:
637 case VAR_FUNC:
638 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200639 case VAR_DICT:
640 case VAR_JOB:
641 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200642 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200643 to_string_error((*type)->tt_type);
644 return FAIL;
645 }
646
647 *type = &t_string;
648 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200650 isn->isn_arg.tostring.offset = offset;
651 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652
653 return OK;
654}
655
656 static int
657check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
658{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200659 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200661 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100662 {
663 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200664 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200666 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667 return FAIL;
668 }
669 return OK;
670}
671
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200672 static int
673generate_add_instr(
674 cctx_T *cctx,
675 vartype_T vartype,
676 type_T *type1,
677 type_T *type2)
678{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100679 garray_T *stack = &cctx->ctx_type_stack;
680 isn_T *isn = generate_instr_drop(cctx,
681 vartype == VAR_NUMBER ? ISN_OPNR
682 : vartype == VAR_LIST ? ISN_ADDLIST
683 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200684#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100685 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200686#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100687 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200688
689 if (vartype != VAR_LIST && vartype != VAR_BLOB
690 && type1->tt_type != VAR_ANY
691 && type2->tt_type != VAR_ANY
692 && check_number_or_float(
693 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
694 return FAIL;
695
696 if (isn != NULL)
697 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100698
699 // When concatenating two lists with different member types the member type
700 // becomes "any".
701 if (vartype == VAR_LIST
702 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
703 && type1->tt_member != type2->tt_member)
704 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
705
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200706 return isn == NULL ? FAIL : OK;
707}
708
709/*
710 * Get the type to use for an instruction for an operation on "type1" and
711 * "type2". If they are matching use a type-specific instruction. Otherwise
712 * fall back to runtime type checking.
713 */
714 static vartype_T
715operator_type(type_T *type1, type_T *type2)
716{
717 if (type1->tt_type == type2->tt_type
718 && (type1->tt_type == VAR_NUMBER
719 || type1->tt_type == VAR_LIST
720#ifdef FEAT_FLOAT
721 || type1->tt_type == VAR_FLOAT
722#endif
723 || type1->tt_type == VAR_BLOB))
724 return type1->tt_type;
725 return VAR_ANY;
726}
727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728/*
729 * Generate an instruction with two arguments. The instruction depends on the
730 * type of the arguments.
731 */
732 static int
733generate_two_op(cctx_T *cctx, char_u *op)
734{
735 garray_T *stack = &cctx->ctx_type_stack;
736 type_T *type1;
737 type_T *type2;
738 vartype_T vartype;
739 isn_T *isn;
740
Bram Moolenaar080457c2020-03-03 21:53:32 +0100741 RETURN_OK_IF_SKIP(cctx);
742
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200743 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
745 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200746 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747
748 switch (*op)
749 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200750 case '+':
751 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 break;
754
755 case '-':
756 case '*':
757 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
758 op) == FAIL)
759 return FAIL;
760 if (vartype == VAR_NUMBER)
761 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
762#ifdef FEAT_FLOAT
763 else if (vartype == VAR_FLOAT)
764 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
765#endif
766 else
767 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
768 if (isn != NULL)
769 isn->isn_arg.op.op_type = *op == '*'
770 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
771 break;
772
Bram Moolenaar4c683752020-04-05 21:38:23 +0200773 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200775 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 && type2->tt_type != VAR_NUMBER))
777 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200778 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 return FAIL;
780 }
781 isn = generate_instr_drop(cctx,
782 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
783 if (isn != NULL)
784 isn->isn_arg.op.op_type = EXPR_REM;
785 break;
786 }
787
788 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200789 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100790 {
791 type_T *type = &t_any;
792
793#ifdef FEAT_FLOAT
794 // float+number and number+float results in float
795 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
796 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
797 type = &t_float;
798#endif
799 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
800 }
801
802 return OK;
803}
804
805/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200806 * Get the instruction to use for comparing "type1" with "type2"
807 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200809 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100810get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811{
812 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813
Bram Moolenaar4c683752020-04-05 21:38:23 +0200814 if (type1 == VAR_UNKNOWN)
815 type1 = VAR_ANY;
816 if (type2 == VAR_UNKNOWN)
817 type2 = VAR_ANY;
818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 if (type1 == type2)
820 {
821 switch (type1)
822 {
823 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
824 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
825 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
826 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
827 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
828 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
829 case VAR_LIST: isntype = ISN_COMPARELIST; break;
830 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
831 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832 default: isntype = ISN_COMPAREANY; break;
833 }
834 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200835 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100837 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838 isntype = ISN_COMPAREANY;
839
Bram Moolenaar657137c2021-01-09 15:45:23 +0100840 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 && (isntype == ISN_COMPAREBOOL
842 || isntype == ISN_COMPARESPECIAL
843 || isntype == ISN_COMPARENR
844 || isntype == ISN_COMPAREFLOAT))
845 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200846 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100847 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200848 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849 }
850 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100851 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100852 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
853 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100854 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
855 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856 && (type1 == VAR_BLOB || type2 == VAR_BLOB
857 || type1 == VAR_LIST || type2 == VAR_LIST))))
858 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200859 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200861 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200863 return isntype;
864}
865
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200866 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100867check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200868{
869 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
870 return FAIL;
871 return OK;
872}
873
Bram Moolenaara5565e42020-05-09 15:44:01 +0200874/*
875 * Generate an ISN_COMPARE* instruction with a boolean result.
876 */
877 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100878generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200879{
880 isntype_T isntype;
881 isn_T *isn;
882 garray_T *stack = &cctx->ctx_type_stack;
883 vartype_T type1;
884 vartype_T type2;
885
886 RETURN_OK_IF_SKIP(cctx);
887
888 // Get the known type of the two items on the stack. If they are matching
889 // use a type-specific instruction. Otherwise fall back to runtime type
890 // checking.
891 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
892 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100893 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200894 if (isntype == ISN_DROP)
895 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896
897 if ((isn = generate_instr(cctx, isntype)) == NULL)
898 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100899 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 isn->isn_arg.op.op_ic = ic;
901
902 // takes two arguments, puts one bool back
903 if (stack->ga_len >= 2)
904 {
905 --stack->ga_len;
906 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
907 }
908
909 return OK;
910}
911
912/*
913 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200914 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 */
916 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200917generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100918{
919 isn_T *isn;
920 garray_T *stack = &cctx->ctx_type_stack;
921
Bram Moolenaar080457c2020-03-03 21:53:32 +0100922 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
924 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200925 isn->isn_arg.tobool.invert = invert;
926 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
928 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200929 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930
931 return OK;
932}
933
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200934/*
935 * Generate an ISN_COND2BOOL instruction.
936 */
937 static int
938generate_COND2BOOL(cctx_T *cctx)
939{
940 isn_T *isn;
941 garray_T *stack = &cctx->ctx_type_stack;
942
943 RETURN_OK_IF_SKIP(cctx);
944 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
945 return FAIL;
946
947 // type becomes bool
948 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
949
950 return OK;
951}
952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200954generate_TYPECHECK(
955 cctx_T *cctx,
956 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100957 int offset,
958 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959{
960 isn_T *isn;
961 garray_T *stack = &cctx->ctx_type_stack;
962
Bram Moolenaar080457c2020-03-03 21:53:32 +0100963 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
965 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200966 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100967 isn->isn_arg.type.ct_off = (int8_T)offset;
968 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969
Bram Moolenaar5e654232020-09-16 15:22:00 +0200970 // type becomes expected
971 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972
973 return OK;
974}
975
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100976 static int
977generate_SETTYPE(
978 cctx_T *cctx,
979 type_T *expected)
980{
981 isn_T *isn;
982
983 RETURN_OK_IF_SKIP(cctx);
984 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
985 return FAIL;
986 isn->isn_arg.type.ct_type = alloc_type(expected);
987 return OK;
988}
989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200991 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
992 * used. Return FALSE if the types will never match.
993 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100994 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200995use_typecheck(type_T *actual, type_T *expected)
996{
997 if (actual->tt_type == VAR_ANY
998 || actual->tt_type == VAR_UNKNOWN
999 || (actual->tt_type == VAR_FUNC
1000 && (expected->tt_type == VAR_FUNC
1001 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001002 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1003 && ((actual->tt_member == &t_void)
1004 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001005 return TRUE;
1006 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1007 && actual->tt_type == expected->tt_type)
1008 // This takes care of a nested list or dict.
1009 return use_typecheck(actual->tt_member, expected->tt_member);
1010 return FALSE;
1011}
1012
1013/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001014 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001015 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001016 * - "actual" is a type that can be "expected" type: add a runtime check; or
1017 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001018 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1019 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001020 */
Bram Moolenaar351ead02021-01-16 16:07:01 +01001021 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001022need_type(
1023 type_T *actual,
1024 type_T *expected,
1025 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001026 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001027 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001028 int silent,
1029 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001030{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001031 where_T where;
1032
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001033 if (expected == &t_bool && actual != &t_bool
1034 && (actual->tt_flags & TTFLAG_BOOL_OK))
1035 {
1036 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1037 // boolean is OK but requires a conversion.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001038 generate_2BOOL(cctx, FALSE, offset);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001039 return OK;
1040 }
1041
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001042 where.wt_index = arg_idx;
1043 where.wt_variable = FALSE;
1044 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001045 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001046
1047 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001048 // If it's a constant a runtime check makes no sense.
1049 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001050 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001051 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001052 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001053 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001054
1055 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001056 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001057 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001058}
1059
1060/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001061 * Check that the top of the type stack has a type that can be used as a
1062 * condition. Give an error and return FAIL if not.
1063 */
1064 static int
1065bool_on_stack(cctx_T *cctx)
1066{
1067 garray_T *stack = &cctx->ctx_type_stack;
1068 type_T *type;
1069
1070 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1071 if (type == &t_bool)
1072 return OK;
1073
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001074 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001075 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1076 // This requires a runtime type check.
1077 return generate_COND2BOOL(cctx);
1078
Bram Moolenaar351ead02021-01-16 16:07:01 +01001079 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001080}
1081
1082/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 * Generate an ISN_PUSHNR instruction.
1084 */
1085 static int
1086generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1087{
1088 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001089 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090
Bram Moolenaar080457c2020-03-03 21:53:32 +01001091 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1093 return FAIL;
1094 isn->isn_arg.number = number;
1095
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001096 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001097 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001098 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 return OK;
1100}
1101
1102/*
1103 * Generate an ISN_PUSHBOOL instruction.
1104 */
1105 static int
1106generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1107{
1108 isn_T *isn;
1109
Bram Moolenaar080457c2020-03-03 21:53:32 +01001110 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1112 return FAIL;
1113 isn->isn_arg.number = number;
1114
1115 return OK;
1116}
1117
1118/*
1119 * Generate an ISN_PUSHSPEC instruction.
1120 */
1121 static int
1122generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1123{
1124 isn_T *isn;
1125
Bram Moolenaar080457c2020-03-03 21:53:32 +01001126 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1128 return FAIL;
1129 isn->isn_arg.number = number;
1130
1131 return OK;
1132}
1133
1134#ifdef FEAT_FLOAT
1135/*
1136 * Generate an ISN_PUSHF instruction.
1137 */
1138 static int
1139generate_PUSHF(cctx_T *cctx, float_T fnumber)
1140{
1141 isn_T *isn;
1142
Bram Moolenaar080457c2020-03-03 21:53:32 +01001143 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1145 return FAIL;
1146 isn->isn_arg.fnumber = fnumber;
1147
1148 return OK;
1149}
1150#endif
1151
1152/*
1153 * Generate an ISN_PUSHS instruction.
1154 * Consumes "str".
1155 */
1156 static int
1157generate_PUSHS(cctx_T *cctx, char_u *str)
1158{
1159 isn_T *isn;
1160
Bram Moolenaar508b5612021-01-02 18:17:26 +01001161 if (cctx->ctx_skip == SKIP_YES)
1162 {
1163 vim_free(str);
1164 return OK;
1165 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1167 return FAIL;
1168 isn->isn_arg.string = str;
1169
1170 return OK;
1171}
1172
1173/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001174 * Generate an ISN_PUSHCHANNEL instruction.
1175 * Consumes "channel".
1176 */
1177 static int
1178generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1179{
1180 isn_T *isn;
1181
Bram Moolenaar080457c2020-03-03 21:53:32 +01001182 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001183 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1184 return FAIL;
1185 isn->isn_arg.channel = channel;
1186
1187 return OK;
1188}
1189
1190/*
1191 * Generate an ISN_PUSHJOB instruction.
1192 * Consumes "job".
1193 */
1194 static int
1195generate_PUSHJOB(cctx_T *cctx, job_T *job)
1196{
1197 isn_T *isn;
1198
Bram Moolenaar080457c2020-03-03 21:53:32 +01001199 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001200 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001201 return FAIL;
1202 isn->isn_arg.job = job;
1203
1204 return OK;
1205}
1206
1207/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 * Generate an ISN_PUSHBLOB instruction.
1209 * Consumes "blob".
1210 */
1211 static int
1212generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1213{
1214 isn_T *isn;
1215
Bram Moolenaar080457c2020-03-03 21:53:32 +01001216 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1218 return FAIL;
1219 isn->isn_arg.blob = blob;
1220
1221 return OK;
1222}
1223
1224/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001225 * Generate an ISN_PUSHFUNC instruction with name "name".
1226 * Consumes "name".
1227 */
1228 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001229generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001230{
1231 isn_T *isn;
1232
Bram Moolenaar080457c2020-03-03 21:53:32 +01001233 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001234 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001235 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001236 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001237
1238 return OK;
1239}
1240
1241/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001242 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001243 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1244 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001245 */
1246 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001247generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001248{
1249 isn_T *isn;
1250 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001251 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1252 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001253 type_T *item_type = &t_any;
1254
1255 RETURN_OK_IF_SKIP(cctx);
1256
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001257 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001258 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001259 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001260 emsg(_(e_listreq));
1261 return FAIL;
1262 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001263 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001264 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1265 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001266 isn->isn_arg.getitem.gi_index = index;
1267 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001268
1269 // add the item type to the type stack
1270 if (ga_grow(stack, 1) == FAIL)
1271 return FAIL;
1272 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1273 ++stack->ga_len;
1274 return OK;
1275}
1276
1277/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001278 * Generate an ISN_SLICE instruction with "count".
1279 */
1280 static int
1281generate_SLICE(cctx_T *cctx, int count)
1282{
1283 isn_T *isn;
1284
1285 RETURN_OK_IF_SKIP(cctx);
1286 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1287 return FAIL;
1288 isn->isn_arg.number = count;
1289 return OK;
1290}
1291
1292/*
1293 * Generate an ISN_CHECKLEN instruction with "min_len".
1294 */
1295 static int
1296generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1297{
1298 isn_T *isn;
1299
1300 RETURN_OK_IF_SKIP(cctx);
1301
1302 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1303 return FAIL;
1304 isn->isn_arg.checklen.cl_min_len = min_len;
1305 isn->isn_arg.checklen.cl_more_OK = more_OK;
1306
1307 return OK;
1308}
1309
1310/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001311 * Generate an ISN_STORE instruction.
1312 */
1313 static int
1314generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1315{
1316 isn_T *isn;
1317
Bram Moolenaar080457c2020-03-03 21:53:32 +01001318 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1320 return FAIL;
1321 if (name != NULL)
1322 isn->isn_arg.string = vim_strsave(name);
1323 else
1324 isn->isn_arg.number = idx;
1325
1326 return OK;
1327}
1328
1329/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001330 * Generate an ISN_STOREOUTER instruction.
1331 */
1332 static int
1333generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1334{
1335 isn_T *isn;
1336
1337 RETURN_OK_IF_SKIP(cctx);
1338 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1339 return FAIL;
1340 isn->isn_arg.outer.outer_idx = idx;
1341 isn->isn_arg.outer.outer_depth = level;
1342
1343 return OK;
1344}
1345
1346/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1348 */
1349 static int
1350generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1351{
1352 isn_T *isn;
1353
Bram Moolenaar080457c2020-03-03 21:53:32 +01001354 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1356 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001357 isn->isn_arg.storenr.stnr_idx = idx;
1358 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359
1360 return OK;
1361}
1362
1363/*
1364 * Generate an ISN_STOREOPT instruction
1365 */
1366 static int
1367generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1368{
1369 isn_T *isn;
1370
Bram Moolenaar080457c2020-03-03 21:53:32 +01001371 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001372 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 return FAIL;
1374 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1375 isn->isn_arg.storeopt.so_flags = opt_flags;
1376
1377 return OK;
1378}
1379
1380/*
1381 * Generate an ISN_LOAD or similar instruction.
1382 */
1383 static int
1384generate_LOAD(
1385 cctx_T *cctx,
1386 isntype_T isn_type,
1387 int idx,
1388 char_u *name,
1389 type_T *type)
1390{
1391 isn_T *isn;
1392
Bram Moolenaar080457c2020-03-03 21:53:32 +01001393 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1395 return FAIL;
1396 if (name != NULL)
1397 isn->isn_arg.string = vim_strsave(name);
1398 else
1399 isn->isn_arg.number = idx;
1400
1401 return OK;
1402}
1403
1404/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001405 * Generate an ISN_LOADOUTER instruction
1406 */
1407 static int
1408generate_LOADOUTER(
1409 cctx_T *cctx,
1410 int idx,
1411 int nesting,
1412 type_T *type)
1413{
1414 isn_T *isn;
1415
1416 RETURN_OK_IF_SKIP(cctx);
1417 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1418 return FAIL;
1419 isn->isn_arg.outer.outer_idx = idx;
1420 isn->isn_arg.outer.outer_depth = nesting;
1421
1422 return OK;
1423}
1424
1425/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001426 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001427 */
1428 static int
1429generate_LOADV(
1430 cctx_T *cctx,
1431 char_u *name,
1432 int error)
1433{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001434 int di_flags;
1435 int vidx = find_vim_var(name, &di_flags);
1436 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001437
Bram Moolenaar080457c2020-03-03 21:53:32 +01001438 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001439 if (vidx < 0)
1440 {
1441 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001442 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001443 return FAIL;
1444 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001445 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001446
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001447 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001448}
1449
1450/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001451 * Generate an ISN_UNLET instruction.
1452 */
1453 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001454generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001455{
1456 isn_T *isn;
1457
1458 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001459 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001460 return FAIL;
1461 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1462 isn->isn_arg.unlet.ul_forceit = forceit;
1463
1464 return OK;
1465}
1466
1467/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001468 * Generate an ISN_LOCKCONST instruction.
1469 */
1470 static int
1471generate_LOCKCONST(cctx_T *cctx)
1472{
1473 isn_T *isn;
1474
1475 RETURN_OK_IF_SKIP(cctx);
1476 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1477 return FAIL;
1478 return OK;
1479}
1480
1481/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 * Generate an ISN_LOADS instruction.
1483 */
1484 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001485generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001487 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001489 int sid,
1490 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491{
1492 isn_T *isn;
1493
Bram Moolenaar080457c2020-03-03 21:53:32 +01001494 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001495 if (isn_type == ISN_LOADS)
1496 isn = generate_instr_type(cctx, isn_type, type);
1497 else
1498 isn = generate_instr_drop(cctx, isn_type, 1);
1499 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001501 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1502 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503
1504 return OK;
1505}
1506
1507/*
1508 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1509 */
1510 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001511generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512 cctx_T *cctx,
1513 isntype_T isn_type,
1514 int sid,
1515 int idx,
1516 type_T *type)
1517{
1518 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001519 scriptref_T *sref;
1520 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521
Bram Moolenaar080457c2020-03-03 21:53:32 +01001522 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 if (isn_type == ISN_LOADSCRIPT)
1524 isn = generate_instr_type(cctx, isn_type, type);
1525 else
1526 isn = generate_instr_drop(cctx, isn_type, 1);
1527 if (isn == NULL)
1528 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001529
1530 // This requires three arguments, which doesn't fit in an instruction, thus
1531 // we need to allocate a struct for this.
1532 sref = ALLOC_ONE(scriptref_T);
1533 if (sref == NULL)
1534 return FAIL;
1535 isn->isn_arg.script.scriptref = sref;
1536 sref->sref_sid = sid;
1537 sref->sref_idx = idx;
1538 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001539 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 return OK;
1541}
1542
1543/*
1544 * Generate an ISN_NEWLIST instruction.
1545 */
1546 static int
1547generate_NEWLIST(cctx_T *cctx, int count)
1548{
1549 isn_T *isn;
1550 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 type_T *type;
1552 type_T *member;
1553
Bram Moolenaar080457c2020-03-03 21:53:32 +01001554 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1556 return FAIL;
1557 isn->isn_arg.number = count;
1558
Bram Moolenaar127542b2020-08-09 17:22:04 +02001559 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001560 if (count == 0)
1561 member = &t_void;
1562 else
1563 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001564 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1565 cctx->ctx_type_list);
1566 type = get_list_type(member, cctx->ctx_type_list);
1567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 // drop the value types
1569 stack->ga_len -= count;
1570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 // add the list type to the type stack
1572 if (ga_grow(stack, 1) == FAIL)
1573 return FAIL;
1574 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1575 ++stack->ga_len;
1576
1577 return OK;
1578}
1579
1580/*
1581 * Generate an ISN_NEWDICT instruction.
1582 */
1583 static int
1584generate_NEWDICT(cctx_T *cctx, int count)
1585{
1586 isn_T *isn;
1587 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 type_T *type;
1589 type_T *member;
1590
Bram Moolenaar080457c2020-03-03 21:53:32 +01001591 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1593 return FAIL;
1594 isn->isn_arg.number = count;
1595
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001596 if (count == 0)
1597 member = &t_void;
1598 else
1599 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001600 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1601 cctx->ctx_type_list);
1602 type = get_dict_type(member, cctx->ctx_type_list);
1603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 // drop the key and value types
1605 stack->ga_len -= 2 * count;
1606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 // add the dict type to the type stack
1608 if (ga_grow(stack, 1) == FAIL)
1609 return FAIL;
1610 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1611 ++stack->ga_len;
1612
1613 return OK;
1614}
1615
1616/*
1617 * Generate an ISN_FUNCREF instruction.
1618 */
1619 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001620generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621{
1622 isn_T *isn;
1623 garray_T *stack = &cctx->ctx_type_stack;
1624
Bram Moolenaar080457c2020-03-03 21:53:32 +01001625 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1627 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001628 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001629 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001631 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001632 // the nested context, including this one.
1633 if (ufunc->uf_flags & FC_CLOSURE)
1634 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 if (ga_grow(stack, 1) == FAIL)
1637 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001638 ((type_T **)stack->ga_data)[stack->ga_len] =
1639 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 ++stack->ga_len;
1641
1642 return OK;
1643}
1644
1645/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001646 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001647 * "lambda_name" and "func_name" must be in allocated memory and will be
1648 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001649 */
1650 static int
1651generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1652{
1653 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001654
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001655 if (cctx->ctx_skip == SKIP_YES)
1656 {
1657 vim_free(lambda_name);
1658 vim_free(func_name);
1659 return OK;
1660 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001661 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001662 {
1663 vim_free(lambda_name);
1664 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001665 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001666 }
1667 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001668 isn->isn_arg.newfunc.nf_global = func_name;
1669
1670 return OK;
1671}
1672
1673/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001674 * Generate an ISN_DEF instruction: list functions
1675 */
1676 static int
1677generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1678{
1679 isn_T *isn;
1680
1681 RETURN_OK_IF_SKIP(cctx);
1682 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1683 return FAIL;
1684 if (len > 0)
1685 {
1686 isn->isn_arg.string = vim_strnsave(name, len);
1687 if (isn->isn_arg.string == NULL)
1688 return FAIL;
1689 }
1690 return OK;
1691}
1692
1693/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 * Generate an ISN_JUMP instruction.
1695 */
1696 static int
1697generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1698{
1699 isn_T *isn;
1700 garray_T *stack = &cctx->ctx_type_stack;
1701
Bram Moolenaar080457c2020-03-03 21:53:32 +01001702 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1704 return FAIL;
1705 isn->isn_arg.jump.jump_when = when;
1706 isn->isn_arg.jump.jump_where = where;
1707
1708 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1709 --stack->ga_len;
1710
1711 return OK;
1712}
1713
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001714/*
1715 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1716 */
1717 static int
1718generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1719{
1720 isn_T *isn;
1721
1722 RETURN_OK_IF_SKIP(cctx);
1723 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1724 return FAIL;
1725 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1726 // jump_where is set later
1727 return OK;
1728}
1729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 static int
1731generate_FOR(cctx_T *cctx, int loop_idx)
1732{
1733 isn_T *isn;
1734 garray_T *stack = &cctx->ctx_type_stack;
1735
Bram Moolenaar080457c2020-03-03 21:53:32 +01001736 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1738 return FAIL;
1739 isn->isn_arg.forloop.for_idx = loop_idx;
1740
1741 if (ga_grow(stack, 1) == FAIL)
1742 return FAIL;
1743 // type doesn't matter, will be stored next
1744 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1745 ++stack->ga_len;
1746
1747 return OK;
1748}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001749/*
1750 * Generate an ISN_TRYCONT instruction.
1751 */
1752 static int
1753generate_TRYCONT(cctx_T *cctx, int levels, int where)
1754{
1755 isn_T *isn;
1756
1757 RETURN_OK_IF_SKIP(cctx);
1758 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1759 return FAIL;
1760 isn->isn_arg.trycont.tct_levels = levels;
1761 isn->isn_arg.trycont.tct_where = where;
1762
1763 return OK;
1764}
1765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766
1767/*
1768 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001769 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 * Return FAIL if the number of arguments is wrong.
1771 */
1772 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001773generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774{
1775 isn_T *isn;
1776 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001777 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001778 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001779 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780
Bram Moolenaar080457c2020-03-03 21:53:32 +01001781 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001782 argoff = check_internal_func(func_idx, argcount);
1783 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 return FAIL;
1785
Bram Moolenaar389df252020-07-09 21:20:47 +02001786 if (method_call && argoff > 1)
1787 {
1788 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1789 return FAIL;
1790 isn->isn_arg.shuffle.shfl_item = argcount;
1791 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1792 }
1793
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001794 if (argcount > 0)
1795 {
1796 // Check the types of the arguments.
1797 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001798 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1799 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001800 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001801 if (internal_func_is_map(func_idx))
1802 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001803 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1806 return FAIL;
1807 isn->isn_arg.bfunc.cbf_idx = func_idx;
1808 isn->isn_arg.bfunc.cbf_argcount = argcount;
1809
Bram Moolenaar94738d82020-10-21 14:25:07 +02001810 // Drop the argument types and push the return type.
1811 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 if (ga_grow(stack, 1) == FAIL)
1813 return FAIL;
1814 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001815 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001816 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001818 if (maptype != NULL && maptype->tt_member != NULL
1819 && maptype->tt_member != &t_any)
1820 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001821 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 return OK;
1824}
1825
1826/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001827 * Generate an ISN_LISTAPPEND instruction. Works like add().
1828 * Argument count is already checked.
1829 */
1830 static int
1831generate_LISTAPPEND(cctx_T *cctx)
1832{
1833 garray_T *stack = &cctx->ctx_type_stack;
1834 type_T *list_type;
1835 type_T *item_type;
1836 type_T *expected;
1837
1838 // Caller already checked that list_type is a list.
1839 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1840 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1841 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001842 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001843 return FAIL;
1844
1845 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1846 return FAIL;
1847
1848 --stack->ga_len; // drop the argument
1849 return OK;
1850}
1851
1852/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001853 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1854 * Argument count is already checked.
1855 */
1856 static int
1857generate_BLOBAPPEND(cctx_T *cctx)
1858{
1859 garray_T *stack = &cctx->ctx_type_stack;
1860 type_T *item_type;
1861
1862 // Caller already checked that blob_type is a blob.
1863 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001864 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001865 return FAIL;
1866
1867 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1868 return FAIL;
1869
1870 --stack->ga_len; // drop the argument
1871 return OK;
1872}
1873
1874/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001875 * Return TRUE if "ufunc" should be compiled, taking into account whether
1876 * "profile" indicates profiling is to be done.
1877 */
1878 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001879func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001880{
1881 switch (ufunc->uf_def_status)
1882 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001883 case UF_TO_BE_COMPILED:
1884 return TRUE;
1885
Bram Moolenaarb2049902021-01-24 12:53:53 +01001886 case UF_COMPILED:
1887 {
1888 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1889 + ufunc->uf_dfunc_idx;
1890
Bram Moolenaare99d4222021-06-13 14:01:26 +02001891 switch (compile_type)
1892 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001893 case CT_PROFILE:
1894#ifdef FEAT_PROFILE
1895 return dfunc->df_instr_prof == NULL;
1896#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001897 case CT_NONE:
1898 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001899 case CT_DEBUG:
1900 return dfunc->df_instr_debug == NULL;
1901 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001902 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001903
1904 case UF_NOT_COMPILED:
1905 case UF_COMPILE_ERROR:
1906 case UF_COMPILING:
1907 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001908 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001909 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001910}
1911
1912/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 * Generate an ISN_DCALL or ISN_UCALL instruction.
1914 * Return FAIL if the number of arguments is wrong.
1915 */
1916 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001917generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918{
1919 isn_T *isn;
1920 garray_T *stack = &cctx->ctx_type_stack;
1921 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001922 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923
Bram Moolenaar080457c2020-03-03 21:53:32 +01001924 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 if (argcount > regular_args && !has_varargs(ufunc))
1926 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001927 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 return FAIL;
1929 }
1930 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1931 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001932 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 return FAIL;
1934 }
1935
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001936 if (ufunc->uf_def_status != UF_NOT_COMPILED
1937 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001938 {
1939 int i;
1940
1941 for (i = 0; i < argcount; ++i)
1942 {
1943 type_T *expected;
1944 type_T *actual;
1945
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001946 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1947 if (actual == &t_special
1948 && i >= regular_args - ufunc->uf_def_args.ga_len)
1949 {
1950 // assume v:none used for default argument value
1951 continue;
1952 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001953 if (i < regular_args)
1954 {
1955 if (ufunc->uf_arg_types == NULL)
1956 continue;
1957 expected = ufunc->uf_arg_types[i];
1958 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001959 else if (ufunc->uf_va_type == NULL
1960 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001961 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001962 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001963 else
1964 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001965 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001966 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001967 {
1968 arg_type_mismatch(expected, actual, i + 1);
1969 return FAIL;
1970 }
1971 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02001972 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001973 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02001974 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001975 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001976 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02001977 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1978 {
1979 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1980 ufunc->uf_name);
1981 return FAIL;
1982 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001985 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001986 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001988 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 {
1990 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1991 isn->isn_arg.dfunc.cdf_argcount = argcount;
1992 }
1993 else
1994 {
1995 // A user function may be deleted and redefined later, can't use the
1996 // ufunc pointer, need to look it up again at runtime.
1997 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1998 isn->isn_arg.ufunc.cuf_argcount = argcount;
1999 }
2000
2001 stack->ga_len -= argcount; // drop the arguments
2002 if (ga_grow(stack, 1) == FAIL)
2003 return FAIL;
2004 // add return value
2005 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2006 ++stack->ga_len;
2007
2008 return OK;
2009}
2010
2011/*
2012 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2013 */
2014 static int
2015generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2016{
2017 isn_T *isn;
2018 garray_T *stack = &cctx->ctx_type_stack;
2019
Bram Moolenaar080457c2020-03-03 21:53:32 +01002020 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2022 return FAIL;
2023 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2024 isn->isn_arg.ufunc.cuf_argcount = argcount;
2025
2026 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002027 if (ga_grow(stack, 1) == FAIL)
2028 return FAIL;
2029 // add return value
2030 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2031 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032
2033 return OK;
2034}
2035
2036/*
2037 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002038 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 */
2040 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002041generate_PCALL(
2042 cctx_T *cctx,
2043 int argcount,
2044 char_u *name,
2045 type_T *type,
2046 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047{
2048 isn_T *isn;
2049 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002050 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051
Bram Moolenaar080457c2020-03-03 21:53:32 +01002052 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002053
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002054 if (type->tt_type == VAR_ANY)
2055 ret_type = &t_any;
2056 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002057 {
2058 if (type->tt_argcount != -1)
2059 {
2060 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2061
2062 if (argcount < type->tt_min_argcount - varargs)
2063 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002064 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002065 return FAIL;
2066 }
2067 if (!varargs && argcount > type->tt_argcount)
2068 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002069 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002070 return FAIL;
2071 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002072 if (type->tt_args != NULL)
2073 {
2074 int i;
2075
2076 for (i = 0; i < argcount; ++i)
2077 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002078 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002079 type_T *actual = ((type_T **)stack->ga_data)[
2080 stack->ga_len + offset];
2081 type_T *expected;
2082
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002083 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002084 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002085 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002086 else if (i >= type->tt_min_argcount
2087 && actual == &t_special)
2088 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002089 else
2090 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002091 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002092 cctx, TRUE, FALSE) == FAIL)
2093 {
2094 arg_type_mismatch(expected, actual, i + 1);
2095 return FAIL;
2096 }
2097 }
2098 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002099 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002100 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002101 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002102 else
2103 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002104 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002105 return FAIL;
2106 }
2107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2109 return FAIL;
2110 isn->isn_arg.pfunc.cpf_top = at_top;
2111 isn->isn_arg.pfunc.cpf_argcount = argcount;
2112
2113 stack->ga_len -= argcount; // drop the arguments
2114
2115 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002116 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002118 // If partial is above the arguments it must be cleared and replaced with
2119 // the return value.
2120 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2121 return FAIL;
2122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 return OK;
2124}
2125
2126/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002127 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 */
2129 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002130generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131{
2132 isn_T *isn;
2133 garray_T *stack = &cctx->ctx_type_stack;
2134 type_T *type;
2135
Bram Moolenaar080457c2020-03-03 21:53:32 +01002136 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002137 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002139 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002141 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002143 if (type->tt_type != VAR_DICT && type != &t_any)
2144 {
2145 emsg(_(e_dictreq));
2146 return FAIL;
2147 }
2148 // change dict type to dict member type
2149 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002150 {
2151 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2152 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154
2155 return OK;
2156}
2157
2158/*
2159 * Generate an ISN_ECHO instruction.
2160 */
2161 static int
2162generate_ECHO(cctx_T *cctx, int with_white, int count)
2163{
2164 isn_T *isn;
2165
Bram Moolenaar080457c2020-03-03 21:53:32 +01002166 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2168 return FAIL;
2169 isn->isn_arg.echo.echo_with_white = with_white;
2170 isn->isn_arg.echo.echo_count = count;
2171
2172 return OK;
2173}
2174
Bram Moolenaarad39c092020-02-26 18:23:43 +01002175/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002176 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002177 */
2178 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002179generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002180{
2181 isn_T *isn;
2182
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002183 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002184 return FAIL;
2185 isn->isn_arg.number = count;
2186
2187 return OK;
2188}
2189
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002190/*
2191 * Generate an ISN_PUT instruction.
2192 */
2193 static int
2194generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2195{
2196 isn_T *isn;
2197
2198 RETURN_OK_IF_SKIP(cctx);
2199 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2200 return FAIL;
2201 isn->isn_arg.put.put_regname = regname;
2202 isn->isn_arg.put.put_lnum = lnum;
2203 return OK;
2204}
2205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 static int
2207generate_EXEC(cctx_T *cctx, char_u *line)
2208{
2209 isn_T *isn;
2210
Bram Moolenaar080457c2020-03-03 21:53:32 +01002211 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2213 return FAIL;
2214 isn->isn_arg.string = vim_strsave(line);
2215 return OK;
2216}
2217
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002218 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002219generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2220{
2221 isn_T *isn;
2222 garray_T *stack = &cctx->ctx_type_stack;
2223
2224 RETURN_OK_IF_SKIP(cctx);
2225 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2226 return FAIL;
2227 isn->isn_arg.string = vim_strsave(line);
2228
2229 if (ga_grow(stack, 1) == FAIL)
2230 return FAIL;
2231 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2232 ++stack->ga_len;
2233
2234 return OK;
2235}
2236
2237 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002238generate_EXECCONCAT(cctx_T *cctx, int count)
2239{
2240 isn_T *isn;
2241
2242 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2243 return FAIL;
2244 isn->isn_arg.number = count;
2245 return OK;
2246}
2247
Bram Moolenaar08597872020-12-10 19:43:40 +01002248/*
2249 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2250 */
2251 static int
2252generate_RANGE(cctx_T *cctx, char_u *range)
2253{
2254 isn_T *isn;
2255 garray_T *stack = &cctx->ctx_type_stack;
2256
2257 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2258 return FAIL;
2259 isn->isn_arg.string = range;
2260
2261 if (ga_grow(stack, 1) == FAIL)
2262 return FAIL;
2263 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2264 ++stack->ga_len;
2265 return OK;
2266}
2267
Bram Moolenaar792f7862020-11-23 08:31:18 +01002268 static int
2269generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2270{
2271 isn_T *isn;
2272
2273 RETURN_OK_IF_SKIP(cctx);
2274 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2275 return FAIL;
2276 isn->isn_arg.unpack.unp_count = var_count;
2277 isn->isn_arg.unpack.unp_semicolon = semicolon;
2278 return OK;
2279}
2280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002282 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002283 */
2284 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002285generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002286{
2287 isn_T *isn;
2288
Bram Moolenaarfa984412021-03-25 22:15:28 +01002289 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002290 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002291 cctx->ctx_has_cmdmod = TRUE;
2292
2293 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002294 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002295 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2296 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2297 return FAIL;
2298 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002299 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002300 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002301 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002302
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002303 return OK;
2304}
2305
2306 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002307generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002308{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002309 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2310 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002311 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002312 return OK;
2313}
2314
Bram Moolenaarfa984412021-03-25 22:15:28 +01002315 static int
2316misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002317{
2318 garray_T *instr = &cctx->ctx_instr;
2319
Bram Moolenaara91a7132021-03-25 21:12:15 +01002320 if (cctx->ctx_has_cmdmod
2321 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2322 == ISN_CMDMOD)
2323 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002324 emsg(_(e_misplaced_command_modifier));
2325 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002326 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002327 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002328}
2329
2330/*
2331 * Get the index of the current instruction.
2332 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2333 */
2334 static int
2335current_instr_idx(cctx_T *cctx)
2336{
2337 garray_T *instr = &cctx->ctx_instr;
2338 int idx = instr->ga_len;
2339
Bram Moolenaare99d4222021-06-13 14:01:26 +02002340 while (idx > 0)
2341 {
2342 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002343 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002344 {
2345 --idx;
2346 continue;
2347 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002348#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002349 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2350 {
2351 --idx;
2352 continue;
2353 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002354#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002355 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2356 {
2357 --idx;
2358 continue;
2359 }
2360 break;
2361 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002362 return idx;
2363}
2364
Bram Moolenaarf002a412021-01-24 13:34:18 +01002365#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002366 static void
2367may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2368{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002369 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002370 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002371}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002372#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002373
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002374/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002376 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002378 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002379reserve_local(
2380 cctx_T *cctx,
2381 char_u *name,
2382 size_t len,
2383 int isConst,
2384 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002387 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002389 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002391 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002392 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 }
2394
2395 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002396 return NULL;
2397 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002398 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002400 // Every local variable uses the next entry on the stack. We could re-use
2401 // the last ones when leaving a scope, but then variables used in a closure
2402 // might get overwritten. To keep things simple do not re-use stack
2403 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002404 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2405 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002406
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002407 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 lvar->lv_const = isConst;
2409 lvar->lv_type = type;
2410
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002411 // Remember the name for debugging.
2412 if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2413 return NULL;
2414 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2415 vim_strsave(lvar->lv_name);
2416 ++dfunc->df_var_names.ga_len;
2417
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002418 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419}
2420
2421/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002422 * Remove local variables above "new_top".
2423 */
2424 static void
2425unwind_locals(cctx_T *cctx, int new_top)
2426{
2427 if (cctx->ctx_locals.ga_len > new_top)
2428 {
2429 int idx;
2430 lvar_T *lvar;
2431
2432 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2433 {
2434 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2435 vim_free(lvar->lv_name);
2436 }
2437 }
2438 cctx->ctx_locals.ga_len = new_top;
2439}
2440
2441/*
2442 * Free all local variables.
2443 */
2444 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002445free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002446{
2447 unwind_locals(cctx, 0);
2448 ga_clear(&cctx->ctx_locals);
2449}
2450
2451/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002452 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2453 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2454 * error if the variable was defined with :const.
2455 */
2456 static int
2457check_item_writable(svar_T *sv, int check_writable, char_u *name)
2458{
2459 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2460 || (check_writable == ASSIGN_FINAL
2461 && sv->sv_const == ASSIGN_CONST))
2462 {
2463 semsg(_(e_readonlyvar), name);
2464 return FAIL;
2465 }
2466 return OK;
2467}
2468
2469/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002471 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 * Returns the index in "sn_var_vals" if found.
2473 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002474 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 */
2476 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002477get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478{
2479 hashtab_T *ht;
2480 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002481 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002482 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 int idx;
2484
Bram Moolenaare3d46852020-08-29 13:39:17 +02002485 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002487 if (sid == current_sctx.sc_sid)
2488 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002489 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002490
2491 if (sav == NULL)
2492 return -2;
2493 idx = sav->sav_var_vals_idx;
2494 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002495 if (check_item_writable(sv, check_writable, name) == FAIL)
2496 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002497 return idx;
2498 }
2499
2500 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501 ht = &SCRIPT_VARS(sid);
2502 di = find_var_in_ht(ht, 0, name, TRUE);
2503 if (di == NULL)
2504 return -2;
2505
2506 // Now find the svar_T index in sn_var_vals.
2507 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2508 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002509 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 if (sv->sv_tv == &di->di_tv)
2511 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002512 if (check_item_writable(sv, check_writable, name) == FAIL)
2513 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 return idx;
2515 }
2516 }
2517 return -1;
2518}
2519
2520/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002521 * Find "name" in imported items of the current script or in "cctx" if not
2522 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 */
2524 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002525find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 int idx;
2528
Bram Moolenaare3d46852020-08-29 13:39:17 +02002529 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002530 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 if (cctx != NULL)
2532 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2533 {
2534 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2535 + idx;
2536
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002537 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2538 : STRLEN(import->imp_name) == len
2539 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 return import;
2541 }
2542
Bram Moolenaarefa94442020-08-08 22:16:00 +02002543 return find_imported_in_script(name, len, current_sctx.sc_sid);
2544}
2545
2546 imported_T *
2547find_imported_in_script(char_u *name, size_t len, int sid)
2548{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002549 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002550 int idx;
2551
Bram Moolenaare3d46852020-08-29 13:39:17 +02002552 if (!SCRIPT_ID_VALID(sid))
2553 return NULL;
2554 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2556 {
2557 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2558
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002559 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2560 : STRLEN(import->imp_name) == len
2561 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 return import;
2563 }
2564 return NULL;
2565}
2566
2567/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002568 * Free all imported variables.
2569 */
2570 static void
2571free_imported(cctx_T *cctx)
2572{
2573 int idx;
2574
2575 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2576 {
2577 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2578
2579 vim_free(import->imp_name);
2580 }
2581 ga_clear(&cctx->ctx_imports);
2582}
2583
2584/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002585 * Return a pointer to the next line that isn't empty or only contains a
2586 * comment. Skips over white space.
2587 * Returns NULL if there is none.
2588 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002589 char_u *
2590peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002591{
2592 int lnum = cctx->ctx_lnum;
2593
2594 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2595 {
2596 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002597 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002598
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002599 // ignore NULLs inserted for continuation lines
2600 if (line != NULL)
2601 {
2602 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002603 if (vim9_bad_comment(p))
2604 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002605 if (*p != NUL && !vim9_comment_start(p))
2606 return p;
2607 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002608 }
2609 return NULL;
2610}
2611
2612/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002613 * Called when checking for a following operator at "arg". When the rest of
2614 * the line is empty or only a comment, peek the next line. If there is a next
2615 * line return a pointer to it and set "nextp".
2616 * Otherwise skip over white space.
2617 */
2618 static char_u *
2619may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2620{
2621 char_u *p = skipwhite(arg);
2622
2623 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002624 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002625 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002626 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002627 if (*nextp != NULL)
2628 return *nextp;
2629 }
2630 return p;
2631}
2632
2633/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002634 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002635 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002636 * Returns NULL when at the end.
2637 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002638 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002639next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002640{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002641 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002642
2643 do
2644 {
2645 ++cctx->ctx_lnum;
2646 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002647 {
2648 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002649 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002650 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002651 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002652 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002653 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002654 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002655 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002656 return line;
2657}
2658
2659/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002660 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002661 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002662 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002663 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2664 */
2665 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002666may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002667{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002668 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002669 if (vim9_bad_comment(*arg))
2670 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002671 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002672 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002673 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002674
2675 if (next == NULL)
2676 return FAIL;
2677 *arg = skipwhite(next);
2678 }
2679 return OK;
2680}
2681
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002682/*
2683 * Idem, and give an error when failed.
2684 */
2685 static int
2686may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2687{
2688 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2689 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002690 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002691 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002692 return FAIL;
2693 }
2694 return OK;
2695}
2696
2697
Bram Moolenaara5565e42020-05-09 15:44:01 +02002698// Structure passed between the compile_expr* functions to keep track of
2699// constants that have been parsed but for which no code was produced yet. If
2700// possible expressions on these constants are applied at compile time. If
2701// that is not possible, the code to push the constants needs to be generated
2702// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002703// Using 50 should be more than enough of 5 levels of ().
2704#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002705typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002706 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002707 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002708 int pp_is_const; // all generated code was constants, used for a
2709 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002710} ppconst_T;
2711
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002712static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002713static int compile_expr0(char_u **arg, cctx_T *cctx);
2714static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2715
Bram Moolenaara5565e42020-05-09 15:44:01 +02002716/*
2717 * Generate a PUSH instruction for "tv".
2718 * "tv" will be consumed or cleared.
2719 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2720 */
2721 static int
2722generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2723{
2724 if (tv != NULL)
2725 {
2726 switch (tv->v_type)
2727 {
2728 case VAR_UNKNOWN:
2729 break;
2730 case VAR_BOOL:
2731 generate_PUSHBOOL(cctx, tv->vval.v_number);
2732 break;
2733 case VAR_SPECIAL:
2734 generate_PUSHSPEC(cctx, tv->vval.v_number);
2735 break;
2736 case VAR_NUMBER:
2737 generate_PUSHNR(cctx, tv->vval.v_number);
2738 break;
2739#ifdef FEAT_FLOAT
2740 case VAR_FLOAT:
2741 generate_PUSHF(cctx, tv->vval.v_float);
2742 break;
2743#endif
2744 case VAR_BLOB:
2745 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2746 tv->vval.v_blob = NULL;
2747 break;
2748 case VAR_STRING:
2749 generate_PUSHS(cctx, tv->vval.v_string);
2750 tv->vval.v_string = NULL;
2751 break;
2752 default:
2753 iemsg("constant type not supported");
2754 clear_tv(tv);
2755 return FAIL;
2756 }
2757 tv->v_type = VAR_UNKNOWN;
2758 }
2759 return OK;
2760}
2761
2762/*
2763 * Generate code for any ppconst entries.
2764 */
2765 static int
2766generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2767{
2768 int i;
2769 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002770 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002771
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002772 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002773 for (i = 0; i < ppconst->pp_used; ++i)
2774 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2775 ret = FAIL;
2776 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002777 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002778 return ret;
2779}
2780
2781/*
2782 * Clear ppconst constants. Used when failing.
2783 */
2784 static void
2785clear_ppconst(ppconst_T *ppconst)
2786{
2787 int i;
2788
2789 for (i = 0; i < ppconst->pp_used; ++i)
2790 clear_tv(&ppconst->pp_tv[i]);
2791 ppconst->pp_used = 0;
2792}
2793
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002794/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002795 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002796 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002797 */
2798 static int
2799compile_member(int is_slice, cctx_T *cctx)
2800{
2801 type_T **typep;
2802 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002803 vartype_T vartype;
2804 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002805
Bram Moolenaar261417b2021-05-06 21:04:55 +02002806 // We can index a list, dict and blob. If we don't know the type
2807 // we can use the index value type. If we still don't know use an "ANY"
2808 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002809 typep = ((type_T **)stack->ga_data) + stack->ga_len
2810 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002811 vartype = (*typep)->tt_type;
2812 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002813 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002814 if (*typep == &t_any && idxtype == &t_string)
2815 vartype = VAR_DICT;
2816 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002817 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002818 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002819 return FAIL;
2820 if (is_slice)
2821 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002822 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2823 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002824 FALSE, FALSE) == FAIL)
2825 return FAIL;
2826 }
2827 }
2828
Bram Moolenaar261417b2021-05-06 21:04:55 +02002829 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002830 {
2831 if (is_slice)
2832 {
2833 emsg(_(e_cannot_slice_dictionary));
2834 return FAIL;
2835 }
2836 if ((*typep)->tt_type == VAR_DICT)
2837 {
2838 *typep = (*typep)->tt_member;
2839 if (*typep == &t_unknown)
2840 // empty dict was used
2841 *typep = &t_any;
2842 }
2843 else
2844 {
2845 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2846 FALSE, FALSE) == FAIL)
2847 return FAIL;
2848 *typep = &t_any;
2849 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002850 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002851 return FAIL;
2852 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2853 return FAIL;
2854 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002855 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002856 {
2857 *typep = &t_string;
2858 if ((is_slice
2859 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2860 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2861 return FAIL;
2862 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002863 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002864 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002865 if (is_slice)
2866 {
2867 *typep = &t_blob;
2868 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2869 return FAIL;
2870 }
2871 else
2872 {
2873 *typep = &t_number;
2874 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2875 return FAIL;
2876 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002877 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002878 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002879 {
2880 if (is_slice)
2881 {
2882 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002883 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002884 2) == FAIL)
2885 return FAIL;
2886 }
2887 else
2888 {
2889 if ((*typep)->tt_type == VAR_LIST)
2890 {
2891 *typep = (*typep)->tt_member;
2892 if (*typep == &t_unknown)
2893 // empty list was used
2894 *typep = &t_any;
2895 }
2896 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002897 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2898 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002899 return FAIL;
2900 }
2901 }
2902 else
2903 {
2904 emsg(_(e_string_list_dict_or_blob_required));
2905 return FAIL;
2906 }
2907 return OK;
2908}
2909
2910/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002911 * Generate an instruction to load script-local variable "name", without the
2912 * leading "s:".
2913 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 */
2915 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002916compile_load_scriptvar(
2917 cctx_T *cctx,
2918 char_u *name, // variable NUL terminated
2919 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002920 char_u **end, // end of variable
2921 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002923 scriptitem_T *si;
2924 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 imported_T *import;
2926
Bram Moolenaare3d46852020-08-29 13:39:17 +02002927 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2928 return FAIL;
2929 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002930 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002931 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002933 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002934 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2935 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 }
2937 if (idx >= 0)
2938 {
2939 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2940
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002941 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 current_sctx.sc_sid, idx, sv->sv_type);
2943 return OK;
2944 }
2945
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002946 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 if (import != NULL)
2948 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002949 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002950 {
2951 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002952 char_u *exp_name;
2953 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002954 ufunc_T *ufunc;
2955 type_T *type;
2956
2957 // Used "import * as Name", need to lookup the member.
2958 if (*p != '.')
2959 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002960 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002961 return FAIL;
2962 }
2963 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002964 if (VIM_ISWHITE(*p))
2965 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002966 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002967 return FAIL;
2968 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002969
Bram Moolenaar1c991142020-07-04 13:15:31 +02002970 // isolate one name
2971 exp_name = p;
2972 while (eval_isnamec(*p))
2973 ++p;
2974 cc = *p;
2975 *p = NUL;
2976
Bram Moolenaaredba7072021-03-13 21:14:18 +01002977 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2978 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002979 *p = cc;
2980 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002981 *end = p;
2982
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002983 if (idx < 0)
2984 {
2985 if (*p == '(' && ufunc != NULL)
2986 {
2987 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2988 return OK;
2989 }
2990 return FAIL;
2991 }
2992
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002993 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2994 import->imp_sid,
2995 idx,
2996 type);
2997 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002998 else if (import->imp_funcname != NULL)
2999 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003000 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003001 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3002 import->imp_sid,
3003 import->imp_var_vals_idx,
3004 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 return OK;
3006 }
3007
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003008 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003009 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 return FAIL;
3011}
3012
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003013 static int
3014generate_funcref(cctx_T *cctx, char_u *name)
3015{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003016 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003017
3018 if (ufunc == NULL)
3019 return FAIL;
3020
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003021 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003022 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3023 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003024 == FAIL)
3025 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003026 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003027}
3028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029/*
3030 * Compile a variable name into a load instruction.
3031 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003032 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 * When "error" is FALSE do not give an error when not found.
3034 */
3035 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003036compile_load(
3037 char_u **arg,
3038 char_u *end_arg,
3039 cctx_T *cctx,
3040 int is_expr,
3041 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042{
3043 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003044 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003045 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003047 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048
3049 if (*(*arg + 1) == ':')
3050 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003051 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003052 {
3053 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054
Bram Moolenaarfa596382021-04-07 21:58:16 +02003055 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003056 switch (**arg)
3057 {
3058 case 'g': isn_type = ISN_LOADGDICT; break;
3059 case 'w': isn_type = ISN_LOADWDICT; break;
3060 case 't': isn_type = ISN_LOADTDICT; break;
3061 case 'b': isn_type = ISN_LOADBDICT; break;
3062 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003063 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003064 goto theend;
3065 }
3066 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3067 goto theend;
3068 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 else
3071 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003072 isntype_T isn_type = ISN_DROP;
3073
Bram Moolenaarfa596382021-04-07 21:58:16 +02003074 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003075 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3076 if (name == NULL)
3077 return FAIL;
3078
3079 switch (**arg)
3080 {
3081 case 'v': res = generate_LOADV(cctx, name, error);
3082 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003083 case 's': if (is_expr && ASCII_ISUPPER(*name)
3084 && find_func(name, FALSE, cctx) != NULL)
3085 res = generate_funcref(cctx, name);
3086 else
3087 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003088 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003089 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003090 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003091 {
3092 if (is_expr && ASCII_ISUPPER(*name)
3093 && find_func(name, FALSE, cctx) != NULL)
3094 res = generate_funcref(cctx, name);
3095 else
3096 isn_type = ISN_LOADG;
3097 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003098 else
3099 {
3100 isn_type = ISN_LOADAUTO;
3101 vim_free(name);
3102 name = vim_strnsave(*arg, end - *arg);
3103 if (name == NULL)
3104 return FAIL;
3105 }
3106 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003107 case 'w': isn_type = ISN_LOADW; break;
3108 case 't': isn_type = ISN_LOADT; break;
3109 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003110 default: // cannot happen, just in case
3111 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003112 goto theend;
3113 }
3114 if (isn_type != ISN_DROP)
3115 {
3116 // Global, Buffer-local, Window-local and Tabpage-local
3117 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003118 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003119 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 }
3122 }
3123 else
3124 {
3125 size_t len = end - *arg;
3126 int idx;
3127 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003128 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129
3130 name = vim_strnsave(*arg, end - *arg);
3131 if (name == NULL)
3132 return FAIL;
3133
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003134 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3135 {
3136 script_autoload(name, FALSE);
3137 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3138 }
3139 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3140 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003142 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003143 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 }
3145 else
3146 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003147 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003148
Bram Moolenaar709664c2020-12-12 14:33:41 +01003149 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003151 type = lvar.lv_type;
3152 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003153 if (lvar.lv_from_outer != 0)
3154 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003155 else
3156 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 }
3158 else
3159 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003160 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003161 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003162 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003163 || find_imported(name, 0, cctx) != NULL)
3164 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003165
Bram Moolenaar0f769812020-09-12 18:32:34 +02003166 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003167 // uppercase letter it can be a user defined function.
3168 // generate_funcref() will fail if the function can't be found.
3169 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003170 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 }
3172 }
3173 if (gen_load)
3174 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003175 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003176 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003177 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003178 cctx->ctx_outer_used = TRUE;
3179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 }
3181
3182 *arg = end;
3183
3184theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003185 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003186 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 vim_free(name);
3188 return res;
3189}
3190
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003191 static void
3192clear_instr_ga(garray_T *gap)
3193{
3194 int idx;
3195
3196 for (idx = 0; idx < gap->ga_len; ++idx)
3197 delete_instr(((isn_T *)gap->ga_data) + idx);
3198 ga_clear(gap);
3199}
3200
3201/*
3202 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3203 * Returns FAIL if compilation fails.
3204 */
3205 static int
3206compile_string(isn_T *isn, cctx_T *cctx)
3207{
3208 char_u *s = isn->isn_arg.string;
3209 garray_T save_ga = cctx->ctx_instr;
3210 int expr_res;
3211 int trailing_error;
3212 int instr_count;
3213 isn_T *instr = NULL;
3214
3215 // Temporarily reset the list of instructions so that the jump labels are
3216 // correct.
3217 cctx->ctx_instr.ga_len = 0;
3218 cctx->ctx_instr.ga_maxlen = 0;
3219 cctx->ctx_instr.ga_data = NULL;
3220 expr_res = compile_expr0(&s, cctx);
3221 s = skipwhite(s);
3222 trailing_error = *s != NUL;
3223
Bram Moolenaarff652882021-05-16 15:24:49 +02003224 if (expr_res == FAIL || trailing_error
3225 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003226 {
3227 if (trailing_error)
3228 semsg(_(e_trailing_arg), s);
3229 clear_instr_ga(&cctx->ctx_instr);
3230 cctx->ctx_instr = save_ga;
3231 return FAIL;
3232 }
3233
3234 // Move the generated instructions into the ISN_INSTR instruction, then
3235 // restore the list of instructions.
3236 instr_count = cctx->ctx_instr.ga_len;
3237 instr = cctx->ctx_instr.ga_data;
3238 instr[instr_count].isn_type = ISN_FINISH;
3239
3240 cctx->ctx_instr = save_ga;
3241 vim_free(isn->isn_arg.string);
3242 isn->isn_type = ISN_INSTR;
3243 isn->isn_arg.instr = instr;
3244 return OK;
3245}
3246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247/*
3248 * Compile the argument expressions.
3249 * "arg" points to just after the "(" and is advanced to after the ")"
3250 */
3251 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003252compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003254 char_u *p = *arg;
3255 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003256 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003257 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258
Bram Moolenaare6085c52020-04-12 20:19:16 +02003259 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003261 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3262 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003263 if (*p == ')')
3264 {
3265 *arg = p + 1;
3266 return OK;
3267 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003268 if (must_end)
3269 {
3270 semsg(_(e_missing_comma_before_argument_str), p);
3271 return FAIL;
3272 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003273
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003274 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003275 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 return FAIL;
3277 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003278
Bram Moolenaarff652882021-05-16 15:24:49 +02003279 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003280 && cctx->ctx_instr.ga_len == instr_count + 1)
3281 {
3282 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3283
3284 // {skip} argument of searchpair() can be compiled if not empty
3285 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3286 compile_string(isn, cctx);
3287 }
3288
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003289 if (*p != ',' && *skipwhite(p) == ',')
3290 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003291 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003292 p = skipwhite(p);
3293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003295 {
3296 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003297 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003298 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003299 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003300 else
3301 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003302 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003303 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003305failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003306 emsg(_(e_missing_close));
3307 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308}
3309
3310/*
3311 * Compile a function call: name(arg1, arg2)
3312 * "arg" points to "name", "arg + varlen" to the "(".
3313 * "argcount_init" is 1 for "value->method()"
3314 * Instructions:
3315 * EVAL arg1
3316 * EVAL arg2
3317 * BCALL / DCALL / UCALL
3318 */
3319 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003320compile_call(
3321 char_u **arg,
3322 size_t varlen,
3323 cctx_T *cctx,
3324 ppconst_T *ppconst,
3325 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326{
3327 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003328 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 int argcount = argcount_init;
3330 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003331 char_u fname_buf[FLEN_FIXED + 1];
3332 char_u *tofree = NULL;
3333 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003334 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003335 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003336 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003337 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338
Bram Moolenaara5565e42020-05-09 15:44:01 +02003339 // we can evaluate "has('name')" at compile time
3340 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3341 {
3342 char_u *s = skipwhite(*arg + varlen + 1);
3343 typval_T argvars[2];
3344
3345 argvars[0].v_type = VAR_UNKNOWN;
3346 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003347 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003348 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003349 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003350 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003351 if (*s == ')' && argvars[0].v_type == VAR_STRING
3352 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003353 {
3354 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3355
3356 *arg = s + 1;
3357 argvars[1].v_type = VAR_UNKNOWN;
3358 tv->v_type = VAR_NUMBER;
3359 tv->vval.v_number = 0;
3360 f_has(argvars, tv);
3361 clear_tv(&argvars[0]);
3362 ++ppconst->pp_used;
3363 return OK;
3364 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003365 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003366 }
3367
3368 if (generate_ppconst(cctx, ppconst) == FAIL)
3369 return FAIL;
3370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 if (varlen >= sizeof(namebuf))
3372 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003373 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 return FAIL;
3375 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003376 vim_strncpy(namebuf, *arg, varlen);
3377 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003379 // We handle the "skip" argument of searchpair() and searchpairpos()
3380 // differently.
3381 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3382 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3383 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3384 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003387 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003388 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389
Bram Moolenaar03290b82020-12-19 16:30:44 +01003390 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003391 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 {
3393 int idx;
3394
3395 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003396 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003398 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003399 if (STRCMP(name, "flatten") == 0)
3400 {
3401 emsg(_(e_cannot_use_flatten_in_vim9_script));
3402 goto theend;
3403 }
3404
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003405 if (STRCMP(name, "add") == 0 && argcount == 2)
3406 {
3407 garray_T *stack = &cctx->ctx_type_stack;
3408 type_T *type = ((type_T **)stack->ga_data)[
3409 stack->ga_len - 2];
3410
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003411 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003412 if (type->tt_type == VAR_LIST)
3413 {
3414 // inline "add(list, item)" so that the type can be checked
3415 res = generate_LISTAPPEND(cctx);
3416 idx = -1;
3417 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003418 else if (type->tt_type == VAR_BLOB)
3419 {
3420 // inline "add(blob, nr)" so that the type can be checked
3421 res = generate_BLOBAPPEND(cctx);
3422 idx = -1;
3423 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003424 }
3425
3426 if (idx >= 0)
3427 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3428 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003429 else
3430 semsg(_(e_unknownfunc), namebuf);
3431 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 }
3433
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003434 // An argument or local variable can be a function reference, this
3435 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003436 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003437 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003438 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003439 // If we can find the function by name generate the right call.
3440 // Skip global functions here, a local funcref takes precedence.
3441 ufunc = find_func(name, FALSE, cctx);
3442 if (ufunc != NULL && !func_is_global(ufunc))
3443 {
3444 res = generate_CALL(cctx, ufunc, argcount);
3445 goto theend;
3446 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448
3449 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003450 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003451 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003453 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003454 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003455 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003456 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003457 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003458
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003459 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003460 goto theend;
3461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462
Bram Moolenaar0f769812020-09-12 18:32:34 +02003463 // If we can find a global function by name generate the right call.
3464 if (ufunc != NULL)
3465 {
3466 res = generate_CALL(cctx, ufunc, argcount);
3467 goto theend;
3468 }
3469
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003470 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003471 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003472 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003473 res = generate_UCALL(cctx, name, argcount);
3474 else
3475 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003476
3477theend:
3478 vim_free(tofree);
3479 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480}
3481
3482// like NAMESPACE_CHAR but with 'a' and 'l'.
3483#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3484
3485/*
3486 * Find the end of a variable or function name. Unlike find_name_end() this
3487 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003488 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 * Return a pointer to just after the name. Equal to "arg" if there is no
3490 * valid name.
3491 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003492 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003493to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494{
3495 char_u *p;
3496
3497 // Quick check for valid starting character.
3498 if (!eval_isnamec1(*arg))
3499 return arg;
3500
3501 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3502 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3503 // and can be used in slice "[n:]".
3504 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003505 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3507 break;
3508 return p;
3509}
3510
3511/*
3512 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003513 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 */
3515 char_u *
3516to_name_const_end(char_u *arg)
3517{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003518 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 typval_T rettv;
3520
3521 if (p == arg && *arg == '[')
3522 {
3523
3524 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003525 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 p = arg;
3527 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 return p;
3529}
3530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531/*
3532 * parse a list: [expr, expr]
3533 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003534 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 */
3536 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003537compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538{
3539 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003540 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003542 int is_const;
3543 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003545 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003547 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003548 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003549 semsg(_(e_list_end), *arg);
3550 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003551 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003552 if (*p == ',')
3553 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003554 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003555 return FAIL;
3556 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003557 if (*p == ']')
3558 {
3559 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003560 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003561 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003562 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003563 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003564 if (!is_const)
3565 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 ++count;
3567 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003568 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003570 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3571 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003572 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003573 return FAIL;
3574 }
3575 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003576 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 p = skipwhite(p);
3578 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003579 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003581 ppconst->pp_is_const = is_all_const;
3582 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583}
3584
3585/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003586 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003587 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003588 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 */
3590 static int
3591compile_lambda(char_u **arg, cctx_T *cctx)
3592{
Bram Moolenaare462f522020-12-27 14:43:30 +01003593 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 typval_T rettv;
3595 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003596 evalarg_T evalarg;
3597
3598 CLEAR_FIELD(evalarg);
3599 evalarg.eval_flags = EVAL_EVALUATE;
3600 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601
3602 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003603 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3604 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003605 {
3606 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003607 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003608 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003609
Bram Moolenaar65c44152020-12-24 15:14:01 +01003610 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003612 ++ufunc->uf_refcount;
3613 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614
Bram Moolenaara9931532021-06-12 15:58:16 +02003615 // Compile it here to get the return type. The return type is optional,
3616 // when it's missing use t_unknown. This is recognized in
3617 // compile_return().
3618 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3619 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaare99d4222021-06-13 14:01:26 +02003620 compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003622 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3623 // points into it. Point to the original line to avoid a dangling pointer.
3624 if (evalarg.eval_tofree_cmdline != NULL)
3625 {
3626 size_t off = *arg - evalarg.eval_tofree_cmdline;
3627
3628 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3629 + off;
3630 }
3631
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003632 clear_evalarg(&evalarg, NULL);
3633
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003634 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003635 {
3636 // The return type will now be known.
3637 set_function_type(ufunc);
3638
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003639 // The function reference count will be 1. When the ISN_FUNCREF
3640 // instruction is deleted the reference count is decremented and the
3641 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003642 return generate_FUNCREF(cctx, ufunc);
3643 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003644
3645 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 return FAIL;
3647}
3648
3649/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003650 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003652 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 */
3654 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003655compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656{
3657 garray_T *instr = &cctx->ctx_instr;
3658 int count = 0;
3659 dict_T *d = dict_alloc();
3660 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003661 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003662 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003663 int is_const;
3664 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665
3666 if (d == NULL)
3667 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003668 if (generate_ppconst(cctx, ppconst) == FAIL)
3669 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003670 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003672 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673
Bram Moolenaar23c55272020-06-21 16:58:13 +02003674 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003675 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003676 *arg = NULL;
3677 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003678 }
3679
3680 if (**arg == '}')
3681 break;
3682
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003683 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003685 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003687 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003688 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003689 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003692 if (isn->isn_type == ISN_PUSHNR)
3693 {
3694 char buf[NUMBUFLEN];
3695
3696 // Convert to string at compile time.
3697 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3698 isn->isn_type = ISN_PUSHS;
3699 isn->isn_arg.string = vim_strsave((char_u *)buf);
3700 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 if (isn->isn_type == ISN_PUSHS)
3702 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003703 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003704 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003705 *arg = skipwhite(*arg);
3706 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003707 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003708 emsg(_(e_missing_matching_bracket_after_dict_key));
3709 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003710 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003711 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003713 else
3714 {
3715 // {"name": value},
3716 // {'name': value},
3717 // {name: value} use "name" as a literal key
3718 key = get_literal_key(arg);
3719 if (key == NULL)
3720 return FAIL;
3721 if (generate_PUSHS(cctx, key) == FAIL)
3722 return FAIL;
3723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724
3725 // Check for duplicate keys, if using string keys.
3726 if (key != NULL)
3727 {
3728 item = dict_find(d, key, -1);
3729 if (item != NULL)
3730 {
3731 semsg(_(e_duplicate_key), key);
3732 goto failret;
3733 }
3734 item = dictitem_alloc(key);
3735 if (item != NULL)
3736 {
3737 item->di_tv.v_type = VAR_UNKNOWN;
3738 item->di_tv.v_lock = 0;
3739 if (dict_add(d, item) == FAIL)
3740 dictitem_free(item);
3741 }
3742 }
3743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 if (**arg != ':')
3745 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003746 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003747 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003748 else
3749 semsg(_(e_missing_dict_colon), *arg);
3750 return FAIL;
3751 }
3752 whitep = *arg + 1;
3753 if (!IS_WHITE_OR_NUL(*whitep))
3754 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003755 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 return FAIL;
3757 }
3758
Bram Moolenaar23c55272020-06-21 16:58:13 +02003759 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003760 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003761 *arg = NULL;
3762 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003763 }
3764
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003765 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003767 if (!is_const)
3768 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 ++count;
3770
Bram Moolenaar2c330432020-04-13 14:41:35 +02003771 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003772 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003773 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003774 *arg = NULL;
3775 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 if (**arg == '}')
3778 break;
3779 if (**arg != ',')
3780 {
3781 semsg(_(e_missing_dict_comma), *arg);
3782 goto failret;
3783 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003784 if (IS_WHITE_OR_NUL(*whitep))
3785 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003786 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003787 return FAIL;
3788 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003789 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003790 if (!IS_WHITE_OR_NUL(*whitep))
3791 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003792 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003793 return FAIL;
3794 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003795 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 }
3797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 *arg = *arg + 1;
3799
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003800 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003801 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003802 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003803 *arg += STRLEN(*arg);
3804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003806 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 return generate_NEWDICT(cctx, count);
3808
3809failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003810 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003811 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003812 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003813 *arg = (char_u *)"";
3814 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 dict_unref(d);
3816 return FAIL;
3817}
3818
3819/*
3820 * Compile "&option".
3821 */
3822 static int
3823compile_get_option(char_u **arg, cctx_T *cctx)
3824{
3825 typval_T rettv;
3826 char_u *start = *arg;
3827 int ret;
3828
3829 // parse the option and get the current value to get the type.
3830 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003831 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 if (ret == OK)
3833 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003834 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003835 char_u *name = vim_strnsave(start, *arg - start);
3836 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3837 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838
3839 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3840 vim_free(name);
3841 }
3842 clear_tv(&rettv);
3843
3844 return ret;
3845}
3846
3847/*
3848 * Compile "$VAR".
3849 */
3850 static int
3851compile_get_env(char_u **arg, cctx_T *cctx)
3852{
3853 char_u *start = *arg;
3854 int len;
3855 int ret;
3856 char_u *name;
3857
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 ++*arg;
3859 len = get_env_len(arg);
3860 if (len == 0)
3861 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003862 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 return FAIL;
3864 }
3865
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003866 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 name = vim_strnsave(start, len + 1);
3868 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3869 vim_free(name);
3870 return ret;
3871}
3872
3873/*
3874 * Compile "@r".
3875 */
3876 static int
3877compile_get_register(char_u **arg, cctx_T *cctx)
3878{
3879 int ret;
3880
3881 ++*arg;
3882 if (**arg == NUL)
3883 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003884 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 return FAIL;
3886 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003887 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 {
3889 emsg_invreg(**arg);
3890 return FAIL;
3891 }
3892 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3893 ++*arg;
3894 return ret;
3895}
3896
3897/*
3898 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003899 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 */
3901 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003902apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003904 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905
3906 // this works from end to start
3907 while (p > start)
3908 {
3909 --p;
3910 if (*p == '-' || *p == '+')
3911 {
3912 // only '-' has an effect, for '+' we only check the type
3913#ifdef FEAT_FLOAT
3914 if (rettv->v_type == VAR_FLOAT)
3915 {
3916 if (*p == '-')
3917 rettv->vval.v_float = -rettv->vval.v_float;
3918 }
3919 else
3920#endif
3921 {
3922 varnumber_T val;
3923 int error = FALSE;
3924
3925 // tv_get_number_chk() accepts a string, but we don't want that
3926 // here
3927 if (check_not_string(rettv) == FAIL)
3928 return FAIL;
3929 val = tv_get_number_chk(rettv, &error);
3930 clear_tv(rettv);
3931 if (error)
3932 return FAIL;
3933 if (*p == '-')
3934 val = -val;
3935 rettv->v_type = VAR_NUMBER;
3936 rettv->vval.v_number = val;
3937 }
3938 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003939 else if (numeric_only)
3940 {
3941 ++p;
3942 break;
3943 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003944 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 {
3946 int v = tv2bool(rettv);
3947
3948 // '!' is permissive in the type.
3949 clear_tv(rettv);
3950 rettv->v_type = VAR_BOOL;
3951 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3952 }
3953 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003954 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 return OK;
3956}
3957
3958/*
3959 * Recognize v: variables that are constants and set "rettv".
3960 */
3961 static void
3962get_vim_constant(char_u **arg, typval_T *rettv)
3963{
3964 if (STRNCMP(*arg, "v:true", 6) == 0)
3965 {
3966 rettv->v_type = VAR_BOOL;
3967 rettv->vval.v_number = VVAL_TRUE;
3968 *arg += 6;
3969 }
3970 else if (STRNCMP(*arg, "v:false", 7) == 0)
3971 {
3972 rettv->v_type = VAR_BOOL;
3973 rettv->vval.v_number = VVAL_FALSE;
3974 *arg += 7;
3975 }
3976 else if (STRNCMP(*arg, "v:null", 6) == 0)
3977 {
3978 rettv->v_type = VAR_SPECIAL;
3979 rettv->vval.v_number = VVAL_NULL;
3980 *arg += 6;
3981 }
3982 else if (STRNCMP(*arg, "v:none", 6) == 0)
3983 {
3984 rettv->v_type = VAR_SPECIAL;
3985 rettv->vval.v_number = VVAL_NONE;
3986 *arg += 6;
3987 }
3988}
3989
Bram Moolenaar657137c2021-01-09 15:45:23 +01003990 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003991get_compare_type(char_u *p, int *len, int *type_is)
3992{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003993 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003994 int i;
3995
3996 switch (p[0])
3997 {
3998 case '=': if (p[1] == '=')
3999 type = EXPR_EQUAL;
4000 else if (p[1] == '~')
4001 type = EXPR_MATCH;
4002 break;
4003 case '!': if (p[1] == '=')
4004 type = EXPR_NEQUAL;
4005 else if (p[1] == '~')
4006 type = EXPR_NOMATCH;
4007 break;
4008 case '>': if (p[1] != '=')
4009 {
4010 type = EXPR_GREATER;
4011 *len = 1;
4012 }
4013 else
4014 type = EXPR_GEQUAL;
4015 break;
4016 case '<': if (p[1] != '=')
4017 {
4018 type = EXPR_SMALLER;
4019 *len = 1;
4020 }
4021 else
4022 type = EXPR_SEQUAL;
4023 break;
4024 case 'i': if (p[1] == 's')
4025 {
4026 // "is" and "isnot"; but not a prefix of a name
4027 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4028 *len = 5;
4029 i = p[*len];
4030 if (!isalnum(i) && i != '_')
4031 {
4032 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4033 *type_is = TRUE;
4034 }
4035 }
4036 break;
4037 }
4038 return type;
4039}
4040
4041/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004042 * Skip over an expression, ignoring most errors.
4043 */
4044 static void
4045skip_expr_cctx(char_u **arg, cctx_T *cctx)
4046{
4047 evalarg_T evalarg;
4048
4049 CLEAR_FIELD(evalarg);
4050 evalarg.eval_cctx = cctx;
4051 skip_expr(arg, &evalarg);
4052}
4053
4054/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004056 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 */
4058 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004059compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004061 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062
4063 // this works from end to start
4064 while (p > start)
4065 {
4066 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004067 while (VIM_ISWHITE(*p))
4068 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 if (*p == '-' || *p == '+')
4070 {
4071 int negate = *p == '-';
4072 isn_T *isn;
4073
4074 // TODO: check type
4075 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4076 {
4077 --p;
4078 if (*p == '-')
4079 negate = !negate;
4080 }
4081 // only '-' has an effect, for '+' we only check the type
4082 if (negate)
4083 isn = generate_instr(cctx, ISN_NEGATENR);
4084 else
4085 isn = generate_instr(cctx, ISN_CHECKNR);
4086 if (isn == NULL)
4087 return FAIL;
4088 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004089 else if (numeric_only)
4090 {
4091 ++p;
4092 break;
4093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 else
4095 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004096 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004098 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004100 if (p[-1] == '!')
4101 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004104 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 return FAIL;
4106 }
4107 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004108 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 return OK;
4110}
4111
4112/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004113 * Compile "(expression)": recursive!
4114 * Return FAIL/OK.
4115 */
4116 static int
4117compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4118{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004119 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004120 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004121
Bram Moolenaar24156692021-01-14 20:35:49 +01004122 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4123 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004124 if (ppconst->pp_used <= PPSIZE - 10)
4125 {
4126 ret = compile_expr1(arg, cctx, ppconst);
4127 }
4128 else
4129 {
4130 // Not enough space in ppconst, flush constants.
4131 if (generate_ppconst(cctx, ppconst) == FAIL)
4132 return FAIL;
4133 ret = compile_expr0(arg, cctx);
4134 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004135 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4136 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004137 if (**arg == ')')
4138 ++*arg;
4139 else if (ret == OK)
4140 {
4141 emsg(_(e_missing_close));
4142 ret = FAIL;
4143 }
4144 return ret;
4145}
4146
4147/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004149 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 */
4151 static int
4152compile_subscript(
4153 char_u **arg,
4154 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004155 char_u *start_leader,
4156 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004157 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004159 char_u *name_start = *end_leader;
4160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 for (;;)
4162 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004163 char_u *p = skipwhite(*arg);
4164
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004165 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004166 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004167 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004168
4169 // If a following line starts with "->{" or "->X" advance to that
4170 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004171 // Also if a following line starts with ".x".
4172 if (next != NULL &&
4173 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004174 && (next[2] == '{'
4175 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004176 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004177 {
4178 next = next_line_from_context(cctx, TRUE);
4179 if (next == NULL)
4180 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004181 *arg = next;
4182 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004183 }
4184 }
4185
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004186 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004187 // not a function call.
4188 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004190 garray_T *stack = &cctx->ctx_type_stack;
4191 type_T *type;
4192 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004194 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004195 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004196 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004199 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4200
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004201 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004202 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004204 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 return FAIL;
4206 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004207 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004209 char_u *pstart = p;
4210
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004211 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004212 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004213 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 // something->method()
4216 // Apply the '!', '-' and '+' first:
4217 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004218 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004221 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004222 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004223 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004224 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004225 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004226 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004227 garray_T *stack = &cctx->ctx_type_stack;
4228 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004229 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004230 int expr_isn_start = cctx->ctx_instr.ga_len;
4231 int expr_isn_end;
4232 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004233
4234 // Funcref call: list->(Refs[2])(arg)
4235 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004236 //
4237 // Fist compile the function expression.
4238 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004239 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004240
4241 // Remember the next instruction index, where the instructions
4242 // for arguments are being written.
4243 expr_isn_end = cctx->ctx_instr.ga_len;
4244
4245 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004246 if (**arg != '(')
4247 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004248 if (*skipwhite(*arg) == '(')
4249 emsg(_(e_nowhitespace));
4250 else
4251 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004252 return FAIL;
4253 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004254 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004255 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004256 return FAIL;
4257
Bram Moolenaar2927c072021-04-05 19:41:21 +02004258 // Move the instructions for the arguments to before the
4259 // instructions of the expression and move the type of the
4260 // expression after the argument types. This is what ISN_PCALL
4261 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004262 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004263 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4264 if (arg_isn_count > 0)
4265 {
4266 int expr_isn_count = expr_isn_end - expr_isn_start;
4267 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4268
4269 if (isn == NULL)
4270 return FAIL;
4271 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4272 + expr_isn_start,
4273 sizeof(isn_T) * expr_isn_count);
4274 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4275 + expr_isn_start,
4276 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4277 sizeof(isn_T) * arg_isn_count);
4278 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4279 + expr_isn_start + arg_isn_count,
4280 isn, sizeof(isn_T) * expr_isn_count);
4281 vim_free(isn);
4282
4283 type = ((type_T **)stack->ga_data)[type_idx_start];
4284 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4285 ((type_T **)stack->ga_data) + type_idx_start + 1,
4286 sizeof(type_T *)
4287 * (stack->ga_len - type_idx_start - 1));
4288 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4289 }
4290
Bram Moolenaar7e368202020-12-25 21:56:57 +01004291 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4292 if (generate_PCALL(cctx, argcount,
4293 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 return FAIL;
4295 }
4296 else
4297 {
4298 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004299 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004300 if (!eval_isnamec1(*p))
4301 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004302 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004303 return FAIL;
4304 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004305 if (ASCII_ISALPHA(*p) && p[1] == ':')
4306 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004307 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 ;
4309 if (*p != '(')
4310 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004311 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 return FAIL;
4313 }
4314 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004315 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 return FAIL;
4317 }
4318 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004319 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004321 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004322
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004323 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004324 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004325 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004326 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004327 // TODO: more arguments
4328 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004329 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004330 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004331 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004332
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004333 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004334 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004335 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004336 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004337 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004338 // missing first index is equal to zero
4339 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004340 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004341 else
4342 {
4343 if (compile_expr0(arg, cctx) == FAIL)
4344 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004345 if (**arg == ':')
4346 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004347 semsg(_(e_white_space_required_before_and_after_str_at_str),
4348 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004349 return FAIL;
4350 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004351 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004352 return FAIL;
4353 *arg = skipwhite(*arg);
4354 }
4355 if (**arg == ':')
4356 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004357 is_slice = TRUE;
4358 ++*arg;
4359 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4360 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004361 semsg(_(e_white_space_required_before_and_after_str_at_str),
4362 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004363 return FAIL;
4364 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004365 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004366 return FAIL;
4367 if (**arg == ']')
4368 // missing second index is equal to end of string
4369 generate_PUSHNR(cctx, -1);
4370 else
4371 {
4372 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004373 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004374 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004375 return FAIL;
4376 *arg = skipwhite(*arg);
4377 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004378 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379
4380 if (**arg != ']')
4381 {
4382 emsg(_(e_missbrac));
4383 return FAIL;
4384 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004385 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386
Bram Moolenaare42939a2021-04-05 17:11:17 +02004387 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004388 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004390 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004392 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004393 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004394 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004395 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004396
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004397 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004398 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004399 {
4400 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004401 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004402 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004403 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004404 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 while (eval_isnamec(*p))
4406 MB_PTR_ADV(p);
4407 if (p == *arg)
4408 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004409 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 return FAIL;
4411 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004412 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 return FAIL;
4414 *arg = p;
4415 }
4416 else
4417 break;
4418 }
4419
4420 // TODO - see handle_subscript():
4421 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4422 // Don't do this when "Func" is already a partial that was bound
4423 // explicitly (pt_auto is FALSE).
4424
4425 return OK;
4426}
4427
4428/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004429 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4430 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004432 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004433 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004434 *
4435 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 */
4437
4438/*
4439 * number number constant
4440 * 0zFFFFFFFF Blob constant
4441 * "string" string constant
4442 * 'string' literal string constant
4443 * &option-name option value
4444 * @r register contents
4445 * identifier variable value
4446 * function() function call
4447 * $VAR environment variable
4448 * (expression) nested expression
4449 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004450 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 *
4452 * Also handle:
4453 * ! in front logical NOT
4454 * - in front unary minus
4455 * + in front unary plus (ignored)
4456 * trailing (arg) funcref/partial call
4457 * trailing [] subscript in String or List
4458 * trailing .name entry in Dictionary
4459 * trailing ->name() method call
4460 */
4461 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004462compile_expr7(
4463 char_u **arg,
4464 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004465 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 char_u *start_leader, *end_leader;
4468 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004469 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004470 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004472 ppconst->pp_is_const = FALSE;
4473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 /*
4475 * Skip '!', '-' and '+' characters. They are handled later.
4476 */
4477 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004478 if (eval_leader(arg, TRUE) == FAIL)
4479 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 end_leader = *arg;
4481
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004482 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483 switch (**arg)
4484 {
4485 /*
4486 * Number constant.
4487 */
4488 case '0': // also for blob starting with 0z
4489 case '1':
4490 case '2':
4491 case '3':
4492 case '4':
4493 case '5':
4494 case '6':
4495 case '7':
4496 case '8':
4497 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004498 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004500 // Apply "-" and "+" just before the number now, right to
4501 // left. Matters especially when "->" follows. Stops at
4502 // '!'.
4503 if (apply_leader(rettv, TRUE,
4504 start_leader, &end_leader) == FAIL)
4505 {
4506 clear_tv(rettv);
4507 return FAIL;
4508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 break;
4510
4511 /*
4512 * String constant: "string".
4513 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004514 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 return FAIL;
4516 break;
4517
4518 /*
4519 * Literal string constant: 'str''ing'.
4520 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004521 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 return FAIL;
4523 break;
4524
4525 /*
4526 * Constant Vim variable.
4527 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004528 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 ret = NOTDONE;
4530 break;
4531
4532 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004533 * "true" constant
4534 */
4535 case 't': if (STRNCMP(*arg, "true", 4) == 0
4536 && !eval_isnamec((*arg)[4]))
4537 {
4538 *arg += 4;
4539 rettv->v_type = VAR_BOOL;
4540 rettv->vval.v_number = VVAL_TRUE;
4541 }
4542 else
4543 ret = NOTDONE;
4544 break;
4545
4546 /*
4547 * "false" constant
4548 */
4549 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4550 && !eval_isnamec((*arg)[5]))
4551 {
4552 *arg += 5;
4553 rettv->v_type = VAR_BOOL;
4554 rettv->vval.v_number = VVAL_FALSE;
4555 }
4556 else
4557 ret = NOTDONE;
4558 break;
4559
4560 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004561 * "null" constant
4562 */
4563 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004564 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004565 {
4566 *arg += 4;
4567 rettv->v_type = VAR_SPECIAL;
4568 rettv->vval.v_number = VVAL_NULL;
4569 }
4570 else
4571 ret = NOTDONE;
4572 break;
4573
4574 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 * List: [expr, expr]
4576 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004577 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4578 return FAIL;
4579 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 break;
4581
4582 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 * Dictionary: {'key': val, 'key': val}
4584 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004585 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4586 return FAIL;
4587 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 break;
4589
4590 /*
4591 * Option value: &name
4592 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004593 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4594 return FAIL;
4595 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 break;
4597
4598 /*
4599 * Environment variable: $VAR.
4600 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004601 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4602 return FAIL;
4603 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 break;
4605
4606 /*
4607 * Register contents: @r.
4608 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004609 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4610 return FAIL;
4611 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 break;
4613 /*
4614 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004615 * lambda: (arg, arg) => expr
4616 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004618 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4619 ret = compile_lambda(arg, cctx);
4620 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004621 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 break;
4623
4624 default: ret = NOTDONE;
4625 break;
4626 }
4627 if (ret == FAIL)
4628 return FAIL;
4629
Bram Moolenaar1c747212020-05-09 18:28:34 +02004630 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004632 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004633 clear_tv(rettv);
4634 else
4635 // A constant expression can possibly be handled compile time,
4636 // return the value instead of generating code.
4637 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 }
4639 else if (ret == NOTDONE)
4640 {
4641 char_u *p;
4642 int r;
4643
4644 if (!eval_isnamec1(**arg))
4645 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004646 if (!vim9_bad_comment(*arg))
4647 {
4648 if (ends_excmd(*skipwhite(*arg)))
4649 semsg(_(e_empty_expression_str), *arg);
4650 else
4651 semsg(_(e_name_expected_str), *arg);
4652 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 return FAIL;
4654 }
4655
4656 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004657 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004658 if (p - *arg == (size_t)1 && **arg == '_')
4659 {
4660 emsg(_(e_cannot_use_underscore_here));
4661 return FAIL;
4662 }
4663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004665 {
4666 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4667 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 {
4670 if (generate_ppconst(cctx, ppconst) == FAIL)
4671 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004672 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004673 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 if (r == FAIL)
4675 return FAIL;
4676 }
4677
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004678 // Handle following "[]", ".member", etc.
4679 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004680 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004681 ppconst) == FAIL)
4682 return FAIL;
4683 if (ppconst->pp_used > 0)
4684 {
4685 // apply the '!', '-' and '+' before the constant
4686 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004687 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004688 return FAIL;
4689 return OK;
4690 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004691 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004693 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694}
4695
4696/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004697 * Give the "white on both sides" error, taking the operator from "p[len]".
4698 */
4699 void
4700error_white_both(char_u *op, int len)
4701{
4702 char_u buf[10];
4703
4704 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004705 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004706}
4707
4708/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004709 * <type>expr7: runtime type check / conversion
4710 */
4711 static int
4712compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4713{
4714 type_T *want_type = NULL;
4715
4716 // Recognize <type>
4717 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4718 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004719 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004720 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4721 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004722 return FAIL;
4723
4724 if (**arg != '>')
4725 {
4726 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004727 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004728 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004729 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004730 return FAIL;
4731 }
4732 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004733 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004734 return FAIL;
4735 }
4736
4737 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4738 return FAIL;
4739
4740 if (want_type != NULL)
4741 {
4742 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004743 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004744 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004745
Bram Moolenaard1103582020-08-14 22:44:25 +02004746 generate_ppconst(cctx, ppconst);
4747 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004748 where.wt_index = 0;
4749 where.wt_variable = FALSE;
4750 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004751 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004752 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004753 return FAIL;
4754 }
4755 }
4756
4757 return OK;
4758}
4759
4760/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 * * number multiplication
4762 * / number division
4763 * % number modulo
4764 */
4765 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004766compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767{
4768 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004769 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004770 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004772 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004773 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774 return FAIL;
4775
4776 /*
4777 * Repeat computing, until no "*", "/" or "%" is following.
4778 */
4779 for (;;)
4780 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004781 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782 if (*op != '*' && *op != '/' && *op != '%')
4783 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004784 if (next != NULL)
4785 {
4786 *arg = next_line_from_context(cctx, TRUE);
4787 op = skipwhite(*arg);
4788 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004789
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004790 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004792 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004793 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004795 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004796 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004798 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004799 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004801
4802 if (ppconst->pp_used == ppconst_used + 2
4803 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4804 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004805 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004806 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4807 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4808 varnumber_T res = 0;
4809 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004811 // both are numbers: compute the result
4812 switch (*op)
4813 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004814 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004815 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004816 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004817 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004818 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004819 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004820 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004821 break;
4822 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004823 if (failed)
4824 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004825 tv1->vval.v_number = res;
4826 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004827 }
4828 else
4829 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004830 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004831 generate_two_op(cctx, op);
4832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 }
4834
4835 return OK;
4836}
4837
4838/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004839 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 * - number subtraction
4841 * .. string concatenation
4842 */
4843 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004844compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845{
4846 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004847 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004849 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850
4851 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004852 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 return FAIL;
4854
4855 /*
4856 * Repeat computing, until no "+", "-" or ".." is following.
4857 */
4858 for (;;)
4859 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004860 op = may_peek_next_line(cctx, *arg, &next);
4861 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004863 if (op[0] == op[1] && *op != '.' && next)
4864 // Finding "++" or "--" on the next line is a separate command.
4865 // But ".." is concatenation.
4866 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004868 if (next != NULL)
4869 {
4870 *arg = next_line_from_context(cctx, TRUE);
4871 op = skipwhite(*arg);
4872 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004874 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004876 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004877 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 }
4879
Bram Moolenaare0de1712020-12-02 17:36:54 +01004880 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004881 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004883 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004884 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 return FAIL;
4886
Bram Moolenaara5565e42020-05-09 15:44:01 +02004887 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004888 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004889 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4890 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4891 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4892 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004894 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4895 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004896
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004897 // concat/subtract/add constant numbers
4898 if (*op == '+')
4899 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4900 else if (*op == '-')
4901 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4902 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004903 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004904 // concatenate constant strings
4905 char_u *s1 = tv1->vval.v_string;
4906 char_u *s2 = tv2->vval.v_string;
4907 size_t len1 = STRLEN(s1);
4908
4909 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4910 if (tv1->vval.v_string == NULL)
4911 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004912 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004913 return FAIL;
4914 }
4915 mch_memmove(tv1->vval.v_string, s1, len1);
4916 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004917 vim_free(s1);
4918 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004919 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004920 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921 }
4922 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004923 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004924 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004925 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004926 if (*op == '.')
4927 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004928 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4929 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004930 return FAIL;
4931 generate_instr_drop(cctx, ISN_CONCAT, 1);
4932 }
4933 else
4934 generate_two_op(cctx, op);
4935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 }
4937
4938 return OK;
4939}
4940
4941/*
4942 * expr5a == expr5b
4943 * expr5a =~ expr5b
4944 * expr5a != expr5b
4945 * expr5a !~ expr5b
4946 * expr5a > expr5b
4947 * expr5a >= expr5b
4948 * expr5a < expr5b
4949 * expr5a <= expr5b
4950 * expr5a is expr5b
4951 * expr5a isnot expr5b
4952 *
4953 * Produces instructions:
4954 * EVAL expr5a Push result of "expr5a"
4955 * EVAL expr5b Push result of "expr5b"
4956 * COMPARE one of the compare instructions
4957 */
4958 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004959compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004961 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004963 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004966 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967
4968 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004969 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 return FAIL;
4971
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004972 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004973 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974
4975 /*
4976 * If there is a comparative operator, use it.
4977 */
4978 if (type != EXPR_UNKNOWN)
4979 {
4980 int ic = FALSE; // Default: do not ignore case
4981
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004982 if (next != NULL)
4983 {
4984 *arg = next_line_from_context(cctx, TRUE);
4985 p = skipwhite(*arg);
4986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 if (type_is && (p[len] == '?' || p[len] == '#'))
4988 {
4989 semsg(_(e_invexpr2), *arg);
4990 return FAIL;
4991 }
4992 // extra question mark appended: ignore case
4993 if (p[len] == '?')
4994 {
4995 ic = TRUE;
4996 ++len;
4997 }
4998 // extra '#' appended: match case (ignored)
4999 else if (p[len] == '#')
5000 ++len;
5001 // nothing appended: match case
5002
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005003 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005005 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005006 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007 }
5008
5009 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005010 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005011 return FAIL;
5012
Bram Moolenaara5565e42020-05-09 15:44:01 +02005013 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 return FAIL;
5015
Bram Moolenaara5565e42020-05-09 15:44:01 +02005016 if (ppconst->pp_used == ppconst_used + 2)
5017 {
5018 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5019 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5020 int ret;
5021
5022 // Both sides are a constant, compute the result now.
5023 // First check for a valid combination of types, this is more
5024 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005025 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005026 ret = FAIL;
5027 else
5028 {
5029 ret = typval_compare(tv1, tv2, type, ic);
5030 tv1->v_type = VAR_BOOL;
5031 tv1->vval.v_number = tv1->vval.v_number
5032 ? VVAL_TRUE : VVAL_FALSE;
5033 clear_tv(tv2);
5034 --ppconst->pp_used;
5035 }
5036 return ret;
5037 }
5038
5039 generate_ppconst(cctx, ppconst);
5040 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041 }
5042
5043 return OK;
5044}
5045
Bram Moolenaar7f141552020-05-09 17:35:53 +02005046static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048/*
5049 * Compile || or &&.
5050 */
5051 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005052compile_and_or(
5053 char_u **arg,
5054 cctx_T *cctx,
5055 char *op,
5056 ppconst_T *ppconst,
5057 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005059 char_u *next;
5060 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061 int opchar = *op;
5062
5063 if (p[0] == opchar && p[1] == opchar)
5064 {
5065 garray_T *instr = &cctx->ctx_instr;
5066 garray_T end_ga;
5067
5068 /*
5069 * Repeat until there is no following "||" or "&&"
5070 */
5071 ga_init2(&end_ga, sizeof(int), 10);
5072 while (p[0] == opchar && p[1] == opchar)
5073 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005074 long start_lnum = SOURCING_LNUM;
5075 int start_ctx_lnum = cctx->ctx_lnum;
5076 int save_lnum;
5077
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005078 if (next != NULL)
5079 {
5080 *arg = next_line_from_context(cctx, TRUE);
5081 p = skipwhite(*arg);
5082 }
5083
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005084 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5085 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005086 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005087 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005088 return FAIL;
5089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005091 // TODO: use ppconst if the value is a constant and check
5092 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005093 generate_ppconst(cctx, ppconst);
5094
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005095 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005096 SOURCING_LNUM = start_lnum;
5097 save_lnum = cctx->ctx_lnum;
5098 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005099 if (bool_on_stack(cctx) == FAIL)
5100 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005101 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005102 ga_clear(&end_ga);
5103 return FAIL;
5104 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005105 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 if (ga_grow(&end_ga, 1) == FAIL)
5108 {
5109 ga_clear(&end_ga);
5110 return FAIL;
5111 }
5112 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5113 ++end_ga.ga_len;
5114 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005115 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116
5117 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005118 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005119 {
5120 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005121 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005122 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005123
Bram Moolenaara5565e42020-05-09 15:44:01 +02005124 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5125 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126 {
5127 ga_clear(&end_ga);
5128 return FAIL;
5129 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005130
5131 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005133 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005135 // Every part must evaluate to a bool.
5136 if (bool_on_stack(cctx) == FAIL)
5137 {
5138 ga_clear(&end_ga);
5139 return FAIL;
5140 }
5141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142 // Fill in the end label in all jumps.
5143 while (end_ga.ga_len > 0)
5144 {
5145 isn_T *isn;
5146
5147 --end_ga.ga_len;
5148 isn = ((isn_T *)instr->ga_data)
5149 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5150 isn->isn_arg.jump.jump_where = instr->ga_len;
5151 }
5152 ga_clear(&end_ga);
5153 }
5154
5155 return OK;
5156}
5157
5158/*
5159 * expr4a && expr4a && expr4a logical AND
5160 *
5161 * Produces instructions:
5162 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005163 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005164 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005165 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005166 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 * EVAL expr4c Push result of "expr4c"
5168 * end:
5169 */
5170 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005171compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005173 int ppconst_used = ppconst->pp_used;
5174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005175 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005176 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 return FAIL;
5178
5179 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005180 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181}
5182
5183/*
5184 * expr3a || expr3b || expr3c logical OR
5185 *
5186 * Produces instructions:
5187 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005188 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005189 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005191 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 * EVAL expr3c Push result of "expr3c"
5193 * end:
5194 */
5195 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005196compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005198 int ppconst_used = ppconst->pp_used;
5199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005201 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 return FAIL;
5203
5204 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005205 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206}
5207
5208/*
5209 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005211 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 * JUMP_IF_FALSE alt jump if false
5213 * EVAL expr1a
5214 * JUMP_ALWAYS end
5215 * alt: EVAL expr1b
5216 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005217 *
5218 * Toplevel expression: expr2 ?? expr1
5219 * Produces instructions:
5220 * EVAL expr2 Push result of "expr2"
5221 * JUMP_AND_KEEP_IF_TRUE end jump if true
5222 * EVAL expr1
5223 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005224 */
5225 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005226compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227{
5228 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005229 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005230 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231
Bram Moolenaar3988f642020-08-27 22:43:03 +02005232 // Ignore all kinds of errors when not producing code.
5233 if (cctx->ctx_skip == SKIP_YES)
5234 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005235 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005236 return OK;
5237 }
5238
Bram Moolenaar61a89812020-05-07 16:58:17 +02005239 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005240 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241 return FAIL;
5242
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005243 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005244 if (*p == '?')
5245 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005246 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 garray_T *instr = &cctx->ctx_instr;
5248 garray_T *stack = &cctx->ctx_type_stack;
5249 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005250 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005252 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005253 int has_const_expr = FALSE;
5254 int const_value = FALSE;
5255 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005257 if (next != NULL)
5258 {
5259 *arg = next_line_from_context(cctx, TRUE);
5260 p = skipwhite(*arg);
5261 }
5262
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005263 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005264 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005265 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005266 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005267 return FAIL;
5268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269
Bram Moolenaara5565e42020-05-09 15:44:01 +02005270 if (ppconst->pp_used == ppconst_used + 1)
5271 {
5272 // the condition is a constant, we know whether the ? or the :
5273 // expression is to be evaluated.
5274 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005275 if (op_falsy)
5276 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5277 else
5278 {
5279 int error = FALSE;
5280
5281 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5282 &error);
5283 if (error)
5284 return FAIL;
5285 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005286 cctx->ctx_skip = save_skip == SKIP_YES ||
5287 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5288
5289 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5290 // "left ?? right" and "left" is truthy: produce "left"
5291 generate_ppconst(cctx, ppconst);
5292 else
5293 {
5294 clear_tv(&ppconst->pp_tv[ppconst_used]);
5295 --ppconst->pp_used;
5296 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005297 }
5298 else
5299 {
5300 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005301 if (op_falsy)
5302 end_idx = instr->ga_len;
5303 generate_JUMP(cctx, op_falsy
5304 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5305 if (op_falsy)
5306 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308
5309 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005310 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005311 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005312 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005313 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005314
Bram Moolenaara5565e42020-05-09 15:44:01 +02005315 if (!has_const_expr)
5316 {
5317 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005319 if (!op_falsy)
5320 {
5321 // remember the type and drop it
5322 --stack->ga_len;
5323 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005325 end_idx = instr->ga_len;
5326 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005327
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005328 // jump here from JUMP_IF_FALSE
5329 isn = ((isn_T *)instr->ga_data) + alt_idx;
5330 isn->isn_arg.jump.jump_where = instr->ga_len;
5331 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005334 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005336 // Check for the ":".
5337 p = may_peek_next_line(cctx, *arg, &next);
5338 if (*p != ':')
5339 {
5340 emsg(_(e_missing_colon));
5341 return FAIL;
5342 }
5343 if (next != NULL)
5344 {
5345 *arg = next_line_from_context(cctx, TRUE);
5346 p = skipwhite(*arg);
5347 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005348
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005349 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5350 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005351 semsg(_(e_white_space_required_before_and_after_str_at_str),
5352 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005353 return FAIL;
5354 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005356 // evaluate the third expression
5357 if (has_const_expr)
5358 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005359 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005360 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005361 return FAIL;
5362 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5363 return FAIL;
5364 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365
Bram Moolenaara5565e42020-05-09 15:44:01 +02005366 if (!has_const_expr)
5367 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005368 type_T **typep;
5369
Bram Moolenaara5565e42020-05-09 15:44:01 +02005370 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005371
Bram Moolenaara5565e42020-05-09 15:44:01 +02005372 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005373 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5374 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005375
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005376 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005377 isn = ((isn_T *)instr->ga_data) + end_idx;
5378 isn->isn_arg.jump.jump_where = instr->ga_len;
5379 }
5380
5381 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005382 }
5383 return OK;
5384}
5385
5386/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005387 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005388 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5389 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005390 */
5391 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005392compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005393{
5394 ppconst_T ppconst;
5395
5396 CLEAR_FIELD(ppconst);
5397 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5398 {
5399 clear_ppconst(&ppconst);
5400 return FAIL;
5401 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005402 if (is_const != NULL)
5403 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005404 if (generate_ppconst(cctx, &ppconst) == FAIL)
5405 return FAIL;
5406 return OK;
5407}
5408
5409/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005410 * Toplevel expression.
5411 */
5412 static int
5413compile_expr0(char_u **arg, cctx_T *cctx)
5414{
5415 return compile_expr0_ext(arg, cctx, NULL);
5416}
5417
5418/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005419 * Compile "return [expr]".
5420 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421 */
5422 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005423compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424{
5425 char_u *p = arg;
5426 garray_T *stack = &cctx->ctx_type_stack;
5427 type_T *stack_type;
5428
5429 if (*p != NUL && *p != '|' && *p != '\n')
5430 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005431 if (legacy)
5432 {
5433 int save_flags = cmdmod.cmod_flags;
5434
5435 generate_LEGACY_EVAL(cctx, p);
5436 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5437 0, cctx, FALSE, FALSE) == FAIL)
5438 return NULL;
5439 cmdmod.cmod_flags |= CMOD_LEGACY;
5440 (void)skip_expr(&p, NULL);
5441 cmdmod.cmod_flags = save_flags;
5442 }
5443 else
5444 {
5445 // compile return argument into instructions
5446 if (compile_expr0(&p, cctx) == FAIL)
5447 return NULL;
5448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005450 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005451 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005452 // "check_return_type" with uf_ret_type set to &t_unknown is used
5453 // for an inline function without a specified return type. Set the
5454 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005455 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005456 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005457 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5458 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005459 || (!check_return_type
5460 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005461 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005462 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005463 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005464 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005465 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005466 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5467 && stack_type->tt_type != VAR_VOID
5468 && stack_type->tt_type != VAR_UNKNOWN)
5469 {
5470 emsg(_(e_returning_value_in_function_without_return_type));
5471 return NULL;
5472 }
5473 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005474 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005475 return NULL;
5476 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005477 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005478 }
5479 else
5480 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005481 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005482 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005483 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5484 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005485 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005486 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005487 return NULL;
5488 }
5489
5490 // No argument, return zero.
5491 generate_PUSHNR(cctx, 0);
5492 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005493
5494 // Undo any command modifiers.
5495 generate_undo_cmdmods(cctx);
5496
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005497 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005498 return NULL;
5499
5500 // "return val | endif" is possible
5501 return skipwhite(p);
5502}
5503
5504/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005505 * Get a line from the compilation context, compatible with exarg_T getline().
5506 * Return a pointer to the line in allocated memory.
5507 * Return NULL for end-of-file or some error.
5508 */
5509 static char_u *
5510exarg_getline(
5511 int c UNUSED,
5512 void *cookie,
5513 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005514 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005515{
5516 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005517 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005518
Bram Moolenaar66250c92020-08-20 15:02:42 +02005519 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005520 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005521 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005522 return NULL;
5523 ++cctx->ctx_lnum;
5524 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5525 // Comment lines result in NULL pointers, skip them.
5526 if (p != NULL)
5527 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005528 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005529}
5530
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005531 void
5532fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5533{
5534 eap->getline = exarg_getline;
5535 eap->cookie = cctx;
5536}
5537
Bram Moolenaar04b12692020-05-04 23:24:44 +02005538/*
5539 * Compile a nested :def command.
5540 */
5541 static char_u *
5542compile_nested_function(exarg_T *eap, cctx_T *cctx)
5543{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005544 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005545 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005546 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005547 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005548 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005549 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005550
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005551 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005552 {
5553 emsg(_(e_cannot_use_bang_with_nested_def));
5554 return NULL;
5555 }
5556
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005557 if (*name_start == '/')
5558 {
5559 name_end = skip_regexp(name_start + 1, '/', TRUE);
5560 if (*name_end == '/')
5561 ++name_end;
5562 eap->nextcmd = check_nextcmd(name_end);
5563 }
5564 if (name_end == name_start || *skipwhite(name_end) != '(')
5565 {
5566 if (!ends_excmd2(name_start, name_end))
5567 {
5568 semsg(_(e_invalid_command_str), eap->cmd);
5569 return NULL;
5570 }
5571
5572 // "def" or "def Name": list functions
5573 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5574 return NULL;
5575 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5576 }
5577
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005578 // Only g:Func() can use a namespace.
5579 if (name_start[1] == ':' && !is_global)
5580 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005581 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005582 return NULL;
5583 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005584 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005585 return NULL;
5586
Bram Moolenaar04b12692020-05-04 23:24:44 +02005587 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005588 fill_exarg_from_cctx(eap, cctx);
5589
Bram Moolenaar04b12692020-05-04 23:24:44 +02005590 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005591 lambda_name = vim_strsave(get_lambda_name());
5592 if (lambda_name == NULL)
5593 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005594 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005595
Bram Moolenaar822ba242020-05-24 23:00:18 +02005596 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005597 {
5598 r = eap->skip ? OK : FAIL;
5599 goto theend;
5600 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005601
5602 // copy over the block scope IDs before compiling
5603 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5604 {
5605 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5606
5607 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5608 if (ufunc->uf_block_ids != NULL)
5609 {
5610 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5611 sizeof(int) * block_depth);
5612 ufunc->uf_block_depth = block_depth;
5613 }
5614 }
5615
Bram Moolenaare99d4222021-06-13 14:01:26 +02005616 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
5617 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005618 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005619 {
5620 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005621 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005622 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005623
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005624 if (is_global)
5625 {
5626 char_u *func_name = vim_strnsave(name_start + 2,
5627 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005628
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005629 if (func_name == NULL)
5630 r = FAIL;
5631 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005632 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005633 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005634 lambda_name = NULL;
5635 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005636 }
5637 else
5638 {
5639 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005640 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005641 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005642
Bram Moolenaareef21022020-08-01 22:16:43 +02005643 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005644 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005645 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005646 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005647 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5648 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005649 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005650
5651theend:
5652 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005653 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005654}
5655
5656/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657 * Return the length of an assignment operator, or zero if there isn't one.
5658 */
5659 int
5660assignment_len(char_u *p, int *heredoc)
5661{
5662 if (*p == '=')
5663 {
5664 if (p[1] == '<' && p[2] == '<')
5665 {
5666 *heredoc = TRUE;
5667 return 3;
5668 }
5669 return 1;
5670 }
5671 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5672 return 2;
5673 if (STRNCMP(p, "..=", 3) == 0)
5674 return 3;
5675 return 0;
5676}
5677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005678/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005679 * Generate the load instruction for "name".
5680 */
5681 static void
5682generate_loadvar(
5683 cctx_T *cctx,
5684 assign_dest_T dest,
5685 char_u *name,
5686 lvar_T *lvar,
5687 type_T *type)
5688{
5689 switch (dest)
5690 {
5691 case dest_option:
5692 // TODO: check the option exists
5693 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5694 break;
5695 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005696 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5697 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5698 else
5699 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005700 break;
5701 case dest_buffer:
5702 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5703 break;
5704 case dest_window:
5705 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5706 break;
5707 case dest_tab:
5708 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5709 break;
5710 case dest_script:
5711 compile_load_scriptvar(cctx,
5712 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5713 break;
5714 case dest_env:
5715 // Include $ in the name here
5716 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5717 break;
5718 case dest_reg:
5719 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5720 break;
5721 case dest_vimvar:
5722 generate_LOADV(cctx, name + 2, TRUE);
5723 break;
5724 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005725 if (lvar->lv_from_outer > 0)
5726 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5727 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005728 else
5729 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5730 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005731 case dest_expr:
5732 // list or dict value should already be on the stack.
5733 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005734 }
5735}
5736
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005737/*
5738 * Skip over "[expr]" or ".member".
5739 * Does not check for any errors.
5740 */
5741 static char_u *
5742skip_index(char_u *start)
5743{
5744 char_u *p = start;
5745
5746 if (*p == '[')
5747 {
5748 p = skipwhite(p + 1);
5749 (void)skip_expr(&p, NULL);
5750 p = skipwhite(p);
5751 if (*p == ']')
5752 return p + 1;
5753 return p;
5754 }
5755 // if (*p == '.')
5756 return to_name_end(p + 1, TRUE);
5757}
5758
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005759 void
5760vim9_declare_error(char_u *name)
5761{
5762 char *scope = "";
5763
5764 switch (*name)
5765 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005766 case 'g': scope = _("global"); break;
5767 case 'b': scope = _("buffer"); break;
5768 case 'w': scope = _("window"); break;
5769 case 't': scope = _("tab"); break;
5770 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005771 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005772 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005773 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005774 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005775 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005776 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005777 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005778 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005779 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005780}
5781
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005782/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005783 * For one assignment figure out the type of destination. Return it in "dest".
5784 * When not recognized "dest" is not set.
5785 * For an option "opt_flags" is set.
5786 * For a v:var "vimvaridx" is set.
5787 * "type" is set to the destination type if known, unchanted otherwise.
5788 * Return FAIL if an error message was given.
5789 */
5790 static int
5791get_var_dest(
5792 char_u *name,
5793 assign_dest_T *dest,
5794 int cmdidx,
5795 int *opt_flags,
5796 int *vimvaridx,
5797 type_T **type,
5798 cctx_T *cctx)
5799{
5800 char_u *p;
5801
5802 if (*name == '&')
5803 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005804 int cc;
5805 long numval;
5806 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005807
5808 *dest = dest_option;
5809 if (cmdidx == CMD_final || cmdidx == CMD_const)
5810 {
5811 emsg(_(e_const_option));
5812 return FAIL;
5813 }
5814 p = name;
5815 p = find_option_end(&p, opt_flags);
5816 if (p == NULL)
5817 {
5818 // cannot happen?
5819 emsg(_(e_letunexp));
5820 return FAIL;
5821 }
5822 cc = *p;
5823 *p = NUL;
5824 opt_type = get_option_value(skip_option_env_lead(name),
5825 &numval, NULL, *opt_flags);
5826 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005827 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005828 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005829 case gov_unknown:
5830 semsg(_(e_unknown_option), name);
5831 return FAIL;
5832 case gov_string:
5833 case gov_hidden_string:
5834 *type = &t_string;
5835 break;
5836 case gov_bool:
5837 case gov_hidden_bool:
5838 *type = &t_bool;
5839 break;
5840 case gov_number:
5841 case gov_hidden_number:
5842 *type = &t_number;
5843 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005844 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005845 }
5846 else if (*name == '$')
5847 {
5848 *dest = dest_env;
5849 *type = &t_string;
5850 }
5851 else if (*name == '@')
5852 {
5853 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5854 {
5855 emsg_invreg(name[1]);
5856 return FAIL;
5857 }
5858 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005859 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005860 }
5861 else if (STRNCMP(name, "g:", 2) == 0)
5862 {
5863 *dest = dest_global;
5864 }
5865 else if (STRNCMP(name, "b:", 2) == 0)
5866 {
5867 *dest = dest_buffer;
5868 }
5869 else if (STRNCMP(name, "w:", 2) == 0)
5870 {
5871 *dest = dest_window;
5872 }
5873 else if (STRNCMP(name, "t:", 2) == 0)
5874 {
5875 *dest = dest_tab;
5876 }
5877 else if (STRNCMP(name, "v:", 2) == 0)
5878 {
5879 typval_T *vtv;
5880 int di_flags;
5881
5882 *vimvaridx = find_vim_var(name + 2, &di_flags);
5883 if (*vimvaridx < 0)
5884 {
5885 semsg(_(e_variable_not_found_str), name);
5886 return FAIL;
5887 }
5888 // We use the current value of "sandbox" here, is that OK?
5889 if (var_check_ro(di_flags, name, FALSE))
5890 return FAIL;
5891 *dest = dest_vimvar;
5892 vtv = get_vim_var_tv(*vimvaridx);
5893 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5894 }
5895 return OK;
5896}
5897
5898/*
5899 * Generate a STORE instruction for "dest", not being "dest_local".
5900 * Return FAIL when out of memory.
5901 */
5902 static int
5903generate_store_var(
5904 cctx_T *cctx,
5905 assign_dest_T dest,
5906 int opt_flags,
5907 int vimvaridx,
5908 int scriptvar_idx,
5909 int scriptvar_sid,
5910 type_T *type,
5911 char_u *name)
5912{
5913 switch (dest)
5914 {
5915 case dest_option:
5916 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5917 opt_flags);
5918 case dest_global:
5919 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005920 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5921 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005922 case dest_buffer:
5923 // include b: with the name, easier to execute that way
5924 return generate_STORE(cctx, ISN_STOREB, 0, name);
5925 case dest_window:
5926 // include w: with the name, easier to execute that way
5927 return generate_STORE(cctx, ISN_STOREW, 0, name);
5928 case dest_tab:
5929 // include t: with the name, easier to execute that way
5930 return generate_STORE(cctx, ISN_STORET, 0, name);
5931 case dest_env:
5932 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5933 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005934 return generate_STORE(cctx, ISN_STOREREG,
5935 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005936 case dest_vimvar:
5937 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5938 case dest_script:
5939 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005940 // "s:" may be included in the name.
5941 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005942 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005943 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5944 scriptvar_sid, scriptvar_idx, type);
5945 case dest_local:
5946 case dest_expr:
5947 // cannot happen
5948 break;
5949 }
5950 return FAIL;
5951}
5952
Bram Moolenaar752fc692021-01-04 21:57:11 +01005953 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005954generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5955{
5956 if (lhs->lhs_dest != dest_local)
5957 return generate_store_var(cctx, lhs->lhs_dest,
5958 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5959 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5960 lhs->lhs_type, lhs->lhs_name);
5961
5962 if (lhs->lhs_lvar != NULL)
5963 {
5964 garray_T *instr = &cctx->ctx_instr;
5965 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5966
5967 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5968 // ISN_STORENR
5969 if (lhs->lhs_lvar->lv_from_outer == 0
5970 && instr->ga_len == instr_count + 1
5971 && isn->isn_type == ISN_PUSHNR)
5972 {
5973 varnumber_T val = isn->isn_arg.number;
5974 garray_T *stack = &cctx->ctx_type_stack;
5975
5976 isn->isn_type = ISN_STORENR;
5977 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5978 isn->isn_arg.storenr.stnr_val = val;
5979 if (stack->ga_len > 0)
5980 --stack->ga_len;
5981 }
5982 else if (lhs->lhs_lvar->lv_from_outer > 0)
5983 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5984 lhs->lhs_lvar->lv_from_outer);
5985 else
5986 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5987 }
5988 return OK;
5989}
5990
5991 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005992is_decl_command(int cmdidx)
5993{
5994 return cmdidx == CMD_let || cmdidx == CMD_var
5995 || cmdidx == CMD_final || cmdidx == CMD_const;
5996}
5997
5998/*
5999 * Figure out the LHS type and other properties for an assignment or one item
6000 * of ":unlet" with an index.
6001 * Returns OK or FAIL.
6002 */
6003 static int
6004compile_lhs(
6005 char_u *var_start,
6006 lhs_T *lhs,
6007 int cmdidx,
6008 int heredoc,
6009 int oplen,
6010 cctx_T *cctx)
6011{
6012 char_u *var_end;
6013 int is_decl = is_decl_command(cmdidx);
6014
6015 CLEAR_POINTER(lhs);
6016 lhs->lhs_dest = dest_local;
6017 lhs->lhs_vimvaridx = -1;
6018 lhs->lhs_scriptvar_idx = -1;
6019
6020 // "dest_end" is the end of the destination, including "[expr]" or
6021 // ".name".
6022 // "var_end" is the end of the variable/option/etc. name.
6023 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6024 if (*var_start == '@')
6025 var_end = var_start + 2;
6026 else
6027 {
6028 // skip over the leading "&", "&l:", "&g:" and "$"
6029 var_end = skip_option_env_lead(var_start);
6030 var_end = to_name_end(var_end, TRUE);
6031 }
6032
6033 // "a: type" is declaring variable "a" with a type, not dict "a:".
6034 if (is_decl && lhs->lhs_dest_end == var_start + 2
6035 && lhs->lhs_dest_end[-1] == ':')
6036 --lhs->lhs_dest_end;
6037 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6038 --var_end;
6039
6040 // compute the length of the destination without "[expr]" or ".name"
6041 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006042 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006043 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6044 if (lhs->lhs_name == NULL)
6045 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006046
6047 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6048 // Something follows after the variable: "var[idx]" or "var.key".
6049 lhs->lhs_has_index = TRUE;
6050
Bram Moolenaar752fc692021-01-04 21:57:11 +01006051 if (heredoc)
6052 lhs->lhs_type = &t_list_string;
6053 else
6054 lhs->lhs_type = &t_any;
6055
6056 if (cctx->ctx_skip != SKIP_YES)
6057 {
6058 int declare_error = FALSE;
6059
6060 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6061 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6062 &lhs->lhs_type, cctx) == FAIL)
6063 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006064 if (lhs->lhs_dest != dest_local
6065 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006066 {
6067 // Specific kind of variable recognized.
6068 declare_error = is_decl;
6069 }
6070 else
6071 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006072 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006073 if (check_reserved_name(lhs->lhs_name) == FAIL)
6074 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006075
6076 if (lookup_local(var_start, lhs->lhs_varlen,
6077 &lhs->lhs_local_lvar, cctx) == OK)
6078 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6079 else
6080 {
6081 CLEAR_FIELD(lhs->lhs_arg_lvar);
6082 if (arg_exists(var_start, lhs->lhs_varlen,
6083 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6084 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6085 {
6086 if (is_decl)
6087 {
6088 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6089 return FAIL;
6090 }
6091 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6092 }
6093 }
6094 if (lhs->lhs_lvar != NULL)
6095 {
6096 if (is_decl)
6097 {
6098 semsg(_(e_variable_already_declared), lhs->lhs_name);
6099 return FAIL;
6100 }
6101 }
6102 else
6103 {
6104 int script_namespace = lhs->lhs_varlen > 1
6105 && STRNCMP(var_start, "s:", 2) == 0;
6106 int script_var = (script_namespace
6107 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006108 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006109 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006110 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006111 imported_T *import =
6112 find_imported(var_start, lhs->lhs_varlen, cctx);
6113
6114 if (script_namespace || script_var || import != NULL)
6115 {
6116 char_u *rawname = lhs->lhs_name
6117 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6118
6119 if (is_decl)
6120 {
6121 if (script_namespace)
6122 semsg(_(e_cannot_declare_script_variable_in_function),
6123 lhs->lhs_name);
6124 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006125 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006126 lhs->lhs_name);
6127 return FAIL;
6128 }
6129 else if (cctx->ctx_ufunc->uf_script_ctx_version
6130 == SCRIPT_VERSION_VIM9
6131 && script_namespace
6132 && !script_var && import == NULL)
6133 {
6134 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6135 return FAIL;
6136 }
6137
6138 lhs->lhs_dest = dest_script;
6139
6140 // existing script-local variables should have a type
6141 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6142 if (import != NULL)
6143 lhs->lhs_scriptvar_sid = import->imp_sid;
6144 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6145 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006146 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006147 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006148 lhs->lhs_scriptvar_sid, rawname,
6149 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6150 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006151 if (lhs->lhs_scriptvar_idx >= 0)
6152 {
6153 scriptitem_T *si = SCRIPT_ITEM(
6154 lhs->lhs_scriptvar_sid);
6155 svar_T *sv =
6156 ((svar_T *)si->sn_var_vals.ga_data)
6157 + lhs->lhs_scriptvar_idx;
6158 lhs->lhs_type = sv->sv_type;
6159 }
6160 }
6161 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006162 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006163 == FAIL)
6164 return FAIL;
6165 }
6166 }
6167
6168 if (declare_error)
6169 {
6170 vim9_declare_error(lhs->lhs_name);
6171 return FAIL;
6172 }
6173 }
6174
6175 // handle "a:name" as a name, not index "name" on "a"
6176 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6177 var_end = lhs->lhs_dest_end;
6178
6179 if (lhs->lhs_dest != dest_option)
6180 {
6181 if (is_decl && *var_end == ':')
6182 {
6183 char_u *p;
6184
6185 // parse optional type: "let var: type = expr"
6186 if (!VIM_ISWHITE(var_end[1]))
6187 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006188 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006189 return FAIL;
6190 }
6191 p = skipwhite(var_end + 1);
6192 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6193 if (lhs->lhs_type == NULL)
6194 return FAIL;
6195 lhs->lhs_has_type = TRUE;
6196 }
6197 else if (lhs->lhs_lvar != NULL)
6198 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6199 }
6200
Bram Moolenaare42939a2021-04-05 17:11:17 +02006201 if (oplen == 3 && !heredoc
6202 && lhs->lhs_dest != dest_global
6203 && !lhs->lhs_has_index
6204 && lhs->lhs_type->tt_type != VAR_STRING
6205 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006206 {
6207 emsg(_(e_can_only_concatenate_to_string));
6208 return FAIL;
6209 }
6210
6211 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6212 && cctx->ctx_skip != SKIP_YES)
6213 {
6214 if (oplen > 1 && !heredoc)
6215 {
6216 // +=, /=, etc. require an existing variable
6217 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6218 return FAIL;
6219 }
6220 if (!is_decl)
6221 {
6222 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6223 return FAIL;
6224 }
6225
Bram Moolenaar3f327882021-03-17 20:56:38 +01006226 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006227 if ((lhs->lhs_type->tt_type == VAR_FUNC
6228 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006229 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006230 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006231
6232 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006233 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6234 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6235 if (lhs->lhs_lvar == NULL)
6236 return FAIL;
6237 lhs->lhs_new_local = TRUE;
6238 }
6239
6240 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006241 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006242 {
6243 // Something follows after the variable: "var[idx]" or "var.key".
6244 // TODO: should we also handle "->func()" here?
6245 if (is_decl)
6246 {
6247 emsg(_(e_cannot_use_index_when_declaring_variable));
6248 return FAIL;
6249 }
6250
6251 if (var_start[lhs->lhs_varlen] == '['
6252 || var_start[lhs->lhs_varlen] == '.')
6253 {
6254 char_u *after = var_start + lhs->lhs_varlen;
6255 char_u *p;
6256
6257 // Only the last index is used below, if there are others
6258 // before it generate code for the expression. Thus for
6259 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6260 for (;;)
6261 {
6262 p = skip_index(after);
6263 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006264 {
6265 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006266 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006267 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006268 after = p;
6269 }
6270 if (after > var_start + lhs->lhs_varlen)
6271 {
6272 lhs->lhs_varlen = after - var_start;
6273 lhs->lhs_dest = dest_expr;
6274 // We don't know the type before evaluating the expression,
6275 // use "any" until then.
6276 lhs->lhs_type = &t_any;
6277 }
6278
Bram Moolenaar752fc692021-01-04 21:57:11 +01006279 if (lhs->lhs_type->tt_member == NULL)
6280 lhs->lhs_member_type = &t_any;
6281 else
6282 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6283 }
6284 else
6285 {
6286 semsg("Not supported yet: %s", var_start);
6287 return FAIL;
6288 }
6289 }
6290 return OK;
6291}
6292
6293/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006294 * Figure out the LHS and check a few errors.
6295 */
6296 static int
6297compile_assign_lhs(
6298 char_u *var_start,
6299 lhs_T *lhs,
6300 int cmdidx,
6301 int is_decl,
6302 int heredoc,
6303 int oplen,
6304 cctx_T *cctx)
6305{
6306 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6307 return FAIL;
6308
6309 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6310 {
6311 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6312 return FAIL;
6313 }
6314 if (!is_decl && lhs->lhs_lvar != NULL
6315 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6316 {
6317 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6318 return FAIL;
6319 }
6320 return OK;
6321}
6322
6323/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006324 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6325 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006326 */
6327 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006328compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006329 char_u *var_start,
6330 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006331 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006332 cctx_T *cctx)
6333{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006334 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006335 char_u *p;
6336 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006337 int need_white_before = TRUE;
6338 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006339
Bram Moolenaar752fc692021-01-04 21:57:11 +01006340 p = var_start + varlen;
6341 if (*p == '[')
6342 {
6343 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006344 if (*p == ':')
6345 {
6346 // empty first index, push zero
6347 r = generate_PUSHNR(cctx, 0);
6348 need_white_before = FALSE;
6349 }
6350 else
6351 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006352
6353 if (r == OK && *skipwhite(p) == ':')
6354 {
6355 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006356 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006357 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006358 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006359 empty_second = *skipwhite(p + 1) == ']';
6360 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6361 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006362 {
6363 semsg(_(e_white_space_required_before_and_after_str_at_str),
6364 ":", p);
6365 return FAIL;
6366 }
6367 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006368 if (*p == ']')
6369 // empty second index, push "none"
6370 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6371 else
6372 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006373 }
6374
Bram Moolenaar752fc692021-01-04 21:57:11 +01006375 if (r == OK && *skipwhite(p) != ']')
6376 {
6377 // this should not happen
6378 emsg(_(e_missbrac));
6379 r = FAIL;
6380 }
6381 }
6382 else // if (*p == '.')
6383 {
6384 char_u *key_end = to_name_end(p + 1, TRUE);
6385 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6386
6387 r = generate_PUSHS(cctx, key);
6388 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006389 return r;
6390}
6391
6392/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006393 * For a LHS with an index, load the variable to be indexed.
6394 */
6395 static int
6396compile_load_lhs(
6397 lhs_T *lhs,
6398 char_u *var_start,
6399 type_T *rhs_type,
6400 cctx_T *cctx)
6401{
6402 if (lhs->lhs_dest == dest_expr)
6403 {
6404 size_t varlen = lhs->lhs_varlen;
6405 int c = var_start[varlen];
6406 char_u *p = var_start;
6407 garray_T *stack = &cctx->ctx_type_stack;
6408
6409 // Evaluate "ll[expr]" of "ll[expr][idx]"
6410 var_start[varlen] = NUL;
6411 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6412 {
6413 // this should not happen
6414 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006415 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006416 return FAIL;
6417 }
6418 var_start[varlen] = c;
6419
6420 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6421 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6422 // now we can properly check the type
6423 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6424 && rhs_type != &t_void
6425 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6426 FALSE, FALSE) == FAIL)
6427 return FAIL;
6428 }
6429 else
6430 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6431 lhs->lhs_lvar, lhs->lhs_type);
6432 return OK;
6433}
6434
6435/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006436 * Produce code for loading "lhs" and also take care of an index.
6437 * Return OK/FAIL.
6438 */
6439 static int
6440compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6441{
6442 compile_load_lhs(lhs, var_start, NULL, cctx);
6443
6444 if (lhs->lhs_has_index)
6445 {
6446 int range = FALSE;
6447
6448 // Get member from list or dict. First compile the
6449 // index value.
6450 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6451 return FAIL;
6452 if (range)
6453 {
6454 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6455 var_start);
6456 return FAIL;
6457 }
6458
6459 // Get the member.
6460 if (compile_member(FALSE, cctx) == FAIL)
6461 return FAIL;
6462 }
6463 return OK;
6464}
6465
6466/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006467 * Assignment to a list or dict member, or ":unlet" for the item, using the
6468 * information in "lhs".
6469 * Returns OK or FAIL.
6470 */
6471 static int
6472compile_assign_unlet(
6473 char_u *var_start,
6474 lhs_T *lhs,
6475 int is_assign,
6476 type_T *rhs_type,
6477 cctx_T *cctx)
6478{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006479 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006480 garray_T *stack = &cctx->ctx_type_stack;
6481 int range = FALSE;
6482
Bram Moolenaar68452172021-04-12 21:21:02 +02006483 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006484 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006485 if (is_assign && range && lhs->lhs_type != &t_blob
6486 && lhs->lhs_type != &t_any)
6487 {
6488 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6489 return FAIL;
6490 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006491
6492 if (lhs->lhs_type == &t_any)
6493 {
6494 // Index on variable of unknown type: check at runtime.
6495 dest_type = VAR_ANY;
6496 }
6497 else
6498 {
6499 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006500 if (dest_type == VAR_DICT && range)
6501 {
6502 emsg(e_cannot_use_range_with_dictionary);
6503 return FAIL;
6504 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006505 if (dest_type == VAR_DICT
6506 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006507 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006508 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006509 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006510 type_T *type;
6511
6512 if (range)
6513 {
6514 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6515 if (need_type(type, &t_number,
6516 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006517 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006518 }
6519 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6520 if ((dest_type != VAR_BLOB || type != &t_special)
6521 && need_type(type, &t_number,
6522 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006523 return FAIL;
6524 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006525 }
6526
6527 // Load the dict or list. On the stack we then have:
6528 // - value (for assignment, not for :unlet)
6529 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006530 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006531 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006532 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6533 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006534
Bram Moolenaar68452172021-04-12 21:21:02 +02006535 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6536 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006537 {
6538 if (is_assign)
6539 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006540 if (range)
6541 {
6542 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6543 return FAIL;
6544 }
6545 else
6546 {
6547 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006548
Bram Moolenaar68452172021-04-12 21:21:02 +02006549 if (isn == NULL)
6550 return FAIL;
6551 isn->isn_arg.vartype = dest_type;
6552 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006553 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006554 else if (range)
6555 {
6556 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6557 return FAIL;
6558 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006559 else
6560 {
6561 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6562 return FAIL;
6563 }
6564 }
6565 else
6566 {
6567 emsg(_(e_indexable_type_required));
6568 return FAIL;
6569 }
6570
6571 return OK;
6572}
6573
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006574/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006575 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006576 * "let name"
6577 * "var name = expr"
6578 * "final name = expr"
6579 * "const name = expr"
6580 * "name = expr"
6581 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006582 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006583 * Return NULL for an error.
6584 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006585 */
6586 static char_u *
6587compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6588{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006589 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006591 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592 char_u *ret = NULL;
6593 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006594 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006597 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 int oplen = 0;
6600 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006601 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006602 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006604 int is_decl = is_decl_command(cmdidx);
6605 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006606 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006608 // Skip over the "var" or "[var, var]" to get to any "=".
6609 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6610 if (p == NULL)
6611 return *arg == '[' ? arg : NULL;
6612
6613 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006614 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006615 // TODO: should we allow this, and figure out type inference from list
6616 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006617 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006618 return NULL;
6619 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006620 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 sp = p;
6623 p = skipwhite(p);
6624 op = p;
6625 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006626
6627 if (var_count > 0 && oplen == 0)
6628 // can be something like "[1, 2]->func()"
6629 return arg;
6630
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006631 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006633 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006634 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006635 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006636 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6637 {
6638 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6639 oplen = 2;
6640 incdec = TRUE;
6641 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 if (heredoc)
6644 {
6645 list_T *l;
6646 listitem_T *li;
6647
6648 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006649 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006650 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006651 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006652 if (l == NULL)
6653 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654
Bram Moolenaar078269b2020-09-21 20:35:55 +02006655 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006657 // Push each line and the create the list.
6658 FOR_ALL_LIST_ITEMS(l, li)
6659 {
6660 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6661 li->li_tv.vval.v_string = NULL;
6662 }
6663 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 list_free(l);
6666 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006667 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006668 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006669 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006670 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006671 char_u *wp;
6672
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006673 // for "[var, var] = expr" evaluate the expression here, loop over the
6674 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006675 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006676
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006677 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006678 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006679 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006680 if (compile_expr0(&p, cctx) == FAIL)
6681 return NULL;
6682 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006684 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006686 type_T *stacktype;
6687
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006688 stacktype = stack->ga_len == 0 ? &t_void
6689 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006690 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006691 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006692 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006693 goto theend;
6694 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006695 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006696 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006697 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006698 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006699 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6700 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006701 if (stacktype->tt_member != NULL)
6702 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006703 }
6704 }
6705
6706 /*
6707 * Loop over variables in "[var, var] = expr".
6708 * For "var = expr" and "let var: type" this is done only once.
6709 */
6710 if (var_count > 0)
6711 var_start = skipwhite(arg + 1); // skip over the "["
6712 else
6713 var_start = arg;
6714 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6715 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006716 int instr_count = -1;
6717
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006718 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6719 {
6720 // Ignore underscore in "[a, _, b] = list".
6721 if (var_count > 0)
6722 {
6723 var_start = skipwhite(var_start + 2);
6724 continue;
6725 }
6726 emsg(_(e_cannot_use_underscore_here));
6727 goto theend;
6728 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006729 vim_free(lhs.lhs_name);
6730
6731 /*
6732 * Figure out the LHS type and other properties.
6733 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006734 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6735 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006736 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006737 if (!heredoc)
6738 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006739 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006740 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006741 if (oplen > 0 && var_count == 0)
6742 {
6743 // skip over the "=" and the expression
6744 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006745 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006746 }
6747 }
6748 else if (oplen > 0)
6749 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006750 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006751 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006752
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006753 // for "+=", "*=", "..=" etc. first load the current value
6754 if (*op != '='
6755 && compile_load_lhs_with_index(&lhs, var_start,
6756 cctx) == FAIL)
6757 goto theend;
6758
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006759 // For "var = expr" evaluate the expression.
6760 if (var_count == 0)
6761 {
6762 int r;
6763
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006764 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006765 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006766 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006767 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006768 r = generate_PUSHNR(cctx, 1);
6769 }
6770 else
6771 {
6772 // Temporarily hide the new local variable here, it is
6773 // not available to this expression.
6774 if (lhs.lhs_new_local)
6775 --cctx->ctx_locals.ga_len;
6776 wp = op + oplen;
6777 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6778 {
6779 if (lhs.lhs_new_local)
6780 ++cctx->ctx_locals.ga_len;
6781 goto theend;
6782 }
6783 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006784 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006785 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006786 if (r == FAIL)
6787 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006788 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006789 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006790 else if (semicolon && var_idx == var_count - 1)
6791 {
6792 // For "[var; var] = expr" get the rest of the list
6793 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6794 goto theend;
6795 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006796 else
6797 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006798 // For "[var, var] = expr" get the "var_idx" item from the
6799 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006800 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006801 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006802 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006803
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006804 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006805 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006806 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006807 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006808 if ((rhs_type->tt_type == VAR_FUNC
6809 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006810 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006811 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006812 goto theend;
6813
Bram Moolenaar752fc692021-01-04 21:57:11 +01006814 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006815 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006816 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006817 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006818 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006819 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006820 }
6821 else
6822 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006823 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006824 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006825 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006826 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006827 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006828 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006829 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006830 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006831 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006832 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006833 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006834 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006835 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006836 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006837 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006838
Bram Moolenaar77709b12021-04-03 21:01:01 +02006839 // Without operator check type here, otherwise below.
6840 // Use the line number of the assignment.
6841 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006842 if (lhs.lhs_has_index)
6843 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006844 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006845 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006846 goto theend;
6847 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006848 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006849 else
6850 {
6851 type_T *lhs_type = lhs.lhs_member_type;
6852
6853 // Special case: assigning to @# can use a number or a
6854 // string.
6855 if (lhs_type == &t_number_or_string
6856 && rhs_type->tt_type == VAR_NUMBER)
6857 lhs_type = &t_number;
6858 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006859 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006860 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006861 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006862 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006863 else if (cmdidx == CMD_final)
6864 {
6865 emsg(_(e_final_requires_a_value));
6866 goto theend;
6867 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006868 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006870 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006871 goto theend;
6872 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006873 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006874 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006875 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006876 goto theend;
6877 }
6878 else
6879 {
6880 // variables are always initialized
6881 if (ga_grow(instr, 1) == FAIL)
6882 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006883 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006884 {
6885 case VAR_BOOL:
6886 generate_PUSHBOOL(cctx, VVAL_FALSE);
6887 break;
6888 case VAR_FLOAT:
6889#ifdef FEAT_FLOAT
6890 generate_PUSHF(cctx, 0.0);
6891#endif
6892 break;
6893 case VAR_STRING:
6894 generate_PUSHS(cctx, NULL);
6895 break;
6896 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006897 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006898 break;
6899 case VAR_FUNC:
6900 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6901 break;
6902 case VAR_LIST:
6903 generate_NEWLIST(cctx, 0);
6904 break;
6905 case VAR_DICT:
6906 generate_NEWDICT(cctx, 0);
6907 break;
6908 case VAR_JOB:
6909 generate_PUSHJOB(cctx, NULL);
6910 break;
6911 case VAR_CHANNEL:
6912 generate_PUSHCHANNEL(cctx, NULL);
6913 break;
6914 case VAR_NUMBER:
6915 case VAR_UNKNOWN:
6916 case VAR_ANY:
6917 case VAR_PARTIAL:
6918 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006919 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006920 case VAR_SPECIAL: // cannot happen
6921 generate_PUSHNR(cctx, 0);
6922 break;
6923 }
6924 }
6925 if (var_count == 0)
6926 end = p;
6927 }
6928
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006929 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006930 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006931 break;
6932
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006933 if (oplen > 0 && *op != '=')
6934 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006935 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006936 type_T *stacktype;
6937
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006938 if (*op == '.')
6939 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006940 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006941 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006942 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006943 if (
6944#ifdef FEAT_FLOAT
6945 // If variable is float operation with number is OK.
6946 !(expected == &t_float && stacktype == &t_number) &&
6947#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006948 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006949 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006950 goto theend;
6951
6952 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006953 {
6954 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6955 goto theend;
6956 }
6957 else if (*op == '+')
6958 {
6959 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006960 operator_type(lhs.lhs_member_type, stacktype),
6961 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006962 goto theend;
6963 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006964 else if (generate_two_op(cctx, op) == FAIL)
6965 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967
Bram Moolenaar752fc692021-01-04 21:57:11 +01006968 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006969 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006970 // Use the info in "lhs" to store the value at the index in the
6971 // list or dict.
6972 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6973 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006974 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006975 }
6976 else
6977 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006978 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006979 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006980 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006981 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006982 generate_LOCKCONST(cctx);
6983
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006984 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006985 && (lhs.lhs_type->tt_type == VAR_DICT
6986 || lhs.lhs_type->tt_type == VAR_LIST)
6987 && lhs.lhs_type->tt_member != NULL
6988 && lhs.lhs_type->tt_member != &t_any
6989 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006990 // Set the type in the list or dict, so that it can be checked,
6991 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006992 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006993
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006994 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6995 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006996 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006997
6998 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006999 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007001
7002 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02007003 if (var_count > 0 && !semicolon)
7004 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007005 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007006 goto theend;
7007 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007008
Bram Moolenaarb2097502020-07-19 17:17:02 +02007009 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010
7011theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007012 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007013 return ret;
7014}
7015
7016/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007017 * Check for an assignment at "eap->cmd", compile it if found.
7018 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7019 */
7020 static int
7021may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7022{
7023 char_u *pskip;
7024 char_u *p;
7025
7026 // Assuming the command starts with a variable or function name,
7027 // find what follows.
7028 // Skip over "var.member", "var[idx]" and the like.
7029 // Also "&opt = val", "$ENV = val" and "@r = val".
7030 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7031 ? eap->cmd + 1 : eap->cmd;
7032 p = to_name_end(pskip, TRUE);
7033 if (p > eap->cmd && *p != NUL)
7034 {
7035 char_u *var_end;
7036 int oplen;
7037 int heredoc;
7038
7039 if (eap->cmd[0] == '@')
7040 var_end = eap->cmd + 2;
7041 else
7042 var_end = find_name_end(pskip, NULL, NULL,
7043 FNE_CHECK_START | FNE_INCL_BR);
7044 oplen = assignment_len(skipwhite(var_end), &heredoc);
7045 if (oplen > 0)
7046 {
7047 size_t len = p - eap->cmd;
7048
7049 // Recognize an assignment if we recognize the variable
7050 // name:
7051 // "g:var = expr"
7052 // "local = expr" where "local" is a local var.
7053 // "script = expr" where "script" is a script-local var.
7054 // "import = expr" where "import" is an imported var
7055 // "&opt = expr"
7056 // "$ENV = expr"
7057 // "@r = expr"
7058 if (*eap->cmd == '&'
7059 || *eap->cmd == '$'
7060 || *eap->cmd == '@'
7061 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007062 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007063 {
7064 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7065 if (*line == NULL || *line == eap->cmd)
7066 return FAIL;
7067 return OK;
7068 }
7069 }
7070 }
7071
7072 if (*eap->cmd == '[')
7073 {
7074 // [var, var] = expr
7075 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7076 if (*line == NULL)
7077 return FAIL;
7078 if (*line != eap->cmd)
7079 return OK;
7080 }
7081 return NOTDONE;
7082}
7083
7084/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007085 * Check if "name" can be "unlet".
7086 */
7087 int
7088check_vim9_unlet(char_u *name)
7089{
7090 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7091 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007092 // "unlet s:var" is allowed in legacy script.
7093 if (*name == 's' && !script_is_vim9())
7094 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007095 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007096 return FAIL;
7097 }
7098 return OK;
7099}
7100
7101/*
7102 * Callback passed to ex_unletlock().
7103 */
7104 static int
7105compile_unlet(
7106 lval_T *lvp,
7107 char_u *name_end,
7108 exarg_T *eap,
7109 int deep UNUSED,
7110 void *coookie)
7111{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007112 cctx_T *cctx = coookie;
7113 char_u *p = lvp->ll_name;
7114 int cc = *name_end;
7115 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007116
Bram Moolenaar752fc692021-01-04 21:57:11 +01007117 if (cctx->ctx_skip == SKIP_YES)
7118 return OK;
7119
7120 *name_end = NUL;
7121 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007122 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007123 // :unlet $ENV_VAR
7124 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7125 }
7126 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7127 {
7128 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007129
Bram Moolenaar752fc692021-01-04 21:57:11 +01007130 // This is similar to assigning: lookup the list/dict, compile the
7131 // idx/key. Then instead of storing the value unlet the item.
7132 // unlet {list}[idx]
7133 // unlet {dict}[key] dict.key
7134 //
7135 // Figure out the LHS type and other properties.
7136 //
7137 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7138
7139 // : unlet an indexed item
7140 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007141 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007142 iemsg("called compile_lhs() without an index");
7143 ret = FAIL;
7144 }
7145 else
7146 {
7147 // Use the info in "lhs" to unlet the item at the index in the
7148 // list or dict.
7149 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007150 }
7151
Bram Moolenaar752fc692021-01-04 21:57:11 +01007152 vim_free(lhs.lhs_name);
7153 }
7154 else if (check_vim9_unlet(p) == FAIL)
7155 {
7156 ret = FAIL;
7157 }
7158 else
7159 {
7160 // Normal name. Only supports g:, w:, t: and b: namespaces.
7161 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007162 }
7163
Bram Moolenaar752fc692021-01-04 21:57:11 +01007164 *name_end = cc;
7165 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007166}
7167
7168/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007169 * Callback passed to ex_unletlock().
7170 */
7171 static int
7172compile_lock_unlock(
7173 lval_T *lvp,
7174 char_u *name_end,
7175 exarg_T *eap,
7176 int deep UNUSED,
7177 void *coookie)
7178{
7179 cctx_T *cctx = coookie;
7180 int cc = *name_end;
7181 char_u *p = lvp->ll_name;
7182 int ret = OK;
7183 size_t len;
7184 char_u *buf;
7185
7186 if (cctx->ctx_skip == SKIP_YES)
7187 return OK;
7188
7189 // Cannot use :lockvar and :unlockvar on local variables.
7190 if (p[1] != ':')
7191 {
7192 char_u *end = skip_var_one(p, FALSE);
7193
7194 if (lookup_local(p, end - p, NULL, cctx) == OK)
7195 {
7196 emsg(_(e_cannot_lock_unlock_local_variable));
7197 return FAIL;
7198 }
7199 }
7200
7201 // Checking is done at runtime.
7202 *name_end = NUL;
7203 len = name_end - p + 20;
7204 buf = alloc(len);
7205 if (buf == NULL)
7206 ret = FAIL;
7207 else
7208 {
7209 vim_snprintf((char *)buf, len, "%s %s",
7210 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7211 p);
7212 ret = generate_EXEC(cctx, buf);
7213
7214 vim_free(buf);
7215 *name_end = cc;
7216 }
7217 return ret;
7218}
7219
7220/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007221 * compile "unlet var", "lock var" and "unlock var"
7222 * "arg" points to "var".
7223 */
7224 static char_u *
7225compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7226{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007227 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7228 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7229 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007230 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7231}
7232
7233/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234 * Compile an :import command.
7235 */
7236 static char_u *
7237compile_import(char_u *arg, cctx_T *cctx)
7238{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007239 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007240}
7241
7242/*
7243 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7244 */
7245 static int
7246compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7247{
7248 garray_T *instr = &cctx->ctx_instr;
7249 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7250
7251 if (endlabel == NULL)
7252 return FAIL;
7253 endlabel->el_next = *el;
7254 *el = endlabel;
7255 endlabel->el_end_label = instr->ga_len;
7256
7257 generate_JUMP(cctx, when, 0);
7258 return OK;
7259}
7260
7261 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007262compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263{
7264 garray_T *instr = &cctx->ctx_instr;
7265
7266 while (*el != NULL)
7267 {
7268 endlabel_T *cur = (*el);
7269 isn_T *isn;
7270
7271 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007272 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273 *el = cur->el_next;
7274 vim_free(cur);
7275 }
7276}
7277
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007278 static void
7279compile_free_jump_to_end(endlabel_T **el)
7280{
7281 while (*el != NULL)
7282 {
7283 endlabel_T *cur = (*el);
7284
7285 *el = cur->el_next;
7286 vim_free(cur);
7287 }
7288}
7289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290/*
7291 * Create a new scope and set up the generic items.
7292 */
7293 static scope_T *
7294new_scope(cctx_T *cctx, scopetype_T type)
7295{
7296 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7297
7298 if (scope == NULL)
7299 return NULL;
7300 scope->se_outer = cctx->ctx_scope;
7301 cctx->ctx_scope = scope;
7302 scope->se_type = type;
7303 scope->se_local_count = cctx->ctx_locals.ga_len;
7304 return scope;
7305}
7306
7307/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007308 * Free the current scope and go back to the outer scope.
7309 */
7310 static void
7311drop_scope(cctx_T *cctx)
7312{
7313 scope_T *scope = cctx->ctx_scope;
7314
7315 if (scope == NULL)
7316 {
7317 iemsg("calling drop_scope() without a scope");
7318 return;
7319 }
7320 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007321 switch (scope->se_type)
7322 {
7323 case IF_SCOPE:
7324 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7325 case FOR_SCOPE:
7326 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7327 case WHILE_SCOPE:
7328 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7329 case TRY_SCOPE:
7330 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7331 case NO_SCOPE:
7332 case BLOCK_SCOPE:
7333 break;
7334 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007335 vim_free(scope);
7336}
7337
7338/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007339 * compile "if expr"
7340 *
7341 * "if expr" Produces instructions:
7342 * EVAL expr Push result of "expr"
7343 * JUMP_IF_FALSE end
7344 * ... body ...
7345 * end:
7346 *
7347 * "if expr | else" Produces instructions:
7348 * EVAL expr Push result of "expr"
7349 * JUMP_IF_FALSE else
7350 * ... body ...
7351 * JUMP_ALWAYS end
7352 * else:
7353 * ... body ...
7354 * end:
7355 *
7356 * "if expr1 | elseif expr2 | else" Produces instructions:
7357 * EVAL expr Push result of "expr"
7358 * JUMP_IF_FALSE elseif
7359 * ... body ...
7360 * JUMP_ALWAYS end
7361 * elseif:
7362 * EVAL expr Push result of "expr"
7363 * JUMP_IF_FALSE else
7364 * ... body ...
7365 * JUMP_ALWAYS end
7366 * else:
7367 * ... body ...
7368 * end:
7369 */
7370 static char_u *
7371compile_if(char_u *arg, cctx_T *cctx)
7372{
7373 char_u *p = arg;
7374 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007375 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007377 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007378 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379
Bram Moolenaara5565e42020-05-09 15:44:01 +02007380 CLEAR_FIELD(ppconst);
7381 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007382 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007383 clear_ppconst(&ppconst);
7384 return NULL;
7385 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007386 if (!ends_excmd2(arg, skipwhite(p)))
7387 {
7388 semsg(_(e_trailing_arg), p);
7389 return NULL;
7390 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007391 if (cctx->ctx_skip == SKIP_YES)
7392 clear_ppconst(&ppconst);
7393 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007394 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007395 int error = FALSE;
7396 int v;
7397
Bram Moolenaara5565e42020-05-09 15:44:01 +02007398 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007399 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007400 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007401 if (error)
7402 return NULL;
7403 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007404 }
7405 else
7406 {
7407 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007408 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007409 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007410 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007411 if (bool_on_stack(cctx) == FAIL)
7412 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007413 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414
Bram Moolenaara91a7132021-03-25 21:12:15 +01007415 // CMDMOD_REV must come before the jump
7416 generate_undo_cmdmods(cctx);
7417
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007418 scope = new_scope(cctx, IF_SCOPE);
7419 if (scope == NULL)
7420 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007421 scope->se_skip_save = skip_save;
7422 // "is_had_return" will be reset if any block does not end in :return
7423 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007425 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007426 {
7427 // "where" is set when ":elseif", "else" or ":endif" is found
7428 scope->se_u.se_if.is_if_label = instr->ga_len;
7429 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7430 }
7431 else
7432 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433
Bram Moolenaarced68a02021-01-24 17:53:47 +01007434#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007435 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007436 && skip_save != SKIP_YES)
7437 {
7438 // generated a profile start, need to generate a profile end, since it
7439 // won't be done after returning
7440 cctx->ctx_skip = SKIP_NOT;
7441 generate_instr(cctx, ISN_PROF_END);
7442 cctx->ctx_skip = SKIP_YES;
7443 }
7444#endif
7445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446 return p;
7447}
7448
7449 static char_u *
7450compile_elseif(char_u *arg, cctx_T *cctx)
7451{
7452 char_u *p = arg;
7453 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007454 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455 isn_T *isn;
7456 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007457 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007458 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459
7460 if (scope == NULL || scope->se_type != IF_SCOPE)
7461 {
7462 emsg(_(e_elseif_without_if));
7463 return NULL;
7464 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007465 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007466 if (!cctx->ctx_had_return)
7467 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468
Bram Moolenaarced68a02021-01-24 17:53:47 +01007469 if (cctx->ctx_skip == SKIP_NOT)
7470 {
7471 // previous block was executed, this one and following will not
7472 cctx->ctx_skip = SKIP_YES;
7473 scope->se_u.se_if.is_seen_skip_not = TRUE;
7474 }
7475 if (scope->se_u.se_if.is_seen_skip_not)
7476 {
7477 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007478 // Do not count the "elseif" for profiling and cmdmod
7479 instr->ga_len = current_instr_idx(cctx);
7480
Bram Moolenaarced68a02021-01-24 17:53:47 +01007481 skip_expr_cctx(&p, cctx);
7482 return p;
7483 }
7484
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007485 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007486 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007487 int moved_cmdmod = FALSE;
7488
7489 // Move any CMDMOD instruction to after the jump
7490 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7491 {
7492 if (ga_grow(instr, 1) == FAIL)
7493 return NULL;
7494 ((isn_T *)instr->ga_data)[instr->ga_len] =
7495 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7496 --instr->ga_len;
7497 moved_cmdmod = TRUE;
7498 }
7499
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007500 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007501 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007502 return NULL;
7503 // previous "if" or "elseif" jumps here
7504 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7505 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007506 if (moved_cmdmod)
7507 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007510 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007511 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007512 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007513 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007514 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007515#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007516 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007517 {
7518 // the previous block was skipped, need to profile this line
7519 generate_instr(cctx, ISN_PROF_START);
7520 instr_count = instr->ga_len;
7521 }
7522#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007523 if (cctx->ctx_compile_type == CT_DEBUG)
7524 {
7525 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007526 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007527 instr_count = instr->ga_len;
7528 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007529 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007530 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007531 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007532 clear_ppconst(&ppconst);
7533 return NULL;
7534 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007535 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007536 if (!ends_excmd2(arg, skipwhite(p)))
7537 {
7538 semsg(_(e_trailing_arg), p);
7539 return NULL;
7540 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007541 if (scope->se_skip_save == SKIP_YES)
7542 clear_ppconst(&ppconst);
7543 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007544 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007545 int error = FALSE;
7546 int v;
7547
Bram Moolenaar7f141552020-05-09 17:35:53 +02007548 // The expression results in a constant.
7549 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007550 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7551 if (error)
7552 return NULL;
7553 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007554 clear_ppconst(&ppconst);
7555 scope->se_u.se_if.is_if_label = -1;
7556 }
7557 else
7558 {
7559 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007560 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007561 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007562 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007563 if (bool_on_stack(cctx) == FAIL)
7564 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565
Bram Moolenaara91a7132021-03-25 21:12:15 +01007566 // CMDMOD_REV must come before the jump
7567 generate_undo_cmdmods(cctx);
7568
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007569 // "where" is set when ":elseif", "else" or ":endif" is found
7570 scope->se_u.se_if.is_if_label = instr->ga_len;
7571 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7572 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573
7574 return p;
7575}
7576
7577 static char_u *
7578compile_else(char_u *arg, cctx_T *cctx)
7579{
7580 char_u *p = arg;
7581 garray_T *instr = &cctx->ctx_instr;
7582 isn_T *isn;
7583 scope_T *scope = cctx->ctx_scope;
7584
7585 if (scope == NULL || scope->se_type != IF_SCOPE)
7586 {
7587 emsg(_(e_else_without_if));
7588 return NULL;
7589 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007590 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007591 if (!cctx->ctx_had_return)
7592 scope->se_u.se_if.is_had_return = FALSE;
7593 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594
Bram Moolenaarced68a02021-01-24 17:53:47 +01007595#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007596 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007597 {
7598 if (cctx->ctx_skip == SKIP_NOT
7599 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7600 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007601 // the previous block was executed, do not count "else" for
7602 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007603 --instr->ga_len;
7604 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7605 {
7606 // the previous block was not executed, this one will, do count the
7607 // "else" for profiling
7608 cctx->ctx_skip = SKIP_NOT;
7609 generate_instr(cctx, ISN_PROF_END);
7610 generate_instr(cctx, ISN_PROF_START);
7611 cctx->ctx_skip = SKIP_YES;
7612 }
7613 }
7614#endif
7615
7616 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007617 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007618 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007619 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007620 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007621 if (!cctx->ctx_had_return
7622 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7623 JUMP_ALWAYS, cctx) == FAIL)
7624 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007625 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007626
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007627 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007628 {
7629 if (scope->se_u.se_if.is_if_label >= 0)
7630 {
7631 // previous "if" or "elseif" jumps here
7632 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7633 isn->isn_arg.jump.jump_where = instr->ga_len;
7634 scope->se_u.se_if.is_if_label = -1;
7635 }
7636 }
7637
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007638 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007639 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7640 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007641
7642 return p;
7643}
7644
7645 static char_u *
7646compile_endif(char_u *arg, cctx_T *cctx)
7647{
7648 scope_T *scope = cctx->ctx_scope;
7649 ifscope_T *ifscope;
7650 garray_T *instr = &cctx->ctx_instr;
7651 isn_T *isn;
7652
Bram Moolenaarfa984412021-03-25 22:15:28 +01007653 if (misplaced_cmdmod(cctx))
7654 return NULL;
7655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007656 if (scope == NULL || scope->se_type != IF_SCOPE)
7657 {
7658 emsg(_(e_endif_without_if));
7659 return NULL;
7660 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007661 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007662 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007663 if (!cctx->ctx_had_return)
7664 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007665
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007666 if (scope->se_u.se_if.is_if_label >= 0)
7667 {
7668 // previous "if" or "elseif" jumps here
7669 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7670 isn->isn_arg.jump.jump_where = instr->ga_len;
7671 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007672 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007673 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007674
7675#ifdef FEAT_PROFILE
7676 // even when skipping we count the endif as executed, unless the block it's
7677 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007678 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007679 && scope->se_skip_save != SKIP_YES)
7680 {
7681 cctx->ctx_skip = SKIP_NOT;
7682 generate_instr(cctx, ISN_PROF_START);
7683 }
7684#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007685 cctx->ctx_skip = scope->se_skip_save;
7686
7687 // If all the blocks end in :return and there is an :else then the
7688 // had_return flag is set.
7689 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007690
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007691 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007692 return arg;
7693}
7694
7695/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007696 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007697 *
7698 * Produces instructions:
7699 * PUSHNR -1
7700 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007701 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007702 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7703 * - if beyond end, jump to "end"
7704 * - otherwise get item from list and push it
7705 * STORE var Store item in "var"
7706 * ... body ...
7707 * JUMP top Jump back to repeat
7708 * end: DROP Drop the result of "expr"
7709 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007710 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7711 * UNPACK 2 Split item in 2
7712 * STORE var1 Store item in "var1"
7713 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 */
7715 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007716compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007718 char_u *arg;
7719 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007720 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007721 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007722 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007723 int var_count = 0;
7724 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007726 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007727 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007728 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007729 lvar_T *loop_lvar; // loop iteration variable
7730 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007732 type_T *item_type = &t_any;
7733 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007734
Bram Moolenaar792f7862020-11-23 08:31:18 +01007735 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007736 if (p == NULL)
7737 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007738 if (var_count == 0)
7739 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740
7741 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007742 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007743 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7744 return NULL;
7745 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007746 {
7747 emsg(_(e_missing_in));
7748 return NULL;
7749 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007750 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007751 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7752 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007754 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7755 // instruction.
7756 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7757 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7758 .isn_type == ISN_DEBUG)
7759 --instr->ga_len;
7760
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007761 scope = new_scope(cctx, FOR_SCOPE);
7762 if (scope == NULL)
7763 return NULL;
7764
Bram Moolenaar792f7862020-11-23 08:31:18 +01007765 // Reserve a variable to store the loop iteration counter and initialize it
7766 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007767 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7768 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007769 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007770 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007771 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007772 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007773 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007774 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007775
7776 // compile "expr", it remains on the stack until "endfor"
7777 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007778 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007779 {
7780 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007781 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007782 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007783 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007784
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007785 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007786 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007787 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007788 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007789 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007791 semsg(_(e_for_loop_on_str_not_supported),
7792 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007793 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007794 return NULL;
7795 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007796
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007797 if (vartype->tt_type == VAR_STRING)
7798 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007799 else if (vartype->tt_type == VAR_BLOB)
7800 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007801 else if (vartype->tt_type == VAR_LIST
7802 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007803 {
7804 if (var_count == 1)
7805 item_type = vartype->tt_member;
7806 else if (vartype->tt_member->tt_type == VAR_LIST
7807 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007808 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007809 item_type = vartype->tt_member->tt_member;
7810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007811
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007812 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007813 generate_undo_cmdmods(cctx);
7814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007816 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007817
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007818 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007819
Bram Moolenaar792f7862020-11-23 08:31:18 +01007820 arg = arg_start;
7821 if (var_count > 1)
7822 {
7823 generate_UNPACK(cctx, var_count, semicolon);
7824 arg = skipwhite(arg + 1); // skip white after '['
7825
7826 // the list item is replaced by a number of items
7827 if (ga_grow(stack, var_count - 1) == FAIL)
7828 {
7829 drop_scope(cctx);
7830 return NULL;
7831 }
7832 --stack->ga_len;
7833 for (idx = 0; idx < var_count; ++idx)
7834 {
7835 ((type_T **)stack->ga_data)[stack->ga_len] =
7836 (semicolon && idx == 0) ? vartype : item_type;
7837 ++stack->ga_len;
7838 }
7839 }
7840
7841 for (idx = 0; idx < var_count; ++idx)
7842 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007843 assign_dest_T dest = dest_local;
7844 int opt_flags = 0;
7845 int vimvaridx = -1;
7846 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007847 type_T *lhs_type = &t_any;
7848 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007849
7850 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007851 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007852 name = vim_strnsave(arg, varlen);
7853 if (name == NULL)
7854 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007855 if (*p == ':')
7856 {
7857 p = skipwhite(p + 1);
7858 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7859 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007860
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007861 // TODO: script var not supported?
7862 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7863 &vimvaridx, &type, cctx) == FAIL)
7864 goto failed;
7865 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007866 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007867 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7868 0, 0, type, name) == FAIL)
7869 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007870 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007871 else if (varlen == 1 && *arg == '_')
7872 {
7873 // Assigning to "_": drop the value.
7874 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7875 goto failed;
7876 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007877 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007878 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007879 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007880 {
7881 semsg(_(e_variable_already_declared), arg);
7882 goto failed;
7883 }
7884
Bram Moolenaarea870692020-12-02 14:24:30 +01007885 if (STRNCMP(name, "s:", 2) == 0)
7886 {
7887 semsg(_(e_cannot_declare_script_variable_in_function), name);
7888 goto failed;
7889 }
7890
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007891 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007892 where.wt_index = var_count > 1 ? idx + 1 : 0;
7893 where.wt_variable = TRUE;
7894 if (lhs_type == &t_any)
7895 lhs_type = item_type;
7896 else if (item_type != &t_unknown
7897 && !(var_count > 1 && item_type == &t_any)
7898 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7899 goto failed;
7900 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007901 if (var_lvar == NULL)
7902 // out of memory or used as an argument
7903 goto failed;
7904
7905 if (semicolon && idx == var_count - 1)
7906 var_lvar->lv_type = vartype;
7907 else
7908 var_lvar->lv_type = item_type;
7909 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7910 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007911
7912 if (*p == ',' || *p == ';')
7913 ++p;
7914 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007915 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007916 }
7917
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007918 if (cctx->ctx_compile_type == CT_DEBUG)
7919 // Add ISN_DEBUG here, so that the loop variables can be inspected.
7920 generate_instr_debug(cctx);
7921
Bram Moolenaar792f7862020-11-23 08:31:18 +01007922 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007923
7924failed:
7925 vim_free(name);
7926 drop_scope(cctx);
7927 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007928}
7929
7930/*
7931 * compile "endfor"
7932 */
7933 static char_u *
7934compile_endfor(char_u *arg, cctx_T *cctx)
7935{
7936 garray_T *instr = &cctx->ctx_instr;
7937 scope_T *scope = cctx->ctx_scope;
7938 forscope_T *forscope;
7939 isn_T *isn;
7940
Bram Moolenaarfa984412021-03-25 22:15:28 +01007941 if (misplaced_cmdmod(cctx))
7942 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007944 if (scope == NULL || scope->se_type != FOR_SCOPE)
7945 {
7946 emsg(_(e_for));
7947 return NULL;
7948 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007949 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007951 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007952
7953 // At end of ":for" scope jump back to the FOR instruction.
7954 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7955
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007956 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007957 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7958 isn->isn_arg.forloop.for_end = instr->ga_len;
7959
7960 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007961 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007962
7963 // Below the ":for" scope drop the "expr" list from the stack.
7964 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7965 return NULL;
7966
7967 vim_free(scope);
7968
7969 return arg;
7970}
7971
7972/*
7973 * compile "while expr"
7974 *
7975 * Produces instructions:
7976 * top: EVAL expr Push result of "expr"
7977 * JUMP_IF_FALSE end jump if false
7978 * ... body ...
7979 * JUMP top Jump back to repeat
7980 * end:
7981 *
7982 */
7983 static char_u *
7984compile_while(char_u *arg, cctx_T *cctx)
7985{
7986 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007987 scope_T *scope;
7988
7989 scope = new_scope(cctx, WHILE_SCOPE);
7990 if (scope == NULL)
7991 return NULL;
7992
Bram Moolenaara91a7132021-03-25 21:12:15 +01007993 // "endwhile" jumps back here, one before when profiling or using cmdmods
7994 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007995
7996 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007997 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007998 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007999 if (!ends_excmd2(arg, skipwhite(p)))
8000 {
8001 semsg(_(e_trailing_arg), p);
8002 return NULL;
8003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008004
Bram Moolenaar13106602020-10-04 16:06:05 +02008005 if (bool_on_stack(cctx) == FAIL)
8006 return FAIL;
8007
Bram Moolenaara91a7132021-03-25 21:12:15 +01008008 // CMDMOD_REV must come before the jump
8009 generate_undo_cmdmods(cctx);
8010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008012 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008013 JUMP_IF_FALSE, cctx) == FAIL)
8014 return FAIL;
8015
8016 return p;
8017}
8018
8019/*
8020 * compile "endwhile"
8021 */
8022 static char_u *
8023compile_endwhile(char_u *arg, cctx_T *cctx)
8024{
8025 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008026 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008027
Bram Moolenaarfa984412021-03-25 22:15:28 +01008028 if (misplaced_cmdmod(cctx))
8029 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008030 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8031 {
8032 emsg(_(e_while));
8033 return NULL;
8034 }
8035 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008036 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008037
Bram Moolenaarf002a412021-01-24 13:34:18 +01008038#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008039 // count the endwhile before jumping
8040 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008041#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008043 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008044 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008045
8046 // Fill in the "end" label in the WHILE statement so it can jump here.
8047 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008048 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8049 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008050
8051 vim_free(scope);
8052
8053 return arg;
8054}
8055
8056/*
8057 * compile "continue"
8058 */
8059 static char_u *
8060compile_continue(char_u *arg, cctx_T *cctx)
8061{
8062 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008063 int try_scopes = 0;
8064 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008065
8066 for (;;)
8067 {
8068 if (scope == NULL)
8069 {
8070 emsg(_(e_continue));
8071 return NULL;
8072 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008073 if (scope->se_type == FOR_SCOPE)
8074 {
8075 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008076 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008077 }
8078 if (scope->se_type == WHILE_SCOPE)
8079 {
8080 loop_label = scope->se_u.se_while.ws_top_label;
8081 break;
8082 }
8083 if (scope->se_type == TRY_SCOPE)
8084 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008085 scope = scope->se_outer;
8086 }
8087
Bram Moolenaarc150c092021-02-13 15:02:46 +01008088 if (try_scopes > 0)
8089 // Inside one or more try/catch blocks we first need to jump to the
8090 // "finally" or "endtry" to cleanup.
8091 generate_TRYCONT(cctx, try_scopes, loop_label);
8092 else
8093 // Jump back to the FOR or WHILE instruction.
8094 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008096 return arg;
8097}
8098
8099/*
8100 * compile "break"
8101 */
8102 static char_u *
8103compile_break(char_u *arg, cctx_T *cctx)
8104{
8105 scope_T *scope = cctx->ctx_scope;
8106 endlabel_T **el;
8107
8108 for (;;)
8109 {
8110 if (scope == NULL)
8111 {
8112 emsg(_(e_break));
8113 return NULL;
8114 }
8115 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8116 break;
8117 scope = scope->se_outer;
8118 }
8119
8120 // Jump to the end of the FOR or WHILE loop.
8121 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008122 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008123 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008124 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008125 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8126 return FAIL;
8127
8128 return arg;
8129}
8130
8131/*
8132 * compile "{" start of block
8133 */
8134 static char_u *
8135compile_block(char_u *arg, cctx_T *cctx)
8136{
8137 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8138 return NULL;
8139 return skipwhite(arg + 1);
8140}
8141
8142/*
8143 * compile end of block: drop one scope
8144 */
8145 static void
8146compile_endblock(cctx_T *cctx)
8147{
8148 scope_T *scope = cctx->ctx_scope;
8149
8150 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008151 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008152 vim_free(scope);
8153}
8154
8155/*
8156 * compile "try"
8157 * Creates a new scope for the try-endtry, pointing to the first catch and
8158 * finally.
8159 * Creates another scope for the "try" block itself.
8160 * TRY instruction sets up exception handling at runtime.
8161 *
8162 * "try"
8163 * TRY -> catch1, -> finally push trystack entry
8164 * ... try block
8165 * "throw {exception}"
8166 * EVAL {exception}
8167 * THROW create exception
8168 * ... try block
8169 * " catch {expr}"
8170 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008171 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008172 * EVAL {expr}
8173 * MATCH
8174 * JUMP nomatch -> catch2
8175 * CATCH remove exception
8176 * ... catch block
8177 * " catch"
8178 * JUMP -> finally
8179 * catch2: CATCH remove exception
8180 * ... catch block
8181 * " finally"
8182 * finally:
8183 * ... finally block
8184 * " endtry"
8185 * ENDTRY pop trystack entry, may rethrow
8186 */
8187 static char_u *
8188compile_try(char_u *arg, cctx_T *cctx)
8189{
8190 garray_T *instr = &cctx->ctx_instr;
8191 scope_T *try_scope;
8192 scope_T *scope;
8193
Bram Moolenaarfa984412021-03-25 22:15:28 +01008194 if (misplaced_cmdmod(cctx))
8195 return NULL;
8196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008197 // scope that holds the jumps that go to catch/finally/endtry
8198 try_scope = new_scope(cctx, TRY_SCOPE);
8199 if (try_scope == NULL)
8200 return NULL;
8201
Bram Moolenaar69f70502021-01-01 16:10:46 +01008202 if (cctx->ctx_skip != SKIP_YES)
8203 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008204 isn_T *isn;
8205
8206 // "try_catch" is set when the first ":catch" is found or when no catch
8207 // is found and ":finally" is found.
8208 // "try_finally" is set when ":finally" is found
8209 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008210 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008211 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8212 return NULL;
8213 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8214 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008215 return NULL;
8216 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008217
8218 // scope for the try block itself
8219 scope = new_scope(cctx, BLOCK_SCOPE);
8220 if (scope == NULL)
8221 return NULL;
8222
8223 return arg;
8224}
8225
8226/*
8227 * compile "catch {expr}"
8228 */
8229 static char_u *
8230compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8231{
8232 scope_T *scope = cctx->ctx_scope;
8233 garray_T *instr = &cctx->ctx_instr;
8234 char_u *p;
8235 isn_T *isn;
8236
Bram Moolenaarfa984412021-03-25 22:15:28 +01008237 if (misplaced_cmdmod(cctx))
8238 return NULL;
8239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008240 // end block scope from :try or :catch
8241 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8242 compile_endblock(cctx);
8243 scope = cctx->ctx_scope;
8244
8245 // Error if not in a :try scope
8246 if (scope == NULL || scope->se_type != TRY_SCOPE)
8247 {
8248 emsg(_(e_catch));
8249 return NULL;
8250 }
8251
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008252 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008253 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008254 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008255 return NULL;
8256 }
8257
Bram Moolenaar69f70502021-01-01 16:10:46 +01008258 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008259 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008260#ifdef FEAT_PROFILE
8261 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008262 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008263 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008264 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008265 .isn_type == ISN_PROF_START)
8266 --instr->ga_len;
8267#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008268 // Jump from end of previous block to :finally or :endtry
8269 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8270 JUMP_ALWAYS, cctx) == FAIL)
8271 return NULL;
8272
8273 // End :try or :catch scope: set value in ISN_TRY instruction
8274 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008275 if (isn->isn_arg.try.try_ref->try_catch == 0)
8276 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008277 if (scope->se_u.se_try.ts_catch_label != 0)
8278 {
8279 // Previous catch without match jumps here
8280 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8281 isn->isn_arg.jump.jump_where = instr->ga_len;
8282 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008283#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008284 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008285 {
8286 // a "throw" that jumps here needs to be counted
8287 generate_instr(cctx, ISN_PROF_END);
8288 // the "catch" is also counted
8289 generate_instr(cctx, ISN_PROF_START);
8290 }
8291#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008292 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008293 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008294 }
8295
8296 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008297 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008298 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008299 scope->se_u.se_try.ts_caught_all = TRUE;
8300 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008301 }
8302 else
8303 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008304 char_u *end;
8305 char_u *pat;
8306 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008307 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008308 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008310 // Push v:exception, push {expr} and MATCH
8311 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8312
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008313 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008314 if (*end != *p)
8315 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008316 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008317 vim_free(tofree);
8318 return FAIL;
8319 }
8320 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008321 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008322 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008323 len = (int)(end - tofree);
8324 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008325 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008326 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008327 if (pat == NULL)
8328 return FAIL;
8329 if (generate_PUSHS(cctx, pat) == FAIL)
8330 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008332 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8333 return NULL;
8334
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008335 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008336 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8337 return NULL;
8338 }
8339
Bram Moolenaar69f70502021-01-01 16:10:46 +01008340 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008341 return NULL;
8342
8343 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8344 return NULL;
8345 return p;
8346}
8347
8348 static char_u *
8349compile_finally(char_u *arg, cctx_T *cctx)
8350{
8351 scope_T *scope = cctx->ctx_scope;
8352 garray_T *instr = &cctx->ctx_instr;
8353 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008354 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008355
Bram Moolenaarfa984412021-03-25 22:15:28 +01008356 if (misplaced_cmdmod(cctx))
8357 return NULL;
8358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008359 // end block scope from :try or :catch
8360 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8361 compile_endblock(cctx);
8362 scope = cctx->ctx_scope;
8363
8364 // Error if not in a :try scope
8365 if (scope == NULL || scope->se_type != TRY_SCOPE)
8366 {
8367 emsg(_(e_finally));
8368 return NULL;
8369 }
8370
8371 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008372 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008373 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008374 {
8375 emsg(_(e_finally_dup));
8376 return NULL;
8377 }
8378
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008379 this_instr = instr->ga_len;
8380#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008381 if (cctx->ctx_compile_type == CT_PROFILE
8382 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008383 .isn_type == ISN_PROF_START)
8384 // jump to the profile start of the "finally"
8385 --this_instr;
8386#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008387
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008388 // Fill in the "end" label in jumps at the end of the blocks.
8389 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8390 this_instr, cctx);
8391
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008392 // If there is no :catch then an exception jumps to :finally.
8393 if (isn->isn_arg.try.try_ref->try_catch == 0)
8394 isn->isn_arg.try.try_ref->try_catch = this_instr;
8395 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008396 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008397 {
8398 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008399 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008400 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008401 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008402 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008403 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8404 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008406 // TODO: set index in ts_finally_label jumps
8407
8408 return arg;
8409}
8410
8411 static char_u *
8412compile_endtry(char_u *arg, cctx_T *cctx)
8413{
8414 scope_T *scope = cctx->ctx_scope;
8415 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008416 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008417
Bram Moolenaarfa984412021-03-25 22:15:28 +01008418 if (misplaced_cmdmod(cctx))
8419 return NULL;
8420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008421 // end block scope from :catch or :finally
8422 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8423 compile_endblock(cctx);
8424 scope = cctx->ctx_scope;
8425
8426 // Error if not in a :try scope
8427 if (scope == NULL || scope->se_type != TRY_SCOPE)
8428 {
8429 if (scope == NULL)
8430 emsg(_(e_no_endtry));
8431 else if (scope->se_type == WHILE_SCOPE)
8432 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008433 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008434 emsg(_(e_endfor));
8435 else
8436 emsg(_(e_endif));
8437 return NULL;
8438 }
8439
Bram Moolenaarc150c092021-02-13 15:02:46 +01008440 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008441 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008442 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008443 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8444 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008445 {
8446 emsg(_(e_missing_catch_or_finally));
8447 return NULL;
8448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008449
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008450#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008451 if (cctx->ctx_compile_type == CT_PROFILE
8452 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008453 .isn_type == ISN_PROF_START)
8454 // move the profile start after "endtry" so that it's not counted when
8455 // the exception is rethrown.
8456 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008457#endif
8458
Bram Moolenaar69f70502021-01-01 16:10:46 +01008459 // Fill in the "end" label in jumps at the end of the blocks, if not
8460 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008461 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8462 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008463
Bram Moolenaar69f70502021-01-01 16:10:46 +01008464 if (scope->se_u.se_try.ts_catch_label != 0)
8465 {
8466 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008467 isn_T *isn = ((isn_T *)instr->ga_data)
8468 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008469 isn->isn_arg.jump.jump_where = instr->ga_len;
8470 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008471 }
8472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008473 compile_endblock(cctx);
8474
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008475 if (cctx->ctx_skip != SKIP_YES)
8476 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008477 // End :catch or :finally scope: set instruction index in ISN_TRY
8478 // instruction
8479 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008480 if (cctx->ctx_skip != SKIP_YES
8481 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8482 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008483#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008484 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008485 generate_instr(cctx, ISN_PROF_START);
8486#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008488 return arg;
8489}
8490
8491/*
8492 * compile "throw {expr}"
8493 */
8494 static char_u *
8495compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8496{
8497 char_u *p = skipwhite(arg);
8498
Bram Moolenaara5565e42020-05-09 15:44:01 +02008499 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008500 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008501 if (cctx->ctx_skip == SKIP_YES)
8502 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008503 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008504 return NULL;
8505 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8506 return NULL;
8507
8508 return p;
8509}
8510
8511/*
8512 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008513 * compile "echomsg expr"
8514 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008515 * compile "execute expr"
8516 */
8517 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008518compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008519{
8520 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008521 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008522 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008523 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008524 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008525 garray_T *stack = &cctx->ctx_type_stack;
8526 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008527
8528 for (;;)
8529 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008530 if (ends_excmd2(prev, p))
8531 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008532 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008533 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008534 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008535
8536 if (cctx->ctx_skip != SKIP_YES)
8537 {
8538 // check for non-void type
8539 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8540 if (type->tt_type == VAR_VOID)
8541 {
8542 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8543 return NULL;
8544 }
8545 }
8546
Bram Moolenaarad39c092020-02-26 18:23:43 +01008547 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008548 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008549 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008550 }
8551
Bram Moolenaare4984292020-12-13 14:19:25 +01008552 if (count > 0)
8553 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008554 long save_lnum = cctx->ctx_lnum;
8555
8556 // Use the line number where the command started.
8557 cctx->ctx_lnum = start_ctx_lnum;
8558
Bram Moolenaare4984292020-12-13 14:19:25 +01008559 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8560 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8561 else if (cmdidx == CMD_execute)
8562 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8563 else if (cmdidx == CMD_echomsg)
8564 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8565 else
8566 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008567
8568 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008570 return p;
8571}
8572
8573/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008574 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008575 * instruction to compute it and return OK.
8576 * Otherwise return FAIL, the caller must deal with any range.
8577 */
8578 static int
8579compile_variable_range(exarg_T *eap, cctx_T *cctx)
8580{
8581 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8582 char_u *p = skipdigits(eap->cmd);
8583
8584 if (p == range_end)
8585 return FAIL;
8586 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8587}
8588
8589/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008590 * :put r
8591 * :put ={expr}
8592 */
8593 static char_u *
8594compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8595{
8596 char_u *line = arg;
8597 linenr_T lnum;
8598 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008599 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008600
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008601 eap->regname = *line;
8602
8603 if (eap->regname == '=')
8604 {
8605 char_u *p = line + 1;
8606
8607 if (compile_expr0(&p, cctx) == FAIL)
8608 return NULL;
8609 line = p;
8610 }
8611 else if (eap->regname != NUL)
8612 ++line;
8613
Bram Moolenaar08597872020-12-10 19:43:40 +01008614 if (compile_variable_range(eap, cctx) == OK)
8615 {
8616 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8617 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008618 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008619 {
8620 // Either no range or a number.
8621 // "errormsg" will not be set because the range is ADDR_LINES.
8622 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008623 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008624 return NULL;
8625 if (eap->addr_count == 0)
8626 lnum = -1;
8627 else
8628 lnum = eap->line2;
8629 if (above)
8630 --lnum;
8631 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008632
8633 generate_PUT(cctx, eap->regname, lnum);
8634 return line;
8635}
8636
8637/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008638 * A command that is not compiled, execute with legacy code.
8639 */
8640 static char_u *
8641compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8642{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008643 char_u *p;
8644 int has_expr = FALSE;
8645 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008646
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008647 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008648 goto theend;
8649
Bram Moolenaare729ce22021-06-06 21:38:09 +02008650 // If there was a prececing command modifier, drop it and include it in the
8651 // EXEC command.
8652 if (cctx->ctx_has_cmdmod)
8653 {
8654 garray_T *instr = &cctx->ctx_instr;
8655 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8656
8657 if (isn->isn_type == ISN_CMDMOD)
8658 {
8659 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8660 ->cmod_filter_regmatch.regprog);
8661 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8662 --instr->ga_len;
8663 cctx->ctx_has_cmdmod = FALSE;
8664 }
8665 }
8666
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008667 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008668 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008669 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008670 int usefilter = FALSE;
8671
8672 has_expr = argt & (EX_XFILE | EX_EXPAND);
8673
8674 // If the command can be followed by a bar, find the bar and truncate
8675 // it, so that the following command can be compiled.
8676 // The '|' is overwritten with a NUL, it is put back below.
8677 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8678 && *eap->arg == '!')
8679 // :w !filter or :r !filter or :r! filter
8680 usefilter = TRUE;
8681 if ((argt & EX_TRLBAR) && !usefilter)
8682 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008683 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008684 separate_nextcmd(eap);
8685 if (eap->nextcmd != NULL)
8686 nextcmd = eap->nextcmd;
8687 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008688 else if (eap->cmdidx == CMD_wincmd)
8689 {
8690 p = eap->arg;
8691 if (*p != NUL)
8692 ++p;
8693 if (*p == 'g' || *p == Ctrl_G)
8694 ++p;
8695 p = skipwhite(p);
8696 if (*p == '|')
8697 {
8698 *p = NUL;
8699 nextcmd = p + 1;
8700 }
8701 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008702 }
8703
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008704 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8705 {
8706 // expand filename in "syntax include [@group] filename"
8707 has_expr = TRUE;
8708 eap->arg = skipwhite(eap->arg + 7);
8709 if (*eap->arg == '@')
8710 eap->arg = skiptowhite(eap->arg);
8711 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008712
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008713 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8714 && STRLEN(eap->arg) > 4)
8715 {
8716 int delim = *eap->arg;
8717
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008718 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008719 if (*p == delim)
8720 {
8721 eap->arg = p + 1;
8722 has_expr = TRUE;
8723 }
8724 }
8725
Bram Moolenaarecac5912021-01-05 19:23:28 +01008726 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8727 {
8728 // TODO: should only expand when appropriate for the command
8729 eap->arg = skiptowhite(eap->arg);
8730 has_expr = TRUE;
8731 }
8732
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008733 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008734 {
8735 int count = 0;
8736 char_u *start = skipwhite(line);
8737
8738 // :cmd xxx`=expr1`yyy`=expr2`zzz
8739 // PUSHS ":cmd xxx"
8740 // eval expr1
8741 // PUSHS "yyy"
8742 // eval expr2
8743 // PUSHS "zzz"
8744 // EXECCONCAT 5
8745 for (;;)
8746 {
8747 if (p > start)
8748 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008749 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008750 ++count;
8751 }
8752 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008753 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008754 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008755 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008756 ++count;
8757 p = skipwhite(p);
8758 if (*p != '`')
8759 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008760 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008761 return NULL;
8762 }
8763 start = p + 1;
8764
8765 p = (char_u *)strstr((char *)start, "`=");
8766 if (p == NULL)
8767 {
8768 if (*skipwhite(start) != NUL)
8769 {
8770 generate_PUSHS(cctx, vim_strsave(start));
8771 ++count;
8772 }
8773 break;
8774 }
8775 }
8776 generate_EXECCONCAT(cctx, count);
8777 }
8778 else
8779 generate_EXEC(cctx, line);
8780
8781theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008782 if (*nextcmd != NUL)
8783 {
8784 // the parser expects a pointer to the bar, put it back
8785 --nextcmd;
8786 *nextcmd = '|';
8787 }
8788
8789 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008790}
8791
Bram Moolenaar20677332021-06-06 17:02:53 +02008792/*
8793 * A script command with heredoc, e.g.
8794 * ruby << EOF
8795 * command
8796 * EOF
8797 * Has been turned into one long line with NL characters by
8798 * get_function_body():
8799 * ruby << EOF<NL> command<NL>EOF
8800 */
8801 static char_u *
8802compile_script(char_u *line, cctx_T *cctx)
8803{
8804 if (cctx->ctx_skip != SKIP_YES)
8805 {
8806 isn_T *isn;
8807
8808 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8809 return NULL;
8810 isn->isn_arg.string = vim_strsave(line);
8811 }
8812 return (char_u *)"";
8813}
8814
Bram Moolenaar8238f082021-04-20 21:10:48 +02008815
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008816/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008817 * :s/pat/repl/
8818 */
8819 static char_u *
8820compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8821{
8822 char_u *cmd = eap->arg;
8823 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8824
8825 if (expr != NULL)
8826 {
8827 int delimiter = *cmd++;
8828
8829 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008830 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008831 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8832 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008833 garray_T save_ga = cctx->ctx_instr;
8834 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008835 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008836 int trailing_error;
8837 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008838 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008839 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008840
8841 cmd += 3;
8842 end = skip_substitute(cmd, delimiter);
8843
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008844 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008845 // labels are correct.
8846 cctx->ctx_instr.ga_len = 0;
8847 cctx->ctx_instr.ga_maxlen = 0;
8848 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008849 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008850 if (end[-1] == NUL)
8851 end[-1] = delimiter;
8852 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008853 trailing_error = *cmd != delimiter && *cmd != NUL;
8854
Bram Moolenaar169502f2021-04-21 12:19:35 +02008855 if (expr_res == FAIL || trailing_error
8856 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008857 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008858 if (trailing_error)
8859 semsg(_(e_trailing_arg), cmd);
8860 clear_instr_ga(&cctx->ctx_instr);
8861 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008862 return NULL;
8863 }
8864
Bram Moolenaar8238f082021-04-20 21:10:48 +02008865 // Move the generated instructions into the ISN_SUBSTITUTE
8866 // instructions, then restore the list of instructions before
8867 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008868 instr_count = cctx->ctx_instr.ga_len;
8869 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008870 instr[instr_count].isn_type = ISN_FINISH;
8871
8872 cctx->ctx_instr = save_ga;
8873 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8874 {
8875 int idx;
8876
8877 for (idx = 0; idx < instr_count; ++idx)
8878 delete_instr(instr + idx);
8879 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008880 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008881 }
8882 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8883 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008884
8885 // skip over flags
8886 if (*end == '&')
8887 ++end;
8888 while (ASCII_ISALPHA(*end) || *end == '#')
8889 ++end;
8890 return end;
8891 }
8892 }
8893
8894 return compile_exec(arg, eap, cctx);
8895}
8896
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008897 static char_u *
8898compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8899{
8900 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008901 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008902
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008903 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008904 {
8905 if (STRNCMP(arg, "END", 3) == 0)
8906 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008907 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008908 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008909 // First load the current variable value.
8910 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8911 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008912 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008913 }
8914
8915 // Gets the redirected text and put it on the stack, then store it
8916 // in the variable.
8917 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8918
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008919 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008920 generate_instr_drop(cctx, ISN_CONCAT, 1);
8921
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008922 if (lhs->lhs_has_index)
8923 {
8924 // Use the info in "lhs" to store the value at the index in the
8925 // list or dict.
8926 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8927 &t_string, cctx) == FAIL)
8928 return NULL;
8929 }
8930 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008931 return NULL;
8932
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008933 VIM_CLEAR(lhs->lhs_name);
8934 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008935 return arg + 3;
8936 }
8937 emsg(_(e_cannot_nest_redir));
8938 return NULL;
8939 }
8940
8941 if (arg[0] == '=' && arg[1] == '>')
8942 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008943 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008944
8945 // redirect to a variable is compiled
8946 arg += 2;
8947 if (*arg == '>')
8948 {
8949 ++arg;
8950 append = TRUE;
8951 }
8952 arg = skipwhite(arg);
8953
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008954 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008955 FALSE, FALSE, 1, cctx) == FAIL)
8956 return NULL;
8957 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008958 lhs->lhs_append = append;
8959 if (lhs->lhs_has_index)
8960 {
8961 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8962 if (lhs->lhs_whole == NULL)
8963 return NULL;
8964 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008965
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008966 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008967 }
8968
8969 // other redirects are handled like at script level
8970 return compile_exec(line, eap, cctx);
8971}
8972
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008973#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008974 static char_u *
8975compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8976{
8977 isn_T *isn;
8978 char_u *p;
8979
8980 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8981 if (isn == NULL)
8982 return NULL;
8983 isn->isn_arg.number = eap->cmdidx;
8984
8985 p = eap->arg;
8986 if (compile_expr0(&p, cctx) == FAIL)
8987 return NULL;
8988
8989 isn = generate_instr(cctx, ISN_CEXPR_CORE);
8990 if (isn == NULL)
8991 return NULL;
8992 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
8993 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
8994 return NULL;
8995 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
8996 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
8997 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
8998
8999 return p;
9000}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009001#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009002
Bram Moolenaar4c137212021-04-19 16:48:48 +02009003/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009004 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009005 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009006 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009007 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009008add_def_function(ufunc_T *ufunc)
9009{
9010 dfunc_T *dfunc;
9011
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009012 if (def_functions.ga_len == 0)
9013 {
9014 // The first position is not used, so that a zero uf_dfunc_idx means it
9015 // wasn't set.
9016 if (ga_grow(&def_functions, 1) == FAIL)
9017 return FAIL;
9018 ++def_functions.ga_len;
9019 }
9020
Bram Moolenaar09689a02020-05-09 22:50:08 +02009021 // Add the function to "def_functions".
9022 if (ga_grow(&def_functions, 1) == FAIL)
9023 return FAIL;
9024 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9025 CLEAR_POINTER(dfunc);
9026 dfunc->df_idx = def_functions.ga_len;
9027 ufunc->uf_dfunc_idx = dfunc->df_idx;
9028 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009029 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009030 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009031 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009032 ++def_functions.ga_len;
9033 return OK;
9034}
9035
9036/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009037 * After ex_function() has collected all the function lines: parse and compile
9038 * the lines into instructions.
9039 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009040 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9041 * the return statement (used for lambda). When uf_ret_type is already set
9042 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009043 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009044 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009045 * This can be used recursively through compile_lambda(), which may reallocate
9046 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009047 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009048 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009049 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009050compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009051 ufunc_T *ufunc,
9052 int check_return_type,
9053 compiletype_T compile_type,
9054 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009055{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009056 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009057 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009058 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009059 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009060 cctx_T cctx;
9061 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009062 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009063 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009064 int ret = FAIL;
9065 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009066 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009067 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009068 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009069 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009070#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009071 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009072#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009073 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009074
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009075 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009076 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009077 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009078 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009079 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9080 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009081 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009082
9083 switch (compile_type)
9084 {
9085 case CT_PROFILE:
9086#ifdef FEAT_PROFILE
9087 instr_dest = dfunc->df_instr_prof; break;
9088#endif
9089 case CT_NONE: instr_dest = dfunc->df_instr; break;
9090 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9091 }
9092 if (instr_dest != NULL)
9093 // Was compiled in this mode before: Free old instructions.
9094 delete_def_function_contents(dfunc, FALSE);
9095 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009096 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009097 else
9098 {
9099 if (add_def_function(ufunc) == FAIL)
9100 return FAIL;
9101 new_def_function = TRUE;
9102 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009103
Bram Moolenaar985116a2020-07-12 17:31:09 +02009104 ufunc->uf_def_status = UF_COMPILING;
9105
Bram Moolenaara80faa82020-04-12 19:37:17 +02009106 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009107
Bram Moolenaare99d4222021-06-13 14:01:26 +02009108 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009109 cctx.ctx_ufunc = ufunc;
9110 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009111 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009112 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9113 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9114 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9115 cctx.ctx_type_list = &ufunc->uf_type_list;
9116 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9117 instr = &cctx.ctx_instr;
9118
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009119 // Set the context to the function, it may be compiled when called from
9120 // another script. Set the script version to the most modern one.
9121 // The line number will be set in next_line_from_context().
9122 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009123 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9124
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009125 // Don't use the flag from ":legacy" here.
9126 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9127
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009128 // Make sure error messages are OK.
9129 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9130 if (do_estack_push)
9131 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009132 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009133
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009134 if (ufunc->uf_def_args.ga_len > 0)
9135 {
9136 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009137 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009138 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009139 int i;
9140 char_u *arg;
9141 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009142 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009143
9144 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009145 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009146 for (i = 0; i < count; ++i)
9147 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009148 garray_T *stack = &cctx.ctx_type_stack;
9149 type_T *val_type;
9150 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009151 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009152 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009153 int jump_instr_idx = instr->ga_len;
9154 isn_T *isn;
9155
9156 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9157 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9158 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009159
9160 // Make sure later arguments are not found.
9161 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009162
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009163 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009164 r = compile_expr0(&arg, &cctx);
9165
9166 ufunc->uf_args.ga_len = uf_args_len;
9167 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009168 goto erret;
9169
9170 // If no type specified use the type of the default value.
9171 // Otherwise check that the default value type matches the
9172 // specified type.
9173 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009174 where.wt_index = arg_idx + 1;
9175 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009176 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009177 {
9178 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009179 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009180 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009181 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009182 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009183 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009184
9185 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009186 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009187
9188 // set instruction index in JUMP_IF_ARG_SET to here
9189 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9190 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009191 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009192
9193 if (did_set_arg_type)
9194 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009195 }
9196
9197 /*
9198 * Loop over all the lines of the function and generate instructions.
9199 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009200 for (;;)
9201 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009202 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009203 int starts_with_colon = FALSE;
9204 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009205 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009206
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009207 // Bail out on the first error to avoid a flood of errors and report
9208 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009209 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009210 goto erret;
9211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009212 if (line != NULL && *line == '|')
9213 // the line continues after a '|'
9214 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009215 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009216 && !(*line == '#' && (line == cctx.ctx_line_start
9217 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009218 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009219 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009220 goto erret;
9221 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009222 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9223 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009224 else
9225 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009226 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009227 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009228 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009229 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009230#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009231 if (cctx.ctx_skip != SKIP_YES)
9232 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009233#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009234 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009235 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009236 // Make a copy, splitting off nextcmd and removing trailing spaces
9237 // may change it.
9238 if (line != NULL)
9239 {
9240 line = vim_strsave(line);
9241 vim_free(line_to_free);
9242 line_to_free = line;
9243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009244 }
9245
Bram Moolenaara80faa82020-04-12 19:37:17 +02009246 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009247 ea.cmdlinep = &line;
9248 ea.cmd = skipwhite(line);
9249
Bram Moolenaarb2049902021-01-24 12:53:53 +01009250 if (*ea.cmd == '#')
9251 {
9252 // "#" starts a comment
9253 line = (char_u *)"";
9254 continue;
9255 }
9256
Bram Moolenaarf002a412021-01-24 13:34:18 +01009257#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009258 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9259 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009260 {
9261 may_generate_prof_end(&cctx, prof_lnum);
9262
9263 prof_lnum = cctx.ctx_lnum;
9264 generate_instr(&cctx, ISN_PROF_START);
9265 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009266#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009267 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9268 && cctx.ctx_skip != SKIP_YES)
9269 {
9270 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009271 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009272 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01009273
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009274 // Some things can be recognized by the first character.
9275 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009276 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009277 case '}':
9278 {
9279 // "}" ends a block scope
9280 scopetype_T stype = cctx.ctx_scope == NULL
9281 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009282
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009283 if (stype == BLOCK_SCOPE)
9284 {
9285 compile_endblock(&cctx);
9286 line = ea.cmd;
9287 }
9288 else
9289 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009290 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009291 goto erret;
9292 }
9293 if (line != NULL)
9294 line = skipwhite(ea.cmd + 1);
9295 continue;
9296 }
9297
9298 case '{':
9299 // "{" starts a block scope
9300 // "{'a': 1}->func() is something else
9301 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9302 {
9303 line = compile_block(ea.cmd, &cctx);
9304 continue;
9305 }
9306 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009307 }
9308
9309 /*
9310 * COMMAND MODIFIERS
9311 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009312 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009313 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9314 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009315 {
9316 if (errormsg != NULL)
9317 goto erret;
9318 // empty line or comment
9319 line = (char_u *)"";
9320 continue;
9321 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009322 generate_cmdmods(&cctx, &local_cmdmod);
9323 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009324
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009325 // Check if there was a colon after the last command modifier or before
9326 // the current position.
9327 for (p = ea.cmd; p >= line; --p)
9328 {
9329 if (*p == ':')
9330 starts_with_colon = TRUE;
9331 if (p < ea.cmd && !VIM_ISWHITE(*p))
9332 break;
9333 }
9334
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009335 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009336 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009337 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009338 {
9339 if (*ea.cmd == '(')
9340 // not for "call()"
9341 ea.cmd = p;
9342 else
9343 ea.cmd = skipwhite(ea.cmd);
9344 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009345
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009346 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009347 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009348 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009349
Bram Moolenaar17126b12021-01-07 22:03:02 +01009350 // Check for assignment after command modifiers.
9351 assign = may_compile_assignment(&ea, &line, &cctx);
9352 if (assign == OK)
9353 goto nextline;
9354 if (assign == FAIL)
9355 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009356 }
9357
9358 /*
9359 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009360 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009361 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009362 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009363 cmd = ea.cmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009364 if (starts_with_colon || !(*cmd == '\''
9365 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009366 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009367 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009368 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009369 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009370 if (!starts_with_colon)
9371 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009372 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009373 goto erret;
9374 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009375 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009376 if (ends_excmd2(line, ea.cmd))
9377 {
9378 // A range without a command: jump to the line.
9379 // TODO: compile to a more efficient command, possibly
9380 // calling parse_cmd_address().
9381 ea.cmdidx = CMD_SIZE;
9382 line = compile_exec(line, &ea, &cctx);
9383 goto nextline;
9384 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009385 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009386 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009387 p = find_ex_command(&ea, NULL, starts_with_colon
9388 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009389
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009390 if (p == NULL)
9391 {
9392 if (cctx.ctx_skip != SKIP_YES)
9393 emsg(_(e_ambiguous_use_of_user_defined_command));
9394 goto erret;
9395 }
9396
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009397 // When using ":legacy cmd" always use compile_exec().
9398 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009399 {
9400 char_u *start = ea.cmd;
9401
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009402 switch (ea.cmdidx)
9403 {
9404 case CMD_if:
9405 case CMD_elseif:
9406 case CMD_else:
9407 case CMD_endif:
9408 case CMD_for:
9409 case CMD_endfor:
9410 case CMD_continue:
9411 case CMD_break:
9412 case CMD_while:
9413 case CMD_endwhile:
9414 case CMD_try:
9415 case CMD_catch:
9416 case CMD_finally:
9417 case CMD_endtry:
9418 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9419 goto erret;
9420 default: break;
9421 }
9422
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009423 // ":legacy return expr" needs to be handled differently.
9424 if (checkforcmd(&start, "return", 4))
9425 ea.cmdidx = CMD_return;
9426 else
9427 ea.cmdidx = CMD_legacy;
9428 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009430 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9431 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009432 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009433 {
9434 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009435 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009436 }
9437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009438 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009439 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009440 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009441 // CMD_var cannot happen, compile_assignment() above would be
9442 // used. Most likely an assignment to a non-existing variable.
9443 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009444 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009446 }
9447
Bram Moolenaar3988f642020-08-27 22:43:03 +02009448 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009449 && ea.cmdidx != CMD_elseif
9450 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009451 && ea.cmdidx != CMD_endif
9452 && ea.cmdidx != CMD_endfor
9453 && ea.cmdidx != CMD_endwhile
9454 && ea.cmdidx != CMD_catch
9455 && ea.cmdidx != CMD_finally
9456 && ea.cmdidx != CMD_endtry)
9457 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009458 emsg(_(e_unreachable_code_after_return));
9459 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009460 }
9461
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009462 p = skipwhite(p);
9463 if (ea.cmdidx != CMD_SIZE
9464 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9465 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009466 if (ea.cmdidx >= 0)
9467 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009468 if ((ea.argt & EX_BANG) && *p == '!')
9469 {
9470 ea.forceit = TRUE;
9471 p = skipwhite(p + 1);
9472 }
9473 }
9474
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009475 switch (ea.cmdidx)
9476 {
9477 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009478 ea.arg = p;
9479 line = compile_nested_function(&ea, &cctx);
9480 break;
9481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009482 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009483 // TODO: should we allow this, e.g. to declare a global
9484 // function?
9485 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009486 goto erret;
9487
9488 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009489 line = compile_return(p, check_return_type,
9490 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009491 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009492 break;
9493
9494 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009495 emsg(_(e_cannot_use_let_in_vim9_script));
9496 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009497 case CMD_var:
9498 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009499 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009500 case CMD_increment:
9501 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009502 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009503 if (line == p)
9504 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009505 break;
9506
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009507 case CMD_unlet:
9508 case CMD_unlockvar:
9509 case CMD_lockvar:
9510 line = compile_unletlock(p, &ea, &cctx);
9511 break;
9512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009513 case CMD_import:
9514 line = compile_import(p, &cctx);
9515 break;
9516
9517 case CMD_if:
9518 line = compile_if(p, &cctx);
9519 break;
9520 case CMD_elseif:
9521 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009522 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009523 break;
9524 case CMD_else:
9525 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009526 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009527 break;
9528 case CMD_endif:
9529 line = compile_endif(p, &cctx);
9530 break;
9531
9532 case CMD_while:
9533 line = compile_while(p, &cctx);
9534 break;
9535 case CMD_endwhile:
9536 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009537 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009538 break;
9539
9540 case CMD_for:
9541 line = compile_for(p, &cctx);
9542 break;
9543 case CMD_endfor:
9544 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009545 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009546 break;
9547 case CMD_continue:
9548 line = compile_continue(p, &cctx);
9549 break;
9550 case CMD_break:
9551 line = compile_break(p, &cctx);
9552 break;
9553
9554 case CMD_try:
9555 line = compile_try(p, &cctx);
9556 break;
9557 case CMD_catch:
9558 line = compile_catch(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_finally:
9562 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009563 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009564 break;
9565 case CMD_endtry:
9566 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009567 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009568 break;
9569 case CMD_throw:
9570 line = compile_throw(p, &cctx);
9571 break;
9572
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009573 case CMD_eval:
9574 if (compile_expr0(&p, &cctx) == FAIL)
9575 goto erret;
9576
Bram Moolenaar3988f642020-08-27 22:43:03 +02009577 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009578 generate_instr_drop(&cctx, ISN_DROP, 1);
9579
9580 line = skipwhite(p);
9581 break;
9582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009583 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009584 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009585 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009586 case CMD_echomsg:
9587 case CMD_echoerr:
9588 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009589 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009590
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009591 case CMD_put:
9592 ea.cmd = cmd;
9593 line = compile_put(p, &ea, &cctx);
9594 break;
9595
Bram Moolenaar4c137212021-04-19 16:48:48 +02009596 case CMD_substitute:
9597 if (cctx.ctx_skip == SKIP_YES)
9598 line = (char_u *)"";
9599 else
9600 {
9601 ea.arg = p;
9602 line = compile_substitute(line, &ea, &cctx);
9603 }
9604 break;
9605
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009606 case CMD_redir:
9607 ea.arg = p;
9608 line = compile_redir(line, &ea, &cctx);
9609 break;
9610
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009611 case CMD_cexpr:
9612 case CMD_lexpr:
9613 case CMD_caddexpr:
9614 case CMD_laddexpr:
9615 case CMD_cgetexpr:
9616 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009617#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009618 ea.arg = p;
9619 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009620#else
9621 ex_ni(&ea);
9622 line = NULL;
9623#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009624 break;
9625
Bram Moolenaar3988f642020-08-27 22:43:03 +02009626 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009627
Bram Moolenaarae616492020-07-28 20:07:27 +02009628 case CMD_append:
9629 case CMD_change:
9630 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009631 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009632 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009633 case CMD_xit:
9634 not_in_vim9(&ea);
9635 goto erret;
9636
Bram Moolenaar002262f2020-07-08 17:47:57 +02009637 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009638 if (cctx.ctx_skip != SKIP_YES)
9639 {
9640 semsg(_(e_invalid_command_str), ea.cmd);
9641 goto erret;
9642 }
9643 // We don't check for a next command here.
9644 line = (char_u *)"";
9645 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009646
Bram Moolenaar20677332021-06-06 17:02:53 +02009647 case CMD_lua:
9648 case CMD_mzscheme:
9649 case CMD_perl:
9650 case CMD_py3:
9651 case CMD_python3:
9652 case CMD_python:
9653 case CMD_pythonx:
9654 case CMD_ruby:
9655 case CMD_tcl:
9656 ea.arg = p;
9657 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009658 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009659 else
9660 // heredoc lines have been concatenated with NL
9661 // characters in get_function_body()
9662 line = compile_script(line, &cctx);
9663 break;
9664
9665 default:
9666 // Not recognized, execute with do_cmdline_cmd().
9667 ea.arg = p;
9668 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009669 break;
9670 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009671nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009672 if (line == NULL)
9673 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009674 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009675
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009676 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009677 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009679 if (cctx.ctx_type_stack.ga_len < 0)
9680 {
9681 iemsg("Type stack underflow");
9682 goto erret;
9683 }
9684 }
9685
9686 if (cctx.ctx_scope != NULL)
9687 {
9688 if (cctx.ctx_scope->se_type == IF_SCOPE)
9689 emsg(_(e_endif));
9690 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9691 emsg(_(e_endwhile));
9692 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9693 emsg(_(e_endfor));
9694 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009695 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009696 goto erret;
9697 }
9698
Bram Moolenaarefd88552020-06-18 20:50:10 +02009699 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009700 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009701 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9702 ufunc->uf_ret_type = &t_void;
9703 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009704 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009705 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009706 goto erret;
9707 }
9708
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009709 // Return void if there is no return at the end.
9710 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009711 }
9712
Bram Moolenaar599410c2021-04-10 14:03:43 +02009713 // When compiled with ":silent!" and there was an error don't consider the
9714 // function compiled.
9715 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009716 {
9717 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9718 + ufunc->uf_dfunc_idx;
9719 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009720 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009721#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009722 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009723 {
9724 dfunc->df_instr_prof = instr->ga_data;
9725 dfunc->df_instr_prof_count = instr->ga_len;
9726 }
9727 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009728#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009729 if (cctx.ctx_compile_type == CT_DEBUG)
9730 {
9731 dfunc->df_instr_debug = instr->ga_data;
9732 dfunc->df_instr_debug_count = instr->ga_len;
9733 }
9734 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009735 {
9736 dfunc->df_instr = instr->ga_data;
9737 dfunc->df_instr_count = instr->ga_len;
9738 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009739 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009740 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009741 if (cctx.ctx_outer_used)
9742 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009743 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009745
9746 ret = OK;
9747
9748erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009749 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009750 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009751 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9752 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009753
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009754 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009755 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009756 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009757 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009758
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009759 // If using the last entry in the table and it was added above, we
9760 // might as well remove it.
9761 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009762 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009763 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009764 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009765 ufunc->uf_dfunc_idx = 0;
9766 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009767 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009768
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009769 while (cctx.ctx_scope != NULL)
9770 drop_scope(&cctx);
9771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009772 if (errormsg != NULL)
9773 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009774 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009775 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009776 }
9777
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009778 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9779 {
9780 if (ret == OK)
9781 {
9782 emsg(_(e_missing_redir_end));
9783 ret = FAIL;
9784 }
9785 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009786 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009787 }
9788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009789 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009790 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009791 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009792 if (do_estack_push)
9793 estack_pop();
9794
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009795 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009796 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009797 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009798 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009799 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009800}
9801
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009802 void
9803set_function_type(ufunc_T *ufunc)
9804{
9805 int varargs = ufunc->uf_va_name != NULL;
9806 int argcount = ufunc->uf_args.ga_len;
9807
9808 // Create a type for the function, with the return type and any
9809 // argument types.
9810 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9811 // The type is included in "tt_args".
9812 if (argcount > 0 || varargs)
9813 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009814 if (ufunc->uf_type_list.ga_itemsize == 0)
9815 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009816 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9817 argcount, &ufunc->uf_type_list);
9818 // Add argument types to the function type.
9819 if (func_type_add_arg_types(ufunc->uf_func_type,
9820 argcount + varargs,
9821 &ufunc->uf_type_list) == FAIL)
9822 return;
9823 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9824 ufunc->uf_func_type->tt_min_argcount =
9825 argcount - ufunc->uf_def_args.ga_len;
9826 if (ufunc->uf_arg_types == NULL)
9827 {
9828 int i;
9829
9830 // lambda does not have argument types.
9831 for (i = 0; i < argcount; ++i)
9832 ufunc->uf_func_type->tt_args[i] = &t_any;
9833 }
9834 else
9835 mch_memmove(ufunc->uf_func_type->tt_args,
9836 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9837 if (varargs)
9838 {
9839 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009840 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009841 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9842 }
9843 }
9844 else
9845 // No arguments, can use a predefined type.
9846 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9847 argcount, &ufunc->uf_type_list);
9848}
9849
9850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009851/*
9852 * Delete an instruction, free what it contains.
9853 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009854 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009855delete_instr(isn_T *isn)
9856{
9857 switch (isn->isn_type)
9858 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009859 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009860 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009861 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009862 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009863 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009864 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009865 case ISN_LOADENV:
9866 case ISN_LOADG:
9867 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009868 case ISN_LOADT:
9869 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009870 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009871 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009872 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009873 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009874 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009875 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009876 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009877 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009878 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009879 case ISN_STOREW:
9880 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009881 vim_free(isn->isn_arg.string);
9882 break;
9883
Bram Moolenaar4c137212021-04-19 16:48:48 +02009884 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009885 {
9886 int idx;
9887 isn_T *list = isn->isn_arg.subs.subs_instr;
9888
9889 vim_free(isn->isn_arg.subs.subs_cmd);
9890 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9891 delete_instr(list + idx);
9892 vim_free(list);
9893 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009894 break;
9895
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009896 case ISN_INSTR:
9897 {
9898 int idx;
9899 isn_T *list = isn->isn_arg.instr;
9900
9901 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9902 delete_instr(list + idx);
9903 vim_free(list);
9904 }
9905 break;
9906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009907 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009908 case ISN_STORES:
9909 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009910 break;
9911
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009912 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009913 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009914 vim_free(isn->isn_arg.unlet.ul_name);
9915 break;
9916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009917 case ISN_STOREOPT:
9918 vim_free(isn->isn_arg.storeopt.so_name);
9919 break;
9920
9921 case ISN_PUSHBLOB: // push blob isn_arg.blob
9922 blob_unref(isn->isn_arg.blob);
9923 break;
9924
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009925 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009926#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009927 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009928#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009929 break;
9930
9931 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009932#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009933 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009934#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009935 break;
9936
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009937 case ISN_UCALL:
9938 vim_free(isn->isn_arg.ufunc.cuf_name);
9939 break;
9940
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009941 case ISN_FUNCREF:
9942 {
9943 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9944 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009945 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009946
Bram Moolenaar077a4232020-12-22 18:33:27 +01009947 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9948 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009949 }
9950 break;
9951
9952 case ISN_DCALL:
9953 {
9954 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9955 + isn->isn_arg.dfunc.cdf_idx;
9956
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009957 if (dfunc->df_ufunc != NULL
9958 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009959 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009960 }
9961 break;
9962
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009963 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009964 {
9965 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9966 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9967
9968 if (ufunc != NULL)
9969 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009970 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009971 func_ptr_unref(ufunc);
9972 }
9973
9974 vim_free(lambda);
9975 vim_free(isn->isn_arg.newfunc.nf_global);
9976 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009977 break;
9978
Bram Moolenaar5e654232020-09-16 15:22:00 +02009979 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009980 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009981 free_type(isn->isn_arg.type.ct_type);
9982 break;
9983
Bram Moolenaar02194d22020-10-24 23:08:38 +02009984 case ISN_CMDMOD:
9985 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9986 ->cmod_filter_regmatch.regprog);
9987 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9988 break;
9989
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009990 case ISN_LOADSCRIPT:
9991 case ISN_STORESCRIPT:
9992 vim_free(isn->isn_arg.script.scriptref);
9993 break;
9994
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009995 case ISN_TRY:
9996 vim_free(isn->isn_arg.try.try_ref);
9997 break;
9998
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009999 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010000 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010001 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10002 break;
10003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010004 case ISN_2BOOL:
10005 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010006 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010007 case ISN_ADDBLOB:
10008 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010009 case ISN_ANYINDEX:
10010 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010011 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010012 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010013 case ISN_BLOBINDEX:
10014 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010015 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010016 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010017 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010018 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010019 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010020 case ISN_COMPAREANY:
10021 case ISN_COMPAREBLOB:
10022 case ISN_COMPAREBOOL:
10023 case ISN_COMPAREDICT:
10024 case ISN_COMPAREFLOAT:
10025 case ISN_COMPAREFUNC:
10026 case ISN_COMPARELIST:
10027 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010028 case ISN_COMPARESPECIAL:
10029 case ISN_COMPARESTRING:
10030 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010031 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010032 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010033 case ISN_DROP:
10034 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010035 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010036 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010037 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010038 case ISN_EXECCONCAT:
10039 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010040 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010041 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010042 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010043 case ISN_GETITEM:
10044 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010045 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010046 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010047 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010048 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010049 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010050 case ISN_LOADBDICT:
10051 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010052 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010053 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010054 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010055 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010056 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010057 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010058 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010059 case ISN_NEGATENR:
10060 case ISN_NEWDICT:
10061 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010062 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010063 case ISN_OPFLOAT:
10064 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010065 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010066 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010067 case ISN_PROF_END:
10068 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010069 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010070 case ISN_PUSHF:
10071 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010072 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010073 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010074 case ISN_REDIREND:
10075 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010076 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010077 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010078 case ISN_SHUFFLE:
10079 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010080 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010081 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010082 case ISN_STORENR:
10083 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010084 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010085 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010086 case ISN_STOREV:
10087 case ISN_STRINDEX:
10088 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010089 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010090 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010091 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010092 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010093 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010094 // nothing allocated
10095 break;
10096 }
10097}
10098
10099/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010100 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010101 */
10102 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010103delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010104{
10105 int idx;
10106
10107 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010108 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010109
10110 if (dfunc->df_instr != NULL)
10111 {
10112 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10113 delete_instr(dfunc->df_instr + idx);
10114 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010115 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010116 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010117 if (dfunc->df_instr_debug != NULL)
10118 {
10119 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10120 delete_instr(dfunc->df_instr_debug + idx);
10121 VIM_CLEAR(dfunc->df_instr_debug);
10122 dfunc->df_instr_debug = NULL;
10123 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010124#ifdef FEAT_PROFILE
10125 if (dfunc->df_instr_prof != NULL)
10126 {
10127 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10128 delete_instr(dfunc->df_instr_prof + idx);
10129 VIM_CLEAR(dfunc->df_instr_prof);
10130 dfunc->df_instr_prof = NULL;
10131 }
10132#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010133
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010134 if (mark_deleted)
10135 dfunc->df_deleted = TRUE;
10136 if (dfunc->df_ufunc != NULL)
10137 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010138}
10139
10140/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010141 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010142 * function, unless another user function still uses it.
10143 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010144 */
10145 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010146unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010147{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010148 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010149 {
10150 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10151 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010152
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010153 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010154 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010155 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010156 ufunc->uf_dfunc_idx = 0;
10157 if (dfunc->df_ufunc == ufunc)
10158 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010159 }
10160}
10161
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010162/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010163 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010164 */
10165 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010166link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010167{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010168 if (ufunc->uf_dfunc_idx > 0)
10169 {
10170 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10171 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010172
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010173 ++dfunc->df_refcount;
10174 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010175}
10176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010177#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010178/*
10179 * Free all functions defined with ":def".
10180 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010181 void
10182free_def_functions(void)
10183{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010184 int idx;
10185
10186 for (idx = 0; idx < def_functions.ga_len; ++idx)
10187 {
10188 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10189
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010190 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010191 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010192 }
10193
10194 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010195}
10196#endif
10197
10198
10199#endif // FEAT_EVAL