blob: 0c73433d5fb9d7e4982fe6a74a60556edea64121 [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 Moolenaarb2049902021-01-24 12:53:53 +0100177 int ctx_profiling; // when TRUE generate ISN_PROF_START
178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200180 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200182 int ctx_has_closure; // set to one if a closures was created in
183 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100185 garray_T ctx_imports; // imported items
186
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200187 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200189 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200191 cctx_T *ctx_outer; // outer scope for lambda or nested
192 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200193 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200196 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200197
Bram Moolenaar02194d22020-10-24 23:08:38 +0200198 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200199
200 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
201 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202};
203
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100204static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205
206/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100207 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100208 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100209 * If "lvar" is NULL only check if the variable can be found.
210 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100212 static int
213lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214{
215 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100216 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100218 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100219 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200220
221 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
223 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100224 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
225 if (STRNCMP(name, lvp->lv_name, len) == 0
226 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200227 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100228 if (lvar != NULL)
229 {
230 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100231 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100232 }
233 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200236
237 // Find local in outer function scope.
238 if (cctx->ctx_outer != NULL)
239 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100240 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100242 if (lvar != NULL)
243 {
244 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100245 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100246 }
247 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200248 }
249 }
250
Bram Moolenaar709664c2020-12-12 14:33:41 +0100251 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252}
253
254/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200255 * Lookup an argument in the current function and an enclosing function.
256 * Returns the argument index in "idxp"
257 * Returns the argument type in "type"
258 * Sets "gen_load_outer" to TRUE if found in outer scope.
259 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260 */
261 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200262arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200263 char_u *name,
264 size_t len,
265 int *idxp,
266 type_T **type,
267 int *gen_load_outer,
268 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269{
270 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200271 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100273 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200274 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
276 {
277 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
278
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200279 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
280 {
281 if (idxp != NULL)
282 {
283 // Arguments are located above the frame pointer. One further
284 // if there is a vararg argument
285 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
286 + STACK_FRAME_SIZE)
287 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
288
289 if (cctx->ctx_ufunc->uf_arg_types != NULL)
290 *type = cctx->ctx_ufunc->uf_arg_types[idx];
291 else
292 *type = &t_any;
293 }
294 return OK;
295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200298 va_name = cctx->ctx_ufunc->uf_va_name;
299 if (va_name != NULL
300 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
301 {
302 if (idxp != NULL)
303 {
304 // varargs is always the last argument
305 *idxp = -STACK_FRAME_SIZE - 1;
306 *type = cctx->ctx_ufunc->uf_va_type;
307 }
308 return OK;
309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100310
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200311 if (cctx->ctx_outer != NULL)
312 {
313 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200314 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200315 == OK)
316 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100317 if (gen_load_outer != NULL)
318 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200319 return OK;
320 }
321 }
322
323 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100324}
325
326/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200327 * Lookup a script-local variable in the current script, possibly defined in a
328 * block that contains the function "cctx->ctx_ufunc".
329 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100330 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331 * Return NULL when not found.
332 */
333 static sallvar_T *
334find_script_var(char_u *name, size_t len, cctx_T *cctx)
335{
336 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
337 hashitem_T *hi;
338 int cc;
339 sallvar_T *sav;
340 ufunc_T *ufunc;
341
342 // Find the list of all script variables with the right name.
343 if (len > 0)
344 {
345 cc = name[len];
346 name[len] = NUL;
347 }
348 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
349 if (len > 0)
350 name[len] = cc;
351 if (HASHITEM_EMPTY(hi))
352 return NULL;
353
354 sav = HI2SAV(hi);
355 if (sav->sav_block_id == 0 || cctx == NULL)
356 // variable defined in the script scope or not in a function.
357 return sav;
358
359 // Go over the variables with this name and find one that was visible
360 // from the function.
361 ufunc = cctx->ctx_ufunc;
362 while (sav != NULL)
363 {
364 int idx;
365
366 // Go over the blocks that this function was defined in. If the
367 // variable block ID matches it was visible to the function.
368 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
369 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
370 return sav;
371 sav = sav->sav_next;
372 }
373
374 return NULL;
375}
376
377/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100378 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200379 */
380 static int
381script_is_vim9()
382{
383 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
384}
385
386/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200388 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389 * Returns OK or FAIL.
390 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200391 static int
392script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200394 if (current_sctx.sc_sid <= 0)
395 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200396 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200397 {
398 // Check script variables that were visible where the function was
399 // defined.
400 if (find_script_var(name, len, cctx) != NULL)
401 return OK;
402 }
403 else
404 {
405 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
406 dictitem_T *di;
407 int cc;
408
409 // Check script variables that are currently visible
410 cc = name[len];
411 name[len] = NUL;
412 di = find_var_in_ht(ht, 0, name, TRUE);
413 name[len] = cc;
414 if (di != NULL)
415 return OK;
416 }
417
418 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419}
420
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100421/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100422 * Return TRUE if "name" is a local variable, argument, script variable or
423 * imported.
424 */
425 static int
426variable_exists(char_u *name, size_t len, cctx_T *cctx)
427{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100428 return (cctx != NULL
429 && (lookup_local(name, len, NULL, cctx) == OK
430 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200431 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100432 || find_imported(name, len, cctx) != NULL;
433}
434
435/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100436 * Return TRUE if "name" is a local variable, argument, script variable,
437 * imported or function.
438 */
439 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100440item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100441{
442 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100443 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100444
445 if (variable_exists(name, len, cctx))
446 return TRUE;
447
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100448 // This is similar to what is in lookup_scriptitem():
449 // Find a function, so that a following "->" works.
450 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
451 // a function call.
452 p = skipwhite(name + len);
453
454 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
455 {
456 // Do not check for an internal function, since it might also be a
457 // valid command, such as ":split" versuse "split()".
458 // Skip "g:" before a function name.
459 is_global = (name[0] == 'g' && name[1] == ':');
460 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
461 }
462 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100463}
464
465/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100466 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200467 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200468 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100469 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100470 * Return FAIL and give an error if it defined.
471 */
472 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100473check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100474{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200475 int c = p[len];
476 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200477
Bram Moolenaarda479c72021-04-10 21:01:38 +0200478 // underscore argument is OK
479 if (len == 1 && *p == '_')
480 return OK;
481
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200482 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100483 {
484 if (is_arg)
485 semsg(_(e_argument_already_declared_in_script_str), p);
486 else
487 semsg(_(e_variable_already_declared_in_script_str), p);
488 return FAIL;
489 }
490
Bram Moolenaarad486a02020-08-01 23:22:18 +0200491 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100492 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100493 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200494 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200495 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200496 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100497 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200498 // A local or script-local function can shadow a global function.
499 if (ufunc == NULL || !func_is_global(ufunc)
500 || (p[0] == 'g' && p[1] == ':'))
501 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100502 if (is_arg)
503 semsg(_(e_argument_name_shadows_existing_variable_str), p);
504 else
505 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200506 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200507 return FAIL;
508 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100509 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200510 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100511 return OK;
512}
513
Bram Moolenaar65b95452020-07-19 14:03:09 +0200514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515/////////////////////////////////////////////////////////////////////
516// Following generate_ functions expect the caller to call ga_grow().
517
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200518#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
519#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521/*
522 * Generate an instruction without arguments.
523 * Returns a pointer to the new instruction, NULL if failed.
524 */
525 static isn_T *
526generate_instr(cctx_T *cctx, isntype_T isn_type)
527{
528 garray_T *instr = &cctx->ctx_instr;
529 isn_T *isn;
530
Bram Moolenaar080457c2020-03-03 21:53:32 +0100531 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 if (ga_grow(instr, 1) == FAIL)
533 return NULL;
534 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
535 isn->isn_type = isn_type;
536 isn->isn_lnum = cctx->ctx_lnum + 1;
537 ++instr->ga_len;
538
539 return isn;
540}
541
542/*
543 * Generate an instruction without arguments.
544 * "drop" will be removed from the stack.
545 * Returns a pointer to the new instruction, NULL if failed.
546 */
547 static isn_T *
548generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
549{
550 garray_T *stack = &cctx->ctx_type_stack;
551
Bram Moolenaar080457c2020-03-03 21:53:32 +0100552 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 stack->ga_len -= drop;
554 return generate_instr(cctx, isn_type);
555}
556
557/*
558 * Generate instruction "isn_type" and put "type" on the type stack.
559 */
560 static isn_T *
561generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
562{
563 isn_T *isn;
564 garray_T *stack = &cctx->ctx_type_stack;
565
566 if ((isn = generate_instr(cctx, isn_type)) == NULL)
567 return NULL;
568
569 if (ga_grow(stack, 1) == FAIL)
570 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200571 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 ++stack->ga_len;
573
574 return isn;
575}
576
577/*
578 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200579 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200580 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 */
582 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200583may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584{
585 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200586 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100588 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100590 RETURN_OK_IF_SKIP(cctx);
591 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200592 switch ((*type)->tt_type)
593 {
594 // nothing to be done
595 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200597 // conversion possible
598 case VAR_SPECIAL:
599 case VAR_BOOL:
600 case VAR_NUMBER:
601 case VAR_FLOAT:
602 break;
603
604 // conversion possible (with runtime check)
605 case VAR_ANY:
606 case VAR_UNKNOWN:
607 isntype = ISN_2STRING_ANY;
608 break;
609
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200610 // conversion possible when tolerant
611 case VAR_LIST:
612 if (tolerant)
613 {
614 isntype = ISN_2STRING_ANY;
615 break;
616 }
617 // FALLTHROUGH
618
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200619 // conversion not possible
620 case VAR_VOID:
621 case VAR_BLOB:
622 case VAR_FUNC:
623 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200624 case VAR_DICT:
625 case VAR_JOB:
626 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200627 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200628 to_string_error((*type)->tt_type);
629 return FAIL;
630 }
631
632 *type = &t_string;
633 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200635 isn->isn_arg.tostring.offset = offset;
636 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637
638 return OK;
639}
640
641 static int
642check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
643{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200644 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200646 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647 {
648 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200649 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200651 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 return FAIL;
653 }
654 return OK;
655}
656
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200657 static int
658generate_add_instr(
659 cctx_T *cctx,
660 vartype_T vartype,
661 type_T *type1,
662 type_T *type2)
663{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100664 garray_T *stack = &cctx->ctx_type_stack;
665 isn_T *isn = generate_instr_drop(cctx,
666 vartype == VAR_NUMBER ? ISN_OPNR
667 : vartype == VAR_LIST ? ISN_ADDLIST
668 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200669#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100670 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200671#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100672 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200673
674 if (vartype != VAR_LIST && vartype != VAR_BLOB
675 && type1->tt_type != VAR_ANY
676 && type2->tt_type != VAR_ANY
677 && check_number_or_float(
678 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
679 return FAIL;
680
681 if (isn != NULL)
682 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100683
684 // When concatenating two lists with different member types the member type
685 // becomes "any".
686 if (vartype == VAR_LIST
687 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
688 && type1->tt_member != type2->tt_member)
689 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
690
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200691 return isn == NULL ? FAIL : OK;
692}
693
694/*
695 * Get the type to use for an instruction for an operation on "type1" and
696 * "type2". If they are matching use a type-specific instruction. Otherwise
697 * fall back to runtime type checking.
698 */
699 static vartype_T
700operator_type(type_T *type1, type_T *type2)
701{
702 if (type1->tt_type == type2->tt_type
703 && (type1->tt_type == VAR_NUMBER
704 || type1->tt_type == VAR_LIST
705#ifdef FEAT_FLOAT
706 || type1->tt_type == VAR_FLOAT
707#endif
708 || type1->tt_type == VAR_BLOB))
709 return type1->tt_type;
710 return VAR_ANY;
711}
712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713/*
714 * Generate an instruction with two arguments. The instruction depends on the
715 * type of the arguments.
716 */
717 static int
718generate_two_op(cctx_T *cctx, char_u *op)
719{
720 garray_T *stack = &cctx->ctx_type_stack;
721 type_T *type1;
722 type_T *type2;
723 vartype_T vartype;
724 isn_T *isn;
725
Bram Moolenaar080457c2020-03-03 21:53:32 +0100726 RETURN_OK_IF_SKIP(cctx);
727
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200728 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
730 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200731 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732
733 switch (*op)
734 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200735 case '+':
736 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 break;
739
740 case '-':
741 case '*':
742 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
743 op) == FAIL)
744 return FAIL;
745 if (vartype == VAR_NUMBER)
746 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
747#ifdef FEAT_FLOAT
748 else if (vartype == VAR_FLOAT)
749 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
750#endif
751 else
752 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
753 if (isn != NULL)
754 isn->isn_arg.op.op_type = *op == '*'
755 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
756 break;
757
Bram Moolenaar4c683752020-04-05 21:38:23 +0200758 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200760 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 && type2->tt_type != VAR_NUMBER))
762 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200763 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 return FAIL;
765 }
766 isn = generate_instr_drop(cctx,
767 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
768 if (isn != NULL)
769 isn->isn_arg.op.op_type = EXPR_REM;
770 break;
771 }
772
773 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200774 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 {
776 type_T *type = &t_any;
777
778#ifdef FEAT_FLOAT
779 // float+number and number+float results in float
780 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
781 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
782 type = &t_float;
783#endif
784 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
785 }
786
787 return OK;
788}
789
790/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200791 * Get the instruction to use for comparing "type1" with "type2"
792 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200794 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100795get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796{
797 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798
Bram Moolenaar4c683752020-04-05 21:38:23 +0200799 if (type1 == VAR_UNKNOWN)
800 type1 = VAR_ANY;
801 if (type2 == VAR_UNKNOWN)
802 type2 = VAR_ANY;
803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 if (type1 == type2)
805 {
806 switch (type1)
807 {
808 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
809 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
810 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
811 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
812 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
813 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
814 case VAR_LIST: isntype = ISN_COMPARELIST; break;
815 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
816 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 default: isntype = ISN_COMPAREANY; break;
818 }
819 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200820 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100822 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 isntype = ISN_COMPAREANY;
824
Bram Moolenaar657137c2021-01-09 15:45:23 +0100825 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 && (isntype == ISN_COMPAREBOOL
827 || isntype == ISN_COMPARESPECIAL
828 || isntype == ISN_COMPARENR
829 || isntype == ISN_COMPAREFLOAT))
830 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200831 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100832 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200833 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834 }
835 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100836 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
838 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100839 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
840 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 && (type1 == VAR_BLOB || type2 == VAR_BLOB
842 || type1 == VAR_LIST || type2 == VAR_LIST))))
843 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200844 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200846 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200848 return isntype;
849}
850
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200851 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100852check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200853{
854 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
855 return FAIL;
856 return OK;
857}
858
Bram Moolenaara5565e42020-05-09 15:44:01 +0200859/*
860 * Generate an ISN_COMPARE* instruction with a boolean result.
861 */
862 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100863generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200864{
865 isntype_T isntype;
866 isn_T *isn;
867 garray_T *stack = &cctx->ctx_type_stack;
868 vartype_T type1;
869 vartype_T type2;
870
871 RETURN_OK_IF_SKIP(cctx);
872
873 // Get the known type of the two items on the stack. If they are matching
874 // use a type-specific instruction. Otherwise fall back to runtime type
875 // checking.
876 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
877 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100878 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200879 if (isntype == ISN_DROP)
880 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881
882 if ((isn = generate_instr(cctx, isntype)) == NULL)
883 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100884 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 isn->isn_arg.op.op_ic = ic;
886
887 // takes two arguments, puts one bool back
888 if (stack->ga_len >= 2)
889 {
890 --stack->ga_len;
891 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
892 }
893
894 return OK;
895}
896
897/*
898 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200899 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 */
901 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200902generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903{
904 isn_T *isn;
905 garray_T *stack = &cctx->ctx_type_stack;
906
Bram Moolenaar080457c2020-03-03 21:53:32 +0100907 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
909 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200910 isn->isn_arg.tobool.invert = invert;
911 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912
913 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200914 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915
916 return OK;
917}
918
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200919/*
920 * Generate an ISN_COND2BOOL instruction.
921 */
922 static int
923generate_COND2BOOL(cctx_T *cctx)
924{
925 isn_T *isn;
926 garray_T *stack = &cctx->ctx_type_stack;
927
928 RETURN_OK_IF_SKIP(cctx);
929 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
930 return FAIL;
931
932 // type becomes bool
933 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
934
935 return OK;
936}
937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200939generate_TYPECHECK(
940 cctx_T *cctx,
941 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100942 int offset,
943 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944{
945 isn_T *isn;
946 garray_T *stack = &cctx->ctx_type_stack;
947
Bram Moolenaar080457c2020-03-03 21:53:32 +0100948 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
950 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200951 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100952 isn->isn_arg.type.ct_off = (int8_T)offset;
953 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954
Bram Moolenaar5e654232020-09-16 15:22:00 +0200955 // type becomes expected
956 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957
958 return OK;
959}
960
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100961 static int
962generate_SETTYPE(
963 cctx_T *cctx,
964 type_T *expected)
965{
966 isn_T *isn;
967
968 RETURN_OK_IF_SKIP(cctx);
969 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
970 return FAIL;
971 isn->isn_arg.type.ct_type = alloc_type(expected);
972 return OK;
973}
974
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200976 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
977 * used. Return FALSE if the types will never match.
978 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100979 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200980use_typecheck(type_T *actual, type_T *expected)
981{
982 if (actual->tt_type == VAR_ANY
983 || actual->tt_type == VAR_UNKNOWN
984 || (actual->tt_type == VAR_FUNC
985 && (expected->tt_type == VAR_FUNC
986 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100987 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
988 && ((actual->tt_member == &t_void)
989 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200990 return TRUE;
991 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
992 && actual->tt_type == expected->tt_type)
993 // This takes care of a nested list or dict.
994 return use_typecheck(actual->tt_member, expected->tt_member);
995 return FALSE;
996}
997
998/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200999 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001000 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001001 * - "actual" is a type that can be "expected" type: add a runtime check; or
1002 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001003 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1004 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001005 */
Bram Moolenaar351ead02021-01-16 16:07:01 +01001006 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001007need_type(
1008 type_T *actual,
1009 type_T *expected,
1010 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001011 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001012 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001013 int silent,
1014 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001015{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001016 where_T where;
1017
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001018 if (expected == &t_bool && actual != &t_bool
1019 && (actual->tt_flags & TTFLAG_BOOL_OK))
1020 {
1021 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1022 // boolean is OK but requires a conversion.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001023 generate_2BOOL(cctx, FALSE, offset);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001024 return OK;
1025 }
1026
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001027 where.wt_index = arg_idx;
1028 where.wt_variable = FALSE;
1029 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001030 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001031
1032 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001033 // If it's a constant a runtime check makes no sense.
1034 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001035 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001036 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001037 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001038 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001039
1040 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001041 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001042 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001043}
1044
1045/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001046 * Check that the top of the type stack has a type that can be used as a
1047 * condition. Give an error and return FAIL if not.
1048 */
1049 static int
1050bool_on_stack(cctx_T *cctx)
1051{
1052 garray_T *stack = &cctx->ctx_type_stack;
1053 type_T *type;
1054
1055 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1056 if (type == &t_bool)
1057 return OK;
1058
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001059 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001060 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1061 // This requires a runtime type check.
1062 return generate_COND2BOOL(cctx);
1063
Bram Moolenaar351ead02021-01-16 16:07:01 +01001064 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001065}
1066
1067/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 * Generate an ISN_PUSHNR instruction.
1069 */
1070 static int
1071generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1072{
1073 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001074 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075
Bram Moolenaar080457c2020-03-03 21:53:32 +01001076 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1078 return FAIL;
1079 isn->isn_arg.number = number;
1080
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001081 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001082 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001083 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 return OK;
1085}
1086
1087/*
1088 * Generate an ISN_PUSHBOOL instruction.
1089 */
1090 static int
1091generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1092{
1093 isn_T *isn;
1094
Bram Moolenaar080457c2020-03-03 21:53:32 +01001095 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1097 return FAIL;
1098 isn->isn_arg.number = number;
1099
1100 return OK;
1101}
1102
1103/*
1104 * Generate an ISN_PUSHSPEC instruction.
1105 */
1106 static int
1107generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1108{
1109 isn_T *isn;
1110
Bram Moolenaar080457c2020-03-03 21:53:32 +01001111 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1113 return FAIL;
1114 isn->isn_arg.number = number;
1115
1116 return OK;
1117}
1118
1119#ifdef FEAT_FLOAT
1120/*
1121 * Generate an ISN_PUSHF instruction.
1122 */
1123 static int
1124generate_PUSHF(cctx_T *cctx, float_T fnumber)
1125{
1126 isn_T *isn;
1127
Bram Moolenaar080457c2020-03-03 21:53:32 +01001128 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1130 return FAIL;
1131 isn->isn_arg.fnumber = fnumber;
1132
1133 return OK;
1134}
1135#endif
1136
1137/*
1138 * Generate an ISN_PUSHS instruction.
1139 * Consumes "str".
1140 */
1141 static int
1142generate_PUSHS(cctx_T *cctx, char_u *str)
1143{
1144 isn_T *isn;
1145
Bram Moolenaar508b5612021-01-02 18:17:26 +01001146 if (cctx->ctx_skip == SKIP_YES)
1147 {
1148 vim_free(str);
1149 return OK;
1150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1152 return FAIL;
1153 isn->isn_arg.string = str;
1154
1155 return OK;
1156}
1157
1158/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001159 * Generate an ISN_PUSHCHANNEL instruction.
1160 * Consumes "channel".
1161 */
1162 static int
1163generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1164{
1165 isn_T *isn;
1166
Bram Moolenaar080457c2020-03-03 21:53:32 +01001167 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001168 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1169 return FAIL;
1170 isn->isn_arg.channel = channel;
1171
1172 return OK;
1173}
1174
1175/*
1176 * Generate an ISN_PUSHJOB instruction.
1177 * Consumes "job".
1178 */
1179 static int
1180generate_PUSHJOB(cctx_T *cctx, job_T *job)
1181{
1182 isn_T *isn;
1183
Bram Moolenaar080457c2020-03-03 21:53:32 +01001184 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001185 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001186 return FAIL;
1187 isn->isn_arg.job = job;
1188
1189 return OK;
1190}
1191
1192/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 * Generate an ISN_PUSHBLOB instruction.
1194 * Consumes "blob".
1195 */
1196 static int
1197generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1198{
1199 isn_T *isn;
1200
Bram Moolenaar080457c2020-03-03 21:53:32 +01001201 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1203 return FAIL;
1204 isn->isn_arg.blob = blob;
1205
1206 return OK;
1207}
1208
1209/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001210 * Generate an ISN_PUSHFUNC instruction with name "name".
1211 * Consumes "name".
1212 */
1213 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001214generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001215{
1216 isn_T *isn;
1217
Bram Moolenaar080457c2020-03-03 21:53:32 +01001218 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001219 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001220 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001221 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001222
1223 return OK;
1224}
1225
1226/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001227 * Generate an ISN_GETITEM instruction with "index".
1228 */
1229 static int
1230generate_GETITEM(cctx_T *cctx, int index)
1231{
1232 isn_T *isn;
1233 garray_T *stack = &cctx->ctx_type_stack;
1234 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1235 type_T *item_type = &t_any;
1236
1237 RETURN_OK_IF_SKIP(cctx);
1238
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001239 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001240 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001241 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001242 emsg(_(e_listreq));
1243 return FAIL;
1244 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001245 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001246 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1247 return FAIL;
1248 isn->isn_arg.number = index;
1249
1250 // add the item type to the type stack
1251 if (ga_grow(stack, 1) == FAIL)
1252 return FAIL;
1253 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1254 ++stack->ga_len;
1255 return OK;
1256}
1257
1258/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001259 * Generate an ISN_SLICE instruction with "count".
1260 */
1261 static int
1262generate_SLICE(cctx_T *cctx, int count)
1263{
1264 isn_T *isn;
1265
1266 RETURN_OK_IF_SKIP(cctx);
1267 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1268 return FAIL;
1269 isn->isn_arg.number = count;
1270 return OK;
1271}
1272
1273/*
1274 * Generate an ISN_CHECKLEN instruction with "min_len".
1275 */
1276 static int
1277generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1278{
1279 isn_T *isn;
1280
1281 RETURN_OK_IF_SKIP(cctx);
1282
1283 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1284 return FAIL;
1285 isn->isn_arg.checklen.cl_min_len = min_len;
1286 isn->isn_arg.checklen.cl_more_OK = more_OK;
1287
1288 return OK;
1289}
1290
1291/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 * Generate an ISN_STORE instruction.
1293 */
1294 static int
1295generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1296{
1297 isn_T *isn;
1298
Bram Moolenaar080457c2020-03-03 21:53:32 +01001299 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1301 return FAIL;
1302 if (name != NULL)
1303 isn->isn_arg.string = vim_strsave(name);
1304 else
1305 isn->isn_arg.number = idx;
1306
1307 return OK;
1308}
1309
1310/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001311 * Generate an ISN_STOREOUTER instruction.
1312 */
1313 static int
1314generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1315{
1316 isn_T *isn;
1317
1318 RETURN_OK_IF_SKIP(cctx);
1319 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1320 return FAIL;
1321 isn->isn_arg.outer.outer_idx = idx;
1322 isn->isn_arg.outer.outer_depth = level;
1323
1324 return OK;
1325}
1326
1327/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1329 */
1330 static int
1331generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1332{
1333 isn_T *isn;
1334
Bram Moolenaar080457c2020-03-03 21:53:32 +01001335 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1337 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001338 isn->isn_arg.storenr.stnr_idx = idx;
1339 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340
1341 return OK;
1342}
1343
1344/*
1345 * Generate an ISN_STOREOPT instruction
1346 */
1347 static int
1348generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1349{
1350 isn_T *isn;
1351
Bram Moolenaar080457c2020-03-03 21:53:32 +01001352 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001353 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 return FAIL;
1355 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1356 isn->isn_arg.storeopt.so_flags = opt_flags;
1357
1358 return OK;
1359}
1360
1361/*
1362 * Generate an ISN_LOAD or similar instruction.
1363 */
1364 static int
1365generate_LOAD(
1366 cctx_T *cctx,
1367 isntype_T isn_type,
1368 int idx,
1369 char_u *name,
1370 type_T *type)
1371{
1372 isn_T *isn;
1373
Bram Moolenaar080457c2020-03-03 21:53:32 +01001374 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1376 return FAIL;
1377 if (name != NULL)
1378 isn->isn_arg.string = vim_strsave(name);
1379 else
1380 isn->isn_arg.number = idx;
1381
1382 return OK;
1383}
1384
1385/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001386 * Generate an ISN_LOADOUTER instruction
1387 */
1388 static int
1389generate_LOADOUTER(
1390 cctx_T *cctx,
1391 int idx,
1392 int nesting,
1393 type_T *type)
1394{
1395 isn_T *isn;
1396
1397 RETURN_OK_IF_SKIP(cctx);
1398 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1399 return FAIL;
1400 isn->isn_arg.outer.outer_idx = idx;
1401 isn->isn_arg.outer.outer_depth = nesting;
1402
1403 return OK;
1404}
1405
1406/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001407 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001408 */
1409 static int
1410generate_LOADV(
1411 cctx_T *cctx,
1412 char_u *name,
1413 int error)
1414{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001415 int di_flags;
1416 int vidx = find_vim_var(name, &di_flags);
1417 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001418
Bram Moolenaar080457c2020-03-03 21:53:32 +01001419 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001420 if (vidx < 0)
1421 {
1422 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001423 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001424 return FAIL;
1425 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001426 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001427
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001428 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001429}
1430
1431/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001432 * Generate an ISN_UNLET instruction.
1433 */
1434 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001435generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001436{
1437 isn_T *isn;
1438
1439 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001440 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001441 return FAIL;
1442 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1443 isn->isn_arg.unlet.ul_forceit = forceit;
1444
1445 return OK;
1446}
1447
1448/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001449 * Generate an ISN_LOCKCONST instruction.
1450 */
1451 static int
1452generate_LOCKCONST(cctx_T *cctx)
1453{
1454 isn_T *isn;
1455
1456 RETURN_OK_IF_SKIP(cctx);
1457 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1458 return FAIL;
1459 return OK;
1460}
1461
1462/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 * Generate an ISN_LOADS instruction.
1464 */
1465 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001466generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001468 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001470 int sid,
1471 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472{
1473 isn_T *isn;
1474
Bram Moolenaar080457c2020-03-03 21:53:32 +01001475 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001476 if (isn_type == ISN_LOADS)
1477 isn = generate_instr_type(cctx, isn_type, type);
1478 else
1479 isn = generate_instr_drop(cctx, isn_type, 1);
1480 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001482 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1483 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484
1485 return OK;
1486}
1487
1488/*
1489 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1490 */
1491 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001492generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 cctx_T *cctx,
1494 isntype_T isn_type,
1495 int sid,
1496 int idx,
1497 type_T *type)
1498{
1499 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001500 scriptref_T *sref;
1501 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502
Bram Moolenaar080457c2020-03-03 21:53:32 +01001503 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 if (isn_type == ISN_LOADSCRIPT)
1505 isn = generate_instr_type(cctx, isn_type, type);
1506 else
1507 isn = generate_instr_drop(cctx, isn_type, 1);
1508 if (isn == NULL)
1509 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001510
1511 // This requires three arguments, which doesn't fit in an instruction, thus
1512 // we need to allocate a struct for this.
1513 sref = ALLOC_ONE(scriptref_T);
1514 if (sref == NULL)
1515 return FAIL;
1516 isn->isn_arg.script.scriptref = sref;
1517 sref->sref_sid = sid;
1518 sref->sref_idx = idx;
1519 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001520 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 return OK;
1522}
1523
1524/*
1525 * Generate an ISN_NEWLIST instruction.
1526 */
1527 static int
1528generate_NEWLIST(cctx_T *cctx, int count)
1529{
1530 isn_T *isn;
1531 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532 type_T *type;
1533 type_T *member;
1534
Bram Moolenaar080457c2020-03-03 21:53:32 +01001535 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1537 return FAIL;
1538 isn->isn_arg.number = count;
1539
Bram Moolenaar127542b2020-08-09 17:22:04 +02001540 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001541 if (count == 0)
1542 member = &t_void;
1543 else
1544 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001545 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1546 cctx->ctx_type_list);
1547 type = get_list_type(member, cctx->ctx_type_list);
1548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 // drop the value types
1550 stack->ga_len -= count;
1551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 // add the list type to the type stack
1553 if (ga_grow(stack, 1) == FAIL)
1554 return FAIL;
1555 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1556 ++stack->ga_len;
1557
1558 return OK;
1559}
1560
1561/*
1562 * Generate an ISN_NEWDICT instruction.
1563 */
1564 static int
1565generate_NEWDICT(cctx_T *cctx, int count)
1566{
1567 isn_T *isn;
1568 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 type_T *type;
1570 type_T *member;
1571
Bram Moolenaar080457c2020-03-03 21:53:32 +01001572 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1574 return FAIL;
1575 isn->isn_arg.number = count;
1576
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001577 if (count == 0)
1578 member = &t_void;
1579 else
1580 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001581 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1582 cctx->ctx_type_list);
1583 type = get_dict_type(member, cctx->ctx_type_list);
1584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 // drop the key and value types
1586 stack->ga_len -= 2 * count;
1587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 // add the dict type to the type stack
1589 if (ga_grow(stack, 1) == FAIL)
1590 return FAIL;
1591 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1592 ++stack->ga_len;
1593
1594 return OK;
1595}
1596
1597/*
1598 * Generate an ISN_FUNCREF instruction.
1599 */
1600 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001601generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602{
1603 isn_T *isn;
1604 garray_T *stack = &cctx->ctx_type_stack;
1605
Bram Moolenaar080457c2020-03-03 21:53:32 +01001606 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1608 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001609 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001610 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001612 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001613 // the nested context, including this one.
1614 if (ufunc->uf_flags & FC_CLOSURE)
1615 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 if (ga_grow(stack, 1) == FAIL)
1618 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001619 ((type_T **)stack->ga_data)[stack->ga_len] =
1620 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 ++stack->ga_len;
1622
1623 return OK;
1624}
1625
1626/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001627 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001628 * "lambda_name" and "func_name" must be in allocated memory and will be
1629 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001630 */
1631 static int
1632generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1633{
1634 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001635
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001636 if (cctx->ctx_skip == SKIP_YES)
1637 {
1638 vim_free(lambda_name);
1639 vim_free(func_name);
1640 return OK;
1641 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001642 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001643 {
1644 vim_free(lambda_name);
1645 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001646 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001647 }
1648 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001649 isn->isn_arg.newfunc.nf_global = func_name;
1650
1651 return OK;
1652}
1653
1654/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001655 * Generate an ISN_DEF instruction: list functions
1656 */
1657 static int
1658generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1659{
1660 isn_T *isn;
1661
1662 RETURN_OK_IF_SKIP(cctx);
1663 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1664 return FAIL;
1665 if (len > 0)
1666 {
1667 isn->isn_arg.string = vim_strnsave(name, len);
1668 if (isn->isn_arg.string == NULL)
1669 return FAIL;
1670 }
1671 return OK;
1672}
1673
1674/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 * Generate an ISN_JUMP instruction.
1676 */
1677 static int
1678generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1679{
1680 isn_T *isn;
1681 garray_T *stack = &cctx->ctx_type_stack;
1682
Bram Moolenaar080457c2020-03-03 21:53:32 +01001683 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1685 return FAIL;
1686 isn->isn_arg.jump.jump_when = when;
1687 isn->isn_arg.jump.jump_where = where;
1688
1689 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1690 --stack->ga_len;
1691
1692 return OK;
1693}
1694
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001695/*
1696 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1697 */
1698 static int
1699generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1700{
1701 isn_T *isn;
1702
1703 RETURN_OK_IF_SKIP(cctx);
1704 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1705 return FAIL;
1706 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1707 // jump_where is set later
1708 return OK;
1709}
1710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 static int
1712generate_FOR(cctx_T *cctx, int loop_idx)
1713{
1714 isn_T *isn;
1715 garray_T *stack = &cctx->ctx_type_stack;
1716
Bram Moolenaar080457c2020-03-03 21:53:32 +01001717 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1719 return FAIL;
1720 isn->isn_arg.forloop.for_idx = loop_idx;
1721
1722 if (ga_grow(stack, 1) == FAIL)
1723 return FAIL;
1724 // type doesn't matter, will be stored next
1725 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1726 ++stack->ga_len;
1727
1728 return OK;
1729}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001730/*
1731 * Generate an ISN_TRYCONT instruction.
1732 */
1733 static int
1734generate_TRYCONT(cctx_T *cctx, int levels, int where)
1735{
1736 isn_T *isn;
1737
1738 RETURN_OK_IF_SKIP(cctx);
1739 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1740 return FAIL;
1741 isn->isn_arg.trycont.tct_levels = levels;
1742 isn->isn_arg.trycont.tct_where = where;
1743
1744 return OK;
1745}
1746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747
1748/*
1749 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001750 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 * Return FAIL if the number of arguments is wrong.
1752 */
1753 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001754generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755{
1756 isn_T *isn;
1757 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001758 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001759 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001760 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761
Bram Moolenaar080457c2020-03-03 21:53:32 +01001762 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001763 argoff = check_internal_func(func_idx, argcount);
1764 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 return FAIL;
1766
Bram Moolenaar389df252020-07-09 21:20:47 +02001767 if (method_call && argoff > 1)
1768 {
1769 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1770 return FAIL;
1771 isn->isn_arg.shuffle.shfl_item = argcount;
1772 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1773 }
1774
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001775 if (argcount > 0)
1776 {
1777 // Check the types of the arguments.
1778 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001779 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1780 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001781 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001782 if (internal_func_is_map(func_idx))
1783 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001784 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1787 return FAIL;
1788 isn->isn_arg.bfunc.cbf_idx = func_idx;
1789 isn->isn_arg.bfunc.cbf_argcount = argcount;
1790
Bram Moolenaar94738d82020-10-21 14:25:07 +02001791 // Drop the argument types and push the return type.
1792 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 if (ga_grow(stack, 1) == FAIL)
1794 return FAIL;
1795 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001796 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001797 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001799 if (maptype != NULL && maptype->tt_member != NULL
1800 && maptype->tt_member != &t_any)
1801 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001802 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 return OK;
1805}
1806
1807/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001808 * Generate an ISN_LISTAPPEND instruction. Works like add().
1809 * Argument count is already checked.
1810 */
1811 static int
1812generate_LISTAPPEND(cctx_T *cctx)
1813{
1814 garray_T *stack = &cctx->ctx_type_stack;
1815 type_T *list_type;
1816 type_T *item_type;
1817 type_T *expected;
1818
1819 // Caller already checked that list_type is a list.
1820 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1821 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1822 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001823 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001824 return FAIL;
1825
1826 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1827 return FAIL;
1828
1829 --stack->ga_len; // drop the argument
1830 return OK;
1831}
1832
1833/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001834 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1835 * Argument count is already checked.
1836 */
1837 static int
1838generate_BLOBAPPEND(cctx_T *cctx)
1839{
1840 garray_T *stack = &cctx->ctx_type_stack;
1841 type_T *item_type;
1842
1843 // Caller already checked that blob_type is a blob.
1844 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001845 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001846 return FAIL;
1847
1848 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1849 return FAIL;
1850
1851 --stack->ga_len; // drop the argument
1852 return OK;
1853}
1854
1855/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001856 * Return TRUE if "ufunc" should be compiled, taking into account whether
1857 * "profile" indicates profiling is to be done.
1858 */
1859 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001860func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001861{
1862 switch (ufunc->uf_def_status)
1863 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001864 case UF_TO_BE_COMPILED:
1865 return TRUE;
1866
Bram Moolenaarb2049902021-01-24 12:53:53 +01001867 case UF_COMPILED:
1868 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001869#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001870 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1871 + ufunc->uf_dfunc_idx;
1872
1873 return profile ? dfunc->df_instr_prof == NULL
1874 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001875#else
1876 break;
1877#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001878 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001879
1880 case UF_NOT_COMPILED:
1881 case UF_COMPILE_ERROR:
1882 case UF_COMPILING:
1883 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001884 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001885 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001886}
1887
1888/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 * Generate an ISN_DCALL or ISN_UCALL instruction.
1890 * Return FAIL if the number of arguments is wrong.
1891 */
1892 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001893generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894{
1895 isn_T *isn;
1896 garray_T *stack = &cctx->ctx_type_stack;
1897 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001898 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899
Bram Moolenaar080457c2020-03-03 21:53:32 +01001900 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901 if (argcount > regular_args && !has_varargs(ufunc))
1902 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001903 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 return FAIL;
1905 }
1906 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1907 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001908 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 return FAIL;
1910 }
1911
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001912 if (ufunc->uf_def_status != UF_NOT_COMPILED
1913 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001914 {
1915 int i;
1916
1917 for (i = 0; i < argcount; ++i)
1918 {
1919 type_T *expected;
1920 type_T *actual;
1921
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001922 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1923 if (actual == &t_special
1924 && i >= regular_args - ufunc->uf_def_args.ga_len)
1925 {
1926 // assume v:none used for default argument value
1927 continue;
1928 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001929 if (i < regular_args)
1930 {
1931 if (ufunc->uf_arg_types == NULL)
1932 continue;
1933 expected = ufunc->uf_arg_types[i];
1934 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001935 else if (ufunc->uf_va_type == NULL
1936 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001937 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001938 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001939 else
1940 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001941 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001942 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001943 {
1944 arg_type_mismatch(expected, actual, i + 1);
1945 return FAIL;
1946 }
1947 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001948 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001949 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001950 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001951 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001952 }
1953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001955 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001956 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001958 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 {
1960 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1961 isn->isn_arg.dfunc.cdf_argcount = argcount;
1962 }
1963 else
1964 {
1965 // A user function may be deleted and redefined later, can't use the
1966 // ufunc pointer, need to look it up again at runtime.
1967 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1968 isn->isn_arg.ufunc.cuf_argcount = argcount;
1969 }
1970
1971 stack->ga_len -= argcount; // drop the arguments
1972 if (ga_grow(stack, 1) == FAIL)
1973 return FAIL;
1974 // add return value
1975 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1976 ++stack->ga_len;
1977
1978 return OK;
1979}
1980
1981/*
1982 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1983 */
1984 static int
1985generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1986{
1987 isn_T *isn;
1988 garray_T *stack = &cctx->ctx_type_stack;
1989
Bram Moolenaar080457c2020-03-03 21:53:32 +01001990 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1992 return FAIL;
1993 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1994 isn->isn_arg.ufunc.cuf_argcount = argcount;
1995
1996 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001997 if (ga_grow(stack, 1) == FAIL)
1998 return FAIL;
1999 // add return value
2000 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2001 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002
2003 return OK;
2004}
2005
2006/*
2007 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002008 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 */
2010 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002011generate_PCALL(
2012 cctx_T *cctx,
2013 int argcount,
2014 char_u *name,
2015 type_T *type,
2016 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017{
2018 isn_T *isn;
2019 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002020 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021
Bram Moolenaar080457c2020-03-03 21:53:32 +01002022 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002023
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002024 if (type->tt_type == VAR_ANY)
2025 ret_type = &t_any;
2026 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002027 {
2028 if (type->tt_argcount != -1)
2029 {
2030 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2031
2032 if (argcount < type->tt_min_argcount - varargs)
2033 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002034 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002035 return FAIL;
2036 }
2037 if (!varargs && argcount > type->tt_argcount)
2038 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002039 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002040 return FAIL;
2041 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002042 if (type->tt_args != NULL)
2043 {
2044 int i;
2045
2046 for (i = 0; i < argcount; ++i)
2047 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002048 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002049 type_T *actual = ((type_T **)stack->ga_data)[
2050 stack->ga_len + offset];
2051 type_T *expected;
2052
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002053 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002054 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002055 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002056 else if (i >= type->tt_min_argcount
2057 && actual == &t_special)
2058 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002059 else
2060 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002061 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002062 cctx, TRUE, FALSE) == FAIL)
2063 {
2064 arg_type_mismatch(expected, actual, i + 1);
2065 return FAIL;
2066 }
2067 }
2068 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002069 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002070 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002071 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002072 else
2073 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002074 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002075 return FAIL;
2076 }
2077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2079 return FAIL;
2080 isn->isn_arg.pfunc.cpf_top = at_top;
2081 isn->isn_arg.pfunc.cpf_argcount = argcount;
2082
2083 stack->ga_len -= argcount; // drop the arguments
2084
2085 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002086 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002088 // If partial is above the arguments it must be cleared and replaced with
2089 // the return value.
2090 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2091 return FAIL;
2092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 return OK;
2094}
2095
2096/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002097 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 */
2099 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002100generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101{
2102 isn_T *isn;
2103 garray_T *stack = &cctx->ctx_type_stack;
2104 type_T *type;
2105
Bram Moolenaar080457c2020-03-03 21:53:32 +01002106 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002107 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002109 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002111 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002113 if (type->tt_type != VAR_DICT && type != &t_any)
2114 {
2115 emsg(_(e_dictreq));
2116 return FAIL;
2117 }
2118 // change dict type to dict member type
2119 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002120 {
2121 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2122 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124
2125 return OK;
2126}
2127
2128/*
2129 * Generate an ISN_ECHO instruction.
2130 */
2131 static int
2132generate_ECHO(cctx_T *cctx, int with_white, int count)
2133{
2134 isn_T *isn;
2135
Bram Moolenaar080457c2020-03-03 21:53:32 +01002136 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2138 return FAIL;
2139 isn->isn_arg.echo.echo_with_white = with_white;
2140 isn->isn_arg.echo.echo_count = count;
2141
2142 return OK;
2143}
2144
Bram Moolenaarad39c092020-02-26 18:23:43 +01002145/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002146 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002147 */
2148 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002149generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002150{
2151 isn_T *isn;
2152
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002153 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002154 return FAIL;
2155 isn->isn_arg.number = count;
2156
2157 return OK;
2158}
2159
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002160/*
2161 * Generate an ISN_PUT instruction.
2162 */
2163 static int
2164generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2165{
2166 isn_T *isn;
2167
2168 RETURN_OK_IF_SKIP(cctx);
2169 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2170 return FAIL;
2171 isn->isn_arg.put.put_regname = regname;
2172 isn->isn_arg.put.put_lnum = lnum;
2173 return OK;
2174}
2175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 static int
2177generate_EXEC(cctx_T *cctx, char_u *line)
2178{
2179 isn_T *isn;
2180
Bram Moolenaar080457c2020-03-03 21:53:32 +01002181 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2183 return FAIL;
2184 isn->isn_arg.string = vim_strsave(line);
2185 return OK;
2186}
2187
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002188 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002189generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2190{
2191 isn_T *isn;
2192 garray_T *stack = &cctx->ctx_type_stack;
2193
2194 RETURN_OK_IF_SKIP(cctx);
2195 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2196 return FAIL;
2197 isn->isn_arg.string = vim_strsave(line);
2198
2199 if (ga_grow(stack, 1) == FAIL)
2200 return FAIL;
2201 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2202 ++stack->ga_len;
2203
2204 return OK;
2205}
2206
2207 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002208generate_EXECCONCAT(cctx_T *cctx, int count)
2209{
2210 isn_T *isn;
2211
2212 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2213 return FAIL;
2214 isn->isn_arg.number = count;
2215 return OK;
2216}
2217
Bram Moolenaar08597872020-12-10 19:43:40 +01002218/*
2219 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2220 */
2221 static int
2222generate_RANGE(cctx_T *cctx, char_u *range)
2223{
2224 isn_T *isn;
2225 garray_T *stack = &cctx->ctx_type_stack;
2226
2227 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2228 return FAIL;
2229 isn->isn_arg.string = range;
2230
2231 if (ga_grow(stack, 1) == FAIL)
2232 return FAIL;
2233 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2234 ++stack->ga_len;
2235 return OK;
2236}
2237
Bram Moolenaar792f7862020-11-23 08:31:18 +01002238 static int
2239generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2240{
2241 isn_T *isn;
2242
2243 RETURN_OK_IF_SKIP(cctx);
2244 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2245 return FAIL;
2246 isn->isn_arg.unpack.unp_count = var_count;
2247 isn->isn_arg.unpack.unp_semicolon = semicolon;
2248 return OK;
2249}
2250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002252 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002253 */
2254 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002255generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002256{
2257 isn_T *isn;
2258
Bram Moolenaarfa984412021-03-25 22:15:28 +01002259 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002260 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002261 cctx->ctx_has_cmdmod = TRUE;
2262
2263 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002264 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002265 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2266 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2267 return FAIL;
2268 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002269 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002270 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002271 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002272
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002273 return OK;
2274}
2275
2276 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002277generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002278{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002279 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2280 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002281 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002282 return OK;
2283}
2284
Bram Moolenaarfa984412021-03-25 22:15:28 +01002285 static int
2286misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002287{
2288 garray_T *instr = &cctx->ctx_instr;
2289
Bram Moolenaara91a7132021-03-25 21:12:15 +01002290 if (cctx->ctx_has_cmdmod
2291 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2292 == ISN_CMDMOD)
2293 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002294 emsg(_(e_misplaced_command_modifier));
2295 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002296 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002297 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002298}
2299
2300/*
2301 * Get the index of the current instruction.
2302 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2303 */
2304 static int
2305current_instr_idx(cctx_T *cctx)
2306{
2307 garray_T *instr = &cctx->ctx_instr;
2308 int idx = instr->ga_len;
2309
2310 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2311 .isn_type == ISN_CMDMOD)
2312 --idx;
2313#ifdef FEAT_PROFILE
2314 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2315 .isn_type == ISN_PROF_START)
2316 --idx;
2317#endif
2318 return idx;
2319}
2320
Bram Moolenaarf002a412021-01-24 13:34:18 +01002321#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002322 static void
2323may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2324{
2325 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002326 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002327}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002328#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002329
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002330/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002331 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002332 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002334 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002335reserve_local(
2336 cctx_T *cctx,
2337 char_u *name,
2338 size_t len,
2339 int isConst,
2340 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 lvar_T *lvar;
2343
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002344 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002346 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002347 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 }
2349
2350 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002351 return NULL;
2352 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002353 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002355 // Every local variable uses the next entry on the stack. We could re-use
2356 // the last ones when leaving a scope, but then variables used in a closure
2357 // might get overwritten. To keep things simple do not re-use stack
2358 // entries. This is less efficient, but memory is cheap these days.
2359 lvar->lv_idx = cctx->ctx_locals_count++;
2360
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002361 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 lvar->lv_const = isConst;
2363 lvar->lv_type = type;
2364
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002365 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366}
2367
2368/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002369 * Remove local variables above "new_top".
2370 */
2371 static void
2372unwind_locals(cctx_T *cctx, int new_top)
2373{
2374 if (cctx->ctx_locals.ga_len > new_top)
2375 {
2376 int idx;
2377 lvar_T *lvar;
2378
2379 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2380 {
2381 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2382 vim_free(lvar->lv_name);
2383 }
2384 }
2385 cctx->ctx_locals.ga_len = new_top;
2386}
2387
2388/*
2389 * Free all local variables.
2390 */
2391 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002392free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002393{
2394 unwind_locals(cctx, 0);
2395 ga_clear(&cctx->ctx_locals);
2396}
2397
2398/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002399 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2400 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2401 * error if the variable was defined with :const.
2402 */
2403 static int
2404check_item_writable(svar_T *sv, int check_writable, char_u *name)
2405{
2406 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2407 || (check_writable == ASSIGN_FINAL
2408 && sv->sv_const == ASSIGN_CONST))
2409 {
2410 semsg(_(e_readonlyvar), name);
2411 return FAIL;
2412 }
2413 return OK;
2414}
2415
2416/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002418 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 * Returns the index in "sn_var_vals" if found.
2420 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002421 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 */
2423 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002424get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425{
2426 hashtab_T *ht;
2427 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002428 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002429 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 int idx;
2431
Bram Moolenaare3d46852020-08-29 13:39:17 +02002432 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002434 if (sid == current_sctx.sc_sid)
2435 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002436 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002437
2438 if (sav == NULL)
2439 return -2;
2440 idx = sav->sav_var_vals_idx;
2441 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002442 if (check_item_writable(sv, check_writable, name) == FAIL)
2443 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002444 return idx;
2445 }
2446
2447 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 ht = &SCRIPT_VARS(sid);
2449 di = find_var_in_ht(ht, 0, name, TRUE);
2450 if (di == NULL)
2451 return -2;
2452
2453 // Now find the svar_T index in sn_var_vals.
2454 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2455 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002456 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 if (sv->sv_tv == &di->di_tv)
2458 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002459 if (check_item_writable(sv, check_writable, name) == FAIL)
2460 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 return idx;
2462 }
2463 }
2464 return -1;
2465}
2466
2467/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002468 * Find "name" in imported items of the current script or in "cctx" if not
2469 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 */
2471 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002472find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 int idx;
2475
Bram Moolenaare3d46852020-08-29 13:39:17 +02002476 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002477 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 if (cctx != NULL)
2479 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2480 {
2481 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2482 + idx;
2483
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002484 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2485 : STRLEN(import->imp_name) == len
2486 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 return import;
2488 }
2489
Bram Moolenaarefa94442020-08-08 22:16:00 +02002490 return find_imported_in_script(name, len, current_sctx.sc_sid);
2491}
2492
2493 imported_T *
2494find_imported_in_script(char_u *name, size_t len, int sid)
2495{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002496 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002497 int idx;
2498
Bram Moolenaare3d46852020-08-29 13:39:17 +02002499 if (!SCRIPT_ID_VALID(sid))
2500 return NULL;
2501 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2503 {
2504 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2505
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002506 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2507 : STRLEN(import->imp_name) == len
2508 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 return import;
2510 }
2511 return NULL;
2512}
2513
2514/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002515 * Free all imported variables.
2516 */
2517 static void
2518free_imported(cctx_T *cctx)
2519{
2520 int idx;
2521
2522 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2523 {
2524 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2525
2526 vim_free(import->imp_name);
2527 }
2528 ga_clear(&cctx->ctx_imports);
2529}
2530
2531/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002532 * Return a pointer to the next line that isn't empty or only contains a
2533 * comment. Skips over white space.
2534 * Returns NULL if there is none.
2535 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002536 char_u *
2537peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002538{
2539 int lnum = cctx->ctx_lnum;
2540
2541 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2542 {
2543 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002544 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002545
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002546 // ignore NULLs inserted for continuation lines
2547 if (line != NULL)
2548 {
2549 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002550 if (vim9_bad_comment(p))
2551 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002552 if (*p != NUL && !vim9_comment_start(p))
2553 return p;
2554 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002555 }
2556 return NULL;
2557}
2558
2559/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002560 * Called when checking for a following operator at "arg". When the rest of
2561 * the line is empty or only a comment, peek the next line. If there is a next
2562 * line return a pointer to it and set "nextp".
2563 * Otherwise skip over white space.
2564 */
2565 static char_u *
2566may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2567{
2568 char_u *p = skipwhite(arg);
2569
2570 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002571 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002572 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002573 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002574 if (*nextp != NULL)
2575 return *nextp;
2576 }
2577 return p;
2578}
2579
2580/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002581 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002582 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002583 * Returns NULL when at the end.
2584 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002585 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002586next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002587{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002588 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002589
2590 do
2591 {
2592 ++cctx->ctx_lnum;
2593 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002594 {
2595 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002596 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002597 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002598 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002599 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002600 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002601 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002602 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002603 return line;
2604}
2605
2606/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002607 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002608 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002609 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002610 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2611 */
2612 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002613may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002614{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002615 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002616 if (vim9_bad_comment(*arg))
2617 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002618 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002619 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002620 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002621
2622 if (next == NULL)
2623 return FAIL;
2624 *arg = skipwhite(next);
2625 }
2626 return OK;
2627}
2628
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002629/*
2630 * Idem, and give an error when failed.
2631 */
2632 static int
2633may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2634{
2635 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2636 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002637 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002638 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002639 return FAIL;
2640 }
2641 return OK;
2642}
2643
2644
Bram Moolenaara5565e42020-05-09 15:44:01 +02002645// Structure passed between the compile_expr* functions to keep track of
2646// constants that have been parsed but for which no code was produced yet. If
2647// possible expressions on these constants are applied at compile time. If
2648// that is not possible, the code to push the constants needs to be generated
2649// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002650// Using 50 should be more than enough of 5 levels of ().
2651#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002652typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002653 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002654 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002655 int pp_is_const; // all generated code was constants, used for a
2656 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002657} ppconst_T;
2658
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002659static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002660static int compile_expr0(char_u **arg, cctx_T *cctx);
2661static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2662
Bram Moolenaara5565e42020-05-09 15:44:01 +02002663/*
2664 * Generate a PUSH instruction for "tv".
2665 * "tv" will be consumed or cleared.
2666 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2667 */
2668 static int
2669generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2670{
2671 if (tv != NULL)
2672 {
2673 switch (tv->v_type)
2674 {
2675 case VAR_UNKNOWN:
2676 break;
2677 case VAR_BOOL:
2678 generate_PUSHBOOL(cctx, tv->vval.v_number);
2679 break;
2680 case VAR_SPECIAL:
2681 generate_PUSHSPEC(cctx, tv->vval.v_number);
2682 break;
2683 case VAR_NUMBER:
2684 generate_PUSHNR(cctx, tv->vval.v_number);
2685 break;
2686#ifdef FEAT_FLOAT
2687 case VAR_FLOAT:
2688 generate_PUSHF(cctx, tv->vval.v_float);
2689 break;
2690#endif
2691 case VAR_BLOB:
2692 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2693 tv->vval.v_blob = NULL;
2694 break;
2695 case VAR_STRING:
2696 generate_PUSHS(cctx, tv->vval.v_string);
2697 tv->vval.v_string = NULL;
2698 break;
2699 default:
2700 iemsg("constant type not supported");
2701 clear_tv(tv);
2702 return FAIL;
2703 }
2704 tv->v_type = VAR_UNKNOWN;
2705 }
2706 return OK;
2707}
2708
2709/*
2710 * Generate code for any ppconst entries.
2711 */
2712 static int
2713generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2714{
2715 int i;
2716 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002717 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002718
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002719 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002720 for (i = 0; i < ppconst->pp_used; ++i)
2721 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2722 ret = FAIL;
2723 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002724 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002725 return ret;
2726}
2727
2728/*
2729 * Clear ppconst constants. Used when failing.
2730 */
2731 static void
2732clear_ppconst(ppconst_T *ppconst)
2733{
2734 int i;
2735
2736 for (i = 0; i < ppconst->pp_used; ++i)
2737 clear_tv(&ppconst->pp_tv[i]);
2738 ppconst->pp_used = 0;
2739}
2740
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002741/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002742 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002743 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002744 */
2745 static int
2746compile_member(int is_slice, cctx_T *cctx)
2747{
2748 type_T **typep;
2749 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002750 vartype_T vartype;
2751 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002752
Bram Moolenaar261417b2021-05-06 21:04:55 +02002753 // We can index a list, dict and blob. If we don't know the type
2754 // we can use the index value type. If we still don't know use an "ANY"
2755 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002756 typep = ((type_T **)stack->ga_data) + stack->ga_len
2757 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002758 vartype = (*typep)->tt_type;
2759 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002760 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002761 if (*typep == &t_any && idxtype == &t_string)
2762 vartype = VAR_DICT;
2763 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002764 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002765 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002766 return FAIL;
2767 if (is_slice)
2768 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002769 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2770 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002771 FALSE, FALSE) == FAIL)
2772 return FAIL;
2773 }
2774 }
2775
Bram Moolenaar261417b2021-05-06 21:04:55 +02002776 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002777 {
2778 if (is_slice)
2779 {
2780 emsg(_(e_cannot_slice_dictionary));
2781 return FAIL;
2782 }
2783 if ((*typep)->tt_type == VAR_DICT)
2784 {
2785 *typep = (*typep)->tt_member;
2786 if (*typep == &t_unknown)
2787 // empty dict was used
2788 *typep = &t_any;
2789 }
2790 else
2791 {
2792 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2793 FALSE, FALSE) == FAIL)
2794 return FAIL;
2795 *typep = &t_any;
2796 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002797 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002798 return FAIL;
2799 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2800 return FAIL;
2801 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002802 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002803 {
2804 *typep = &t_string;
2805 if ((is_slice
2806 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2807 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2808 return FAIL;
2809 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002810 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002811 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002812 if (is_slice)
2813 {
2814 *typep = &t_blob;
2815 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2816 return FAIL;
2817 }
2818 else
2819 {
2820 *typep = &t_number;
2821 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2822 return FAIL;
2823 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002824 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002825 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002826 {
2827 if (is_slice)
2828 {
2829 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002830 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002831 2) == FAIL)
2832 return FAIL;
2833 }
2834 else
2835 {
2836 if ((*typep)->tt_type == VAR_LIST)
2837 {
2838 *typep = (*typep)->tt_member;
2839 if (*typep == &t_unknown)
2840 // empty list was used
2841 *typep = &t_any;
2842 }
2843 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002844 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2845 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002846 return FAIL;
2847 }
2848 }
2849 else
2850 {
2851 emsg(_(e_string_list_dict_or_blob_required));
2852 return FAIL;
2853 }
2854 return OK;
2855}
2856
2857/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002858 * Generate an instruction to load script-local variable "name", without the
2859 * leading "s:".
2860 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 */
2862 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002863compile_load_scriptvar(
2864 cctx_T *cctx,
2865 char_u *name, // variable NUL terminated
2866 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002867 char_u **end, // end of variable
2868 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002870 scriptitem_T *si;
2871 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 imported_T *import;
2873
Bram Moolenaare3d46852020-08-29 13:39:17 +02002874 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2875 return FAIL;
2876 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002877 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002878 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002880 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002881 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2882 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 }
2884 if (idx >= 0)
2885 {
2886 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2887
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002888 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 current_sctx.sc_sid, idx, sv->sv_type);
2890 return OK;
2891 }
2892
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002893 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 if (import != NULL)
2895 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002896 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002897 {
2898 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002899 char_u *exp_name;
2900 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002901 ufunc_T *ufunc;
2902 type_T *type;
2903
2904 // Used "import * as Name", need to lookup the member.
2905 if (*p != '.')
2906 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002907 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002908 return FAIL;
2909 }
2910 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002911 if (VIM_ISWHITE(*p))
2912 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002913 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002914 return FAIL;
2915 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002916
Bram Moolenaar1c991142020-07-04 13:15:31 +02002917 // isolate one name
2918 exp_name = p;
2919 while (eval_isnamec(*p))
2920 ++p;
2921 cc = *p;
2922 *p = NUL;
2923
Bram Moolenaaredba7072021-03-13 21:14:18 +01002924 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2925 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002926 *p = cc;
2927 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002928 *end = p;
2929
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002930 if (idx < 0)
2931 {
2932 if (*p == '(' && ufunc != NULL)
2933 {
2934 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2935 return OK;
2936 }
2937 return FAIL;
2938 }
2939
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002940 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2941 import->imp_sid,
2942 idx,
2943 type);
2944 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002945 else if (import->imp_funcname != NULL)
2946 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002947 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002948 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2949 import->imp_sid,
2950 import->imp_var_vals_idx,
2951 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 return OK;
2953 }
2954
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002955 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002956 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 return FAIL;
2958}
2959
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002960 static int
2961generate_funcref(cctx_T *cctx, char_u *name)
2962{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002963 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002964
2965 if (ufunc == NULL)
2966 return FAIL;
2967
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002968 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002969 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2970 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002971 == FAIL)
2972 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002973 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002974}
2975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976/*
2977 * Compile a variable name into a load instruction.
2978 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002979 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 * When "error" is FALSE do not give an error when not found.
2981 */
2982 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002983compile_load(
2984 char_u **arg,
2985 char_u *end_arg,
2986 cctx_T *cctx,
2987 int is_expr,
2988 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989{
2990 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002991 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002992 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002994 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995
2996 if (*(*arg + 1) == ':')
2997 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002998 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002999 {
3000 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001
Bram Moolenaarfa596382021-04-07 21:58:16 +02003002 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003003 switch (**arg)
3004 {
3005 case 'g': isn_type = ISN_LOADGDICT; break;
3006 case 'w': isn_type = ISN_LOADWDICT; break;
3007 case 't': isn_type = ISN_LOADTDICT; break;
3008 case 'b': isn_type = ISN_LOADBDICT; break;
3009 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003010 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003011 goto theend;
3012 }
3013 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3014 goto theend;
3015 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 else
3018 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003019 isntype_T isn_type = ISN_DROP;
3020
Bram Moolenaarfa596382021-04-07 21:58:16 +02003021 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003022 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3023 if (name == NULL)
3024 return FAIL;
3025
3026 switch (**arg)
3027 {
3028 case 'v': res = generate_LOADV(cctx, name, error);
3029 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003030 case 's': if (is_expr && ASCII_ISUPPER(*name)
3031 && find_func(name, FALSE, cctx) != NULL)
3032 res = generate_funcref(cctx, name);
3033 else
3034 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003035 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003036 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003037 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003038 {
3039 if (is_expr && ASCII_ISUPPER(*name)
3040 && find_func(name, FALSE, cctx) != NULL)
3041 res = generate_funcref(cctx, name);
3042 else
3043 isn_type = ISN_LOADG;
3044 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003045 else
3046 {
3047 isn_type = ISN_LOADAUTO;
3048 vim_free(name);
3049 name = vim_strnsave(*arg, end - *arg);
3050 if (name == NULL)
3051 return FAIL;
3052 }
3053 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003054 case 'w': isn_type = ISN_LOADW; break;
3055 case 't': isn_type = ISN_LOADT; break;
3056 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003057 default: // cannot happen, just in case
3058 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003059 goto theend;
3060 }
3061 if (isn_type != ISN_DROP)
3062 {
3063 // Global, Buffer-local, Window-local and Tabpage-local
3064 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003065 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003066 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 }
3069 }
3070 else
3071 {
3072 size_t len = end - *arg;
3073 int idx;
3074 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003075 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076
3077 name = vim_strnsave(*arg, end - *arg);
3078 if (name == NULL)
3079 return FAIL;
3080
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003081 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003083 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003084 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 }
3086 else
3087 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003088 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003089
Bram Moolenaar709664c2020-12-12 14:33:41 +01003090 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003092 type = lvar.lv_type;
3093 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003094 if (lvar.lv_from_outer != 0)
3095 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003096 else
3097 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 }
3099 else
3100 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003101 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003102 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003103 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003104 || find_imported(name, 0, cctx) != NULL)
3105 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003106
Bram Moolenaar0f769812020-09-12 18:32:34 +02003107 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003108 // uppercase letter it can be a user defined function.
3109 // generate_funcref() will fail if the function can't be found.
3110 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003111 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 }
3113 }
3114 if (gen_load)
3115 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003116 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003117 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003118 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003119 cctx->ctx_outer_used = TRUE;
3120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 }
3122
3123 *arg = end;
3124
3125theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003126 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003127 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 vim_free(name);
3129 return res;
3130}
3131
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003132 static void
3133clear_instr_ga(garray_T *gap)
3134{
3135 int idx;
3136
3137 for (idx = 0; idx < gap->ga_len; ++idx)
3138 delete_instr(((isn_T *)gap->ga_data) + idx);
3139 ga_clear(gap);
3140}
3141
3142/*
3143 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3144 * Returns FAIL if compilation fails.
3145 */
3146 static int
3147compile_string(isn_T *isn, cctx_T *cctx)
3148{
3149 char_u *s = isn->isn_arg.string;
3150 garray_T save_ga = cctx->ctx_instr;
3151 int expr_res;
3152 int trailing_error;
3153 int instr_count;
3154 isn_T *instr = NULL;
3155
3156 // Temporarily reset the list of instructions so that the jump labels are
3157 // correct.
3158 cctx->ctx_instr.ga_len = 0;
3159 cctx->ctx_instr.ga_maxlen = 0;
3160 cctx->ctx_instr.ga_data = NULL;
3161 expr_res = compile_expr0(&s, cctx);
3162 s = skipwhite(s);
3163 trailing_error = *s != NUL;
3164
Bram Moolenaarff652882021-05-16 15:24:49 +02003165 if (expr_res == FAIL || trailing_error
3166 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003167 {
3168 if (trailing_error)
3169 semsg(_(e_trailing_arg), s);
3170 clear_instr_ga(&cctx->ctx_instr);
3171 cctx->ctx_instr = save_ga;
3172 return FAIL;
3173 }
3174
3175 // Move the generated instructions into the ISN_INSTR instruction, then
3176 // restore the list of instructions.
3177 instr_count = cctx->ctx_instr.ga_len;
3178 instr = cctx->ctx_instr.ga_data;
3179 instr[instr_count].isn_type = ISN_FINISH;
3180
3181 cctx->ctx_instr = save_ga;
3182 vim_free(isn->isn_arg.string);
3183 isn->isn_type = ISN_INSTR;
3184 isn->isn_arg.instr = instr;
3185 return OK;
3186}
3187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188/*
3189 * Compile the argument expressions.
3190 * "arg" points to just after the "(" and is advanced to after the ")"
3191 */
3192 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003193compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003195 char_u *p = *arg;
3196 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003197 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003198 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199
Bram Moolenaare6085c52020-04-12 20:19:16 +02003200 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003202 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3203 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003204 if (*p == ')')
3205 {
3206 *arg = p + 1;
3207 return OK;
3208 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003209 if (must_end)
3210 {
3211 semsg(_(e_missing_comma_before_argument_str), p);
3212 return FAIL;
3213 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003214
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003215 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003216 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 return FAIL;
3218 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003219
Bram Moolenaarff652882021-05-16 15:24:49 +02003220 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003221 && cctx->ctx_instr.ga_len == instr_count + 1)
3222 {
3223 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3224
3225 // {skip} argument of searchpair() can be compiled if not empty
3226 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3227 compile_string(isn, cctx);
3228 }
3229
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003230 if (*p != ',' && *skipwhite(p) == ',')
3231 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003232 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003233 p = skipwhite(p);
3234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003236 {
3237 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003238 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003239 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003240 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003241 else
3242 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003243 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003244 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003246failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003247 emsg(_(e_missing_close));
3248 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249}
3250
3251/*
3252 * Compile a function call: name(arg1, arg2)
3253 * "arg" points to "name", "arg + varlen" to the "(".
3254 * "argcount_init" is 1 for "value->method()"
3255 * Instructions:
3256 * EVAL arg1
3257 * EVAL arg2
3258 * BCALL / DCALL / UCALL
3259 */
3260 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003261compile_call(
3262 char_u **arg,
3263 size_t varlen,
3264 cctx_T *cctx,
3265 ppconst_T *ppconst,
3266 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267{
3268 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003269 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 int argcount = argcount_init;
3271 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003272 char_u fname_buf[FLEN_FIXED + 1];
3273 char_u *tofree = NULL;
3274 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003275 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003276 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003277 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003278 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279
Bram Moolenaara5565e42020-05-09 15:44:01 +02003280 // we can evaluate "has('name')" at compile time
3281 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3282 {
3283 char_u *s = skipwhite(*arg + varlen + 1);
3284 typval_T argvars[2];
3285
3286 argvars[0].v_type = VAR_UNKNOWN;
3287 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003288 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003289 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003290 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003291 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003292 if (*s == ')' && argvars[0].v_type == VAR_STRING
3293 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003294 {
3295 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3296
3297 *arg = s + 1;
3298 argvars[1].v_type = VAR_UNKNOWN;
3299 tv->v_type = VAR_NUMBER;
3300 tv->vval.v_number = 0;
3301 f_has(argvars, tv);
3302 clear_tv(&argvars[0]);
3303 ++ppconst->pp_used;
3304 return OK;
3305 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003306 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003307 }
3308
3309 if (generate_ppconst(cctx, ppconst) == FAIL)
3310 return FAIL;
3311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 if (varlen >= sizeof(namebuf))
3313 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003314 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 return FAIL;
3316 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003317 vim_strncpy(namebuf, *arg, varlen);
3318 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003320 // We handle the "skip" argument of searchpair() and searchpairpos()
3321 // differently.
3322 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3323 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3324 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3325 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003328 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003329 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330
Bram Moolenaar03290b82020-12-19 16:30:44 +01003331 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003332 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 {
3334 int idx;
3335
3336 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003337 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003339 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003340 if (STRCMP(name, "flatten") == 0)
3341 {
3342 emsg(_(e_cannot_use_flatten_in_vim9_script));
3343 goto theend;
3344 }
3345
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003346 if (STRCMP(name, "add") == 0 && argcount == 2)
3347 {
3348 garray_T *stack = &cctx->ctx_type_stack;
3349 type_T *type = ((type_T **)stack->ga_data)[
3350 stack->ga_len - 2];
3351
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003352 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003353 if (type->tt_type == VAR_LIST)
3354 {
3355 // inline "add(list, item)" so that the type can be checked
3356 res = generate_LISTAPPEND(cctx);
3357 idx = -1;
3358 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003359 else if (type->tt_type == VAR_BLOB)
3360 {
3361 // inline "add(blob, nr)" so that the type can be checked
3362 res = generate_BLOBAPPEND(cctx);
3363 idx = -1;
3364 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003365 }
3366
3367 if (idx >= 0)
3368 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3369 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003370 else
3371 semsg(_(e_unknownfunc), namebuf);
3372 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 }
3374
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003375 // An argument or local variable can be a function reference, this
3376 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003377 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003378 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003379 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003380 // If we can find the function by name generate the right call.
3381 // Skip global functions here, a local funcref takes precedence.
3382 ufunc = find_func(name, FALSE, cctx);
3383 if (ufunc != NULL && !func_is_global(ufunc))
3384 {
3385 res = generate_CALL(cctx, ufunc, argcount);
3386 goto theend;
3387 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389
3390 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003391 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003392 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003394 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003395 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003396 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003397 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003398 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003399
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003400 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003401 goto theend;
3402 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403
Bram Moolenaar0f769812020-09-12 18:32:34 +02003404 // If we can find a global function by name generate the right call.
3405 if (ufunc != NULL)
3406 {
3407 res = generate_CALL(cctx, ufunc, argcount);
3408 goto theend;
3409 }
3410
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003411 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003412 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003413 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003414 res = generate_UCALL(cctx, name, argcount);
3415 else
3416 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003417
3418theend:
3419 vim_free(tofree);
3420 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421}
3422
3423// like NAMESPACE_CHAR but with 'a' and 'l'.
3424#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3425
3426/*
3427 * Find the end of a variable or function name. Unlike find_name_end() this
3428 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003429 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 * Return a pointer to just after the name. Equal to "arg" if there is no
3431 * valid name.
3432 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003433 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003434to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435{
3436 char_u *p;
3437
3438 // Quick check for valid starting character.
3439 if (!eval_isnamec1(*arg))
3440 return arg;
3441
3442 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3443 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3444 // and can be used in slice "[n:]".
3445 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003446 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3448 break;
3449 return p;
3450}
3451
3452/*
3453 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003454 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 */
3456 char_u *
3457to_name_const_end(char_u *arg)
3458{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003459 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 typval_T rettv;
3461
3462 if (p == arg && *arg == '[')
3463 {
3464
3465 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003466 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 p = arg;
3468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 return p;
3470}
3471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472/*
3473 * parse a list: [expr, expr]
3474 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003475 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 */
3477 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003478compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479{
3480 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003481 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003483 int is_const;
3484 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003486 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003488 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003489 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003490 semsg(_(e_list_end), *arg);
3491 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003492 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003493 if (*p == ',')
3494 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003495 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003496 return FAIL;
3497 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003498 if (*p == ']')
3499 {
3500 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003501 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003502 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003503 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003504 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003505 if (!is_const)
3506 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 ++count;
3508 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003509 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003511 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3512 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003513 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003514 return FAIL;
3515 }
3516 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003517 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 p = skipwhite(p);
3519 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003520 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003522 ppconst->pp_is_const = is_all_const;
3523 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524}
3525
3526/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003527 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003528 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003529 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 */
3531 static int
3532compile_lambda(char_u **arg, cctx_T *cctx)
3533{
Bram Moolenaare462f522020-12-27 14:43:30 +01003534 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 typval_T rettv;
3536 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003537 evalarg_T evalarg;
3538
3539 CLEAR_FIELD(evalarg);
3540 evalarg.eval_flags = EVAL_EVALUATE;
3541 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542
3543 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003544 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3545 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003546 {
3547 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003548 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003549 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003550
Bram Moolenaar65c44152020-12-24 15:14:01 +01003551 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003553 ++ufunc->uf_refcount;
3554 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555
Bram Moolenaar65c44152020-12-24 15:14:01 +01003556 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003557 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003559 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3560 // points into it. Point to the original line to avoid a dangling pointer.
3561 if (evalarg.eval_tofree_cmdline != NULL)
3562 {
3563 size_t off = *arg - evalarg.eval_tofree_cmdline;
3564
3565 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3566 + off;
3567 }
3568
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003569 clear_evalarg(&evalarg, NULL);
3570
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003571 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003572 {
3573 // The return type will now be known.
3574 set_function_type(ufunc);
3575
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003576 // The function reference count will be 1. When the ISN_FUNCREF
3577 // instruction is deleted the reference count is decremented and the
3578 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003579 return generate_FUNCREF(cctx, ufunc);
3580 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003581
3582 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 return FAIL;
3584}
3585
3586/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003587 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003589 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 */
3591 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003592compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593{
3594 garray_T *instr = &cctx->ctx_instr;
3595 int count = 0;
3596 dict_T *d = dict_alloc();
3597 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003598 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003599 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003600 int is_const;
3601 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602
3603 if (d == NULL)
3604 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003605 if (generate_ppconst(cctx, ppconst) == FAIL)
3606 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003607 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003609 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610
Bram Moolenaar23c55272020-06-21 16:58:13 +02003611 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003612 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003613 *arg = NULL;
3614 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003615 }
3616
3617 if (**arg == '}')
3618 break;
3619
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003620 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003622 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003624 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003625 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003626 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003629 if (isn->isn_type == ISN_PUSHNR)
3630 {
3631 char buf[NUMBUFLEN];
3632
3633 // Convert to string at compile time.
3634 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3635 isn->isn_type = ISN_PUSHS;
3636 isn->isn_arg.string = vim_strsave((char_u *)buf);
3637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 if (isn->isn_type == ISN_PUSHS)
3639 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003640 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003641 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003642 *arg = skipwhite(*arg);
3643 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003644 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003645 emsg(_(e_missing_matching_bracket_after_dict_key));
3646 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003647 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003648 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003650 else
3651 {
3652 // {"name": value},
3653 // {'name': value},
3654 // {name: value} use "name" as a literal key
3655 key = get_literal_key(arg);
3656 if (key == NULL)
3657 return FAIL;
3658 if (generate_PUSHS(cctx, key) == FAIL)
3659 return FAIL;
3660 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661
3662 // Check for duplicate keys, if using string keys.
3663 if (key != NULL)
3664 {
3665 item = dict_find(d, key, -1);
3666 if (item != NULL)
3667 {
3668 semsg(_(e_duplicate_key), key);
3669 goto failret;
3670 }
3671 item = dictitem_alloc(key);
3672 if (item != NULL)
3673 {
3674 item->di_tv.v_type = VAR_UNKNOWN;
3675 item->di_tv.v_lock = 0;
3676 if (dict_add(d, item) == FAIL)
3677 dictitem_free(item);
3678 }
3679 }
3680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 if (**arg != ':')
3682 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003683 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003684 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003685 else
3686 semsg(_(e_missing_dict_colon), *arg);
3687 return FAIL;
3688 }
3689 whitep = *arg + 1;
3690 if (!IS_WHITE_OR_NUL(*whitep))
3691 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003692 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 return FAIL;
3694 }
3695
Bram Moolenaar23c55272020-06-21 16:58:13 +02003696 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003697 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003698 *arg = NULL;
3699 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003700 }
3701
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003702 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003704 if (!is_const)
3705 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 ++count;
3707
Bram Moolenaar2c330432020-04-13 14:41:35 +02003708 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003709 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003710 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003711 *arg = NULL;
3712 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 if (**arg == '}')
3715 break;
3716 if (**arg != ',')
3717 {
3718 semsg(_(e_missing_dict_comma), *arg);
3719 goto failret;
3720 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003721 if (IS_WHITE_OR_NUL(*whitep))
3722 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003723 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003724 return FAIL;
3725 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003726 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003727 if (!IS_WHITE_OR_NUL(*whitep))
3728 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003729 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003730 return FAIL;
3731 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003732 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 }
3734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 *arg = *arg + 1;
3736
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003737 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003738 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003739 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003740 *arg += STRLEN(*arg);
3741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003743 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 return generate_NEWDICT(cctx, count);
3745
3746failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003747 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003748 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003749 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003750 *arg = (char_u *)"";
3751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 dict_unref(d);
3753 return FAIL;
3754}
3755
3756/*
3757 * Compile "&option".
3758 */
3759 static int
3760compile_get_option(char_u **arg, cctx_T *cctx)
3761{
3762 typval_T rettv;
3763 char_u *start = *arg;
3764 int ret;
3765
3766 // parse the option and get the current value to get the type.
3767 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003768 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 if (ret == OK)
3770 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003771 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003772 char_u *name = vim_strnsave(start, *arg - start);
3773 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3774 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775
3776 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3777 vim_free(name);
3778 }
3779 clear_tv(&rettv);
3780
3781 return ret;
3782}
3783
3784/*
3785 * Compile "$VAR".
3786 */
3787 static int
3788compile_get_env(char_u **arg, cctx_T *cctx)
3789{
3790 char_u *start = *arg;
3791 int len;
3792 int ret;
3793 char_u *name;
3794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 ++*arg;
3796 len = get_env_len(arg);
3797 if (len == 0)
3798 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003799 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 return FAIL;
3801 }
3802
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003803 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 name = vim_strnsave(start, len + 1);
3805 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3806 vim_free(name);
3807 return ret;
3808}
3809
3810/*
3811 * Compile "@r".
3812 */
3813 static int
3814compile_get_register(char_u **arg, cctx_T *cctx)
3815{
3816 int ret;
3817
3818 ++*arg;
3819 if (**arg == NUL)
3820 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003821 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 return FAIL;
3823 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003824 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 {
3826 emsg_invreg(**arg);
3827 return FAIL;
3828 }
3829 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3830 ++*arg;
3831 return ret;
3832}
3833
3834/*
3835 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003836 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 */
3838 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003839apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003841 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842
3843 // this works from end to start
3844 while (p > start)
3845 {
3846 --p;
3847 if (*p == '-' || *p == '+')
3848 {
3849 // only '-' has an effect, for '+' we only check the type
3850#ifdef FEAT_FLOAT
3851 if (rettv->v_type == VAR_FLOAT)
3852 {
3853 if (*p == '-')
3854 rettv->vval.v_float = -rettv->vval.v_float;
3855 }
3856 else
3857#endif
3858 {
3859 varnumber_T val;
3860 int error = FALSE;
3861
3862 // tv_get_number_chk() accepts a string, but we don't want that
3863 // here
3864 if (check_not_string(rettv) == FAIL)
3865 return FAIL;
3866 val = tv_get_number_chk(rettv, &error);
3867 clear_tv(rettv);
3868 if (error)
3869 return FAIL;
3870 if (*p == '-')
3871 val = -val;
3872 rettv->v_type = VAR_NUMBER;
3873 rettv->vval.v_number = val;
3874 }
3875 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003876 else if (numeric_only)
3877 {
3878 ++p;
3879 break;
3880 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003881 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 {
3883 int v = tv2bool(rettv);
3884
3885 // '!' is permissive in the type.
3886 clear_tv(rettv);
3887 rettv->v_type = VAR_BOOL;
3888 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3889 }
3890 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003891 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 return OK;
3893}
3894
3895/*
3896 * Recognize v: variables that are constants and set "rettv".
3897 */
3898 static void
3899get_vim_constant(char_u **arg, typval_T *rettv)
3900{
3901 if (STRNCMP(*arg, "v:true", 6) == 0)
3902 {
3903 rettv->v_type = VAR_BOOL;
3904 rettv->vval.v_number = VVAL_TRUE;
3905 *arg += 6;
3906 }
3907 else if (STRNCMP(*arg, "v:false", 7) == 0)
3908 {
3909 rettv->v_type = VAR_BOOL;
3910 rettv->vval.v_number = VVAL_FALSE;
3911 *arg += 7;
3912 }
3913 else if (STRNCMP(*arg, "v:null", 6) == 0)
3914 {
3915 rettv->v_type = VAR_SPECIAL;
3916 rettv->vval.v_number = VVAL_NULL;
3917 *arg += 6;
3918 }
3919 else if (STRNCMP(*arg, "v:none", 6) == 0)
3920 {
3921 rettv->v_type = VAR_SPECIAL;
3922 rettv->vval.v_number = VVAL_NONE;
3923 *arg += 6;
3924 }
3925}
3926
Bram Moolenaar657137c2021-01-09 15:45:23 +01003927 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003928get_compare_type(char_u *p, int *len, int *type_is)
3929{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003930 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003931 int i;
3932
3933 switch (p[0])
3934 {
3935 case '=': if (p[1] == '=')
3936 type = EXPR_EQUAL;
3937 else if (p[1] == '~')
3938 type = EXPR_MATCH;
3939 break;
3940 case '!': if (p[1] == '=')
3941 type = EXPR_NEQUAL;
3942 else if (p[1] == '~')
3943 type = EXPR_NOMATCH;
3944 break;
3945 case '>': if (p[1] != '=')
3946 {
3947 type = EXPR_GREATER;
3948 *len = 1;
3949 }
3950 else
3951 type = EXPR_GEQUAL;
3952 break;
3953 case '<': if (p[1] != '=')
3954 {
3955 type = EXPR_SMALLER;
3956 *len = 1;
3957 }
3958 else
3959 type = EXPR_SEQUAL;
3960 break;
3961 case 'i': if (p[1] == 's')
3962 {
3963 // "is" and "isnot"; but not a prefix of a name
3964 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3965 *len = 5;
3966 i = p[*len];
3967 if (!isalnum(i) && i != '_')
3968 {
3969 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3970 *type_is = TRUE;
3971 }
3972 }
3973 break;
3974 }
3975 return type;
3976}
3977
3978/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003979 * Skip over an expression, ignoring most errors.
3980 */
3981 static void
3982skip_expr_cctx(char_u **arg, cctx_T *cctx)
3983{
3984 evalarg_T evalarg;
3985
3986 CLEAR_FIELD(evalarg);
3987 evalarg.eval_cctx = cctx;
3988 skip_expr(arg, &evalarg);
3989}
3990
3991/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003993 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 */
3995 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003996compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003998 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999
4000 // this works from end to start
4001 while (p > start)
4002 {
4003 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004004 while (VIM_ISWHITE(*p))
4005 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 if (*p == '-' || *p == '+')
4007 {
4008 int negate = *p == '-';
4009 isn_T *isn;
4010
4011 // TODO: check type
4012 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4013 {
4014 --p;
4015 if (*p == '-')
4016 negate = !negate;
4017 }
4018 // only '-' has an effect, for '+' we only check the type
4019 if (negate)
4020 isn = generate_instr(cctx, ISN_NEGATENR);
4021 else
4022 isn = generate_instr(cctx, ISN_CHECKNR);
4023 if (isn == NULL)
4024 return FAIL;
4025 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004026 else if (numeric_only)
4027 {
4028 ++p;
4029 break;
4030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 else
4032 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004033 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004035 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004037 if (p[-1] == '!')
4038 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004041 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 return FAIL;
4043 }
4044 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004045 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 return OK;
4047}
4048
4049/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004050 * Compile "(expression)": recursive!
4051 * Return FAIL/OK.
4052 */
4053 static int
4054compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4055{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004056 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004057 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004058
Bram Moolenaar24156692021-01-14 20:35:49 +01004059 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4060 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004061 if (ppconst->pp_used <= PPSIZE - 10)
4062 {
4063 ret = compile_expr1(arg, cctx, ppconst);
4064 }
4065 else
4066 {
4067 // Not enough space in ppconst, flush constants.
4068 if (generate_ppconst(cctx, ppconst) == FAIL)
4069 return FAIL;
4070 ret = compile_expr0(arg, cctx);
4071 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004072 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4073 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004074 if (**arg == ')')
4075 ++*arg;
4076 else if (ret == OK)
4077 {
4078 emsg(_(e_missing_close));
4079 ret = FAIL;
4080 }
4081 return ret;
4082}
4083
4084/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004086 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 */
4088 static int
4089compile_subscript(
4090 char_u **arg,
4091 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004092 char_u *start_leader,
4093 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004094 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004096 char_u *name_start = *end_leader;
4097
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 for (;;)
4099 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004100 char_u *p = skipwhite(*arg);
4101
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004102 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004103 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004104 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004105
4106 // If a following line starts with "->{" or "->X" advance to that
4107 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004108 // Also if a following line starts with ".x".
4109 if (next != NULL &&
4110 ((next[0] == '-' && next[1] == '>'
4111 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004112 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004113 {
4114 next = next_line_from_context(cctx, TRUE);
4115 if (next == NULL)
4116 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004117 *arg = next;
4118 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004119 }
4120 }
4121
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004122 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004123 // not a function call.
4124 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004126 garray_T *stack = &cctx->ctx_type_stack;
4127 type_T *type;
4128 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004130 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004131 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004132 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004135 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4136
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004137 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004138 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004140 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 return FAIL;
4142 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004143 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004145 char_u *pstart = p;
4146
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004147 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004148 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004149 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004150
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 // something->method()
4152 // Apply the '!', '-' and '+' first:
4153 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004154 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004157 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004158 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004159 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004160 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004161 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004162 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004163 garray_T *stack = &cctx->ctx_type_stack;
4164 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004165 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004166 int expr_isn_start = cctx->ctx_instr.ga_len;
4167 int expr_isn_end;
4168 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004169
4170 // Funcref call: list->(Refs[2])(arg)
4171 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004172 //
4173 // Fist compile the function expression.
4174 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004175 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004176
4177 // Remember the next instruction index, where the instructions
4178 // for arguments are being written.
4179 expr_isn_end = cctx->ctx_instr.ga_len;
4180
4181 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004182 if (**arg != '(')
4183 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004184 if (*skipwhite(*arg) == '(')
4185 emsg(_(e_nowhitespace));
4186 else
4187 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004188 return FAIL;
4189 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004190 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004191 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004192 return FAIL;
4193
Bram Moolenaar2927c072021-04-05 19:41:21 +02004194 // Move the instructions for the arguments to before the
4195 // instructions of the expression and move the type of the
4196 // expression after the argument types. This is what ISN_PCALL
4197 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004198 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004199 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4200 if (arg_isn_count > 0)
4201 {
4202 int expr_isn_count = expr_isn_end - expr_isn_start;
4203 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4204
4205 if (isn == NULL)
4206 return FAIL;
4207 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4208 + expr_isn_start,
4209 sizeof(isn_T) * expr_isn_count);
4210 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4211 + expr_isn_start,
4212 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4213 sizeof(isn_T) * arg_isn_count);
4214 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4215 + expr_isn_start + arg_isn_count,
4216 isn, sizeof(isn_T) * expr_isn_count);
4217 vim_free(isn);
4218
4219 type = ((type_T **)stack->ga_data)[type_idx_start];
4220 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4221 ((type_T **)stack->ga_data) + type_idx_start + 1,
4222 sizeof(type_T *)
4223 * (stack->ga_len - type_idx_start - 1));
4224 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4225 }
4226
Bram Moolenaar7e368202020-12-25 21:56:57 +01004227 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4228 if (generate_PCALL(cctx, argcount,
4229 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 return FAIL;
4231 }
4232 else
4233 {
4234 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004235 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004236 if (!eval_isnamec1(*p))
4237 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004238 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004239 return FAIL;
4240 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004241 if (ASCII_ISALPHA(*p) && p[1] == ':')
4242 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004243 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 ;
4245 if (*p != '(')
4246 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004247 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 return FAIL;
4249 }
4250 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004251 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 return FAIL;
4253 }
4254 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004255 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004257 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004258
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004259 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004260 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004261 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004262 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004263 // TODO: more arguments
4264 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004265 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004266 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004267 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004268
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004269 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004270 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004271 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004272 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004273 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004274 // missing first index is equal to zero
4275 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004276 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004277 else
4278 {
4279 if (compile_expr0(arg, cctx) == FAIL)
4280 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004281 if (**arg == ':')
4282 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004283 semsg(_(e_white_space_required_before_and_after_str_at_str),
4284 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004285 return FAIL;
4286 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004287 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004288 return FAIL;
4289 *arg = skipwhite(*arg);
4290 }
4291 if (**arg == ':')
4292 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004293 is_slice = TRUE;
4294 ++*arg;
4295 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4296 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004297 semsg(_(e_white_space_required_before_and_after_str_at_str),
4298 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004299 return FAIL;
4300 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004301 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004302 return FAIL;
4303 if (**arg == ']')
4304 // missing second index is equal to end of string
4305 generate_PUSHNR(cctx, -1);
4306 else
4307 {
4308 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004309 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004310 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004311 return FAIL;
4312 *arg = skipwhite(*arg);
4313 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315
4316 if (**arg != ']')
4317 {
4318 emsg(_(e_missbrac));
4319 return FAIL;
4320 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004321 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322
Bram Moolenaare42939a2021-04-05 17:11:17 +02004323 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004324 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004326 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004328 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004329 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004330 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004331 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004332
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004333 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004334 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004335 {
4336 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004337 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004338 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004339 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004340 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 while (eval_isnamec(*p))
4342 MB_PTR_ADV(p);
4343 if (p == *arg)
4344 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004345 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 return FAIL;
4347 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004348 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 return FAIL;
4350 *arg = p;
4351 }
4352 else
4353 break;
4354 }
4355
4356 // TODO - see handle_subscript():
4357 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4358 // Don't do this when "Func" is already a partial that was bound
4359 // explicitly (pt_auto is FALSE).
4360
4361 return OK;
4362}
4363
4364/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004365 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4366 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004368 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004369 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004370 *
4371 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 */
4373
4374/*
4375 * number number constant
4376 * 0zFFFFFFFF Blob constant
4377 * "string" string constant
4378 * 'string' literal string constant
4379 * &option-name option value
4380 * @r register contents
4381 * identifier variable value
4382 * function() function call
4383 * $VAR environment variable
4384 * (expression) nested expression
4385 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004386 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 *
4388 * Also handle:
4389 * ! in front logical NOT
4390 * - in front unary minus
4391 * + in front unary plus (ignored)
4392 * trailing (arg) funcref/partial call
4393 * trailing [] subscript in String or List
4394 * trailing .name entry in Dictionary
4395 * trailing ->name() method call
4396 */
4397 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004398compile_expr7(
4399 char_u **arg,
4400 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004401 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 char_u *start_leader, *end_leader;
4404 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004405 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004406 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004408 ppconst->pp_is_const = FALSE;
4409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 /*
4411 * Skip '!', '-' and '+' characters. They are handled later.
4412 */
4413 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004414 if (eval_leader(arg, TRUE) == FAIL)
4415 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 end_leader = *arg;
4417
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004418 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 switch (**arg)
4420 {
4421 /*
4422 * Number constant.
4423 */
4424 case '0': // also for blob starting with 0z
4425 case '1':
4426 case '2':
4427 case '3':
4428 case '4':
4429 case '5':
4430 case '6':
4431 case '7':
4432 case '8':
4433 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004434 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004436 // Apply "-" and "+" just before the number now, right to
4437 // left. Matters especially when "->" follows. Stops at
4438 // '!'.
4439 if (apply_leader(rettv, TRUE,
4440 start_leader, &end_leader) == FAIL)
4441 {
4442 clear_tv(rettv);
4443 return FAIL;
4444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 break;
4446
4447 /*
4448 * String constant: "string".
4449 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004450 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 return FAIL;
4452 break;
4453
4454 /*
4455 * Literal string constant: 'str''ing'.
4456 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004457 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 return FAIL;
4459 break;
4460
4461 /*
4462 * Constant Vim variable.
4463 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004464 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 ret = NOTDONE;
4466 break;
4467
4468 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004469 * "true" constant
4470 */
4471 case 't': if (STRNCMP(*arg, "true", 4) == 0
4472 && !eval_isnamec((*arg)[4]))
4473 {
4474 *arg += 4;
4475 rettv->v_type = VAR_BOOL;
4476 rettv->vval.v_number = VVAL_TRUE;
4477 }
4478 else
4479 ret = NOTDONE;
4480 break;
4481
4482 /*
4483 * "false" constant
4484 */
4485 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4486 && !eval_isnamec((*arg)[5]))
4487 {
4488 *arg += 5;
4489 rettv->v_type = VAR_BOOL;
4490 rettv->vval.v_number = VVAL_FALSE;
4491 }
4492 else
4493 ret = NOTDONE;
4494 break;
4495
4496 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004497 * "null" constant
4498 */
4499 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004500 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004501 {
4502 *arg += 4;
4503 rettv->v_type = VAR_SPECIAL;
4504 rettv->vval.v_number = VVAL_NULL;
4505 }
4506 else
4507 ret = NOTDONE;
4508 break;
4509
4510 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 * List: [expr, expr]
4512 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004513 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4514 return FAIL;
4515 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 break;
4517
4518 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 * Dictionary: {'key': val, 'key': val}
4520 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004521 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4522 return FAIL;
4523 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 break;
4525
4526 /*
4527 * Option value: &name
4528 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004529 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4530 return FAIL;
4531 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 break;
4533
4534 /*
4535 * Environment variable: $VAR.
4536 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004537 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4538 return FAIL;
4539 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 break;
4541
4542 /*
4543 * Register contents: @r.
4544 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004545 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4546 return FAIL;
4547 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 break;
4549 /*
4550 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004551 * lambda: (arg, arg) => expr
4552 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004554 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4555 ret = compile_lambda(arg, cctx);
4556 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004557 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 break;
4559
4560 default: ret = NOTDONE;
4561 break;
4562 }
4563 if (ret == FAIL)
4564 return FAIL;
4565
Bram Moolenaar1c747212020-05-09 18:28:34 +02004566 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004568 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569 clear_tv(rettv);
4570 else
4571 // A constant expression can possibly be handled compile time,
4572 // return the value instead of generating code.
4573 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 }
4575 else if (ret == NOTDONE)
4576 {
4577 char_u *p;
4578 int r;
4579
4580 if (!eval_isnamec1(**arg))
4581 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004582 if (!vim9_bad_comment(*arg))
4583 {
4584 if (ends_excmd(*skipwhite(*arg)))
4585 semsg(_(e_empty_expression_str), *arg);
4586 else
4587 semsg(_(e_name_expected_str), *arg);
4588 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 return FAIL;
4590 }
4591
4592 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004593 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004594 if (p - *arg == (size_t)1 && **arg == '_')
4595 {
4596 emsg(_(e_cannot_use_underscore_here));
4597 return FAIL;
4598 }
4599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004601 {
4602 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004605 {
4606 if (generate_ppconst(cctx, ppconst) == FAIL)
4607 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004608 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 if (r == FAIL)
4611 return FAIL;
4612 }
4613
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004614 // Handle following "[]", ".member", etc.
4615 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004616 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004617 ppconst) == FAIL)
4618 return FAIL;
4619 if (ppconst->pp_used > 0)
4620 {
4621 // apply the '!', '-' and '+' before the constant
4622 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004623 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004624 return FAIL;
4625 return OK;
4626 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004627 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004629 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630}
4631
4632/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004633 * Give the "white on both sides" error, taking the operator from "p[len]".
4634 */
4635 void
4636error_white_both(char_u *op, int len)
4637{
4638 char_u buf[10];
4639
4640 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004641 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004642}
4643
4644/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004645 * <type>expr7: runtime type check / conversion
4646 */
4647 static int
4648compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4649{
4650 type_T *want_type = NULL;
4651
4652 // Recognize <type>
4653 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4654 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004655 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004656 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4657 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004658 return FAIL;
4659
4660 if (**arg != '>')
4661 {
4662 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004663 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004664 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004665 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004666 return FAIL;
4667 }
4668 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004669 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004670 return FAIL;
4671 }
4672
4673 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4674 return FAIL;
4675
4676 if (want_type != NULL)
4677 {
4678 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004679 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004680 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004681
Bram Moolenaard1103582020-08-14 22:44:25 +02004682 generate_ppconst(cctx, ppconst);
4683 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004684 where.wt_index = 0;
4685 where.wt_variable = FALSE;
4686 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004687 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004688 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004689 return FAIL;
4690 }
4691 }
4692
4693 return OK;
4694}
4695
4696/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 * * number multiplication
4698 * / number division
4699 * % number modulo
4700 */
4701 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004702compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703{
4704 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004705 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004706 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004708 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004709 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 return FAIL;
4711
4712 /*
4713 * Repeat computing, until no "*", "/" or "%" is following.
4714 */
4715 for (;;)
4716 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004717 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 if (*op != '*' && *op != '/' && *op != '%')
4719 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004720 if (next != NULL)
4721 {
4722 *arg = next_line_from_context(cctx, TRUE);
4723 op = skipwhite(*arg);
4724 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004725
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004726 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004728 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004729 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004731 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004732 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004734 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004735 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004737
4738 if (ppconst->pp_used == ppconst_used + 2
4739 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4740 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004741 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004742 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4743 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4744 varnumber_T res = 0;
4745 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004747 // both are numbers: compute the result
4748 switch (*op)
4749 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004750 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004751 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004752 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004753 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004754 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004755 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004756 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004757 break;
4758 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004759 if (failed)
4760 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004761 tv1->vval.v_number = res;
4762 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004763 }
4764 else
4765 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004766 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004767 generate_two_op(cctx, op);
4768 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 }
4770
4771 return OK;
4772}
4773
4774/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004775 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 * - number subtraction
4777 * .. string concatenation
4778 */
4779 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004780compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781{
4782 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004783 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004785 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786
4787 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004788 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 return FAIL;
4790
4791 /*
4792 * Repeat computing, until no "+", "-" or ".." is following.
4793 */
4794 for (;;)
4795 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004796 op = may_peek_next_line(cctx, *arg, &next);
4797 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004799 if (op[0] == op[1] && *op != '.' && next)
4800 // Finding "++" or "--" on the next line is a separate command.
4801 // But ".." is concatenation.
4802 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004804 if (next != NULL)
4805 {
4806 *arg = next_line_from_context(cctx, TRUE);
4807 op = skipwhite(*arg);
4808 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004810 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004812 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004813 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 }
4815
Bram Moolenaare0de1712020-12-02 17:36:54 +01004816 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004817 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004818
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004819 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004820 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 return FAIL;
4822
Bram Moolenaara5565e42020-05-09 15:44:01 +02004823 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004824 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004825 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4826 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4827 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4828 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004830 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4831 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004832
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004833 // concat/subtract/add constant numbers
4834 if (*op == '+')
4835 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4836 else if (*op == '-')
4837 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4838 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004839 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004840 // concatenate constant strings
4841 char_u *s1 = tv1->vval.v_string;
4842 char_u *s2 = tv2->vval.v_string;
4843 size_t len1 = STRLEN(s1);
4844
4845 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4846 if (tv1->vval.v_string == NULL)
4847 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004848 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004849 return FAIL;
4850 }
4851 mch_memmove(tv1->vval.v_string, s1, len1);
4852 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004853 vim_free(s1);
4854 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004855 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004856 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 }
4858 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004859 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004860 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004861 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004862 if (*op == '.')
4863 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004864 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4865 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004866 return FAIL;
4867 generate_instr_drop(cctx, ISN_CONCAT, 1);
4868 }
4869 else
4870 generate_two_op(cctx, op);
4871 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 }
4873
4874 return OK;
4875}
4876
4877/*
4878 * expr5a == expr5b
4879 * expr5a =~ expr5b
4880 * expr5a != expr5b
4881 * expr5a !~ expr5b
4882 * expr5a > expr5b
4883 * expr5a >= expr5b
4884 * expr5a < expr5b
4885 * expr5a <= expr5b
4886 * expr5a is expr5b
4887 * expr5a isnot expr5b
4888 *
4889 * Produces instructions:
4890 * EVAL expr5a Push result of "expr5a"
4891 * EVAL expr5b Push result of "expr5b"
4892 * COMPARE one of the compare instructions
4893 */
4894 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004895compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004897 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004899 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004902 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903
4904 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004905 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906 return FAIL;
4907
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004908 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004909 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910
4911 /*
4912 * If there is a comparative operator, use it.
4913 */
4914 if (type != EXPR_UNKNOWN)
4915 {
4916 int ic = FALSE; // Default: do not ignore case
4917
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004918 if (next != NULL)
4919 {
4920 *arg = next_line_from_context(cctx, TRUE);
4921 p = skipwhite(*arg);
4922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923 if (type_is && (p[len] == '?' || p[len] == '#'))
4924 {
4925 semsg(_(e_invexpr2), *arg);
4926 return FAIL;
4927 }
4928 // extra question mark appended: ignore case
4929 if (p[len] == '?')
4930 {
4931 ic = TRUE;
4932 ++len;
4933 }
4934 // extra '#' appended: match case (ignored)
4935 else if (p[len] == '#')
4936 ++len;
4937 // nothing appended: match case
4938
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004939 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004941 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004942 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 }
4944
4945 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004946 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004947 return FAIL;
4948
Bram Moolenaara5565e42020-05-09 15:44:01 +02004949 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 return FAIL;
4951
Bram Moolenaara5565e42020-05-09 15:44:01 +02004952 if (ppconst->pp_used == ppconst_used + 2)
4953 {
4954 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4955 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4956 int ret;
4957
4958 // Both sides are a constant, compute the result now.
4959 // First check for a valid combination of types, this is more
4960 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004961 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004962 ret = FAIL;
4963 else
4964 {
4965 ret = typval_compare(tv1, tv2, type, ic);
4966 tv1->v_type = VAR_BOOL;
4967 tv1->vval.v_number = tv1->vval.v_number
4968 ? VVAL_TRUE : VVAL_FALSE;
4969 clear_tv(tv2);
4970 --ppconst->pp_used;
4971 }
4972 return ret;
4973 }
4974
4975 generate_ppconst(cctx, ppconst);
4976 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 }
4978
4979 return OK;
4980}
4981
Bram Moolenaar7f141552020-05-09 17:35:53 +02004982static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984/*
4985 * Compile || or &&.
4986 */
4987 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004988compile_and_or(
4989 char_u **arg,
4990 cctx_T *cctx,
4991 char *op,
4992 ppconst_T *ppconst,
4993 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004995 char_u *next;
4996 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997 int opchar = *op;
4998
4999 if (p[0] == opchar && p[1] == opchar)
5000 {
5001 garray_T *instr = &cctx->ctx_instr;
5002 garray_T end_ga;
5003
5004 /*
5005 * Repeat until there is no following "||" or "&&"
5006 */
5007 ga_init2(&end_ga, sizeof(int), 10);
5008 while (p[0] == opchar && p[1] == opchar)
5009 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005010 long start_lnum = SOURCING_LNUM;
5011 int start_ctx_lnum = cctx->ctx_lnum;
5012 int save_lnum;
5013
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005014 if (next != NULL)
5015 {
5016 *arg = next_line_from_context(cctx, TRUE);
5017 p = skipwhite(*arg);
5018 }
5019
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005020 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5021 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005022 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005023 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005024 return FAIL;
5025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005027 // TODO: use ppconst if the value is a constant and check
5028 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005029 generate_ppconst(cctx, ppconst);
5030
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005031 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005032 SOURCING_LNUM = start_lnum;
5033 save_lnum = cctx->ctx_lnum;
5034 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005035 if (bool_on_stack(cctx) == FAIL)
5036 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005037 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005038 ga_clear(&end_ga);
5039 return FAIL;
5040 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005041 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043 if (ga_grow(&end_ga, 1) == FAIL)
5044 {
5045 ga_clear(&end_ga);
5046 return FAIL;
5047 }
5048 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5049 ++end_ga.ga_len;
5050 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005051 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052
5053 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005054 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005055 {
5056 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005057 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005058 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005059
Bram Moolenaara5565e42020-05-09 15:44:01 +02005060 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5061 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 {
5063 ga_clear(&end_ga);
5064 return FAIL;
5065 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005066
5067 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005069 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005071 // Every part must evaluate to a bool.
5072 if (bool_on_stack(cctx) == FAIL)
5073 {
5074 ga_clear(&end_ga);
5075 return FAIL;
5076 }
5077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 // Fill in the end label in all jumps.
5079 while (end_ga.ga_len > 0)
5080 {
5081 isn_T *isn;
5082
5083 --end_ga.ga_len;
5084 isn = ((isn_T *)instr->ga_data)
5085 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5086 isn->isn_arg.jump.jump_where = instr->ga_len;
5087 }
5088 ga_clear(&end_ga);
5089 }
5090
5091 return OK;
5092}
5093
5094/*
5095 * expr4a && expr4a && expr4a logical AND
5096 *
5097 * Produces instructions:
5098 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005099 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005100 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005102 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 * EVAL expr4c Push result of "expr4c"
5104 * end:
5105 */
5106 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005107compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005109 int ppconst_used = ppconst->pp_used;
5110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005112 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 return FAIL;
5114
5115 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005116 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117}
5118
5119/*
5120 * expr3a || expr3b || expr3c logical OR
5121 *
5122 * Produces instructions:
5123 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005124 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005125 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005127 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 * EVAL expr3c Push result of "expr3c"
5129 * end:
5130 */
5131 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005132compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005134 int ppconst_used = ppconst->pp_used;
5135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005136 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005137 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 return FAIL;
5139
5140 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005141 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142}
5143
5144/*
5145 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005147 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 * JUMP_IF_FALSE alt jump if false
5149 * EVAL expr1a
5150 * JUMP_ALWAYS end
5151 * alt: EVAL expr1b
5152 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005153 *
5154 * Toplevel expression: expr2 ?? expr1
5155 * Produces instructions:
5156 * EVAL expr2 Push result of "expr2"
5157 * JUMP_AND_KEEP_IF_TRUE end jump if true
5158 * EVAL expr1
5159 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005160 */
5161 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005162compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163{
5164 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005165 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005166 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167
Bram Moolenaar3988f642020-08-27 22:43:03 +02005168 // Ignore all kinds of errors when not producing code.
5169 if (cctx->ctx_skip == SKIP_YES)
5170 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005171 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005172 return OK;
5173 }
5174
Bram Moolenaar61a89812020-05-07 16:58:17 +02005175 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005176 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 return FAIL;
5178
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005179 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 if (*p == '?')
5181 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005182 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183 garray_T *instr = &cctx->ctx_instr;
5184 garray_T *stack = &cctx->ctx_type_stack;
5185 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005186 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005187 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005188 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005189 int has_const_expr = FALSE;
5190 int const_value = FALSE;
5191 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005193 if (next != NULL)
5194 {
5195 *arg = next_line_from_context(cctx, TRUE);
5196 p = skipwhite(*arg);
5197 }
5198
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005199 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005200 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005201 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005202 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005203 return FAIL;
5204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205
Bram Moolenaara5565e42020-05-09 15:44:01 +02005206 if (ppconst->pp_used == ppconst_used + 1)
5207 {
5208 // the condition is a constant, we know whether the ? or the :
5209 // expression is to be evaluated.
5210 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005211 if (op_falsy)
5212 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5213 else
5214 {
5215 int error = FALSE;
5216
5217 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5218 &error);
5219 if (error)
5220 return FAIL;
5221 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005222 cctx->ctx_skip = save_skip == SKIP_YES ||
5223 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5224
5225 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5226 // "left ?? right" and "left" is truthy: produce "left"
5227 generate_ppconst(cctx, ppconst);
5228 else
5229 {
5230 clear_tv(&ppconst->pp_tv[ppconst_used]);
5231 --ppconst->pp_used;
5232 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005233 }
5234 else
5235 {
5236 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005237 if (op_falsy)
5238 end_idx = instr->ga_len;
5239 generate_JUMP(cctx, op_falsy
5240 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5241 if (op_falsy)
5242 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005244
5245 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005246 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005247 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005248 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005249 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005250
Bram Moolenaara5565e42020-05-09 15:44:01 +02005251 if (!has_const_expr)
5252 {
5253 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005255 if (!op_falsy)
5256 {
5257 // remember the type and drop it
5258 --stack->ga_len;
5259 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005260
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005261 end_idx = instr->ga_len;
5262 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005263
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005264 // jump here from JUMP_IF_FALSE
5265 isn = ((isn_T *)instr->ga_data) + alt_idx;
5266 isn->isn_arg.jump.jump_where = instr->ga_len;
5267 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005270 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005272 // Check for the ":".
5273 p = may_peek_next_line(cctx, *arg, &next);
5274 if (*p != ':')
5275 {
5276 emsg(_(e_missing_colon));
5277 return FAIL;
5278 }
5279 if (next != NULL)
5280 {
5281 *arg = next_line_from_context(cctx, TRUE);
5282 p = skipwhite(*arg);
5283 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005284
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005285 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5286 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005287 semsg(_(e_white_space_required_before_and_after_str_at_str),
5288 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005289 return FAIL;
5290 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005292 // evaluate the third expression
5293 if (has_const_expr)
5294 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005295 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005296 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005297 return FAIL;
5298 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5299 return FAIL;
5300 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301
Bram Moolenaara5565e42020-05-09 15:44:01 +02005302 if (!has_const_expr)
5303 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005304 type_T **typep;
5305
Bram Moolenaara5565e42020-05-09 15:44:01 +02005306 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005307
Bram Moolenaara5565e42020-05-09 15:44:01 +02005308 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005309 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5310 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005311
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005312 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005313 isn = ((isn_T *)instr->ga_data) + end_idx;
5314 isn->isn_arg.jump.jump_where = instr->ga_len;
5315 }
5316
5317 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 }
5319 return OK;
5320}
5321
5322/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005323 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005324 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5325 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005326 */
5327 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005328compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005329{
5330 ppconst_T ppconst;
5331
5332 CLEAR_FIELD(ppconst);
5333 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5334 {
5335 clear_ppconst(&ppconst);
5336 return FAIL;
5337 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005338 if (is_const != NULL)
5339 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005340 if (generate_ppconst(cctx, &ppconst) == FAIL)
5341 return FAIL;
5342 return OK;
5343}
5344
5345/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005346 * Toplevel expression.
5347 */
5348 static int
5349compile_expr0(char_u **arg, cctx_T *cctx)
5350{
5351 return compile_expr0_ext(arg, cctx, NULL);
5352}
5353
5354/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005355 * Compile "return [expr]".
5356 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357 */
5358 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005359compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360{
5361 char_u *p = arg;
5362 garray_T *stack = &cctx->ctx_type_stack;
5363 type_T *stack_type;
5364
5365 if (*p != NUL && *p != '|' && *p != '\n')
5366 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005367 if (legacy)
5368 {
5369 int save_flags = cmdmod.cmod_flags;
5370
5371 generate_LEGACY_EVAL(cctx, p);
5372 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5373 0, cctx, FALSE, FALSE) == FAIL)
5374 return NULL;
5375 cmdmod.cmod_flags |= CMOD_LEGACY;
5376 (void)skip_expr(&p, NULL);
5377 cmdmod.cmod_flags = save_flags;
5378 }
5379 else
5380 {
5381 // compile return argument into instructions
5382 if (compile_expr0(&p, cctx) == FAIL)
5383 return NULL;
5384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005385
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005386 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005387 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005388 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005389 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005390 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5391 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005392 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005393 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005394 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005395 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005396 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005397 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5398 && stack_type->tt_type != VAR_VOID
5399 && stack_type->tt_type != VAR_UNKNOWN)
5400 {
5401 emsg(_(e_returning_value_in_function_without_return_type));
5402 return NULL;
5403 }
5404 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005405 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005406 return NULL;
5407 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005408 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409 }
5410 else
5411 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005412 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005413 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005414 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5415 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005416 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005417 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418 return NULL;
5419 }
5420
5421 // No argument, return zero.
5422 generate_PUSHNR(cctx, 0);
5423 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005424
5425 // Undo any command modifiers.
5426 generate_undo_cmdmods(cctx);
5427
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005428 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429 return NULL;
5430
5431 // "return val | endif" is possible
5432 return skipwhite(p);
5433}
5434
5435/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005436 * Get a line from the compilation context, compatible with exarg_T getline().
5437 * Return a pointer to the line in allocated memory.
5438 * Return NULL for end-of-file or some error.
5439 */
5440 static char_u *
5441exarg_getline(
5442 int c UNUSED,
5443 void *cookie,
5444 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005445 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005446{
5447 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005448 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005449
Bram Moolenaar66250c92020-08-20 15:02:42 +02005450 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005451 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005452 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005453 return NULL;
5454 ++cctx->ctx_lnum;
5455 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5456 // Comment lines result in NULL pointers, skip them.
5457 if (p != NULL)
5458 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005459 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005460}
5461
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005462 void
5463fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5464{
5465 eap->getline = exarg_getline;
5466 eap->cookie = cctx;
5467}
5468
Bram Moolenaar04b12692020-05-04 23:24:44 +02005469/*
5470 * Compile a nested :def command.
5471 */
5472 static char_u *
5473compile_nested_function(exarg_T *eap, cctx_T *cctx)
5474{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005475 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005476 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005477 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005478 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005479 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005480 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005481
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005482 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005483 {
5484 emsg(_(e_cannot_use_bang_with_nested_def));
5485 return NULL;
5486 }
5487
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005488 if (*name_start == '/')
5489 {
5490 name_end = skip_regexp(name_start + 1, '/', TRUE);
5491 if (*name_end == '/')
5492 ++name_end;
5493 eap->nextcmd = check_nextcmd(name_end);
5494 }
5495 if (name_end == name_start || *skipwhite(name_end) != '(')
5496 {
5497 if (!ends_excmd2(name_start, name_end))
5498 {
5499 semsg(_(e_invalid_command_str), eap->cmd);
5500 return NULL;
5501 }
5502
5503 // "def" or "def Name": list functions
5504 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5505 return NULL;
5506 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5507 }
5508
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005509 // Only g:Func() can use a namespace.
5510 if (name_start[1] == ':' && !is_global)
5511 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005512 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005513 return NULL;
5514 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005515 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005516 return NULL;
5517
Bram Moolenaar04b12692020-05-04 23:24:44 +02005518 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005519 fill_exarg_from_cctx(eap, cctx);
5520
Bram Moolenaar04b12692020-05-04 23:24:44 +02005521 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005522 lambda_name = vim_strsave(get_lambda_name());
5523 if (lambda_name == NULL)
5524 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005525 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005526
Bram Moolenaar822ba242020-05-24 23:00:18 +02005527 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005528 {
5529 r = eap->skip ? OK : FAIL;
5530 goto theend;
5531 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005532
5533 // copy over the block scope IDs before compiling
5534 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5535 {
5536 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5537
5538 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5539 if (ufunc->uf_block_ids != NULL)
5540 {
5541 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5542 sizeof(int) * block_depth);
5543 ufunc->uf_block_depth = block_depth;
5544 }
5545 }
5546
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005547 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5548 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005549 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005550 {
5551 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005552 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005553 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005554
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005555 if (is_global)
5556 {
5557 char_u *func_name = vim_strnsave(name_start + 2,
5558 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005559
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005560 if (func_name == NULL)
5561 r = FAIL;
5562 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005563 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005564 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005565 lambda_name = NULL;
5566 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005567 }
5568 else
5569 {
5570 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005571 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005572 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005573
Bram Moolenaareef21022020-08-01 22:16:43 +02005574 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005575 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005576 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005577 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005578 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5579 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005580 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005581
5582theend:
5583 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005584 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005585}
5586
5587/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588 * Return the length of an assignment operator, or zero if there isn't one.
5589 */
5590 int
5591assignment_len(char_u *p, int *heredoc)
5592{
5593 if (*p == '=')
5594 {
5595 if (p[1] == '<' && p[2] == '<')
5596 {
5597 *heredoc = TRUE;
5598 return 3;
5599 }
5600 return 1;
5601 }
5602 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5603 return 2;
5604 if (STRNCMP(p, "..=", 3) == 0)
5605 return 3;
5606 return 0;
5607}
5608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005610 * Generate the load instruction for "name".
5611 */
5612 static void
5613generate_loadvar(
5614 cctx_T *cctx,
5615 assign_dest_T dest,
5616 char_u *name,
5617 lvar_T *lvar,
5618 type_T *type)
5619{
5620 switch (dest)
5621 {
5622 case dest_option:
5623 // TODO: check the option exists
5624 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5625 break;
5626 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005627 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5628 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5629 else
5630 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005631 break;
5632 case dest_buffer:
5633 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5634 break;
5635 case dest_window:
5636 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5637 break;
5638 case dest_tab:
5639 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5640 break;
5641 case dest_script:
5642 compile_load_scriptvar(cctx,
5643 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5644 break;
5645 case dest_env:
5646 // Include $ in the name here
5647 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5648 break;
5649 case dest_reg:
5650 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5651 break;
5652 case dest_vimvar:
5653 generate_LOADV(cctx, name + 2, TRUE);
5654 break;
5655 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005656 if (lvar->lv_from_outer > 0)
5657 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5658 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005659 else
5660 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5661 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005662 case dest_expr:
5663 // list or dict value should already be on the stack.
5664 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005665 }
5666}
5667
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005668/*
5669 * Skip over "[expr]" or ".member".
5670 * Does not check for any errors.
5671 */
5672 static char_u *
5673skip_index(char_u *start)
5674{
5675 char_u *p = start;
5676
5677 if (*p == '[')
5678 {
5679 p = skipwhite(p + 1);
5680 (void)skip_expr(&p, NULL);
5681 p = skipwhite(p);
5682 if (*p == ']')
5683 return p + 1;
5684 return p;
5685 }
5686 // if (*p == '.')
5687 return to_name_end(p + 1, TRUE);
5688}
5689
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005690 void
5691vim9_declare_error(char_u *name)
5692{
5693 char *scope = "";
5694
5695 switch (*name)
5696 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005697 case 'g': scope = _("global"); break;
5698 case 'b': scope = _("buffer"); break;
5699 case 'w': scope = _("window"); break;
5700 case 't': scope = _("tab"); break;
5701 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005702 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005703 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005704 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005705 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005706 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005707 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005708 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005709 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005710 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005711}
5712
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005713/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005714 * For one assignment figure out the type of destination. Return it in "dest".
5715 * When not recognized "dest" is not set.
5716 * For an option "opt_flags" is set.
5717 * For a v:var "vimvaridx" is set.
5718 * "type" is set to the destination type if known, unchanted otherwise.
5719 * Return FAIL if an error message was given.
5720 */
5721 static int
5722get_var_dest(
5723 char_u *name,
5724 assign_dest_T *dest,
5725 int cmdidx,
5726 int *opt_flags,
5727 int *vimvaridx,
5728 type_T **type,
5729 cctx_T *cctx)
5730{
5731 char_u *p;
5732
5733 if (*name == '&')
5734 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005735 int cc;
5736 long numval;
5737 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005738
5739 *dest = dest_option;
5740 if (cmdidx == CMD_final || cmdidx == CMD_const)
5741 {
5742 emsg(_(e_const_option));
5743 return FAIL;
5744 }
5745 p = name;
5746 p = find_option_end(&p, opt_flags);
5747 if (p == NULL)
5748 {
5749 // cannot happen?
5750 emsg(_(e_letunexp));
5751 return FAIL;
5752 }
5753 cc = *p;
5754 *p = NUL;
5755 opt_type = get_option_value(skip_option_env_lead(name),
5756 &numval, NULL, *opt_flags);
5757 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005758 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005759 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005760 case gov_unknown:
5761 semsg(_(e_unknown_option), name);
5762 return FAIL;
5763 case gov_string:
5764 case gov_hidden_string:
5765 *type = &t_string;
5766 break;
5767 case gov_bool:
5768 case gov_hidden_bool:
5769 *type = &t_bool;
5770 break;
5771 case gov_number:
5772 case gov_hidden_number:
5773 *type = &t_number;
5774 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005775 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005776 }
5777 else if (*name == '$')
5778 {
5779 *dest = dest_env;
5780 *type = &t_string;
5781 }
5782 else if (*name == '@')
5783 {
5784 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5785 {
5786 emsg_invreg(name[1]);
5787 return FAIL;
5788 }
5789 *dest = dest_reg;
5790 *type = &t_string;
5791 }
5792 else if (STRNCMP(name, "g:", 2) == 0)
5793 {
5794 *dest = dest_global;
5795 }
5796 else if (STRNCMP(name, "b:", 2) == 0)
5797 {
5798 *dest = dest_buffer;
5799 }
5800 else if (STRNCMP(name, "w:", 2) == 0)
5801 {
5802 *dest = dest_window;
5803 }
5804 else if (STRNCMP(name, "t:", 2) == 0)
5805 {
5806 *dest = dest_tab;
5807 }
5808 else if (STRNCMP(name, "v:", 2) == 0)
5809 {
5810 typval_T *vtv;
5811 int di_flags;
5812
5813 *vimvaridx = find_vim_var(name + 2, &di_flags);
5814 if (*vimvaridx < 0)
5815 {
5816 semsg(_(e_variable_not_found_str), name);
5817 return FAIL;
5818 }
5819 // We use the current value of "sandbox" here, is that OK?
5820 if (var_check_ro(di_flags, name, FALSE))
5821 return FAIL;
5822 *dest = dest_vimvar;
5823 vtv = get_vim_var_tv(*vimvaridx);
5824 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5825 }
5826 return OK;
5827}
5828
5829/*
5830 * Generate a STORE instruction for "dest", not being "dest_local".
5831 * Return FAIL when out of memory.
5832 */
5833 static int
5834generate_store_var(
5835 cctx_T *cctx,
5836 assign_dest_T dest,
5837 int opt_flags,
5838 int vimvaridx,
5839 int scriptvar_idx,
5840 int scriptvar_sid,
5841 type_T *type,
5842 char_u *name)
5843{
5844 switch (dest)
5845 {
5846 case dest_option:
5847 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5848 opt_flags);
5849 case dest_global:
5850 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005851 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5852 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005853 case dest_buffer:
5854 // include b: with the name, easier to execute that way
5855 return generate_STORE(cctx, ISN_STOREB, 0, name);
5856 case dest_window:
5857 // include w: with the name, easier to execute that way
5858 return generate_STORE(cctx, ISN_STOREW, 0, name);
5859 case dest_tab:
5860 // include t: with the name, easier to execute that way
5861 return generate_STORE(cctx, ISN_STORET, 0, name);
5862 case dest_env:
5863 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5864 case dest_reg:
5865 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5866 case dest_vimvar:
5867 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5868 case dest_script:
5869 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005870 // "s:" may be included in the name.
5871 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005872 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005873 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5874 scriptvar_sid, scriptvar_idx, type);
5875 case dest_local:
5876 case dest_expr:
5877 // cannot happen
5878 break;
5879 }
5880 return FAIL;
5881}
5882
Bram Moolenaar752fc692021-01-04 21:57:11 +01005883 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005884generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5885{
5886 if (lhs->lhs_dest != dest_local)
5887 return generate_store_var(cctx, lhs->lhs_dest,
5888 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5889 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5890 lhs->lhs_type, lhs->lhs_name);
5891
5892 if (lhs->lhs_lvar != NULL)
5893 {
5894 garray_T *instr = &cctx->ctx_instr;
5895 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5896
5897 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5898 // ISN_STORENR
5899 if (lhs->lhs_lvar->lv_from_outer == 0
5900 && instr->ga_len == instr_count + 1
5901 && isn->isn_type == ISN_PUSHNR)
5902 {
5903 varnumber_T val = isn->isn_arg.number;
5904 garray_T *stack = &cctx->ctx_type_stack;
5905
5906 isn->isn_type = ISN_STORENR;
5907 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5908 isn->isn_arg.storenr.stnr_val = val;
5909 if (stack->ga_len > 0)
5910 --stack->ga_len;
5911 }
5912 else if (lhs->lhs_lvar->lv_from_outer > 0)
5913 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5914 lhs->lhs_lvar->lv_from_outer);
5915 else
5916 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5917 }
5918 return OK;
5919}
5920
5921 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005922is_decl_command(int cmdidx)
5923{
5924 return cmdidx == CMD_let || cmdidx == CMD_var
5925 || cmdidx == CMD_final || cmdidx == CMD_const;
5926}
5927
5928/*
5929 * Figure out the LHS type and other properties for an assignment or one item
5930 * of ":unlet" with an index.
5931 * Returns OK or FAIL.
5932 */
5933 static int
5934compile_lhs(
5935 char_u *var_start,
5936 lhs_T *lhs,
5937 int cmdidx,
5938 int heredoc,
5939 int oplen,
5940 cctx_T *cctx)
5941{
5942 char_u *var_end;
5943 int is_decl = is_decl_command(cmdidx);
5944
5945 CLEAR_POINTER(lhs);
5946 lhs->lhs_dest = dest_local;
5947 lhs->lhs_vimvaridx = -1;
5948 lhs->lhs_scriptvar_idx = -1;
5949
5950 // "dest_end" is the end of the destination, including "[expr]" or
5951 // ".name".
5952 // "var_end" is the end of the variable/option/etc. name.
5953 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5954 if (*var_start == '@')
5955 var_end = var_start + 2;
5956 else
5957 {
5958 // skip over the leading "&", "&l:", "&g:" and "$"
5959 var_end = skip_option_env_lead(var_start);
5960 var_end = to_name_end(var_end, TRUE);
5961 }
5962
5963 // "a: type" is declaring variable "a" with a type, not dict "a:".
5964 if (is_decl && lhs->lhs_dest_end == var_start + 2
5965 && lhs->lhs_dest_end[-1] == ':')
5966 --lhs->lhs_dest_end;
5967 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5968 --var_end;
5969
5970 // compute the length of the destination without "[expr]" or ".name"
5971 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02005972 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005973 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5974 if (lhs->lhs_name == NULL)
5975 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005976
5977 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5978 // Something follows after the variable: "var[idx]" or "var.key".
5979 lhs->lhs_has_index = TRUE;
5980
Bram Moolenaar752fc692021-01-04 21:57:11 +01005981 if (heredoc)
5982 lhs->lhs_type = &t_list_string;
5983 else
5984 lhs->lhs_type = &t_any;
5985
5986 if (cctx->ctx_skip != SKIP_YES)
5987 {
5988 int declare_error = FALSE;
5989
5990 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5991 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5992 &lhs->lhs_type, cctx) == FAIL)
5993 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005994 if (lhs->lhs_dest != dest_local
5995 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005996 {
5997 // Specific kind of variable recognized.
5998 declare_error = is_decl;
5999 }
6000 else
6001 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006002 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006003 if (check_reserved_name(lhs->lhs_name) == FAIL)
6004 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006005
6006 if (lookup_local(var_start, lhs->lhs_varlen,
6007 &lhs->lhs_local_lvar, cctx) == OK)
6008 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6009 else
6010 {
6011 CLEAR_FIELD(lhs->lhs_arg_lvar);
6012 if (arg_exists(var_start, lhs->lhs_varlen,
6013 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6014 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6015 {
6016 if (is_decl)
6017 {
6018 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6019 return FAIL;
6020 }
6021 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6022 }
6023 }
6024 if (lhs->lhs_lvar != NULL)
6025 {
6026 if (is_decl)
6027 {
6028 semsg(_(e_variable_already_declared), lhs->lhs_name);
6029 return FAIL;
6030 }
6031 }
6032 else
6033 {
6034 int script_namespace = lhs->lhs_varlen > 1
6035 && STRNCMP(var_start, "s:", 2) == 0;
6036 int script_var = (script_namespace
6037 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006038 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006039 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006040 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006041 imported_T *import =
6042 find_imported(var_start, lhs->lhs_varlen, cctx);
6043
6044 if (script_namespace || script_var || import != NULL)
6045 {
6046 char_u *rawname = lhs->lhs_name
6047 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6048
6049 if (is_decl)
6050 {
6051 if (script_namespace)
6052 semsg(_(e_cannot_declare_script_variable_in_function),
6053 lhs->lhs_name);
6054 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006055 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006056 lhs->lhs_name);
6057 return FAIL;
6058 }
6059 else if (cctx->ctx_ufunc->uf_script_ctx_version
6060 == SCRIPT_VERSION_VIM9
6061 && script_namespace
6062 && !script_var && import == NULL)
6063 {
6064 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6065 return FAIL;
6066 }
6067
6068 lhs->lhs_dest = dest_script;
6069
6070 // existing script-local variables should have a type
6071 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6072 if (import != NULL)
6073 lhs->lhs_scriptvar_sid = import->imp_sid;
6074 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6075 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006076 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006077 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006078 lhs->lhs_scriptvar_sid, rawname,
6079 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6080 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006081 if (lhs->lhs_scriptvar_idx >= 0)
6082 {
6083 scriptitem_T *si = SCRIPT_ITEM(
6084 lhs->lhs_scriptvar_sid);
6085 svar_T *sv =
6086 ((svar_T *)si->sn_var_vals.ga_data)
6087 + lhs->lhs_scriptvar_idx;
6088 lhs->lhs_type = sv->sv_type;
6089 }
6090 }
6091 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006092 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006093 == FAIL)
6094 return FAIL;
6095 }
6096 }
6097
6098 if (declare_error)
6099 {
6100 vim9_declare_error(lhs->lhs_name);
6101 return FAIL;
6102 }
6103 }
6104
6105 // handle "a:name" as a name, not index "name" on "a"
6106 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6107 var_end = lhs->lhs_dest_end;
6108
6109 if (lhs->lhs_dest != dest_option)
6110 {
6111 if (is_decl && *var_end == ':')
6112 {
6113 char_u *p;
6114
6115 // parse optional type: "let var: type = expr"
6116 if (!VIM_ISWHITE(var_end[1]))
6117 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006118 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006119 return FAIL;
6120 }
6121 p = skipwhite(var_end + 1);
6122 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6123 if (lhs->lhs_type == NULL)
6124 return FAIL;
6125 lhs->lhs_has_type = TRUE;
6126 }
6127 else if (lhs->lhs_lvar != NULL)
6128 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6129 }
6130
Bram Moolenaare42939a2021-04-05 17:11:17 +02006131 if (oplen == 3 && !heredoc
6132 && lhs->lhs_dest != dest_global
6133 && !lhs->lhs_has_index
6134 && lhs->lhs_type->tt_type != VAR_STRING
6135 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006136 {
6137 emsg(_(e_can_only_concatenate_to_string));
6138 return FAIL;
6139 }
6140
6141 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6142 && cctx->ctx_skip != SKIP_YES)
6143 {
6144 if (oplen > 1 && !heredoc)
6145 {
6146 // +=, /=, etc. require an existing variable
6147 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6148 return FAIL;
6149 }
6150 if (!is_decl)
6151 {
6152 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6153 return FAIL;
6154 }
6155
Bram Moolenaar3f327882021-03-17 20:56:38 +01006156 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006157 if ((lhs->lhs_type->tt_type == VAR_FUNC
6158 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006159 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006160 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006161
6162 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006163 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6164 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6165 if (lhs->lhs_lvar == NULL)
6166 return FAIL;
6167 lhs->lhs_new_local = TRUE;
6168 }
6169
6170 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006171 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006172 {
6173 // Something follows after the variable: "var[idx]" or "var.key".
6174 // TODO: should we also handle "->func()" here?
6175 if (is_decl)
6176 {
6177 emsg(_(e_cannot_use_index_when_declaring_variable));
6178 return FAIL;
6179 }
6180
6181 if (var_start[lhs->lhs_varlen] == '['
6182 || var_start[lhs->lhs_varlen] == '.')
6183 {
6184 char_u *after = var_start + lhs->lhs_varlen;
6185 char_u *p;
6186
6187 // Only the last index is used below, if there are others
6188 // before it generate code for the expression. Thus for
6189 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6190 for (;;)
6191 {
6192 p = skip_index(after);
6193 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006194 {
6195 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006196 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006197 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006198 after = p;
6199 }
6200 if (after > var_start + lhs->lhs_varlen)
6201 {
6202 lhs->lhs_varlen = after - var_start;
6203 lhs->lhs_dest = dest_expr;
6204 // We don't know the type before evaluating the expression,
6205 // use "any" until then.
6206 lhs->lhs_type = &t_any;
6207 }
6208
Bram Moolenaar752fc692021-01-04 21:57:11 +01006209 if (lhs->lhs_type->tt_member == NULL)
6210 lhs->lhs_member_type = &t_any;
6211 else
6212 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6213 }
6214 else
6215 {
6216 semsg("Not supported yet: %s", var_start);
6217 return FAIL;
6218 }
6219 }
6220 return OK;
6221}
6222
6223/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006224 * Figure out the LHS and check a few errors.
6225 */
6226 static int
6227compile_assign_lhs(
6228 char_u *var_start,
6229 lhs_T *lhs,
6230 int cmdidx,
6231 int is_decl,
6232 int heredoc,
6233 int oplen,
6234 cctx_T *cctx)
6235{
6236 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6237 return FAIL;
6238
6239 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6240 {
6241 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6242 return FAIL;
6243 }
6244 if (!is_decl && lhs->lhs_lvar != NULL
6245 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6246 {
6247 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6248 return FAIL;
6249 }
6250 return OK;
6251}
6252
6253/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006254 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6255 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006256 */
6257 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006258compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006259 char_u *var_start,
6260 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006261 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006262 cctx_T *cctx)
6263{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006264 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006265 char_u *p;
6266 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006267 int need_white_before = TRUE;
6268 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006269
Bram Moolenaar752fc692021-01-04 21:57:11 +01006270 p = var_start + varlen;
6271 if (*p == '[')
6272 {
6273 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006274 if (*p == ':')
6275 {
6276 // empty first index, push zero
6277 r = generate_PUSHNR(cctx, 0);
6278 need_white_before = FALSE;
6279 }
6280 else
6281 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006282
6283 if (r == OK && *skipwhite(p) == ':')
6284 {
6285 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006286 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006287 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006288 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006289 empty_second = *skipwhite(p + 1) == ']';
6290 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6291 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006292 {
6293 semsg(_(e_white_space_required_before_and_after_str_at_str),
6294 ":", p);
6295 return FAIL;
6296 }
6297 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006298 if (*p == ']')
6299 // empty second index, push "none"
6300 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6301 else
6302 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006303 }
6304
Bram Moolenaar752fc692021-01-04 21:57:11 +01006305 if (r == OK && *skipwhite(p) != ']')
6306 {
6307 // this should not happen
6308 emsg(_(e_missbrac));
6309 r = FAIL;
6310 }
6311 }
6312 else // if (*p == '.')
6313 {
6314 char_u *key_end = to_name_end(p + 1, TRUE);
6315 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6316
6317 r = generate_PUSHS(cctx, key);
6318 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006319 return r;
6320}
6321
6322/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006323 * For a LHS with an index, load the variable to be indexed.
6324 */
6325 static int
6326compile_load_lhs(
6327 lhs_T *lhs,
6328 char_u *var_start,
6329 type_T *rhs_type,
6330 cctx_T *cctx)
6331{
6332 if (lhs->lhs_dest == dest_expr)
6333 {
6334 size_t varlen = lhs->lhs_varlen;
6335 int c = var_start[varlen];
6336 char_u *p = var_start;
6337 garray_T *stack = &cctx->ctx_type_stack;
6338
6339 // Evaluate "ll[expr]" of "ll[expr][idx]"
6340 var_start[varlen] = NUL;
6341 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6342 {
6343 // this should not happen
6344 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006345 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006346 return FAIL;
6347 }
6348 var_start[varlen] = c;
6349
6350 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6351 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6352 // now we can properly check the type
6353 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6354 && rhs_type != &t_void
6355 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6356 FALSE, FALSE) == FAIL)
6357 return FAIL;
6358 }
6359 else
6360 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6361 lhs->lhs_lvar, lhs->lhs_type);
6362 return OK;
6363}
6364
6365/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006366 * Produce code for loading "lhs" and also take care of an index.
6367 * Return OK/FAIL.
6368 */
6369 static int
6370compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6371{
6372 compile_load_lhs(lhs, var_start, NULL, cctx);
6373
6374 if (lhs->lhs_has_index)
6375 {
6376 int range = FALSE;
6377
6378 // Get member from list or dict. First compile the
6379 // index value.
6380 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6381 return FAIL;
6382 if (range)
6383 {
6384 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6385 var_start);
6386 return FAIL;
6387 }
6388
6389 // Get the member.
6390 if (compile_member(FALSE, cctx) == FAIL)
6391 return FAIL;
6392 }
6393 return OK;
6394}
6395
6396/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006397 * Assignment to a list or dict member, or ":unlet" for the item, using the
6398 * information in "lhs".
6399 * Returns OK or FAIL.
6400 */
6401 static int
6402compile_assign_unlet(
6403 char_u *var_start,
6404 lhs_T *lhs,
6405 int is_assign,
6406 type_T *rhs_type,
6407 cctx_T *cctx)
6408{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006409 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006410 garray_T *stack = &cctx->ctx_type_stack;
6411 int range = FALSE;
6412
Bram Moolenaar68452172021-04-12 21:21:02 +02006413 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006414 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006415 if (is_assign && range && lhs->lhs_type != &t_blob
6416 && lhs->lhs_type != &t_any)
6417 {
6418 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6419 return FAIL;
6420 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006421
6422 if (lhs->lhs_type == &t_any)
6423 {
6424 // Index on variable of unknown type: check at runtime.
6425 dest_type = VAR_ANY;
6426 }
6427 else
6428 {
6429 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006430 if (dest_type == VAR_DICT && range)
6431 {
6432 emsg(e_cannot_use_range_with_dictionary);
6433 return FAIL;
6434 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006435 if (dest_type == VAR_DICT
6436 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006437 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006438 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006439 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006440 type_T *type;
6441
6442 if (range)
6443 {
6444 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6445 if (need_type(type, &t_number,
6446 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006447 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006448 }
6449 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6450 if ((dest_type != VAR_BLOB || type != &t_special)
6451 && need_type(type, &t_number,
6452 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006453 return FAIL;
6454 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006455 }
6456
6457 // Load the dict or list. On the stack we then have:
6458 // - value (for assignment, not for :unlet)
6459 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006460 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006461 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006462 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6463 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006464
Bram Moolenaar68452172021-04-12 21:21:02 +02006465 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6466 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006467 {
6468 if (is_assign)
6469 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006470 if (range)
6471 {
6472 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6473 return FAIL;
6474 }
6475 else
6476 {
6477 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006478
Bram Moolenaar68452172021-04-12 21:21:02 +02006479 if (isn == NULL)
6480 return FAIL;
6481 isn->isn_arg.vartype = dest_type;
6482 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006483 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006484 else if (range)
6485 {
6486 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6487 return FAIL;
6488 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006489 else
6490 {
6491 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6492 return FAIL;
6493 }
6494 }
6495 else
6496 {
6497 emsg(_(e_indexable_type_required));
6498 return FAIL;
6499 }
6500
6501 return OK;
6502}
6503
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006504/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006505 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006506 * "let name"
6507 * "var name = expr"
6508 * "final name = expr"
6509 * "const name = expr"
6510 * "name = expr"
6511 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006512 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006513 * Return NULL for an error.
6514 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006515 */
6516 static char_u *
6517compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6518{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006519 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006521 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 char_u *ret = NULL;
6523 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006524 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006526 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006527 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006529 int oplen = 0;
6530 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006531 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006532 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006534 int is_decl = is_decl_command(cmdidx);
6535 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006536 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006538 // Skip over the "var" or "[var, var]" to get to any "=".
6539 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6540 if (p == NULL)
6541 return *arg == '[' ? arg : NULL;
6542
6543 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006545 // TODO: should we allow this, and figure out type inference from list
6546 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006547 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006548 return NULL;
6549 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006550 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552 sp = p;
6553 p = skipwhite(p);
6554 op = p;
6555 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006556
6557 if (var_count > 0 && oplen == 0)
6558 // can be something like "[1, 2]->func()"
6559 return arg;
6560
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006561 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006563 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006564 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006565 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006566 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6567 {
6568 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6569 oplen = 2;
6570 incdec = TRUE;
6571 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 if (heredoc)
6574 {
6575 list_T *l;
6576 listitem_T *li;
6577
6578 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006579 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006581 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006582 if (l == NULL)
6583 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006584
Bram Moolenaar078269b2020-09-21 20:35:55 +02006585 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006587 // Push each line and the create the list.
6588 FOR_ALL_LIST_ITEMS(l, li)
6589 {
6590 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6591 li->li_tv.vval.v_string = NULL;
6592 }
6593 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 list_free(l);
6596 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006597 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006599 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006601 char_u *wp;
6602
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006603 // for "[var, var] = expr" evaluate the expression here, loop over the
6604 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006605 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006606
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006607 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006608 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006609 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006610 if (compile_expr0(&p, cctx) == FAIL)
6611 return NULL;
6612 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006614 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006616 type_T *stacktype;
6617
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006618 stacktype = stack->ga_len == 0 ? &t_void
6619 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006620 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006622 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006623 goto theend;
6624 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006625 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006626 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006627 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006628 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006629 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6630 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006631 if (stacktype->tt_member != NULL)
6632 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006633 }
6634 }
6635
6636 /*
6637 * Loop over variables in "[var, var] = expr".
6638 * For "var = expr" and "let var: type" this is done only once.
6639 */
6640 if (var_count > 0)
6641 var_start = skipwhite(arg + 1); // skip over the "["
6642 else
6643 var_start = arg;
6644 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6645 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006646 int instr_count = -1;
6647
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006648 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6649 {
6650 // Ignore underscore in "[a, _, b] = list".
6651 if (var_count > 0)
6652 {
6653 var_start = skipwhite(var_start + 2);
6654 continue;
6655 }
6656 emsg(_(e_cannot_use_underscore_here));
6657 goto theend;
6658 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006659 vim_free(lhs.lhs_name);
6660
6661 /*
6662 * Figure out the LHS type and other properties.
6663 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006664 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6665 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006666 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006667 if (!heredoc)
6668 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006669 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006670 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006671 if (oplen > 0 && var_count == 0)
6672 {
6673 // skip over the "=" and the expression
6674 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006675 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006676 }
6677 }
6678 else if (oplen > 0)
6679 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006680 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006681 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006682
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006683 // For "var = expr" evaluate the expression.
6684 if (var_count == 0)
6685 {
6686 int r;
6687
6688 // for "+=", "*=", "..=" etc. first load the current value
6689 if (*op != '=')
6690 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006691 if (compile_load_lhs_with_index(&lhs, var_start,
6692 cctx) == FAIL)
6693 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006694 }
6695
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006696 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006697 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006698 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006699 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006700 r = generate_PUSHNR(cctx, 1);
6701 }
6702 else
6703 {
6704 // Temporarily hide the new local variable here, it is
6705 // not available to this expression.
6706 if (lhs.lhs_new_local)
6707 --cctx->ctx_locals.ga_len;
6708 wp = op + oplen;
6709 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6710 {
6711 if (lhs.lhs_new_local)
6712 ++cctx->ctx_locals.ga_len;
6713 goto theend;
6714 }
6715 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006716 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006717 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006718 if (r == FAIL)
6719 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006720 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006721 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006722 else if (semicolon && var_idx == var_count - 1)
6723 {
6724 // For "[var; var] = expr" get the rest of the list
6725 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6726 goto theend;
6727 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006728 else
6729 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006730 // For "[var, var] = expr" get the "var_idx" item from the
6731 // list.
6732 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006733 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006734 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006735
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006736 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006737 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006738 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006739 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006740 if ((rhs_type->tt_type == VAR_FUNC
6741 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006742 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006743 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006744 goto theend;
6745
Bram Moolenaar752fc692021-01-04 21:57:11 +01006746 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006747 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006748 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006749 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006750 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006751 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006752 }
6753 else
6754 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006755 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006756 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006757 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006758 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006759 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006760 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006761 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006762 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006763 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006764 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006765 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006766 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006767 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006768 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006769 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006770
Bram Moolenaar77709b12021-04-03 21:01:01 +02006771 // Without operator check type here, otherwise below.
6772 // Use the line number of the assignment.
6773 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006774 if (lhs.lhs_has_index)
6775 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006776 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006777 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006778 goto theend;
6779 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006780 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006781 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006782 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006783 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006784 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006785 else if (cmdidx == CMD_final)
6786 {
6787 emsg(_(e_final_requires_a_value));
6788 goto theend;
6789 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006790 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006792 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006793 goto theend;
6794 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006795 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006796 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006797 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006798 goto theend;
6799 }
6800 else
6801 {
6802 // variables are always initialized
6803 if (ga_grow(instr, 1) == FAIL)
6804 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006805 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006806 {
6807 case VAR_BOOL:
6808 generate_PUSHBOOL(cctx, VVAL_FALSE);
6809 break;
6810 case VAR_FLOAT:
6811#ifdef FEAT_FLOAT
6812 generate_PUSHF(cctx, 0.0);
6813#endif
6814 break;
6815 case VAR_STRING:
6816 generate_PUSHS(cctx, NULL);
6817 break;
6818 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006819 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006820 break;
6821 case VAR_FUNC:
6822 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6823 break;
6824 case VAR_LIST:
6825 generate_NEWLIST(cctx, 0);
6826 break;
6827 case VAR_DICT:
6828 generate_NEWDICT(cctx, 0);
6829 break;
6830 case VAR_JOB:
6831 generate_PUSHJOB(cctx, NULL);
6832 break;
6833 case VAR_CHANNEL:
6834 generate_PUSHCHANNEL(cctx, NULL);
6835 break;
6836 case VAR_NUMBER:
6837 case VAR_UNKNOWN:
6838 case VAR_ANY:
6839 case VAR_PARTIAL:
6840 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006841 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006842 case VAR_SPECIAL: // cannot happen
6843 generate_PUSHNR(cctx, 0);
6844 break;
6845 }
6846 }
6847 if (var_count == 0)
6848 end = p;
6849 }
6850
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006851 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006852 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006853 break;
6854
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006855 if (oplen > 0 && *op != '=')
6856 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006857 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006858 type_T *stacktype;
6859
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006860 if (*op == '.')
6861 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006862 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006863 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006864 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006865 if (
6866#ifdef FEAT_FLOAT
6867 // If variable is float operation with number is OK.
6868 !(expected == &t_float && stacktype == &t_number) &&
6869#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006870 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006871 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006872 goto theend;
6873
6874 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006875 {
6876 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6877 goto theend;
6878 }
6879 else if (*op == '+')
6880 {
6881 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006882 operator_type(lhs.lhs_member_type, stacktype),
6883 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006884 goto theend;
6885 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006886 else if (generate_two_op(cctx, op) == FAIL)
6887 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889
Bram Moolenaar752fc692021-01-04 21:57:11 +01006890 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006891 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006892 // Use the info in "lhs" to store the value at the index in the
6893 // list or dict.
6894 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6895 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006896 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006897 }
6898 else
6899 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006900 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006901 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006902 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006903 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006904 generate_LOCKCONST(cctx);
6905
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006906 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006907 && (lhs.lhs_type->tt_type == VAR_DICT
6908 || lhs.lhs_type->tt_type == VAR_LIST)
6909 && lhs.lhs_type->tt_member != NULL
6910 && lhs.lhs_type->tt_member != &t_any
6911 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006912 // Set the type in the list or dict, so that it can be checked,
6913 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006914 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006915
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006916 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6917 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006918 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006919
6920 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006921 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006923
6924 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006925 if (var_count > 0 && !semicolon)
6926 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006927 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006928 goto theend;
6929 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006930
Bram Moolenaarb2097502020-07-19 17:17:02 +02006931 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932
6933theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006934 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 return ret;
6936}
6937
6938/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006939 * Check for an assignment at "eap->cmd", compile it if found.
6940 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6941 */
6942 static int
6943may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6944{
6945 char_u *pskip;
6946 char_u *p;
6947
6948 // Assuming the command starts with a variable or function name,
6949 // find what follows.
6950 // Skip over "var.member", "var[idx]" and the like.
6951 // Also "&opt = val", "$ENV = val" and "@r = val".
6952 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6953 ? eap->cmd + 1 : eap->cmd;
6954 p = to_name_end(pskip, TRUE);
6955 if (p > eap->cmd && *p != NUL)
6956 {
6957 char_u *var_end;
6958 int oplen;
6959 int heredoc;
6960
6961 if (eap->cmd[0] == '@')
6962 var_end = eap->cmd + 2;
6963 else
6964 var_end = find_name_end(pskip, NULL, NULL,
6965 FNE_CHECK_START | FNE_INCL_BR);
6966 oplen = assignment_len(skipwhite(var_end), &heredoc);
6967 if (oplen > 0)
6968 {
6969 size_t len = p - eap->cmd;
6970
6971 // Recognize an assignment if we recognize the variable
6972 // name:
6973 // "g:var = expr"
6974 // "local = expr" where "local" is a local var.
6975 // "script = expr" where "script" is a script-local var.
6976 // "import = expr" where "import" is an imported var
6977 // "&opt = expr"
6978 // "$ENV = expr"
6979 // "@r = expr"
6980 if (*eap->cmd == '&'
6981 || *eap->cmd == '$'
6982 || *eap->cmd == '@'
6983 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006984 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006985 {
6986 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6987 if (*line == NULL || *line == eap->cmd)
6988 return FAIL;
6989 return OK;
6990 }
6991 }
6992 }
6993
6994 if (*eap->cmd == '[')
6995 {
6996 // [var, var] = expr
6997 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6998 if (*line == NULL)
6999 return FAIL;
7000 if (*line != eap->cmd)
7001 return OK;
7002 }
7003 return NOTDONE;
7004}
7005
7006/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007007 * Check if "name" can be "unlet".
7008 */
7009 int
7010check_vim9_unlet(char_u *name)
7011{
7012 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7013 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007014 // "unlet s:var" is allowed in legacy script.
7015 if (*name == 's' && !script_is_vim9())
7016 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007017 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007018 return FAIL;
7019 }
7020 return OK;
7021}
7022
7023/*
7024 * Callback passed to ex_unletlock().
7025 */
7026 static int
7027compile_unlet(
7028 lval_T *lvp,
7029 char_u *name_end,
7030 exarg_T *eap,
7031 int deep UNUSED,
7032 void *coookie)
7033{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007034 cctx_T *cctx = coookie;
7035 char_u *p = lvp->ll_name;
7036 int cc = *name_end;
7037 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007038
Bram Moolenaar752fc692021-01-04 21:57:11 +01007039 if (cctx->ctx_skip == SKIP_YES)
7040 return OK;
7041
7042 *name_end = NUL;
7043 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007044 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007045 // :unlet $ENV_VAR
7046 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7047 }
7048 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7049 {
7050 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007051
Bram Moolenaar752fc692021-01-04 21:57:11 +01007052 // This is similar to assigning: lookup the list/dict, compile the
7053 // idx/key. Then instead of storing the value unlet the item.
7054 // unlet {list}[idx]
7055 // unlet {dict}[key] dict.key
7056 //
7057 // Figure out the LHS type and other properties.
7058 //
7059 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7060
7061 // : unlet an indexed item
7062 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007063 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007064 iemsg("called compile_lhs() without an index");
7065 ret = FAIL;
7066 }
7067 else
7068 {
7069 // Use the info in "lhs" to unlet the item at the index in the
7070 // list or dict.
7071 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007072 }
7073
Bram Moolenaar752fc692021-01-04 21:57:11 +01007074 vim_free(lhs.lhs_name);
7075 }
7076 else if (check_vim9_unlet(p) == FAIL)
7077 {
7078 ret = FAIL;
7079 }
7080 else
7081 {
7082 // Normal name. Only supports g:, w:, t: and b: namespaces.
7083 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007084 }
7085
Bram Moolenaar752fc692021-01-04 21:57:11 +01007086 *name_end = cc;
7087 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007088}
7089
7090/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007091 * Callback passed to ex_unletlock().
7092 */
7093 static int
7094compile_lock_unlock(
7095 lval_T *lvp,
7096 char_u *name_end,
7097 exarg_T *eap,
7098 int deep UNUSED,
7099 void *coookie)
7100{
7101 cctx_T *cctx = coookie;
7102 int cc = *name_end;
7103 char_u *p = lvp->ll_name;
7104 int ret = OK;
7105 size_t len;
7106 char_u *buf;
7107
7108 if (cctx->ctx_skip == SKIP_YES)
7109 return OK;
7110
7111 // Cannot use :lockvar and :unlockvar on local variables.
7112 if (p[1] != ':')
7113 {
7114 char_u *end = skip_var_one(p, FALSE);
7115
7116 if (lookup_local(p, end - p, NULL, cctx) == OK)
7117 {
7118 emsg(_(e_cannot_lock_unlock_local_variable));
7119 return FAIL;
7120 }
7121 }
7122
7123 // Checking is done at runtime.
7124 *name_end = NUL;
7125 len = name_end - p + 20;
7126 buf = alloc(len);
7127 if (buf == NULL)
7128 ret = FAIL;
7129 else
7130 {
7131 vim_snprintf((char *)buf, len, "%s %s",
7132 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7133 p);
7134 ret = generate_EXEC(cctx, buf);
7135
7136 vim_free(buf);
7137 *name_end = cc;
7138 }
7139 return ret;
7140}
7141
7142/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007143 * compile "unlet var", "lock var" and "unlock var"
7144 * "arg" points to "var".
7145 */
7146 static char_u *
7147compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7148{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007149 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7150 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7151 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007152 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7153}
7154
7155/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007156 * Compile an :import command.
7157 */
7158 static char_u *
7159compile_import(char_u *arg, cctx_T *cctx)
7160{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007161 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162}
7163
7164/*
7165 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7166 */
7167 static int
7168compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7169{
7170 garray_T *instr = &cctx->ctx_instr;
7171 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7172
7173 if (endlabel == NULL)
7174 return FAIL;
7175 endlabel->el_next = *el;
7176 *el = endlabel;
7177 endlabel->el_end_label = instr->ga_len;
7178
7179 generate_JUMP(cctx, when, 0);
7180 return OK;
7181}
7182
7183 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007184compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185{
7186 garray_T *instr = &cctx->ctx_instr;
7187
7188 while (*el != NULL)
7189 {
7190 endlabel_T *cur = (*el);
7191 isn_T *isn;
7192
7193 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007194 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 *el = cur->el_next;
7196 vim_free(cur);
7197 }
7198}
7199
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007200 static void
7201compile_free_jump_to_end(endlabel_T **el)
7202{
7203 while (*el != NULL)
7204 {
7205 endlabel_T *cur = (*el);
7206
7207 *el = cur->el_next;
7208 vim_free(cur);
7209 }
7210}
7211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007212/*
7213 * Create a new scope and set up the generic items.
7214 */
7215 static scope_T *
7216new_scope(cctx_T *cctx, scopetype_T type)
7217{
7218 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7219
7220 if (scope == NULL)
7221 return NULL;
7222 scope->se_outer = cctx->ctx_scope;
7223 cctx->ctx_scope = scope;
7224 scope->se_type = type;
7225 scope->se_local_count = cctx->ctx_locals.ga_len;
7226 return scope;
7227}
7228
7229/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007230 * Free the current scope and go back to the outer scope.
7231 */
7232 static void
7233drop_scope(cctx_T *cctx)
7234{
7235 scope_T *scope = cctx->ctx_scope;
7236
7237 if (scope == NULL)
7238 {
7239 iemsg("calling drop_scope() without a scope");
7240 return;
7241 }
7242 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007243 switch (scope->se_type)
7244 {
7245 case IF_SCOPE:
7246 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7247 case FOR_SCOPE:
7248 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7249 case WHILE_SCOPE:
7250 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7251 case TRY_SCOPE:
7252 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7253 case NO_SCOPE:
7254 case BLOCK_SCOPE:
7255 break;
7256 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007257 vim_free(scope);
7258}
7259
7260/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 * compile "if expr"
7262 *
7263 * "if expr" Produces instructions:
7264 * EVAL expr Push result of "expr"
7265 * JUMP_IF_FALSE end
7266 * ... body ...
7267 * end:
7268 *
7269 * "if expr | else" Produces instructions:
7270 * EVAL expr Push result of "expr"
7271 * JUMP_IF_FALSE else
7272 * ... body ...
7273 * JUMP_ALWAYS end
7274 * else:
7275 * ... body ...
7276 * end:
7277 *
7278 * "if expr1 | elseif expr2 | else" Produces instructions:
7279 * EVAL expr Push result of "expr"
7280 * JUMP_IF_FALSE elseif
7281 * ... body ...
7282 * JUMP_ALWAYS end
7283 * elseif:
7284 * EVAL expr Push result of "expr"
7285 * JUMP_IF_FALSE else
7286 * ... body ...
7287 * JUMP_ALWAYS end
7288 * else:
7289 * ... body ...
7290 * end:
7291 */
7292 static char_u *
7293compile_if(char_u *arg, cctx_T *cctx)
7294{
7295 char_u *p = arg;
7296 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007297 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007299 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007300 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301
Bram Moolenaara5565e42020-05-09 15:44:01 +02007302 CLEAR_FIELD(ppconst);
7303 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007304 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007305 clear_ppconst(&ppconst);
7306 return NULL;
7307 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007308 if (!ends_excmd2(arg, skipwhite(p)))
7309 {
7310 semsg(_(e_trailing_arg), p);
7311 return NULL;
7312 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007313 if (cctx->ctx_skip == SKIP_YES)
7314 clear_ppconst(&ppconst);
7315 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007316 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007317 int error = FALSE;
7318 int v;
7319
Bram Moolenaara5565e42020-05-09 15:44:01 +02007320 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007321 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007322 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007323 if (error)
7324 return NULL;
7325 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007326 }
7327 else
7328 {
7329 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007330 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007331 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007332 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007333 if (bool_on_stack(cctx) == FAIL)
7334 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336
Bram Moolenaara91a7132021-03-25 21:12:15 +01007337 // CMDMOD_REV must come before the jump
7338 generate_undo_cmdmods(cctx);
7339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 scope = new_scope(cctx, IF_SCOPE);
7341 if (scope == NULL)
7342 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007343 scope->se_skip_save = skip_save;
7344 // "is_had_return" will be reset if any block does not end in :return
7345 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007347 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007348 {
7349 // "where" is set when ":elseif", "else" or ":endif" is found
7350 scope->se_u.se_if.is_if_label = instr->ga_len;
7351 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7352 }
7353 else
7354 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355
Bram Moolenaarced68a02021-01-24 17:53:47 +01007356#ifdef FEAT_PROFILE
7357 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7358 && skip_save != SKIP_YES)
7359 {
7360 // generated a profile start, need to generate a profile end, since it
7361 // won't be done after returning
7362 cctx->ctx_skip = SKIP_NOT;
7363 generate_instr(cctx, ISN_PROF_END);
7364 cctx->ctx_skip = SKIP_YES;
7365 }
7366#endif
7367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368 return p;
7369}
7370
7371 static char_u *
7372compile_elseif(char_u *arg, cctx_T *cctx)
7373{
7374 char_u *p = arg;
7375 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007376 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 isn_T *isn;
7378 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007379 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007380 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381
7382 if (scope == NULL || scope->se_type != IF_SCOPE)
7383 {
7384 emsg(_(e_elseif_without_if));
7385 return NULL;
7386 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007387 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007388 if (!cctx->ctx_had_return)
7389 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390
Bram Moolenaarced68a02021-01-24 17:53:47 +01007391 if (cctx->ctx_skip == SKIP_NOT)
7392 {
7393 // previous block was executed, this one and following will not
7394 cctx->ctx_skip = SKIP_YES;
7395 scope->se_u.se_if.is_seen_skip_not = TRUE;
7396 }
7397 if (scope->se_u.se_if.is_seen_skip_not)
7398 {
7399 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007400 // Do not count the "elseif" for profiling and cmdmod
7401 instr->ga_len = current_instr_idx(cctx);
7402
Bram Moolenaarced68a02021-01-24 17:53:47 +01007403 skip_expr_cctx(&p, cctx);
7404 return p;
7405 }
7406
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007407 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007408 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007409 int moved_cmdmod = FALSE;
7410
7411 // Move any CMDMOD instruction to after the jump
7412 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7413 {
7414 if (ga_grow(instr, 1) == FAIL)
7415 return NULL;
7416 ((isn_T *)instr->ga_data)[instr->ga_len] =
7417 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7418 --instr->ga_len;
7419 moved_cmdmod = TRUE;
7420 }
7421
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007422 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007424 return NULL;
7425 // previous "if" or "elseif" jumps here
7426 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7427 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007428 if (moved_cmdmod)
7429 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007432 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007433 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007434 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007435 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007436 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007437#ifdef FEAT_PROFILE
7438 if (cctx->ctx_profiling)
7439 {
7440 // the previous block was skipped, need to profile this line
7441 generate_instr(cctx, ISN_PROF_START);
7442 instr_count = instr->ga_len;
7443 }
7444#endif
7445 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007446 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007447 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007448 clear_ppconst(&ppconst);
7449 return NULL;
7450 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007451 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007452 if (!ends_excmd2(arg, skipwhite(p)))
7453 {
7454 semsg(_(e_trailing_arg), p);
7455 return NULL;
7456 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007457 if (scope->se_skip_save == SKIP_YES)
7458 clear_ppconst(&ppconst);
7459 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007460 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007461 int error = FALSE;
7462 int v;
7463
Bram Moolenaar7f141552020-05-09 17:35:53 +02007464 // The expression results in a constant.
7465 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007466 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7467 if (error)
7468 return NULL;
7469 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007470 clear_ppconst(&ppconst);
7471 scope->se_u.se_if.is_if_label = -1;
7472 }
7473 else
7474 {
7475 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007476 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007477 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007478 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007479 if (bool_on_stack(cctx) == FAIL)
7480 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481
Bram Moolenaara91a7132021-03-25 21:12:15 +01007482 // CMDMOD_REV must come before the jump
7483 generate_undo_cmdmods(cctx);
7484
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007485 // "where" is set when ":elseif", "else" or ":endif" is found
7486 scope->se_u.se_if.is_if_label = instr->ga_len;
7487 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7488 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489
7490 return p;
7491}
7492
7493 static char_u *
7494compile_else(char_u *arg, cctx_T *cctx)
7495{
7496 char_u *p = arg;
7497 garray_T *instr = &cctx->ctx_instr;
7498 isn_T *isn;
7499 scope_T *scope = cctx->ctx_scope;
7500
7501 if (scope == NULL || scope->se_type != IF_SCOPE)
7502 {
7503 emsg(_(e_else_without_if));
7504 return NULL;
7505 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007506 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007507 if (!cctx->ctx_had_return)
7508 scope->se_u.se_if.is_had_return = FALSE;
7509 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510
Bram Moolenaarced68a02021-01-24 17:53:47 +01007511#ifdef FEAT_PROFILE
7512 if (cctx->ctx_profiling)
7513 {
7514 if (cctx->ctx_skip == SKIP_NOT
7515 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7516 .isn_type == ISN_PROF_START)
7517 // the previous block was executed, do not count "else" for profiling
7518 --instr->ga_len;
7519 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7520 {
7521 // the previous block was not executed, this one will, do count the
7522 // "else" for profiling
7523 cctx->ctx_skip = SKIP_NOT;
7524 generate_instr(cctx, ISN_PROF_END);
7525 generate_instr(cctx, ISN_PROF_START);
7526 cctx->ctx_skip = SKIP_YES;
7527 }
7528 }
7529#endif
7530
7531 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007532 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007533 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007534 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007535 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007536 if (!cctx->ctx_had_return
7537 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7538 JUMP_ALWAYS, cctx) == FAIL)
7539 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007540 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007541
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007542 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007543 {
7544 if (scope->se_u.se_if.is_if_label >= 0)
7545 {
7546 // previous "if" or "elseif" jumps here
7547 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7548 isn->isn_arg.jump.jump_where = instr->ga_len;
7549 scope->se_u.se_if.is_if_label = -1;
7550 }
7551 }
7552
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007553 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007554 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7555 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556
7557 return p;
7558}
7559
7560 static char_u *
7561compile_endif(char_u *arg, cctx_T *cctx)
7562{
7563 scope_T *scope = cctx->ctx_scope;
7564 ifscope_T *ifscope;
7565 garray_T *instr = &cctx->ctx_instr;
7566 isn_T *isn;
7567
Bram Moolenaarfa984412021-03-25 22:15:28 +01007568 if (misplaced_cmdmod(cctx))
7569 return NULL;
7570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007571 if (scope == NULL || scope->se_type != IF_SCOPE)
7572 {
7573 emsg(_(e_endif_without_if));
7574 return NULL;
7575 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007576 ifscope = &scope->se_u.se_if;
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 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007581 if (scope->se_u.se_if.is_if_label >= 0)
7582 {
7583 // previous "if" or "elseif" jumps here
7584 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7585 isn->isn_arg.jump.jump_where = instr->ga_len;
7586 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007587 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007588 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007589
7590#ifdef FEAT_PROFILE
7591 // even when skipping we count the endif as executed, unless the block it's
7592 // in is skipped
7593 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7594 && scope->se_skip_save != SKIP_YES)
7595 {
7596 cctx->ctx_skip = SKIP_NOT;
7597 generate_instr(cctx, ISN_PROF_START);
7598 }
7599#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007600 cctx->ctx_skip = scope->se_skip_save;
7601
7602 // If all the blocks end in :return and there is an :else then the
7603 // had_return flag is set.
7604 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007605
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007606 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607 return arg;
7608}
7609
7610/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007611 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007612 *
7613 * Produces instructions:
7614 * PUSHNR -1
7615 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007616 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7618 * - if beyond end, jump to "end"
7619 * - otherwise get item from list and push it
7620 * STORE var Store item in "var"
7621 * ... body ...
7622 * JUMP top Jump back to repeat
7623 * end: DROP Drop the result of "expr"
7624 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007625 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7626 * UNPACK 2 Split item in 2
7627 * STORE var1 Store item in "var1"
7628 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 */
7630 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007631compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007632{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007633 char_u *arg;
7634 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007635 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007636 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007637 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007638 int var_count = 0;
7639 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007641 garray_T *stack = &cctx->ctx_type_stack;
7642 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007643 lvar_T *loop_lvar; // loop iteration variable
7644 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007646 type_T *item_type = &t_any;
7647 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007648
Bram Moolenaar792f7862020-11-23 08:31:18 +01007649 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007650 if (p == NULL)
7651 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007652 if (var_count == 0)
7653 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654
7655 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007656 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007657 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7658 return NULL;
7659 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660 {
7661 emsg(_(e_missing_in));
7662 return NULL;
7663 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007664 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007665 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7666 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007668 scope = new_scope(cctx, FOR_SCOPE);
7669 if (scope == NULL)
7670 return NULL;
7671
Bram Moolenaar792f7862020-11-23 08:31:18 +01007672 // Reserve a variable to store the loop iteration counter and initialize it
7673 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007674 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7675 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007676 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007677 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007678 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007679 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007680 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007681 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007682
7683 // compile "expr", it remains on the stack until "endfor"
7684 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007685 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007686 {
7687 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007688 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007689 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007690 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007691
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007692 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007693 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007695 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007696 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007697 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007698 semsg(_(e_for_loop_on_str_not_supported),
7699 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007700 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007701 return NULL;
7702 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007703
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007704 if (vartype->tt_type == VAR_STRING)
7705 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007706 else if (vartype->tt_type == VAR_BLOB)
7707 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007708 else if (vartype->tt_type == VAR_LIST
7709 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007710 {
7711 if (var_count == 1)
7712 item_type = vartype->tt_member;
7713 else if (vartype->tt_member->tt_type == VAR_LIST
7714 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007715 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007716 item_type = vartype->tt_member->tt_member;
7717 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007718
Bram Moolenaara91a7132021-03-25 21:12:15 +01007719 // CMDMOD_REV must come before the FOR instruction
7720 generate_undo_cmdmods(cctx);
7721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007722 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007723 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007724 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725
Bram Moolenaar792f7862020-11-23 08:31:18 +01007726 arg = arg_start;
7727 if (var_count > 1)
7728 {
7729 generate_UNPACK(cctx, var_count, semicolon);
7730 arg = skipwhite(arg + 1); // skip white after '['
7731
7732 // the list item is replaced by a number of items
7733 if (ga_grow(stack, var_count - 1) == FAIL)
7734 {
7735 drop_scope(cctx);
7736 return NULL;
7737 }
7738 --stack->ga_len;
7739 for (idx = 0; idx < var_count; ++idx)
7740 {
7741 ((type_T **)stack->ga_data)[stack->ga_len] =
7742 (semicolon && idx == 0) ? vartype : item_type;
7743 ++stack->ga_len;
7744 }
7745 }
7746
7747 for (idx = 0; idx < var_count; ++idx)
7748 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007749 assign_dest_T dest = dest_local;
7750 int opt_flags = 0;
7751 int vimvaridx = -1;
7752 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007753 type_T *lhs_type = &t_any;
7754 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007755
7756 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007757 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007758 name = vim_strnsave(arg, varlen);
7759 if (name == NULL)
7760 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007761 if (*p == ':')
7762 {
7763 p = skipwhite(p + 1);
7764 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7765 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007766
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007767 // TODO: script var not supported?
7768 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7769 &vimvaridx, &type, cctx) == FAIL)
7770 goto failed;
7771 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007772 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007773 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7774 0, 0, type, name) == FAIL)
7775 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007776 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007777 else if (varlen == 1 && *arg == '_')
7778 {
7779 // Assigning to "_": drop the value.
7780 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7781 goto failed;
7782 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007783 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007784 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007785 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007786 {
7787 semsg(_(e_variable_already_declared), arg);
7788 goto failed;
7789 }
7790
Bram Moolenaarea870692020-12-02 14:24:30 +01007791 if (STRNCMP(name, "s:", 2) == 0)
7792 {
7793 semsg(_(e_cannot_declare_script_variable_in_function), name);
7794 goto failed;
7795 }
7796
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007797 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007798 where.wt_index = var_count > 1 ? idx + 1 : 0;
7799 where.wt_variable = TRUE;
7800 if (lhs_type == &t_any)
7801 lhs_type = item_type;
7802 else if (item_type != &t_unknown
7803 && !(var_count > 1 && item_type == &t_any)
7804 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7805 goto failed;
7806 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007807 if (var_lvar == NULL)
7808 // out of memory or used as an argument
7809 goto failed;
7810
7811 if (semicolon && idx == var_count - 1)
7812 var_lvar->lv_type = vartype;
7813 else
7814 var_lvar->lv_type = item_type;
7815 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7816 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007817
7818 if (*p == ',' || *p == ';')
7819 ++p;
7820 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007821 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007822 }
7823
7824 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007825
7826failed:
7827 vim_free(name);
7828 drop_scope(cctx);
7829 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830}
7831
7832/*
7833 * compile "endfor"
7834 */
7835 static char_u *
7836compile_endfor(char_u *arg, cctx_T *cctx)
7837{
7838 garray_T *instr = &cctx->ctx_instr;
7839 scope_T *scope = cctx->ctx_scope;
7840 forscope_T *forscope;
7841 isn_T *isn;
7842
Bram Moolenaarfa984412021-03-25 22:15:28 +01007843 if (misplaced_cmdmod(cctx))
7844 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007845
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007846 if (scope == NULL || scope->se_type != FOR_SCOPE)
7847 {
7848 emsg(_(e_for));
7849 return NULL;
7850 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007851 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007852 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007853 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007854
7855 // At end of ":for" scope jump back to the FOR instruction.
7856 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7857
7858 // Fill in the "end" label in the FOR statement so it can jump here
7859 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7860 isn->isn_arg.forloop.for_end = instr->ga_len;
7861
7862 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007863 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007864
7865 // Below the ":for" scope drop the "expr" list from the stack.
7866 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7867 return NULL;
7868
7869 vim_free(scope);
7870
7871 return arg;
7872}
7873
7874/*
7875 * compile "while expr"
7876 *
7877 * Produces instructions:
7878 * top: EVAL expr Push result of "expr"
7879 * JUMP_IF_FALSE end jump if false
7880 * ... body ...
7881 * JUMP top Jump back to repeat
7882 * end:
7883 *
7884 */
7885 static char_u *
7886compile_while(char_u *arg, cctx_T *cctx)
7887{
7888 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007889 scope_T *scope;
7890
7891 scope = new_scope(cctx, WHILE_SCOPE);
7892 if (scope == NULL)
7893 return NULL;
7894
Bram Moolenaara91a7132021-03-25 21:12:15 +01007895 // "endwhile" jumps back here, one before when profiling or using cmdmods
7896 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007897
7898 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007899 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007900 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007901 if (!ends_excmd2(arg, skipwhite(p)))
7902 {
7903 semsg(_(e_trailing_arg), p);
7904 return NULL;
7905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007906
Bram Moolenaar13106602020-10-04 16:06:05 +02007907 if (bool_on_stack(cctx) == FAIL)
7908 return FAIL;
7909
Bram Moolenaara91a7132021-03-25 21:12:15 +01007910 // CMDMOD_REV must come before the jump
7911 generate_undo_cmdmods(cctx);
7912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007913 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007914 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007915 JUMP_IF_FALSE, cctx) == FAIL)
7916 return FAIL;
7917
7918 return p;
7919}
7920
7921/*
7922 * compile "endwhile"
7923 */
7924 static char_u *
7925compile_endwhile(char_u *arg, cctx_T *cctx)
7926{
7927 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007928 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007929
Bram Moolenaarfa984412021-03-25 22:15:28 +01007930 if (misplaced_cmdmod(cctx))
7931 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7933 {
7934 emsg(_(e_while));
7935 return NULL;
7936 }
7937 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007938 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007939
Bram Moolenaarf002a412021-01-24 13:34:18 +01007940#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007941 // count the endwhile before jumping
7942 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007943#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007945 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007946 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007947
7948 // Fill in the "end" label in the WHILE statement so it can jump here.
7949 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007950 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7951 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007952
7953 vim_free(scope);
7954
7955 return arg;
7956}
7957
7958/*
7959 * compile "continue"
7960 */
7961 static char_u *
7962compile_continue(char_u *arg, cctx_T *cctx)
7963{
7964 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007965 int try_scopes = 0;
7966 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007967
7968 for (;;)
7969 {
7970 if (scope == NULL)
7971 {
7972 emsg(_(e_continue));
7973 return NULL;
7974 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007975 if (scope->se_type == FOR_SCOPE)
7976 {
7977 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007978 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007979 }
7980 if (scope->se_type == WHILE_SCOPE)
7981 {
7982 loop_label = scope->se_u.se_while.ws_top_label;
7983 break;
7984 }
7985 if (scope->se_type == TRY_SCOPE)
7986 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007987 scope = scope->se_outer;
7988 }
7989
Bram Moolenaarc150c092021-02-13 15:02:46 +01007990 if (try_scopes > 0)
7991 // Inside one or more try/catch blocks we first need to jump to the
7992 // "finally" or "endtry" to cleanup.
7993 generate_TRYCONT(cctx, try_scopes, loop_label);
7994 else
7995 // Jump back to the FOR or WHILE instruction.
7996 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007998 return arg;
7999}
8000
8001/*
8002 * compile "break"
8003 */
8004 static char_u *
8005compile_break(char_u *arg, cctx_T *cctx)
8006{
8007 scope_T *scope = cctx->ctx_scope;
8008 endlabel_T **el;
8009
8010 for (;;)
8011 {
8012 if (scope == NULL)
8013 {
8014 emsg(_(e_break));
8015 return NULL;
8016 }
8017 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8018 break;
8019 scope = scope->se_outer;
8020 }
8021
8022 // Jump to the end of the FOR or WHILE loop.
8023 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008024 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008025 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008026 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008027 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8028 return FAIL;
8029
8030 return arg;
8031}
8032
8033/*
8034 * compile "{" start of block
8035 */
8036 static char_u *
8037compile_block(char_u *arg, cctx_T *cctx)
8038{
8039 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8040 return NULL;
8041 return skipwhite(arg + 1);
8042}
8043
8044/*
8045 * compile end of block: drop one scope
8046 */
8047 static void
8048compile_endblock(cctx_T *cctx)
8049{
8050 scope_T *scope = cctx->ctx_scope;
8051
8052 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008053 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008054 vim_free(scope);
8055}
8056
8057/*
8058 * compile "try"
8059 * Creates a new scope for the try-endtry, pointing to the first catch and
8060 * finally.
8061 * Creates another scope for the "try" block itself.
8062 * TRY instruction sets up exception handling at runtime.
8063 *
8064 * "try"
8065 * TRY -> catch1, -> finally push trystack entry
8066 * ... try block
8067 * "throw {exception}"
8068 * EVAL {exception}
8069 * THROW create exception
8070 * ... try block
8071 * " catch {expr}"
8072 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008073 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008074 * EVAL {expr}
8075 * MATCH
8076 * JUMP nomatch -> catch2
8077 * CATCH remove exception
8078 * ... catch block
8079 * " catch"
8080 * JUMP -> finally
8081 * catch2: CATCH remove exception
8082 * ... catch block
8083 * " finally"
8084 * finally:
8085 * ... finally block
8086 * " endtry"
8087 * ENDTRY pop trystack entry, may rethrow
8088 */
8089 static char_u *
8090compile_try(char_u *arg, cctx_T *cctx)
8091{
8092 garray_T *instr = &cctx->ctx_instr;
8093 scope_T *try_scope;
8094 scope_T *scope;
8095
Bram Moolenaarfa984412021-03-25 22:15:28 +01008096 if (misplaced_cmdmod(cctx))
8097 return NULL;
8098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008099 // scope that holds the jumps that go to catch/finally/endtry
8100 try_scope = new_scope(cctx, TRY_SCOPE);
8101 if (try_scope == NULL)
8102 return NULL;
8103
Bram Moolenaar69f70502021-01-01 16:10:46 +01008104 if (cctx->ctx_skip != SKIP_YES)
8105 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008106 isn_T *isn;
8107
8108 // "try_catch" is set when the first ":catch" is found or when no catch
8109 // is found and ":finally" is found.
8110 // "try_finally" is set when ":finally" is found
8111 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008112 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008113 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8114 return NULL;
8115 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8116 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008117 return NULL;
8118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008119
8120 // scope for the try block itself
8121 scope = new_scope(cctx, BLOCK_SCOPE);
8122 if (scope == NULL)
8123 return NULL;
8124
8125 return arg;
8126}
8127
8128/*
8129 * compile "catch {expr}"
8130 */
8131 static char_u *
8132compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8133{
8134 scope_T *scope = cctx->ctx_scope;
8135 garray_T *instr = &cctx->ctx_instr;
8136 char_u *p;
8137 isn_T *isn;
8138
Bram Moolenaarfa984412021-03-25 22:15:28 +01008139 if (misplaced_cmdmod(cctx))
8140 return NULL;
8141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008142 // end block scope from :try or :catch
8143 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8144 compile_endblock(cctx);
8145 scope = cctx->ctx_scope;
8146
8147 // Error if not in a :try scope
8148 if (scope == NULL || scope->se_type != TRY_SCOPE)
8149 {
8150 emsg(_(e_catch));
8151 return NULL;
8152 }
8153
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008154 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008156 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008157 return NULL;
8158 }
8159
Bram Moolenaar69f70502021-01-01 16:10:46 +01008160 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008161 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008162#ifdef FEAT_PROFILE
8163 // the profile-start should be after the jump
8164 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8165 .isn_type == ISN_PROF_START)
8166 --instr->ga_len;
8167#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008168 // Jump from end of previous block to :finally or :endtry
8169 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8170 JUMP_ALWAYS, cctx) == FAIL)
8171 return NULL;
8172
8173 // End :try or :catch scope: set value in ISN_TRY instruction
8174 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008175 if (isn->isn_arg.try.try_ref->try_catch == 0)
8176 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008177 if (scope->se_u.se_try.ts_catch_label != 0)
8178 {
8179 // Previous catch without match jumps here
8180 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8181 isn->isn_arg.jump.jump_where = instr->ga_len;
8182 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008183#ifdef FEAT_PROFILE
8184 if (cctx->ctx_profiling)
8185 {
8186 // a "throw" that jumps here needs to be counted
8187 generate_instr(cctx, ISN_PROF_END);
8188 // the "catch" is also counted
8189 generate_instr(cctx, ISN_PROF_START);
8190 }
8191#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008192 }
8193
8194 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008195 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008196 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008197 scope->se_u.se_try.ts_caught_all = TRUE;
8198 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008199 }
8200 else
8201 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008202 char_u *end;
8203 char_u *pat;
8204 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008205 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008206 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008208 // Push v:exception, push {expr} and MATCH
8209 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8210
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008211 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008212 if (*end != *p)
8213 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008214 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008215 vim_free(tofree);
8216 return FAIL;
8217 }
8218 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008219 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008220 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008221 len = (int)(end - tofree);
8222 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008223 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008224 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008225 if (pat == NULL)
8226 return FAIL;
8227 if (generate_PUSHS(cctx, pat) == FAIL)
8228 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008230 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8231 return NULL;
8232
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008233 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008234 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8235 return NULL;
8236 }
8237
Bram Moolenaar69f70502021-01-01 16:10:46 +01008238 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008239 return NULL;
8240
8241 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8242 return NULL;
8243 return p;
8244}
8245
8246 static char_u *
8247compile_finally(char_u *arg, cctx_T *cctx)
8248{
8249 scope_T *scope = cctx->ctx_scope;
8250 garray_T *instr = &cctx->ctx_instr;
8251 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008252 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008253
Bram Moolenaarfa984412021-03-25 22:15:28 +01008254 if (misplaced_cmdmod(cctx))
8255 return NULL;
8256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008257 // end block scope from :try or :catch
8258 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8259 compile_endblock(cctx);
8260 scope = cctx->ctx_scope;
8261
8262 // Error if not in a :try scope
8263 if (scope == NULL || scope->se_type != TRY_SCOPE)
8264 {
8265 emsg(_(e_finally));
8266 return NULL;
8267 }
8268
8269 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008270 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008271 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008272 {
8273 emsg(_(e_finally_dup));
8274 return NULL;
8275 }
8276
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008277 this_instr = instr->ga_len;
8278#ifdef FEAT_PROFILE
8279 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8280 .isn_type == ISN_PROF_START)
8281 // jump to the profile start of the "finally"
8282 --this_instr;
8283#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008284
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008285 // Fill in the "end" label in jumps at the end of the blocks.
8286 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8287 this_instr, cctx);
8288
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008289 // If there is no :catch then an exception jumps to :finally.
8290 if (isn->isn_arg.try.try_ref->try_catch == 0)
8291 isn->isn_arg.try.try_ref->try_catch = this_instr;
8292 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008293 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008294 {
8295 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008296 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008297 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008298 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008299 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008300 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8301 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008303 // TODO: set index in ts_finally_label jumps
8304
8305 return arg;
8306}
8307
8308 static char_u *
8309compile_endtry(char_u *arg, cctx_T *cctx)
8310{
8311 scope_T *scope = cctx->ctx_scope;
8312 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008313 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008314
Bram Moolenaarfa984412021-03-25 22:15:28 +01008315 if (misplaced_cmdmod(cctx))
8316 return NULL;
8317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008318 // end block scope from :catch or :finally
8319 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8320 compile_endblock(cctx);
8321 scope = cctx->ctx_scope;
8322
8323 // Error if not in a :try scope
8324 if (scope == NULL || scope->se_type != TRY_SCOPE)
8325 {
8326 if (scope == NULL)
8327 emsg(_(e_no_endtry));
8328 else if (scope->se_type == WHILE_SCOPE)
8329 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008330 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008331 emsg(_(e_endfor));
8332 else
8333 emsg(_(e_endif));
8334 return NULL;
8335 }
8336
Bram Moolenaarc150c092021-02-13 15:02:46 +01008337 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008338 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008339 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008340 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8341 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008342 {
8343 emsg(_(e_missing_catch_or_finally));
8344 return NULL;
8345 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008346
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008347#ifdef FEAT_PROFILE
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008348 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8349 .isn_type == ISN_PROF_START)
8350 // move the profile start after "endtry" so that it's not counted when
8351 // the exception is rethrown.
8352 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008353#endif
8354
Bram Moolenaar69f70502021-01-01 16:10:46 +01008355 // Fill in the "end" label in jumps at the end of the blocks, if not
8356 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008357 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8358 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008359
Bram Moolenaar69f70502021-01-01 16:10:46 +01008360 if (scope->se_u.se_try.ts_catch_label != 0)
8361 {
8362 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008363 isn_T *isn = ((isn_T *)instr->ga_data)
8364 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008365 isn->isn_arg.jump.jump_where = instr->ga_len;
8366 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008367 }
8368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008369 compile_endblock(cctx);
8370
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008371 if (cctx->ctx_skip != SKIP_YES)
8372 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008373 // End :catch or :finally scope: set instruction index in ISN_TRY
8374 // instruction
8375 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008376 if (cctx->ctx_skip != SKIP_YES
8377 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8378 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008379#ifdef FEAT_PROFILE
8380 if (cctx->ctx_profiling)
8381 generate_instr(cctx, ISN_PROF_START);
8382#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008383 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008384 return arg;
8385}
8386
8387/*
8388 * compile "throw {expr}"
8389 */
8390 static char_u *
8391compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8392{
8393 char_u *p = skipwhite(arg);
8394
Bram Moolenaara5565e42020-05-09 15:44:01 +02008395 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008396 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008397 if (cctx->ctx_skip == SKIP_YES)
8398 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008399 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008400 return NULL;
8401 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8402 return NULL;
8403
8404 return p;
8405}
8406
8407/*
8408 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008409 * compile "echomsg expr"
8410 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008411 * compile "execute expr"
8412 */
8413 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008414compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008415{
8416 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008417 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008418 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008419 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008420 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008421 garray_T *stack = &cctx->ctx_type_stack;
8422 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008423
8424 for (;;)
8425 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008426 if (ends_excmd2(prev, p))
8427 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008428 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008429 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008430 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008431
8432 if (cctx->ctx_skip != SKIP_YES)
8433 {
8434 // check for non-void type
8435 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8436 if (type->tt_type == VAR_VOID)
8437 {
8438 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8439 return NULL;
8440 }
8441 }
8442
Bram Moolenaarad39c092020-02-26 18:23:43 +01008443 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008444 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008445 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008446 }
8447
Bram Moolenaare4984292020-12-13 14:19:25 +01008448 if (count > 0)
8449 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008450 long save_lnum = cctx->ctx_lnum;
8451
8452 // Use the line number where the command started.
8453 cctx->ctx_lnum = start_ctx_lnum;
8454
Bram Moolenaare4984292020-12-13 14:19:25 +01008455 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8456 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8457 else if (cmdidx == CMD_execute)
8458 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8459 else if (cmdidx == CMD_echomsg)
8460 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8461 else
8462 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008463
8464 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008465 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008466 return p;
8467}
8468
8469/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008470 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008471 * instruction to compute it and return OK.
8472 * Otherwise return FAIL, the caller must deal with any range.
8473 */
8474 static int
8475compile_variable_range(exarg_T *eap, cctx_T *cctx)
8476{
8477 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8478 char_u *p = skipdigits(eap->cmd);
8479
8480 if (p == range_end)
8481 return FAIL;
8482 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8483}
8484
8485/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008486 * :put r
8487 * :put ={expr}
8488 */
8489 static char_u *
8490compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8491{
8492 char_u *line = arg;
8493 linenr_T lnum;
8494 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008495 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008496
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008497 eap->regname = *line;
8498
8499 if (eap->regname == '=')
8500 {
8501 char_u *p = line + 1;
8502
8503 if (compile_expr0(&p, cctx) == FAIL)
8504 return NULL;
8505 line = p;
8506 }
8507 else if (eap->regname != NUL)
8508 ++line;
8509
Bram Moolenaar08597872020-12-10 19:43:40 +01008510 if (compile_variable_range(eap, cctx) == OK)
8511 {
8512 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8513 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008514 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008515 {
8516 // Either no range or a number.
8517 // "errormsg" will not be set because the range is ADDR_LINES.
8518 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008519 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008520 return NULL;
8521 if (eap->addr_count == 0)
8522 lnum = -1;
8523 else
8524 lnum = eap->line2;
8525 if (above)
8526 --lnum;
8527 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008528
8529 generate_PUT(cctx, eap->regname, lnum);
8530 return line;
8531}
8532
8533/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008534 * A command that is not compiled, execute with legacy code.
8535 */
8536 static char_u *
8537compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8538{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008539 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008540 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008541 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008542
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008543 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008544 goto theend;
8545
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008546 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008547 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008548 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008549 int usefilter = FALSE;
8550
8551 has_expr = argt & (EX_XFILE | EX_EXPAND);
8552
8553 // If the command can be followed by a bar, find the bar and truncate
8554 // it, so that the following command can be compiled.
8555 // The '|' is overwritten with a NUL, it is put back below.
8556 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8557 && *eap->arg == '!')
8558 // :w !filter or :r !filter or :r! filter
8559 usefilter = TRUE;
8560 if ((argt & EX_TRLBAR) && !usefilter)
8561 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008562 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008563 separate_nextcmd(eap);
8564 if (eap->nextcmd != NULL)
8565 nextcmd = eap->nextcmd;
8566 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008567 else if (eap->cmdidx == CMD_wincmd)
8568 {
8569 p = eap->arg;
8570 if (*p != NUL)
8571 ++p;
8572 if (*p == 'g' || *p == Ctrl_G)
8573 ++p;
8574 p = skipwhite(p);
8575 if (*p == '|')
8576 {
8577 *p = NUL;
8578 nextcmd = p + 1;
8579 }
8580 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008581 }
8582
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008583 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8584 {
8585 // expand filename in "syntax include [@group] filename"
8586 has_expr = TRUE;
8587 eap->arg = skipwhite(eap->arg + 7);
8588 if (*eap->arg == '@')
8589 eap->arg = skiptowhite(eap->arg);
8590 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008591
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008592 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8593 && STRLEN(eap->arg) > 4)
8594 {
8595 int delim = *eap->arg;
8596
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008597 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008598 if (*p == delim)
8599 {
8600 eap->arg = p + 1;
8601 has_expr = TRUE;
8602 }
8603 }
8604
Bram Moolenaarecac5912021-01-05 19:23:28 +01008605 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8606 {
8607 // TODO: should only expand when appropriate for the command
8608 eap->arg = skiptowhite(eap->arg);
8609 has_expr = TRUE;
8610 }
8611
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008612 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008613 {
8614 int count = 0;
8615 char_u *start = skipwhite(line);
8616
8617 // :cmd xxx`=expr1`yyy`=expr2`zzz
8618 // PUSHS ":cmd xxx"
8619 // eval expr1
8620 // PUSHS "yyy"
8621 // eval expr2
8622 // PUSHS "zzz"
8623 // EXECCONCAT 5
8624 for (;;)
8625 {
8626 if (p > start)
8627 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008628 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008629 ++count;
8630 }
8631 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008632 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008633 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008634 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008635 ++count;
8636 p = skipwhite(p);
8637 if (*p != '`')
8638 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008639 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008640 return NULL;
8641 }
8642 start = p + 1;
8643
8644 p = (char_u *)strstr((char *)start, "`=");
8645 if (p == NULL)
8646 {
8647 if (*skipwhite(start) != NUL)
8648 {
8649 generate_PUSHS(cctx, vim_strsave(start));
8650 ++count;
8651 }
8652 break;
8653 }
8654 }
8655 generate_EXECCONCAT(cctx, count);
8656 }
8657 else
8658 generate_EXEC(cctx, line);
8659
8660theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008661 if (*nextcmd != NUL)
8662 {
8663 // the parser expects a pointer to the bar, put it back
8664 --nextcmd;
8665 *nextcmd = '|';
8666 }
8667
8668 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008669}
8670
Bram Moolenaar20677332021-06-06 17:02:53 +02008671/*
8672 * A script command with heredoc, e.g.
8673 * ruby << EOF
8674 * command
8675 * EOF
8676 * Has been turned into one long line with NL characters by
8677 * get_function_body():
8678 * ruby << EOF<NL> command<NL>EOF
8679 */
8680 static char_u *
8681compile_script(char_u *line, cctx_T *cctx)
8682{
8683 if (cctx->ctx_skip != SKIP_YES)
8684 {
8685 isn_T *isn;
8686
8687 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8688 return NULL;
8689 isn->isn_arg.string = vim_strsave(line);
8690 }
8691 return (char_u *)"";
8692}
8693
Bram Moolenaar8238f082021-04-20 21:10:48 +02008694
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008695/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008696 * :s/pat/repl/
8697 */
8698 static char_u *
8699compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8700{
8701 char_u *cmd = eap->arg;
8702 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8703
8704 if (expr != NULL)
8705 {
8706 int delimiter = *cmd++;
8707
8708 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008709 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008710 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8711 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008712 garray_T save_ga = cctx->ctx_instr;
8713 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008714 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008715 int trailing_error;
8716 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008717 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008718 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008719
8720 cmd += 3;
8721 end = skip_substitute(cmd, delimiter);
8722
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008723 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008724 // labels are correct.
8725 cctx->ctx_instr.ga_len = 0;
8726 cctx->ctx_instr.ga_maxlen = 0;
8727 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008728 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008729 if (end[-1] == NUL)
8730 end[-1] = delimiter;
8731 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008732 trailing_error = *cmd != delimiter && *cmd != NUL;
8733
Bram Moolenaar169502f2021-04-21 12:19:35 +02008734 if (expr_res == FAIL || trailing_error
8735 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008736 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008737 if (trailing_error)
8738 semsg(_(e_trailing_arg), cmd);
8739 clear_instr_ga(&cctx->ctx_instr);
8740 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008741 return NULL;
8742 }
8743
Bram Moolenaar8238f082021-04-20 21:10:48 +02008744 // Move the generated instructions into the ISN_SUBSTITUTE
8745 // instructions, then restore the list of instructions before
8746 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008747 instr_count = cctx->ctx_instr.ga_len;
8748 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008749 instr[instr_count].isn_type = ISN_FINISH;
8750
8751 cctx->ctx_instr = save_ga;
8752 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8753 {
8754 int idx;
8755
8756 for (idx = 0; idx < instr_count; ++idx)
8757 delete_instr(instr + idx);
8758 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008759 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008760 }
8761 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8762 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008763
8764 // skip over flags
8765 if (*end == '&')
8766 ++end;
8767 while (ASCII_ISALPHA(*end) || *end == '#')
8768 ++end;
8769 return end;
8770 }
8771 }
8772
8773 return compile_exec(arg, eap, cctx);
8774}
8775
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008776 static char_u *
8777compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8778{
8779 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008780 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008781
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008782 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008783 {
8784 if (STRNCMP(arg, "END", 3) == 0)
8785 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008786 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008787 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008788 // First load the current variable value.
8789 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8790 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008791 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008792 }
8793
8794 // Gets the redirected text and put it on the stack, then store it
8795 // in the variable.
8796 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8797
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008798 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008799 generate_instr_drop(cctx, ISN_CONCAT, 1);
8800
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008801 if (lhs->lhs_has_index)
8802 {
8803 // Use the info in "lhs" to store the value at the index in the
8804 // list or dict.
8805 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8806 &t_string, cctx) == FAIL)
8807 return NULL;
8808 }
8809 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008810 return NULL;
8811
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008812 VIM_CLEAR(lhs->lhs_name);
8813 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008814 return arg + 3;
8815 }
8816 emsg(_(e_cannot_nest_redir));
8817 return NULL;
8818 }
8819
8820 if (arg[0] == '=' && arg[1] == '>')
8821 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008822 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008823
8824 // redirect to a variable is compiled
8825 arg += 2;
8826 if (*arg == '>')
8827 {
8828 ++arg;
8829 append = TRUE;
8830 }
8831 arg = skipwhite(arg);
8832
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008833 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008834 FALSE, FALSE, 1, cctx) == FAIL)
8835 return NULL;
8836 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008837 lhs->lhs_append = append;
8838 if (lhs->lhs_has_index)
8839 {
8840 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8841 if (lhs->lhs_whole == NULL)
8842 return NULL;
8843 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008844
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008845 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008846 }
8847
8848 // other redirects are handled like at script level
8849 return compile_exec(line, eap, cctx);
8850}
8851
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008852#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008853 static char_u *
8854compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8855{
8856 isn_T *isn;
8857 char_u *p;
8858
8859 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8860 if (isn == NULL)
8861 return NULL;
8862 isn->isn_arg.number = eap->cmdidx;
8863
8864 p = eap->arg;
8865 if (compile_expr0(&p, cctx) == FAIL)
8866 return NULL;
8867
8868 isn = generate_instr(cctx, ISN_CEXPR_CORE);
8869 if (isn == NULL)
8870 return NULL;
8871 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
8872 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
8873 return NULL;
8874 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
8875 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
8876 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
8877
8878 return p;
8879}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008880#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008881
Bram Moolenaar4c137212021-04-19 16:48:48 +02008882/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008883 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008884 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008885 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008886 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008887add_def_function(ufunc_T *ufunc)
8888{
8889 dfunc_T *dfunc;
8890
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008891 if (def_functions.ga_len == 0)
8892 {
8893 // The first position is not used, so that a zero uf_dfunc_idx means it
8894 // wasn't set.
8895 if (ga_grow(&def_functions, 1) == FAIL)
8896 return FAIL;
8897 ++def_functions.ga_len;
8898 }
8899
Bram Moolenaar09689a02020-05-09 22:50:08 +02008900 // Add the function to "def_functions".
8901 if (ga_grow(&def_functions, 1) == FAIL)
8902 return FAIL;
8903 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8904 CLEAR_POINTER(dfunc);
8905 dfunc->df_idx = def_functions.ga_len;
8906 ufunc->uf_dfunc_idx = dfunc->df_idx;
8907 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008908 dfunc->df_name = vim_strsave(ufunc->uf_name);
8909 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008910 ++def_functions.ga_len;
8911 return OK;
8912}
8913
8914/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008915 * After ex_function() has collected all the function lines: parse and compile
8916 * the lines into instructions.
8917 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008918 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8919 * the return statement (used for lambda). When uf_ret_type is already set
8920 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008921 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008922 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008923 * This can be used recursively through compile_lambda(), which may reallocate
8924 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008925 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008926 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008927 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008928compile_def_function(
8929 ufunc_T *ufunc,
8930 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008931 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008932 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008933{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008934 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008935 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008936 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008937 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008938 cctx_T cctx;
8939 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008940 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008941 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008942 int ret = FAIL;
8943 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008944 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02008945 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008946 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008947 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008948#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008949 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008950#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008951
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008952 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008953 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008954 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008955 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008956 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8957 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008958 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008959 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008960 else
8961 {
8962 if (add_def_function(ufunc) == FAIL)
8963 return FAIL;
8964 new_def_function = TRUE;
8965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008966
Bram Moolenaar985116a2020-07-12 17:31:09 +02008967 ufunc->uf_def_status = UF_COMPILING;
8968
Bram Moolenaara80faa82020-04-12 19:37:17 +02008969 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008970
Bram Moolenaarf002a412021-01-24 13:34:18 +01008971#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008972 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008973#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008974 cctx.ctx_ufunc = ufunc;
8975 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008976 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008977 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8978 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8979 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8980 cctx.ctx_type_list = &ufunc->uf_type_list;
8981 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8982 instr = &cctx.ctx_instr;
8983
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008984 // Set the context to the function, it may be compiled when called from
8985 // another script. Set the script version to the most modern one.
8986 // The line number will be set in next_line_from_context().
8987 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008988 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8989
Bram Moolenaardc4c2302021-04-25 13:54:42 +02008990 // Don't use the flag from ":legacy" here.
8991 cmdmod.cmod_flags &= ~CMOD_LEGACY;
8992
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008993 // Make sure error messages are OK.
8994 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8995 if (do_estack_push)
8996 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008997 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008998
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008999 if (ufunc->uf_def_args.ga_len > 0)
9000 {
9001 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009002 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009003 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009004 int i;
9005 char_u *arg;
9006 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009007 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009008
9009 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009010 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009011 for (i = 0; i < count; ++i)
9012 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009013 garray_T *stack = &cctx.ctx_type_stack;
9014 type_T *val_type;
9015 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009016 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009017 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009018 int jump_instr_idx = instr->ga_len;
9019 isn_T *isn;
9020
9021 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9022 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9023 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009024
9025 // Make sure later arguments are not found.
9026 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009027
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009028 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009029 r = compile_expr0(&arg, &cctx);
9030
9031 ufunc->uf_args.ga_len = uf_args_len;
9032 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009033 goto erret;
9034
9035 // If no type specified use the type of the default value.
9036 // Otherwise check that the default value type matches the
9037 // specified type.
9038 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009039 where.wt_index = arg_idx + 1;
9040 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009041 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009042 {
9043 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009044 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009045 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009046 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009047 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009048 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009049
9050 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009051 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009052
9053 // set instruction index in JUMP_IF_ARG_SET to here
9054 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9055 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009056 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009057
9058 if (did_set_arg_type)
9059 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009060 }
9061
9062 /*
9063 * Loop over all the lines of the function and generate instructions.
9064 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009065 for (;;)
9066 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009067 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009068 int starts_with_colon = FALSE;
9069 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009070 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009071
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009072 // Bail out on the first error to avoid a flood of errors and report
9073 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009074 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009075 goto erret;
9076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009077 if (line != NULL && *line == '|')
9078 // the line continues after a '|'
9079 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009080 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009081 && !(*line == '#' && (line == cctx.ctx_line_start
9082 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009083 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009084 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009085 goto erret;
9086 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009087 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9088 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009089 else
9090 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009091 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009092 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009093 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009094 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009095#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009096 if (cctx.ctx_skip != SKIP_YES)
9097 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009098#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009099 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009100 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009101 // Make a copy, splitting off nextcmd and removing trailing spaces
9102 // may change it.
9103 if (line != NULL)
9104 {
9105 line = vim_strsave(line);
9106 vim_free(line_to_free);
9107 line_to_free = line;
9108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009109 }
9110
Bram Moolenaara80faa82020-04-12 19:37:17 +02009111 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009112 ea.cmdlinep = &line;
9113 ea.cmd = skipwhite(line);
9114
Bram Moolenaarb2049902021-01-24 12:53:53 +01009115 if (*ea.cmd == '#')
9116 {
9117 // "#" starts a comment
9118 line = (char_u *)"";
9119 continue;
9120 }
9121
Bram Moolenaarf002a412021-01-24 13:34:18 +01009122#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009123 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
9124 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009125 {
9126 may_generate_prof_end(&cctx, prof_lnum);
9127
9128 prof_lnum = cctx.ctx_lnum;
9129 generate_instr(&cctx, ISN_PROF_START);
9130 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009131#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009132
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009133 // Some things can be recognized by the first character.
9134 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009135 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009136 case '}':
9137 {
9138 // "}" ends a block scope
9139 scopetype_T stype = cctx.ctx_scope == NULL
9140 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009141
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009142 if (stype == BLOCK_SCOPE)
9143 {
9144 compile_endblock(&cctx);
9145 line = ea.cmd;
9146 }
9147 else
9148 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009149 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009150 goto erret;
9151 }
9152 if (line != NULL)
9153 line = skipwhite(ea.cmd + 1);
9154 continue;
9155 }
9156
9157 case '{':
9158 // "{" starts a block scope
9159 // "{'a': 1}->func() is something else
9160 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9161 {
9162 line = compile_block(ea.cmd, &cctx);
9163 continue;
9164 }
9165 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009166 }
9167
9168 /*
9169 * COMMAND MODIFIERS
9170 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009171 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009172 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9173 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009174 {
9175 if (errormsg != NULL)
9176 goto erret;
9177 // empty line or comment
9178 line = (char_u *)"";
9179 continue;
9180 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009181 generate_cmdmods(&cctx, &local_cmdmod);
9182 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009183
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009184 // Check if there was a colon after the last command modifier or before
9185 // the current position.
9186 for (p = ea.cmd; p >= line; --p)
9187 {
9188 if (*p == ':')
9189 starts_with_colon = TRUE;
9190 if (p < ea.cmd && !VIM_ISWHITE(*p))
9191 break;
9192 }
9193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009194 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009195 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009196 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009197 {
9198 if (*ea.cmd == '(')
9199 // not for "call()"
9200 ea.cmd = p;
9201 else
9202 ea.cmd = skipwhite(ea.cmd);
9203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009204
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009205 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009206 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009207 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009208
Bram Moolenaar17126b12021-01-07 22:03:02 +01009209 // Check for assignment after command modifiers.
9210 assign = may_compile_assignment(&ea, &line, &cctx);
9211 if (assign == OK)
9212 goto nextline;
9213 if (assign == FAIL)
9214 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009215 }
9216
9217 /*
9218 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009219 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009220 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009221 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009222 cmd = ea.cmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009223 if (starts_with_colon || !(*cmd == '\''
9224 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009225 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009226 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009227 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009228 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009229 if (!starts_with_colon)
9230 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009231 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009232 goto erret;
9233 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009234 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009235 if (ends_excmd2(line, ea.cmd))
9236 {
9237 // A range without a command: jump to the line.
9238 // TODO: compile to a more efficient command, possibly
9239 // calling parse_cmd_address().
9240 ea.cmdidx = CMD_SIZE;
9241 line = compile_exec(line, &ea, &cctx);
9242 goto nextline;
9243 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009244 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009245 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009246 p = find_ex_command(&ea, NULL, starts_with_colon
9247 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009248
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009249 if (p == NULL)
9250 {
9251 if (cctx.ctx_skip != SKIP_YES)
9252 emsg(_(e_ambiguous_use_of_user_defined_command));
9253 goto erret;
9254 }
9255
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009256 // When using ":legacy cmd" always use compile_exec().
9257 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009258 {
9259 char_u *start = ea.cmd;
9260
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009261 switch (ea.cmdidx)
9262 {
9263 case CMD_if:
9264 case CMD_elseif:
9265 case CMD_else:
9266 case CMD_endif:
9267 case CMD_for:
9268 case CMD_endfor:
9269 case CMD_continue:
9270 case CMD_break:
9271 case CMD_while:
9272 case CMD_endwhile:
9273 case CMD_try:
9274 case CMD_catch:
9275 case CMD_finally:
9276 case CMD_endtry:
9277 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9278 goto erret;
9279 default: break;
9280 }
9281
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009282 // ":legacy return expr" needs to be handled differently.
9283 if (checkforcmd(&start, "return", 4))
9284 ea.cmdidx = CMD_return;
9285 else
9286 ea.cmdidx = CMD_legacy;
9287 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009289 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9290 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009291 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009292 {
9293 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009294 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009295 }
9296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009297 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009298 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009299 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009300 // CMD_var cannot happen, compile_assignment() above would be
9301 // used. Most likely an assignment to a non-existing variable.
9302 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009303 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009305 }
9306
Bram Moolenaar3988f642020-08-27 22:43:03 +02009307 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009308 && ea.cmdidx != CMD_elseif
9309 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009310 && ea.cmdidx != CMD_endif
9311 && ea.cmdidx != CMD_endfor
9312 && ea.cmdidx != CMD_endwhile
9313 && ea.cmdidx != CMD_catch
9314 && ea.cmdidx != CMD_finally
9315 && ea.cmdidx != CMD_endtry)
9316 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009317 emsg(_(e_unreachable_code_after_return));
9318 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009319 }
9320
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009321 p = skipwhite(p);
9322 if (ea.cmdidx != CMD_SIZE
9323 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9324 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009325 if (ea.cmdidx >= 0)
9326 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009327 if ((ea.argt & EX_BANG) && *p == '!')
9328 {
9329 ea.forceit = TRUE;
9330 p = skipwhite(p + 1);
9331 }
9332 }
9333
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009334 switch (ea.cmdidx)
9335 {
9336 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009337 ea.arg = p;
9338 line = compile_nested_function(&ea, &cctx);
9339 break;
9340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009341 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009342 // TODO: should we allow this, e.g. to declare a global
9343 // function?
9344 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009345 goto erret;
9346
9347 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009348 line = compile_return(p, check_return_type,
9349 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009350 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009351 break;
9352
9353 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009354 emsg(_(e_cannot_use_let_in_vim9_script));
9355 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009356 case CMD_var:
9357 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009358 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009359 case CMD_increment:
9360 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009361 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009362 if (line == p)
9363 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009364 break;
9365
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009366 case CMD_unlet:
9367 case CMD_unlockvar:
9368 case CMD_lockvar:
9369 line = compile_unletlock(p, &ea, &cctx);
9370 break;
9371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009372 case CMD_import:
9373 line = compile_import(p, &cctx);
9374 break;
9375
9376 case CMD_if:
9377 line = compile_if(p, &cctx);
9378 break;
9379 case CMD_elseif:
9380 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009381 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009382 break;
9383 case CMD_else:
9384 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009385 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009386 break;
9387 case CMD_endif:
9388 line = compile_endif(p, &cctx);
9389 break;
9390
9391 case CMD_while:
9392 line = compile_while(p, &cctx);
9393 break;
9394 case CMD_endwhile:
9395 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009396 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009397 break;
9398
9399 case CMD_for:
9400 line = compile_for(p, &cctx);
9401 break;
9402 case CMD_endfor:
9403 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009404 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009405 break;
9406 case CMD_continue:
9407 line = compile_continue(p, &cctx);
9408 break;
9409 case CMD_break:
9410 line = compile_break(p, &cctx);
9411 break;
9412
9413 case CMD_try:
9414 line = compile_try(p, &cctx);
9415 break;
9416 case CMD_catch:
9417 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009418 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009419 break;
9420 case CMD_finally:
9421 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009422 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009423 break;
9424 case CMD_endtry:
9425 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009426 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009427 break;
9428 case CMD_throw:
9429 line = compile_throw(p, &cctx);
9430 break;
9431
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009432 case CMD_eval:
9433 if (compile_expr0(&p, &cctx) == FAIL)
9434 goto erret;
9435
Bram Moolenaar3988f642020-08-27 22:43:03 +02009436 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009437 generate_instr_drop(&cctx, ISN_DROP, 1);
9438
9439 line = skipwhite(p);
9440 break;
9441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009442 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009443 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009444 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009445 case CMD_echomsg:
9446 case CMD_echoerr:
9447 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009448 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009449
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009450 case CMD_put:
9451 ea.cmd = cmd;
9452 line = compile_put(p, &ea, &cctx);
9453 break;
9454
Bram Moolenaar4c137212021-04-19 16:48:48 +02009455 case CMD_substitute:
9456 if (cctx.ctx_skip == SKIP_YES)
9457 line = (char_u *)"";
9458 else
9459 {
9460 ea.arg = p;
9461 line = compile_substitute(line, &ea, &cctx);
9462 }
9463 break;
9464
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009465 case CMD_redir:
9466 ea.arg = p;
9467 line = compile_redir(line, &ea, &cctx);
9468 break;
9469
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009470 case CMD_cexpr:
9471 case CMD_lexpr:
9472 case CMD_caddexpr:
9473 case CMD_laddexpr:
9474 case CMD_cgetexpr:
9475 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009476#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009477 ea.arg = p;
9478 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009479#else
9480 ex_ni(&ea);
9481 line = NULL;
9482#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009483 break;
9484
Bram Moolenaar3988f642020-08-27 22:43:03 +02009485 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009486
Bram Moolenaarae616492020-07-28 20:07:27 +02009487 case CMD_append:
9488 case CMD_change:
9489 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009490 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009491 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009492 case CMD_xit:
9493 not_in_vim9(&ea);
9494 goto erret;
9495
Bram Moolenaar002262f2020-07-08 17:47:57 +02009496 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009497 if (cctx.ctx_skip != SKIP_YES)
9498 {
9499 semsg(_(e_invalid_command_str), ea.cmd);
9500 goto erret;
9501 }
9502 // We don't check for a next command here.
9503 line = (char_u *)"";
9504 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009505
Bram Moolenaar20677332021-06-06 17:02:53 +02009506 case CMD_lua:
9507 case CMD_mzscheme:
9508 case CMD_perl:
9509 case CMD_py3:
9510 case CMD_python3:
9511 case CMD_python:
9512 case CMD_pythonx:
9513 case CMD_ruby:
9514 case CMD_tcl:
9515 ea.arg = p;
9516 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009517 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009518 else
9519 // heredoc lines have been concatenated with NL
9520 // characters in get_function_body()
9521 line = compile_script(line, &cctx);
9522 break;
9523
9524 default:
9525 // Not recognized, execute with do_cmdline_cmd().
9526 ea.arg = p;
9527 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009528 break;
9529 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009530nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009531 if (line == NULL)
9532 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009533 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009534
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009535 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009536 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009537
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009538 if (cctx.ctx_type_stack.ga_len < 0)
9539 {
9540 iemsg("Type stack underflow");
9541 goto erret;
9542 }
9543 }
9544
9545 if (cctx.ctx_scope != NULL)
9546 {
9547 if (cctx.ctx_scope->se_type == IF_SCOPE)
9548 emsg(_(e_endif));
9549 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9550 emsg(_(e_endwhile));
9551 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9552 emsg(_(e_endfor));
9553 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009554 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009555 goto erret;
9556 }
9557
Bram Moolenaarefd88552020-06-18 20:50:10 +02009558 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009559 {
9560 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
9561 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009562 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009563 goto erret;
9564 }
9565
9566 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01009567 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009568 }
9569
Bram Moolenaar599410c2021-04-10 14:03:43 +02009570 // When compiled with ":silent!" and there was an error don't consider the
9571 // function compiled.
9572 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009573 {
9574 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9575 + ufunc->uf_dfunc_idx;
9576 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009577 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009578#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009579 if (cctx.ctx_profiling)
9580 {
9581 dfunc->df_instr_prof = instr->ga_data;
9582 dfunc->df_instr_prof_count = instr->ga_len;
9583 }
9584 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009585#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009586 {
9587 dfunc->df_instr = instr->ga_data;
9588 dfunc->df_instr_count = instr->ga_len;
9589 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009590 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009591 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009592 if (cctx.ctx_outer_used)
9593 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009594 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009595 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009596
9597 ret = OK;
9598
9599erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009600 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009601 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009602 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9603 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009604
Bram Moolenaar8238f082021-04-20 21:10:48 +02009605 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009606 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009607
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009608 // If using the last entry in the table and it was added above, we
9609 // might as well remove it.
9610 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009611 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009612 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009613 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009614 ufunc->uf_dfunc_idx = 0;
9615 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009616 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009617
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009618 while (cctx.ctx_scope != NULL)
9619 drop_scope(&cctx);
9620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009621 if (errormsg != NULL)
9622 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009623 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009624 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009625 }
9626
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009627 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9628 {
9629 if (ret == OK)
9630 {
9631 emsg(_(e_missing_redir_end));
9632 ret = FAIL;
9633 }
9634 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009635 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009636 }
9637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009638 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009639 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009640 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009641 if (do_estack_push)
9642 estack_pop();
9643
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009644 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009645 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009646 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009647 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009648 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009649}
9650
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009651 void
9652set_function_type(ufunc_T *ufunc)
9653{
9654 int varargs = ufunc->uf_va_name != NULL;
9655 int argcount = ufunc->uf_args.ga_len;
9656
9657 // Create a type for the function, with the return type and any
9658 // argument types.
9659 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9660 // The type is included in "tt_args".
9661 if (argcount > 0 || varargs)
9662 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009663 if (ufunc->uf_type_list.ga_itemsize == 0)
9664 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009665 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9666 argcount, &ufunc->uf_type_list);
9667 // Add argument types to the function type.
9668 if (func_type_add_arg_types(ufunc->uf_func_type,
9669 argcount + varargs,
9670 &ufunc->uf_type_list) == FAIL)
9671 return;
9672 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9673 ufunc->uf_func_type->tt_min_argcount =
9674 argcount - ufunc->uf_def_args.ga_len;
9675 if (ufunc->uf_arg_types == NULL)
9676 {
9677 int i;
9678
9679 // lambda does not have argument types.
9680 for (i = 0; i < argcount; ++i)
9681 ufunc->uf_func_type->tt_args[i] = &t_any;
9682 }
9683 else
9684 mch_memmove(ufunc->uf_func_type->tt_args,
9685 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9686 if (varargs)
9687 {
9688 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009689 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009690 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9691 }
9692 }
9693 else
9694 // No arguments, can use a predefined type.
9695 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9696 argcount, &ufunc->uf_type_list);
9697}
9698
9699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009700/*
9701 * Delete an instruction, free what it contains.
9702 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009703 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009704delete_instr(isn_T *isn)
9705{
9706 switch (isn->isn_type)
9707 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009708 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009709 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009710 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009711 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009712 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009713 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009714 case ISN_LOADENV:
9715 case ISN_LOADG:
9716 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009717 case ISN_LOADT:
9718 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009719 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009720 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009721 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009722 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009723 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009724 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009725 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009726 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009727 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009728 case ISN_STOREW:
9729 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009730 vim_free(isn->isn_arg.string);
9731 break;
9732
Bram Moolenaar4c137212021-04-19 16:48:48 +02009733 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009734 {
9735 int idx;
9736 isn_T *list = isn->isn_arg.subs.subs_instr;
9737
9738 vim_free(isn->isn_arg.subs.subs_cmd);
9739 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9740 delete_instr(list + idx);
9741 vim_free(list);
9742 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009743 break;
9744
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009745 case ISN_INSTR:
9746 {
9747 int idx;
9748 isn_T *list = isn->isn_arg.instr;
9749
9750 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9751 delete_instr(list + idx);
9752 vim_free(list);
9753 }
9754 break;
9755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009756 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009757 case ISN_STORES:
9758 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009759 break;
9760
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009761 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009762 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009763 vim_free(isn->isn_arg.unlet.ul_name);
9764 break;
9765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009766 case ISN_STOREOPT:
9767 vim_free(isn->isn_arg.storeopt.so_name);
9768 break;
9769
9770 case ISN_PUSHBLOB: // push blob isn_arg.blob
9771 blob_unref(isn->isn_arg.blob);
9772 break;
9773
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009774 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009775#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009776 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009777#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009778 break;
9779
9780 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009781#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009782 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009783#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009784 break;
9785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009786 case ISN_UCALL:
9787 vim_free(isn->isn_arg.ufunc.cuf_name);
9788 break;
9789
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009790 case ISN_FUNCREF:
9791 {
9792 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9793 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009794 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009795
Bram Moolenaar077a4232020-12-22 18:33:27 +01009796 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9797 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009798 }
9799 break;
9800
9801 case ISN_DCALL:
9802 {
9803 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9804 + isn->isn_arg.dfunc.cdf_idx;
9805
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009806 if (dfunc->df_ufunc != NULL
9807 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009808 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009809 }
9810 break;
9811
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009812 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009813 {
9814 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9815 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9816
9817 if (ufunc != NULL)
9818 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009819 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009820 func_ptr_unref(ufunc);
9821 }
9822
9823 vim_free(lambda);
9824 vim_free(isn->isn_arg.newfunc.nf_global);
9825 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009826 break;
9827
Bram Moolenaar5e654232020-09-16 15:22:00 +02009828 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009829 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009830 free_type(isn->isn_arg.type.ct_type);
9831 break;
9832
Bram Moolenaar02194d22020-10-24 23:08:38 +02009833 case ISN_CMDMOD:
9834 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9835 ->cmod_filter_regmatch.regprog);
9836 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9837 break;
9838
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009839 case ISN_LOADSCRIPT:
9840 case ISN_STORESCRIPT:
9841 vim_free(isn->isn_arg.script.scriptref);
9842 break;
9843
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009844 case ISN_TRY:
9845 vim_free(isn->isn_arg.try.try_ref);
9846 break;
9847
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009848 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +02009849 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009850 vim_free(isn->isn_arg.cexpr.cexpr_ref);
9851 break;
9852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009853 case ISN_2BOOL:
9854 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009855 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009856 case ISN_ADDBLOB:
9857 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009858 case ISN_ANYINDEX:
9859 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009860 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009861 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009862 case ISN_BLOBINDEX:
9863 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009864 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009865 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009866 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009867 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009868 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009869 case ISN_COMPAREANY:
9870 case ISN_COMPAREBLOB:
9871 case ISN_COMPAREBOOL:
9872 case ISN_COMPAREDICT:
9873 case ISN_COMPAREFLOAT:
9874 case ISN_COMPAREFUNC:
9875 case ISN_COMPARELIST:
9876 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009877 case ISN_COMPARESPECIAL:
9878 case ISN_COMPARESTRING:
9879 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009880 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009881 case ISN_DROP:
9882 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009883 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009884 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009885 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009886 case ISN_EXECCONCAT:
9887 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009888 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009889 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009890 case ISN_GETITEM:
9891 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009892 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009893 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009894 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009895 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009896 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009897 case ISN_LOADBDICT:
9898 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009899 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009900 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009901 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009902 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009903 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009904 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009905 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009906 case ISN_NEGATENR:
9907 case ISN_NEWDICT:
9908 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009909 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009910 case ISN_OPFLOAT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009911 case ISN_FINISH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009912 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009913 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009914 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009915 case ISN_PROF_END:
9916 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009917 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009918 case ISN_PUSHF:
9919 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009920 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009921 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009922 case ISN_REDIREND:
9923 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009924 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009925 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009926 case ISN_SHUFFLE:
9927 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009928 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009929 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009930 case ISN_STORENR:
9931 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009932 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009933 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009934 case ISN_STOREV:
9935 case ISN_STRINDEX:
9936 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009937 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009938 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009939 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009940 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009941 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009942 // nothing allocated
9943 break;
9944 }
9945}
9946
9947/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009948 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009949 */
9950 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009951delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009952{
9953 int idx;
9954
9955 ga_clear(&dfunc->df_def_args_isn);
9956
9957 if (dfunc->df_instr != NULL)
9958 {
9959 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9960 delete_instr(dfunc->df_instr + idx);
9961 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009962 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009963 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009964#ifdef FEAT_PROFILE
9965 if (dfunc->df_instr_prof != NULL)
9966 {
9967 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9968 delete_instr(dfunc->df_instr_prof + idx);
9969 VIM_CLEAR(dfunc->df_instr_prof);
9970 dfunc->df_instr_prof = NULL;
9971 }
9972#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009973
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009974 if (mark_deleted)
9975 dfunc->df_deleted = TRUE;
9976 if (dfunc->df_ufunc != NULL)
9977 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009978}
9979
9980/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009981 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009982 * function, unless another user function still uses it.
9983 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009984 */
9985 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009986unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009987{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009988 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009989 {
9990 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9991 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009992
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009993 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009994 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009995 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009996 ufunc->uf_dfunc_idx = 0;
9997 if (dfunc->df_ufunc == ufunc)
9998 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009999 }
10000}
10001
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010002/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010003 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010004 */
10005 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010006link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010007{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010008 if (ufunc->uf_dfunc_idx > 0)
10009 {
10010 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10011 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010012
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010013 ++dfunc->df_refcount;
10014 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010015}
10016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010017#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010018/*
10019 * Free all functions defined with ":def".
10020 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010021 void
10022free_def_functions(void)
10023{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010024 int idx;
10025
10026 for (idx = 0; idx < def_functions.ga_len; ++idx)
10027 {
10028 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10029
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010030 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010031 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010032 }
10033
10034 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010035}
10036#endif
10037
10038
10039#endif // FEAT_EVAL