blob: 663a52dc7b5bd5c8f6fcd8e190fab371df6d881f [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".
1243 */
1244 static int
1245generate_GETITEM(cctx_T *cctx, int index)
1246{
1247 isn_T *isn;
1248 garray_T *stack = &cctx->ctx_type_stack;
1249 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1250 type_T *item_type = &t_any;
1251
1252 RETURN_OK_IF_SKIP(cctx);
1253
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001254 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001255 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001256 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001257 emsg(_(e_listreq));
1258 return FAIL;
1259 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001260 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001261 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1262 return FAIL;
1263 isn->isn_arg.number = index;
1264
1265 // add the item type to the type stack
1266 if (ga_grow(stack, 1) == FAIL)
1267 return FAIL;
1268 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1269 ++stack->ga_len;
1270 return OK;
1271}
1272
1273/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001274 * Generate an ISN_SLICE instruction with "count".
1275 */
1276 static int
1277generate_SLICE(cctx_T *cctx, int count)
1278{
1279 isn_T *isn;
1280
1281 RETURN_OK_IF_SKIP(cctx);
1282 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1283 return FAIL;
1284 isn->isn_arg.number = count;
1285 return OK;
1286}
1287
1288/*
1289 * Generate an ISN_CHECKLEN instruction with "min_len".
1290 */
1291 static int
1292generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1293{
1294 isn_T *isn;
1295
1296 RETURN_OK_IF_SKIP(cctx);
1297
1298 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1299 return FAIL;
1300 isn->isn_arg.checklen.cl_min_len = min_len;
1301 isn->isn_arg.checklen.cl_more_OK = more_OK;
1302
1303 return OK;
1304}
1305
1306/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 * Generate an ISN_STORE instruction.
1308 */
1309 static int
1310generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1311{
1312 isn_T *isn;
1313
Bram Moolenaar080457c2020-03-03 21:53:32 +01001314 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1316 return FAIL;
1317 if (name != NULL)
1318 isn->isn_arg.string = vim_strsave(name);
1319 else
1320 isn->isn_arg.number = idx;
1321
1322 return OK;
1323}
1324
1325/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001326 * Generate an ISN_STOREOUTER instruction.
1327 */
1328 static int
1329generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1330{
1331 isn_T *isn;
1332
1333 RETURN_OK_IF_SKIP(cctx);
1334 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1335 return FAIL;
1336 isn->isn_arg.outer.outer_idx = idx;
1337 isn->isn_arg.outer.outer_depth = level;
1338
1339 return OK;
1340}
1341
1342/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1344 */
1345 static int
1346generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1347{
1348 isn_T *isn;
1349
Bram Moolenaar080457c2020-03-03 21:53:32 +01001350 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1352 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001353 isn->isn_arg.storenr.stnr_idx = idx;
1354 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355
1356 return OK;
1357}
1358
1359/*
1360 * Generate an ISN_STOREOPT instruction
1361 */
1362 static int
1363generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1364{
1365 isn_T *isn;
1366
Bram Moolenaar080457c2020-03-03 21:53:32 +01001367 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001368 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 return FAIL;
1370 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1371 isn->isn_arg.storeopt.so_flags = opt_flags;
1372
1373 return OK;
1374}
1375
1376/*
1377 * Generate an ISN_LOAD or similar instruction.
1378 */
1379 static int
1380generate_LOAD(
1381 cctx_T *cctx,
1382 isntype_T isn_type,
1383 int idx,
1384 char_u *name,
1385 type_T *type)
1386{
1387 isn_T *isn;
1388
Bram Moolenaar080457c2020-03-03 21:53:32 +01001389 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1391 return FAIL;
1392 if (name != NULL)
1393 isn->isn_arg.string = vim_strsave(name);
1394 else
1395 isn->isn_arg.number = idx;
1396
1397 return OK;
1398}
1399
1400/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001401 * Generate an ISN_LOADOUTER instruction
1402 */
1403 static int
1404generate_LOADOUTER(
1405 cctx_T *cctx,
1406 int idx,
1407 int nesting,
1408 type_T *type)
1409{
1410 isn_T *isn;
1411
1412 RETURN_OK_IF_SKIP(cctx);
1413 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1414 return FAIL;
1415 isn->isn_arg.outer.outer_idx = idx;
1416 isn->isn_arg.outer.outer_depth = nesting;
1417
1418 return OK;
1419}
1420
1421/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001422 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001423 */
1424 static int
1425generate_LOADV(
1426 cctx_T *cctx,
1427 char_u *name,
1428 int error)
1429{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001430 int di_flags;
1431 int vidx = find_vim_var(name, &di_flags);
1432 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001433
Bram Moolenaar080457c2020-03-03 21:53:32 +01001434 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001435 if (vidx < 0)
1436 {
1437 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001438 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001439 return FAIL;
1440 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001441 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001442
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001443 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001444}
1445
1446/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001447 * Generate an ISN_UNLET instruction.
1448 */
1449 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001450generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001451{
1452 isn_T *isn;
1453
1454 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001455 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001456 return FAIL;
1457 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1458 isn->isn_arg.unlet.ul_forceit = forceit;
1459
1460 return OK;
1461}
1462
1463/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001464 * Generate an ISN_LOCKCONST instruction.
1465 */
1466 static int
1467generate_LOCKCONST(cctx_T *cctx)
1468{
1469 isn_T *isn;
1470
1471 RETURN_OK_IF_SKIP(cctx);
1472 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1473 return FAIL;
1474 return OK;
1475}
1476
1477/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 * Generate an ISN_LOADS instruction.
1479 */
1480 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001481generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001483 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001485 int sid,
1486 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487{
1488 isn_T *isn;
1489
Bram Moolenaar080457c2020-03-03 21:53:32 +01001490 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001491 if (isn_type == ISN_LOADS)
1492 isn = generate_instr_type(cctx, isn_type, type);
1493 else
1494 isn = generate_instr_drop(cctx, isn_type, 1);
1495 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001497 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1498 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499
1500 return OK;
1501}
1502
1503/*
1504 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1505 */
1506 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001507generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 cctx_T *cctx,
1509 isntype_T isn_type,
1510 int sid,
1511 int idx,
1512 type_T *type)
1513{
1514 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001515 scriptref_T *sref;
1516 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517
Bram Moolenaar080457c2020-03-03 21:53:32 +01001518 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 if (isn_type == ISN_LOADSCRIPT)
1520 isn = generate_instr_type(cctx, isn_type, type);
1521 else
1522 isn = generate_instr_drop(cctx, isn_type, 1);
1523 if (isn == NULL)
1524 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001525
1526 // This requires three arguments, which doesn't fit in an instruction, thus
1527 // we need to allocate a struct for this.
1528 sref = ALLOC_ONE(scriptref_T);
1529 if (sref == NULL)
1530 return FAIL;
1531 isn->isn_arg.script.scriptref = sref;
1532 sref->sref_sid = sid;
1533 sref->sref_idx = idx;
1534 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001535 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 return OK;
1537}
1538
1539/*
1540 * Generate an ISN_NEWLIST instruction.
1541 */
1542 static int
1543generate_NEWLIST(cctx_T *cctx, int count)
1544{
1545 isn_T *isn;
1546 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 type_T *type;
1548 type_T *member;
1549
Bram Moolenaar080457c2020-03-03 21:53:32 +01001550 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1552 return FAIL;
1553 isn->isn_arg.number = count;
1554
Bram Moolenaar127542b2020-08-09 17:22:04 +02001555 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001556 if (count == 0)
1557 member = &t_void;
1558 else
1559 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001560 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1561 cctx->ctx_type_list);
1562 type = get_list_type(member, cctx->ctx_type_list);
1563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 // drop the value types
1565 stack->ga_len -= count;
1566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567 // add the list type to the type stack
1568 if (ga_grow(stack, 1) == FAIL)
1569 return FAIL;
1570 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1571 ++stack->ga_len;
1572
1573 return OK;
1574}
1575
1576/*
1577 * Generate an ISN_NEWDICT instruction.
1578 */
1579 static int
1580generate_NEWDICT(cctx_T *cctx, int count)
1581{
1582 isn_T *isn;
1583 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 type_T *type;
1585 type_T *member;
1586
Bram Moolenaar080457c2020-03-03 21:53:32 +01001587 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1589 return FAIL;
1590 isn->isn_arg.number = count;
1591
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001592 if (count == 0)
1593 member = &t_void;
1594 else
1595 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001596 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1597 cctx->ctx_type_list);
1598 type = get_dict_type(member, cctx->ctx_type_list);
1599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 // drop the key and value types
1601 stack->ga_len -= 2 * count;
1602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 // add the dict type to the type stack
1604 if (ga_grow(stack, 1) == FAIL)
1605 return FAIL;
1606 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1607 ++stack->ga_len;
1608
1609 return OK;
1610}
1611
1612/*
1613 * Generate an ISN_FUNCREF instruction.
1614 */
1615 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001616generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617{
1618 isn_T *isn;
1619 garray_T *stack = &cctx->ctx_type_stack;
1620
Bram Moolenaar080457c2020-03-03 21:53:32 +01001621 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1623 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001624 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001625 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001627 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001628 // the nested context, including this one.
1629 if (ufunc->uf_flags & FC_CLOSURE)
1630 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 if (ga_grow(stack, 1) == FAIL)
1633 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001634 ((type_T **)stack->ga_data)[stack->ga_len] =
1635 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 ++stack->ga_len;
1637
1638 return OK;
1639}
1640
1641/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001642 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001643 * "lambda_name" and "func_name" must be in allocated memory and will be
1644 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001645 */
1646 static int
1647generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1648{
1649 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001650
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001651 if (cctx->ctx_skip == SKIP_YES)
1652 {
1653 vim_free(lambda_name);
1654 vim_free(func_name);
1655 return OK;
1656 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001657 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001658 {
1659 vim_free(lambda_name);
1660 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001661 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001662 }
1663 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001664 isn->isn_arg.newfunc.nf_global = func_name;
1665
1666 return OK;
1667}
1668
1669/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001670 * Generate an ISN_DEF instruction: list functions
1671 */
1672 static int
1673generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1674{
1675 isn_T *isn;
1676
1677 RETURN_OK_IF_SKIP(cctx);
1678 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1679 return FAIL;
1680 if (len > 0)
1681 {
1682 isn->isn_arg.string = vim_strnsave(name, len);
1683 if (isn->isn_arg.string == NULL)
1684 return FAIL;
1685 }
1686 return OK;
1687}
1688
1689/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 * Generate an ISN_JUMP instruction.
1691 */
1692 static int
1693generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1694{
1695 isn_T *isn;
1696 garray_T *stack = &cctx->ctx_type_stack;
1697
Bram Moolenaar080457c2020-03-03 21:53:32 +01001698 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1700 return FAIL;
1701 isn->isn_arg.jump.jump_when = when;
1702 isn->isn_arg.jump.jump_where = where;
1703
1704 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1705 --stack->ga_len;
1706
1707 return OK;
1708}
1709
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001710/*
1711 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1712 */
1713 static int
1714generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1715{
1716 isn_T *isn;
1717
1718 RETURN_OK_IF_SKIP(cctx);
1719 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1720 return FAIL;
1721 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1722 // jump_where is set later
1723 return OK;
1724}
1725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 static int
1727generate_FOR(cctx_T *cctx, int loop_idx)
1728{
1729 isn_T *isn;
1730 garray_T *stack = &cctx->ctx_type_stack;
1731
Bram Moolenaar080457c2020-03-03 21:53:32 +01001732 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1734 return FAIL;
1735 isn->isn_arg.forloop.for_idx = loop_idx;
1736
1737 if (ga_grow(stack, 1) == FAIL)
1738 return FAIL;
1739 // type doesn't matter, will be stored next
1740 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1741 ++stack->ga_len;
1742
1743 return OK;
1744}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001745/*
1746 * Generate an ISN_TRYCONT instruction.
1747 */
1748 static int
1749generate_TRYCONT(cctx_T *cctx, int levels, int where)
1750{
1751 isn_T *isn;
1752
1753 RETURN_OK_IF_SKIP(cctx);
1754 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1755 return FAIL;
1756 isn->isn_arg.trycont.tct_levels = levels;
1757 isn->isn_arg.trycont.tct_where = where;
1758
1759 return OK;
1760}
1761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762
1763/*
1764 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001765 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766 * Return FAIL if the number of arguments is wrong.
1767 */
1768 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001769generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770{
1771 isn_T *isn;
1772 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001773 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001774 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001775 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776
Bram Moolenaar080457c2020-03-03 21:53:32 +01001777 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001778 argoff = check_internal_func(func_idx, argcount);
1779 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 return FAIL;
1781
Bram Moolenaar389df252020-07-09 21:20:47 +02001782 if (method_call && argoff > 1)
1783 {
1784 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1785 return FAIL;
1786 isn->isn_arg.shuffle.shfl_item = argcount;
1787 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1788 }
1789
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001790 if (argcount > 0)
1791 {
1792 // Check the types of the arguments.
1793 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001794 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1795 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001796 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001797 if (internal_func_is_map(func_idx))
1798 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001799 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1802 return FAIL;
1803 isn->isn_arg.bfunc.cbf_idx = func_idx;
1804 isn->isn_arg.bfunc.cbf_argcount = argcount;
1805
Bram Moolenaar94738d82020-10-21 14:25:07 +02001806 // Drop the argument types and push the return type.
1807 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 if (ga_grow(stack, 1) == FAIL)
1809 return FAIL;
1810 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001811 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001812 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001814 if (maptype != NULL && maptype->tt_member != NULL
1815 && maptype->tt_member != &t_any)
1816 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001817 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 return OK;
1820}
1821
1822/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001823 * Generate an ISN_LISTAPPEND instruction. Works like add().
1824 * Argument count is already checked.
1825 */
1826 static int
1827generate_LISTAPPEND(cctx_T *cctx)
1828{
1829 garray_T *stack = &cctx->ctx_type_stack;
1830 type_T *list_type;
1831 type_T *item_type;
1832 type_T *expected;
1833
1834 // Caller already checked that list_type is a list.
1835 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1836 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1837 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001838 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001839 return FAIL;
1840
1841 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1842 return FAIL;
1843
1844 --stack->ga_len; // drop the argument
1845 return OK;
1846}
1847
1848/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001849 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1850 * Argument count is already checked.
1851 */
1852 static int
1853generate_BLOBAPPEND(cctx_T *cctx)
1854{
1855 garray_T *stack = &cctx->ctx_type_stack;
1856 type_T *item_type;
1857
1858 // Caller already checked that blob_type is a blob.
1859 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001860 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001861 return FAIL;
1862
1863 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1864 return FAIL;
1865
1866 --stack->ga_len; // drop the argument
1867 return OK;
1868}
1869
1870/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001871 * Return TRUE if "ufunc" should be compiled, taking into account whether
1872 * "profile" indicates profiling is to be done.
1873 */
1874 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001875func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001876{
1877 switch (ufunc->uf_def_status)
1878 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001879 case UF_TO_BE_COMPILED:
1880 return TRUE;
1881
Bram Moolenaarb2049902021-01-24 12:53:53 +01001882 case UF_COMPILED:
1883 {
1884 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1885 + ufunc->uf_dfunc_idx;
1886
Bram Moolenaare99d4222021-06-13 14:01:26 +02001887 switch (compile_type)
1888 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001889 case CT_PROFILE:
1890#ifdef FEAT_PROFILE
1891 return dfunc->df_instr_prof == NULL;
1892#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001893 case CT_NONE:
1894 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001895 case CT_DEBUG:
1896 return dfunc->df_instr_debug == NULL;
1897 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001898 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001899
1900 case UF_NOT_COMPILED:
1901 case UF_COMPILE_ERROR:
1902 case UF_COMPILING:
1903 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001904 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001905 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001906}
1907
1908/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 * Generate an ISN_DCALL or ISN_UCALL instruction.
1910 * Return FAIL if the number of arguments is wrong.
1911 */
1912 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001913generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914{
1915 isn_T *isn;
1916 garray_T *stack = &cctx->ctx_type_stack;
1917 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001918 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919
Bram Moolenaar080457c2020-03-03 21:53:32 +01001920 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 if (argcount > regular_args && !has_varargs(ufunc))
1922 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001923 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 return FAIL;
1925 }
1926 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1927 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001928 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 return FAIL;
1930 }
1931
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001932 if (ufunc->uf_def_status != UF_NOT_COMPILED
1933 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001934 {
1935 int i;
1936
1937 for (i = 0; i < argcount; ++i)
1938 {
1939 type_T *expected;
1940 type_T *actual;
1941
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001942 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1943 if (actual == &t_special
1944 && i >= regular_args - ufunc->uf_def_args.ga_len)
1945 {
1946 // assume v:none used for default argument value
1947 continue;
1948 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001949 if (i < regular_args)
1950 {
1951 if (ufunc->uf_arg_types == NULL)
1952 continue;
1953 expected = ufunc->uf_arg_types[i];
1954 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001955 else if (ufunc->uf_va_type == NULL
1956 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001957 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001958 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001959 else
1960 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001961 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001962 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001963 {
1964 arg_type_mismatch(expected, actual, i + 1);
1965 return FAIL;
1966 }
1967 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02001968 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001969 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02001970 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001971 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001972 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02001973 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1974 {
1975 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1976 ufunc->uf_name);
1977 return FAIL;
1978 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001981 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001982 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001984 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 {
1986 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1987 isn->isn_arg.dfunc.cdf_argcount = argcount;
1988 }
1989 else
1990 {
1991 // A user function may be deleted and redefined later, can't use the
1992 // ufunc pointer, need to look it up again at runtime.
1993 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1994 isn->isn_arg.ufunc.cuf_argcount = argcount;
1995 }
1996
1997 stack->ga_len -= argcount; // drop the arguments
1998 if (ga_grow(stack, 1) == FAIL)
1999 return FAIL;
2000 // add return value
2001 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2002 ++stack->ga_len;
2003
2004 return OK;
2005}
2006
2007/*
2008 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2009 */
2010 static int
2011generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2012{
2013 isn_T *isn;
2014 garray_T *stack = &cctx->ctx_type_stack;
2015
Bram Moolenaar080457c2020-03-03 21:53:32 +01002016 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2018 return FAIL;
2019 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2020 isn->isn_arg.ufunc.cuf_argcount = argcount;
2021
2022 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002023 if (ga_grow(stack, 1) == FAIL)
2024 return FAIL;
2025 // add return value
2026 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2027 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028
2029 return OK;
2030}
2031
2032/*
2033 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002034 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 */
2036 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002037generate_PCALL(
2038 cctx_T *cctx,
2039 int argcount,
2040 char_u *name,
2041 type_T *type,
2042 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043{
2044 isn_T *isn;
2045 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002046 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047
Bram Moolenaar080457c2020-03-03 21:53:32 +01002048 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002049
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002050 if (type->tt_type == VAR_ANY)
2051 ret_type = &t_any;
2052 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002053 {
2054 if (type->tt_argcount != -1)
2055 {
2056 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2057
2058 if (argcount < type->tt_min_argcount - varargs)
2059 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002060 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002061 return FAIL;
2062 }
2063 if (!varargs && argcount > type->tt_argcount)
2064 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002065 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002066 return FAIL;
2067 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002068 if (type->tt_args != NULL)
2069 {
2070 int i;
2071
2072 for (i = 0; i < argcount; ++i)
2073 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002074 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002075 type_T *actual = ((type_T **)stack->ga_data)[
2076 stack->ga_len + offset];
2077 type_T *expected;
2078
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002079 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002080 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002081 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002082 else if (i >= type->tt_min_argcount
2083 && actual == &t_special)
2084 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002085 else
2086 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002087 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002088 cctx, TRUE, FALSE) == FAIL)
2089 {
2090 arg_type_mismatch(expected, actual, i + 1);
2091 return FAIL;
2092 }
2093 }
2094 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002095 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002096 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002097 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002098 else
2099 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002100 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002101 return FAIL;
2102 }
2103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2105 return FAIL;
2106 isn->isn_arg.pfunc.cpf_top = at_top;
2107 isn->isn_arg.pfunc.cpf_argcount = argcount;
2108
2109 stack->ga_len -= argcount; // drop the arguments
2110
2111 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002112 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002114 // If partial is above the arguments it must be cleared and replaced with
2115 // the return value.
2116 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2117 return FAIL;
2118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 return OK;
2120}
2121
2122/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002123 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 */
2125 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002126generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127{
2128 isn_T *isn;
2129 garray_T *stack = &cctx->ctx_type_stack;
2130 type_T *type;
2131
Bram Moolenaar080457c2020-03-03 21:53:32 +01002132 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002133 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002135 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002137 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002139 if (type->tt_type != VAR_DICT && type != &t_any)
2140 {
2141 emsg(_(e_dictreq));
2142 return FAIL;
2143 }
2144 // change dict type to dict member type
2145 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002146 {
2147 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2148 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150
2151 return OK;
2152}
2153
2154/*
2155 * Generate an ISN_ECHO instruction.
2156 */
2157 static int
2158generate_ECHO(cctx_T *cctx, int with_white, int count)
2159{
2160 isn_T *isn;
2161
Bram Moolenaar080457c2020-03-03 21:53:32 +01002162 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2164 return FAIL;
2165 isn->isn_arg.echo.echo_with_white = with_white;
2166 isn->isn_arg.echo.echo_count = count;
2167
2168 return OK;
2169}
2170
Bram Moolenaarad39c092020-02-26 18:23:43 +01002171/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002172 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002173 */
2174 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002175generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002176{
2177 isn_T *isn;
2178
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002179 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002180 return FAIL;
2181 isn->isn_arg.number = count;
2182
2183 return OK;
2184}
2185
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002186/*
2187 * Generate an ISN_PUT instruction.
2188 */
2189 static int
2190generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2191{
2192 isn_T *isn;
2193
2194 RETURN_OK_IF_SKIP(cctx);
2195 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2196 return FAIL;
2197 isn->isn_arg.put.put_regname = regname;
2198 isn->isn_arg.put.put_lnum = lnum;
2199 return OK;
2200}
2201
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 static int
2203generate_EXEC(cctx_T *cctx, char_u *line)
2204{
2205 isn_T *isn;
2206
Bram Moolenaar080457c2020-03-03 21:53:32 +01002207 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2209 return FAIL;
2210 isn->isn_arg.string = vim_strsave(line);
2211 return OK;
2212}
2213
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002214 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002215generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2216{
2217 isn_T *isn;
2218 garray_T *stack = &cctx->ctx_type_stack;
2219
2220 RETURN_OK_IF_SKIP(cctx);
2221 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2222 return FAIL;
2223 isn->isn_arg.string = vim_strsave(line);
2224
2225 if (ga_grow(stack, 1) == FAIL)
2226 return FAIL;
2227 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2228 ++stack->ga_len;
2229
2230 return OK;
2231}
2232
2233 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002234generate_EXECCONCAT(cctx_T *cctx, int count)
2235{
2236 isn_T *isn;
2237
2238 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2239 return FAIL;
2240 isn->isn_arg.number = count;
2241 return OK;
2242}
2243
Bram Moolenaar08597872020-12-10 19:43:40 +01002244/*
2245 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2246 */
2247 static int
2248generate_RANGE(cctx_T *cctx, char_u *range)
2249{
2250 isn_T *isn;
2251 garray_T *stack = &cctx->ctx_type_stack;
2252
2253 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2254 return FAIL;
2255 isn->isn_arg.string = range;
2256
2257 if (ga_grow(stack, 1) == FAIL)
2258 return FAIL;
2259 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2260 ++stack->ga_len;
2261 return OK;
2262}
2263
Bram Moolenaar792f7862020-11-23 08:31:18 +01002264 static int
2265generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2266{
2267 isn_T *isn;
2268
2269 RETURN_OK_IF_SKIP(cctx);
2270 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2271 return FAIL;
2272 isn->isn_arg.unpack.unp_count = var_count;
2273 isn->isn_arg.unpack.unp_semicolon = semicolon;
2274 return OK;
2275}
2276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002278 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002279 */
2280 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002281generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002282{
2283 isn_T *isn;
2284
Bram Moolenaarfa984412021-03-25 22:15:28 +01002285 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002286 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002287 cctx->ctx_has_cmdmod = TRUE;
2288
2289 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002290 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002291 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2292 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2293 return FAIL;
2294 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002295 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002296 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002297 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002298
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002299 return OK;
2300}
2301
2302 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002303generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002304{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002305 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2306 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002307 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002308 return OK;
2309}
2310
Bram Moolenaarfa984412021-03-25 22:15:28 +01002311 static int
2312misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002313{
2314 garray_T *instr = &cctx->ctx_instr;
2315
Bram Moolenaara91a7132021-03-25 21:12:15 +01002316 if (cctx->ctx_has_cmdmod
2317 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2318 == ISN_CMDMOD)
2319 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002320 emsg(_(e_misplaced_command_modifier));
2321 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002322 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002323 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002324}
2325
2326/*
2327 * Get the index of the current instruction.
2328 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2329 */
2330 static int
2331current_instr_idx(cctx_T *cctx)
2332{
2333 garray_T *instr = &cctx->ctx_instr;
2334 int idx = instr->ga_len;
2335
Bram Moolenaare99d4222021-06-13 14:01:26 +02002336 while (idx > 0)
2337 {
2338 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002339 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002340 {
2341 --idx;
2342 continue;
2343 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002344#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002345 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2346 {
2347 --idx;
2348 continue;
2349 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002350#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002351 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2352 {
2353 --idx;
2354 continue;
2355 }
2356 break;
2357 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002358 return idx;
2359}
2360
Bram Moolenaarf002a412021-01-24 13:34:18 +01002361#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002362 static void
2363may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2364{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002365 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002366 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002367}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002368#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002369
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002370/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002372 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002374 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002375reserve_local(
2376 cctx_T *cctx,
2377 char_u *name,
2378 size_t len,
2379 int isConst,
2380 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002383 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002385 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002387 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002388 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 }
2390
2391 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002392 return NULL;
2393 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002394 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002396 // Every local variable uses the next entry on the stack. We could re-use
2397 // the last ones when leaving a scope, but then variables used in a closure
2398 // might get overwritten. To keep things simple do not re-use stack
2399 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002400 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2401 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002402
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002403 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 lvar->lv_const = isConst;
2405 lvar->lv_type = type;
2406
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002407 // Remember the name for debugging.
2408 if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2409 return NULL;
2410 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2411 vim_strsave(lvar->lv_name);
2412 ++dfunc->df_var_names.ga_len;
2413
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002414 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415}
2416
2417/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002418 * Remove local variables above "new_top".
2419 */
2420 static void
2421unwind_locals(cctx_T *cctx, int new_top)
2422{
2423 if (cctx->ctx_locals.ga_len > new_top)
2424 {
2425 int idx;
2426 lvar_T *lvar;
2427
2428 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2429 {
2430 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2431 vim_free(lvar->lv_name);
2432 }
2433 }
2434 cctx->ctx_locals.ga_len = new_top;
2435}
2436
2437/*
2438 * Free all local variables.
2439 */
2440 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002441free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002442{
2443 unwind_locals(cctx, 0);
2444 ga_clear(&cctx->ctx_locals);
2445}
2446
2447/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002448 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2449 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2450 * error if the variable was defined with :const.
2451 */
2452 static int
2453check_item_writable(svar_T *sv, int check_writable, char_u *name)
2454{
2455 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2456 || (check_writable == ASSIGN_FINAL
2457 && sv->sv_const == ASSIGN_CONST))
2458 {
2459 semsg(_(e_readonlyvar), name);
2460 return FAIL;
2461 }
2462 return OK;
2463}
2464
2465/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002467 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 * Returns the index in "sn_var_vals" if found.
2469 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002470 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 */
2472 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002473get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474{
2475 hashtab_T *ht;
2476 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002477 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002478 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 int idx;
2480
Bram Moolenaare3d46852020-08-29 13:39:17 +02002481 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002483 if (sid == current_sctx.sc_sid)
2484 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002485 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002486
2487 if (sav == NULL)
2488 return -2;
2489 idx = sav->sav_var_vals_idx;
2490 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002491 if (check_item_writable(sv, check_writable, name) == FAIL)
2492 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002493 return idx;
2494 }
2495
2496 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 ht = &SCRIPT_VARS(sid);
2498 di = find_var_in_ht(ht, 0, name, TRUE);
2499 if (di == NULL)
2500 return -2;
2501
2502 // Now find the svar_T index in sn_var_vals.
2503 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2504 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002505 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 if (sv->sv_tv == &di->di_tv)
2507 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002508 if (check_item_writable(sv, check_writable, name) == FAIL)
2509 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 return idx;
2511 }
2512 }
2513 return -1;
2514}
2515
2516/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002517 * Find "name" in imported items of the current script or in "cctx" if not
2518 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 */
2520 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002521find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 int idx;
2524
Bram Moolenaare3d46852020-08-29 13:39:17 +02002525 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002526 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 if (cctx != NULL)
2528 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2529 {
2530 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2531 + idx;
2532
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002533 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2534 : STRLEN(import->imp_name) == len
2535 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 return import;
2537 }
2538
Bram Moolenaarefa94442020-08-08 22:16:00 +02002539 return find_imported_in_script(name, len, current_sctx.sc_sid);
2540}
2541
2542 imported_T *
2543find_imported_in_script(char_u *name, size_t len, int sid)
2544{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002545 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002546 int idx;
2547
Bram Moolenaare3d46852020-08-29 13:39:17 +02002548 if (!SCRIPT_ID_VALID(sid))
2549 return NULL;
2550 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2552 {
2553 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2554
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002555 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2556 : STRLEN(import->imp_name) == len
2557 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 return import;
2559 }
2560 return NULL;
2561}
2562
2563/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002564 * Free all imported variables.
2565 */
2566 static void
2567free_imported(cctx_T *cctx)
2568{
2569 int idx;
2570
2571 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2572 {
2573 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2574
2575 vim_free(import->imp_name);
2576 }
2577 ga_clear(&cctx->ctx_imports);
2578}
2579
2580/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002581 * Return a pointer to the next line that isn't empty or only contains a
2582 * comment. Skips over white space.
2583 * Returns NULL if there is none.
2584 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002585 char_u *
2586peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002587{
2588 int lnum = cctx->ctx_lnum;
2589
2590 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2591 {
2592 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002593 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002594
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002595 // ignore NULLs inserted for continuation lines
2596 if (line != NULL)
2597 {
2598 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002599 if (vim9_bad_comment(p))
2600 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002601 if (*p != NUL && !vim9_comment_start(p))
2602 return p;
2603 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002604 }
2605 return NULL;
2606}
2607
2608/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002609 * Called when checking for a following operator at "arg". When the rest of
2610 * the line is empty or only a comment, peek the next line. If there is a next
2611 * line return a pointer to it and set "nextp".
2612 * Otherwise skip over white space.
2613 */
2614 static char_u *
2615may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2616{
2617 char_u *p = skipwhite(arg);
2618
2619 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002620 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002621 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002622 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002623 if (*nextp != NULL)
2624 return *nextp;
2625 }
2626 return p;
2627}
2628
2629/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002630 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002631 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002632 * Returns NULL when at the end.
2633 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002634 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002635next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002636{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002637 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002638
2639 do
2640 {
2641 ++cctx->ctx_lnum;
2642 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002643 {
2644 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002645 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002646 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002647 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002648 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002649 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002650 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002651 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002652 return line;
2653}
2654
2655/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002656 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002657 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002658 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002659 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2660 */
2661 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002662may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002663{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002664 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002665 if (vim9_bad_comment(*arg))
2666 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002667 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002668 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002669 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002670
2671 if (next == NULL)
2672 return FAIL;
2673 *arg = skipwhite(next);
2674 }
2675 return OK;
2676}
2677
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002678/*
2679 * Idem, and give an error when failed.
2680 */
2681 static int
2682may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2683{
2684 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2685 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002686 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002687 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002688 return FAIL;
2689 }
2690 return OK;
2691}
2692
2693
Bram Moolenaara5565e42020-05-09 15:44:01 +02002694// Structure passed between the compile_expr* functions to keep track of
2695// constants that have been parsed but for which no code was produced yet. If
2696// possible expressions on these constants are applied at compile time. If
2697// that is not possible, the code to push the constants needs to be generated
2698// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002699// Using 50 should be more than enough of 5 levels of ().
2700#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002701typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002702 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002703 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002704 int pp_is_const; // all generated code was constants, used for a
2705 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002706} ppconst_T;
2707
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002708static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002709static int compile_expr0(char_u **arg, cctx_T *cctx);
2710static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2711
Bram Moolenaara5565e42020-05-09 15:44:01 +02002712/*
2713 * Generate a PUSH instruction for "tv".
2714 * "tv" will be consumed or cleared.
2715 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2716 */
2717 static int
2718generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2719{
2720 if (tv != NULL)
2721 {
2722 switch (tv->v_type)
2723 {
2724 case VAR_UNKNOWN:
2725 break;
2726 case VAR_BOOL:
2727 generate_PUSHBOOL(cctx, tv->vval.v_number);
2728 break;
2729 case VAR_SPECIAL:
2730 generate_PUSHSPEC(cctx, tv->vval.v_number);
2731 break;
2732 case VAR_NUMBER:
2733 generate_PUSHNR(cctx, tv->vval.v_number);
2734 break;
2735#ifdef FEAT_FLOAT
2736 case VAR_FLOAT:
2737 generate_PUSHF(cctx, tv->vval.v_float);
2738 break;
2739#endif
2740 case VAR_BLOB:
2741 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2742 tv->vval.v_blob = NULL;
2743 break;
2744 case VAR_STRING:
2745 generate_PUSHS(cctx, tv->vval.v_string);
2746 tv->vval.v_string = NULL;
2747 break;
2748 default:
2749 iemsg("constant type not supported");
2750 clear_tv(tv);
2751 return FAIL;
2752 }
2753 tv->v_type = VAR_UNKNOWN;
2754 }
2755 return OK;
2756}
2757
2758/*
2759 * Generate code for any ppconst entries.
2760 */
2761 static int
2762generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2763{
2764 int i;
2765 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002766 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002767
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002768 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002769 for (i = 0; i < ppconst->pp_used; ++i)
2770 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2771 ret = FAIL;
2772 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002773 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002774 return ret;
2775}
2776
2777/*
2778 * Clear ppconst constants. Used when failing.
2779 */
2780 static void
2781clear_ppconst(ppconst_T *ppconst)
2782{
2783 int i;
2784
2785 for (i = 0; i < ppconst->pp_used; ++i)
2786 clear_tv(&ppconst->pp_tv[i]);
2787 ppconst->pp_used = 0;
2788}
2789
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002790/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002791 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002792 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002793 */
2794 static int
2795compile_member(int is_slice, cctx_T *cctx)
2796{
2797 type_T **typep;
2798 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002799 vartype_T vartype;
2800 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002801
Bram Moolenaar261417b2021-05-06 21:04:55 +02002802 // We can index a list, dict and blob. If we don't know the type
2803 // we can use the index value type. If we still don't know use an "ANY"
2804 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002805 typep = ((type_T **)stack->ga_data) + stack->ga_len
2806 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002807 vartype = (*typep)->tt_type;
2808 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002809 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002810 if (*typep == &t_any && idxtype == &t_string)
2811 vartype = VAR_DICT;
2812 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002813 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002814 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002815 return FAIL;
2816 if (is_slice)
2817 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002818 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2819 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002820 FALSE, FALSE) == FAIL)
2821 return FAIL;
2822 }
2823 }
2824
Bram Moolenaar261417b2021-05-06 21:04:55 +02002825 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002826 {
2827 if (is_slice)
2828 {
2829 emsg(_(e_cannot_slice_dictionary));
2830 return FAIL;
2831 }
2832 if ((*typep)->tt_type == VAR_DICT)
2833 {
2834 *typep = (*typep)->tt_member;
2835 if (*typep == &t_unknown)
2836 // empty dict was used
2837 *typep = &t_any;
2838 }
2839 else
2840 {
2841 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2842 FALSE, FALSE) == FAIL)
2843 return FAIL;
2844 *typep = &t_any;
2845 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002846 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002847 return FAIL;
2848 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2849 return FAIL;
2850 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002851 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002852 {
2853 *typep = &t_string;
2854 if ((is_slice
2855 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2856 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2857 return FAIL;
2858 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002859 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002860 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002861 if (is_slice)
2862 {
2863 *typep = &t_blob;
2864 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2865 return FAIL;
2866 }
2867 else
2868 {
2869 *typep = &t_number;
2870 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2871 return FAIL;
2872 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002873 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002874 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002875 {
2876 if (is_slice)
2877 {
2878 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002879 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002880 2) == FAIL)
2881 return FAIL;
2882 }
2883 else
2884 {
2885 if ((*typep)->tt_type == VAR_LIST)
2886 {
2887 *typep = (*typep)->tt_member;
2888 if (*typep == &t_unknown)
2889 // empty list was used
2890 *typep = &t_any;
2891 }
2892 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002893 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2894 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002895 return FAIL;
2896 }
2897 }
2898 else
2899 {
2900 emsg(_(e_string_list_dict_or_blob_required));
2901 return FAIL;
2902 }
2903 return OK;
2904}
2905
2906/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002907 * Generate an instruction to load script-local variable "name", without the
2908 * leading "s:".
2909 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 */
2911 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002912compile_load_scriptvar(
2913 cctx_T *cctx,
2914 char_u *name, // variable NUL terminated
2915 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002916 char_u **end, // end of variable
2917 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002919 scriptitem_T *si;
2920 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 imported_T *import;
2922
Bram Moolenaare3d46852020-08-29 13:39:17 +02002923 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2924 return FAIL;
2925 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002926 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002927 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002929 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002930 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2931 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 }
2933 if (idx >= 0)
2934 {
2935 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2936
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002937 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 current_sctx.sc_sid, idx, sv->sv_type);
2939 return OK;
2940 }
2941
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002942 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 if (import != NULL)
2944 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002945 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002946 {
2947 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002948 char_u *exp_name;
2949 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002950 ufunc_T *ufunc;
2951 type_T *type;
2952
2953 // Used "import * as Name", need to lookup the member.
2954 if (*p != '.')
2955 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002956 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002957 return FAIL;
2958 }
2959 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002960 if (VIM_ISWHITE(*p))
2961 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002962 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002963 return FAIL;
2964 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002965
Bram Moolenaar1c991142020-07-04 13:15:31 +02002966 // isolate one name
2967 exp_name = p;
2968 while (eval_isnamec(*p))
2969 ++p;
2970 cc = *p;
2971 *p = NUL;
2972
Bram Moolenaaredba7072021-03-13 21:14:18 +01002973 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2974 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002975 *p = cc;
2976 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002977 *end = p;
2978
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002979 if (idx < 0)
2980 {
2981 if (*p == '(' && ufunc != NULL)
2982 {
2983 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2984 return OK;
2985 }
2986 return FAIL;
2987 }
2988
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002989 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2990 import->imp_sid,
2991 idx,
2992 type);
2993 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002994 else if (import->imp_funcname != NULL)
2995 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002996 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002997 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2998 import->imp_sid,
2999 import->imp_var_vals_idx,
3000 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 return OK;
3002 }
3003
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003004 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003005 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 return FAIL;
3007}
3008
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003009 static int
3010generate_funcref(cctx_T *cctx, char_u *name)
3011{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003012 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003013
3014 if (ufunc == NULL)
3015 return FAIL;
3016
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003017 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003018 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3019 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003020 == FAIL)
3021 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003022 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003023}
3024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025/*
3026 * Compile a variable name into a load instruction.
3027 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003028 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 * When "error" is FALSE do not give an error when not found.
3030 */
3031 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003032compile_load(
3033 char_u **arg,
3034 char_u *end_arg,
3035 cctx_T *cctx,
3036 int is_expr,
3037 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038{
3039 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003040 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003041 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003043 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044
3045 if (*(*arg + 1) == ':')
3046 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003047 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003048 {
3049 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050
Bram Moolenaarfa596382021-04-07 21:58:16 +02003051 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003052 switch (**arg)
3053 {
3054 case 'g': isn_type = ISN_LOADGDICT; break;
3055 case 'w': isn_type = ISN_LOADWDICT; break;
3056 case 't': isn_type = ISN_LOADTDICT; break;
3057 case 'b': isn_type = ISN_LOADBDICT; break;
3058 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003059 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003060 goto theend;
3061 }
3062 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3063 goto theend;
3064 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 else
3067 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003068 isntype_T isn_type = ISN_DROP;
3069
Bram Moolenaarfa596382021-04-07 21:58:16 +02003070 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003071 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3072 if (name == NULL)
3073 return FAIL;
3074
3075 switch (**arg)
3076 {
3077 case 'v': res = generate_LOADV(cctx, name, error);
3078 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003079 case 's': if (is_expr && ASCII_ISUPPER(*name)
3080 && find_func(name, FALSE, cctx) != NULL)
3081 res = generate_funcref(cctx, name);
3082 else
3083 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003084 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003085 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003086 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003087 {
3088 if (is_expr && ASCII_ISUPPER(*name)
3089 && find_func(name, FALSE, cctx) != NULL)
3090 res = generate_funcref(cctx, name);
3091 else
3092 isn_type = ISN_LOADG;
3093 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003094 else
3095 {
3096 isn_type = ISN_LOADAUTO;
3097 vim_free(name);
3098 name = vim_strnsave(*arg, end - *arg);
3099 if (name == NULL)
3100 return FAIL;
3101 }
3102 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003103 case 'w': isn_type = ISN_LOADW; break;
3104 case 't': isn_type = ISN_LOADT; break;
3105 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003106 default: // cannot happen, just in case
3107 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003108 goto theend;
3109 }
3110 if (isn_type != ISN_DROP)
3111 {
3112 // Global, Buffer-local, Window-local and Tabpage-local
3113 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003114 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003115 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 }
3118 }
3119 else
3120 {
3121 size_t len = end - *arg;
3122 int idx;
3123 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003124 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125
3126 name = vim_strnsave(*arg, end - *arg);
3127 if (name == NULL)
3128 return FAIL;
3129
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003130 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3131 {
3132 script_autoload(name, FALSE);
3133 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3134 }
3135 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3136 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003138 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003139 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 }
3141 else
3142 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003143 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003144
Bram Moolenaar709664c2020-12-12 14:33:41 +01003145 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003147 type = lvar.lv_type;
3148 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003149 if (lvar.lv_from_outer != 0)
3150 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003151 else
3152 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 }
3154 else
3155 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003156 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003157 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003158 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003159 || find_imported(name, 0, cctx) != NULL)
3160 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003161
Bram Moolenaar0f769812020-09-12 18:32:34 +02003162 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003163 // uppercase letter it can be a user defined function.
3164 // generate_funcref() will fail if the function can't be found.
3165 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003166 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 }
3168 }
3169 if (gen_load)
3170 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003171 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003172 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003173 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003174 cctx->ctx_outer_used = TRUE;
3175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 }
3177
3178 *arg = end;
3179
3180theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003181 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003182 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 vim_free(name);
3184 return res;
3185}
3186
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003187 static void
3188clear_instr_ga(garray_T *gap)
3189{
3190 int idx;
3191
3192 for (idx = 0; idx < gap->ga_len; ++idx)
3193 delete_instr(((isn_T *)gap->ga_data) + idx);
3194 ga_clear(gap);
3195}
3196
3197/*
3198 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3199 * Returns FAIL if compilation fails.
3200 */
3201 static int
3202compile_string(isn_T *isn, cctx_T *cctx)
3203{
3204 char_u *s = isn->isn_arg.string;
3205 garray_T save_ga = cctx->ctx_instr;
3206 int expr_res;
3207 int trailing_error;
3208 int instr_count;
3209 isn_T *instr = NULL;
3210
3211 // Temporarily reset the list of instructions so that the jump labels are
3212 // correct.
3213 cctx->ctx_instr.ga_len = 0;
3214 cctx->ctx_instr.ga_maxlen = 0;
3215 cctx->ctx_instr.ga_data = NULL;
3216 expr_res = compile_expr0(&s, cctx);
3217 s = skipwhite(s);
3218 trailing_error = *s != NUL;
3219
Bram Moolenaarff652882021-05-16 15:24:49 +02003220 if (expr_res == FAIL || trailing_error
3221 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003222 {
3223 if (trailing_error)
3224 semsg(_(e_trailing_arg), s);
3225 clear_instr_ga(&cctx->ctx_instr);
3226 cctx->ctx_instr = save_ga;
3227 return FAIL;
3228 }
3229
3230 // Move the generated instructions into the ISN_INSTR instruction, then
3231 // restore the list of instructions.
3232 instr_count = cctx->ctx_instr.ga_len;
3233 instr = cctx->ctx_instr.ga_data;
3234 instr[instr_count].isn_type = ISN_FINISH;
3235
3236 cctx->ctx_instr = save_ga;
3237 vim_free(isn->isn_arg.string);
3238 isn->isn_type = ISN_INSTR;
3239 isn->isn_arg.instr = instr;
3240 return OK;
3241}
3242
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243/*
3244 * Compile the argument expressions.
3245 * "arg" points to just after the "(" and is advanced to after the ")"
3246 */
3247 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003248compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003250 char_u *p = *arg;
3251 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003252 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003253 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254
Bram Moolenaare6085c52020-04-12 20:19:16 +02003255 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003257 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3258 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003259 if (*p == ')')
3260 {
3261 *arg = p + 1;
3262 return OK;
3263 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003264 if (must_end)
3265 {
3266 semsg(_(e_missing_comma_before_argument_str), p);
3267 return FAIL;
3268 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003269
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003270 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003271 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 return FAIL;
3273 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003274
Bram Moolenaarff652882021-05-16 15:24:49 +02003275 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003276 && cctx->ctx_instr.ga_len == instr_count + 1)
3277 {
3278 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3279
3280 // {skip} argument of searchpair() can be compiled if not empty
3281 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3282 compile_string(isn, cctx);
3283 }
3284
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003285 if (*p != ',' && *skipwhite(p) == ',')
3286 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003287 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003288 p = skipwhite(p);
3289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003291 {
3292 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003293 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003294 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003295 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003296 else
3297 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003298 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003299 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003301failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003302 emsg(_(e_missing_close));
3303 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304}
3305
3306/*
3307 * Compile a function call: name(arg1, arg2)
3308 * "arg" points to "name", "arg + varlen" to the "(".
3309 * "argcount_init" is 1 for "value->method()"
3310 * Instructions:
3311 * EVAL arg1
3312 * EVAL arg2
3313 * BCALL / DCALL / UCALL
3314 */
3315 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003316compile_call(
3317 char_u **arg,
3318 size_t varlen,
3319 cctx_T *cctx,
3320 ppconst_T *ppconst,
3321 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322{
3323 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003324 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 int argcount = argcount_init;
3326 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003327 char_u fname_buf[FLEN_FIXED + 1];
3328 char_u *tofree = NULL;
3329 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003330 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003331 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003332 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003333 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334
Bram Moolenaara5565e42020-05-09 15:44:01 +02003335 // we can evaluate "has('name')" at compile time
3336 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3337 {
3338 char_u *s = skipwhite(*arg + varlen + 1);
3339 typval_T argvars[2];
3340
3341 argvars[0].v_type = VAR_UNKNOWN;
3342 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003343 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003344 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003345 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003346 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003347 if (*s == ')' && argvars[0].v_type == VAR_STRING
3348 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003349 {
3350 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3351
3352 *arg = s + 1;
3353 argvars[1].v_type = VAR_UNKNOWN;
3354 tv->v_type = VAR_NUMBER;
3355 tv->vval.v_number = 0;
3356 f_has(argvars, tv);
3357 clear_tv(&argvars[0]);
3358 ++ppconst->pp_used;
3359 return OK;
3360 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003361 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003362 }
3363
3364 if (generate_ppconst(cctx, ppconst) == FAIL)
3365 return FAIL;
3366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 if (varlen >= sizeof(namebuf))
3368 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003369 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 return FAIL;
3371 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003372 vim_strncpy(namebuf, *arg, varlen);
3373 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003375 // We handle the "skip" argument of searchpair() and searchpairpos()
3376 // differently.
3377 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3378 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3379 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3380 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003383 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003384 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385
Bram Moolenaar03290b82020-12-19 16:30:44 +01003386 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003387 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 {
3389 int idx;
3390
3391 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003392 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003394 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003395 if (STRCMP(name, "flatten") == 0)
3396 {
3397 emsg(_(e_cannot_use_flatten_in_vim9_script));
3398 goto theend;
3399 }
3400
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003401 if (STRCMP(name, "add") == 0 && argcount == 2)
3402 {
3403 garray_T *stack = &cctx->ctx_type_stack;
3404 type_T *type = ((type_T **)stack->ga_data)[
3405 stack->ga_len - 2];
3406
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003407 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003408 if (type->tt_type == VAR_LIST)
3409 {
3410 // inline "add(list, item)" so that the type can be checked
3411 res = generate_LISTAPPEND(cctx);
3412 idx = -1;
3413 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003414 else if (type->tt_type == VAR_BLOB)
3415 {
3416 // inline "add(blob, nr)" so that the type can be checked
3417 res = generate_BLOBAPPEND(cctx);
3418 idx = -1;
3419 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003420 }
3421
3422 if (idx >= 0)
3423 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3424 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003425 else
3426 semsg(_(e_unknownfunc), namebuf);
3427 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 }
3429
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003430 // An argument or local variable can be a function reference, this
3431 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003432 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003433 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003434 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003435 // If we can find the function by name generate the right call.
3436 // Skip global functions here, a local funcref takes precedence.
3437 ufunc = find_func(name, FALSE, cctx);
3438 if (ufunc != NULL && !func_is_global(ufunc))
3439 {
3440 res = generate_CALL(cctx, ufunc, argcount);
3441 goto theend;
3442 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444
3445 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003446 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003447 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003449 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003450 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003451 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003452 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003453 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003454
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003455 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003456 goto theend;
3457 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458
Bram Moolenaar0f769812020-09-12 18:32:34 +02003459 // If we can find a global function by name generate the right call.
3460 if (ufunc != NULL)
3461 {
3462 res = generate_CALL(cctx, ufunc, argcount);
3463 goto theend;
3464 }
3465
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003466 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003467 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003468 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003469 res = generate_UCALL(cctx, name, argcount);
3470 else
3471 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003472
3473theend:
3474 vim_free(tofree);
3475 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476}
3477
3478// like NAMESPACE_CHAR but with 'a' and 'l'.
3479#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3480
3481/*
3482 * Find the end of a variable or function name. Unlike find_name_end() this
3483 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003484 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 * Return a pointer to just after the name. Equal to "arg" if there is no
3486 * valid name.
3487 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003488 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003489to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490{
3491 char_u *p;
3492
3493 // Quick check for valid starting character.
3494 if (!eval_isnamec1(*arg))
3495 return arg;
3496
3497 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3498 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3499 // and can be used in slice "[n:]".
3500 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003501 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3503 break;
3504 return p;
3505}
3506
3507/*
3508 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003509 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 */
3511 char_u *
3512to_name_const_end(char_u *arg)
3513{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003514 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 typval_T rettv;
3516
3517 if (p == arg && *arg == '[')
3518 {
3519
3520 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003521 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 p = arg;
3523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 return p;
3525}
3526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527/*
3528 * parse a list: [expr, expr]
3529 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003530 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 */
3532 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003533compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534{
3535 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003536 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003538 int is_const;
3539 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003541 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003543 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003544 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003545 semsg(_(e_list_end), *arg);
3546 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003547 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003548 if (*p == ',')
3549 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003550 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003551 return FAIL;
3552 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003553 if (*p == ']')
3554 {
3555 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003556 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003557 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003558 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003559 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003560 if (!is_const)
3561 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 ++count;
3563 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003564 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003566 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3567 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003568 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003569 return FAIL;
3570 }
3571 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003572 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 p = skipwhite(p);
3574 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003575 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003577 ppconst->pp_is_const = is_all_const;
3578 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579}
3580
3581/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003582 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003583 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003584 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 */
3586 static int
3587compile_lambda(char_u **arg, cctx_T *cctx)
3588{
Bram Moolenaare462f522020-12-27 14:43:30 +01003589 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 typval_T rettv;
3591 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003592 evalarg_T evalarg;
3593
3594 CLEAR_FIELD(evalarg);
3595 evalarg.eval_flags = EVAL_EVALUATE;
3596 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597
3598 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003599 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3600 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003601 {
3602 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003603 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003604 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003605
Bram Moolenaar65c44152020-12-24 15:14:01 +01003606 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003608 ++ufunc->uf_refcount;
3609 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610
Bram Moolenaara9931532021-06-12 15:58:16 +02003611 // Compile it here to get the return type. The return type is optional,
3612 // when it's missing use t_unknown. This is recognized in
3613 // compile_return().
3614 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3615 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaare99d4222021-06-13 14:01:26 +02003616 compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003618 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3619 // points into it. Point to the original line to avoid a dangling pointer.
3620 if (evalarg.eval_tofree_cmdline != NULL)
3621 {
3622 size_t off = *arg - evalarg.eval_tofree_cmdline;
3623
3624 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3625 + off;
3626 }
3627
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003628 clear_evalarg(&evalarg, NULL);
3629
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003630 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003631 {
3632 // The return type will now be known.
3633 set_function_type(ufunc);
3634
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003635 // The function reference count will be 1. When the ISN_FUNCREF
3636 // instruction is deleted the reference count is decremented and the
3637 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003638 return generate_FUNCREF(cctx, ufunc);
3639 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003640
3641 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 return FAIL;
3643}
3644
3645/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003646 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003648 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 */
3650 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003651compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652{
3653 garray_T *instr = &cctx->ctx_instr;
3654 int count = 0;
3655 dict_T *d = dict_alloc();
3656 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003657 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003658 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003659 int is_const;
3660 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661
3662 if (d == NULL)
3663 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003664 if (generate_ppconst(cctx, ppconst) == FAIL)
3665 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003666 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003668 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669
Bram Moolenaar23c55272020-06-21 16:58:13 +02003670 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003671 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003672 *arg = NULL;
3673 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003674 }
3675
3676 if (**arg == '}')
3677 break;
3678
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003679 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003681 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003683 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003684 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003685 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003688 if (isn->isn_type == ISN_PUSHNR)
3689 {
3690 char buf[NUMBUFLEN];
3691
3692 // Convert to string at compile time.
3693 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3694 isn->isn_type = ISN_PUSHS;
3695 isn->isn_arg.string = vim_strsave((char_u *)buf);
3696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 if (isn->isn_type == ISN_PUSHS)
3698 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003699 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003700 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003701 *arg = skipwhite(*arg);
3702 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003703 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003704 emsg(_(e_missing_matching_bracket_after_dict_key));
3705 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003706 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003707 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003709 else
3710 {
3711 // {"name": value},
3712 // {'name': value},
3713 // {name: value} use "name" as a literal key
3714 key = get_literal_key(arg);
3715 if (key == NULL)
3716 return FAIL;
3717 if (generate_PUSHS(cctx, key) == FAIL)
3718 return FAIL;
3719 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720
3721 // Check for duplicate keys, if using string keys.
3722 if (key != NULL)
3723 {
3724 item = dict_find(d, key, -1);
3725 if (item != NULL)
3726 {
3727 semsg(_(e_duplicate_key), key);
3728 goto failret;
3729 }
3730 item = dictitem_alloc(key);
3731 if (item != NULL)
3732 {
3733 item->di_tv.v_type = VAR_UNKNOWN;
3734 item->di_tv.v_lock = 0;
3735 if (dict_add(d, item) == FAIL)
3736 dictitem_free(item);
3737 }
3738 }
3739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 if (**arg != ':')
3741 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003742 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003743 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003744 else
3745 semsg(_(e_missing_dict_colon), *arg);
3746 return FAIL;
3747 }
3748 whitep = *arg + 1;
3749 if (!IS_WHITE_OR_NUL(*whitep))
3750 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003751 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 return FAIL;
3753 }
3754
Bram Moolenaar23c55272020-06-21 16:58:13 +02003755 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003756 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003757 *arg = NULL;
3758 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003759 }
3760
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003761 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003763 if (!is_const)
3764 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 ++count;
3766
Bram Moolenaar2c330432020-04-13 14:41:35 +02003767 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003768 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003769 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003770 *arg = NULL;
3771 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003772 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 if (**arg == '}')
3774 break;
3775 if (**arg != ',')
3776 {
3777 semsg(_(e_missing_dict_comma), *arg);
3778 goto failret;
3779 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003780 if (IS_WHITE_OR_NUL(*whitep))
3781 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003782 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003783 return FAIL;
3784 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003785 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003786 if (!IS_WHITE_OR_NUL(*whitep))
3787 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003788 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003789 return FAIL;
3790 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003791 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 }
3793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 *arg = *arg + 1;
3795
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003796 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003797 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003798 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003799 *arg += STRLEN(*arg);
3800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003802 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 return generate_NEWDICT(cctx, count);
3804
3805failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003806 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003807 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003808 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003809 *arg = (char_u *)"";
3810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 dict_unref(d);
3812 return FAIL;
3813}
3814
3815/*
3816 * Compile "&option".
3817 */
3818 static int
3819compile_get_option(char_u **arg, cctx_T *cctx)
3820{
3821 typval_T rettv;
3822 char_u *start = *arg;
3823 int ret;
3824
3825 // parse the option and get the current value to get the type.
3826 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003827 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 if (ret == OK)
3829 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003830 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003831 char_u *name = vim_strnsave(start, *arg - start);
3832 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3833 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834
3835 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3836 vim_free(name);
3837 }
3838 clear_tv(&rettv);
3839
3840 return ret;
3841}
3842
3843/*
3844 * Compile "$VAR".
3845 */
3846 static int
3847compile_get_env(char_u **arg, cctx_T *cctx)
3848{
3849 char_u *start = *arg;
3850 int len;
3851 int ret;
3852 char_u *name;
3853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 ++*arg;
3855 len = get_env_len(arg);
3856 if (len == 0)
3857 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003858 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 return FAIL;
3860 }
3861
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003862 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 name = vim_strnsave(start, len + 1);
3864 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3865 vim_free(name);
3866 return ret;
3867}
3868
3869/*
3870 * Compile "@r".
3871 */
3872 static int
3873compile_get_register(char_u **arg, cctx_T *cctx)
3874{
3875 int ret;
3876
3877 ++*arg;
3878 if (**arg == NUL)
3879 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003880 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 return FAIL;
3882 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003883 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 {
3885 emsg_invreg(**arg);
3886 return FAIL;
3887 }
3888 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3889 ++*arg;
3890 return ret;
3891}
3892
3893/*
3894 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003895 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 */
3897 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003898apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003900 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901
3902 // this works from end to start
3903 while (p > start)
3904 {
3905 --p;
3906 if (*p == '-' || *p == '+')
3907 {
3908 // only '-' has an effect, for '+' we only check the type
3909#ifdef FEAT_FLOAT
3910 if (rettv->v_type == VAR_FLOAT)
3911 {
3912 if (*p == '-')
3913 rettv->vval.v_float = -rettv->vval.v_float;
3914 }
3915 else
3916#endif
3917 {
3918 varnumber_T val;
3919 int error = FALSE;
3920
3921 // tv_get_number_chk() accepts a string, but we don't want that
3922 // here
3923 if (check_not_string(rettv) == FAIL)
3924 return FAIL;
3925 val = tv_get_number_chk(rettv, &error);
3926 clear_tv(rettv);
3927 if (error)
3928 return FAIL;
3929 if (*p == '-')
3930 val = -val;
3931 rettv->v_type = VAR_NUMBER;
3932 rettv->vval.v_number = val;
3933 }
3934 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003935 else if (numeric_only)
3936 {
3937 ++p;
3938 break;
3939 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003940 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 {
3942 int v = tv2bool(rettv);
3943
3944 // '!' is permissive in the type.
3945 clear_tv(rettv);
3946 rettv->v_type = VAR_BOOL;
3947 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3948 }
3949 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003950 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 return OK;
3952}
3953
3954/*
3955 * Recognize v: variables that are constants and set "rettv".
3956 */
3957 static void
3958get_vim_constant(char_u **arg, typval_T *rettv)
3959{
3960 if (STRNCMP(*arg, "v:true", 6) == 0)
3961 {
3962 rettv->v_type = VAR_BOOL;
3963 rettv->vval.v_number = VVAL_TRUE;
3964 *arg += 6;
3965 }
3966 else if (STRNCMP(*arg, "v:false", 7) == 0)
3967 {
3968 rettv->v_type = VAR_BOOL;
3969 rettv->vval.v_number = VVAL_FALSE;
3970 *arg += 7;
3971 }
3972 else if (STRNCMP(*arg, "v:null", 6) == 0)
3973 {
3974 rettv->v_type = VAR_SPECIAL;
3975 rettv->vval.v_number = VVAL_NULL;
3976 *arg += 6;
3977 }
3978 else if (STRNCMP(*arg, "v:none", 6) == 0)
3979 {
3980 rettv->v_type = VAR_SPECIAL;
3981 rettv->vval.v_number = VVAL_NONE;
3982 *arg += 6;
3983 }
3984}
3985
Bram Moolenaar657137c2021-01-09 15:45:23 +01003986 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003987get_compare_type(char_u *p, int *len, int *type_is)
3988{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003989 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003990 int i;
3991
3992 switch (p[0])
3993 {
3994 case '=': if (p[1] == '=')
3995 type = EXPR_EQUAL;
3996 else if (p[1] == '~')
3997 type = EXPR_MATCH;
3998 break;
3999 case '!': if (p[1] == '=')
4000 type = EXPR_NEQUAL;
4001 else if (p[1] == '~')
4002 type = EXPR_NOMATCH;
4003 break;
4004 case '>': if (p[1] != '=')
4005 {
4006 type = EXPR_GREATER;
4007 *len = 1;
4008 }
4009 else
4010 type = EXPR_GEQUAL;
4011 break;
4012 case '<': if (p[1] != '=')
4013 {
4014 type = EXPR_SMALLER;
4015 *len = 1;
4016 }
4017 else
4018 type = EXPR_SEQUAL;
4019 break;
4020 case 'i': if (p[1] == 's')
4021 {
4022 // "is" and "isnot"; but not a prefix of a name
4023 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4024 *len = 5;
4025 i = p[*len];
4026 if (!isalnum(i) && i != '_')
4027 {
4028 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4029 *type_is = TRUE;
4030 }
4031 }
4032 break;
4033 }
4034 return type;
4035}
4036
4037/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004038 * Skip over an expression, ignoring most errors.
4039 */
4040 static void
4041skip_expr_cctx(char_u **arg, cctx_T *cctx)
4042{
4043 evalarg_T evalarg;
4044
4045 CLEAR_FIELD(evalarg);
4046 evalarg.eval_cctx = cctx;
4047 skip_expr(arg, &evalarg);
4048}
4049
4050/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004052 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 */
4054 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004055compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004057 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058
4059 // this works from end to start
4060 while (p > start)
4061 {
4062 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004063 while (VIM_ISWHITE(*p))
4064 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 if (*p == '-' || *p == '+')
4066 {
4067 int negate = *p == '-';
4068 isn_T *isn;
4069
4070 // TODO: check type
4071 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4072 {
4073 --p;
4074 if (*p == '-')
4075 negate = !negate;
4076 }
4077 // only '-' has an effect, for '+' we only check the type
4078 if (negate)
4079 isn = generate_instr(cctx, ISN_NEGATENR);
4080 else
4081 isn = generate_instr(cctx, ISN_CHECKNR);
4082 if (isn == NULL)
4083 return FAIL;
4084 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004085 else if (numeric_only)
4086 {
4087 ++p;
4088 break;
4089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 else
4091 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004092 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004094 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004096 if (p[-1] == '!')
4097 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004100 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 return FAIL;
4102 }
4103 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004104 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 return OK;
4106}
4107
4108/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004109 * Compile "(expression)": recursive!
4110 * Return FAIL/OK.
4111 */
4112 static int
4113compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4114{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004115 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004116 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004117
Bram Moolenaar24156692021-01-14 20:35:49 +01004118 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4119 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004120 if (ppconst->pp_used <= PPSIZE - 10)
4121 {
4122 ret = compile_expr1(arg, cctx, ppconst);
4123 }
4124 else
4125 {
4126 // Not enough space in ppconst, flush constants.
4127 if (generate_ppconst(cctx, ppconst) == FAIL)
4128 return FAIL;
4129 ret = compile_expr0(arg, cctx);
4130 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004131 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4132 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004133 if (**arg == ')')
4134 ++*arg;
4135 else if (ret == OK)
4136 {
4137 emsg(_(e_missing_close));
4138 ret = FAIL;
4139 }
4140 return ret;
4141}
4142
4143/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004145 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 */
4147 static int
4148compile_subscript(
4149 char_u **arg,
4150 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004151 char_u *start_leader,
4152 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004153 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004155 char_u *name_start = *end_leader;
4156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 for (;;)
4158 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004159 char_u *p = skipwhite(*arg);
4160
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004161 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004162 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004163 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004164
4165 // If a following line starts with "->{" or "->X" advance to that
4166 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004167 // Also if a following line starts with ".x".
4168 if (next != NULL &&
4169 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004170 && (next[2] == '{'
4171 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004172 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004173 {
4174 next = next_line_from_context(cctx, TRUE);
4175 if (next == NULL)
4176 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004177 *arg = next;
4178 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004179 }
4180 }
4181
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004182 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004183 // not a function call.
4184 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004186 garray_T *stack = &cctx->ctx_type_stack;
4187 type_T *type;
4188 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004190 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004191 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004192 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004195 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4196
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004197 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004198 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004200 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 return FAIL;
4202 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004203 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004205 char_u *pstart = p;
4206
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004207 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004208 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004209 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 // something->method()
4212 // Apply the '!', '-' and '+' first:
4213 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004214 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004217 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004218 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004219 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004220 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004221 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004222 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004223 garray_T *stack = &cctx->ctx_type_stack;
4224 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004225 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004226 int expr_isn_start = cctx->ctx_instr.ga_len;
4227 int expr_isn_end;
4228 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004229
4230 // Funcref call: list->(Refs[2])(arg)
4231 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004232 //
4233 // Fist compile the function expression.
4234 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004235 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004236
4237 // Remember the next instruction index, where the instructions
4238 // for arguments are being written.
4239 expr_isn_end = cctx->ctx_instr.ga_len;
4240
4241 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004242 if (**arg != '(')
4243 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004244 if (*skipwhite(*arg) == '(')
4245 emsg(_(e_nowhitespace));
4246 else
4247 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004248 return FAIL;
4249 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004250 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004251 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004252 return FAIL;
4253
Bram Moolenaar2927c072021-04-05 19:41:21 +02004254 // Move the instructions for the arguments to before the
4255 // instructions of the expression and move the type of the
4256 // expression after the argument types. This is what ISN_PCALL
4257 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004258 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004259 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4260 if (arg_isn_count > 0)
4261 {
4262 int expr_isn_count = expr_isn_end - expr_isn_start;
4263 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4264
4265 if (isn == NULL)
4266 return FAIL;
4267 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4268 + expr_isn_start,
4269 sizeof(isn_T) * expr_isn_count);
4270 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4271 + expr_isn_start,
4272 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4273 sizeof(isn_T) * arg_isn_count);
4274 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4275 + expr_isn_start + arg_isn_count,
4276 isn, sizeof(isn_T) * expr_isn_count);
4277 vim_free(isn);
4278
4279 type = ((type_T **)stack->ga_data)[type_idx_start];
4280 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4281 ((type_T **)stack->ga_data) + type_idx_start + 1,
4282 sizeof(type_T *)
4283 * (stack->ga_len - type_idx_start - 1));
4284 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4285 }
4286
Bram Moolenaar7e368202020-12-25 21:56:57 +01004287 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4288 if (generate_PCALL(cctx, argcount,
4289 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 return FAIL;
4291 }
4292 else
4293 {
4294 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004295 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004296 if (!eval_isnamec1(*p))
4297 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004298 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004299 return FAIL;
4300 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004301 if (ASCII_ISALPHA(*p) && p[1] == ':')
4302 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004303 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 ;
4305 if (*p != '(')
4306 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004307 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 return FAIL;
4309 }
4310 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004311 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 return FAIL;
4313 }
4314 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004315 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004317 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004318
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004319 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004320 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004321 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004322 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004323 // TODO: more arguments
4324 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004325 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004326 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004327 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004328
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004329 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004330 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004331 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004332 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004333 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004334 // missing first index is equal to zero
4335 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004336 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004337 else
4338 {
4339 if (compile_expr0(arg, cctx) == FAIL)
4340 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004341 if (**arg == ':')
4342 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004343 semsg(_(e_white_space_required_before_and_after_str_at_str),
4344 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004345 return FAIL;
4346 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004347 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004348 return FAIL;
4349 *arg = skipwhite(*arg);
4350 }
4351 if (**arg == ':')
4352 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004353 is_slice = TRUE;
4354 ++*arg;
4355 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4356 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004357 semsg(_(e_white_space_required_before_and_after_str_at_str),
4358 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004359 return FAIL;
4360 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004361 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004362 return FAIL;
4363 if (**arg == ']')
4364 // missing second index is equal to end of string
4365 generate_PUSHNR(cctx, -1);
4366 else
4367 {
4368 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004369 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004370 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004371 return FAIL;
4372 *arg = skipwhite(*arg);
4373 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004374 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375
4376 if (**arg != ']')
4377 {
4378 emsg(_(e_missbrac));
4379 return FAIL;
4380 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004381 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382
Bram Moolenaare42939a2021-04-05 17:11:17 +02004383 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004384 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004386 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004388 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004389 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004390 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004391 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004392
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004393 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004394 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004395 {
4396 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004397 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004398 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004399 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004400 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 while (eval_isnamec(*p))
4402 MB_PTR_ADV(p);
4403 if (p == *arg)
4404 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004405 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 return FAIL;
4407 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004408 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 return FAIL;
4410 *arg = p;
4411 }
4412 else
4413 break;
4414 }
4415
4416 // TODO - see handle_subscript():
4417 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4418 // Don't do this when "Func" is already a partial that was bound
4419 // explicitly (pt_auto is FALSE).
4420
4421 return OK;
4422}
4423
4424/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004425 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4426 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004428 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004429 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004430 *
4431 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 */
4433
4434/*
4435 * number number constant
4436 * 0zFFFFFFFF Blob constant
4437 * "string" string constant
4438 * 'string' literal string constant
4439 * &option-name option value
4440 * @r register contents
4441 * identifier variable value
4442 * function() function call
4443 * $VAR environment variable
4444 * (expression) nested expression
4445 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004446 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 *
4448 * Also handle:
4449 * ! in front logical NOT
4450 * - in front unary minus
4451 * + in front unary plus (ignored)
4452 * trailing (arg) funcref/partial call
4453 * trailing [] subscript in String or List
4454 * trailing .name entry in Dictionary
4455 * trailing ->name() method call
4456 */
4457 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004458compile_expr7(
4459 char_u **arg,
4460 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004461 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 char_u *start_leader, *end_leader;
4464 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004465 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004466 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004468 ppconst->pp_is_const = FALSE;
4469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 /*
4471 * Skip '!', '-' and '+' characters. They are handled later.
4472 */
4473 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004474 if (eval_leader(arg, TRUE) == FAIL)
4475 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 end_leader = *arg;
4477
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004478 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 switch (**arg)
4480 {
4481 /*
4482 * Number constant.
4483 */
4484 case '0': // also for blob starting with 0z
4485 case '1':
4486 case '2':
4487 case '3':
4488 case '4':
4489 case '5':
4490 case '6':
4491 case '7':
4492 case '8':
4493 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004494 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004496 // Apply "-" and "+" just before the number now, right to
4497 // left. Matters especially when "->" follows. Stops at
4498 // '!'.
4499 if (apply_leader(rettv, TRUE,
4500 start_leader, &end_leader) == FAIL)
4501 {
4502 clear_tv(rettv);
4503 return FAIL;
4504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 break;
4506
4507 /*
4508 * String constant: "string".
4509 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004510 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 return FAIL;
4512 break;
4513
4514 /*
4515 * Literal string constant: 'str''ing'.
4516 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004517 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 return FAIL;
4519 break;
4520
4521 /*
4522 * Constant Vim variable.
4523 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004524 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 ret = NOTDONE;
4526 break;
4527
4528 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004529 * "true" constant
4530 */
4531 case 't': if (STRNCMP(*arg, "true", 4) == 0
4532 && !eval_isnamec((*arg)[4]))
4533 {
4534 *arg += 4;
4535 rettv->v_type = VAR_BOOL;
4536 rettv->vval.v_number = VVAL_TRUE;
4537 }
4538 else
4539 ret = NOTDONE;
4540 break;
4541
4542 /*
4543 * "false" constant
4544 */
4545 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4546 && !eval_isnamec((*arg)[5]))
4547 {
4548 *arg += 5;
4549 rettv->v_type = VAR_BOOL;
4550 rettv->vval.v_number = VVAL_FALSE;
4551 }
4552 else
4553 ret = NOTDONE;
4554 break;
4555
4556 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004557 * "null" constant
4558 */
4559 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004560 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004561 {
4562 *arg += 4;
4563 rettv->v_type = VAR_SPECIAL;
4564 rettv->vval.v_number = VVAL_NULL;
4565 }
4566 else
4567 ret = NOTDONE;
4568 break;
4569
4570 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 * List: [expr, expr]
4572 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004573 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4574 return FAIL;
4575 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 break;
4577
4578 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 * Dictionary: {'key': val, 'key': val}
4580 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004581 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4582 return FAIL;
4583 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 break;
4585
4586 /*
4587 * Option value: &name
4588 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004589 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4590 return FAIL;
4591 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 break;
4593
4594 /*
4595 * Environment variable: $VAR.
4596 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004597 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4598 return FAIL;
4599 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 break;
4601
4602 /*
4603 * Register contents: @r.
4604 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004605 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4606 return FAIL;
4607 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 break;
4609 /*
4610 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004611 * lambda: (arg, arg) => expr
4612 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004614 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4615 ret = compile_lambda(arg, cctx);
4616 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004617 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 break;
4619
4620 default: ret = NOTDONE;
4621 break;
4622 }
4623 if (ret == FAIL)
4624 return FAIL;
4625
Bram Moolenaar1c747212020-05-09 18:28:34 +02004626 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004628 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004629 clear_tv(rettv);
4630 else
4631 // A constant expression can possibly be handled compile time,
4632 // return the value instead of generating code.
4633 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 }
4635 else if (ret == NOTDONE)
4636 {
4637 char_u *p;
4638 int r;
4639
4640 if (!eval_isnamec1(**arg))
4641 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004642 if (!vim9_bad_comment(*arg))
4643 {
4644 if (ends_excmd(*skipwhite(*arg)))
4645 semsg(_(e_empty_expression_str), *arg);
4646 else
4647 semsg(_(e_name_expected_str), *arg);
4648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 return FAIL;
4650 }
4651
4652 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004653 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004654 if (p - *arg == (size_t)1 && **arg == '_')
4655 {
4656 emsg(_(e_cannot_use_underscore_here));
4657 return FAIL;
4658 }
4659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004661 {
4662 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4663 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004665 {
4666 if (generate_ppconst(cctx, ppconst) == FAIL)
4667 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004668 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 if (r == FAIL)
4671 return FAIL;
4672 }
4673
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004674 // Handle following "[]", ".member", etc.
4675 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004676 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004677 ppconst) == FAIL)
4678 return FAIL;
4679 if (ppconst->pp_used > 0)
4680 {
4681 // apply the '!', '-' and '+' before the constant
4682 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004683 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004684 return FAIL;
4685 return OK;
4686 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004687 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004689 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690}
4691
4692/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004693 * Give the "white on both sides" error, taking the operator from "p[len]".
4694 */
4695 void
4696error_white_both(char_u *op, int len)
4697{
4698 char_u buf[10];
4699
4700 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004701 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004702}
4703
4704/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004705 * <type>expr7: runtime type check / conversion
4706 */
4707 static int
4708compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4709{
4710 type_T *want_type = NULL;
4711
4712 // Recognize <type>
4713 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4714 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004715 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004716 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4717 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004718 return FAIL;
4719
4720 if (**arg != '>')
4721 {
4722 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004723 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004724 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004725 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004726 return FAIL;
4727 }
4728 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004729 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004730 return FAIL;
4731 }
4732
4733 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4734 return FAIL;
4735
4736 if (want_type != NULL)
4737 {
4738 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004739 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004740 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004741
Bram Moolenaard1103582020-08-14 22:44:25 +02004742 generate_ppconst(cctx, ppconst);
4743 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004744 where.wt_index = 0;
4745 where.wt_variable = FALSE;
4746 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004747 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004748 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004749 return FAIL;
4750 }
4751 }
4752
4753 return OK;
4754}
4755
4756/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 * * number multiplication
4758 * / number division
4759 * % number modulo
4760 */
4761 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004762compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763{
4764 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004765 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004766 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004768 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004769 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 return FAIL;
4771
4772 /*
4773 * Repeat computing, until no "*", "/" or "%" is following.
4774 */
4775 for (;;)
4776 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004777 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 if (*op != '*' && *op != '/' && *op != '%')
4779 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004780 if (next != NULL)
4781 {
4782 *arg = next_line_from_context(cctx, TRUE);
4783 op = skipwhite(*arg);
4784 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004785
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004786 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004788 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004789 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004791 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004792 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004794 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004795 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004797
4798 if (ppconst->pp_used == ppconst_used + 2
4799 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4800 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004801 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004802 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4803 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4804 varnumber_T res = 0;
4805 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004807 // both are numbers: compute the result
4808 switch (*op)
4809 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004810 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004811 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004812 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004813 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004814 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004815 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004816 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004817 break;
4818 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004819 if (failed)
4820 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004821 tv1->vval.v_number = res;
4822 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004823 }
4824 else
4825 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004826 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004827 generate_two_op(cctx, op);
4828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 }
4830
4831 return OK;
4832}
4833
4834/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004835 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836 * - number subtraction
4837 * .. string concatenation
4838 */
4839 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004840compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841{
4842 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004843 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004845 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846
4847 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004848 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 return FAIL;
4850
4851 /*
4852 * Repeat computing, until no "+", "-" or ".." is following.
4853 */
4854 for (;;)
4855 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004856 op = may_peek_next_line(cctx, *arg, &next);
4857 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004859 if (op[0] == op[1] && *op != '.' && next)
4860 // Finding "++" or "--" on the next line is a separate command.
4861 // But ".." is concatenation.
4862 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004864 if (next != NULL)
4865 {
4866 *arg = next_line_from_context(cctx, TRUE);
4867 op = skipwhite(*arg);
4868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004870 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004872 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004873 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 }
4875
Bram Moolenaare0de1712020-12-02 17:36:54 +01004876 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004877 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004879 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004880 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 return FAIL;
4882
Bram Moolenaara5565e42020-05-09 15:44:01 +02004883 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004884 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004885 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4886 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4887 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4888 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004890 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4891 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004892
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004893 // concat/subtract/add constant numbers
4894 if (*op == '+')
4895 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4896 else if (*op == '-')
4897 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4898 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004899 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004900 // concatenate constant strings
4901 char_u *s1 = tv1->vval.v_string;
4902 char_u *s2 = tv2->vval.v_string;
4903 size_t len1 = STRLEN(s1);
4904
4905 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4906 if (tv1->vval.v_string == NULL)
4907 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004908 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004909 return FAIL;
4910 }
4911 mch_memmove(tv1->vval.v_string, s1, len1);
4912 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004913 vim_free(s1);
4914 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004915 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004916 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 }
4918 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004919 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004920 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004921 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004922 if (*op == '.')
4923 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004924 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4925 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004926 return FAIL;
4927 generate_instr_drop(cctx, ISN_CONCAT, 1);
4928 }
4929 else
4930 generate_two_op(cctx, op);
4931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 }
4933
4934 return OK;
4935}
4936
4937/*
4938 * expr5a == expr5b
4939 * expr5a =~ expr5b
4940 * expr5a != expr5b
4941 * expr5a !~ expr5b
4942 * expr5a > expr5b
4943 * expr5a >= expr5b
4944 * expr5a < expr5b
4945 * expr5a <= expr5b
4946 * expr5a is expr5b
4947 * expr5a isnot expr5b
4948 *
4949 * Produces instructions:
4950 * EVAL expr5a Push result of "expr5a"
4951 * EVAL expr5b Push result of "expr5b"
4952 * COMPARE one of the compare instructions
4953 */
4954 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004955compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004957 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004959 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004962 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963
4964 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004965 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 return FAIL;
4967
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004968 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004969 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970
4971 /*
4972 * If there is a comparative operator, use it.
4973 */
4974 if (type != EXPR_UNKNOWN)
4975 {
4976 int ic = FALSE; // Default: do not ignore case
4977
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004978 if (next != NULL)
4979 {
4980 *arg = next_line_from_context(cctx, TRUE);
4981 p = skipwhite(*arg);
4982 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 if (type_is && (p[len] == '?' || p[len] == '#'))
4984 {
4985 semsg(_(e_invexpr2), *arg);
4986 return FAIL;
4987 }
4988 // extra question mark appended: ignore case
4989 if (p[len] == '?')
4990 {
4991 ic = TRUE;
4992 ++len;
4993 }
4994 // extra '#' appended: match case (ignored)
4995 else if (p[len] == '#')
4996 ++len;
4997 // nothing appended: match case
4998
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004999 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005001 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005002 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 }
5004
5005 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005006 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005007 return FAIL;
5008
Bram Moolenaara5565e42020-05-09 15:44:01 +02005009 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 return FAIL;
5011
Bram Moolenaara5565e42020-05-09 15:44:01 +02005012 if (ppconst->pp_used == ppconst_used + 2)
5013 {
5014 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5015 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5016 int ret;
5017
5018 // Both sides are a constant, compute the result now.
5019 // First check for a valid combination of types, this is more
5020 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005021 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005022 ret = FAIL;
5023 else
5024 {
5025 ret = typval_compare(tv1, tv2, type, ic);
5026 tv1->v_type = VAR_BOOL;
5027 tv1->vval.v_number = tv1->vval.v_number
5028 ? VVAL_TRUE : VVAL_FALSE;
5029 clear_tv(tv2);
5030 --ppconst->pp_used;
5031 }
5032 return ret;
5033 }
5034
5035 generate_ppconst(cctx, ppconst);
5036 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005037 }
5038
5039 return OK;
5040}
5041
Bram Moolenaar7f141552020-05-09 17:35:53 +02005042static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044/*
5045 * Compile || or &&.
5046 */
5047 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005048compile_and_or(
5049 char_u **arg,
5050 cctx_T *cctx,
5051 char *op,
5052 ppconst_T *ppconst,
5053 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005055 char_u *next;
5056 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 int opchar = *op;
5058
5059 if (p[0] == opchar && p[1] == opchar)
5060 {
5061 garray_T *instr = &cctx->ctx_instr;
5062 garray_T end_ga;
5063
5064 /*
5065 * Repeat until there is no following "||" or "&&"
5066 */
5067 ga_init2(&end_ga, sizeof(int), 10);
5068 while (p[0] == opchar && p[1] == opchar)
5069 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005070 long start_lnum = SOURCING_LNUM;
5071 int start_ctx_lnum = cctx->ctx_lnum;
5072 int save_lnum;
5073
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005074 if (next != NULL)
5075 {
5076 *arg = next_line_from_context(cctx, TRUE);
5077 p = skipwhite(*arg);
5078 }
5079
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005080 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5081 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005082 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005083 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005084 return FAIL;
5085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005087 // TODO: use ppconst if the value is a constant and check
5088 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005089 generate_ppconst(cctx, ppconst);
5090
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005091 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005092 SOURCING_LNUM = start_lnum;
5093 save_lnum = cctx->ctx_lnum;
5094 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005095 if (bool_on_stack(cctx) == FAIL)
5096 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005097 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005098 ga_clear(&end_ga);
5099 return FAIL;
5100 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005101 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 if (ga_grow(&end_ga, 1) == FAIL)
5104 {
5105 ga_clear(&end_ga);
5106 return FAIL;
5107 }
5108 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5109 ++end_ga.ga_len;
5110 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005111 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112
5113 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005114 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005115 {
5116 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005117 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005118 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005119
Bram Moolenaara5565e42020-05-09 15:44:01 +02005120 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5121 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122 {
5123 ga_clear(&end_ga);
5124 return FAIL;
5125 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005126
5127 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005129 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005131 // Every part must evaluate to a bool.
5132 if (bool_on_stack(cctx) == FAIL)
5133 {
5134 ga_clear(&end_ga);
5135 return FAIL;
5136 }
5137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 // Fill in the end label in all jumps.
5139 while (end_ga.ga_len > 0)
5140 {
5141 isn_T *isn;
5142
5143 --end_ga.ga_len;
5144 isn = ((isn_T *)instr->ga_data)
5145 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5146 isn->isn_arg.jump.jump_where = instr->ga_len;
5147 }
5148 ga_clear(&end_ga);
5149 }
5150
5151 return OK;
5152}
5153
5154/*
5155 * expr4a && expr4a && expr4a logical AND
5156 *
5157 * Produces instructions:
5158 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005159 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005160 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005162 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163 * EVAL expr4c Push result of "expr4c"
5164 * end:
5165 */
5166 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005167compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005169 int ppconst_used = ppconst->pp_used;
5170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005172 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 return FAIL;
5174
5175 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005176 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177}
5178
5179/*
5180 * expr3a || expr3b || expr3c logical OR
5181 *
5182 * Produces instructions:
5183 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005184 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005185 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005187 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005188 * EVAL expr3c Push result of "expr3c"
5189 * end:
5190 */
5191 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005192compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005194 int ppconst_used = ppconst->pp_used;
5195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005197 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198 return FAIL;
5199
5200 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005201 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202}
5203
5204/*
5205 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005207 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 * JUMP_IF_FALSE alt jump if false
5209 * EVAL expr1a
5210 * JUMP_ALWAYS end
5211 * alt: EVAL expr1b
5212 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005213 *
5214 * Toplevel expression: expr2 ?? expr1
5215 * Produces instructions:
5216 * EVAL expr2 Push result of "expr2"
5217 * JUMP_AND_KEEP_IF_TRUE end jump if true
5218 * EVAL expr1
5219 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 */
5221 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005222compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223{
5224 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005225 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005226 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227
Bram Moolenaar3988f642020-08-27 22:43:03 +02005228 // Ignore all kinds of errors when not producing code.
5229 if (cctx->ctx_skip == SKIP_YES)
5230 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005231 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005232 return OK;
5233 }
5234
Bram Moolenaar61a89812020-05-07 16:58:17 +02005235 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005236 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 return FAIL;
5238
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005239 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240 if (*p == '?')
5241 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005242 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243 garray_T *instr = &cctx->ctx_instr;
5244 garray_T *stack = &cctx->ctx_type_stack;
5245 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005246 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005248 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005249 int has_const_expr = FALSE;
5250 int const_value = FALSE;
5251 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005253 if (next != NULL)
5254 {
5255 *arg = next_line_from_context(cctx, TRUE);
5256 p = skipwhite(*arg);
5257 }
5258
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005259 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005260 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005261 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005262 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005263 return FAIL;
5264 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265
Bram Moolenaara5565e42020-05-09 15:44:01 +02005266 if (ppconst->pp_used == ppconst_used + 1)
5267 {
5268 // the condition is a constant, we know whether the ? or the :
5269 // expression is to be evaluated.
5270 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005271 if (op_falsy)
5272 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5273 else
5274 {
5275 int error = FALSE;
5276
5277 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5278 &error);
5279 if (error)
5280 return FAIL;
5281 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005282 cctx->ctx_skip = save_skip == SKIP_YES ||
5283 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5284
5285 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5286 // "left ?? right" and "left" is truthy: produce "left"
5287 generate_ppconst(cctx, ppconst);
5288 else
5289 {
5290 clear_tv(&ppconst->pp_tv[ppconst_used]);
5291 --ppconst->pp_used;
5292 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005293 }
5294 else
5295 {
5296 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005297 if (op_falsy)
5298 end_idx = instr->ga_len;
5299 generate_JUMP(cctx, op_falsy
5300 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5301 if (op_falsy)
5302 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005303 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304
5305 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005306 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005307 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005308 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005309 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310
Bram Moolenaara5565e42020-05-09 15:44:01 +02005311 if (!has_const_expr)
5312 {
5313 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005314
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005315 if (!op_falsy)
5316 {
5317 // remember the type and drop it
5318 --stack->ga_len;
5319 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005321 end_idx = instr->ga_len;
5322 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005323
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005324 // jump here from JUMP_IF_FALSE
5325 isn = ((isn_T *)instr->ga_data) + alt_idx;
5326 isn->isn_arg.jump.jump_where = instr->ga_len;
5327 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005328 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005329
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005330 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005332 // Check for the ":".
5333 p = may_peek_next_line(cctx, *arg, &next);
5334 if (*p != ':')
5335 {
5336 emsg(_(e_missing_colon));
5337 return FAIL;
5338 }
5339 if (next != NULL)
5340 {
5341 *arg = next_line_from_context(cctx, TRUE);
5342 p = skipwhite(*arg);
5343 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005344
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005345 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5346 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005347 semsg(_(e_white_space_required_before_and_after_str_at_str),
5348 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005349 return FAIL;
5350 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005352 // evaluate the third expression
5353 if (has_const_expr)
5354 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005355 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005356 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005357 return FAIL;
5358 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5359 return FAIL;
5360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361
Bram Moolenaara5565e42020-05-09 15:44:01 +02005362 if (!has_const_expr)
5363 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005364 type_T **typep;
5365
Bram Moolenaara5565e42020-05-09 15:44:01 +02005366 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367
Bram Moolenaara5565e42020-05-09 15:44:01 +02005368 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005369 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5370 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005371
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005372 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005373 isn = ((isn_T *)instr->ga_data) + end_idx;
5374 isn->isn_arg.jump.jump_where = instr->ga_len;
5375 }
5376
5377 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378 }
5379 return OK;
5380}
5381
5382/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005383 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005384 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5385 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005386 */
5387 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005388compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005389{
5390 ppconst_T ppconst;
5391
5392 CLEAR_FIELD(ppconst);
5393 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5394 {
5395 clear_ppconst(&ppconst);
5396 return FAIL;
5397 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005398 if (is_const != NULL)
5399 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005400 if (generate_ppconst(cctx, &ppconst) == FAIL)
5401 return FAIL;
5402 return OK;
5403}
5404
5405/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005406 * Toplevel expression.
5407 */
5408 static int
5409compile_expr0(char_u **arg, cctx_T *cctx)
5410{
5411 return compile_expr0_ext(arg, cctx, NULL);
5412}
5413
5414/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005415 * Compile "return [expr]".
5416 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005417 */
5418 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005419compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005420{
5421 char_u *p = arg;
5422 garray_T *stack = &cctx->ctx_type_stack;
5423 type_T *stack_type;
5424
5425 if (*p != NUL && *p != '|' && *p != '\n')
5426 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005427 if (legacy)
5428 {
5429 int save_flags = cmdmod.cmod_flags;
5430
5431 generate_LEGACY_EVAL(cctx, p);
5432 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5433 0, cctx, FALSE, FALSE) == FAIL)
5434 return NULL;
5435 cmdmod.cmod_flags |= CMOD_LEGACY;
5436 (void)skip_expr(&p, NULL);
5437 cmdmod.cmod_flags = save_flags;
5438 }
5439 else
5440 {
5441 // compile return argument into instructions
5442 if (compile_expr0(&p, cctx) == FAIL)
5443 return NULL;
5444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005445
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005446 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005447 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005448 // "check_return_type" with uf_ret_type set to &t_unknown is used
5449 // for an inline function without a specified return type. Set the
5450 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005451 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005452 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005453 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5454 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005455 || (!check_return_type
5456 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005457 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005458 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005459 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005460 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005461 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005462 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5463 && stack_type->tt_type != VAR_VOID
5464 && stack_type->tt_type != VAR_UNKNOWN)
5465 {
5466 emsg(_(e_returning_value_in_function_without_return_type));
5467 return NULL;
5468 }
5469 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005470 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005471 return NULL;
5472 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474 }
5475 else
5476 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005477 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005478 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005479 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5480 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005481 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005482 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005483 return NULL;
5484 }
5485
5486 // No argument, return zero.
5487 generate_PUSHNR(cctx, 0);
5488 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005489
5490 // Undo any command modifiers.
5491 generate_undo_cmdmods(cctx);
5492
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005493 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494 return NULL;
5495
5496 // "return val | endif" is possible
5497 return skipwhite(p);
5498}
5499
5500/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005501 * Get a line from the compilation context, compatible with exarg_T getline().
5502 * Return a pointer to the line in allocated memory.
5503 * Return NULL for end-of-file or some error.
5504 */
5505 static char_u *
5506exarg_getline(
5507 int c UNUSED,
5508 void *cookie,
5509 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005510 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005511{
5512 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005513 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005514
Bram Moolenaar66250c92020-08-20 15:02:42 +02005515 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005516 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005517 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005518 return NULL;
5519 ++cctx->ctx_lnum;
5520 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5521 // Comment lines result in NULL pointers, skip them.
5522 if (p != NULL)
5523 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005524 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005525}
5526
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005527 void
5528fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5529{
5530 eap->getline = exarg_getline;
5531 eap->cookie = cctx;
5532}
5533
Bram Moolenaar04b12692020-05-04 23:24:44 +02005534/*
5535 * Compile a nested :def command.
5536 */
5537 static char_u *
5538compile_nested_function(exarg_T *eap, cctx_T *cctx)
5539{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005540 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005541 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005542 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005543 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005544 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005545 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005546
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005547 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005548 {
5549 emsg(_(e_cannot_use_bang_with_nested_def));
5550 return NULL;
5551 }
5552
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005553 if (*name_start == '/')
5554 {
5555 name_end = skip_regexp(name_start + 1, '/', TRUE);
5556 if (*name_end == '/')
5557 ++name_end;
5558 eap->nextcmd = check_nextcmd(name_end);
5559 }
5560 if (name_end == name_start || *skipwhite(name_end) != '(')
5561 {
5562 if (!ends_excmd2(name_start, name_end))
5563 {
5564 semsg(_(e_invalid_command_str), eap->cmd);
5565 return NULL;
5566 }
5567
5568 // "def" or "def Name": list functions
5569 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5570 return NULL;
5571 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5572 }
5573
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005574 // Only g:Func() can use a namespace.
5575 if (name_start[1] == ':' && !is_global)
5576 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005577 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005578 return NULL;
5579 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005580 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005581 return NULL;
5582
Bram Moolenaar04b12692020-05-04 23:24:44 +02005583 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005584 fill_exarg_from_cctx(eap, cctx);
5585
Bram Moolenaar04b12692020-05-04 23:24:44 +02005586 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005587 lambda_name = vim_strsave(get_lambda_name());
5588 if (lambda_name == NULL)
5589 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005590 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005591
Bram Moolenaar822ba242020-05-24 23:00:18 +02005592 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005593 {
5594 r = eap->skip ? OK : FAIL;
5595 goto theend;
5596 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005597
5598 // copy over the block scope IDs before compiling
5599 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5600 {
5601 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5602
5603 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5604 if (ufunc->uf_block_ids != NULL)
5605 {
5606 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5607 sizeof(int) * block_depth);
5608 ufunc->uf_block_depth = block_depth;
5609 }
5610 }
5611
Bram Moolenaare99d4222021-06-13 14:01:26 +02005612 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
5613 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005614 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005615 {
5616 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005617 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005618 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005619
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005620 if (is_global)
5621 {
5622 char_u *func_name = vim_strnsave(name_start + 2,
5623 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005624
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005625 if (func_name == NULL)
5626 r = FAIL;
5627 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005628 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005629 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005630 lambda_name = NULL;
5631 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005632 }
5633 else
5634 {
5635 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005636 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005637 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005638
Bram Moolenaareef21022020-08-01 22:16:43 +02005639 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005640 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005641 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005642 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005643 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5644 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005645 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005646
5647theend:
5648 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005649 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005650}
5651
5652/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005653 * Return the length of an assignment operator, or zero if there isn't one.
5654 */
5655 int
5656assignment_len(char_u *p, int *heredoc)
5657{
5658 if (*p == '=')
5659 {
5660 if (p[1] == '<' && p[2] == '<')
5661 {
5662 *heredoc = TRUE;
5663 return 3;
5664 }
5665 return 1;
5666 }
5667 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5668 return 2;
5669 if (STRNCMP(p, "..=", 3) == 0)
5670 return 3;
5671 return 0;
5672}
5673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005675 * Generate the load instruction for "name".
5676 */
5677 static void
5678generate_loadvar(
5679 cctx_T *cctx,
5680 assign_dest_T dest,
5681 char_u *name,
5682 lvar_T *lvar,
5683 type_T *type)
5684{
5685 switch (dest)
5686 {
5687 case dest_option:
5688 // TODO: check the option exists
5689 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5690 break;
5691 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005692 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5693 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5694 else
5695 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005696 break;
5697 case dest_buffer:
5698 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5699 break;
5700 case dest_window:
5701 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5702 break;
5703 case dest_tab:
5704 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5705 break;
5706 case dest_script:
5707 compile_load_scriptvar(cctx,
5708 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5709 break;
5710 case dest_env:
5711 // Include $ in the name here
5712 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5713 break;
5714 case dest_reg:
5715 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5716 break;
5717 case dest_vimvar:
5718 generate_LOADV(cctx, name + 2, TRUE);
5719 break;
5720 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005721 if (lvar->lv_from_outer > 0)
5722 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5723 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005724 else
5725 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5726 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005727 case dest_expr:
5728 // list or dict value should already be on the stack.
5729 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005730 }
5731}
5732
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005733/*
5734 * Skip over "[expr]" or ".member".
5735 * Does not check for any errors.
5736 */
5737 static char_u *
5738skip_index(char_u *start)
5739{
5740 char_u *p = start;
5741
5742 if (*p == '[')
5743 {
5744 p = skipwhite(p + 1);
5745 (void)skip_expr(&p, NULL);
5746 p = skipwhite(p);
5747 if (*p == ']')
5748 return p + 1;
5749 return p;
5750 }
5751 // if (*p == '.')
5752 return to_name_end(p + 1, TRUE);
5753}
5754
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005755 void
5756vim9_declare_error(char_u *name)
5757{
5758 char *scope = "";
5759
5760 switch (*name)
5761 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005762 case 'g': scope = _("global"); break;
5763 case 'b': scope = _("buffer"); break;
5764 case 'w': scope = _("window"); break;
5765 case 't': scope = _("tab"); break;
5766 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005767 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005768 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005769 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005770 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005771 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005772 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005773 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005774 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005775 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005776}
5777
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005778/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005779 * For one assignment figure out the type of destination. Return it in "dest".
5780 * When not recognized "dest" is not set.
5781 * For an option "opt_flags" is set.
5782 * For a v:var "vimvaridx" is set.
5783 * "type" is set to the destination type if known, unchanted otherwise.
5784 * Return FAIL if an error message was given.
5785 */
5786 static int
5787get_var_dest(
5788 char_u *name,
5789 assign_dest_T *dest,
5790 int cmdidx,
5791 int *opt_flags,
5792 int *vimvaridx,
5793 type_T **type,
5794 cctx_T *cctx)
5795{
5796 char_u *p;
5797
5798 if (*name == '&')
5799 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005800 int cc;
5801 long numval;
5802 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005803
5804 *dest = dest_option;
5805 if (cmdidx == CMD_final || cmdidx == CMD_const)
5806 {
5807 emsg(_(e_const_option));
5808 return FAIL;
5809 }
5810 p = name;
5811 p = find_option_end(&p, opt_flags);
5812 if (p == NULL)
5813 {
5814 // cannot happen?
5815 emsg(_(e_letunexp));
5816 return FAIL;
5817 }
5818 cc = *p;
5819 *p = NUL;
5820 opt_type = get_option_value(skip_option_env_lead(name),
5821 &numval, NULL, *opt_flags);
5822 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005823 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005824 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005825 case gov_unknown:
5826 semsg(_(e_unknown_option), name);
5827 return FAIL;
5828 case gov_string:
5829 case gov_hidden_string:
5830 *type = &t_string;
5831 break;
5832 case gov_bool:
5833 case gov_hidden_bool:
5834 *type = &t_bool;
5835 break;
5836 case gov_number:
5837 case gov_hidden_number:
5838 *type = &t_number;
5839 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005840 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005841 }
5842 else if (*name == '$')
5843 {
5844 *dest = dest_env;
5845 *type = &t_string;
5846 }
5847 else if (*name == '@')
5848 {
5849 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5850 {
5851 emsg_invreg(name[1]);
5852 return FAIL;
5853 }
5854 *dest = dest_reg;
5855 *type = &t_string;
5856 }
5857 else if (STRNCMP(name, "g:", 2) == 0)
5858 {
5859 *dest = dest_global;
5860 }
5861 else if (STRNCMP(name, "b:", 2) == 0)
5862 {
5863 *dest = dest_buffer;
5864 }
5865 else if (STRNCMP(name, "w:", 2) == 0)
5866 {
5867 *dest = dest_window;
5868 }
5869 else if (STRNCMP(name, "t:", 2) == 0)
5870 {
5871 *dest = dest_tab;
5872 }
5873 else if (STRNCMP(name, "v:", 2) == 0)
5874 {
5875 typval_T *vtv;
5876 int di_flags;
5877
5878 *vimvaridx = find_vim_var(name + 2, &di_flags);
5879 if (*vimvaridx < 0)
5880 {
5881 semsg(_(e_variable_not_found_str), name);
5882 return FAIL;
5883 }
5884 // We use the current value of "sandbox" here, is that OK?
5885 if (var_check_ro(di_flags, name, FALSE))
5886 return FAIL;
5887 *dest = dest_vimvar;
5888 vtv = get_vim_var_tv(*vimvaridx);
5889 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5890 }
5891 return OK;
5892}
5893
5894/*
5895 * Generate a STORE instruction for "dest", not being "dest_local".
5896 * Return FAIL when out of memory.
5897 */
5898 static int
5899generate_store_var(
5900 cctx_T *cctx,
5901 assign_dest_T dest,
5902 int opt_flags,
5903 int vimvaridx,
5904 int scriptvar_idx,
5905 int scriptvar_sid,
5906 type_T *type,
5907 char_u *name)
5908{
5909 switch (dest)
5910 {
5911 case dest_option:
5912 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5913 opt_flags);
5914 case dest_global:
5915 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005916 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5917 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005918 case dest_buffer:
5919 // include b: with the name, easier to execute that way
5920 return generate_STORE(cctx, ISN_STOREB, 0, name);
5921 case dest_window:
5922 // include w: with the name, easier to execute that way
5923 return generate_STORE(cctx, ISN_STOREW, 0, name);
5924 case dest_tab:
5925 // include t: with the name, easier to execute that way
5926 return generate_STORE(cctx, ISN_STORET, 0, name);
5927 case dest_env:
5928 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5929 case dest_reg:
5930 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5931 case dest_vimvar:
5932 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5933 case dest_script:
5934 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005935 // "s:" may be included in the name.
5936 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005937 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005938 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5939 scriptvar_sid, scriptvar_idx, type);
5940 case dest_local:
5941 case dest_expr:
5942 // cannot happen
5943 break;
5944 }
5945 return FAIL;
5946}
5947
Bram Moolenaar752fc692021-01-04 21:57:11 +01005948 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005949generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5950{
5951 if (lhs->lhs_dest != dest_local)
5952 return generate_store_var(cctx, lhs->lhs_dest,
5953 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5954 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5955 lhs->lhs_type, lhs->lhs_name);
5956
5957 if (lhs->lhs_lvar != NULL)
5958 {
5959 garray_T *instr = &cctx->ctx_instr;
5960 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5961
5962 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5963 // ISN_STORENR
5964 if (lhs->lhs_lvar->lv_from_outer == 0
5965 && instr->ga_len == instr_count + 1
5966 && isn->isn_type == ISN_PUSHNR)
5967 {
5968 varnumber_T val = isn->isn_arg.number;
5969 garray_T *stack = &cctx->ctx_type_stack;
5970
5971 isn->isn_type = ISN_STORENR;
5972 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5973 isn->isn_arg.storenr.stnr_val = val;
5974 if (stack->ga_len > 0)
5975 --stack->ga_len;
5976 }
5977 else if (lhs->lhs_lvar->lv_from_outer > 0)
5978 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5979 lhs->lhs_lvar->lv_from_outer);
5980 else
5981 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5982 }
5983 return OK;
5984}
5985
5986 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005987is_decl_command(int cmdidx)
5988{
5989 return cmdidx == CMD_let || cmdidx == CMD_var
5990 || cmdidx == CMD_final || cmdidx == CMD_const;
5991}
5992
5993/*
5994 * Figure out the LHS type and other properties for an assignment or one item
5995 * of ":unlet" with an index.
5996 * Returns OK or FAIL.
5997 */
5998 static int
5999compile_lhs(
6000 char_u *var_start,
6001 lhs_T *lhs,
6002 int cmdidx,
6003 int heredoc,
6004 int oplen,
6005 cctx_T *cctx)
6006{
6007 char_u *var_end;
6008 int is_decl = is_decl_command(cmdidx);
6009
6010 CLEAR_POINTER(lhs);
6011 lhs->lhs_dest = dest_local;
6012 lhs->lhs_vimvaridx = -1;
6013 lhs->lhs_scriptvar_idx = -1;
6014
6015 // "dest_end" is the end of the destination, including "[expr]" or
6016 // ".name".
6017 // "var_end" is the end of the variable/option/etc. name.
6018 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6019 if (*var_start == '@')
6020 var_end = var_start + 2;
6021 else
6022 {
6023 // skip over the leading "&", "&l:", "&g:" and "$"
6024 var_end = skip_option_env_lead(var_start);
6025 var_end = to_name_end(var_end, TRUE);
6026 }
6027
6028 // "a: type" is declaring variable "a" with a type, not dict "a:".
6029 if (is_decl && lhs->lhs_dest_end == var_start + 2
6030 && lhs->lhs_dest_end[-1] == ':')
6031 --lhs->lhs_dest_end;
6032 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6033 --var_end;
6034
6035 // compute the length of the destination without "[expr]" or ".name"
6036 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006037 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006038 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6039 if (lhs->lhs_name == NULL)
6040 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006041
6042 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6043 // Something follows after the variable: "var[idx]" or "var.key".
6044 lhs->lhs_has_index = TRUE;
6045
Bram Moolenaar752fc692021-01-04 21:57:11 +01006046 if (heredoc)
6047 lhs->lhs_type = &t_list_string;
6048 else
6049 lhs->lhs_type = &t_any;
6050
6051 if (cctx->ctx_skip != SKIP_YES)
6052 {
6053 int declare_error = FALSE;
6054
6055 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6056 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6057 &lhs->lhs_type, cctx) == FAIL)
6058 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006059 if (lhs->lhs_dest != dest_local
6060 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006061 {
6062 // Specific kind of variable recognized.
6063 declare_error = is_decl;
6064 }
6065 else
6066 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006067 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006068 if (check_reserved_name(lhs->lhs_name) == FAIL)
6069 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006070
6071 if (lookup_local(var_start, lhs->lhs_varlen,
6072 &lhs->lhs_local_lvar, cctx) == OK)
6073 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6074 else
6075 {
6076 CLEAR_FIELD(lhs->lhs_arg_lvar);
6077 if (arg_exists(var_start, lhs->lhs_varlen,
6078 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6079 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6080 {
6081 if (is_decl)
6082 {
6083 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6084 return FAIL;
6085 }
6086 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6087 }
6088 }
6089 if (lhs->lhs_lvar != NULL)
6090 {
6091 if (is_decl)
6092 {
6093 semsg(_(e_variable_already_declared), lhs->lhs_name);
6094 return FAIL;
6095 }
6096 }
6097 else
6098 {
6099 int script_namespace = lhs->lhs_varlen > 1
6100 && STRNCMP(var_start, "s:", 2) == 0;
6101 int script_var = (script_namespace
6102 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006103 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006104 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006105 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006106 imported_T *import =
6107 find_imported(var_start, lhs->lhs_varlen, cctx);
6108
6109 if (script_namespace || script_var || import != NULL)
6110 {
6111 char_u *rawname = lhs->lhs_name
6112 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6113
6114 if (is_decl)
6115 {
6116 if (script_namespace)
6117 semsg(_(e_cannot_declare_script_variable_in_function),
6118 lhs->lhs_name);
6119 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006120 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006121 lhs->lhs_name);
6122 return FAIL;
6123 }
6124 else if (cctx->ctx_ufunc->uf_script_ctx_version
6125 == SCRIPT_VERSION_VIM9
6126 && script_namespace
6127 && !script_var && import == NULL)
6128 {
6129 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6130 return FAIL;
6131 }
6132
6133 lhs->lhs_dest = dest_script;
6134
6135 // existing script-local variables should have a type
6136 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6137 if (import != NULL)
6138 lhs->lhs_scriptvar_sid = import->imp_sid;
6139 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6140 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006141 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006142 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006143 lhs->lhs_scriptvar_sid, rawname,
6144 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6145 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006146 if (lhs->lhs_scriptvar_idx >= 0)
6147 {
6148 scriptitem_T *si = SCRIPT_ITEM(
6149 lhs->lhs_scriptvar_sid);
6150 svar_T *sv =
6151 ((svar_T *)si->sn_var_vals.ga_data)
6152 + lhs->lhs_scriptvar_idx;
6153 lhs->lhs_type = sv->sv_type;
6154 }
6155 }
6156 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006157 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006158 == FAIL)
6159 return FAIL;
6160 }
6161 }
6162
6163 if (declare_error)
6164 {
6165 vim9_declare_error(lhs->lhs_name);
6166 return FAIL;
6167 }
6168 }
6169
6170 // handle "a:name" as a name, not index "name" on "a"
6171 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6172 var_end = lhs->lhs_dest_end;
6173
6174 if (lhs->lhs_dest != dest_option)
6175 {
6176 if (is_decl && *var_end == ':')
6177 {
6178 char_u *p;
6179
6180 // parse optional type: "let var: type = expr"
6181 if (!VIM_ISWHITE(var_end[1]))
6182 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006183 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006184 return FAIL;
6185 }
6186 p = skipwhite(var_end + 1);
6187 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6188 if (lhs->lhs_type == NULL)
6189 return FAIL;
6190 lhs->lhs_has_type = TRUE;
6191 }
6192 else if (lhs->lhs_lvar != NULL)
6193 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6194 }
6195
Bram Moolenaare42939a2021-04-05 17:11:17 +02006196 if (oplen == 3 && !heredoc
6197 && lhs->lhs_dest != dest_global
6198 && !lhs->lhs_has_index
6199 && lhs->lhs_type->tt_type != VAR_STRING
6200 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006201 {
6202 emsg(_(e_can_only_concatenate_to_string));
6203 return FAIL;
6204 }
6205
6206 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6207 && cctx->ctx_skip != SKIP_YES)
6208 {
6209 if (oplen > 1 && !heredoc)
6210 {
6211 // +=, /=, etc. require an existing variable
6212 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6213 return FAIL;
6214 }
6215 if (!is_decl)
6216 {
6217 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6218 return FAIL;
6219 }
6220
Bram Moolenaar3f327882021-03-17 20:56:38 +01006221 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006222 if ((lhs->lhs_type->tt_type == VAR_FUNC
6223 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006224 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006225 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006226
6227 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006228 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6229 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6230 if (lhs->lhs_lvar == NULL)
6231 return FAIL;
6232 lhs->lhs_new_local = TRUE;
6233 }
6234
6235 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006236 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006237 {
6238 // Something follows after the variable: "var[idx]" or "var.key".
6239 // TODO: should we also handle "->func()" here?
6240 if (is_decl)
6241 {
6242 emsg(_(e_cannot_use_index_when_declaring_variable));
6243 return FAIL;
6244 }
6245
6246 if (var_start[lhs->lhs_varlen] == '['
6247 || var_start[lhs->lhs_varlen] == '.')
6248 {
6249 char_u *after = var_start + lhs->lhs_varlen;
6250 char_u *p;
6251
6252 // Only the last index is used below, if there are others
6253 // before it generate code for the expression. Thus for
6254 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6255 for (;;)
6256 {
6257 p = skip_index(after);
6258 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006259 {
6260 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006261 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006262 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006263 after = p;
6264 }
6265 if (after > var_start + lhs->lhs_varlen)
6266 {
6267 lhs->lhs_varlen = after - var_start;
6268 lhs->lhs_dest = dest_expr;
6269 // We don't know the type before evaluating the expression,
6270 // use "any" until then.
6271 lhs->lhs_type = &t_any;
6272 }
6273
Bram Moolenaar752fc692021-01-04 21:57:11 +01006274 if (lhs->lhs_type->tt_member == NULL)
6275 lhs->lhs_member_type = &t_any;
6276 else
6277 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6278 }
6279 else
6280 {
6281 semsg("Not supported yet: %s", var_start);
6282 return FAIL;
6283 }
6284 }
6285 return OK;
6286}
6287
6288/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006289 * Figure out the LHS and check a few errors.
6290 */
6291 static int
6292compile_assign_lhs(
6293 char_u *var_start,
6294 lhs_T *lhs,
6295 int cmdidx,
6296 int is_decl,
6297 int heredoc,
6298 int oplen,
6299 cctx_T *cctx)
6300{
6301 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6302 return FAIL;
6303
6304 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6305 {
6306 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6307 return FAIL;
6308 }
6309 if (!is_decl && lhs->lhs_lvar != NULL
6310 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6311 {
6312 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6313 return FAIL;
6314 }
6315 return OK;
6316}
6317
6318/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006319 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6320 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006321 */
6322 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006323compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006324 char_u *var_start,
6325 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006326 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006327 cctx_T *cctx)
6328{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006329 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006330 char_u *p;
6331 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006332 int need_white_before = TRUE;
6333 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006334
Bram Moolenaar752fc692021-01-04 21:57:11 +01006335 p = var_start + varlen;
6336 if (*p == '[')
6337 {
6338 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006339 if (*p == ':')
6340 {
6341 // empty first index, push zero
6342 r = generate_PUSHNR(cctx, 0);
6343 need_white_before = FALSE;
6344 }
6345 else
6346 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006347
6348 if (r == OK && *skipwhite(p) == ':')
6349 {
6350 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006351 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006352 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006353 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006354 empty_second = *skipwhite(p + 1) == ']';
6355 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6356 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006357 {
6358 semsg(_(e_white_space_required_before_and_after_str_at_str),
6359 ":", p);
6360 return FAIL;
6361 }
6362 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006363 if (*p == ']')
6364 // empty second index, push "none"
6365 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6366 else
6367 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006368 }
6369
Bram Moolenaar752fc692021-01-04 21:57:11 +01006370 if (r == OK && *skipwhite(p) != ']')
6371 {
6372 // this should not happen
6373 emsg(_(e_missbrac));
6374 r = FAIL;
6375 }
6376 }
6377 else // if (*p == '.')
6378 {
6379 char_u *key_end = to_name_end(p + 1, TRUE);
6380 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6381
6382 r = generate_PUSHS(cctx, key);
6383 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006384 return r;
6385}
6386
6387/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006388 * For a LHS with an index, load the variable to be indexed.
6389 */
6390 static int
6391compile_load_lhs(
6392 lhs_T *lhs,
6393 char_u *var_start,
6394 type_T *rhs_type,
6395 cctx_T *cctx)
6396{
6397 if (lhs->lhs_dest == dest_expr)
6398 {
6399 size_t varlen = lhs->lhs_varlen;
6400 int c = var_start[varlen];
6401 char_u *p = var_start;
6402 garray_T *stack = &cctx->ctx_type_stack;
6403
6404 // Evaluate "ll[expr]" of "ll[expr][idx]"
6405 var_start[varlen] = NUL;
6406 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6407 {
6408 // this should not happen
6409 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006410 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006411 return FAIL;
6412 }
6413 var_start[varlen] = c;
6414
6415 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6416 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6417 // now we can properly check the type
6418 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6419 && rhs_type != &t_void
6420 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6421 FALSE, FALSE) == FAIL)
6422 return FAIL;
6423 }
6424 else
6425 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6426 lhs->lhs_lvar, lhs->lhs_type);
6427 return OK;
6428}
6429
6430/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006431 * Produce code for loading "lhs" and also take care of an index.
6432 * Return OK/FAIL.
6433 */
6434 static int
6435compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6436{
6437 compile_load_lhs(lhs, var_start, NULL, cctx);
6438
6439 if (lhs->lhs_has_index)
6440 {
6441 int range = FALSE;
6442
6443 // Get member from list or dict. First compile the
6444 // index value.
6445 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6446 return FAIL;
6447 if (range)
6448 {
6449 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6450 var_start);
6451 return FAIL;
6452 }
6453
6454 // Get the member.
6455 if (compile_member(FALSE, cctx) == FAIL)
6456 return FAIL;
6457 }
6458 return OK;
6459}
6460
6461/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006462 * Assignment to a list or dict member, or ":unlet" for the item, using the
6463 * information in "lhs".
6464 * Returns OK or FAIL.
6465 */
6466 static int
6467compile_assign_unlet(
6468 char_u *var_start,
6469 lhs_T *lhs,
6470 int is_assign,
6471 type_T *rhs_type,
6472 cctx_T *cctx)
6473{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006474 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006475 garray_T *stack = &cctx->ctx_type_stack;
6476 int range = FALSE;
6477
Bram Moolenaar68452172021-04-12 21:21:02 +02006478 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006479 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006480 if (is_assign && range && lhs->lhs_type != &t_blob
6481 && lhs->lhs_type != &t_any)
6482 {
6483 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6484 return FAIL;
6485 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006486
6487 if (lhs->lhs_type == &t_any)
6488 {
6489 // Index on variable of unknown type: check at runtime.
6490 dest_type = VAR_ANY;
6491 }
6492 else
6493 {
6494 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006495 if (dest_type == VAR_DICT && range)
6496 {
6497 emsg(e_cannot_use_range_with_dictionary);
6498 return FAIL;
6499 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006500 if (dest_type == VAR_DICT
6501 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006502 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006503 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006504 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006505 type_T *type;
6506
6507 if (range)
6508 {
6509 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6510 if (need_type(type, &t_number,
6511 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006512 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006513 }
6514 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6515 if ((dest_type != VAR_BLOB || type != &t_special)
6516 && need_type(type, &t_number,
6517 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006518 return FAIL;
6519 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006520 }
6521
6522 // Load the dict or list. On the stack we then have:
6523 // - value (for assignment, not for :unlet)
6524 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006525 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006526 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006527 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6528 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006529
Bram Moolenaar68452172021-04-12 21:21:02 +02006530 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6531 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006532 {
6533 if (is_assign)
6534 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006535 if (range)
6536 {
6537 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6538 return FAIL;
6539 }
6540 else
6541 {
6542 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006543
Bram Moolenaar68452172021-04-12 21:21:02 +02006544 if (isn == NULL)
6545 return FAIL;
6546 isn->isn_arg.vartype = dest_type;
6547 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006548 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006549 else if (range)
6550 {
6551 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6552 return FAIL;
6553 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006554 else
6555 {
6556 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6557 return FAIL;
6558 }
6559 }
6560 else
6561 {
6562 emsg(_(e_indexable_type_required));
6563 return FAIL;
6564 }
6565
6566 return OK;
6567}
6568
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006569/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006570 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006571 * "let name"
6572 * "var name = expr"
6573 * "final name = expr"
6574 * "const name = expr"
6575 * "name = expr"
6576 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006577 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006578 * Return NULL for an error.
6579 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 */
6581 static char_u *
6582compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6583{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006584 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006585 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006586 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 char_u *ret = NULL;
6588 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006589 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006592 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006593 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 int oplen = 0;
6595 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006596 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006597 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006599 int is_decl = is_decl_command(cmdidx);
6600 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006601 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006603 // Skip over the "var" or "[var, var]" to get to any "=".
6604 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6605 if (p == NULL)
6606 return *arg == '[' ? arg : NULL;
6607
6608 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006609 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006610 // TODO: should we allow this, and figure out type inference from list
6611 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006612 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 return NULL;
6614 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006615 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 sp = p;
6618 p = skipwhite(p);
6619 op = p;
6620 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006621
6622 if (var_count > 0 && oplen == 0)
6623 // can be something like "[1, 2]->func()"
6624 return arg;
6625
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006626 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006627 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006628 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006629 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006630 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006631 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6632 {
6633 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6634 oplen = 2;
6635 incdec = TRUE;
6636 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006638 if (heredoc)
6639 {
6640 list_T *l;
6641 listitem_T *li;
6642
6643 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006644 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006645 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006646 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006647 if (l == NULL)
6648 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006649
Bram Moolenaar078269b2020-09-21 20:35:55 +02006650 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006652 // Push each line and the create the list.
6653 FOR_ALL_LIST_ITEMS(l, li)
6654 {
6655 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6656 li->li_tv.vval.v_string = NULL;
6657 }
6658 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660 list_free(l);
6661 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006662 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006664 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006666 char_u *wp;
6667
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006668 // for "[var, var] = expr" evaluate the expression here, loop over the
6669 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006670 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006671
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006672 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006673 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006674 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006675 if (compile_expr0(&p, cctx) == FAIL)
6676 return NULL;
6677 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006679 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006680 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006681 type_T *stacktype;
6682
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006683 stacktype = stack->ga_len == 0 ? &t_void
6684 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006685 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006687 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006688 goto theend;
6689 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006690 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006691 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006692 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006693 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006694 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6695 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006696 if (stacktype->tt_member != NULL)
6697 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006698 }
6699 }
6700
6701 /*
6702 * Loop over variables in "[var, var] = expr".
6703 * For "var = expr" and "let var: type" this is done only once.
6704 */
6705 if (var_count > 0)
6706 var_start = skipwhite(arg + 1); // skip over the "["
6707 else
6708 var_start = arg;
6709 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6710 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006711 int instr_count = -1;
6712
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006713 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6714 {
6715 // Ignore underscore in "[a, _, b] = list".
6716 if (var_count > 0)
6717 {
6718 var_start = skipwhite(var_start + 2);
6719 continue;
6720 }
6721 emsg(_(e_cannot_use_underscore_here));
6722 goto theend;
6723 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006724 vim_free(lhs.lhs_name);
6725
6726 /*
6727 * Figure out the LHS type and other properties.
6728 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006729 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6730 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006731 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006732 if (!heredoc)
6733 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006734 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006735 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006736 if (oplen > 0 && var_count == 0)
6737 {
6738 // skip over the "=" and the expression
6739 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006740 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006741 }
6742 }
6743 else if (oplen > 0)
6744 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006745 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006746 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006747
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006748 // For "var = expr" evaluate the expression.
6749 if (var_count == 0)
6750 {
6751 int r;
6752
6753 // for "+=", "*=", "..=" etc. first load the current value
6754 if (*op != '=')
6755 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006756 if (compile_load_lhs_with_index(&lhs, var_start,
6757 cctx) == FAIL)
6758 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006759 }
6760
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006761 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006762 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006763 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006764 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006765 r = generate_PUSHNR(cctx, 1);
6766 }
6767 else
6768 {
6769 // Temporarily hide the new local variable here, it is
6770 // not available to this expression.
6771 if (lhs.lhs_new_local)
6772 --cctx->ctx_locals.ga_len;
6773 wp = op + oplen;
6774 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6775 {
6776 if (lhs.lhs_new_local)
6777 ++cctx->ctx_locals.ga_len;
6778 goto theend;
6779 }
6780 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006781 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006782 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006783 if (r == FAIL)
6784 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006785 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006786 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006787 else if (semicolon && var_idx == var_count - 1)
6788 {
6789 // For "[var; var] = expr" get the rest of the list
6790 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6791 goto theend;
6792 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006793 else
6794 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006795 // For "[var, var] = expr" get the "var_idx" item from the
6796 // list.
6797 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006798 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006799 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006800
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006801 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006802 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006803 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006804 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006805 if ((rhs_type->tt_type == VAR_FUNC
6806 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006807 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006808 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006809 goto theend;
6810
Bram Moolenaar752fc692021-01-04 21:57:11 +01006811 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006812 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006813 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006814 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006815 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006816 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006817 }
6818 else
6819 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006820 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006821 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006822 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006823 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006824 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006825 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006826 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006827 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006828 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006829 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006830 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006831 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006832 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006833 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006834 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006835
Bram Moolenaar77709b12021-04-03 21:01:01 +02006836 // Without operator check type here, otherwise below.
6837 // Use the line number of the assignment.
6838 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006839 if (lhs.lhs_has_index)
6840 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006841 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006842 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006843 goto theend;
6844 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006845 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006846 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006847 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006848 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006849 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006850 else if (cmdidx == CMD_final)
6851 {
6852 emsg(_(e_final_requires_a_value));
6853 goto theend;
6854 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006855 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006856 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006857 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006858 goto theend;
6859 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006860 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006861 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006862 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006863 goto theend;
6864 }
6865 else
6866 {
6867 // variables are always initialized
6868 if (ga_grow(instr, 1) == FAIL)
6869 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006870 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006871 {
6872 case VAR_BOOL:
6873 generate_PUSHBOOL(cctx, VVAL_FALSE);
6874 break;
6875 case VAR_FLOAT:
6876#ifdef FEAT_FLOAT
6877 generate_PUSHF(cctx, 0.0);
6878#endif
6879 break;
6880 case VAR_STRING:
6881 generate_PUSHS(cctx, NULL);
6882 break;
6883 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006884 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006885 break;
6886 case VAR_FUNC:
6887 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6888 break;
6889 case VAR_LIST:
6890 generate_NEWLIST(cctx, 0);
6891 break;
6892 case VAR_DICT:
6893 generate_NEWDICT(cctx, 0);
6894 break;
6895 case VAR_JOB:
6896 generate_PUSHJOB(cctx, NULL);
6897 break;
6898 case VAR_CHANNEL:
6899 generate_PUSHCHANNEL(cctx, NULL);
6900 break;
6901 case VAR_NUMBER:
6902 case VAR_UNKNOWN:
6903 case VAR_ANY:
6904 case VAR_PARTIAL:
6905 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006906 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006907 case VAR_SPECIAL: // cannot happen
6908 generate_PUSHNR(cctx, 0);
6909 break;
6910 }
6911 }
6912 if (var_count == 0)
6913 end = p;
6914 }
6915
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006916 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006917 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006918 break;
6919
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006920 if (oplen > 0 && *op != '=')
6921 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006922 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006923 type_T *stacktype;
6924
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006925 if (*op == '.')
6926 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006927 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006928 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006929 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006930 if (
6931#ifdef FEAT_FLOAT
6932 // If variable is float operation with number is OK.
6933 !(expected == &t_float && stacktype == &t_number) &&
6934#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006935 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006936 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006937 goto theend;
6938
6939 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006940 {
6941 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6942 goto theend;
6943 }
6944 else if (*op == '+')
6945 {
6946 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006947 operator_type(lhs.lhs_member_type, stacktype),
6948 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006949 goto theend;
6950 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006951 else if (generate_two_op(cctx, op) == FAIL)
6952 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954
Bram Moolenaar752fc692021-01-04 21:57:11 +01006955 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006956 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006957 // Use the info in "lhs" to store the value at the index in the
6958 // list or dict.
6959 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6960 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006961 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006962 }
6963 else
6964 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006965 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006966 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006967 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006968 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006969 generate_LOCKCONST(cctx);
6970
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006971 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006972 && (lhs.lhs_type->tt_type == VAR_DICT
6973 || lhs.lhs_type->tt_type == VAR_LIST)
6974 && lhs.lhs_type->tt_member != NULL
6975 && lhs.lhs_type->tt_member != &t_any
6976 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006977 // Set the type in the list or dict, so that it can be checked,
6978 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006979 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006980
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006981 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6982 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006983 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006984
6985 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006986 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006988
6989 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006990 if (var_count > 0 && !semicolon)
6991 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006992 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006993 goto theend;
6994 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006995
Bram Moolenaarb2097502020-07-19 17:17:02 +02006996 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997
6998theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006999 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 return ret;
7001}
7002
7003/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007004 * Check for an assignment at "eap->cmd", compile it if found.
7005 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7006 */
7007 static int
7008may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7009{
7010 char_u *pskip;
7011 char_u *p;
7012
7013 // Assuming the command starts with a variable or function name,
7014 // find what follows.
7015 // Skip over "var.member", "var[idx]" and the like.
7016 // Also "&opt = val", "$ENV = val" and "@r = val".
7017 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7018 ? eap->cmd + 1 : eap->cmd;
7019 p = to_name_end(pskip, TRUE);
7020 if (p > eap->cmd && *p != NUL)
7021 {
7022 char_u *var_end;
7023 int oplen;
7024 int heredoc;
7025
7026 if (eap->cmd[0] == '@')
7027 var_end = eap->cmd + 2;
7028 else
7029 var_end = find_name_end(pskip, NULL, NULL,
7030 FNE_CHECK_START | FNE_INCL_BR);
7031 oplen = assignment_len(skipwhite(var_end), &heredoc);
7032 if (oplen > 0)
7033 {
7034 size_t len = p - eap->cmd;
7035
7036 // Recognize an assignment if we recognize the variable
7037 // name:
7038 // "g:var = expr"
7039 // "local = expr" where "local" is a local var.
7040 // "script = expr" where "script" is a script-local var.
7041 // "import = expr" where "import" is an imported var
7042 // "&opt = expr"
7043 // "$ENV = expr"
7044 // "@r = expr"
7045 if (*eap->cmd == '&'
7046 || *eap->cmd == '$'
7047 || *eap->cmd == '@'
7048 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007049 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007050 {
7051 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7052 if (*line == NULL || *line == eap->cmd)
7053 return FAIL;
7054 return OK;
7055 }
7056 }
7057 }
7058
7059 if (*eap->cmd == '[')
7060 {
7061 // [var, var] = expr
7062 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7063 if (*line == NULL)
7064 return FAIL;
7065 if (*line != eap->cmd)
7066 return OK;
7067 }
7068 return NOTDONE;
7069}
7070
7071/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007072 * Check if "name" can be "unlet".
7073 */
7074 int
7075check_vim9_unlet(char_u *name)
7076{
7077 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7078 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007079 // "unlet s:var" is allowed in legacy script.
7080 if (*name == 's' && !script_is_vim9())
7081 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007082 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007083 return FAIL;
7084 }
7085 return OK;
7086}
7087
7088/*
7089 * Callback passed to ex_unletlock().
7090 */
7091 static int
7092compile_unlet(
7093 lval_T *lvp,
7094 char_u *name_end,
7095 exarg_T *eap,
7096 int deep UNUSED,
7097 void *coookie)
7098{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007099 cctx_T *cctx = coookie;
7100 char_u *p = lvp->ll_name;
7101 int cc = *name_end;
7102 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007103
Bram Moolenaar752fc692021-01-04 21:57:11 +01007104 if (cctx->ctx_skip == SKIP_YES)
7105 return OK;
7106
7107 *name_end = NUL;
7108 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007109 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007110 // :unlet $ENV_VAR
7111 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7112 }
7113 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7114 {
7115 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007116
Bram Moolenaar752fc692021-01-04 21:57:11 +01007117 // This is similar to assigning: lookup the list/dict, compile the
7118 // idx/key. Then instead of storing the value unlet the item.
7119 // unlet {list}[idx]
7120 // unlet {dict}[key] dict.key
7121 //
7122 // Figure out the LHS type and other properties.
7123 //
7124 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7125
7126 // : unlet an indexed item
7127 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007128 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007129 iemsg("called compile_lhs() without an index");
7130 ret = FAIL;
7131 }
7132 else
7133 {
7134 // Use the info in "lhs" to unlet the item at the index in the
7135 // list or dict.
7136 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007137 }
7138
Bram Moolenaar752fc692021-01-04 21:57:11 +01007139 vim_free(lhs.lhs_name);
7140 }
7141 else if (check_vim9_unlet(p) == FAIL)
7142 {
7143 ret = FAIL;
7144 }
7145 else
7146 {
7147 // Normal name. Only supports g:, w:, t: and b: namespaces.
7148 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007149 }
7150
Bram Moolenaar752fc692021-01-04 21:57:11 +01007151 *name_end = cc;
7152 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007153}
7154
7155/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007156 * Callback passed to ex_unletlock().
7157 */
7158 static int
7159compile_lock_unlock(
7160 lval_T *lvp,
7161 char_u *name_end,
7162 exarg_T *eap,
7163 int deep UNUSED,
7164 void *coookie)
7165{
7166 cctx_T *cctx = coookie;
7167 int cc = *name_end;
7168 char_u *p = lvp->ll_name;
7169 int ret = OK;
7170 size_t len;
7171 char_u *buf;
7172
7173 if (cctx->ctx_skip == SKIP_YES)
7174 return OK;
7175
7176 // Cannot use :lockvar and :unlockvar on local variables.
7177 if (p[1] != ':')
7178 {
7179 char_u *end = skip_var_one(p, FALSE);
7180
7181 if (lookup_local(p, end - p, NULL, cctx) == OK)
7182 {
7183 emsg(_(e_cannot_lock_unlock_local_variable));
7184 return FAIL;
7185 }
7186 }
7187
7188 // Checking is done at runtime.
7189 *name_end = NUL;
7190 len = name_end - p + 20;
7191 buf = alloc(len);
7192 if (buf == NULL)
7193 ret = FAIL;
7194 else
7195 {
7196 vim_snprintf((char *)buf, len, "%s %s",
7197 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7198 p);
7199 ret = generate_EXEC(cctx, buf);
7200
7201 vim_free(buf);
7202 *name_end = cc;
7203 }
7204 return ret;
7205}
7206
7207/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007208 * compile "unlet var", "lock var" and "unlock var"
7209 * "arg" points to "var".
7210 */
7211 static char_u *
7212compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7213{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007214 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7215 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7216 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007217 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7218}
7219
7220/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007221 * Compile an :import command.
7222 */
7223 static char_u *
7224compile_import(char_u *arg, cctx_T *cctx)
7225{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007226 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227}
7228
7229/*
7230 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7231 */
7232 static int
7233compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7234{
7235 garray_T *instr = &cctx->ctx_instr;
7236 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7237
7238 if (endlabel == NULL)
7239 return FAIL;
7240 endlabel->el_next = *el;
7241 *el = endlabel;
7242 endlabel->el_end_label = instr->ga_len;
7243
7244 generate_JUMP(cctx, when, 0);
7245 return OK;
7246}
7247
7248 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007249compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007250{
7251 garray_T *instr = &cctx->ctx_instr;
7252
7253 while (*el != NULL)
7254 {
7255 endlabel_T *cur = (*el);
7256 isn_T *isn;
7257
7258 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007259 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260 *el = cur->el_next;
7261 vim_free(cur);
7262 }
7263}
7264
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007265 static void
7266compile_free_jump_to_end(endlabel_T **el)
7267{
7268 while (*el != NULL)
7269 {
7270 endlabel_T *cur = (*el);
7271
7272 *el = cur->el_next;
7273 vim_free(cur);
7274 }
7275}
7276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277/*
7278 * Create a new scope and set up the generic items.
7279 */
7280 static scope_T *
7281new_scope(cctx_T *cctx, scopetype_T type)
7282{
7283 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7284
7285 if (scope == NULL)
7286 return NULL;
7287 scope->se_outer = cctx->ctx_scope;
7288 cctx->ctx_scope = scope;
7289 scope->se_type = type;
7290 scope->se_local_count = cctx->ctx_locals.ga_len;
7291 return scope;
7292}
7293
7294/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007295 * Free the current scope and go back to the outer scope.
7296 */
7297 static void
7298drop_scope(cctx_T *cctx)
7299{
7300 scope_T *scope = cctx->ctx_scope;
7301
7302 if (scope == NULL)
7303 {
7304 iemsg("calling drop_scope() without a scope");
7305 return;
7306 }
7307 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007308 switch (scope->se_type)
7309 {
7310 case IF_SCOPE:
7311 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7312 case FOR_SCOPE:
7313 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7314 case WHILE_SCOPE:
7315 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7316 case TRY_SCOPE:
7317 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7318 case NO_SCOPE:
7319 case BLOCK_SCOPE:
7320 break;
7321 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007322 vim_free(scope);
7323}
7324
7325/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326 * compile "if expr"
7327 *
7328 * "if expr" Produces instructions:
7329 * EVAL expr Push result of "expr"
7330 * JUMP_IF_FALSE end
7331 * ... body ...
7332 * end:
7333 *
7334 * "if expr | else" Produces instructions:
7335 * EVAL expr Push result of "expr"
7336 * JUMP_IF_FALSE else
7337 * ... body ...
7338 * JUMP_ALWAYS end
7339 * else:
7340 * ... body ...
7341 * end:
7342 *
7343 * "if expr1 | elseif expr2 | else" Produces instructions:
7344 * EVAL expr Push result of "expr"
7345 * JUMP_IF_FALSE elseif
7346 * ... body ...
7347 * JUMP_ALWAYS end
7348 * elseif:
7349 * EVAL expr Push result of "expr"
7350 * JUMP_IF_FALSE else
7351 * ... body ...
7352 * JUMP_ALWAYS end
7353 * else:
7354 * ... body ...
7355 * end:
7356 */
7357 static char_u *
7358compile_if(char_u *arg, cctx_T *cctx)
7359{
7360 char_u *p = arg;
7361 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007362 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007364 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007365 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366
Bram Moolenaara5565e42020-05-09 15:44:01 +02007367 CLEAR_FIELD(ppconst);
7368 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007369 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007370 clear_ppconst(&ppconst);
7371 return NULL;
7372 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007373 if (!ends_excmd2(arg, skipwhite(p)))
7374 {
7375 semsg(_(e_trailing_arg), p);
7376 return NULL;
7377 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007378 if (cctx->ctx_skip == SKIP_YES)
7379 clear_ppconst(&ppconst);
7380 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007381 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007382 int error = FALSE;
7383 int v;
7384
Bram Moolenaara5565e42020-05-09 15:44:01 +02007385 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007386 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007387 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007388 if (error)
7389 return NULL;
7390 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007391 }
7392 else
7393 {
7394 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007395 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007396 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007397 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007398 if (bool_on_stack(cctx) == FAIL)
7399 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401
Bram Moolenaara91a7132021-03-25 21:12:15 +01007402 // CMDMOD_REV must come before the jump
7403 generate_undo_cmdmods(cctx);
7404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405 scope = new_scope(cctx, IF_SCOPE);
7406 if (scope == NULL)
7407 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007408 scope->se_skip_save = skip_save;
7409 // "is_had_return" will be reset if any block does not end in :return
7410 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007412 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007413 {
7414 // "where" is set when ":elseif", "else" or ":endif" is found
7415 scope->se_u.se_if.is_if_label = instr->ga_len;
7416 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7417 }
7418 else
7419 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420
Bram Moolenaarced68a02021-01-24 17:53:47 +01007421#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007422 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007423 && skip_save != SKIP_YES)
7424 {
7425 // generated a profile start, need to generate a profile end, since it
7426 // won't be done after returning
7427 cctx->ctx_skip = SKIP_NOT;
7428 generate_instr(cctx, ISN_PROF_END);
7429 cctx->ctx_skip = SKIP_YES;
7430 }
7431#endif
7432
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433 return p;
7434}
7435
7436 static char_u *
7437compile_elseif(char_u *arg, cctx_T *cctx)
7438{
7439 char_u *p = arg;
7440 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007441 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007442 isn_T *isn;
7443 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007444 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007445 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446
7447 if (scope == NULL || scope->se_type != IF_SCOPE)
7448 {
7449 emsg(_(e_elseif_without_if));
7450 return NULL;
7451 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007452 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007453 if (!cctx->ctx_had_return)
7454 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455
Bram Moolenaarced68a02021-01-24 17:53:47 +01007456 if (cctx->ctx_skip == SKIP_NOT)
7457 {
7458 // previous block was executed, this one and following will not
7459 cctx->ctx_skip = SKIP_YES;
7460 scope->se_u.se_if.is_seen_skip_not = TRUE;
7461 }
7462 if (scope->se_u.se_if.is_seen_skip_not)
7463 {
7464 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007465 // Do not count the "elseif" for profiling and cmdmod
7466 instr->ga_len = current_instr_idx(cctx);
7467
Bram Moolenaarced68a02021-01-24 17:53:47 +01007468 skip_expr_cctx(&p, cctx);
7469 return p;
7470 }
7471
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007472 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007473 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007474 int moved_cmdmod = FALSE;
7475
7476 // Move any CMDMOD instruction to after the jump
7477 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7478 {
7479 if (ga_grow(instr, 1) == FAIL)
7480 return NULL;
7481 ((isn_T *)instr->ga_data)[instr->ga_len] =
7482 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7483 --instr->ga_len;
7484 moved_cmdmod = TRUE;
7485 }
7486
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007487 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007489 return NULL;
7490 // previous "if" or "elseif" jumps here
7491 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7492 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007493 if (moved_cmdmod)
7494 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007497 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007498 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007499 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007500 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007501 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007502#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007503 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007504 {
7505 // the previous block was skipped, need to profile this line
7506 generate_instr(cctx, ISN_PROF_START);
7507 instr_count = instr->ga_len;
7508 }
7509#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007510 if (cctx->ctx_compile_type == CT_DEBUG)
7511 {
7512 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007513 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007514 instr_count = instr->ga_len;
7515 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007516 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007517 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007518 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007519 clear_ppconst(&ppconst);
7520 return NULL;
7521 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007522 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007523 if (!ends_excmd2(arg, skipwhite(p)))
7524 {
7525 semsg(_(e_trailing_arg), p);
7526 return NULL;
7527 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007528 if (scope->se_skip_save == SKIP_YES)
7529 clear_ppconst(&ppconst);
7530 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007531 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007532 int error = FALSE;
7533 int v;
7534
Bram Moolenaar7f141552020-05-09 17:35:53 +02007535 // The expression results in a constant.
7536 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007537 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7538 if (error)
7539 return NULL;
7540 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007541 clear_ppconst(&ppconst);
7542 scope->se_u.se_if.is_if_label = -1;
7543 }
7544 else
7545 {
7546 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007547 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007548 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007549 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007550 if (bool_on_stack(cctx) == FAIL)
7551 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552
Bram Moolenaara91a7132021-03-25 21:12:15 +01007553 // CMDMOD_REV must come before the jump
7554 generate_undo_cmdmods(cctx);
7555
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007556 // "where" is set when ":elseif", "else" or ":endif" is found
7557 scope->se_u.se_if.is_if_label = instr->ga_len;
7558 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7559 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007560
7561 return p;
7562}
7563
7564 static char_u *
7565compile_else(char_u *arg, cctx_T *cctx)
7566{
7567 char_u *p = arg;
7568 garray_T *instr = &cctx->ctx_instr;
7569 isn_T *isn;
7570 scope_T *scope = cctx->ctx_scope;
7571
7572 if (scope == NULL || scope->se_type != IF_SCOPE)
7573 {
7574 emsg(_(e_else_without_if));
7575 return NULL;
7576 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007577 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007578 if (!cctx->ctx_had_return)
7579 scope->se_u.se_if.is_had_return = FALSE;
7580 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007581
Bram Moolenaarced68a02021-01-24 17:53:47 +01007582#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007583 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007584 {
7585 if (cctx->ctx_skip == SKIP_NOT
7586 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7587 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007588 // the previous block was executed, do not count "else" for
7589 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007590 --instr->ga_len;
7591 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7592 {
7593 // the previous block was not executed, this one will, do count the
7594 // "else" for profiling
7595 cctx->ctx_skip = SKIP_NOT;
7596 generate_instr(cctx, ISN_PROF_END);
7597 generate_instr(cctx, ISN_PROF_START);
7598 cctx->ctx_skip = SKIP_YES;
7599 }
7600 }
7601#endif
7602
7603 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007604 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007605 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007606 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007607 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007608 if (!cctx->ctx_had_return
7609 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7610 JUMP_ALWAYS, cctx) == FAIL)
7611 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007612 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007613
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007614 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007615 {
7616 if (scope->se_u.se_if.is_if_label >= 0)
7617 {
7618 // previous "if" or "elseif" jumps here
7619 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7620 isn->isn_arg.jump.jump_where = instr->ga_len;
7621 scope->se_u.se_if.is_if_label = -1;
7622 }
7623 }
7624
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007625 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007626 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007628
7629 return p;
7630}
7631
7632 static char_u *
7633compile_endif(char_u *arg, cctx_T *cctx)
7634{
7635 scope_T *scope = cctx->ctx_scope;
7636 ifscope_T *ifscope;
7637 garray_T *instr = &cctx->ctx_instr;
7638 isn_T *isn;
7639
Bram Moolenaarfa984412021-03-25 22:15:28 +01007640 if (misplaced_cmdmod(cctx))
7641 return NULL;
7642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007643 if (scope == NULL || scope->se_type != IF_SCOPE)
7644 {
7645 emsg(_(e_endif_without_if));
7646 return NULL;
7647 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007648 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007649 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007650 if (!cctx->ctx_had_return)
7651 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007652
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007653 if (scope->se_u.se_if.is_if_label >= 0)
7654 {
7655 // previous "if" or "elseif" jumps here
7656 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7657 isn->isn_arg.jump.jump_where = instr->ga_len;
7658 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007659 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007660 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007661
7662#ifdef FEAT_PROFILE
7663 // even when skipping we count the endif as executed, unless the block it's
7664 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007665 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007666 && scope->se_skip_save != SKIP_YES)
7667 {
7668 cctx->ctx_skip = SKIP_NOT;
7669 generate_instr(cctx, ISN_PROF_START);
7670 }
7671#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007672 cctx->ctx_skip = scope->se_skip_save;
7673
7674 // If all the blocks end in :return and there is an :else then the
7675 // had_return flag is set.
7676 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007677
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007678 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007679 return arg;
7680}
7681
7682/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007683 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684 *
7685 * Produces instructions:
7686 * PUSHNR -1
7687 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007688 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007689 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7690 * - if beyond end, jump to "end"
7691 * - otherwise get item from list and push it
7692 * STORE var Store item in "var"
7693 * ... body ...
7694 * JUMP top Jump back to repeat
7695 * end: DROP Drop the result of "expr"
7696 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007697 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7698 * UNPACK 2 Split item in 2
7699 * STORE var1 Store item in "var1"
7700 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007701 */
7702 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007703compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007705 char_u *arg;
7706 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007707 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007708 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007709 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007710 int var_count = 0;
7711 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007712 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713 garray_T *stack = &cctx->ctx_type_stack;
7714 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007715 lvar_T *loop_lvar; // loop iteration variable
7716 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007718 type_T *item_type = &t_any;
7719 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007720
Bram Moolenaar792f7862020-11-23 08:31:18 +01007721 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007722 if (p == NULL)
7723 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007724 if (var_count == 0)
7725 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007726
7727 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007728 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007729 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7730 return NULL;
7731 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007732 {
7733 emsg(_(e_missing_in));
7734 return NULL;
7735 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007736 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007737 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7738 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740 scope = new_scope(cctx, FOR_SCOPE);
7741 if (scope == NULL)
7742 return NULL;
7743
Bram Moolenaar792f7862020-11-23 08:31:18 +01007744 // Reserve a variable to store the loop iteration counter and initialize it
7745 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007746 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7747 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007748 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007749 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007750 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007751 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007752 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007753 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007754
7755 // compile "expr", it remains on the stack until "endfor"
7756 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007757 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007758 {
7759 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007760 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007761 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007762 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007763
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007764 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007765 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007766 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007767 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007768 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007769 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007770 semsg(_(e_for_loop_on_str_not_supported),
7771 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007772 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007773 return NULL;
7774 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007775
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007776 if (vartype->tt_type == VAR_STRING)
7777 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007778 else if (vartype->tt_type == VAR_BLOB)
7779 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007780 else if (vartype->tt_type == VAR_LIST
7781 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007782 {
7783 if (var_count == 1)
7784 item_type = vartype->tt_member;
7785 else if (vartype->tt_member->tt_type == VAR_LIST
7786 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007787 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007788 item_type = vartype->tt_member->tt_member;
7789 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790
Bram Moolenaara91a7132021-03-25 21:12:15 +01007791 // CMDMOD_REV must come before the FOR instruction
7792 generate_undo_cmdmods(cctx);
7793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007794 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007795 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007796 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007797
Bram Moolenaar792f7862020-11-23 08:31:18 +01007798 arg = arg_start;
7799 if (var_count > 1)
7800 {
7801 generate_UNPACK(cctx, var_count, semicolon);
7802 arg = skipwhite(arg + 1); // skip white after '['
7803
7804 // the list item is replaced by a number of items
7805 if (ga_grow(stack, var_count - 1) == FAIL)
7806 {
7807 drop_scope(cctx);
7808 return NULL;
7809 }
7810 --stack->ga_len;
7811 for (idx = 0; idx < var_count; ++idx)
7812 {
7813 ((type_T **)stack->ga_data)[stack->ga_len] =
7814 (semicolon && idx == 0) ? vartype : item_type;
7815 ++stack->ga_len;
7816 }
7817 }
7818
7819 for (idx = 0; idx < var_count; ++idx)
7820 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007821 assign_dest_T dest = dest_local;
7822 int opt_flags = 0;
7823 int vimvaridx = -1;
7824 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007825 type_T *lhs_type = &t_any;
7826 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007827
7828 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007829 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007830 name = vim_strnsave(arg, varlen);
7831 if (name == NULL)
7832 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007833 if (*p == ':')
7834 {
7835 p = skipwhite(p + 1);
7836 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7837 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007838
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007839 // TODO: script var not supported?
7840 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7841 &vimvaridx, &type, cctx) == FAIL)
7842 goto failed;
7843 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007844 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007845 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7846 0, 0, type, name) == FAIL)
7847 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007848 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007849 else if (varlen == 1 && *arg == '_')
7850 {
7851 // Assigning to "_": drop the value.
7852 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7853 goto failed;
7854 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007855 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007856 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007857 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007858 {
7859 semsg(_(e_variable_already_declared), arg);
7860 goto failed;
7861 }
7862
Bram Moolenaarea870692020-12-02 14:24:30 +01007863 if (STRNCMP(name, "s:", 2) == 0)
7864 {
7865 semsg(_(e_cannot_declare_script_variable_in_function), name);
7866 goto failed;
7867 }
7868
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007869 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007870 where.wt_index = var_count > 1 ? idx + 1 : 0;
7871 where.wt_variable = TRUE;
7872 if (lhs_type == &t_any)
7873 lhs_type = item_type;
7874 else if (item_type != &t_unknown
7875 && !(var_count > 1 && item_type == &t_any)
7876 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7877 goto failed;
7878 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007879 if (var_lvar == NULL)
7880 // out of memory or used as an argument
7881 goto failed;
7882
7883 if (semicolon && idx == var_count - 1)
7884 var_lvar->lv_type = vartype;
7885 else
7886 var_lvar->lv_type = item_type;
7887 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7888 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007889
7890 if (*p == ',' || *p == ';')
7891 ++p;
7892 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007893 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007894 }
7895
7896 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007897
7898failed:
7899 vim_free(name);
7900 drop_scope(cctx);
7901 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007902}
7903
7904/*
7905 * compile "endfor"
7906 */
7907 static char_u *
7908compile_endfor(char_u *arg, cctx_T *cctx)
7909{
7910 garray_T *instr = &cctx->ctx_instr;
7911 scope_T *scope = cctx->ctx_scope;
7912 forscope_T *forscope;
7913 isn_T *isn;
7914
Bram Moolenaarfa984412021-03-25 22:15:28 +01007915 if (misplaced_cmdmod(cctx))
7916 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007918 if (scope == NULL || scope->se_type != FOR_SCOPE)
7919 {
7920 emsg(_(e_for));
7921 return NULL;
7922 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007923 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007924 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007925 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926
7927 // At end of ":for" scope jump back to the FOR instruction.
7928 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7929
7930 // Fill in the "end" label in the FOR statement so it can jump here
7931 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7932 isn->isn_arg.forloop.for_end = instr->ga_len;
7933
7934 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007935 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007936
7937 // Below the ":for" scope drop the "expr" list from the stack.
7938 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7939 return NULL;
7940
7941 vim_free(scope);
7942
7943 return arg;
7944}
7945
7946/*
7947 * compile "while expr"
7948 *
7949 * Produces instructions:
7950 * top: EVAL expr Push result of "expr"
7951 * JUMP_IF_FALSE end jump if false
7952 * ... body ...
7953 * JUMP top Jump back to repeat
7954 * end:
7955 *
7956 */
7957 static char_u *
7958compile_while(char_u *arg, cctx_T *cctx)
7959{
7960 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007961 scope_T *scope;
7962
7963 scope = new_scope(cctx, WHILE_SCOPE);
7964 if (scope == NULL)
7965 return NULL;
7966
Bram Moolenaara91a7132021-03-25 21:12:15 +01007967 // "endwhile" jumps back here, one before when profiling or using cmdmods
7968 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007969
7970 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007971 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007972 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007973 if (!ends_excmd2(arg, skipwhite(p)))
7974 {
7975 semsg(_(e_trailing_arg), p);
7976 return NULL;
7977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007978
Bram Moolenaar13106602020-10-04 16:06:05 +02007979 if (bool_on_stack(cctx) == FAIL)
7980 return FAIL;
7981
Bram Moolenaara91a7132021-03-25 21:12:15 +01007982 // CMDMOD_REV must come before the jump
7983 generate_undo_cmdmods(cctx);
7984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007985 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007986 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007987 JUMP_IF_FALSE, cctx) == FAIL)
7988 return FAIL;
7989
7990 return p;
7991}
7992
7993/*
7994 * compile "endwhile"
7995 */
7996 static char_u *
7997compile_endwhile(char_u *arg, cctx_T *cctx)
7998{
7999 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008000 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008001
Bram Moolenaarfa984412021-03-25 22:15:28 +01008002 if (misplaced_cmdmod(cctx))
8003 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008004 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8005 {
8006 emsg(_(e_while));
8007 return NULL;
8008 }
8009 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008010 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011
Bram Moolenaarf002a412021-01-24 13:34:18 +01008012#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008013 // count the endwhile before jumping
8014 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008015#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008017 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008018 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008019
8020 // Fill in the "end" label in the WHILE statement so it can jump here.
8021 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008022 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8023 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008024
8025 vim_free(scope);
8026
8027 return arg;
8028}
8029
8030/*
8031 * compile "continue"
8032 */
8033 static char_u *
8034compile_continue(char_u *arg, cctx_T *cctx)
8035{
8036 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008037 int try_scopes = 0;
8038 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008039
8040 for (;;)
8041 {
8042 if (scope == NULL)
8043 {
8044 emsg(_(e_continue));
8045 return NULL;
8046 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008047 if (scope->se_type == FOR_SCOPE)
8048 {
8049 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008050 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008051 }
8052 if (scope->se_type == WHILE_SCOPE)
8053 {
8054 loop_label = scope->se_u.se_while.ws_top_label;
8055 break;
8056 }
8057 if (scope->se_type == TRY_SCOPE)
8058 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008059 scope = scope->se_outer;
8060 }
8061
Bram Moolenaarc150c092021-02-13 15:02:46 +01008062 if (try_scopes > 0)
8063 // Inside one or more try/catch blocks we first need to jump to the
8064 // "finally" or "endtry" to cleanup.
8065 generate_TRYCONT(cctx, try_scopes, loop_label);
8066 else
8067 // Jump back to the FOR or WHILE instruction.
8068 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008070 return arg;
8071}
8072
8073/*
8074 * compile "break"
8075 */
8076 static char_u *
8077compile_break(char_u *arg, cctx_T *cctx)
8078{
8079 scope_T *scope = cctx->ctx_scope;
8080 endlabel_T **el;
8081
8082 for (;;)
8083 {
8084 if (scope == NULL)
8085 {
8086 emsg(_(e_break));
8087 return NULL;
8088 }
8089 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8090 break;
8091 scope = scope->se_outer;
8092 }
8093
8094 // Jump to the end of the FOR or WHILE loop.
8095 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008096 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008098 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008099 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8100 return FAIL;
8101
8102 return arg;
8103}
8104
8105/*
8106 * compile "{" start of block
8107 */
8108 static char_u *
8109compile_block(char_u *arg, cctx_T *cctx)
8110{
8111 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8112 return NULL;
8113 return skipwhite(arg + 1);
8114}
8115
8116/*
8117 * compile end of block: drop one scope
8118 */
8119 static void
8120compile_endblock(cctx_T *cctx)
8121{
8122 scope_T *scope = cctx->ctx_scope;
8123
8124 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008125 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008126 vim_free(scope);
8127}
8128
8129/*
8130 * compile "try"
8131 * Creates a new scope for the try-endtry, pointing to the first catch and
8132 * finally.
8133 * Creates another scope for the "try" block itself.
8134 * TRY instruction sets up exception handling at runtime.
8135 *
8136 * "try"
8137 * TRY -> catch1, -> finally push trystack entry
8138 * ... try block
8139 * "throw {exception}"
8140 * EVAL {exception}
8141 * THROW create exception
8142 * ... try block
8143 * " catch {expr}"
8144 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008145 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008146 * EVAL {expr}
8147 * MATCH
8148 * JUMP nomatch -> catch2
8149 * CATCH remove exception
8150 * ... catch block
8151 * " catch"
8152 * JUMP -> finally
8153 * catch2: CATCH remove exception
8154 * ... catch block
8155 * " finally"
8156 * finally:
8157 * ... finally block
8158 * " endtry"
8159 * ENDTRY pop trystack entry, may rethrow
8160 */
8161 static char_u *
8162compile_try(char_u *arg, cctx_T *cctx)
8163{
8164 garray_T *instr = &cctx->ctx_instr;
8165 scope_T *try_scope;
8166 scope_T *scope;
8167
Bram Moolenaarfa984412021-03-25 22:15:28 +01008168 if (misplaced_cmdmod(cctx))
8169 return NULL;
8170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008171 // scope that holds the jumps that go to catch/finally/endtry
8172 try_scope = new_scope(cctx, TRY_SCOPE);
8173 if (try_scope == NULL)
8174 return NULL;
8175
Bram Moolenaar69f70502021-01-01 16:10:46 +01008176 if (cctx->ctx_skip != SKIP_YES)
8177 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008178 isn_T *isn;
8179
8180 // "try_catch" is set when the first ":catch" is found or when no catch
8181 // is found and ":finally" is found.
8182 // "try_finally" is set when ":finally" is found
8183 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008184 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008185 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8186 return NULL;
8187 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8188 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008189 return NULL;
8190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008191
8192 // scope for the try block itself
8193 scope = new_scope(cctx, BLOCK_SCOPE);
8194 if (scope == NULL)
8195 return NULL;
8196
8197 return arg;
8198}
8199
8200/*
8201 * compile "catch {expr}"
8202 */
8203 static char_u *
8204compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8205{
8206 scope_T *scope = cctx->ctx_scope;
8207 garray_T *instr = &cctx->ctx_instr;
8208 char_u *p;
8209 isn_T *isn;
8210
Bram Moolenaarfa984412021-03-25 22:15:28 +01008211 if (misplaced_cmdmod(cctx))
8212 return NULL;
8213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008214 // end block scope from :try or :catch
8215 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8216 compile_endblock(cctx);
8217 scope = cctx->ctx_scope;
8218
8219 // Error if not in a :try scope
8220 if (scope == NULL || scope->se_type != TRY_SCOPE)
8221 {
8222 emsg(_(e_catch));
8223 return NULL;
8224 }
8225
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008226 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008227 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008228 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008229 return NULL;
8230 }
8231
Bram Moolenaar69f70502021-01-01 16:10:46 +01008232 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008233 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008234#ifdef FEAT_PROFILE
8235 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008236 if (cctx->ctx_compile_type == CT_PROFILE
8237 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008238 .isn_type == ISN_PROF_START)
8239 --instr->ga_len;
8240#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008241 // Jump from end of previous block to :finally or :endtry
8242 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8243 JUMP_ALWAYS, cctx) == FAIL)
8244 return NULL;
8245
8246 // End :try or :catch scope: set value in ISN_TRY instruction
8247 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008248 if (isn->isn_arg.try.try_ref->try_catch == 0)
8249 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008250 if (scope->se_u.se_try.ts_catch_label != 0)
8251 {
8252 // Previous catch without match jumps here
8253 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8254 isn->isn_arg.jump.jump_where = instr->ga_len;
8255 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008256#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008257 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008258 {
8259 // a "throw" that jumps here needs to be counted
8260 generate_instr(cctx, ISN_PROF_END);
8261 // the "catch" is also counted
8262 generate_instr(cctx, ISN_PROF_START);
8263 }
8264#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008265 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008266 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008267 }
8268
8269 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008270 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008271 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008272 scope->se_u.se_try.ts_caught_all = TRUE;
8273 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008274 }
8275 else
8276 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008277 char_u *end;
8278 char_u *pat;
8279 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008280 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008281 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008282
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008283 // Push v:exception, push {expr} and MATCH
8284 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8285
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008286 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008287 if (*end != *p)
8288 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008289 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008290 vim_free(tofree);
8291 return FAIL;
8292 }
8293 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008294 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008295 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008296 len = (int)(end - tofree);
8297 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008298 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008299 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008300 if (pat == NULL)
8301 return FAIL;
8302 if (generate_PUSHS(cctx, pat) == FAIL)
8303 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008305 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8306 return NULL;
8307
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008308 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008309 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8310 return NULL;
8311 }
8312
Bram Moolenaar69f70502021-01-01 16:10:46 +01008313 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008314 return NULL;
8315
8316 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8317 return NULL;
8318 return p;
8319}
8320
8321 static char_u *
8322compile_finally(char_u *arg, cctx_T *cctx)
8323{
8324 scope_T *scope = cctx->ctx_scope;
8325 garray_T *instr = &cctx->ctx_instr;
8326 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008327 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008328
Bram Moolenaarfa984412021-03-25 22:15:28 +01008329 if (misplaced_cmdmod(cctx))
8330 return NULL;
8331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008332 // end block scope from :try or :catch
8333 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8334 compile_endblock(cctx);
8335 scope = cctx->ctx_scope;
8336
8337 // Error if not in a :try scope
8338 if (scope == NULL || scope->se_type != TRY_SCOPE)
8339 {
8340 emsg(_(e_finally));
8341 return NULL;
8342 }
8343
8344 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008345 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008346 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008347 {
8348 emsg(_(e_finally_dup));
8349 return NULL;
8350 }
8351
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008352 this_instr = instr->ga_len;
8353#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008354 if (cctx->ctx_compile_type == CT_PROFILE
8355 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008356 .isn_type == ISN_PROF_START)
8357 // jump to the profile start of the "finally"
8358 --this_instr;
8359#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008360
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008361 // Fill in the "end" label in jumps at the end of the blocks.
8362 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8363 this_instr, cctx);
8364
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008365 // If there is no :catch then an exception jumps to :finally.
8366 if (isn->isn_arg.try.try_ref->try_catch == 0)
8367 isn->isn_arg.try.try_ref->try_catch = this_instr;
8368 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008369 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008370 {
8371 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008372 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008373 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008374 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008375 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008376 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8377 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008379 // TODO: set index in ts_finally_label jumps
8380
8381 return arg;
8382}
8383
8384 static char_u *
8385compile_endtry(char_u *arg, cctx_T *cctx)
8386{
8387 scope_T *scope = cctx->ctx_scope;
8388 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008389 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008390
Bram Moolenaarfa984412021-03-25 22:15:28 +01008391 if (misplaced_cmdmod(cctx))
8392 return NULL;
8393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008394 // end block scope from :catch or :finally
8395 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8396 compile_endblock(cctx);
8397 scope = cctx->ctx_scope;
8398
8399 // Error if not in a :try scope
8400 if (scope == NULL || scope->se_type != TRY_SCOPE)
8401 {
8402 if (scope == NULL)
8403 emsg(_(e_no_endtry));
8404 else if (scope->se_type == WHILE_SCOPE)
8405 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008406 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008407 emsg(_(e_endfor));
8408 else
8409 emsg(_(e_endif));
8410 return NULL;
8411 }
8412
Bram Moolenaarc150c092021-02-13 15:02:46 +01008413 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008414 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008415 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008416 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8417 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008418 {
8419 emsg(_(e_missing_catch_or_finally));
8420 return NULL;
8421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008422
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008423#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008424 if (cctx->ctx_compile_type == CT_PROFILE
8425 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008426 .isn_type == ISN_PROF_START)
8427 // move the profile start after "endtry" so that it's not counted when
8428 // the exception is rethrown.
8429 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008430#endif
8431
Bram Moolenaar69f70502021-01-01 16:10:46 +01008432 // Fill in the "end" label in jumps at the end of the blocks, if not
8433 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008434 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8435 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008436
Bram Moolenaar69f70502021-01-01 16:10:46 +01008437 if (scope->se_u.se_try.ts_catch_label != 0)
8438 {
8439 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008440 isn_T *isn = ((isn_T *)instr->ga_data)
8441 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008442 isn->isn_arg.jump.jump_where = instr->ga_len;
8443 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008444 }
8445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008446 compile_endblock(cctx);
8447
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008448 if (cctx->ctx_skip != SKIP_YES)
8449 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008450 // End :catch or :finally scope: set instruction index in ISN_TRY
8451 // instruction
8452 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008453 if (cctx->ctx_skip != SKIP_YES
8454 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8455 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008456#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008457 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008458 generate_instr(cctx, ISN_PROF_START);
8459#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008460 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008461 return arg;
8462}
8463
8464/*
8465 * compile "throw {expr}"
8466 */
8467 static char_u *
8468compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8469{
8470 char_u *p = skipwhite(arg);
8471
Bram Moolenaara5565e42020-05-09 15:44:01 +02008472 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008473 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008474 if (cctx->ctx_skip == SKIP_YES)
8475 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008476 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008477 return NULL;
8478 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8479 return NULL;
8480
8481 return p;
8482}
8483
8484/*
8485 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008486 * compile "echomsg expr"
8487 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008488 * compile "execute expr"
8489 */
8490 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008491compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008492{
8493 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008494 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008495 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008496 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008497 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008498 garray_T *stack = &cctx->ctx_type_stack;
8499 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008500
8501 for (;;)
8502 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008503 if (ends_excmd2(prev, p))
8504 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008505 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008506 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008507 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008508
8509 if (cctx->ctx_skip != SKIP_YES)
8510 {
8511 // check for non-void type
8512 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8513 if (type->tt_type == VAR_VOID)
8514 {
8515 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8516 return NULL;
8517 }
8518 }
8519
Bram Moolenaarad39c092020-02-26 18:23:43 +01008520 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008521 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008522 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008523 }
8524
Bram Moolenaare4984292020-12-13 14:19:25 +01008525 if (count > 0)
8526 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008527 long save_lnum = cctx->ctx_lnum;
8528
8529 // Use the line number where the command started.
8530 cctx->ctx_lnum = start_ctx_lnum;
8531
Bram Moolenaare4984292020-12-13 14:19:25 +01008532 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8533 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8534 else if (cmdidx == CMD_execute)
8535 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8536 else if (cmdidx == CMD_echomsg)
8537 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8538 else
8539 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008540
8541 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008543 return p;
8544}
8545
8546/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008547 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008548 * instruction to compute it and return OK.
8549 * Otherwise return FAIL, the caller must deal with any range.
8550 */
8551 static int
8552compile_variable_range(exarg_T *eap, cctx_T *cctx)
8553{
8554 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8555 char_u *p = skipdigits(eap->cmd);
8556
8557 if (p == range_end)
8558 return FAIL;
8559 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8560}
8561
8562/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008563 * :put r
8564 * :put ={expr}
8565 */
8566 static char_u *
8567compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8568{
8569 char_u *line = arg;
8570 linenr_T lnum;
8571 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008572 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008573
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008574 eap->regname = *line;
8575
8576 if (eap->regname == '=')
8577 {
8578 char_u *p = line + 1;
8579
8580 if (compile_expr0(&p, cctx) == FAIL)
8581 return NULL;
8582 line = p;
8583 }
8584 else if (eap->regname != NUL)
8585 ++line;
8586
Bram Moolenaar08597872020-12-10 19:43:40 +01008587 if (compile_variable_range(eap, cctx) == OK)
8588 {
8589 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8590 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008591 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008592 {
8593 // Either no range or a number.
8594 // "errormsg" will not be set because the range is ADDR_LINES.
8595 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008596 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008597 return NULL;
8598 if (eap->addr_count == 0)
8599 lnum = -1;
8600 else
8601 lnum = eap->line2;
8602 if (above)
8603 --lnum;
8604 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008605
8606 generate_PUT(cctx, eap->regname, lnum);
8607 return line;
8608}
8609
8610/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008611 * A command that is not compiled, execute with legacy code.
8612 */
8613 static char_u *
8614compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8615{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008616 char_u *p;
8617 int has_expr = FALSE;
8618 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008619
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008620 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008621 goto theend;
8622
Bram Moolenaare729ce22021-06-06 21:38:09 +02008623 // If there was a prececing command modifier, drop it and include it in the
8624 // EXEC command.
8625 if (cctx->ctx_has_cmdmod)
8626 {
8627 garray_T *instr = &cctx->ctx_instr;
8628 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8629
8630 if (isn->isn_type == ISN_CMDMOD)
8631 {
8632 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8633 ->cmod_filter_regmatch.regprog);
8634 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8635 --instr->ga_len;
8636 cctx->ctx_has_cmdmod = FALSE;
8637 }
8638 }
8639
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008640 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008641 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008642 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008643 int usefilter = FALSE;
8644
8645 has_expr = argt & (EX_XFILE | EX_EXPAND);
8646
8647 // If the command can be followed by a bar, find the bar and truncate
8648 // it, so that the following command can be compiled.
8649 // The '|' is overwritten with a NUL, it is put back below.
8650 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8651 && *eap->arg == '!')
8652 // :w !filter or :r !filter or :r! filter
8653 usefilter = TRUE;
8654 if ((argt & EX_TRLBAR) && !usefilter)
8655 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008656 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008657 separate_nextcmd(eap);
8658 if (eap->nextcmd != NULL)
8659 nextcmd = eap->nextcmd;
8660 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008661 else if (eap->cmdidx == CMD_wincmd)
8662 {
8663 p = eap->arg;
8664 if (*p != NUL)
8665 ++p;
8666 if (*p == 'g' || *p == Ctrl_G)
8667 ++p;
8668 p = skipwhite(p);
8669 if (*p == '|')
8670 {
8671 *p = NUL;
8672 nextcmd = p + 1;
8673 }
8674 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008675 }
8676
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008677 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8678 {
8679 // expand filename in "syntax include [@group] filename"
8680 has_expr = TRUE;
8681 eap->arg = skipwhite(eap->arg + 7);
8682 if (*eap->arg == '@')
8683 eap->arg = skiptowhite(eap->arg);
8684 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008685
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008686 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8687 && STRLEN(eap->arg) > 4)
8688 {
8689 int delim = *eap->arg;
8690
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008691 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008692 if (*p == delim)
8693 {
8694 eap->arg = p + 1;
8695 has_expr = TRUE;
8696 }
8697 }
8698
Bram Moolenaarecac5912021-01-05 19:23:28 +01008699 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8700 {
8701 // TODO: should only expand when appropriate for the command
8702 eap->arg = skiptowhite(eap->arg);
8703 has_expr = TRUE;
8704 }
8705
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008706 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008707 {
8708 int count = 0;
8709 char_u *start = skipwhite(line);
8710
8711 // :cmd xxx`=expr1`yyy`=expr2`zzz
8712 // PUSHS ":cmd xxx"
8713 // eval expr1
8714 // PUSHS "yyy"
8715 // eval expr2
8716 // PUSHS "zzz"
8717 // EXECCONCAT 5
8718 for (;;)
8719 {
8720 if (p > start)
8721 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008722 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008723 ++count;
8724 }
8725 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008726 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008727 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008728 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008729 ++count;
8730 p = skipwhite(p);
8731 if (*p != '`')
8732 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008733 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008734 return NULL;
8735 }
8736 start = p + 1;
8737
8738 p = (char_u *)strstr((char *)start, "`=");
8739 if (p == NULL)
8740 {
8741 if (*skipwhite(start) != NUL)
8742 {
8743 generate_PUSHS(cctx, vim_strsave(start));
8744 ++count;
8745 }
8746 break;
8747 }
8748 }
8749 generate_EXECCONCAT(cctx, count);
8750 }
8751 else
8752 generate_EXEC(cctx, line);
8753
8754theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008755 if (*nextcmd != NUL)
8756 {
8757 // the parser expects a pointer to the bar, put it back
8758 --nextcmd;
8759 *nextcmd = '|';
8760 }
8761
8762 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008763}
8764
Bram Moolenaar20677332021-06-06 17:02:53 +02008765/*
8766 * A script command with heredoc, e.g.
8767 * ruby << EOF
8768 * command
8769 * EOF
8770 * Has been turned into one long line with NL characters by
8771 * get_function_body():
8772 * ruby << EOF<NL> command<NL>EOF
8773 */
8774 static char_u *
8775compile_script(char_u *line, cctx_T *cctx)
8776{
8777 if (cctx->ctx_skip != SKIP_YES)
8778 {
8779 isn_T *isn;
8780
8781 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8782 return NULL;
8783 isn->isn_arg.string = vim_strsave(line);
8784 }
8785 return (char_u *)"";
8786}
8787
Bram Moolenaar8238f082021-04-20 21:10:48 +02008788
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008789/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008790 * :s/pat/repl/
8791 */
8792 static char_u *
8793compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8794{
8795 char_u *cmd = eap->arg;
8796 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8797
8798 if (expr != NULL)
8799 {
8800 int delimiter = *cmd++;
8801
8802 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008803 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008804 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8805 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008806 garray_T save_ga = cctx->ctx_instr;
8807 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008808 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008809 int trailing_error;
8810 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008811 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008812 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008813
8814 cmd += 3;
8815 end = skip_substitute(cmd, delimiter);
8816
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008817 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008818 // labels are correct.
8819 cctx->ctx_instr.ga_len = 0;
8820 cctx->ctx_instr.ga_maxlen = 0;
8821 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008822 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008823 if (end[-1] == NUL)
8824 end[-1] = delimiter;
8825 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008826 trailing_error = *cmd != delimiter && *cmd != NUL;
8827
Bram Moolenaar169502f2021-04-21 12:19:35 +02008828 if (expr_res == FAIL || trailing_error
8829 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008830 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008831 if (trailing_error)
8832 semsg(_(e_trailing_arg), cmd);
8833 clear_instr_ga(&cctx->ctx_instr);
8834 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008835 return NULL;
8836 }
8837
Bram Moolenaar8238f082021-04-20 21:10:48 +02008838 // Move the generated instructions into the ISN_SUBSTITUTE
8839 // instructions, then restore the list of instructions before
8840 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008841 instr_count = cctx->ctx_instr.ga_len;
8842 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008843 instr[instr_count].isn_type = ISN_FINISH;
8844
8845 cctx->ctx_instr = save_ga;
8846 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8847 {
8848 int idx;
8849
8850 for (idx = 0; idx < instr_count; ++idx)
8851 delete_instr(instr + idx);
8852 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008853 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008854 }
8855 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8856 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008857
8858 // skip over flags
8859 if (*end == '&')
8860 ++end;
8861 while (ASCII_ISALPHA(*end) || *end == '#')
8862 ++end;
8863 return end;
8864 }
8865 }
8866
8867 return compile_exec(arg, eap, cctx);
8868}
8869
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008870 static char_u *
8871compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8872{
8873 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008874 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008875
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008876 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008877 {
8878 if (STRNCMP(arg, "END", 3) == 0)
8879 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008880 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008881 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008882 // First load the current variable value.
8883 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8884 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008885 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008886 }
8887
8888 // Gets the redirected text and put it on the stack, then store it
8889 // in the variable.
8890 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8891
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008892 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008893 generate_instr_drop(cctx, ISN_CONCAT, 1);
8894
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008895 if (lhs->lhs_has_index)
8896 {
8897 // Use the info in "lhs" to store the value at the index in the
8898 // list or dict.
8899 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8900 &t_string, cctx) == FAIL)
8901 return NULL;
8902 }
8903 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008904 return NULL;
8905
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008906 VIM_CLEAR(lhs->lhs_name);
8907 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008908 return arg + 3;
8909 }
8910 emsg(_(e_cannot_nest_redir));
8911 return NULL;
8912 }
8913
8914 if (arg[0] == '=' && arg[1] == '>')
8915 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008916 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008917
8918 // redirect to a variable is compiled
8919 arg += 2;
8920 if (*arg == '>')
8921 {
8922 ++arg;
8923 append = TRUE;
8924 }
8925 arg = skipwhite(arg);
8926
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008927 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008928 FALSE, FALSE, 1, cctx) == FAIL)
8929 return NULL;
8930 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008931 lhs->lhs_append = append;
8932 if (lhs->lhs_has_index)
8933 {
8934 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8935 if (lhs->lhs_whole == NULL)
8936 return NULL;
8937 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008938
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008939 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008940 }
8941
8942 // other redirects are handled like at script level
8943 return compile_exec(line, eap, cctx);
8944}
8945
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008946#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008947 static char_u *
8948compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8949{
8950 isn_T *isn;
8951 char_u *p;
8952
8953 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8954 if (isn == NULL)
8955 return NULL;
8956 isn->isn_arg.number = eap->cmdidx;
8957
8958 p = eap->arg;
8959 if (compile_expr0(&p, cctx) == FAIL)
8960 return NULL;
8961
8962 isn = generate_instr(cctx, ISN_CEXPR_CORE);
8963 if (isn == NULL)
8964 return NULL;
8965 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
8966 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
8967 return NULL;
8968 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
8969 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
8970 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
8971
8972 return p;
8973}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008974#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008975
Bram Moolenaar4c137212021-04-19 16:48:48 +02008976/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008977 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008978 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008979 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008980 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008981add_def_function(ufunc_T *ufunc)
8982{
8983 dfunc_T *dfunc;
8984
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008985 if (def_functions.ga_len == 0)
8986 {
8987 // The first position is not used, so that a zero uf_dfunc_idx means it
8988 // wasn't set.
8989 if (ga_grow(&def_functions, 1) == FAIL)
8990 return FAIL;
8991 ++def_functions.ga_len;
8992 }
8993
Bram Moolenaar09689a02020-05-09 22:50:08 +02008994 // Add the function to "def_functions".
8995 if (ga_grow(&def_functions, 1) == FAIL)
8996 return FAIL;
8997 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8998 CLEAR_POINTER(dfunc);
8999 dfunc->df_idx = def_functions.ga_len;
9000 ufunc->uf_dfunc_idx = dfunc->df_idx;
9001 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009002 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009003 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009004 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009005 ++def_functions.ga_len;
9006 return OK;
9007}
9008
9009/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009010 * After ex_function() has collected all the function lines: parse and compile
9011 * the lines into instructions.
9012 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009013 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9014 * the return statement (used for lambda). When uf_ret_type is already set
9015 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009016 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009017 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009018 * This can be used recursively through compile_lambda(), which may reallocate
9019 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009020 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009021 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009022 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009023compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009024 ufunc_T *ufunc,
9025 int check_return_type,
9026 compiletype_T compile_type,
9027 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009028{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009029 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009030 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009031 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009032 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009033 cctx_T cctx;
9034 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009035 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009036 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009037 int ret = FAIL;
9038 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009039 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009040 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009041 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009042 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009043#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009044 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009045#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009046 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009047
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009048 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009049 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009050 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009051 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009052 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9053 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009054 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009055
9056 switch (compile_type)
9057 {
9058 case CT_PROFILE:
9059#ifdef FEAT_PROFILE
9060 instr_dest = dfunc->df_instr_prof; break;
9061#endif
9062 case CT_NONE: instr_dest = dfunc->df_instr; break;
9063 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9064 }
9065 if (instr_dest != NULL)
9066 // Was compiled in this mode before: Free old instructions.
9067 delete_def_function_contents(dfunc, FALSE);
9068 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009069 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009070 else
9071 {
9072 if (add_def_function(ufunc) == FAIL)
9073 return FAIL;
9074 new_def_function = TRUE;
9075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009076
Bram Moolenaar985116a2020-07-12 17:31:09 +02009077 ufunc->uf_def_status = UF_COMPILING;
9078
Bram Moolenaara80faa82020-04-12 19:37:17 +02009079 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009080
Bram Moolenaare99d4222021-06-13 14:01:26 +02009081 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009082 cctx.ctx_ufunc = ufunc;
9083 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009084 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009085 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9086 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9087 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9088 cctx.ctx_type_list = &ufunc->uf_type_list;
9089 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9090 instr = &cctx.ctx_instr;
9091
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009092 // Set the context to the function, it may be compiled when called from
9093 // another script. Set the script version to the most modern one.
9094 // The line number will be set in next_line_from_context().
9095 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009096 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9097
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009098 // Don't use the flag from ":legacy" here.
9099 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9100
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009101 // Make sure error messages are OK.
9102 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9103 if (do_estack_push)
9104 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009105 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009106
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009107 if (ufunc->uf_def_args.ga_len > 0)
9108 {
9109 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009110 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009111 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009112 int i;
9113 char_u *arg;
9114 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009115 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009116
9117 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009118 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009119 for (i = 0; i < count; ++i)
9120 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009121 garray_T *stack = &cctx.ctx_type_stack;
9122 type_T *val_type;
9123 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009124 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009125 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009126 int jump_instr_idx = instr->ga_len;
9127 isn_T *isn;
9128
9129 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9130 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9131 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009132
9133 // Make sure later arguments are not found.
9134 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009135
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009136 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009137 r = compile_expr0(&arg, &cctx);
9138
9139 ufunc->uf_args.ga_len = uf_args_len;
9140 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009141 goto erret;
9142
9143 // If no type specified use the type of the default value.
9144 // Otherwise check that the default value type matches the
9145 // specified type.
9146 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009147 where.wt_index = arg_idx + 1;
9148 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009149 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009150 {
9151 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009152 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009153 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009154 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009155 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009156 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009157
9158 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009159 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009160
9161 // set instruction index in JUMP_IF_ARG_SET to here
9162 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9163 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009164 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009165
9166 if (did_set_arg_type)
9167 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009168 }
9169
9170 /*
9171 * Loop over all the lines of the function and generate instructions.
9172 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009173 for (;;)
9174 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009175 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009176 int starts_with_colon = FALSE;
9177 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009178 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009179
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009180 // Bail out on the first error to avoid a flood of errors and report
9181 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009182 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009183 goto erret;
9184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009185 if (line != NULL && *line == '|')
9186 // the line continues after a '|'
9187 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009188 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009189 && !(*line == '#' && (line == cctx.ctx_line_start
9190 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009191 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009192 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009193 goto erret;
9194 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009195 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9196 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009197 else
9198 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009199 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009200 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009201 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009202 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009203#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009204 if (cctx.ctx_skip != SKIP_YES)
9205 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009206#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009207 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009208 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009209 // Make a copy, splitting off nextcmd and removing trailing spaces
9210 // may change it.
9211 if (line != NULL)
9212 {
9213 line = vim_strsave(line);
9214 vim_free(line_to_free);
9215 line_to_free = line;
9216 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009217 }
9218
Bram Moolenaara80faa82020-04-12 19:37:17 +02009219 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009220 ea.cmdlinep = &line;
9221 ea.cmd = skipwhite(line);
9222
Bram Moolenaarb2049902021-01-24 12:53:53 +01009223 if (*ea.cmd == '#')
9224 {
9225 // "#" starts a comment
9226 line = (char_u *)"";
9227 continue;
9228 }
9229
Bram Moolenaarf002a412021-01-24 13:34:18 +01009230#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009231 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9232 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009233 {
9234 may_generate_prof_end(&cctx, prof_lnum);
9235
9236 prof_lnum = cctx.ctx_lnum;
9237 generate_instr(&cctx, ISN_PROF_START);
9238 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009239#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009240 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9241 && cctx.ctx_skip != SKIP_YES)
9242 {
9243 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009244 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009245 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01009246
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009247 // Some things can be recognized by the first character.
9248 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009249 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009250 case '}':
9251 {
9252 // "}" ends a block scope
9253 scopetype_T stype = cctx.ctx_scope == NULL
9254 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009255
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009256 if (stype == BLOCK_SCOPE)
9257 {
9258 compile_endblock(&cctx);
9259 line = ea.cmd;
9260 }
9261 else
9262 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009263 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009264 goto erret;
9265 }
9266 if (line != NULL)
9267 line = skipwhite(ea.cmd + 1);
9268 continue;
9269 }
9270
9271 case '{':
9272 // "{" starts a block scope
9273 // "{'a': 1}->func() is something else
9274 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9275 {
9276 line = compile_block(ea.cmd, &cctx);
9277 continue;
9278 }
9279 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009280 }
9281
9282 /*
9283 * COMMAND MODIFIERS
9284 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009285 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009286 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9287 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009288 {
9289 if (errormsg != NULL)
9290 goto erret;
9291 // empty line or comment
9292 line = (char_u *)"";
9293 continue;
9294 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009295 generate_cmdmods(&cctx, &local_cmdmod);
9296 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009297
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009298 // Check if there was a colon after the last command modifier or before
9299 // the current position.
9300 for (p = ea.cmd; p >= line; --p)
9301 {
9302 if (*p == ':')
9303 starts_with_colon = TRUE;
9304 if (p < ea.cmd && !VIM_ISWHITE(*p))
9305 break;
9306 }
9307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009308 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009309 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009310 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009311 {
9312 if (*ea.cmd == '(')
9313 // not for "call()"
9314 ea.cmd = p;
9315 else
9316 ea.cmd = skipwhite(ea.cmd);
9317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009318
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009319 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009320 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009321 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009322
Bram Moolenaar17126b12021-01-07 22:03:02 +01009323 // Check for assignment after command modifiers.
9324 assign = may_compile_assignment(&ea, &line, &cctx);
9325 if (assign == OK)
9326 goto nextline;
9327 if (assign == FAIL)
9328 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009329 }
9330
9331 /*
9332 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009333 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009334 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009335 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009336 cmd = ea.cmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009337 if (starts_with_colon || !(*cmd == '\''
9338 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009339 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009340 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009341 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009342 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009343 if (!starts_with_colon)
9344 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009345 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009346 goto erret;
9347 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009348 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009349 if (ends_excmd2(line, ea.cmd))
9350 {
9351 // A range without a command: jump to the line.
9352 // TODO: compile to a more efficient command, possibly
9353 // calling parse_cmd_address().
9354 ea.cmdidx = CMD_SIZE;
9355 line = compile_exec(line, &ea, &cctx);
9356 goto nextline;
9357 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009358 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009359 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009360 p = find_ex_command(&ea, NULL, starts_with_colon
9361 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009362
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009363 if (p == NULL)
9364 {
9365 if (cctx.ctx_skip != SKIP_YES)
9366 emsg(_(e_ambiguous_use_of_user_defined_command));
9367 goto erret;
9368 }
9369
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009370 // When using ":legacy cmd" always use compile_exec().
9371 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009372 {
9373 char_u *start = ea.cmd;
9374
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009375 switch (ea.cmdidx)
9376 {
9377 case CMD_if:
9378 case CMD_elseif:
9379 case CMD_else:
9380 case CMD_endif:
9381 case CMD_for:
9382 case CMD_endfor:
9383 case CMD_continue:
9384 case CMD_break:
9385 case CMD_while:
9386 case CMD_endwhile:
9387 case CMD_try:
9388 case CMD_catch:
9389 case CMD_finally:
9390 case CMD_endtry:
9391 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9392 goto erret;
9393 default: break;
9394 }
9395
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009396 // ":legacy return expr" needs to be handled differently.
9397 if (checkforcmd(&start, "return", 4))
9398 ea.cmdidx = CMD_return;
9399 else
9400 ea.cmdidx = CMD_legacy;
9401 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009403 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9404 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009405 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009406 {
9407 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009408 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009409 }
9410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009411 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009412 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009413 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009414 // CMD_var cannot happen, compile_assignment() above would be
9415 // used. Most likely an assignment to a non-existing variable.
9416 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009417 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009419 }
9420
Bram Moolenaar3988f642020-08-27 22:43:03 +02009421 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009422 && ea.cmdidx != CMD_elseif
9423 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009424 && ea.cmdidx != CMD_endif
9425 && ea.cmdidx != CMD_endfor
9426 && ea.cmdidx != CMD_endwhile
9427 && ea.cmdidx != CMD_catch
9428 && ea.cmdidx != CMD_finally
9429 && ea.cmdidx != CMD_endtry)
9430 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009431 emsg(_(e_unreachable_code_after_return));
9432 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009433 }
9434
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009435 p = skipwhite(p);
9436 if (ea.cmdidx != CMD_SIZE
9437 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9438 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009439 if (ea.cmdidx >= 0)
9440 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009441 if ((ea.argt & EX_BANG) && *p == '!')
9442 {
9443 ea.forceit = TRUE;
9444 p = skipwhite(p + 1);
9445 }
9446 }
9447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009448 switch (ea.cmdidx)
9449 {
9450 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009451 ea.arg = p;
9452 line = compile_nested_function(&ea, &cctx);
9453 break;
9454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009455 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009456 // TODO: should we allow this, e.g. to declare a global
9457 // function?
9458 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009459 goto erret;
9460
9461 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009462 line = compile_return(p, check_return_type,
9463 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009464 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009465 break;
9466
9467 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009468 emsg(_(e_cannot_use_let_in_vim9_script));
9469 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009470 case CMD_var:
9471 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009472 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009473 case CMD_increment:
9474 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009475 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009476 if (line == p)
9477 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009478 break;
9479
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009480 case CMD_unlet:
9481 case CMD_unlockvar:
9482 case CMD_lockvar:
9483 line = compile_unletlock(p, &ea, &cctx);
9484 break;
9485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009486 case CMD_import:
9487 line = compile_import(p, &cctx);
9488 break;
9489
9490 case CMD_if:
9491 line = compile_if(p, &cctx);
9492 break;
9493 case CMD_elseif:
9494 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009495 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009496 break;
9497 case CMD_else:
9498 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009499 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009500 break;
9501 case CMD_endif:
9502 line = compile_endif(p, &cctx);
9503 break;
9504
9505 case CMD_while:
9506 line = compile_while(p, &cctx);
9507 break;
9508 case CMD_endwhile:
9509 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009510 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009511 break;
9512
9513 case CMD_for:
9514 line = compile_for(p, &cctx);
9515 break;
9516 case CMD_endfor:
9517 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009518 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009519 break;
9520 case CMD_continue:
9521 line = compile_continue(p, &cctx);
9522 break;
9523 case CMD_break:
9524 line = compile_break(p, &cctx);
9525 break;
9526
9527 case CMD_try:
9528 line = compile_try(p, &cctx);
9529 break;
9530 case CMD_catch:
9531 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009532 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009533 break;
9534 case CMD_finally:
9535 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009536 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009537 break;
9538 case CMD_endtry:
9539 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009540 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009541 break;
9542 case CMD_throw:
9543 line = compile_throw(p, &cctx);
9544 break;
9545
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009546 case CMD_eval:
9547 if (compile_expr0(&p, &cctx) == FAIL)
9548 goto erret;
9549
Bram Moolenaar3988f642020-08-27 22:43:03 +02009550 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009551 generate_instr_drop(&cctx, ISN_DROP, 1);
9552
9553 line = skipwhite(p);
9554 break;
9555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009556 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009557 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009558 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009559 case CMD_echomsg:
9560 case CMD_echoerr:
9561 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009562 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009563
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009564 case CMD_put:
9565 ea.cmd = cmd;
9566 line = compile_put(p, &ea, &cctx);
9567 break;
9568
Bram Moolenaar4c137212021-04-19 16:48:48 +02009569 case CMD_substitute:
9570 if (cctx.ctx_skip == SKIP_YES)
9571 line = (char_u *)"";
9572 else
9573 {
9574 ea.arg = p;
9575 line = compile_substitute(line, &ea, &cctx);
9576 }
9577 break;
9578
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009579 case CMD_redir:
9580 ea.arg = p;
9581 line = compile_redir(line, &ea, &cctx);
9582 break;
9583
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009584 case CMD_cexpr:
9585 case CMD_lexpr:
9586 case CMD_caddexpr:
9587 case CMD_laddexpr:
9588 case CMD_cgetexpr:
9589 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009590#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009591 ea.arg = p;
9592 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009593#else
9594 ex_ni(&ea);
9595 line = NULL;
9596#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009597 break;
9598
Bram Moolenaar3988f642020-08-27 22:43:03 +02009599 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009600
Bram Moolenaarae616492020-07-28 20:07:27 +02009601 case CMD_append:
9602 case CMD_change:
9603 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009604 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009605 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009606 case CMD_xit:
9607 not_in_vim9(&ea);
9608 goto erret;
9609
Bram Moolenaar002262f2020-07-08 17:47:57 +02009610 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009611 if (cctx.ctx_skip != SKIP_YES)
9612 {
9613 semsg(_(e_invalid_command_str), ea.cmd);
9614 goto erret;
9615 }
9616 // We don't check for a next command here.
9617 line = (char_u *)"";
9618 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009619
Bram Moolenaar20677332021-06-06 17:02:53 +02009620 case CMD_lua:
9621 case CMD_mzscheme:
9622 case CMD_perl:
9623 case CMD_py3:
9624 case CMD_python3:
9625 case CMD_python:
9626 case CMD_pythonx:
9627 case CMD_ruby:
9628 case CMD_tcl:
9629 ea.arg = p;
9630 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009631 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009632 else
9633 // heredoc lines have been concatenated with NL
9634 // characters in get_function_body()
9635 line = compile_script(line, &cctx);
9636 break;
9637
9638 default:
9639 // Not recognized, execute with do_cmdline_cmd().
9640 ea.arg = p;
9641 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009642 break;
9643 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009644nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009645 if (line == NULL)
9646 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009647 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009648
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009649 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009650 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009652 if (cctx.ctx_type_stack.ga_len < 0)
9653 {
9654 iemsg("Type stack underflow");
9655 goto erret;
9656 }
9657 }
9658
9659 if (cctx.ctx_scope != NULL)
9660 {
9661 if (cctx.ctx_scope->se_type == IF_SCOPE)
9662 emsg(_(e_endif));
9663 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9664 emsg(_(e_endwhile));
9665 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9666 emsg(_(e_endfor));
9667 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009668 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009669 goto erret;
9670 }
9671
Bram Moolenaarefd88552020-06-18 20:50:10 +02009672 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009673 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009674 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9675 ufunc->uf_ret_type = &t_void;
9676 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009677 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009678 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009679 goto erret;
9680 }
9681
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009682 // Return void if there is no return at the end.
9683 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009684 }
9685
Bram Moolenaar599410c2021-04-10 14:03:43 +02009686 // When compiled with ":silent!" and there was an error don't consider the
9687 // function compiled.
9688 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009689 {
9690 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9691 + ufunc->uf_dfunc_idx;
9692 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009693 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009694#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009695 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009696 {
9697 dfunc->df_instr_prof = instr->ga_data;
9698 dfunc->df_instr_prof_count = instr->ga_len;
9699 }
9700 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009701#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009702 if (cctx.ctx_compile_type == CT_DEBUG)
9703 {
9704 dfunc->df_instr_debug = instr->ga_data;
9705 dfunc->df_instr_debug_count = instr->ga_len;
9706 }
9707 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009708 {
9709 dfunc->df_instr = instr->ga_data;
9710 dfunc->df_instr_count = instr->ga_len;
9711 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009712 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009713 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009714 if (cctx.ctx_outer_used)
9715 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009716 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009717 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009718
9719 ret = OK;
9720
9721erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009722 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009723 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009724 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9725 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009726
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009727 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009728 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009729 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009730 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009731
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009732 // If using the last entry in the table and it was added above, we
9733 // might as well remove it.
9734 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009735 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009736 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009737 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009738 ufunc->uf_dfunc_idx = 0;
9739 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009740 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009741
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009742 while (cctx.ctx_scope != NULL)
9743 drop_scope(&cctx);
9744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009745 if (errormsg != NULL)
9746 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009747 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009748 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009749 }
9750
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009751 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9752 {
9753 if (ret == OK)
9754 {
9755 emsg(_(e_missing_redir_end));
9756 ret = FAIL;
9757 }
9758 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009759 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009760 }
9761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009762 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009763 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009764 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009765 if (do_estack_push)
9766 estack_pop();
9767
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009768 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009769 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009770 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009771 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009772 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009773}
9774
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009775 void
9776set_function_type(ufunc_T *ufunc)
9777{
9778 int varargs = ufunc->uf_va_name != NULL;
9779 int argcount = ufunc->uf_args.ga_len;
9780
9781 // Create a type for the function, with the return type and any
9782 // argument types.
9783 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9784 // The type is included in "tt_args".
9785 if (argcount > 0 || varargs)
9786 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009787 if (ufunc->uf_type_list.ga_itemsize == 0)
9788 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009789 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9790 argcount, &ufunc->uf_type_list);
9791 // Add argument types to the function type.
9792 if (func_type_add_arg_types(ufunc->uf_func_type,
9793 argcount + varargs,
9794 &ufunc->uf_type_list) == FAIL)
9795 return;
9796 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9797 ufunc->uf_func_type->tt_min_argcount =
9798 argcount - ufunc->uf_def_args.ga_len;
9799 if (ufunc->uf_arg_types == NULL)
9800 {
9801 int i;
9802
9803 // lambda does not have argument types.
9804 for (i = 0; i < argcount; ++i)
9805 ufunc->uf_func_type->tt_args[i] = &t_any;
9806 }
9807 else
9808 mch_memmove(ufunc->uf_func_type->tt_args,
9809 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9810 if (varargs)
9811 {
9812 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009813 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009814 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9815 }
9816 }
9817 else
9818 // No arguments, can use a predefined type.
9819 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9820 argcount, &ufunc->uf_type_list);
9821}
9822
9823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009824/*
9825 * Delete an instruction, free what it contains.
9826 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009827 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009828delete_instr(isn_T *isn)
9829{
9830 switch (isn->isn_type)
9831 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009832 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009833 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009834 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009835 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009836 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009837 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009838 case ISN_LOADENV:
9839 case ISN_LOADG:
9840 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009841 case ISN_LOADT:
9842 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009843 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009844 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009845 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009846 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009847 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009848 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009849 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009850 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009851 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009852 case ISN_STOREW:
9853 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009854 vim_free(isn->isn_arg.string);
9855 break;
9856
Bram Moolenaar4c137212021-04-19 16:48:48 +02009857 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009858 {
9859 int idx;
9860 isn_T *list = isn->isn_arg.subs.subs_instr;
9861
9862 vim_free(isn->isn_arg.subs.subs_cmd);
9863 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9864 delete_instr(list + idx);
9865 vim_free(list);
9866 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009867 break;
9868
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009869 case ISN_INSTR:
9870 {
9871 int idx;
9872 isn_T *list = isn->isn_arg.instr;
9873
9874 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9875 delete_instr(list + idx);
9876 vim_free(list);
9877 }
9878 break;
9879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009880 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009881 case ISN_STORES:
9882 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009883 break;
9884
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009885 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009886 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009887 vim_free(isn->isn_arg.unlet.ul_name);
9888 break;
9889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009890 case ISN_STOREOPT:
9891 vim_free(isn->isn_arg.storeopt.so_name);
9892 break;
9893
9894 case ISN_PUSHBLOB: // push blob isn_arg.blob
9895 blob_unref(isn->isn_arg.blob);
9896 break;
9897
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009898 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009899#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009900 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009901#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009902 break;
9903
9904 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009905#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009906 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009907#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009908 break;
9909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009910 case ISN_UCALL:
9911 vim_free(isn->isn_arg.ufunc.cuf_name);
9912 break;
9913
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009914 case ISN_FUNCREF:
9915 {
9916 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9917 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009918 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009919
Bram Moolenaar077a4232020-12-22 18:33:27 +01009920 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9921 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009922 }
9923 break;
9924
9925 case ISN_DCALL:
9926 {
9927 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9928 + isn->isn_arg.dfunc.cdf_idx;
9929
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009930 if (dfunc->df_ufunc != NULL
9931 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009932 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009933 }
9934 break;
9935
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009936 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009937 {
9938 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9939 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9940
9941 if (ufunc != NULL)
9942 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009943 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009944 func_ptr_unref(ufunc);
9945 }
9946
9947 vim_free(lambda);
9948 vim_free(isn->isn_arg.newfunc.nf_global);
9949 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009950 break;
9951
Bram Moolenaar5e654232020-09-16 15:22:00 +02009952 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009953 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009954 free_type(isn->isn_arg.type.ct_type);
9955 break;
9956
Bram Moolenaar02194d22020-10-24 23:08:38 +02009957 case ISN_CMDMOD:
9958 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9959 ->cmod_filter_regmatch.regprog);
9960 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9961 break;
9962
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009963 case ISN_LOADSCRIPT:
9964 case ISN_STORESCRIPT:
9965 vim_free(isn->isn_arg.script.scriptref);
9966 break;
9967
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009968 case ISN_TRY:
9969 vim_free(isn->isn_arg.try.try_ref);
9970 break;
9971
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009972 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +02009973 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009974 vim_free(isn->isn_arg.cexpr.cexpr_ref);
9975 break;
9976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009977 case ISN_2BOOL:
9978 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009979 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009980 case ISN_ADDBLOB:
9981 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009982 case ISN_ANYINDEX:
9983 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009984 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009985 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009986 case ISN_BLOBINDEX:
9987 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009988 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009989 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009990 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009991 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009992 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009993 case ISN_COMPAREANY:
9994 case ISN_COMPAREBLOB:
9995 case ISN_COMPAREBOOL:
9996 case ISN_COMPAREDICT:
9997 case ISN_COMPAREFLOAT:
9998 case ISN_COMPAREFUNC:
9999 case ISN_COMPARELIST:
10000 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010001 case ISN_COMPARESPECIAL:
10002 case ISN_COMPARESTRING:
10003 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010004 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010005 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010006 case ISN_DROP:
10007 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010008 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010009 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010010 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010011 case ISN_EXECCONCAT:
10012 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010013 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010014 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010015 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010016 case ISN_GETITEM:
10017 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010018 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010019 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010020 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010021 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010022 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010023 case ISN_LOADBDICT:
10024 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010025 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010026 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010027 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010028 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010029 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010030 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010031 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010032 case ISN_NEGATENR:
10033 case ISN_NEWDICT:
10034 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010035 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010036 case ISN_OPFLOAT:
10037 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010038 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010039 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010040 case ISN_PROF_END:
10041 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010042 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010043 case ISN_PUSHF:
10044 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010045 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010046 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010047 case ISN_REDIREND:
10048 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010049 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010050 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010051 case ISN_SHUFFLE:
10052 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010053 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010054 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010055 case ISN_STORENR:
10056 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010057 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010058 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010059 case ISN_STOREV:
10060 case ISN_STRINDEX:
10061 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010062 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010063 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010064 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010065 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010066 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010067 // nothing allocated
10068 break;
10069 }
10070}
10071
10072/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010073 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010074 */
10075 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010076delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010077{
10078 int idx;
10079
10080 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010081 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010082
10083 if (dfunc->df_instr != NULL)
10084 {
10085 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10086 delete_instr(dfunc->df_instr + idx);
10087 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010088 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010089 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010090 if (dfunc->df_instr_debug != NULL)
10091 {
10092 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10093 delete_instr(dfunc->df_instr_debug + idx);
10094 VIM_CLEAR(dfunc->df_instr_debug);
10095 dfunc->df_instr_debug = NULL;
10096 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010097#ifdef FEAT_PROFILE
10098 if (dfunc->df_instr_prof != NULL)
10099 {
10100 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10101 delete_instr(dfunc->df_instr_prof + idx);
10102 VIM_CLEAR(dfunc->df_instr_prof);
10103 dfunc->df_instr_prof = NULL;
10104 }
10105#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010106
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010107 if (mark_deleted)
10108 dfunc->df_deleted = TRUE;
10109 if (dfunc->df_ufunc != NULL)
10110 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010111}
10112
10113/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010114 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010115 * function, unless another user function still uses it.
10116 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010117 */
10118 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010119unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010120{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010121 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010122 {
10123 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10124 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010125
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010126 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010127 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010128 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010129 ufunc->uf_dfunc_idx = 0;
10130 if (dfunc->df_ufunc == ufunc)
10131 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010132 }
10133}
10134
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010135/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010136 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010137 */
10138 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010139link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010140{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010141 if (ufunc->uf_dfunc_idx > 0)
10142 {
10143 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10144 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010145
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010146 ++dfunc->df_refcount;
10147 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010148}
10149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010150#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010151/*
10152 * Free all functions defined with ":def".
10153 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010154 void
10155free_def_functions(void)
10156{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010157 int idx;
10158
10159 for (idx = 0; idx < def_functions.ga_len; ++idx)
10160 {
10161 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10162
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010163 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010164 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010165 }
10166
10167 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010168}
10169#endif
10170
10171
10172#endif // FEAT_EVAL