blob: 47847af3aaa1553cab77cd608117edc6e3c37cb7 [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 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02001953 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1954 {
1955 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1956 ufunc->uf_name);
1957 return FAIL;
1958 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001961 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001962 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001964 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 {
1966 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1967 isn->isn_arg.dfunc.cdf_argcount = argcount;
1968 }
1969 else
1970 {
1971 // A user function may be deleted and redefined later, can't use the
1972 // ufunc pointer, need to look it up again at runtime.
1973 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1974 isn->isn_arg.ufunc.cuf_argcount = argcount;
1975 }
1976
1977 stack->ga_len -= argcount; // drop the arguments
1978 if (ga_grow(stack, 1) == FAIL)
1979 return FAIL;
1980 // add return value
1981 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1982 ++stack->ga_len;
1983
1984 return OK;
1985}
1986
1987/*
1988 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1989 */
1990 static int
1991generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1992{
1993 isn_T *isn;
1994 garray_T *stack = &cctx->ctx_type_stack;
1995
Bram Moolenaar080457c2020-03-03 21:53:32 +01001996 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1998 return FAIL;
1999 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2000 isn->isn_arg.ufunc.cuf_argcount = argcount;
2001
2002 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002003 if (ga_grow(stack, 1) == FAIL)
2004 return FAIL;
2005 // add return value
2006 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2007 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008
2009 return OK;
2010}
2011
2012/*
2013 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002014 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 */
2016 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002017generate_PCALL(
2018 cctx_T *cctx,
2019 int argcount,
2020 char_u *name,
2021 type_T *type,
2022 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023{
2024 isn_T *isn;
2025 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002026 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027
Bram Moolenaar080457c2020-03-03 21:53:32 +01002028 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002029
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002030 if (type->tt_type == VAR_ANY)
2031 ret_type = &t_any;
2032 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002033 {
2034 if (type->tt_argcount != -1)
2035 {
2036 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2037
2038 if (argcount < type->tt_min_argcount - varargs)
2039 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002040 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002041 return FAIL;
2042 }
2043 if (!varargs && argcount > type->tt_argcount)
2044 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002045 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002046 return FAIL;
2047 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002048 if (type->tt_args != NULL)
2049 {
2050 int i;
2051
2052 for (i = 0; i < argcount; ++i)
2053 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002054 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002055 type_T *actual = ((type_T **)stack->ga_data)[
2056 stack->ga_len + offset];
2057 type_T *expected;
2058
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002059 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002060 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002061 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002062 else if (i >= type->tt_min_argcount
2063 && actual == &t_special)
2064 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002065 else
2066 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002067 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002068 cctx, TRUE, FALSE) == FAIL)
2069 {
2070 arg_type_mismatch(expected, actual, i + 1);
2071 return FAIL;
2072 }
2073 }
2074 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002075 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002076 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002077 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002078 else
2079 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002080 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002081 return FAIL;
2082 }
2083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2085 return FAIL;
2086 isn->isn_arg.pfunc.cpf_top = at_top;
2087 isn->isn_arg.pfunc.cpf_argcount = argcount;
2088
2089 stack->ga_len -= argcount; // drop the arguments
2090
2091 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002092 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002094 // If partial is above the arguments it must be cleared and replaced with
2095 // the return value.
2096 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2097 return FAIL;
2098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 return OK;
2100}
2101
2102/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002103 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 */
2105 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002106generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107{
2108 isn_T *isn;
2109 garray_T *stack = &cctx->ctx_type_stack;
2110 type_T *type;
2111
Bram Moolenaar080457c2020-03-03 21:53:32 +01002112 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002113 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002115 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002117 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002119 if (type->tt_type != VAR_DICT && type != &t_any)
2120 {
2121 emsg(_(e_dictreq));
2122 return FAIL;
2123 }
2124 // change dict type to dict member type
2125 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002126 {
2127 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2128 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130
2131 return OK;
2132}
2133
2134/*
2135 * Generate an ISN_ECHO instruction.
2136 */
2137 static int
2138generate_ECHO(cctx_T *cctx, int with_white, int count)
2139{
2140 isn_T *isn;
2141
Bram Moolenaar080457c2020-03-03 21:53:32 +01002142 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2144 return FAIL;
2145 isn->isn_arg.echo.echo_with_white = with_white;
2146 isn->isn_arg.echo.echo_count = count;
2147
2148 return OK;
2149}
2150
Bram Moolenaarad39c092020-02-26 18:23:43 +01002151/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002152 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002153 */
2154 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002155generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002156{
2157 isn_T *isn;
2158
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002159 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002160 return FAIL;
2161 isn->isn_arg.number = count;
2162
2163 return OK;
2164}
2165
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002166/*
2167 * Generate an ISN_PUT instruction.
2168 */
2169 static int
2170generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2171{
2172 isn_T *isn;
2173
2174 RETURN_OK_IF_SKIP(cctx);
2175 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2176 return FAIL;
2177 isn->isn_arg.put.put_regname = regname;
2178 isn->isn_arg.put.put_lnum = lnum;
2179 return OK;
2180}
2181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 static int
2183generate_EXEC(cctx_T *cctx, char_u *line)
2184{
2185 isn_T *isn;
2186
Bram Moolenaar080457c2020-03-03 21:53:32 +01002187 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2189 return FAIL;
2190 isn->isn_arg.string = vim_strsave(line);
2191 return OK;
2192}
2193
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002194 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002195generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2196{
2197 isn_T *isn;
2198 garray_T *stack = &cctx->ctx_type_stack;
2199
2200 RETURN_OK_IF_SKIP(cctx);
2201 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2202 return FAIL;
2203 isn->isn_arg.string = vim_strsave(line);
2204
2205 if (ga_grow(stack, 1) == FAIL)
2206 return FAIL;
2207 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2208 ++stack->ga_len;
2209
2210 return OK;
2211}
2212
2213 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002214generate_EXECCONCAT(cctx_T *cctx, int count)
2215{
2216 isn_T *isn;
2217
2218 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2219 return FAIL;
2220 isn->isn_arg.number = count;
2221 return OK;
2222}
2223
Bram Moolenaar08597872020-12-10 19:43:40 +01002224/*
2225 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2226 */
2227 static int
2228generate_RANGE(cctx_T *cctx, char_u *range)
2229{
2230 isn_T *isn;
2231 garray_T *stack = &cctx->ctx_type_stack;
2232
2233 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2234 return FAIL;
2235 isn->isn_arg.string = range;
2236
2237 if (ga_grow(stack, 1) == FAIL)
2238 return FAIL;
2239 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2240 ++stack->ga_len;
2241 return OK;
2242}
2243
Bram Moolenaar792f7862020-11-23 08:31:18 +01002244 static int
2245generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2246{
2247 isn_T *isn;
2248
2249 RETURN_OK_IF_SKIP(cctx);
2250 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2251 return FAIL;
2252 isn->isn_arg.unpack.unp_count = var_count;
2253 isn->isn_arg.unpack.unp_semicolon = semicolon;
2254 return OK;
2255}
2256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002258 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002259 */
2260 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002261generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002262{
2263 isn_T *isn;
2264
Bram Moolenaarfa984412021-03-25 22:15:28 +01002265 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002266 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002267 cctx->ctx_has_cmdmod = TRUE;
2268
2269 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002270 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002271 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2272 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2273 return FAIL;
2274 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002275 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002276 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002277 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002278
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002279 return OK;
2280}
2281
2282 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002283generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002284{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002285 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2286 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002287 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002288 return OK;
2289}
2290
Bram Moolenaarfa984412021-03-25 22:15:28 +01002291 static int
2292misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002293{
2294 garray_T *instr = &cctx->ctx_instr;
2295
Bram Moolenaara91a7132021-03-25 21:12:15 +01002296 if (cctx->ctx_has_cmdmod
2297 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2298 == ISN_CMDMOD)
2299 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002300 emsg(_(e_misplaced_command_modifier));
2301 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002302 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002303 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002304}
2305
2306/*
2307 * Get the index of the current instruction.
2308 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2309 */
2310 static int
2311current_instr_idx(cctx_T *cctx)
2312{
2313 garray_T *instr = &cctx->ctx_instr;
2314 int idx = instr->ga_len;
2315
2316 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2317 .isn_type == ISN_CMDMOD)
2318 --idx;
2319#ifdef FEAT_PROFILE
2320 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2321 .isn_type == ISN_PROF_START)
2322 --idx;
2323#endif
2324 return idx;
2325}
2326
Bram Moolenaarf002a412021-01-24 13:34:18 +01002327#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002328 static void
2329may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2330{
2331 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002332 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002333}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002334#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002335
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002336/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002338 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002340 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002341reserve_local(
2342 cctx_T *cctx,
2343 char_u *name,
2344 size_t len,
2345 int isConst,
2346 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 lvar_T *lvar;
2349
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002350 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002352 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002353 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 }
2355
2356 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002357 return NULL;
2358 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002359 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002360
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002361 // Every local variable uses the next entry on the stack. We could re-use
2362 // the last ones when leaving a scope, but then variables used in a closure
2363 // might get overwritten. To keep things simple do not re-use stack
2364 // entries. This is less efficient, but memory is cheap these days.
2365 lvar->lv_idx = cctx->ctx_locals_count++;
2366
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002367 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 lvar->lv_const = isConst;
2369 lvar->lv_type = type;
2370
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002371 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372}
2373
2374/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002375 * Remove local variables above "new_top".
2376 */
2377 static void
2378unwind_locals(cctx_T *cctx, int new_top)
2379{
2380 if (cctx->ctx_locals.ga_len > new_top)
2381 {
2382 int idx;
2383 lvar_T *lvar;
2384
2385 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2386 {
2387 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2388 vim_free(lvar->lv_name);
2389 }
2390 }
2391 cctx->ctx_locals.ga_len = new_top;
2392}
2393
2394/*
2395 * Free all local variables.
2396 */
2397 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002398free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002399{
2400 unwind_locals(cctx, 0);
2401 ga_clear(&cctx->ctx_locals);
2402}
2403
2404/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002405 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2406 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2407 * error if the variable was defined with :const.
2408 */
2409 static int
2410check_item_writable(svar_T *sv, int check_writable, char_u *name)
2411{
2412 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2413 || (check_writable == ASSIGN_FINAL
2414 && sv->sv_const == ASSIGN_CONST))
2415 {
2416 semsg(_(e_readonlyvar), name);
2417 return FAIL;
2418 }
2419 return OK;
2420}
2421
2422/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002424 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 * Returns the index in "sn_var_vals" if found.
2426 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002427 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 */
2429 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002430get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431{
2432 hashtab_T *ht;
2433 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002434 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002435 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 int idx;
2437
Bram Moolenaare3d46852020-08-29 13:39:17 +02002438 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002440 if (sid == current_sctx.sc_sid)
2441 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002442 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002443
2444 if (sav == NULL)
2445 return -2;
2446 idx = sav->sav_var_vals_idx;
2447 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002448 if (check_item_writable(sv, check_writable, name) == FAIL)
2449 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002450 return idx;
2451 }
2452
2453 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 ht = &SCRIPT_VARS(sid);
2455 di = find_var_in_ht(ht, 0, name, TRUE);
2456 if (di == NULL)
2457 return -2;
2458
2459 // Now find the svar_T index in sn_var_vals.
2460 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2461 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002462 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 if (sv->sv_tv == &di->di_tv)
2464 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002465 if (check_item_writable(sv, check_writable, name) == FAIL)
2466 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 return idx;
2468 }
2469 }
2470 return -1;
2471}
2472
2473/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002474 * Find "name" in imported items of the current script or in "cctx" if not
2475 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 */
2477 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002478find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 int idx;
2481
Bram Moolenaare3d46852020-08-29 13:39:17 +02002482 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002483 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 if (cctx != NULL)
2485 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2486 {
2487 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2488 + idx;
2489
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002490 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2491 : STRLEN(import->imp_name) == len
2492 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 return import;
2494 }
2495
Bram Moolenaarefa94442020-08-08 22:16:00 +02002496 return find_imported_in_script(name, len, current_sctx.sc_sid);
2497}
2498
2499 imported_T *
2500find_imported_in_script(char_u *name, size_t len, int sid)
2501{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002502 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002503 int idx;
2504
Bram Moolenaare3d46852020-08-29 13:39:17 +02002505 if (!SCRIPT_ID_VALID(sid))
2506 return NULL;
2507 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2509 {
2510 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2511
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002512 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2513 : STRLEN(import->imp_name) == len
2514 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 return import;
2516 }
2517 return NULL;
2518}
2519
2520/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002521 * Free all imported variables.
2522 */
2523 static void
2524free_imported(cctx_T *cctx)
2525{
2526 int idx;
2527
2528 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2529 {
2530 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2531
2532 vim_free(import->imp_name);
2533 }
2534 ga_clear(&cctx->ctx_imports);
2535}
2536
2537/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002538 * Return a pointer to the next line that isn't empty or only contains a
2539 * comment. Skips over white space.
2540 * Returns NULL if there is none.
2541 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002542 char_u *
2543peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002544{
2545 int lnum = cctx->ctx_lnum;
2546
2547 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2548 {
2549 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002550 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002551
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002552 // ignore NULLs inserted for continuation lines
2553 if (line != NULL)
2554 {
2555 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002556 if (vim9_bad_comment(p))
2557 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002558 if (*p != NUL && !vim9_comment_start(p))
2559 return p;
2560 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002561 }
2562 return NULL;
2563}
2564
2565/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002566 * Called when checking for a following operator at "arg". When the rest of
2567 * the line is empty or only a comment, peek the next line. If there is a next
2568 * line return a pointer to it and set "nextp".
2569 * Otherwise skip over white space.
2570 */
2571 static char_u *
2572may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2573{
2574 char_u *p = skipwhite(arg);
2575
2576 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002577 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002578 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002579 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002580 if (*nextp != NULL)
2581 return *nextp;
2582 }
2583 return p;
2584}
2585
2586/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002587 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002588 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002589 * Returns NULL when at the end.
2590 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002591 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002592next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002593{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002594 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002595
2596 do
2597 {
2598 ++cctx->ctx_lnum;
2599 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002600 {
2601 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002602 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002603 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002604 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002605 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002606 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002607 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002608 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002609 return line;
2610}
2611
2612/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002613 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002614 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002615 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002616 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2617 */
2618 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002619may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002620{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002621 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002622 if (vim9_bad_comment(*arg))
2623 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002624 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002625 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002626 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002627
2628 if (next == NULL)
2629 return FAIL;
2630 *arg = skipwhite(next);
2631 }
2632 return OK;
2633}
2634
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002635/*
2636 * Idem, and give an error when failed.
2637 */
2638 static int
2639may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2640{
2641 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2642 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002643 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002644 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002645 return FAIL;
2646 }
2647 return OK;
2648}
2649
2650
Bram Moolenaara5565e42020-05-09 15:44:01 +02002651// Structure passed between the compile_expr* functions to keep track of
2652// constants that have been parsed but for which no code was produced yet. If
2653// possible expressions on these constants are applied at compile time. If
2654// that is not possible, the code to push the constants needs to be generated
2655// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002656// Using 50 should be more than enough of 5 levels of ().
2657#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002658typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002659 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002660 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002661 int pp_is_const; // all generated code was constants, used for a
2662 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002663} ppconst_T;
2664
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002665static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002666static int compile_expr0(char_u **arg, cctx_T *cctx);
2667static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2668
Bram Moolenaara5565e42020-05-09 15:44:01 +02002669/*
2670 * Generate a PUSH instruction for "tv".
2671 * "tv" will be consumed or cleared.
2672 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2673 */
2674 static int
2675generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2676{
2677 if (tv != NULL)
2678 {
2679 switch (tv->v_type)
2680 {
2681 case VAR_UNKNOWN:
2682 break;
2683 case VAR_BOOL:
2684 generate_PUSHBOOL(cctx, tv->vval.v_number);
2685 break;
2686 case VAR_SPECIAL:
2687 generate_PUSHSPEC(cctx, tv->vval.v_number);
2688 break;
2689 case VAR_NUMBER:
2690 generate_PUSHNR(cctx, tv->vval.v_number);
2691 break;
2692#ifdef FEAT_FLOAT
2693 case VAR_FLOAT:
2694 generate_PUSHF(cctx, tv->vval.v_float);
2695 break;
2696#endif
2697 case VAR_BLOB:
2698 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2699 tv->vval.v_blob = NULL;
2700 break;
2701 case VAR_STRING:
2702 generate_PUSHS(cctx, tv->vval.v_string);
2703 tv->vval.v_string = NULL;
2704 break;
2705 default:
2706 iemsg("constant type not supported");
2707 clear_tv(tv);
2708 return FAIL;
2709 }
2710 tv->v_type = VAR_UNKNOWN;
2711 }
2712 return OK;
2713}
2714
2715/*
2716 * Generate code for any ppconst entries.
2717 */
2718 static int
2719generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2720{
2721 int i;
2722 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002723 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002724
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002725 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002726 for (i = 0; i < ppconst->pp_used; ++i)
2727 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2728 ret = FAIL;
2729 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002730 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002731 return ret;
2732}
2733
2734/*
2735 * Clear ppconst constants. Used when failing.
2736 */
2737 static void
2738clear_ppconst(ppconst_T *ppconst)
2739{
2740 int i;
2741
2742 for (i = 0; i < ppconst->pp_used; ++i)
2743 clear_tv(&ppconst->pp_tv[i]);
2744 ppconst->pp_used = 0;
2745}
2746
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002747/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002748 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002749 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002750 */
2751 static int
2752compile_member(int is_slice, cctx_T *cctx)
2753{
2754 type_T **typep;
2755 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002756 vartype_T vartype;
2757 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002758
Bram Moolenaar261417b2021-05-06 21:04:55 +02002759 // We can index a list, dict and blob. If we don't know the type
2760 // we can use the index value type. If we still don't know use an "ANY"
2761 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002762 typep = ((type_T **)stack->ga_data) + stack->ga_len
2763 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002764 vartype = (*typep)->tt_type;
2765 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002766 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002767 if (*typep == &t_any && idxtype == &t_string)
2768 vartype = VAR_DICT;
2769 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002770 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002771 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002772 return FAIL;
2773 if (is_slice)
2774 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002775 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2776 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002777 FALSE, FALSE) == FAIL)
2778 return FAIL;
2779 }
2780 }
2781
Bram Moolenaar261417b2021-05-06 21:04:55 +02002782 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002783 {
2784 if (is_slice)
2785 {
2786 emsg(_(e_cannot_slice_dictionary));
2787 return FAIL;
2788 }
2789 if ((*typep)->tt_type == VAR_DICT)
2790 {
2791 *typep = (*typep)->tt_member;
2792 if (*typep == &t_unknown)
2793 // empty dict was used
2794 *typep = &t_any;
2795 }
2796 else
2797 {
2798 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2799 FALSE, FALSE) == FAIL)
2800 return FAIL;
2801 *typep = &t_any;
2802 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002803 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002804 return FAIL;
2805 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2806 return FAIL;
2807 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002808 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002809 {
2810 *typep = &t_string;
2811 if ((is_slice
2812 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2813 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2814 return FAIL;
2815 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002816 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002817 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002818 if (is_slice)
2819 {
2820 *typep = &t_blob;
2821 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2822 return FAIL;
2823 }
2824 else
2825 {
2826 *typep = &t_number;
2827 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2828 return FAIL;
2829 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002830 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002831 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002832 {
2833 if (is_slice)
2834 {
2835 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002836 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002837 2) == FAIL)
2838 return FAIL;
2839 }
2840 else
2841 {
2842 if ((*typep)->tt_type == VAR_LIST)
2843 {
2844 *typep = (*typep)->tt_member;
2845 if (*typep == &t_unknown)
2846 // empty list was used
2847 *typep = &t_any;
2848 }
2849 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002850 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2851 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002852 return FAIL;
2853 }
2854 }
2855 else
2856 {
2857 emsg(_(e_string_list_dict_or_blob_required));
2858 return FAIL;
2859 }
2860 return OK;
2861}
2862
2863/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002864 * Generate an instruction to load script-local variable "name", without the
2865 * leading "s:".
2866 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 */
2868 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002869compile_load_scriptvar(
2870 cctx_T *cctx,
2871 char_u *name, // variable NUL terminated
2872 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002873 char_u **end, // end of variable
2874 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002876 scriptitem_T *si;
2877 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 imported_T *import;
2879
Bram Moolenaare3d46852020-08-29 13:39:17 +02002880 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2881 return FAIL;
2882 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002883 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002884 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002886 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002887 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2888 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 }
2890 if (idx >= 0)
2891 {
2892 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2893
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002894 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 current_sctx.sc_sid, idx, sv->sv_type);
2896 return OK;
2897 }
2898
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002899 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 if (import != NULL)
2901 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002902 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002903 {
2904 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002905 char_u *exp_name;
2906 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002907 ufunc_T *ufunc;
2908 type_T *type;
2909
2910 // Used "import * as Name", need to lookup the member.
2911 if (*p != '.')
2912 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002913 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002914 return FAIL;
2915 }
2916 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002917 if (VIM_ISWHITE(*p))
2918 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002919 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002920 return FAIL;
2921 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002922
Bram Moolenaar1c991142020-07-04 13:15:31 +02002923 // isolate one name
2924 exp_name = p;
2925 while (eval_isnamec(*p))
2926 ++p;
2927 cc = *p;
2928 *p = NUL;
2929
Bram Moolenaaredba7072021-03-13 21:14:18 +01002930 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2931 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002932 *p = cc;
2933 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002934 *end = p;
2935
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002936 if (idx < 0)
2937 {
2938 if (*p == '(' && ufunc != NULL)
2939 {
2940 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2941 return OK;
2942 }
2943 return FAIL;
2944 }
2945
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002946 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2947 import->imp_sid,
2948 idx,
2949 type);
2950 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002951 else if (import->imp_funcname != NULL)
2952 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002953 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002954 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2955 import->imp_sid,
2956 import->imp_var_vals_idx,
2957 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 return OK;
2959 }
2960
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002961 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002962 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 return FAIL;
2964}
2965
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002966 static int
2967generate_funcref(cctx_T *cctx, char_u *name)
2968{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002969 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002970
2971 if (ufunc == NULL)
2972 return FAIL;
2973
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002974 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002975 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2976 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002977 == FAIL)
2978 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002979 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002980}
2981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982/*
2983 * Compile a variable name into a load instruction.
2984 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002985 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 * When "error" is FALSE do not give an error when not found.
2987 */
2988 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002989compile_load(
2990 char_u **arg,
2991 char_u *end_arg,
2992 cctx_T *cctx,
2993 int is_expr,
2994 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995{
2996 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002997 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002998 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003000 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001
3002 if (*(*arg + 1) == ':')
3003 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003004 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003005 {
3006 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007
Bram Moolenaarfa596382021-04-07 21:58:16 +02003008 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003009 switch (**arg)
3010 {
3011 case 'g': isn_type = ISN_LOADGDICT; break;
3012 case 'w': isn_type = ISN_LOADWDICT; break;
3013 case 't': isn_type = ISN_LOADTDICT; break;
3014 case 'b': isn_type = ISN_LOADBDICT; break;
3015 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003016 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003017 goto theend;
3018 }
3019 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3020 goto theend;
3021 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003022 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 else
3024 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003025 isntype_T isn_type = ISN_DROP;
3026
Bram Moolenaarfa596382021-04-07 21:58:16 +02003027 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003028 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3029 if (name == NULL)
3030 return FAIL;
3031
3032 switch (**arg)
3033 {
3034 case 'v': res = generate_LOADV(cctx, name, error);
3035 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003036 case 's': if (is_expr && ASCII_ISUPPER(*name)
3037 && find_func(name, FALSE, cctx) != NULL)
3038 res = generate_funcref(cctx, name);
3039 else
3040 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003041 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003042 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003043 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003044 {
3045 if (is_expr && ASCII_ISUPPER(*name)
3046 && find_func(name, FALSE, cctx) != NULL)
3047 res = generate_funcref(cctx, name);
3048 else
3049 isn_type = ISN_LOADG;
3050 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003051 else
3052 {
3053 isn_type = ISN_LOADAUTO;
3054 vim_free(name);
3055 name = vim_strnsave(*arg, end - *arg);
3056 if (name == NULL)
3057 return FAIL;
3058 }
3059 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003060 case 'w': isn_type = ISN_LOADW; break;
3061 case 't': isn_type = ISN_LOADT; break;
3062 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003063 default: // cannot happen, just in case
3064 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003065 goto theend;
3066 }
3067 if (isn_type != ISN_DROP)
3068 {
3069 // Global, Buffer-local, Window-local and Tabpage-local
3070 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003071 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003072 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 }
3075 }
3076 else
3077 {
3078 size_t len = end - *arg;
3079 int idx;
3080 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003081 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082
3083 name = vim_strnsave(*arg, end - *arg);
3084 if (name == NULL)
3085 return FAIL;
3086
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003087 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3088 {
3089 script_autoload(name, FALSE);
3090 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3091 }
3092 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3093 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003095 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003096 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 }
3098 else
3099 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003100 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003101
Bram Moolenaar709664c2020-12-12 14:33:41 +01003102 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003104 type = lvar.lv_type;
3105 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003106 if (lvar.lv_from_outer != 0)
3107 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003108 else
3109 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 }
3111 else
3112 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003113 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003114 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003115 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003116 || find_imported(name, 0, cctx) != NULL)
3117 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003118
Bram Moolenaar0f769812020-09-12 18:32:34 +02003119 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003120 // uppercase letter it can be a user defined function.
3121 // generate_funcref() will fail if the function can't be found.
3122 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003123 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 }
3125 }
3126 if (gen_load)
3127 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003128 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003129 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003130 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003131 cctx->ctx_outer_used = TRUE;
3132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 }
3134
3135 *arg = end;
3136
3137theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003138 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003139 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 vim_free(name);
3141 return res;
3142}
3143
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003144 static void
3145clear_instr_ga(garray_T *gap)
3146{
3147 int idx;
3148
3149 for (idx = 0; idx < gap->ga_len; ++idx)
3150 delete_instr(((isn_T *)gap->ga_data) + idx);
3151 ga_clear(gap);
3152}
3153
3154/*
3155 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3156 * Returns FAIL if compilation fails.
3157 */
3158 static int
3159compile_string(isn_T *isn, cctx_T *cctx)
3160{
3161 char_u *s = isn->isn_arg.string;
3162 garray_T save_ga = cctx->ctx_instr;
3163 int expr_res;
3164 int trailing_error;
3165 int instr_count;
3166 isn_T *instr = NULL;
3167
3168 // Temporarily reset the list of instructions so that the jump labels are
3169 // correct.
3170 cctx->ctx_instr.ga_len = 0;
3171 cctx->ctx_instr.ga_maxlen = 0;
3172 cctx->ctx_instr.ga_data = NULL;
3173 expr_res = compile_expr0(&s, cctx);
3174 s = skipwhite(s);
3175 trailing_error = *s != NUL;
3176
Bram Moolenaarff652882021-05-16 15:24:49 +02003177 if (expr_res == FAIL || trailing_error
3178 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003179 {
3180 if (trailing_error)
3181 semsg(_(e_trailing_arg), s);
3182 clear_instr_ga(&cctx->ctx_instr);
3183 cctx->ctx_instr = save_ga;
3184 return FAIL;
3185 }
3186
3187 // Move the generated instructions into the ISN_INSTR instruction, then
3188 // restore the list of instructions.
3189 instr_count = cctx->ctx_instr.ga_len;
3190 instr = cctx->ctx_instr.ga_data;
3191 instr[instr_count].isn_type = ISN_FINISH;
3192
3193 cctx->ctx_instr = save_ga;
3194 vim_free(isn->isn_arg.string);
3195 isn->isn_type = ISN_INSTR;
3196 isn->isn_arg.instr = instr;
3197 return OK;
3198}
3199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200/*
3201 * Compile the argument expressions.
3202 * "arg" points to just after the "(" and is advanced to after the ")"
3203 */
3204 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003205compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003207 char_u *p = *arg;
3208 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003209 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003210 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211
Bram Moolenaare6085c52020-04-12 20:19:16 +02003212 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003214 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3215 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003216 if (*p == ')')
3217 {
3218 *arg = p + 1;
3219 return OK;
3220 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003221 if (must_end)
3222 {
3223 semsg(_(e_missing_comma_before_argument_str), p);
3224 return FAIL;
3225 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003226
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003227 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003228 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 return FAIL;
3230 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003231
Bram Moolenaarff652882021-05-16 15:24:49 +02003232 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003233 && cctx->ctx_instr.ga_len == instr_count + 1)
3234 {
3235 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3236
3237 // {skip} argument of searchpair() can be compiled if not empty
3238 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3239 compile_string(isn, cctx);
3240 }
3241
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003242 if (*p != ',' && *skipwhite(p) == ',')
3243 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003244 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003245 p = skipwhite(p);
3246 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003248 {
3249 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003250 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003251 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003252 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003253 else
3254 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003255 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003256 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003258failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003259 emsg(_(e_missing_close));
3260 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261}
3262
3263/*
3264 * Compile a function call: name(arg1, arg2)
3265 * "arg" points to "name", "arg + varlen" to the "(".
3266 * "argcount_init" is 1 for "value->method()"
3267 * Instructions:
3268 * EVAL arg1
3269 * EVAL arg2
3270 * BCALL / DCALL / UCALL
3271 */
3272 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003273compile_call(
3274 char_u **arg,
3275 size_t varlen,
3276 cctx_T *cctx,
3277 ppconst_T *ppconst,
3278 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279{
3280 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003281 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 int argcount = argcount_init;
3283 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003284 char_u fname_buf[FLEN_FIXED + 1];
3285 char_u *tofree = NULL;
3286 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003287 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003288 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003289 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003290 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291
Bram Moolenaara5565e42020-05-09 15:44:01 +02003292 // we can evaluate "has('name')" at compile time
3293 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3294 {
3295 char_u *s = skipwhite(*arg + varlen + 1);
3296 typval_T argvars[2];
3297
3298 argvars[0].v_type = VAR_UNKNOWN;
3299 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003300 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003301 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003302 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003303 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003304 if (*s == ')' && argvars[0].v_type == VAR_STRING
3305 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003306 {
3307 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3308
3309 *arg = s + 1;
3310 argvars[1].v_type = VAR_UNKNOWN;
3311 tv->v_type = VAR_NUMBER;
3312 tv->vval.v_number = 0;
3313 f_has(argvars, tv);
3314 clear_tv(&argvars[0]);
3315 ++ppconst->pp_used;
3316 return OK;
3317 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003318 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003319 }
3320
3321 if (generate_ppconst(cctx, ppconst) == FAIL)
3322 return FAIL;
3323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 if (varlen >= sizeof(namebuf))
3325 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003326 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 return FAIL;
3328 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003329 vim_strncpy(namebuf, *arg, varlen);
3330 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003332 // We handle the "skip" argument of searchpair() and searchpairpos()
3333 // differently.
3334 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3335 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3336 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3337 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003340 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003341 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342
Bram Moolenaar03290b82020-12-19 16:30:44 +01003343 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003344 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 {
3346 int idx;
3347
3348 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003349 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003351 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003352 if (STRCMP(name, "flatten") == 0)
3353 {
3354 emsg(_(e_cannot_use_flatten_in_vim9_script));
3355 goto theend;
3356 }
3357
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003358 if (STRCMP(name, "add") == 0 && argcount == 2)
3359 {
3360 garray_T *stack = &cctx->ctx_type_stack;
3361 type_T *type = ((type_T **)stack->ga_data)[
3362 stack->ga_len - 2];
3363
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003364 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003365 if (type->tt_type == VAR_LIST)
3366 {
3367 // inline "add(list, item)" so that the type can be checked
3368 res = generate_LISTAPPEND(cctx);
3369 idx = -1;
3370 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003371 else if (type->tt_type == VAR_BLOB)
3372 {
3373 // inline "add(blob, nr)" so that the type can be checked
3374 res = generate_BLOBAPPEND(cctx);
3375 idx = -1;
3376 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003377 }
3378
3379 if (idx >= 0)
3380 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3381 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003382 else
3383 semsg(_(e_unknownfunc), namebuf);
3384 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 }
3386
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003387 // An argument or local variable can be a function reference, this
3388 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003389 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003390 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003391 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003392 // If we can find the function by name generate the right call.
3393 // Skip global functions here, a local funcref takes precedence.
3394 ufunc = find_func(name, FALSE, cctx);
3395 if (ufunc != NULL && !func_is_global(ufunc))
3396 {
3397 res = generate_CALL(cctx, ufunc, argcount);
3398 goto theend;
3399 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401
3402 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003403 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003404 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003406 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003407 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003408 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003409 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003410 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003411
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003412 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003413 goto theend;
3414 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415
Bram Moolenaar0f769812020-09-12 18:32:34 +02003416 // If we can find a global function by name generate the right call.
3417 if (ufunc != NULL)
3418 {
3419 res = generate_CALL(cctx, ufunc, argcount);
3420 goto theend;
3421 }
3422
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003423 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003424 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003425 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003426 res = generate_UCALL(cctx, name, argcount);
3427 else
3428 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003429
3430theend:
3431 vim_free(tofree);
3432 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433}
3434
3435// like NAMESPACE_CHAR but with 'a' and 'l'.
3436#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3437
3438/*
3439 * Find the end of a variable or function name. Unlike find_name_end() this
3440 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003441 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 * Return a pointer to just after the name. Equal to "arg" if there is no
3443 * valid name.
3444 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003445 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003446to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447{
3448 char_u *p;
3449
3450 // Quick check for valid starting character.
3451 if (!eval_isnamec1(*arg))
3452 return arg;
3453
3454 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3455 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3456 // and can be used in slice "[n:]".
3457 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003458 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3460 break;
3461 return p;
3462}
3463
3464/*
3465 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003466 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 */
3468 char_u *
3469to_name_const_end(char_u *arg)
3470{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003471 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 typval_T rettv;
3473
3474 if (p == arg && *arg == '[')
3475 {
3476
3477 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003478 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 p = arg;
3480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 return p;
3482}
3483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484/*
3485 * parse a list: [expr, expr]
3486 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003487 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 */
3489 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003490compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491{
3492 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003493 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003495 int is_const;
3496 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003498 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003500 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003501 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003502 semsg(_(e_list_end), *arg);
3503 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003504 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003505 if (*p == ',')
3506 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003507 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003508 return FAIL;
3509 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003510 if (*p == ']')
3511 {
3512 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003513 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003514 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003515 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003516 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003517 if (!is_const)
3518 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 ++count;
3520 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003521 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003523 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3524 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003525 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003526 return FAIL;
3527 }
3528 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003529 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 p = skipwhite(p);
3531 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003532 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003534 ppconst->pp_is_const = is_all_const;
3535 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536}
3537
3538/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003539 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003540 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003541 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 */
3543 static int
3544compile_lambda(char_u **arg, cctx_T *cctx)
3545{
Bram Moolenaare462f522020-12-27 14:43:30 +01003546 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 typval_T rettv;
3548 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003549 evalarg_T evalarg;
3550
3551 CLEAR_FIELD(evalarg);
3552 evalarg.eval_flags = EVAL_EVALUATE;
3553 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554
3555 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003556 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3557 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003558 {
3559 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003560 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003561 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003562
Bram Moolenaar65c44152020-12-24 15:14:01 +01003563 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003565 ++ufunc->uf_refcount;
3566 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567
Bram Moolenaara9931532021-06-12 15:58:16 +02003568 // Compile it here to get the return type. The return type is optional,
3569 // when it's missing use t_unknown. This is recognized in
3570 // compile_return().
3571 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3572 ufunc->uf_ret_type = &t_unknown;
3573 compile_def_function(ufunc, FALSE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003575 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3576 // points into it. Point to the original line to avoid a dangling pointer.
3577 if (evalarg.eval_tofree_cmdline != NULL)
3578 {
3579 size_t off = *arg - evalarg.eval_tofree_cmdline;
3580
3581 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3582 + off;
3583 }
3584
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003585 clear_evalarg(&evalarg, NULL);
3586
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003587 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003588 {
3589 // The return type will now be known.
3590 set_function_type(ufunc);
3591
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003592 // The function reference count will be 1. When the ISN_FUNCREF
3593 // instruction is deleted the reference count is decremented and the
3594 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003595 return generate_FUNCREF(cctx, ufunc);
3596 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003597
3598 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 return FAIL;
3600}
3601
3602/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003603 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003605 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 */
3607 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003608compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609{
3610 garray_T *instr = &cctx->ctx_instr;
3611 int count = 0;
3612 dict_T *d = dict_alloc();
3613 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003614 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003615 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003616 int is_const;
3617 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618
3619 if (d == NULL)
3620 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003621 if (generate_ppconst(cctx, ppconst) == FAIL)
3622 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003623 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003625 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626
Bram Moolenaar23c55272020-06-21 16:58:13 +02003627 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003628 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003629 *arg = NULL;
3630 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003631 }
3632
3633 if (**arg == '}')
3634 break;
3635
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003636 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003638 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003640 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003641 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003642 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003645 if (isn->isn_type == ISN_PUSHNR)
3646 {
3647 char buf[NUMBUFLEN];
3648
3649 // Convert to string at compile time.
3650 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3651 isn->isn_type = ISN_PUSHS;
3652 isn->isn_arg.string = vim_strsave((char_u *)buf);
3653 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 if (isn->isn_type == ISN_PUSHS)
3655 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003656 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003657 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003658 *arg = skipwhite(*arg);
3659 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003660 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003661 emsg(_(e_missing_matching_bracket_after_dict_key));
3662 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003663 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003664 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003666 else
3667 {
3668 // {"name": value},
3669 // {'name': value},
3670 // {name: value} use "name" as a literal key
3671 key = get_literal_key(arg);
3672 if (key == NULL)
3673 return FAIL;
3674 if (generate_PUSHS(cctx, key) == FAIL)
3675 return FAIL;
3676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677
3678 // Check for duplicate keys, if using string keys.
3679 if (key != NULL)
3680 {
3681 item = dict_find(d, key, -1);
3682 if (item != NULL)
3683 {
3684 semsg(_(e_duplicate_key), key);
3685 goto failret;
3686 }
3687 item = dictitem_alloc(key);
3688 if (item != NULL)
3689 {
3690 item->di_tv.v_type = VAR_UNKNOWN;
3691 item->di_tv.v_lock = 0;
3692 if (dict_add(d, item) == FAIL)
3693 dictitem_free(item);
3694 }
3695 }
3696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 if (**arg != ':')
3698 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003699 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003700 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003701 else
3702 semsg(_(e_missing_dict_colon), *arg);
3703 return FAIL;
3704 }
3705 whitep = *arg + 1;
3706 if (!IS_WHITE_OR_NUL(*whitep))
3707 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003708 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 return FAIL;
3710 }
3711
Bram Moolenaar23c55272020-06-21 16:58:13 +02003712 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003713 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003714 *arg = NULL;
3715 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003716 }
3717
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003718 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003720 if (!is_const)
3721 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 ++count;
3723
Bram Moolenaar2c330432020-04-13 14:41:35 +02003724 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003725 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003726 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003727 *arg = NULL;
3728 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 if (**arg == '}')
3731 break;
3732 if (**arg != ',')
3733 {
3734 semsg(_(e_missing_dict_comma), *arg);
3735 goto failret;
3736 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003737 if (IS_WHITE_OR_NUL(*whitep))
3738 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003739 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003740 return FAIL;
3741 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003742 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003743 if (!IS_WHITE_OR_NUL(*whitep))
3744 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003745 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003746 return FAIL;
3747 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003748 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 }
3750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 *arg = *arg + 1;
3752
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003753 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003754 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003755 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003756 *arg += STRLEN(*arg);
3757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003759 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 return generate_NEWDICT(cctx, count);
3761
3762failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003763 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003764 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003765 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003766 *arg = (char_u *)"";
3767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 dict_unref(d);
3769 return FAIL;
3770}
3771
3772/*
3773 * Compile "&option".
3774 */
3775 static int
3776compile_get_option(char_u **arg, cctx_T *cctx)
3777{
3778 typval_T rettv;
3779 char_u *start = *arg;
3780 int ret;
3781
3782 // parse the option and get the current value to get the type.
3783 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003784 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 if (ret == OK)
3786 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003787 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003788 char_u *name = vim_strnsave(start, *arg - start);
3789 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3790 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791
3792 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3793 vim_free(name);
3794 }
3795 clear_tv(&rettv);
3796
3797 return ret;
3798}
3799
3800/*
3801 * Compile "$VAR".
3802 */
3803 static int
3804compile_get_env(char_u **arg, cctx_T *cctx)
3805{
3806 char_u *start = *arg;
3807 int len;
3808 int ret;
3809 char_u *name;
3810
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 ++*arg;
3812 len = get_env_len(arg);
3813 if (len == 0)
3814 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003815 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 return FAIL;
3817 }
3818
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003819 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 name = vim_strnsave(start, len + 1);
3821 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3822 vim_free(name);
3823 return ret;
3824}
3825
3826/*
3827 * Compile "@r".
3828 */
3829 static int
3830compile_get_register(char_u **arg, cctx_T *cctx)
3831{
3832 int ret;
3833
3834 ++*arg;
3835 if (**arg == NUL)
3836 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003837 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 return FAIL;
3839 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003840 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 {
3842 emsg_invreg(**arg);
3843 return FAIL;
3844 }
3845 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3846 ++*arg;
3847 return ret;
3848}
3849
3850/*
3851 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003852 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 */
3854 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003855apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003857 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858
3859 // this works from end to start
3860 while (p > start)
3861 {
3862 --p;
3863 if (*p == '-' || *p == '+')
3864 {
3865 // only '-' has an effect, for '+' we only check the type
3866#ifdef FEAT_FLOAT
3867 if (rettv->v_type == VAR_FLOAT)
3868 {
3869 if (*p == '-')
3870 rettv->vval.v_float = -rettv->vval.v_float;
3871 }
3872 else
3873#endif
3874 {
3875 varnumber_T val;
3876 int error = FALSE;
3877
3878 // tv_get_number_chk() accepts a string, but we don't want that
3879 // here
3880 if (check_not_string(rettv) == FAIL)
3881 return FAIL;
3882 val = tv_get_number_chk(rettv, &error);
3883 clear_tv(rettv);
3884 if (error)
3885 return FAIL;
3886 if (*p == '-')
3887 val = -val;
3888 rettv->v_type = VAR_NUMBER;
3889 rettv->vval.v_number = val;
3890 }
3891 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003892 else if (numeric_only)
3893 {
3894 ++p;
3895 break;
3896 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003897 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 {
3899 int v = tv2bool(rettv);
3900
3901 // '!' is permissive in the type.
3902 clear_tv(rettv);
3903 rettv->v_type = VAR_BOOL;
3904 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3905 }
3906 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003907 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 return OK;
3909}
3910
3911/*
3912 * Recognize v: variables that are constants and set "rettv".
3913 */
3914 static void
3915get_vim_constant(char_u **arg, typval_T *rettv)
3916{
3917 if (STRNCMP(*arg, "v:true", 6) == 0)
3918 {
3919 rettv->v_type = VAR_BOOL;
3920 rettv->vval.v_number = VVAL_TRUE;
3921 *arg += 6;
3922 }
3923 else if (STRNCMP(*arg, "v:false", 7) == 0)
3924 {
3925 rettv->v_type = VAR_BOOL;
3926 rettv->vval.v_number = VVAL_FALSE;
3927 *arg += 7;
3928 }
3929 else if (STRNCMP(*arg, "v:null", 6) == 0)
3930 {
3931 rettv->v_type = VAR_SPECIAL;
3932 rettv->vval.v_number = VVAL_NULL;
3933 *arg += 6;
3934 }
3935 else if (STRNCMP(*arg, "v:none", 6) == 0)
3936 {
3937 rettv->v_type = VAR_SPECIAL;
3938 rettv->vval.v_number = VVAL_NONE;
3939 *arg += 6;
3940 }
3941}
3942
Bram Moolenaar657137c2021-01-09 15:45:23 +01003943 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003944get_compare_type(char_u *p, int *len, int *type_is)
3945{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003946 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003947 int i;
3948
3949 switch (p[0])
3950 {
3951 case '=': if (p[1] == '=')
3952 type = EXPR_EQUAL;
3953 else if (p[1] == '~')
3954 type = EXPR_MATCH;
3955 break;
3956 case '!': if (p[1] == '=')
3957 type = EXPR_NEQUAL;
3958 else if (p[1] == '~')
3959 type = EXPR_NOMATCH;
3960 break;
3961 case '>': if (p[1] != '=')
3962 {
3963 type = EXPR_GREATER;
3964 *len = 1;
3965 }
3966 else
3967 type = EXPR_GEQUAL;
3968 break;
3969 case '<': if (p[1] != '=')
3970 {
3971 type = EXPR_SMALLER;
3972 *len = 1;
3973 }
3974 else
3975 type = EXPR_SEQUAL;
3976 break;
3977 case 'i': if (p[1] == 's')
3978 {
3979 // "is" and "isnot"; but not a prefix of a name
3980 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3981 *len = 5;
3982 i = p[*len];
3983 if (!isalnum(i) && i != '_')
3984 {
3985 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3986 *type_is = TRUE;
3987 }
3988 }
3989 break;
3990 }
3991 return type;
3992}
3993
3994/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003995 * Skip over an expression, ignoring most errors.
3996 */
3997 static void
3998skip_expr_cctx(char_u **arg, cctx_T *cctx)
3999{
4000 evalarg_T evalarg;
4001
4002 CLEAR_FIELD(evalarg);
4003 evalarg.eval_cctx = cctx;
4004 skip_expr(arg, &evalarg);
4005}
4006
4007/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004009 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 */
4011 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004012compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004014 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015
4016 // this works from end to start
4017 while (p > start)
4018 {
4019 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004020 while (VIM_ISWHITE(*p))
4021 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 if (*p == '-' || *p == '+')
4023 {
4024 int negate = *p == '-';
4025 isn_T *isn;
4026
4027 // TODO: check type
4028 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4029 {
4030 --p;
4031 if (*p == '-')
4032 negate = !negate;
4033 }
4034 // only '-' has an effect, for '+' we only check the type
4035 if (negate)
4036 isn = generate_instr(cctx, ISN_NEGATENR);
4037 else
4038 isn = generate_instr(cctx, ISN_CHECKNR);
4039 if (isn == NULL)
4040 return FAIL;
4041 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004042 else if (numeric_only)
4043 {
4044 ++p;
4045 break;
4046 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 else
4048 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004049 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004051 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004053 if (p[-1] == '!')
4054 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004057 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 return FAIL;
4059 }
4060 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004061 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 return OK;
4063}
4064
4065/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004066 * Compile "(expression)": recursive!
4067 * Return FAIL/OK.
4068 */
4069 static int
4070compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4071{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004072 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004073 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004074
Bram Moolenaar24156692021-01-14 20:35:49 +01004075 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4076 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004077 if (ppconst->pp_used <= PPSIZE - 10)
4078 {
4079 ret = compile_expr1(arg, cctx, ppconst);
4080 }
4081 else
4082 {
4083 // Not enough space in ppconst, flush constants.
4084 if (generate_ppconst(cctx, ppconst) == FAIL)
4085 return FAIL;
4086 ret = compile_expr0(arg, cctx);
4087 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004088 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4089 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004090 if (**arg == ')')
4091 ++*arg;
4092 else if (ret == OK)
4093 {
4094 emsg(_(e_missing_close));
4095 ret = FAIL;
4096 }
4097 return ret;
4098}
4099
4100/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004102 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 */
4104 static int
4105compile_subscript(
4106 char_u **arg,
4107 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004108 char_u *start_leader,
4109 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004110 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004112 char_u *name_start = *end_leader;
4113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 for (;;)
4115 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004116 char_u *p = skipwhite(*arg);
4117
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004118 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004119 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004120 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004121
4122 // If a following line starts with "->{" or "->X" advance to that
4123 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004124 // Also if a following line starts with ".x".
4125 if (next != NULL &&
4126 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004127 && (next[2] == '{'
4128 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004129 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004130 {
4131 next = next_line_from_context(cctx, TRUE);
4132 if (next == NULL)
4133 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004134 *arg = next;
4135 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004136 }
4137 }
4138
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004139 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004140 // not a function call.
4141 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004143 garray_T *stack = &cctx->ctx_type_stack;
4144 type_T *type;
4145 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146
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 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004152 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4153
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004154 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004155 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004157 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 return FAIL;
4159 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004160 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004162 char_u *pstart = p;
4163
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004164 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004165 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004166 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 // something->method()
4169 // Apply the '!', '-' and '+' first:
4170 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004171 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004174 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004175 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004176 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004177 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004178 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004179 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004180 garray_T *stack = &cctx->ctx_type_stack;
4181 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004182 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004183 int expr_isn_start = cctx->ctx_instr.ga_len;
4184 int expr_isn_end;
4185 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004186
4187 // Funcref call: list->(Refs[2])(arg)
4188 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004189 //
4190 // Fist compile the function expression.
4191 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004192 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004193
4194 // Remember the next instruction index, where the instructions
4195 // for arguments are being written.
4196 expr_isn_end = cctx->ctx_instr.ga_len;
4197
4198 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004199 if (**arg != '(')
4200 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004201 if (*skipwhite(*arg) == '(')
4202 emsg(_(e_nowhitespace));
4203 else
4204 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004205 return FAIL;
4206 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004207 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004208 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004209 return FAIL;
4210
Bram Moolenaar2927c072021-04-05 19:41:21 +02004211 // Move the instructions for the arguments to before the
4212 // instructions of the expression and move the type of the
4213 // expression after the argument types. This is what ISN_PCALL
4214 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004215 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004216 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4217 if (arg_isn_count > 0)
4218 {
4219 int expr_isn_count = expr_isn_end - expr_isn_start;
4220 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4221
4222 if (isn == NULL)
4223 return FAIL;
4224 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4225 + expr_isn_start,
4226 sizeof(isn_T) * expr_isn_count);
4227 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4228 + expr_isn_start,
4229 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4230 sizeof(isn_T) * arg_isn_count);
4231 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4232 + expr_isn_start + arg_isn_count,
4233 isn, sizeof(isn_T) * expr_isn_count);
4234 vim_free(isn);
4235
4236 type = ((type_T **)stack->ga_data)[type_idx_start];
4237 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4238 ((type_T **)stack->ga_data) + type_idx_start + 1,
4239 sizeof(type_T *)
4240 * (stack->ga_len - type_idx_start - 1));
4241 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4242 }
4243
Bram Moolenaar7e368202020-12-25 21:56:57 +01004244 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4245 if (generate_PCALL(cctx, argcount,
4246 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 return FAIL;
4248 }
4249 else
4250 {
4251 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004252 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004253 if (!eval_isnamec1(*p))
4254 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004255 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004256 return FAIL;
4257 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004258 if (ASCII_ISALPHA(*p) && p[1] == ':')
4259 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004260 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 ;
4262 if (*p != '(')
4263 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004264 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 return FAIL;
4266 }
4267 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004268 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 return FAIL;
4270 }
4271 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004272 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004274 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004275
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004276 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004277 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004278 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004279 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004280 // TODO: more arguments
4281 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004282 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004283 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004284 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004285
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004286 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004287 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004288 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004289 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004290 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004291 // missing first index is equal to zero
4292 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004293 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004294 else
4295 {
4296 if (compile_expr0(arg, cctx) == FAIL)
4297 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004298 if (**arg == ':')
4299 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004300 semsg(_(e_white_space_required_before_and_after_str_at_str),
4301 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004302 return FAIL;
4303 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004304 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004305 return FAIL;
4306 *arg = skipwhite(*arg);
4307 }
4308 if (**arg == ':')
4309 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004310 is_slice = TRUE;
4311 ++*arg;
4312 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4313 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004314 semsg(_(e_white_space_required_before_and_after_str_at_str),
4315 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004316 return FAIL;
4317 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004318 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004319 return FAIL;
4320 if (**arg == ']')
4321 // missing second index is equal to end of string
4322 generate_PUSHNR(cctx, -1);
4323 else
4324 {
4325 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004326 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004327 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004328 return FAIL;
4329 *arg = skipwhite(*arg);
4330 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332
4333 if (**arg != ']')
4334 {
4335 emsg(_(e_missbrac));
4336 return FAIL;
4337 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004338 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339
Bram Moolenaare42939a2021-04-05 17:11:17 +02004340 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004341 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004343 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004345 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004346 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004347 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004348 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004349
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004350 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004351 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004352 {
4353 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004354 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004355 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004356 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004357 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 while (eval_isnamec(*p))
4359 MB_PTR_ADV(p);
4360 if (p == *arg)
4361 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004362 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 return FAIL;
4364 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004365 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 return FAIL;
4367 *arg = p;
4368 }
4369 else
4370 break;
4371 }
4372
4373 // TODO - see handle_subscript():
4374 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4375 // Don't do this when "Func" is already a partial that was bound
4376 // explicitly (pt_auto is FALSE).
4377
4378 return OK;
4379}
4380
4381/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004382 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4383 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004385 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004386 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004387 *
4388 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 */
4390
4391/*
4392 * number number constant
4393 * 0zFFFFFFFF Blob constant
4394 * "string" string constant
4395 * 'string' literal string constant
4396 * &option-name option value
4397 * @r register contents
4398 * identifier variable value
4399 * function() function call
4400 * $VAR environment variable
4401 * (expression) nested expression
4402 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004403 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 *
4405 * Also handle:
4406 * ! in front logical NOT
4407 * - in front unary minus
4408 * + in front unary plus (ignored)
4409 * trailing (arg) funcref/partial call
4410 * trailing [] subscript in String or List
4411 * trailing .name entry in Dictionary
4412 * trailing ->name() method call
4413 */
4414 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004415compile_expr7(
4416 char_u **arg,
4417 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004418 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 char_u *start_leader, *end_leader;
4421 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004422 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004423 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004425 ppconst->pp_is_const = FALSE;
4426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 /*
4428 * Skip '!', '-' and '+' characters. They are handled later.
4429 */
4430 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004431 if (eval_leader(arg, TRUE) == FAIL)
4432 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 end_leader = *arg;
4434
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004435 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 switch (**arg)
4437 {
4438 /*
4439 * Number constant.
4440 */
4441 case '0': // also for blob starting with 0z
4442 case '1':
4443 case '2':
4444 case '3':
4445 case '4':
4446 case '5':
4447 case '6':
4448 case '7':
4449 case '8':
4450 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004451 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004453 // Apply "-" and "+" just before the number now, right to
4454 // left. Matters especially when "->" follows. Stops at
4455 // '!'.
4456 if (apply_leader(rettv, TRUE,
4457 start_leader, &end_leader) == FAIL)
4458 {
4459 clear_tv(rettv);
4460 return FAIL;
4461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 break;
4463
4464 /*
4465 * String constant: "string".
4466 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004467 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 return FAIL;
4469 break;
4470
4471 /*
4472 * Literal string constant: 'str''ing'.
4473 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004474 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 return FAIL;
4476 break;
4477
4478 /*
4479 * Constant Vim variable.
4480 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004481 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 ret = NOTDONE;
4483 break;
4484
4485 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004486 * "true" constant
4487 */
4488 case 't': if (STRNCMP(*arg, "true", 4) == 0
4489 && !eval_isnamec((*arg)[4]))
4490 {
4491 *arg += 4;
4492 rettv->v_type = VAR_BOOL;
4493 rettv->vval.v_number = VVAL_TRUE;
4494 }
4495 else
4496 ret = NOTDONE;
4497 break;
4498
4499 /*
4500 * "false" constant
4501 */
4502 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4503 && !eval_isnamec((*arg)[5]))
4504 {
4505 *arg += 5;
4506 rettv->v_type = VAR_BOOL;
4507 rettv->vval.v_number = VVAL_FALSE;
4508 }
4509 else
4510 ret = NOTDONE;
4511 break;
4512
4513 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004514 * "null" constant
4515 */
4516 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004517 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004518 {
4519 *arg += 4;
4520 rettv->v_type = VAR_SPECIAL;
4521 rettv->vval.v_number = VVAL_NULL;
4522 }
4523 else
4524 ret = NOTDONE;
4525 break;
4526
4527 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 * List: [expr, expr]
4529 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004530 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4531 return FAIL;
4532 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 break;
4534
4535 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 * Dictionary: {'key': val, 'key': val}
4537 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004538 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4539 return FAIL;
4540 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 break;
4542
4543 /*
4544 * Option value: &name
4545 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004546 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4547 return FAIL;
4548 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 break;
4550
4551 /*
4552 * Environment variable: $VAR.
4553 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004554 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4555 return FAIL;
4556 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 break;
4558
4559 /*
4560 * Register contents: @r.
4561 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004562 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4563 return FAIL;
4564 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 break;
4566 /*
4567 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004568 * lambda: (arg, arg) => expr
4569 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004571 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4572 ret = compile_lambda(arg, cctx);
4573 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004574 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 break;
4576
4577 default: ret = NOTDONE;
4578 break;
4579 }
4580 if (ret == FAIL)
4581 return FAIL;
4582
Bram Moolenaar1c747212020-05-09 18:28:34 +02004583 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004585 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004586 clear_tv(rettv);
4587 else
4588 // A constant expression can possibly be handled compile time,
4589 // return the value instead of generating code.
4590 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 }
4592 else if (ret == NOTDONE)
4593 {
4594 char_u *p;
4595 int r;
4596
4597 if (!eval_isnamec1(**arg))
4598 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004599 if (!vim9_bad_comment(*arg))
4600 {
4601 if (ends_excmd(*skipwhite(*arg)))
4602 semsg(_(e_empty_expression_str), *arg);
4603 else
4604 semsg(_(e_name_expected_str), *arg);
4605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 return FAIL;
4607 }
4608
4609 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004610 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004611 if (p - *arg == (size_t)1 && **arg == '_')
4612 {
4613 emsg(_(e_cannot_use_underscore_here));
4614 return FAIL;
4615 }
4616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004618 {
4619 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4620 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004622 {
4623 if (generate_ppconst(cctx, ppconst) == FAIL)
4624 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004625 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 if (r == FAIL)
4628 return FAIL;
4629 }
4630
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004631 // Handle following "[]", ".member", etc.
4632 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004633 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004634 ppconst) == FAIL)
4635 return FAIL;
4636 if (ppconst->pp_used > 0)
4637 {
4638 // apply the '!', '-' and '+' before the constant
4639 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004640 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004641 return FAIL;
4642 return OK;
4643 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004644 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004646 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647}
4648
4649/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004650 * Give the "white on both sides" error, taking the operator from "p[len]".
4651 */
4652 void
4653error_white_both(char_u *op, int len)
4654{
4655 char_u buf[10];
4656
4657 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004658 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004659}
4660
4661/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004662 * <type>expr7: runtime type check / conversion
4663 */
4664 static int
4665compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4666{
4667 type_T *want_type = NULL;
4668
4669 // Recognize <type>
4670 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4671 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004672 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004673 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4674 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004675 return FAIL;
4676
4677 if (**arg != '>')
4678 {
4679 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004680 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004681 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004682 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004683 return FAIL;
4684 }
4685 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004686 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004687 return FAIL;
4688 }
4689
4690 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4691 return FAIL;
4692
4693 if (want_type != NULL)
4694 {
4695 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004696 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004697 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004698
Bram Moolenaard1103582020-08-14 22:44:25 +02004699 generate_ppconst(cctx, ppconst);
4700 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004701 where.wt_index = 0;
4702 where.wt_variable = FALSE;
4703 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004704 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004705 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004706 return FAIL;
4707 }
4708 }
4709
4710 return OK;
4711}
4712
4713/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 * * number multiplication
4715 * / number division
4716 * % number modulo
4717 */
4718 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004719compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720{
4721 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004722 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004723 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004725 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004726 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 return FAIL;
4728
4729 /*
4730 * Repeat computing, until no "*", "/" or "%" is following.
4731 */
4732 for (;;)
4733 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004734 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 if (*op != '*' && *op != '/' && *op != '%')
4736 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004737 if (next != NULL)
4738 {
4739 *arg = next_line_from_context(cctx, TRUE);
4740 op = skipwhite(*arg);
4741 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004742
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004743 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004745 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004746 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004748 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004749 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004751 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004752 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004754
4755 if (ppconst->pp_used == ppconst_used + 2
4756 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4757 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004758 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004759 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4760 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4761 varnumber_T res = 0;
4762 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004764 // both are numbers: compute the result
4765 switch (*op)
4766 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004767 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004768 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004769 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004770 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004771 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004772 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004773 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004774 break;
4775 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004776 if (failed)
4777 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004778 tv1->vval.v_number = res;
4779 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004780 }
4781 else
4782 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004783 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004784 generate_two_op(cctx, op);
4785 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 }
4787
4788 return OK;
4789}
4790
4791/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004792 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 * - number subtraction
4794 * .. string concatenation
4795 */
4796 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004797compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798{
4799 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004800 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004802 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803
4804 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004805 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 return FAIL;
4807
4808 /*
4809 * Repeat computing, until no "+", "-" or ".." is following.
4810 */
4811 for (;;)
4812 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004813 op = may_peek_next_line(cctx, *arg, &next);
4814 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004816 if (op[0] == op[1] && *op != '.' && next)
4817 // Finding "++" or "--" on the next line is a separate command.
4818 // But ".." is concatenation.
4819 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004821 if (next != NULL)
4822 {
4823 *arg = next_line_from_context(cctx, TRUE);
4824 op = skipwhite(*arg);
4825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004827 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004829 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004830 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831 }
4832
Bram Moolenaare0de1712020-12-02 17:36:54 +01004833 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004834 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004836 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004837 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 return FAIL;
4839
Bram Moolenaara5565e42020-05-09 15:44:01 +02004840 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004841 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004842 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4843 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4844 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4845 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004847 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4848 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004849
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004850 // concat/subtract/add constant numbers
4851 if (*op == '+')
4852 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4853 else if (*op == '-')
4854 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4855 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004856 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004857 // concatenate constant strings
4858 char_u *s1 = tv1->vval.v_string;
4859 char_u *s2 = tv2->vval.v_string;
4860 size_t len1 = STRLEN(s1);
4861
4862 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4863 if (tv1->vval.v_string == NULL)
4864 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004865 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004866 return FAIL;
4867 }
4868 mch_memmove(tv1->vval.v_string, s1, len1);
4869 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004870 vim_free(s1);
4871 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004872 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004873 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 }
4875 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004876 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004877 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004878 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004879 if (*op == '.')
4880 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004881 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4882 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004883 return FAIL;
4884 generate_instr_drop(cctx, ISN_CONCAT, 1);
4885 }
4886 else
4887 generate_two_op(cctx, op);
4888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 }
4890
4891 return OK;
4892}
4893
4894/*
4895 * expr5a == expr5b
4896 * expr5a =~ expr5b
4897 * expr5a != expr5b
4898 * expr5a !~ expr5b
4899 * expr5a > expr5b
4900 * expr5a >= expr5b
4901 * expr5a < expr5b
4902 * expr5a <= expr5b
4903 * expr5a is expr5b
4904 * expr5a isnot expr5b
4905 *
4906 * Produces instructions:
4907 * EVAL expr5a Push result of "expr5a"
4908 * EVAL expr5b Push result of "expr5b"
4909 * COMPARE one of the compare instructions
4910 */
4911 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004912compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004914 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004916 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004919 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920
4921 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004922 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923 return FAIL;
4924
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004925 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004926 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004927
4928 /*
4929 * If there is a comparative operator, use it.
4930 */
4931 if (type != EXPR_UNKNOWN)
4932 {
4933 int ic = FALSE; // Default: do not ignore case
4934
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004935 if (next != NULL)
4936 {
4937 *arg = next_line_from_context(cctx, TRUE);
4938 p = skipwhite(*arg);
4939 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 if (type_is && (p[len] == '?' || p[len] == '#'))
4941 {
4942 semsg(_(e_invexpr2), *arg);
4943 return FAIL;
4944 }
4945 // extra question mark appended: ignore case
4946 if (p[len] == '?')
4947 {
4948 ic = TRUE;
4949 ++len;
4950 }
4951 // extra '#' appended: match case (ignored)
4952 else if (p[len] == '#')
4953 ++len;
4954 // nothing appended: match case
4955
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004956 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004958 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004959 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 }
4961
4962 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004963 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004964 return FAIL;
4965
Bram Moolenaara5565e42020-05-09 15:44:01 +02004966 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 return FAIL;
4968
Bram Moolenaara5565e42020-05-09 15:44:01 +02004969 if (ppconst->pp_used == ppconst_used + 2)
4970 {
4971 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4972 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4973 int ret;
4974
4975 // Both sides are a constant, compute the result now.
4976 // First check for a valid combination of types, this is more
4977 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004978 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004979 ret = FAIL;
4980 else
4981 {
4982 ret = typval_compare(tv1, tv2, type, ic);
4983 tv1->v_type = VAR_BOOL;
4984 tv1->vval.v_number = tv1->vval.v_number
4985 ? VVAL_TRUE : VVAL_FALSE;
4986 clear_tv(tv2);
4987 --ppconst->pp_used;
4988 }
4989 return ret;
4990 }
4991
4992 generate_ppconst(cctx, ppconst);
4993 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 }
4995
4996 return OK;
4997}
4998
Bram Moolenaar7f141552020-05-09 17:35:53 +02004999static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001/*
5002 * Compile || or &&.
5003 */
5004 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005005compile_and_or(
5006 char_u **arg,
5007 cctx_T *cctx,
5008 char *op,
5009 ppconst_T *ppconst,
5010 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005012 char_u *next;
5013 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 int opchar = *op;
5015
5016 if (p[0] == opchar && p[1] == opchar)
5017 {
5018 garray_T *instr = &cctx->ctx_instr;
5019 garray_T end_ga;
5020
5021 /*
5022 * Repeat until there is no following "||" or "&&"
5023 */
5024 ga_init2(&end_ga, sizeof(int), 10);
5025 while (p[0] == opchar && p[1] == opchar)
5026 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005027 long start_lnum = SOURCING_LNUM;
5028 int start_ctx_lnum = cctx->ctx_lnum;
5029 int save_lnum;
5030
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005031 if (next != NULL)
5032 {
5033 *arg = next_line_from_context(cctx, TRUE);
5034 p = skipwhite(*arg);
5035 }
5036
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005037 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5038 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005039 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005040 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005041 return FAIL;
5042 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005044 // TODO: use ppconst if the value is a constant and check
5045 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005046 generate_ppconst(cctx, ppconst);
5047
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005048 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005049 SOURCING_LNUM = start_lnum;
5050 save_lnum = cctx->ctx_lnum;
5051 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005052 if (bool_on_stack(cctx) == FAIL)
5053 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005054 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005055 ga_clear(&end_ga);
5056 return FAIL;
5057 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005058 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 if (ga_grow(&end_ga, 1) == FAIL)
5061 {
5062 ga_clear(&end_ga);
5063 return FAIL;
5064 }
5065 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5066 ++end_ga.ga_len;
5067 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005068 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069
5070 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005071 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005072 {
5073 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005074 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005075 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005076
Bram Moolenaara5565e42020-05-09 15:44:01 +02005077 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5078 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 {
5080 ga_clear(&end_ga);
5081 return FAIL;
5082 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005083
5084 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005086 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005088 // Every part must evaluate to a bool.
5089 if (bool_on_stack(cctx) == FAIL)
5090 {
5091 ga_clear(&end_ga);
5092 return FAIL;
5093 }
5094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095 // Fill in the end label in all jumps.
5096 while (end_ga.ga_len > 0)
5097 {
5098 isn_T *isn;
5099
5100 --end_ga.ga_len;
5101 isn = ((isn_T *)instr->ga_data)
5102 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5103 isn->isn_arg.jump.jump_where = instr->ga_len;
5104 }
5105 ga_clear(&end_ga);
5106 }
5107
5108 return OK;
5109}
5110
5111/*
5112 * expr4a && expr4a && expr4a logical AND
5113 *
5114 * Produces instructions:
5115 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005116 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005117 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005119 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120 * EVAL expr4c Push result of "expr4c"
5121 * end:
5122 */
5123 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005124compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005126 int ppconst_used = ppconst->pp_used;
5127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005129 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 return FAIL;
5131
5132 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005133 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134}
5135
5136/*
5137 * expr3a || expr3b || expr3c logical OR
5138 *
5139 * Produces instructions:
5140 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005141 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005142 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005143 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005144 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145 * EVAL expr3c Push result of "expr3c"
5146 * end:
5147 */
5148 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005149compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005150{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005151 int ppconst_used = ppconst->pp_used;
5152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005154 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155 return FAIL;
5156
5157 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005158 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005159}
5160
5161/*
5162 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005164 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005165 * JUMP_IF_FALSE alt jump if false
5166 * EVAL expr1a
5167 * JUMP_ALWAYS end
5168 * alt: EVAL expr1b
5169 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005170 *
5171 * Toplevel expression: expr2 ?? expr1
5172 * Produces instructions:
5173 * EVAL expr2 Push result of "expr2"
5174 * JUMP_AND_KEEP_IF_TRUE end jump if true
5175 * EVAL expr1
5176 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 */
5178 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005179compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180{
5181 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005182 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005183 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184
Bram Moolenaar3988f642020-08-27 22:43:03 +02005185 // Ignore all kinds of errors when not producing code.
5186 if (cctx->ctx_skip == SKIP_YES)
5187 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005188 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005189 return OK;
5190 }
5191
Bram Moolenaar61a89812020-05-07 16:58:17 +02005192 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005193 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005194 return FAIL;
5195
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005196 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 if (*p == '?')
5198 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005199 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 garray_T *instr = &cctx->ctx_instr;
5201 garray_T *stack = &cctx->ctx_type_stack;
5202 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005203 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005205 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005206 int has_const_expr = FALSE;
5207 int const_value = FALSE;
5208 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005210 if (next != NULL)
5211 {
5212 *arg = next_line_from_context(cctx, TRUE);
5213 p = skipwhite(*arg);
5214 }
5215
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005216 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005217 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005218 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005219 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005220 return FAIL;
5221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222
Bram Moolenaara5565e42020-05-09 15:44:01 +02005223 if (ppconst->pp_used == ppconst_used + 1)
5224 {
5225 // the condition is a constant, we know whether the ? or the :
5226 // expression is to be evaluated.
5227 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005228 if (op_falsy)
5229 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5230 else
5231 {
5232 int error = FALSE;
5233
5234 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5235 &error);
5236 if (error)
5237 return FAIL;
5238 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005239 cctx->ctx_skip = save_skip == SKIP_YES ||
5240 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5241
5242 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5243 // "left ?? right" and "left" is truthy: produce "left"
5244 generate_ppconst(cctx, ppconst);
5245 else
5246 {
5247 clear_tv(&ppconst->pp_tv[ppconst_used]);
5248 --ppconst->pp_used;
5249 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005250 }
5251 else
5252 {
5253 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005254 if (op_falsy)
5255 end_idx = instr->ga_len;
5256 generate_JUMP(cctx, op_falsy
5257 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5258 if (op_falsy)
5259 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005260 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261
5262 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005263 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005264 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005265 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005266 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267
Bram Moolenaara5565e42020-05-09 15:44:01 +02005268 if (!has_const_expr)
5269 {
5270 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005272 if (!op_falsy)
5273 {
5274 // remember the type and drop it
5275 --stack->ga_len;
5276 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005277
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005278 end_idx = instr->ga_len;
5279 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005280
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005281 // jump here from JUMP_IF_FALSE
5282 isn = ((isn_T *)instr->ga_data) + alt_idx;
5283 isn->isn_arg.jump.jump_where = instr->ga_len;
5284 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005285 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005287 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005289 // Check for the ":".
5290 p = may_peek_next_line(cctx, *arg, &next);
5291 if (*p != ':')
5292 {
5293 emsg(_(e_missing_colon));
5294 return FAIL;
5295 }
5296 if (next != NULL)
5297 {
5298 *arg = next_line_from_context(cctx, TRUE);
5299 p = skipwhite(*arg);
5300 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005301
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005302 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5303 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005304 semsg(_(e_white_space_required_before_and_after_str_at_str),
5305 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005306 return FAIL;
5307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005309 // evaluate the third expression
5310 if (has_const_expr)
5311 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005312 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005313 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005314 return FAIL;
5315 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5316 return FAIL;
5317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318
Bram Moolenaara5565e42020-05-09 15:44:01 +02005319 if (!has_const_expr)
5320 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005321 type_T **typep;
5322
Bram Moolenaara5565e42020-05-09 15:44:01 +02005323 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324
Bram Moolenaara5565e42020-05-09 15:44:01 +02005325 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005326 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5327 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005328
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005329 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005330 isn = ((isn_T *)instr->ga_data) + end_idx;
5331 isn->isn_arg.jump.jump_where = instr->ga_len;
5332 }
5333
5334 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335 }
5336 return OK;
5337}
5338
5339/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005340 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005341 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5342 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005343 */
5344 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005345compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005346{
5347 ppconst_T ppconst;
5348
5349 CLEAR_FIELD(ppconst);
5350 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5351 {
5352 clear_ppconst(&ppconst);
5353 return FAIL;
5354 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005355 if (is_const != NULL)
5356 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005357 if (generate_ppconst(cctx, &ppconst) == FAIL)
5358 return FAIL;
5359 return OK;
5360}
5361
5362/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005363 * Toplevel expression.
5364 */
5365 static int
5366compile_expr0(char_u **arg, cctx_T *cctx)
5367{
5368 return compile_expr0_ext(arg, cctx, NULL);
5369}
5370
5371/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005372 * Compile "return [expr]".
5373 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005374 */
5375 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005376compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377{
5378 char_u *p = arg;
5379 garray_T *stack = &cctx->ctx_type_stack;
5380 type_T *stack_type;
5381
5382 if (*p != NUL && *p != '|' && *p != '\n')
5383 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005384 if (legacy)
5385 {
5386 int save_flags = cmdmod.cmod_flags;
5387
5388 generate_LEGACY_EVAL(cctx, p);
5389 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5390 0, cctx, FALSE, FALSE) == FAIL)
5391 return NULL;
5392 cmdmod.cmod_flags |= CMOD_LEGACY;
5393 (void)skip_expr(&p, NULL);
5394 cmdmod.cmod_flags = save_flags;
5395 }
5396 else
5397 {
5398 // compile return argument into instructions
5399 if (compile_expr0(&p, cctx) == FAIL)
5400 return NULL;
5401 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005403 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005404 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005405 // "check_return_type" with uf_ret_type set to &t_unknown is used
5406 // for an inline function without a specified return type. Set the
5407 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005408 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005409 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005410 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5411 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005412 || (!check_return_type
5413 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005414 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005415 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005416 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005417 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005418 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005419 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5420 && stack_type->tt_type != VAR_VOID
5421 && stack_type->tt_type != VAR_UNKNOWN)
5422 {
5423 emsg(_(e_returning_value_in_function_without_return_type));
5424 return NULL;
5425 }
5426 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005427 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005428 return NULL;
5429 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 }
5432 else
5433 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005434 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005435 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005436 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5437 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005438 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005439 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005440 return NULL;
5441 }
5442
5443 // No argument, return zero.
5444 generate_PUSHNR(cctx, 0);
5445 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005446
5447 // Undo any command modifiers.
5448 generate_undo_cmdmods(cctx);
5449
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005450 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005451 return NULL;
5452
5453 // "return val | endif" is possible
5454 return skipwhite(p);
5455}
5456
5457/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005458 * Get a line from the compilation context, compatible with exarg_T getline().
5459 * Return a pointer to the line in allocated memory.
5460 * Return NULL for end-of-file or some error.
5461 */
5462 static char_u *
5463exarg_getline(
5464 int c UNUSED,
5465 void *cookie,
5466 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005467 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005468{
5469 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005470 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005471
Bram Moolenaar66250c92020-08-20 15:02:42 +02005472 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005473 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005474 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005475 return NULL;
5476 ++cctx->ctx_lnum;
5477 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5478 // Comment lines result in NULL pointers, skip them.
5479 if (p != NULL)
5480 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005481 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005482}
5483
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005484 void
5485fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5486{
5487 eap->getline = exarg_getline;
5488 eap->cookie = cctx;
5489}
5490
Bram Moolenaar04b12692020-05-04 23:24:44 +02005491/*
5492 * Compile a nested :def command.
5493 */
5494 static char_u *
5495compile_nested_function(exarg_T *eap, cctx_T *cctx)
5496{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005497 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005498 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005499 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005500 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005501 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005502 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005503
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005504 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005505 {
5506 emsg(_(e_cannot_use_bang_with_nested_def));
5507 return NULL;
5508 }
5509
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005510 if (*name_start == '/')
5511 {
5512 name_end = skip_regexp(name_start + 1, '/', TRUE);
5513 if (*name_end == '/')
5514 ++name_end;
5515 eap->nextcmd = check_nextcmd(name_end);
5516 }
5517 if (name_end == name_start || *skipwhite(name_end) != '(')
5518 {
5519 if (!ends_excmd2(name_start, name_end))
5520 {
5521 semsg(_(e_invalid_command_str), eap->cmd);
5522 return NULL;
5523 }
5524
5525 // "def" or "def Name": list functions
5526 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5527 return NULL;
5528 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5529 }
5530
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005531 // Only g:Func() can use a namespace.
5532 if (name_start[1] == ':' && !is_global)
5533 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005534 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005535 return NULL;
5536 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005537 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005538 return NULL;
5539
Bram Moolenaar04b12692020-05-04 23:24:44 +02005540 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005541 fill_exarg_from_cctx(eap, cctx);
5542
Bram Moolenaar04b12692020-05-04 23:24:44 +02005543 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005544 lambda_name = vim_strsave(get_lambda_name());
5545 if (lambda_name == NULL)
5546 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005547 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005548
Bram Moolenaar822ba242020-05-24 23:00:18 +02005549 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005550 {
5551 r = eap->skip ? OK : FAIL;
5552 goto theend;
5553 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005554
5555 // copy over the block scope IDs before compiling
5556 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5557 {
5558 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5559
5560 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5561 if (ufunc->uf_block_ids != NULL)
5562 {
5563 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5564 sizeof(int) * block_depth);
5565 ufunc->uf_block_depth = block_depth;
5566 }
5567 }
5568
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005569 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5570 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005571 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005572 {
5573 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005574 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005575 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005576
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005577 if (is_global)
5578 {
5579 char_u *func_name = vim_strnsave(name_start + 2,
5580 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005581
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005582 if (func_name == NULL)
5583 r = FAIL;
5584 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005585 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005586 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005587 lambda_name = NULL;
5588 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005589 }
5590 else
5591 {
5592 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005593 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005594 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005595
Bram Moolenaareef21022020-08-01 22:16:43 +02005596 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005597 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005598 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005599 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005600 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5601 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005602 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005603
5604theend:
5605 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005606 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005607}
5608
5609/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005610 * Return the length of an assignment operator, or zero if there isn't one.
5611 */
5612 int
5613assignment_len(char_u *p, int *heredoc)
5614{
5615 if (*p == '=')
5616 {
5617 if (p[1] == '<' && p[2] == '<')
5618 {
5619 *heredoc = TRUE;
5620 return 3;
5621 }
5622 return 1;
5623 }
5624 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5625 return 2;
5626 if (STRNCMP(p, "..=", 3) == 0)
5627 return 3;
5628 return 0;
5629}
5630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005631/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005632 * Generate the load instruction for "name".
5633 */
5634 static void
5635generate_loadvar(
5636 cctx_T *cctx,
5637 assign_dest_T dest,
5638 char_u *name,
5639 lvar_T *lvar,
5640 type_T *type)
5641{
5642 switch (dest)
5643 {
5644 case dest_option:
5645 // TODO: check the option exists
5646 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5647 break;
5648 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005649 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5650 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5651 else
5652 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005653 break;
5654 case dest_buffer:
5655 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5656 break;
5657 case dest_window:
5658 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5659 break;
5660 case dest_tab:
5661 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5662 break;
5663 case dest_script:
5664 compile_load_scriptvar(cctx,
5665 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5666 break;
5667 case dest_env:
5668 // Include $ in the name here
5669 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5670 break;
5671 case dest_reg:
5672 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5673 break;
5674 case dest_vimvar:
5675 generate_LOADV(cctx, name + 2, TRUE);
5676 break;
5677 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005678 if (lvar->lv_from_outer > 0)
5679 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5680 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005681 else
5682 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5683 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005684 case dest_expr:
5685 // list or dict value should already be on the stack.
5686 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005687 }
5688}
5689
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005690/*
5691 * Skip over "[expr]" or ".member".
5692 * Does not check for any errors.
5693 */
5694 static char_u *
5695skip_index(char_u *start)
5696{
5697 char_u *p = start;
5698
5699 if (*p == '[')
5700 {
5701 p = skipwhite(p + 1);
5702 (void)skip_expr(&p, NULL);
5703 p = skipwhite(p);
5704 if (*p == ']')
5705 return p + 1;
5706 return p;
5707 }
5708 // if (*p == '.')
5709 return to_name_end(p + 1, TRUE);
5710}
5711
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005712 void
5713vim9_declare_error(char_u *name)
5714{
5715 char *scope = "";
5716
5717 switch (*name)
5718 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005719 case 'g': scope = _("global"); break;
5720 case 'b': scope = _("buffer"); break;
5721 case 'w': scope = _("window"); break;
5722 case 't': scope = _("tab"); break;
5723 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005724 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005725 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005726 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005727 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005728 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005729 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005730 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005731 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005732 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005733}
5734
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005735/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005736 * For one assignment figure out the type of destination. Return it in "dest".
5737 * When not recognized "dest" is not set.
5738 * For an option "opt_flags" is set.
5739 * For a v:var "vimvaridx" is set.
5740 * "type" is set to the destination type if known, unchanted otherwise.
5741 * Return FAIL if an error message was given.
5742 */
5743 static int
5744get_var_dest(
5745 char_u *name,
5746 assign_dest_T *dest,
5747 int cmdidx,
5748 int *opt_flags,
5749 int *vimvaridx,
5750 type_T **type,
5751 cctx_T *cctx)
5752{
5753 char_u *p;
5754
5755 if (*name == '&')
5756 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005757 int cc;
5758 long numval;
5759 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005760
5761 *dest = dest_option;
5762 if (cmdidx == CMD_final || cmdidx == CMD_const)
5763 {
5764 emsg(_(e_const_option));
5765 return FAIL;
5766 }
5767 p = name;
5768 p = find_option_end(&p, opt_flags);
5769 if (p == NULL)
5770 {
5771 // cannot happen?
5772 emsg(_(e_letunexp));
5773 return FAIL;
5774 }
5775 cc = *p;
5776 *p = NUL;
5777 opt_type = get_option_value(skip_option_env_lead(name),
5778 &numval, NULL, *opt_flags);
5779 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005780 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005781 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005782 case gov_unknown:
5783 semsg(_(e_unknown_option), name);
5784 return FAIL;
5785 case gov_string:
5786 case gov_hidden_string:
5787 *type = &t_string;
5788 break;
5789 case gov_bool:
5790 case gov_hidden_bool:
5791 *type = &t_bool;
5792 break;
5793 case gov_number:
5794 case gov_hidden_number:
5795 *type = &t_number;
5796 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005797 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005798 }
5799 else if (*name == '$')
5800 {
5801 *dest = dest_env;
5802 *type = &t_string;
5803 }
5804 else if (*name == '@')
5805 {
5806 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5807 {
5808 emsg_invreg(name[1]);
5809 return FAIL;
5810 }
5811 *dest = dest_reg;
5812 *type = &t_string;
5813 }
5814 else if (STRNCMP(name, "g:", 2) == 0)
5815 {
5816 *dest = dest_global;
5817 }
5818 else if (STRNCMP(name, "b:", 2) == 0)
5819 {
5820 *dest = dest_buffer;
5821 }
5822 else if (STRNCMP(name, "w:", 2) == 0)
5823 {
5824 *dest = dest_window;
5825 }
5826 else if (STRNCMP(name, "t:", 2) == 0)
5827 {
5828 *dest = dest_tab;
5829 }
5830 else if (STRNCMP(name, "v:", 2) == 0)
5831 {
5832 typval_T *vtv;
5833 int di_flags;
5834
5835 *vimvaridx = find_vim_var(name + 2, &di_flags);
5836 if (*vimvaridx < 0)
5837 {
5838 semsg(_(e_variable_not_found_str), name);
5839 return FAIL;
5840 }
5841 // We use the current value of "sandbox" here, is that OK?
5842 if (var_check_ro(di_flags, name, FALSE))
5843 return FAIL;
5844 *dest = dest_vimvar;
5845 vtv = get_vim_var_tv(*vimvaridx);
5846 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5847 }
5848 return OK;
5849}
5850
5851/*
5852 * Generate a STORE instruction for "dest", not being "dest_local".
5853 * Return FAIL when out of memory.
5854 */
5855 static int
5856generate_store_var(
5857 cctx_T *cctx,
5858 assign_dest_T dest,
5859 int opt_flags,
5860 int vimvaridx,
5861 int scriptvar_idx,
5862 int scriptvar_sid,
5863 type_T *type,
5864 char_u *name)
5865{
5866 switch (dest)
5867 {
5868 case dest_option:
5869 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5870 opt_flags);
5871 case dest_global:
5872 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005873 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5874 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005875 case dest_buffer:
5876 // include b: with the name, easier to execute that way
5877 return generate_STORE(cctx, ISN_STOREB, 0, name);
5878 case dest_window:
5879 // include w: with the name, easier to execute that way
5880 return generate_STORE(cctx, ISN_STOREW, 0, name);
5881 case dest_tab:
5882 // include t: with the name, easier to execute that way
5883 return generate_STORE(cctx, ISN_STORET, 0, name);
5884 case dest_env:
5885 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5886 case dest_reg:
5887 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5888 case dest_vimvar:
5889 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5890 case dest_script:
5891 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005892 // "s:" may be included in the name.
5893 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005894 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005895 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5896 scriptvar_sid, scriptvar_idx, type);
5897 case dest_local:
5898 case dest_expr:
5899 // cannot happen
5900 break;
5901 }
5902 return FAIL;
5903}
5904
Bram Moolenaar752fc692021-01-04 21:57:11 +01005905 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005906generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5907{
5908 if (lhs->lhs_dest != dest_local)
5909 return generate_store_var(cctx, lhs->lhs_dest,
5910 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5911 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5912 lhs->lhs_type, lhs->lhs_name);
5913
5914 if (lhs->lhs_lvar != NULL)
5915 {
5916 garray_T *instr = &cctx->ctx_instr;
5917 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5918
5919 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5920 // ISN_STORENR
5921 if (lhs->lhs_lvar->lv_from_outer == 0
5922 && instr->ga_len == instr_count + 1
5923 && isn->isn_type == ISN_PUSHNR)
5924 {
5925 varnumber_T val = isn->isn_arg.number;
5926 garray_T *stack = &cctx->ctx_type_stack;
5927
5928 isn->isn_type = ISN_STORENR;
5929 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5930 isn->isn_arg.storenr.stnr_val = val;
5931 if (stack->ga_len > 0)
5932 --stack->ga_len;
5933 }
5934 else if (lhs->lhs_lvar->lv_from_outer > 0)
5935 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5936 lhs->lhs_lvar->lv_from_outer);
5937 else
5938 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5939 }
5940 return OK;
5941}
5942
5943 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005944is_decl_command(int cmdidx)
5945{
5946 return cmdidx == CMD_let || cmdidx == CMD_var
5947 || cmdidx == CMD_final || cmdidx == CMD_const;
5948}
5949
5950/*
5951 * Figure out the LHS type and other properties for an assignment or one item
5952 * of ":unlet" with an index.
5953 * Returns OK or FAIL.
5954 */
5955 static int
5956compile_lhs(
5957 char_u *var_start,
5958 lhs_T *lhs,
5959 int cmdidx,
5960 int heredoc,
5961 int oplen,
5962 cctx_T *cctx)
5963{
5964 char_u *var_end;
5965 int is_decl = is_decl_command(cmdidx);
5966
5967 CLEAR_POINTER(lhs);
5968 lhs->lhs_dest = dest_local;
5969 lhs->lhs_vimvaridx = -1;
5970 lhs->lhs_scriptvar_idx = -1;
5971
5972 // "dest_end" is the end of the destination, including "[expr]" or
5973 // ".name".
5974 // "var_end" is the end of the variable/option/etc. name.
5975 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5976 if (*var_start == '@')
5977 var_end = var_start + 2;
5978 else
5979 {
5980 // skip over the leading "&", "&l:", "&g:" and "$"
5981 var_end = skip_option_env_lead(var_start);
5982 var_end = to_name_end(var_end, TRUE);
5983 }
5984
5985 // "a: type" is declaring variable "a" with a type, not dict "a:".
5986 if (is_decl && lhs->lhs_dest_end == var_start + 2
5987 && lhs->lhs_dest_end[-1] == ':')
5988 --lhs->lhs_dest_end;
5989 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5990 --var_end;
5991
5992 // compute the length of the destination without "[expr]" or ".name"
5993 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02005994 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005995 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5996 if (lhs->lhs_name == NULL)
5997 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005998
5999 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6000 // Something follows after the variable: "var[idx]" or "var.key".
6001 lhs->lhs_has_index = TRUE;
6002
Bram Moolenaar752fc692021-01-04 21:57:11 +01006003 if (heredoc)
6004 lhs->lhs_type = &t_list_string;
6005 else
6006 lhs->lhs_type = &t_any;
6007
6008 if (cctx->ctx_skip != SKIP_YES)
6009 {
6010 int declare_error = FALSE;
6011
6012 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6013 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6014 &lhs->lhs_type, cctx) == FAIL)
6015 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006016 if (lhs->lhs_dest != dest_local
6017 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006018 {
6019 // Specific kind of variable recognized.
6020 declare_error = is_decl;
6021 }
6022 else
6023 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006024 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006025 if (check_reserved_name(lhs->lhs_name) == FAIL)
6026 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006027
6028 if (lookup_local(var_start, lhs->lhs_varlen,
6029 &lhs->lhs_local_lvar, cctx) == OK)
6030 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6031 else
6032 {
6033 CLEAR_FIELD(lhs->lhs_arg_lvar);
6034 if (arg_exists(var_start, lhs->lhs_varlen,
6035 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6036 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6037 {
6038 if (is_decl)
6039 {
6040 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6041 return FAIL;
6042 }
6043 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6044 }
6045 }
6046 if (lhs->lhs_lvar != NULL)
6047 {
6048 if (is_decl)
6049 {
6050 semsg(_(e_variable_already_declared), lhs->lhs_name);
6051 return FAIL;
6052 }
6053 }
6054 else
6055 {
6056 int script_namespace = lhs->lhs_varlen > 1
6057 && STRNCMP(var_start, "s:", 2) == 0;
6058 int script_var = (script_namespace
6059 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006060 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006061 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006062 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006063 imported_T *import =
6064 find_imported(var_start, lhs->lhs_varlen, cctx);
6065
6066 if (script_namespace || script_var || import != NULL)
6067 {
6068 char_u *rawname = lhs->lhs_name
6069 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6070
6071 if (is_decl)
6072 {
6073 if (script_namespace)
6074 semsg(_(e_cannot_declare_script_variable_in_function),
6075 lhs->lhs_name);
6076 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006077 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006078 lhs->lhs_name);
6079 return FAIL;
6080 }
6081 else if (cctx->ctx_ufunc->uf_script_ctx_version
6082 == SCRIPT_VERSION_VIM9
6083 && script_namespace
6084 && !script_var && import == NULL)
6085 {
6086 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6087 return FAIL;
6088 }
6089
6090 lhs->lhs_dest = dest_script;
6091
6092 // existing script-local variables should have a type
6093 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6094 if (import != NULL)
6095 lhs->lhs_scriptvar_sid = import->imp_sid;
6096 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6097 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006098 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006099 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006100 lhs->lhs_scriptvar_sid, rawname,
6101 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6102 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006103 if (lhs->lhs_scriptvar_idx >= 0)
6104 {
6105 scriptitem_T *si = SCRIPT_ITEM(
6106 lhs->lhs_scriptvar_sid);
6107 svar_T *sv =
6108 ((svar_T *)si->sn_var_vals.ga_data)
6109 + lhs->lhs_scriptvar_idx;
6110 lhs->lhs_type = sv->sv_type;
6111 }
6112 }
6113 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006114 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006115 == FAIL)
6116 return FAIL;
6117 }
6118 }
6119
6120 if (declare_error)
6121 {
6122 vim9_declare_error(lhs->lhs_name);
6123 return FAIL;
6124 }
6125 }
6126
6127 // handle "a:name" as a name, not index "name" on "a"
6128 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6129 var_end = lhs->lhs_dest_end;
6130
6131 if (lhs->lhs_dest != dest_option)
6132 {
6133 if (is_decl && *var_end == ':')
6134 {
6135 char_u *p;
6136
6137 // parse optional type: "let var: type = expr"
6138 if (!VIM_ISWHITE(var_end[1]))
6139 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006140 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006141 return FAIL;
6142 }
6143 p = skipwhite(var_end + 1);
6144 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6145 if (lhs->lhs_type == NULL)
6146 return FAIL;
6147 lhs->lhs_has_type = TRUE;
6148 }
6149 else if (lhs->lhs_lvar != NULL)
6150 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6151 }
6152
Bram Moolenaare42939a2021-04-05 17:11:17 +02006153 if (oplen == 3 && !heredoc
6154 && lhs->lhs_dest != dest_global
6155 && !lhs->lhs_has_index
6156 && lhs->lhs_type->tt_type != VAR_STRING
6157 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006158 {
6159 emsg(_(e_can_only_concatenate_to_string));
6160 return FAIL;
6161 }
6162
6163 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6164 && cctx->ctx_skip != SKIP_YES)
6165 {
6166 if (oplen > 1 && !heredoc)
6167 {
6168 // +=, /=, etc. require an existing variable
6169 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6170 return FAIL;
6171 }
6172 if (!is_decl)
6173 {
6174 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6175 return FAIL;
6176 }
6177
Bram Moolenaar3f327882021-03-17 20:56:38 +01006178 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006179 if ((lhs->lhs_type->tt_type == VAR_FUNC
6180 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006181 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006182 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006183
6184 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006185 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6186 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6187 if (lhs->lhs_lvar == NULL)
6188 return FAIL;
6189 lhs->lhs_new_local = TRUE;
6190 }
6191
6192 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006193 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006194 {
6195 // Something follows after the variable: "var[idx]" or "var.key".
6196 // TODO: should we also handle "->func()" here?
6197 if (is_decl)
6198 {
6199 emsg(_(e_cannot_use_index_when_declaring_variable));
6200 return FAIL;
6201 }
6202
6203 if (var_start[lhs->lhs_varlen] == '['
6204 || var_start[lhs->lhs_varlen] == '.')
6205 {
6206 char_u *after = var_start + lhs->lhs_varlen;
6207 char_u *p;
6208
6209 // Only the last index is used below, if there are others
6210 // before it generate code for the expression. Thus for
6211 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6212 for (;;)
6213 {
6214 p = skip_index(after);
6215 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006216 {
6217 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006218 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006219 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006220 after = p;
6221 }
6222 if (after > var_start + lhs->lhs_varlen)
6223 {
6224 lhs->lhs_varlen = after - var_start;
6225 lhs->lhs_dest = dest_expr;
6226 // We don't know the type before evaluating the expression,
6227 // use "any" until then.
6228 lhs->lhs_type = &t_any;
6229 }
6230
Bram Moolenaar752fc692021-01-04 21:57:11 +01006231 if (lhs->lhs_type->tt_member == NULL)
6232 lhs->lhs_member_type = &t_any;
6233 else
6234 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6235 }
6236 else
6237 {
6238 semsg("Not supported yet: %s", var_start);
6239 return FAIL;
6240 }
6241 }
6242 return OK;
6243}
6244
6245/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006246 * Figure out the LHS and check a few errors.
6247 */
6248 static int
6249compile_assign_lhs(
6250 char_u *var_start,
6251 lhs_T *lhs,
6252 int cmdidx,
6253 int is_decl,
6254 int heredoc,
6255 int oplen,
6256 cctx_T *cctx)
6257{
6258 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6259 return FAIL;
6260
6261 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6262 {
6263 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6264 return FAIL;
6265 }
6266 if (!is_decl && lhs->lhs_lvar != NULL
6267 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6268 {
6269 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6270 return FAIL;
6271 }
6272 return OK;
6273}
6274
6275/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006276 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6277 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006278 */
6279 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006280compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006281 char_u *var_start,
6282 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006283 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006284 cctx_T *cctx)
6285{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006286 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006287 char_u *p;
6288 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006289 int need_white_before = TRUE;
6290 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006291
Bram Moolenaar752fc692021-01-04 21:57:11 +01006292 p = var_start + varlen;
6293 if (*p == '[')
6294 {
6295 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006296 if (*p == ':')
6297 {
6298 // empty first index, push zero
6299 r = generate_PUSHNR(cctx, 0);
6300 need_white_before = FALSE;
6301 }
6302 else
6303 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006304
6305 if (r == OK && *skipwhite(p) == ':')
6306 {
6307 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006308 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006309 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006310 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006311 empty_second = *skipwhite(p + 1) == ']';
6312 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6313 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006314 {
6315 semsg(_(e_white_space_required_before_and_after_str_at_str),
6316 ":", p);
6317 return FAIL;
6318 }
6319 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006320 if (*p == ']')
6321 // empty second index, push "none"
6322 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6323 else
6324 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006325 }
6326
Bram Moolenaar752fc692021-01-04 21:57:11 +01006327 if (r == OK && *skipwhite(p) != ']')
6328 {
6329 // this should not happen
6330 emsg(_(e_missbrac));
6331 r = FAIL;
6332 }
6333 }
6334 else // if (*p == '.')
6335 {
6336 char_u *key_end = to_name_end(p + 1, TRUE);
6337 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6338
6339 r = generate_PUSHS(cctx, key);
6340 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006341 return r;
6342}
6343
6344/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006345 * For a LHS with an index, load the variable to be indexed.
6346 */
6347 static int
6348compile_load_lhs(
6349 lhs_T *lhs,
6350 char_u *var_start,
6351 type_T *rhs_type,
6352 cctx_T *cctx)
6353{
6354 if (lhs->lhs_dest == dest_expr)
6355 {
6356 size_t varlen = lhs->lhs_varlen;
6357 int c = var_start[varlen];
6358 char_u *p = var_start;
6359 garray_T *stack = &cctx->ctx_type_stack;
6360
6361 // Evaluate "ll[expr]" of "ll[expr][idx]"
6362 var_start[varlen] = NUL;
6363 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6364 {
6365 // this should not happen
6366 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006367 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006368 return FAIL;
6369 }
6370 var_start[varlen] = c;
6371
6372 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6373 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6374 // now we can properly check the type
6375 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6376 && rhs_type != &t_void
6377 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6378 FALSE, FALSE) == FAIL)
6379 return FAIL;
6380 }
6381 else
6382 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6383 lhs->lhs_lvar, lhs->lhs_type);
6384 return OK;
6385}
6386
6387/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006388 * Produce code for loading "lhs" and also take care of an index.
6389 * Return OK/FAIL.
6390 */
6391 static int
6392compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6393{
6394 compile_load_lhs(lhs, var_start, NULL, cctx);
6395
6396 if (lhs->lhs_has_index)
6397 {
6398 int range = FALSE;
6399
6400 // Get member from list or dict. First compile the
6401 // index value.
6402 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6403 return FAIL;
6404 if (range)
6405 {
6406 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6407 var_start);
6408 return FAIL;
6409 }
6410
6411 // Get the member.
6412 if (compile_member(FALSE, cctx) == FAIL)
6413 return FAIL;
6414 }
6415 return OK;
6416}
6417
6418/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006419 * Assignment to a list or dict member, or ":unlet" for the item, using the
6420 * information in "lhs".
6421 * Returns OK or FAIL.
6422 */
6423 static int
6424compile_assign_unlet(
6425 char_u *var_start,
6426 lhs_T *lhs,
6427 int is_assign,
6428 type_T *rhs_type,
6429 cctx_T *cctx)
6430{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006431 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006432 garray_T *stack = &cctx->ctx_type_stack;
6433 int range = FALSE;
6434
Bram Moolenaar68452172021-04-12 21:21:02 +02006435 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006436 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006437 if (is_assign && range && lhs->lhs_type != &t_blob
6438 && lhs->lhs_type != &t_any)
6439 {
6440 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6441 return FAIL;
6442 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006443
6444 if (lhs->lhs_type == &t_any)
6445 {
6446 // Index on variable of unknown type: check at runtime.
6447 dest_type = VAR_ANY;
6448 }
6449 else
6450 {
6451 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006452 if (dest_type == VAR_DICT && range)
6453 {
6454 emsg(e_cannot_use_range_with_dictionary);
6455 return FAIL;
6456 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006457 if (dest_type == VAR_DICT
6458 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006459 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006460 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006461 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006462 type_T *type;
6463
6464 if (range)
6465 {
6466 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6467 if (need_type(type, &t_number,
6468 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006469 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006470 }
6471 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6472 if ((dest_type != VAR_BLOB || type != &t_special)
6473 && need_type(type, &t_number,
6474 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006475 return FAIL;
6476 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006477 }
6478
6479 // Load the dict or list. On the stack we then have:
6480 // - value (for assignment, not for :unlet)
6481 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006482 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006483 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006484 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6485 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006486
Bram Moolenaar68452172021-04-12 21:21:02 +02006487 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6488 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006489 {
6490 if (is_assign)
6491 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006492 if (range)
6493 {
6494 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6495 return FAIL;
6496 }
6497 else
6498 {
6499 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006500
Bram Moolenaar68452172021-04-12 21:21:02 +02006501 if (isn == NULL)
6502 return FAIL;
6503 isn->isn_arg.vartype = dest_type;
6504 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006505 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006506 else if (range)
6507 {
6508 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6509 return FAIL;
6510 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006511 else
6512 {
6513 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6514 return FAIL;
6515 }
6516 }
6517 else
6518 {
6519 emsg(_(e_indexable_type_required));
6520 return FAIL;
6521 }
6522
6523 return OK;
6524}
6525
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006526/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006527 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006528 * "let name"
6529 * "var name = expr"
6530 * "final name = expr"
6531 * "const name = expr"
6532 * "name = expr"
6533 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006534 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006535 * Return NULL for an error.
6536 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537 */
6538 static char_u *
6539compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6540{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006541 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006543 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 char_u *ret = NULL;
6545 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006546 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006548 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006549 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551 int oplen = 0;
6552 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006553 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006554 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006555 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006556 int is_decl = is_decl_command(cmdidx);
6557 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006558 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006559
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006560 // Skip over the "var" or "[var, var]" to get to any "=".
6561 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6562 if (p == NULL)
6563 return *arg == '[' ? arg : NULL;
6564
6565 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006567 // TODO: should we allow this, and figure out type inference from list
6568 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006569 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006570 return NULL;
6571 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006572 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006574 sp = p;
6575 p = skipwhite(p);
6576 op = p;
6577 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006578
6579 if (var_count > 0 && oplen == 0)
6580 // can be something like "[1, 2]->func()"
6581 return arg;
6582
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006583 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006584 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006585 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006586 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006587 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006588 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6589 {
6590 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6591 oplen = 2;
6592 incdec = TRUE;
6593 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 if (heredoc)
6596 {
6597 list_T *l;
6598 listitem_T *li;
6599
6600 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006601 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006603 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006604 if (l == NULL)
6605 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606
Bram Moolenaar078269b2020-09-21 20:35:55 +02006607 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006609 // Push each line and the create the list.
6610 FOR_ALL_LIST_ITEMS(l, li)
6611 {
6612 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6613 li->li_tv.vval.v_string = NULL;
6614 }
6615 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 list_free(l);
6618 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006619 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006621 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006623 char_u *wp;
6624
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006625 // for "[var, var] = expr" evaluate the expression here, loop over the
6626 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006627 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006628
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006629 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006630 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006631 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006632 if (compile_expr0(&p, cctx) == FAIL)
6633 return NULL;
6634 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006635
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006636 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006638 type_T *stacktype;
6639
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006640 stacktype = stack->ga_len == 0 ? &t_void
6641 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006642 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006644 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006645 goto theend;
6646 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006647 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006648 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006649 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006650 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006651 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6652 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006653 if (stacktype->tt_member != NULL)
6654 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006655 }
6656 }
6657
6658 /*
6659 * Loop over variables in "[var, var] = expr".
6660 * For "var = expr" and "let var: type" this is done only once.
6661 */
6662 if (var_count > 0)
6663 var_start = skipwhite(arg + 1); // skip over the "["
6664 else
6665 var_start = arg;
6666 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6667 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006668 int instr_count = -1;
6669
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006670 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6671 {
6672 // Ignore underscore in "[a, _, b] = list".
6673 if (var_count > 0)
6674 {
6675 var_start = skipwhite(var_start + 2);
6676 continue;
6677 }
6678 emsg(_(e_cannot_use_underscore_here));
6679 goto theend;
6680 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006681 vim_free(lhs.lhs_name);
6682
6683 /*
6684 * Figure out the LHS type and other properties.
6685 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006686 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6687 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006688 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006689 if (!heredoc)
6690 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006691 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006692 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006693 if (oplen > 0 && var_count == 0)
6694 {
6695 // skip over the "=" and the expression
6696 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006697 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006698 }
6699 }
6700 else if (oplen > 0)
6701 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006702 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006703 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006704
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006705 // For "var = expr" evaluate the expression.
6706 if (var_count == 0)
6707 {
6708 int r;
6709
6710 // for "+=", "*=", "..=" etc. first load the current value
6711 if (*op != '=')
6712 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006713 if (compile_load_lhs_with_index(&lhs, var_start,
6714 cctx) == FAIL)
6715 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006716 }
6717
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006718 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006719 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006720 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006721 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006722 r = generate_PUSHNR(cctx, 1);
6723 }
6724 else
6725 {
6726 // Temporarily hide the new local variable here, it is
6727 // not available to this expression.
6728 if (lhs.lhs_new_local)
6729 --cctx->ctx_locals.ga_len;
6730 wp = op + oplen;
6731 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6732 {
6733 if (lhs.lhs_new_local)
6734 ++cctx->ctx_locals.ga_len;
6735 goto theend;
6736 }
6737 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006738 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006739 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006740 if (r == FAIL)
6741 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006742 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006743 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006744 else if (semicolon && var_idx == var_count - 1)
6745 {
6746 // For "[var; var] = expr" get the rest of the list
6747 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6748 goto theend;
6749 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006750 else
6751 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006752 // For "[var, var] = expr" get the "var_idx" item from the
6753 // list.
6754 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006755 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006756 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006757
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006758 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006759 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006760 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006761 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006762 if ((rhs_type->tt_type == VAR_FUNC
6763 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006764 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006765 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006766 goto theend;
6767
Bram Moolenaar752fc692021-01-04 21:57:11 +01006768 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006769 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006770 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006771 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006772 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006773 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006774 }
6775 else
6776 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006777 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006778 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006779 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006780 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006781 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006782 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006783 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006784 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006785 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006786 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006787 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006788 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006789 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006790 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006791 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006792
Bram Moolenaar77709b12021-04-03 21:01:01 +02006793 // Without operator check type here, otherwise below.
6794 // Use the line number of the assignment.
6795 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006796 if (lhs.lhs_has_index)
6797 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006798 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006799 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006800 goto theend;
6801 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006802 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006803 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006804 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006805 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006807 else if (cmdidx == CMD_final)
6808 {
6809 emsg(_(e_final_requires_a_value));
6810 goto theend;
6811 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006812 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006813 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006814 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006815 goto theend;
6816 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006817 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006818 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006819 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006820 goto theend;
6821 }
6822 else
6823 {
6824 // variables are always initialized
6825 if (ga_grow(instr, 1) == FAIL)
6826 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006827 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006828 {
6829 case VAR_BOOL:
6830 generate_PUSHBOOL(cctx, VVAL_FALSE);
6831 break;
6832 case VAR_FLOAT:
6833#ifdef FEAT_FLOAT
6834 generate_PUSHF(cctx, 0.0);
6835#endif
6836 break;
6837 case VAR_STRING:
6838 generate_PUSHS(cctx, NULL);
6839 break;
6840 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006841 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006842 break;
6843 case VAR_FUNC:
6844 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6845 break;
6846 case VAR_LIST:
6847 generate_NEWLIST(cctx, 0);
6848 break;
6849 case VAR_DICT:
6850 generate_NEWDICT(cctx, 0);
6851 break;
6852 case VAR_JOB:
6853 generate_PUSHJOB(cctx, NULL);
6854 break;
6855 case VAR_CHANNEL:
6856 generate_PUSHCHANNEL(cctx, NULL);
6857 break;
6858 case VAR_NUMBER:
6859 case VAR_UNKNOWN:
6860 case VAR_ANY:
6861 case VAR_PARTIAL:
6862 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006863 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006864 case VAR_SPECIAL: // cannot happen
6865 generate_PUSHNR(cctx, 0);
6866 break;
6867 }
6868 }
6869 if (var_count == 0)
6870 end = p;
6871 }
6872
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006873 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006874 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006875 break;
6876
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006877 if (oplen > 0 && *op != '=')
6878 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006879 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006880 type_T *stacktype;
6881
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006882 if (*op == '.')
6883 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006884 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006885 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006886 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006887 if (
6888#ifdef FEAT_FLOAT
6889 // If variable is float operation with number is OK.
6890 !(expected == &t_float && stacktype == &t_number) &&
6891#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006892 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006893 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006894 goto theend;
6895
6896 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006897 {
6898 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6899 goto theend;
6900 }
6901 else if (*op == '+')
6902 {
6903 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006904 operator_type(lhs.lhs_member_type, stacktype),
6905 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006906 goto theend;
6907 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006908 else if (generate_two_op(cctx, op) == FAIL)
6909 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006910 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006911
Bram Moolenaar752fc692021-01-04 21:57:11 +01006912 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006913 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006914 // Use the info in "lhs" to store the value at the index in the
6915 // list or dict.
6916 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6917 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006918 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006919 }
6920 else
6921 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006922 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006923 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006924 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006925 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006926 generate_LOCKCONST(cctx);
6927
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006928 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006929 && (lhs.lhs_type->tt_type == VAR_DICT
6930 || lhs.lhs_type->tt_type == VAR_LIST)
6931 && lhs.lhs_type->tt_member != NULL
6932 && lhs.lhs_type->tt_member != &t_any
6933 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006934 // Set the type in the list or dict, so that it can be checked,
6935 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006936 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006937
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006938 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6939 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006940 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006941
6942 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006943 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006945
6946 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006947 if (var_count > 0 && !semicolon)
6948 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006949 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006950 goto theend;
6951 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006952
Bram Moolenaarb2097502020-07-19 17:17:02 +02006953 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954
6955theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006956 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957 return ret;
6958}
6959
6960/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006961 * Check for an assignment at "eap->cmd", compile it if found.
6962 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6963 */
6964 static int
6965may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6966{
6967 char_u *pskip;
6968 char_u *p;
6969
6970 // Assuming the command starts with a variable or function name,
6971 // find what follows.
6972 // Skip over "var.member", "var[idx]" and the like.
6973 // Also "&opt = val", "$ENV = val" and "@r = val".
6974 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6975 ? eap->cmd + 1 : eap->cmd;
6976 p = to_name_end(pskip, TRUE);
6977 if (p > eap->cmd && *p != NUL)
6978 {
6979 char_u *var_end;
6980 int oplen;
6981 int heredoc;
6982
6983 if (eap->cmd[0] == '@')
6984 var_end = eap->cmd + 2;
6985 else
6986 var_end = find_name_end(pskip, NULL, NULL,
6987 FNE_CHECK_START | FNE_INCL_BR);
6988 oplen = assignment_len(skipwhite(var_end), &heredoc);
6989 if (oplen > 0)
6990 {
6991 size_t len = p - eap->cmd;
6992
6993 // Recognize an assignment if we recognize the variable
6994 // name:
6995 // "g:var = expr"
6996 // "local = expr" where "local" is a local var.
6997 // "script = expr" where "script" is a script-local var.
6998 // "import = expr" where "import" is an imported var
6999 // "&opt = expr"
7000 // "$ENV = expr"
7001 // "@r = expr"
7002 if (*eap->cmd == '&'
7003 || *eap->cmd == '$'
7004 || *eap->cmd == '@'
7005 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007006 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007007 {
7008 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7009 if (*line == NULL || *line == eap->cmd)
7010 return FAIL;
7011 return OK;
7012 }
7013 }
7014 }
7015
7016 if (*eap->cmd == '[')
7017 {
7018 // [var, var] = expr
7019 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7020 if (*line == NULL)
7021 return FAIL;
7022 if (*line != eap->cmd)
7023 return OK;
7024 }
7025 return NOTDONE;
7026}
7027
7028/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007029 * Check if "name" can be "unlet".
7030 */
7031 int
7032check_vim9_unlet(char_u *name)
7033{
7034 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7035 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007036 // "unlet s:var" is allowed in legacy script.
7037 if (*name == 's' && !script_is_vim9())
7038 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007039 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007040 return FAIL;
7041 }
7042 return OK;
7043}
7044
7045/*
7046 * Callback passed to ex_unletlock().
7047 */
7048 static int
7049compile_unlet(
7050 lval_T *lvp,
7051 char_u *name_end,
7052 exarg_T *eap,
7053 int deep UNUSED,
7054 void *coookie)
7055{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007056 cctx_T *cctx = coookie;
7057 char_u *p = lvp->ll_name;
7058 int cc = *name_end;
7059 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007060
Bram Moolenaar752fc692021-01-04 21:57:11 +01007061 if (cctx->ctx_skip == SKIP_YES)
7062 return OK;
7063
7064 *name_end = NUL;
7065 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007066 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007067 // :unlet $ENV_VAR
7068 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7069 }
7070 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7071 {
7072 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007073
Bram Moolenaar752fc692021-01-04 21:57:11 +01007074 // This is similar to assigning: lookup the list/dict, compile the
7075 // idx/key. Then instead of storing the value unlet the item.
7076 // unlet {list}[idx]
7077 // unlet {dict}[key] dict.key
7078 //
7079 // Figure out the LHS type and other properties.
7080 //
7081 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7082
7083 // : unlet an indexed item
7084 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007085 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007086 iemsg("called compile_lhs() without an index");
7087 ret = FAIL;
7088 }
7089 else
7090 {
7091 // Use the info in "lhs" to unlet the item at the index in the
7092 // list or dict.
7093 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007094 }
7095
Bram Moolenaar752fc692021-01-04 21:57:11 +01007096 vim_free(lhs.lhs_name);
7097 }
7098 else if (check_vim9_unlet(p) == FAIL)
7099 {
7100 ret = FAIL;
7101 }
7102 else
7103 {
7104 // Normal name. Only supports g:, w:, t: and b: namespaces.
7105 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007106 }
7107
Bram Moolenaar752fc692021-01-04 21:57:11 +01007108 *name_end = cc;
7109 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007110}
7111
7112/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007113 * Callback passed to ex_unletlock().
7114 */
7115 static int
7116compile_lock_unlock(
7117 lval_T *lvp,
7118 char_u *name_end,
7119 exarg_T *eap,
7120 int deep UNUSED,
7121 void *coookie)
7122{
7123 cctx_T *cctx = coookie;
7124 int cc = *name_end;
7125 char_u *p = lvp->ll_name;
7126 int ret = OK;
7127 size_t len;
7128 char_u *buf;
7129
7130 if (cctx->ctx_skip == SKIP_YES)
7131 return OK;
7132
7133 // Cannot use :lockvar and :unlockvar on local variables.
7134 if (p[1] != ':')
7135 {
7136 char_u *end = skip_var_one(p, FALSE);
7137
7138 if (lookup_local(p, end - p, NULL, cctx) == OK)
7139 {
7140 emsg(_(e_cannot_lock_unlock_local_variable));
7141 return FAIL;
7142 }
7143 }
7144
7145 // Checking is done at runtime.
7146 *name_end = NUL;
7147 len = name_end - p + 20;
7148 buf = alloc(len);
7149 if (buf == NULL)
7150 ret = FAIL;
7151 else
7152 {
7153 vim_snprintf((char *)buf, len, "%s %s",
7154 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7155 p);
7156 ret = generate_EXEC(cctx, buf);
7157
7158 vim_free(buf);
7159 *name_end = cc;
7160 }
7161 return ret;
7162}
7163
7164/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007165 * compile "unlet var", "lock var" and "unlock var"
7166 * "arg" points to "var".
7167 */
7168 static char_u *
7169compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7170{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007171 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7172 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7173 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007174 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7175}
7176
7177/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178 * Compile an :import command.
7179 */
7180 static char_u *
7181compile_import(char_u *arg, cctx_T *cctx)
7182{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007183 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184}
7185
7186/*
7187 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7188 */
7189 static int
7190compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7191{
7192 garray_T *instr = &cctx->ctx_instr;
7193 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7194
7195 if (endlabel == NULL)
7196 return FAIL;
7197 endlabel->el_next = *el;
7198 *el = endlabel;
7199 endlabel->el_end_label = instr->ga_len;
7200
7201 generate_JUMP(cctx, when, 0);
7202 return OK;
7203}
7204
7205 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007206compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007207{
7208 garray_T *instr = &cctx->ctx_instr;
7209
7210 while (*el != NULL)
7211 {
7212 endlabel_T *cur = (*el);
7213 isn_T *isn;
7214
7215 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007216 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217 *el = cur->el_next;
7218 vim_free(cur);
7219 }
7220}
7221
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007222 static void
7223compile_free_jump_to_end(endlabel_T **el)
7224{
7225 while (*el != NULL)
7226 {
7227 endlabel_T *cur = (*el);
7228
7229 *el = cur->el_next;
7230 vim_free(cur);
7231 }
7232}
7233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234/*
7235 * Create a new scope and set up the generic items.
7236 */
7237 static scope_T *
7238new_scope(cctx_T *cctx, scopetype_T type)
7239{
7240 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7241
7242 if (scope == NULL)
7243 return NULL;
7244 scope->se_outer = cctx->ctx_scope;
7245 cctx->ctx_scope = scope;
7246 scope->se_type = type;
7247 scope->se_local_count = cctx->ctx_locals.ga_len;
7248 return scope;
7249}
7250
7251/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007252 * Free the current scope and go back to the outer scope.
7253 */
7254 static void
7255drop_scope(cctx_T *cctx)
7256{
7257 scope_T *scope = cctx->ctx_scope;
7258
7259 if (scope == NULL)
7260 {
7261 iemsg("calling drop_scope() without a scope");
7262 return;
7263 }
7264 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007265 switch (scope->se_type)
7266 {
7267 case IF_SCOPE:
7268 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7269 case FOR_SCOPE:
7270 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7271 case WHILE_SCOPE:
7272 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7273 case TRY_SCOPE:
7274 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7275 case NO_SCOPE:
7276 case BLOCK_SCOPE:
7277 break;
7278 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007279 vim_free(scope);
7280}
7281
7282/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007283 * compile "if expr"
7284 *
7285 * "if expr" Produces instructions:
7286 * EVAL expr Push result of "expr"
7287 * JUMP_IF_FALSE end
7288 * ... body ...
7289 * end:
7290 *
7291 * "if expr | else" Produces instructions:
7292 * EVAL expr Push result of "expr"
7293 * JUMP_IF_FALSE else
7294 * ... body ...
7295 * JUMP_ALWAYS end
7296 * else:
7297 * ... body ...
7298 * end:
7299 *
7300 * "if expr1 | elseif expr2 | else" Produces instructions:
7301 * EVAL expr Push result of "expr"
7302 * JUMP_IF_FALSE elseif
7303 * ... body ...
7304 * JUMP_ALWAYS end
7305 * elseif:
7306 * EVAL expr Push result of "expr"
7307 * JUMP_IF_FALSE else
7308 * ... body ...
7309 * JUMP_ALWAYS end
7310 * else:
7311 * ... body ...
7312 * end:
7313 */
7314 static char_u *
7315compile_if(char_u *arg, cctx_T *cctx)
7316{
7317 char_u *p = arg;
7318 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007319 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007320 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007321 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007322 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323
Bram Moolenaara5565e42020-05-09 15:44:01 +02007324 CLEAR_FIELD(ppconst);
7325 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007326 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007327 clear_ppconst(&ppconst);
7328 return NULL;
7329 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007330 if (!ends_excmd2(arg, skipwhite(p)))
7331 {
7332 semsg(_(e_trailing_arg), p);
7333 return NULL;
7334 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007335 if (cctx->ctx_skip == SKIP_YES)
7336 clear_ppconst(&ppconst);
7337 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007338 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007339 int error = FALSE;
7340 int v;
7341
Bram Moolenaara5565e42020-05-09 15:44:01 +02007342 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007343 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007344 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007345 if (error)
7346 return NULL;
7347 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007348 }
7349 else
7350 {
7351 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007352 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007353 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007354 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007355 if (bool_on_stack(cctx) == FAIL)
7356 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007358
Bram Moolenaara91a7132021-03-25 21:12:15 +01007359 // CMDMOD_REV must come before the jump
7360 generate_undo_cmdmods(cctx);
7361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362 scope = new_scope(cctx, IF_SCOPE);
7363 if (scope == NULL)
7364 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007365 scope->se_skip_save = skip_save;
7366 // "is_had_return" will be reset if any block does not end in :return
7367 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007369 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007370 {
7371 // "where" is set when ":elseif", "else" or ":endif" is found
7372 scope->se_u.se_if.is_if_label = instr->ga_len;
7373 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7374 }
7375 else
7376 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377
Bram Moolenaarced68a02021-01-24 17:53:47 +01007378#ifdef FEAT_PROFILE
7379 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7380 && skip_save != SKIP_YES)
7381 {
7382 // generated a profile start, need to generate a profile end, since it
7383 // won't be done after returning
7384 cctx->ctx_skip = SKIP_NOT;
7385 generate_instr(cctx, ISN_PROF_END);
7386 cctx->ctx_skip = SKIP_YES;
7387 }
7388#endif
7389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 return p;
7391}
7392
7393 static char_u *
7394compile_elseif(char_u *arg, cctx_T *cctx)
7395{
7396 char_u *p = arg;
7397 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007398 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 isn_T *isn;
7400 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007401 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007402 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403
7404 if (scope == NULL || scope->se_type != IF_SCOPE)
7405 {
7406 emsg(_(e_elseif_without_if));
7407 return NULL;
7408 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007409 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007410 if (!cctx->ctx_had_return)
7411 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412
Bram Moolenaarced68a02021-01-24 17:53:47 +01007413 if (cctx->ctx_skip == SKIP_NOT)
7414 {
7415 // previous block was executed, this one and following will not
7416 cctx->ctx_skip = SKIP_YES;
7417 scope->se_u.se_if.is_seen_skip_not = TRUE;
7418 }
7419 if (scope->se_u.se_if.is_seen_skip_not)
7420 {
7421 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007422 // Do not count the "elseif" for profiling and cmdmod
7423 instr->ga_len = current_instr_idx(cctx);
7424
Bram Moolenaarced68a02021-01-24 17:53:47 +01007425 skip_expr_cctx(&p, cctx);
7426 return p;
7427 }
7428
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007429 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007430 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007431 int moved_cmdmod = FALSE;
7432
7433 // Move any CMDMOD instruction to after the jump
7434 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7435 {
7436 if (ga_grow(instr, 1) == FAIL)
7437 return NULL;
7438 ((isn_T *)instr->ga_data)[instr->ga_len] =
7439 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7440 --instr->ga_len;
7441 moved_cmdmod = TRUE;
7442 }
7443
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007444 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007446 return NULL;
7447 // previous "if" or "elseif" jumps here
7448 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7449 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007450 if (moved_cmdmod)
7451 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007454 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007455 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007456 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007457 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007458 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007459#ifdef FEAT_PROFILE
7460 if (cctx->ctx_profiling)
7461 {
7462 // the previous block was skipped, need to profile this line
7463 generate_instr(cctx, ISN_PROF_START);
7464 instr_count = instr->ga_len;
7465 }
7466#endif
7467 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007468 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007469 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007470 clear_ppconst(&ppconst);
7471 return NULL;
7472 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007473 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007474 if (!ends_excmd2(arg, skipwhite(p)))
7475 {
7476 semsg(_(e_trailing_arg), p);
7477 return NULL;
7478 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007479 if (scope->se_skip_save == SKIP_YES)
7480 clear_ppconst(&ppconst);
7481 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007482 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007483 int error = FALSE;
7484 int v;
7485
Bram Moolenaar7f141552020-05-09 17:35:53 +02007486 // The expression results in a constant.
7487 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007488 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7489 if (error)
7490 return NULL;
7491 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007492 clear_ppconst(&ppconst);
7493 scope->se_u.se_if.is_if_label = -1;
7494 }
7495 else
7496 {
7497 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007498 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007499 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007500 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007501 if (bool_on_stack(cctx) == FAIL)
7502 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007503
Bram Moolenaara91a7132021-03-25 21:12:15 +01007504 // CMDMOD_REV must come before the jump
7505 generate_undo_cmdmods(cctx);
7506
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007507 // "where" is set when ":elseif", "else" or ":endif" is found
7508 scope->se_u.se_if.is_if_label = instr->ga_len;
7509 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511
7512 return p;
7513}
7514
7515 static char_u *
7516compile_else(char_u *arg, cctx_T *cctx)
7517{
7518 char_u *p = arg;
7519 garray_T *instr = &cctx->ctx_instr;
7520 isn_T *isn;
7521 scope_T *scope = cctx->ctx_scope;
7522
7523 if (scope == NULL || scope->se_type != IF_SCOPE)
7524 {
7525 emsg(_(e_else_without_if));
7526 return NULL;
7527 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007528 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007529 if (!cctx->ctx_had_return)
7530 scope->se_u.se_if.is_had_return = FALSE;
7531 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532
Bram Moolenaarced68a02021-01-24 17:53:47 +01007533#ifdef FEAT_PROFILE
7534 if (cctx->ctx_profiling)
7535 {
7536 if (cctx->ctx_skip == SKIP_NOT
7537 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7538 .isn_type == ISN_PROF_START)
7539 // the previous block was executed, do not count "else" for profiling
7540 --instr->ga_len;
7541 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7542 {
7543 // the previous block was not executed, this one will, do count the
7544 // "else" for profiling
7545 cctx->ctx_skip = SKIP_NOT;
7546 generate_instr(cctx, ISN_PROF_END);
7547 generate_instr(cctx, ISN_PROF_START);
7548 cctx->ctx_skip = SKIP_YES;
7549 }
7550 }
7551#endif
7552
7553 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007554 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007555 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007556 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007557 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007558 if (!cctx->ctx_had_return
7559 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7560 JUMP_ALWAYS, cctx) == FAIL)
7561 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007562 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007563
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007564 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007565 {
7566 if (scope->se_u.se_if.is_if_label >= 0)
7567 {
7568 // previous "if" or "elseif" jumps here
7569 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7570 isn->isn_arg.jump.jump_where = instr->ga_len;
7571 scope->se_u.se_if.is_if_label = -1;
7572 }
7573 }
7574
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007575 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007576 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7577 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007578
7579 return p;
7580}
7581
7582 static char_u *
7583compile_endif(char_u *arg, cctx_T *cctx)
7584{
7585 scope_T *scope = cctx->ctx_scope;
7586 ifscope_T *ifscope;
7587 garray_T *instr = &cctx->ctx_instr;
7588 isn_T *isn;
7589
Bram Moolenaarfa984412021-03-25 22:15:28 +01007590 if (misplaced_cmdmod(cctx))
7591 return NULL;
7592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007593 if (scope == NULL || scope->se_type != IF_SCOPE)
7594 {
7595 emsg(_(e_endif_without_if));
7596 return NULL;
7597 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007598 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007599 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007600 if (!cctx->ctx_had_return)
7601 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007603 if (scope->se_u.se_if.is_if_label >= 0)
7604 {
7605 // previous "if" or "elseif" jumps here
7606 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7607 isn->isn_arg.jump.jump_where = instr->ga_len;
7608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007610 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007611
7612#ifdef FEAT_PROFILE
7613 // even when skipping we count the endif as executed, unless the block it's
7614 // in is skipped
7615 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7616 && scope->se_skip_save != SKIP_YES)
7617 {
7618 cctx->ctx_skip = SKIP_NOT;
7619 generate_instr(cctx, ISN_PROF_START);
7620 }
7621#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007622 cctx->ctx_skip = scope->se_skip_save;
7623
7624 // If all the blocks end in :return and there is an :else then the
7625 // had_return flag is set.
7626 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007628 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 return arg;
7630}
7631
7632/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007633 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007634 *
7635 * Produces instructions:
7636 * PUSHNR -1
7637 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007638 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007639 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7640 * - if beyond end, jump to "end"
7641 * - otherwise get item from list and push it
7642 * STORE var Store item in "var"
7643 * ... body ...
7644 * JUMP top Jump back to repeat
7645 * end: DROP Drop the result of "expr"
7646 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007647 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7648 * UNPACK 2 Split item in 2
7649 * STORE var1 Store item in "var1"
7650 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651 */
7652 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007653compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007655 char_u *arg;
7656 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007657 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007658 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007659 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007660 int var_count = 0;
7661 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007662 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007663 garray_T *stack = &cctx->ctx_type_stack;
7664 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007665 lvar_T *loop_lvar; // loop iteration variable
7666 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007668 type_T *item_type = &t_any;
7669 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007670
Bram Moolenaar792f7862020-11-23 08:31:18 +01007671 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007672 if (p == NULL)
7673 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007674 if (var_count == 0)
7675 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007676
7677 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007678 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007679 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7680 return NULL;
7681 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007682 {
7683 emsg(_(e_missing_in));
7684 return NULL;
7685 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007686 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007687 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7688 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007690 scope = new_scope(cctx, FOR_SCOPE);
7691 if (scope == NULL)
7692 return NULL;
7693
Bram Moolenaar792f7862020-11-23 08:31:18 +01007694 // Reserve a variable to store the loop iteration counter and initialize it
7695 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007696 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7697 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007698 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007699 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007700 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007701 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007702 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007703 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704
7705 // compile "expr", it remains on the stack until "endfor"
7706 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007707 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007708 {
7709 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007710 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007711 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007712 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007714 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007715 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007716 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007717 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007718 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007719 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007720 semsg(_(e_for_loop_on_str_not_supported),
7721 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007722 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007723 return NULL;
7724 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007725
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007726 if (vartype->tt_type == VAR_STRING)
7727 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007728 else if (vartype->tt_type == VAR_BLOB)
7729 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007730 else if (vartype->tt_type == VAR_LIST
7731 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007732 {
7733 if (var_count == 1)
7734 item_type = vartype->tt_member;
7735 else if (vartype->tt_member->tt_type == VAR_LIST
7736 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007737 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007738 item_type = vartype->tt_member->tt_member;
7739 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740
Bram Moolenaara91a7132021-03-25 21:12:15 +01007741 // CMDMOD_REV must come before the FOR instruction
7742 generate_undo_cmdmods(cctx);
7743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007744 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007745 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007746 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007747
Bram Moolenaar792f7862020-11-23 08:31:18 +01007748 arg = arg_start;
7749 if (var_count > 1)
7750 {
7751 generate_UNPACK(cctx, var_count, semicolon);
7752 arg = skipwhite(arg + 1); // skip white after '['
7753
7754 // the list item is replaced by a number of items
7755 if (ga_grow(stack, var_count - 1) == FAIL)
7756 {
7757 drop_scope(cctx);
7758 return NULL;
7759 }
7760 --stack->ga_len;
7761 for (idx = 0; idx < var_count; ++idx)
7762 {
7763 ((type_T **)stack->ga_data)[stack->ga_len] =
7764 (semicolon && idx == 0) ? vartype : item_type;
7765 ++stack->ga_len;
7766 }
7767 }
7768
7769 for (idx = 0; idx < var_count; ++idx)
7770 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007771 assign_dest_T dest = dest_local;
7772 int opt_flags = 0;
7773 int vimvaridx = -1;
7774 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007775 type_T *lhs_type = &t_any;
7776 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007777
7778 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007779 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007780 name = vim_strnsave(arg, varlen);
7781 if (name == NULL)
7782 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007783 if (*p == ':')
7784 {
7785 p = skipwhite(p + 1);
7786 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7787 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007788
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007789 // TODO: script var not supported?
7790 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7791 &vimvaridx, &type, cctx) == FAIL)
7792 goto failed;
7793 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007794 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007795 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7796 0, 0, type, name) == FAIL)
7797 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007798 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007799 else if (varlen == 1 && *arg == '_')
7800 {
7801 // Assigning to "_": drop the value.
7802 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7803 goto failed;
7804 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007805 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007806 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007807 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007808 {
7809 semsg(_(e_variable_already_declared), arg);
7810 goto failed;
7811 }
7812
Bram Moolenaarea870692020-12-02 14:24:30 +01007813 if (STRNCMP(name, "s:", 2) == 0)
7814 {
7815 semsg(_(e_cannot_declare_script_variable_in_function), name);
7816 goto failed;
7817 }
7818
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007819 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007820 where.wt_index = var_count > 1 ? idx + 1 : 0;
7821 where.wt_variable = TRUE;
7822 if (lhs_type == &t_any)
7823 lhs_type = item_type;
7824 else if (item_type != &t_unknown
7825 && !(var_count > 1 && item_type == &t_any)
7826 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7827 goto failed;
7828 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007829 if (var_lvar == NULL)
7830 // out of memory or used as an argument
7831 goto failed;
7832
7833 if (semicolon && idx == var_count - 1)
7834 var_lvar->lv_type = vartype;
7835 else
7836 var_lvar->lv_type = item_type;
7837 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7838 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007839
7840 if (*p == ',' || *p == ';')
7841 ++p;
7842 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007843 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007844 }
7845
7846 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007847
7848failed:
7849 vim_free(name);
7850 drop_scope(cctx);
7851 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007852}
7853
7854/*
7855 * compile "endfor"
7856 */
7857 static char_u *
7858compile_endfor(char_u *arg, cctx_T *cctx)
7859{
7860 garray_T *instr = &cctx->ctx_instr;
7861 scope_T *scope = cctx->ctx_scope;
7862 forscope_T *forscope;
7863 isn_T *isn;
7864
Bram Moolenaarfa984412021-03-25 22:15:28 +01007865 if (misplaced_cmdmod(cctx))
7866 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868 if (scope == NULL || scope->se_type != FOR_SCOPE)
7869 {
7870 emsg(_(e_for));
7871 return NULL;
7872 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007873 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007874 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007875 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007876
7877 // At end of ":for" scope jump back to the FOR instruction.
7878 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7879
7880 // Fill in the "end" label in the FOR statement so it can jump here
7881 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7882 isn->isn_arg.forloop.for_end = instr->ga_len;
7883
7884 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007885 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007886
7887 // Below the ":for" scope drop the "expr" list from the stack.
7888 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7889 return NULL;
7890
7891 vim_free(scope);
7892
7893 return arg;
7894}
7895
7896/*
7897 * compile "while expr"
7898 *
7899 * Produces instructions:
7900 * top: EVAL expr Push result of "expr"
7901 * JUMP_IF_FALSE end jump if false
7902 * ... body ...
7903 * JUMP top Jump back to repeat
7904 * end:
7905 *
7906 */
7907 static char_u *
7908compile_while(char_u *arg, cctx_T *cctx)
7909{
7910 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007911 scope_T *scope;
7912
7913 scope = new_scope(cctx, WHILE_SCOPE);
7914 if (scope == NULL)
7915 return NULL;
7916
Bram Moolenaara91a7132021-03-25 21:12:15 +01007917 // "endwhile" jumps back here, one before when profiling or using cmdmods
7918 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007919
7920 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007921 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007922 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007923 if (!ends_excmd2(arg, skipwhite(p)))
7924 {
7925 semsg(_(e_trailing_arg), p);
7926 return NULL;
7927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007928
Bram Moolenaar13106602020-10-04 16:06:05 +02007929 if (bool_on_stack(cctx) == FAIL)
7930 return FAIL;
7931
Bram Moolenaara91a7132021-03-25 21:12:15 +01007932 // CMDMOD_REV must come before the jump
7933 generate_undo_cmdmods(cctx);
7934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007936 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007937 JUMP_IF_FALSE, cctx) == FAIL)
7938 return FAIL;
7939
7940 return p;
7941}
7942
7943/*
7944 * compile "endwhile"
7945 */
7946 static char_u *
7947compile_endwhile(char_u *arg, cctx_T *cctx)
7948{
7949 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007950 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007951
Bram Moolenaarfa984412021-03-25 22:15:28 +01007952 if (misplaced_cmdmod(cctx))
7953 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7955 {
7956 emsg(_(e_while));
7957 return NULL;
7958 }
7959 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007960 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007961
Bram Moolenaarf002a412021-01-24 13:34:18 +01007962#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007963 // count the endwhile before jumping
7964 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007965#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007967 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007968 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007969
7970 // Fill in the "end" label in the WHILE statement so it can jump here.
7971 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007972 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7973 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007974
7975 vim_free(scope);
7976
7977 return arg;
7978}
7979
7980/*
7981 * compile "continue"
7982 */
7983 static char_u *
7984compile_continue(char_u *arg, cctx_T *cctx)
7985{
7986 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007987 int try_scopes = 0;
7988 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007989
7990 for (;;)
7991 {
7992 if (scope == NULL)
7993 {
7994 emsg(_(e_continue));
7995 return NULL;
7996 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007997 if (scope->se_type == FOR_SCOPE)
7998 {
7999 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008000 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008001 }
8002 if (scope->se_type == WHILE_SCOPE)
8003 {
8004 loop_label = scope->se_u.se_while.ws_top_label;
8005 break;
8006 }
8007 if (scope->se_type == TRY_SCOPE)
8008 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008009 scope = scope->se_outer;
8010 }
8011
Bram Moolenaarc150c092021-02-13 15:02:46 +01008012 if (try_scopes > 0)
8013 // Inside one or more try/catch blocks we first need to jump to the
8014 // "finally" or "endtry" to cleanup.
8015 generate_TRYCONT(cctx, try_scopes, loop_label);
8016 else
8017 // Jump back to the FOR or WHILE instruction.
8018 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008020 return arg;
8021}
8022
8023/*
8024 * compile "break"
8025 */
8026 static char_u *
8027compile_break(char_u *arg, cctx_T *cctx)
8028{
8029 scope_T *scope = cctx->ctx_scope;
8030 endlabel_T **el;
8031
8032 for (;;)
8033 {
8034 if (scope == NULL)
8035 {
8036 emsg(_(e_break));
8037 return NULL;
8038 }
8039 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8040 break;
8041 scope = scope->se_outer;
8042 }
8043
8044 // Jump to the end of the FOR or WHILE loop.
8045 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008046 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008047 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008048 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008049 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8050 return FAIL;
8051
8052 return arg;
8053}
8054
8055/*
8056 * compile "{" start of block
8057 */
8058 static char_u *
8059compile_block(char_u *arg, cctx_T *cctx)
8060{
8061 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8062 return NULL;
8063 return skipwhite(arg + 1);
8064}
8065
8066/*
8067 * compile end of block: drop one scope
8068 */
8069 static void
8070compile_endblock(cctx_T *cctx)
8071{
8072 scope_T *scope = cctx->ctx_scope;
8073
8074 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008075 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008076 vim_free(scope);
8077}
8078
8079/*
8080 * compile "try"
8081 * Creates a new scope for the try-endtry, pointing to the first catch and
8082 * finally.
8083 * Creates another scope for the "try" block itself.
8084 * TRY instruction sets up exception handling at runtime.
8085 *
8086 * "try"
8087 * TRY -> catch1, -> finally push trystack entry
8088 * ... try block
8089 * "throw {exception}"
8090 * EVAL {exception}
8091 * THROW create exception
8092 * ... try block
8093 * " catch {expr}"
8094 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008095 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008096 * EVAL {expr}
8097 * MATCH
8098 * JUMP nomatch -> catch2
8099 * CATCH remove exception
8100 * ... catch block
8101 * " catch"
8102 * JUMP -> finally
8103 * catch2: CATCH remove exception
8104 * ... catch block
8105 * " finally"
8106 * finally:
8107 * ... finally block
8108 * " endtry"
8109 * ENDTRY pop trystack entry, may rethrow
8110 */
8111 static char_u *
8112compile_try(char_u *arg, cctx_T *cctx)
8113{
8114 garray_T *instr = &cctx->ctx_instr;
8115 scope_T *try_scope;
8116 scope_T *scope;
8117
Bram Moolenaarfa984412021-03-25 22:15:28 +01008118 if (misplaced_cmdmod(cctx))
8119 return NULL;
8120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008121 // scope that holds the jumps that go to catch/finally/endtry
8122 try_scope = new_scope(cctx, TRY_SCOPE);
8123 if (try_scope == NULL)
8124 return NULL;
8125
Bram Moolenaar69f70502021-01-01 16:10:46 +01008126 if (cctx->ctx_skip != SKIP_YES)
8127 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008128 isn_T *isn;
8129
8130 // "try_catch" is set when the first ":catch" is found or when no catch
8131 // is found and ":finally" is found.
8132 // "try_finally" is set when ":finally" is found
8133 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008134 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008135 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8136 return NULL;
8137 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8138 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008139 return NULL;
8140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008141
8142 // scope for the try block itself
8143 scope = new_scope(cctx, BLOCK_SCOPE);
8144 if (scope == NULL)
8145 return NULL;
8146
8147 return arg;
8148}
8149
8150/*
8151 * compile "catch {expr}"
8152 */
8153 static char_u *
8154compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8155{
8156 scope_T *scope = cctx->ctx_scope;
8157 garray_T *instr = &cctx->ctx_instr;
8158 char_u *p;
8159 isn_T *isn;
8160
Bram Moolenaarfa984412021-03-25 22:15:28 +01008161 if (misplaced_cmdmod(cctx))
8162 return NULL;
8163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008164 // end block scope from :try or :catch
8165 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8166 compile_endblock(cctx);
8167 scope = cctx->ctx_scope;
8168
8169 // Error if not in a :try scope
8170 if (scope == NULL || scope->se_type != TRY_SCOPE)
8171 {
8172 emsg(_(e_catch));
8173 return NULL;
8174 }
8175
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008176 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008178 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008179 return NULL;
8180 }
8181
Bram Moolenaar69f70502021-01-01 16:10:46 +01008182 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008183 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008184#ifdef FEAT_PROFILE
8185 // the profile-start should be after the jump
8186 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8187 .isn_type == ISN_PROF_START)
8188 --instr->ga_len;
8189#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008190 // Jump from end of previous block to :finally or :endtry
8191 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8192 JUMP_ALWAYS, cctx) == FAIL)
8193 return NULL;
8194
8195 // End :try or :catch scope: set value in ISN_TRY instruction
8196 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008197 if (isn->isn_arg.try.try_ref->try_catch == 0)
8198 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008199 if (scope->se_u.se_try.ts_catch_label != 0)
8200 {
8201 // Previous catch without match jumps here
8202 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8203 isn->isn_arg.jump.jump_where = instr->ga_len;
8204 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008205#ifdef FEAT_PROFILE
8206 if (cctx->ctx_profiling)
8207 {
8208 // a "throw" that jumps here needs to be counted
8209 generate_instr(cctx, ISN_PROF_END);
8210 // the "catch" is also counted
8211 generate_instr(cctx, ISN_PROF_START);
8212 }
8213#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008214 }
8215
8216 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008217 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008218 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008219 scope->se_u.se_try.ts_caught_all = TRUE;
8220 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008221 }
8222 else
8223 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008224 char_u *end;
8225 char_u *pat;
8226 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008227 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008228 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008230 // Push v:exception, push {expr} and MATCH
8231 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8232
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008233 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008234 if (*end != *p)
8235 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008236 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008237 vim_free(tofree);
8238 return FAIL;
8239 }
8240 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008241 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008242 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008243 len = (int)(end - tofree);
8244 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008245 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008246 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008247 if (pat == NULL)
8248 return FAIL;
8249 if (generate_PUSHS(cctx, pat) == FAIL)
8250 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008252 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8253 return NULL;
8254
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008255 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008256 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8257 return NULL;
8258 }
8259
Bram Moolenaar69f70502021-01-01 16:10:46 +01008260 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008261 return NULL;
8262
8263 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8264 return NULL;
8265 return p;
8266}
8267
8268 static char_u *
8269compile_finally(char_u *arg, cctx_T *cctx)
8270{
8271 scope_T *scope = cctx->ctx_scope;
8272 garray_T *instr = &cctx->ctx_instr;
8273 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008274 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008275
Bram Moolenaarfa984412021-03-25 22:15:28 +01008276 if (misplaced_cmdmod(cctx))
8277 return NULL;
8278
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008279 // end block scope from :try or :catch
8280 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8281 compile_endblock(cctx);
8282 scope = cctx->ctx_scope;
8283
8284 // Error if not in a :try scope
8285 if (scope == NULL || scope->se_type != TRY_SCOPE)
8286 {
8287 emsg(_(e_finally));
8288 return NULL;
8289 }
8290
8291 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008292 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008293 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008294 {
8295 emsg(_(e_finally_dup));
8296 return NULL;
8297 }
8298
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008299 this_instr = instr->ga_len;
8300#ifdef FEAT_PROFILE
8301 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8302 .isn_type == ISN_PROF_START)
8303 // jump to the profile start of the "finally"
8304 --this_instr;
8305#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008306
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008307 // Fill in the "end" label in jumps at the end of the blocks.
8308 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8309 this_instr, cctx);
8310
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008311 // If there is no :catch then an exception jumps to :finally.
8312 if (isn->isn_arg.try.try_ref->try_catch == 0)
8313 isn->isn_arg.try.try_ref->try_catch = this_instr;
8314 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008315 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008316 {
8317 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008318 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008319 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008320 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008321 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008322 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8323 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008325 // TODO: set index in ts_finally_label jumps
8326
8327 return arg;
8328}
8329
8330 static char_u *
8331compile_endtry(char_u *arg, cctx_T *cctx)
8332{
8333 scope_T *scope = cctx->ctx_scope;
8334 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008335 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008336
Bram Moolenaarfa984412021-03-25 22:15:28 +01008337 if (misplaced_cmdmod(cctx))
8338 return NULL;
8339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008340 // end block scope from :catch or :finally
8341 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8342 compile_endblock(cctx);
8343 scope = cctx->ctx_scope;
8344
8345 // Error if not in a :try scope
8346 if (scope == NULL || scope->se_type != TRY_SCOPE)
8347 {
8348 if (scope == NULL)
8349 emsg(_(e_no_endtry));
8350 else if (scope->se_type == WHILE_SCOPE)
8351 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008352 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008353 emsg(_(e_endfor));
8354 else
8355 emsg(_(e_endif));
8356 return NULL;
8357 }
8358
Bram Moolenaarc150c092021-02-13 15:02:46 +01008359 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008360 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008361 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008362 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8363 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008364 {
8365 emsg(_(e_missing_catch_or_finally));
8366 return NULL;
8367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008368
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008369#ifdef FEAT_PROFILE
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008370 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8371 .isn_type == ISN_PROF_START)
8372 // move the profile start after "endtry" so that it's not counted when
8373 // the exception is rethrown.
8374 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008375#endif
8376
Bram Moolenaar69f70502021-01-01 16:10:46 +01008377 // Fill in the "end" label in jumps at the end of the blocks, if not
8378 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008379 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8380 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008381
Bram Moolenaar69f70502021-01-01 16:10:46 +01008382 if (scope->se_u.se_try.ts_catch_label != 0)
8383 {
8384 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008385 isn_T *isn = ((isn_T *)instr->ga_data)
8386 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008387 isn->isn_arg.jump.jump_where = instr->ga_len;
8388 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008389 }
8390
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008391 compile_endblock(cctx);
8392
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008393 if (cctx->ctx_skip != SKIP_YES)
8394 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008395 // End :catch or :finally scope: set instruction index in ISN_TRY
8396 // instruction
8397 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008398 if (cctx->ctx_skip != SKIP_YES
8399 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8400 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008401#ifdef FEAT_PROFILE
8402 if (cctx->ctx_profiling)
8403 generate_instr(cctx, ISN_PROF_START);
8404#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008405 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008406 return arg;
8407}
8408
8409/*
8410 * compile "throw {expr}"
8411 */
8412 static char_u *
8413compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8414{
8415 char_u *p = skipwhite(arg);
8416
Bram Moolenaara5565e42020-05-09 15:44:01 +02008417 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008419 if (cctx->ctx_skip == SKIP_YES)
8420 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008421 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008422 return NULL;
8423 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8424 return NULL;
8425
8426 return p;
8427}
8428
8429/*
8430 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008431 * compile "echomsg expr"
8432 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008433 * compile "execute expr"
8434 */
8435 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008436compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008437{
8438 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008439 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008440 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008441 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008442 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008443 garray_T *stack = &cctx->ctx_type_stack;
8444 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008445
8446 for (;;)
8447 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008448 if (ends_excmd2(prev, p))
8449 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008450 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008451 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008452 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008453
8454 if (cctx->ctx_skip != SKIP_YES)
8455 {
8456 // check for non-void type
8457 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8458 if (type->tt_type == VAR_VOID)
8459 {
8460 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8461 return NULL;
8462 }
8463 }
8464
Bram Moolenaarad39c092020-02-26 18:23:43 +01008465 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008466 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008467 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008468 }
8469
Bram Moolenaare4984292020-12-13 14:19:25 +01008470 if (count > 0)
8471 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008472 long save_lnum = cctx->ctx_lnum;
8473
8474 // Use the line number where the command started.
8475 cctx->ctx_lnum = start_ctx_lnum;
8476
Bram Moolenaare4984292020-12-13 14:19:25 +01008477 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8478 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8479 else if (cmdidx == CMD_execute)
8480 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8481 else if (cmdidx == CMD_echomsg)
8482 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8483 else
8484 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008485
8486 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008488 return p;
8489}
8490
8491/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008492 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008493 * instruction to compute it and return OK.
8494 * Otherwise return FAIL, the caller must deal with any range.
8495 */
8496 static int
8497compile_variable_range(exarg_T *eap, cctx_T *cctx)
8498{
8499 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8500 char_u *p = skipdigits(eap->cmd);
8501
8502 if (p == range_end)
8503 return FAIL;
8504 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8505}
8506
8507/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008508 * :put r
8509 * :put ={expr}
8510 */
8511 static char_u *
8512compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8513{
8514 char_u *line = arg;
8515 linenr_T lnum;
8516 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008517 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008518
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008519 eap->regname = *line;
8520
8521 if (eap->regname == '=')
8522 {
8523 char_u *p = line + 1;
8524
8525 if (compile_expr0(&p, cctx) == FAIL)
8526 return NULL;
8527 line = p;
8528 }
8529 else if (eap->regname != NUL)
8530 ++line;
8531
Bram Moolenaar08597872020-12-10 19:43:40 +01008532 if (compile_variable_range(eap, cctx) == OK)
8533 {
8534 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8535 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008536 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008537 {
8538 // Either no range or a number.
8539 // "errormsg" will not be set because the range is ADDR_LINES.
8540 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008541 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008542 return NULL;
8543 if (eap->addr_count == 0)
8544 lnum = -1;
8545 else
8546 lnum = eap->line2;
8547 if (above)
8548 --lnum;
8549 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008550
8551 generate_PUT(cctx, eap->regname, lnum);
8552 return line;
8553}
8554
8555/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008556 * A command that is not compiled, execute with legacy code.
8557 */
8558 static char_u *
8559compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8560{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008561 char_u *p;
8562 int has_expr = FALSE;
8563 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008564
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008565 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008566 goto theend;
8567
Bram Moolenaare729ce22021-06-06 21:38:09 +02008568 // If there was a prececing command modifier, drop it and include it in the
8569 // EXEC command.
8570 if (cctx->ctx_has_cmdmod)
8571 {
8572 garray_T *instr = &cctx->ctx_instr;
8573 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8574
8575 if (isn->isn_type == ISN_CMDMOD)
8576 {
8577 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8578 ->cmod_filter_regmatch.regprog);
8579 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8580 --instr->ga_len;
8581 cctx->ctx_has_cmdmod = FALSE;
8582 }
8583 }
8584
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008585 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008586 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008587 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008588 int usefilter = FALSE;
8589
8590 has_expr = argt & (EX_XFILE | EX_EXPAND);
8591
8592 // If the command can be followed by a bar, find the bar and truncate
8593 // it, so that the following command can be compiled.
8594 // The '|' is overwritten with a NUL, it is put back below.
8595 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8596 && *eap->arg == '!')
8597 // :w !filter or :r !filter or :r! filter
8598 usefilter = TRUE;
8599 if ((argt & EX_TRLBAR) && !usefilter)
8600 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008601 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008602 separate_nextcmd(eap);
8603 if (eap->nextcmd != NULL)
8604 nextcmd = eap->nextcmd;
8605 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008606 else if (eap->cmdidx == CMD_wincmd)
8607 {
8608 p = eap->arg;
8609 if (*p != NUL)
8610 ++p;
8611 if (*p == 'g' || *p == Ctrl_G)
8612 ++p;
8613 p = skipwhite(p);
8614 if (*p == '|')
8615 {
8616 *p = NUL;
8617 nextcmd = p + 1;
8618 }
8619 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008620 }
8621
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008622 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8623 {
8624 // expand filename in "syntax include [@group] filename"
8625 has_expr = TRUE;
8626 eap->arg = skipwhite(eap->arg + 7);
8627 if (*eap->arg == '@')
8628 eap->arg = skiptowhite(eap->arg);
8629 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008630
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008631 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8632 && STRLEN(eap->arg) > 4)
8633 {
8634 int delim = *eap->arg;
8635
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008636 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008637 if (*p == delim)
8638 {
8639 eap->arg = p + 1;
8640 has_expr = TRUE;
8641 }
8642 }
8643
Bram Moolenaarecac5912021-01-05 19:23:28 +01008644 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8645 {
8646 // TODO: should only expand when appropriate for the command
8647 eap->arg = skiptowhite(eap->arg);
8648 has_expr = TRUE;
8649 }
8650
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008651 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008652 {
8653 int count = 0;
8654 char_u *start = skipwhite(line);
8655
8656 // :cmd xxx`=expr1`yyy`=expr2`zzz
8657 // PUSHS ":cmd xxx"
8658 // eval expr1
8659 // PUSHS "yyy"
8660 // eval expr2
8661 // PUSHS "zzz"
8662 // EXECCONCAT 5
8663 for (;;)
8664 {
8665 if (p > start)
8666 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008667 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008668 ++count;
8669 }
8670 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008671 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008672 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008673 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008674 ++count;
8675 p = skipwhite(p);
8676 if (*p != '`')
8677 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008678 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008679 return NULL;
8680 }
8681 start = p + 1;
8682
8683 p = (char_u *)strstr((char *)start, "`=");
8684 if (p == NULL)
8685 {
8686 if (*skipwhite(start) != NUL)
8687 {
8688 generate_PUSHS(cctx, vim_strsave(start));
8689 ++count;
8690 }
8691 break;
8692 }
8693 }
8694 generate_EXECCONCAT(cctx, count);
8695 }
8696 else
8697 generate_EXEC(cctx, line);
8698
8699theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008700 if (*nextcmd != NUL)
8701 {
8702 // the parser expects a pointer to the bar, put it back
8703 --nextcmd;
8704 *nextcmd = '|';
8705 }
8706
8707 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008708}
8709
Bram Moolenaar20677332021-06-06 17:02:53 +02008710/*
8711 * A script command with heredoc, e.g.
8712 * ruby << EOF
8713 * command
8714 * EOF
8715 * Has been turned into one long line with NL characters by
8716 * get_function_body():
8717 * ruby << EOF<NL> command<NL>EOF
8718 */
8719 static char_u *
8720compile_script(char_u *line, cctx_T *cctx)
8721{
8722 if (cctx->ctx_skip != SKIP_YES)
8723 {
8724 isn_T *isn;
8725
8726 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8727 return NULL;
8728 isn->isn_arg.string = vim_strsave(line);
8729 }
8730 return (char_u *)"";
8731}
8732
Bram Moolenaar8238f082021-04-20 21:10:48 +02008733
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008734/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008735 * :s/pat/repl/
8736 */
8737 static char_u *
8738compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8739{
8740 char_u *cmd = eap->arg;
8741 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8742
8743 if (expr != NULL)
8744 {
8745 int delimiter = *cmd++;
8746
8747 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008748 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008749 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8750 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008751 garray_T save_ga = cctx->ctx_instr;
8752 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008753 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008754 int trailing_error;
8755 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008756 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008757 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008758
8759 cmd += 3;
8760 end = skip_substitute(cmd, delimiter);
8761
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008762 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008763 // labels are correct.
8764 cctx->ctx_instr.ga_len = 0;
8765 cctx->ctx_instr.ga_maxlen = 0;
8766 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008767 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008768 if (end[-1] == NUL)
8769 end[-1] = delimiter;
8770 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008771 trailing_error = *cmd != delimiter && *cmd != NUL;
8772
Bram Moolenaar169502f2021-04-21 12:19:35 +02008773 if (expr_res == FAIL || trailing_error
8774 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008775 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008776 if (trailing_error)
8777 semsg(_(e_trailing_arg), cmd);
8778 clear_instr_ga(&cctx->ctx_instr);
8779 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008780 return NULL;
8781 }
8782
Bram Moolenaar8238f082021-04-20 21:10:48 +02008783 // Move the generated instructions into the ISN_SUBSTITUTE
8784 // instructions, then restore the list of instructions before
8785 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008786 instr_count = cctx->ctx_instr.ga_len;
8787 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008788 instr[instr_count].isn_type = ISN_FINISH;
8789
8790 cctx->ctx_instr = save_ga;
8791 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8792 {
8793 int idx;
8794
8795 for (idx = 0; idx < instr_count; ++idx)
8796 delete_instr(instr + idx);
8797 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008798 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008799 }
8800 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8801 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008802
8803 // skip over flags
8804 if (*end == '&')
8805 ++end;
8806 while (ASCII_ISALPHA(*end) || *end == '#')
8807 ++end;
8808 return end;
8809 }
8810 }
8811
8812 return compile_exec(arg, eap, cctx);
8813}
8814
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008815 static char_u *
8816compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8817{
8818 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008819 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008820
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008821 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008822 {
8823 if (STRNCMP(arg, "END", 3) == 0)
8824 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008825 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008826 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008827 // First load the current variable value.
8828 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8829 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008830 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008831 }
8832
8833 // Gets the redirected text and put it on the stack, then store it
8834 // in the variable.
8835 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8836
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008837 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008838 generate_instr_drop(cctx, ISN_CONCAT, 1);
8839
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008840 if (lhs->lhs_has_index)
8841 {
8842 // Use the info in "lhs" to store the value at the index in the
8843 // list or dict.
8844 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8845 &t_string, cctx) == FAIL)
8846 return NULL;
8847 }
8848 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008849 return NULL;
8850
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008851 VIM_CLEAR(lhs->lhs_name);
8852 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008853 return arg + 3;
8854 }
8855 emsg(_(e_cannot_nest_redir));
8856 return NULL;
8857 }
8858
8859 if (arg[0] == '=' && arg[1] == '>')
8860 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008861 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008862
8863 // redirect to a variable is compiled
8864 arg += 2;
8865 if (*arg == '>')
8866 {
8867 ++arg;
8868 append = TRUE;
8869 }
8870 arg = skipwhite(arg);
8871
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008872 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008873 FALSE, FALSE, 1, cctx) == FAIL)
8874 return NULL;
8875 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008876 lhs->lhs_append = append;
8877 if (lhs->lhs_has_index)
8878 {
8879 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8880 if (lhs->lhs_whole == NULL)
8881 return NULL;
8882 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008883
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008884 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008885 }
8886
8887 // other redirects are handled like at script level
8888 return compile_exec(line, eap, cctx);
8889}
8890
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008891#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008892 static char_u *
8893compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8894{
8895 isn_T *isn;
8896 char_u *p;
8897
8898 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8899 if (isn == NULL)
8900 return NULL;
8901 isn->isn_arg.number = eap->cmdidx;
8902
8903 p = eap->arg;
8904 if (compile_expr0(&p, cctx) == FAIL)
8905 return NULL;
8906
8907 isn = generate_instr(cctx, ISN_CEXPR_CORE);
8908 if (isn == NULL)
8909 return NULL;
8910 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
8911 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
8912 return NULL;
8913 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
8914 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
8915 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
8916
8917 return p;
8918}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008919#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008920
Bram Moolenaar4c137212021-04-19 16:48:48 +02008921/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008922 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008923 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008924 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008925 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008926add_def_function(ufunc_T *ufunc)
8927{
8928 dfunc_T *dfunc;
8929
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008930 if (def_functions.ga_len == 0)
8931 {
8932 // The first position is not used, so that a zero uf_dfunc_idx means it
8933 // wasn't set.
8934 if (ga_grow(&def_functions, 1) == FAIL)
8935 return FAIL;
8936 ++def_functions.ga_len;
8937 }
8938
Bram Moolenaar09689a02020-05-09 22:50:08 +02008939 // Add the function to "def_functions".
8940 if (ga_grow(&def_functions, 1) == FAIL)
8941 return FAIL;
8942 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8943 CLEAR_POINTER(dfunc);
8944 dfunc->df_idx = def_functions.ga_len;
8945 ufunc->uf_dfunc_idx = dfunc->df_idx;
8946 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008947 dfunc->df_name = vim_strsave(ufunc->uf_name);
8948 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008949 ++def_functions.ga_len;
8950 return OK;
8951}
8952
8953/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008954 * After ex_function() has collected all the function lines: parse and compile
8955 * the lines into instructions.
8956 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008957 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8958 * the return statement (used for lambda). When uf_ret_type is already set
8959 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008960 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008961 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008962 * This can be used recursively through compile_lambda(), which may reallocate
8963 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008964 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008965 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008966 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008967compile_def_function(
8968 ufunc_T *ufunc,
8969 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008970 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008971 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008972{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008973 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008974 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008975 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008976 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008977 cctx_T cctx;
8978 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008979 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008980 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008981 int ret = FAIL;
8982 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008983 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02008984 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008985 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008986 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008987#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008988 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008989#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008990
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008991 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008992 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008993 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008994 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008995 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8996 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008997 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008998 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008999 else
9000 {
9001 if (add_def_function(ufunc) == FAIL)
9002 return FAIL;
9003 new_def_function = TRUE;
9004 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009005
Bram Moolenaar985116a2020-07-12 17:31:09 +02009006 ufunc->uf_def_status = UF_COMPILING;
9007
Bram Moolenaara80faa82020-04-12 19:37:17 +02009008 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009009
Bram Moolenaarf002a412021-01-24 13:34:18 +01009010#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009011 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009012#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009013 cctx.ctx_ufunc = ufunc;
9014 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009015 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009016 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9017 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9018 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9019 cctx.ctx_type_list = &ufunc->uf_type_list;
9020 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9021 instr = &cctx.ctx_instr;
9022
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009023 // Set the context to the function, it may be compiled when called from
9024 // another script. Set the script version to the most modern one.
9025 // The line number will be set in next_line_from_context().
9026 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009027 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9028
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009029 // Don't use the flag from ":legacy" here.
9030 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9031
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009032 // Make sure error messages are OK.
9033 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9034 if (do_estack_push)
9035 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009036 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009037
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009038 if (ufunc->uf_def_args.ga_len > 0)
9039 {
9040 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009041 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009042 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009043 int i;
9044 char_u *arg;
9045 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009046 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009047
9048 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009049 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009050 for (i = 0; i < count; ++i)
9051 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009052 garray_T *stack = &cctx.ctx_type_stack;
9053 type_T *val_type;
9054 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009055 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009056 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009057 int jump_instr_idx = instr->ga_len;
9058 isn_T *isn;
9059
9060 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9061 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9062 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009063
9064 // Make sure later arguments are not found.
9065 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009066
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009067 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009068 r = compile_expr0(&arg, &cctx);
9069
9070 ufunc->uf_args.ga_len = uf_args_len;
9071 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009072 goto erret;
9073
9074 // If no type specified use the type of the default value.
9075 // Otherwise check that the default value type matches the
9076 // specified type.
9077 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009078 where.wt_index = arg_idx + 1;
9079 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009080 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009081 {
9082 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009083 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009084 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009085 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009086 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009087 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009088
9089 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009090 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009091
9092 // set instruction index in JUMP_IF_ARG_SET to here
9093 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9094 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009095 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009096
9097 if (did_set_arg_type)
9098 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009099 }
9100
9101 /*
9102 * Loop over all the lines of the function and generate instructions.
9103 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009104 for (;;)
9105 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009106 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009107 int starts_with_colon = FALSE;
9108 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009109 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009110
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009111 // Bail out on the first error to avoid a flood of errors and report
9112 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009113 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009114 goto erret;
9115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009116 if (line != NULL && *line == '|')
9117 // the line continues after a '|'
9118 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009119 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009120 && !(*line == '#' && (line == cctx.ctx_line_start
9121 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009122 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009123 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009124 goto erret;
9125 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009126 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9127 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009128 else
9129 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009130 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009131 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009132 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009133 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009134#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009135 if (cctx.ctx_skip != SKIP_YES)
9136 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009137#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009138 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009139 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009140 // Make a copy, splitting off nextcmd and removing trailing spaces
9141 // may change it.
9142 if (line != NULL)
9143 {
9144 line = vim_strsave(line);
9145 vim_free(line_to_free);
9146 line_to_free = line;
9147 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009148 }
9149
Bram Moolenaara80faa82020-04-12 19:37:17 +02009150 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009151 ea.cmdlinep = &line;
9152 ea.cmd = skipwhite(line);
9153
Bram Moolenaarb2049902021-01-24 12:53:53 +01009154 if (*ea.cmd == '#')
9155 {
9156 // "#" starts a comment
9157 line = (char_u *)"";
9158 continue;
9159 }
9160
Bram Moolenaarf002a412021-01-24 13:34:18 +01009161#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009162 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
9163 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009164 {
9165 may_generate_prof_end(&cctx, prof_lnum);
9166
9167 prof_lnum = cctx.ctx_lnum;
9168 generate_instr(&cctx, ISN_PROF_START);
9169 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009170#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009171
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009172 // Some things can be recognized by the first character.
9173 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009174 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009175 case '}':
9176 {
9177 // "}" ends a block scope
9178 scopetype_T stype = cctx.ctx_scope == NULL
9179 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009180
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009181 if (stype == BLOCK_SCOPE)
9182 {
9183 compile_endblock(&cctx);
9184 line = ea.cmd;
9185 }
9186 else
9187 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009188 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009189 goto erret;
9190 }
9191 if (line != NULL)
9192 line = skipwhite(ea.cmd + 1);
9193 continue;
9194 }
9195
9196 case '{':
9197 // "{" starts a block scope
9198 // "{'a': 1}->func() is something else
9199 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9200 {
9201 line = compile_block(ea.cmd, &cctx);
9202 continue;
9203 }
9204 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009205 }
9206
9207 /*
9208 * COMMAND MODIFIERS
9209 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009210 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009211 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9212 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009213 {
9214 if (errormsg != NULL)
9215 goto erret;
9216 // empty line or comment
9217 line = (char_u *)"";
9218 continue;
9219 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009220 generate_cmdmods(&cctx, &local_cmdmod);
9221 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009222
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009223 // Check if there was a colon after the last command modifier or before
9224 // the current position.
9225 for (p = ea.cmd; p >= line; --p)
9226 {
9227 if (*p == ':')
9228 starts_with_colon = TRUE;
9229 if (p < ea.cmd && !VIM_ISWHITE(*p))
9230 break;
9231 }
9232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009233 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009234 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009235 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009236 {
9237 if (*ea.cmd == '(')
9238 // not for "call()"
9239 ea.cmd = p;
9240 else
9241 ea.cmd = skipwhite(ea.cmd);
9242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009243
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009244 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009245 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009246 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009247
Bram Moolenaar17126b12021-01-07 22:03:02 +01009248 // Check for assignment after command modifiers.
9249 assign = may_compile_assignment(&ea, &line, &cctx);
9250 if (assign == OK)
9251 goto nextline;
9252 if (assign == FAIL)
9253 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009254 }
9255
9256 /*
9257 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009258 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009259 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009260 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009261 cmd = ea.cmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009262 if (starts_with_colon || !(*cmd == '\''
9263 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009264 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009265 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009266 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009267 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009268 if (!starts_with_colon)
9269 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009270 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009271 goto erret;
9272 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009273 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009274 if (ends_excmd2(line, ea.cmd))
9275 {
9276 // A range without a command: jump to the line.
9277 // TODO: compile to a more efficient command, possibly
9278 // calling parse_cmd_address().
9279 ea.cmdidx = CMD_SIZE;
9280 line = compile_exec(line, &ea, &cctx);
9281 goto nextline;
9282 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009283 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009284 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009285 p = find_ex_command(&ea, NULL, starts_with_colon
9286 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009287
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009288 if (p == NULL)
9289 {
9290 if (cctx.ctx_skip != SKIP_YES)
9291 emsg(_(e_ambiguous_use_of_user_defined_command));
9292 goto erret;
9293 }
9294
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009295 // When using ":legacy cmd" always use compile_exec().
9296 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009297 {
9298 char_u *start = ea.cmd;
9299
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009300 switch (ea.cmdidx)
9301 {
9302 case CMD_if:
9303 case CMD_elseif:
9304 case CMD_else:
9305 case CMD_endif:
9306 case CMD_for:
9307 case CMD_endfor:
9308 case CMD_continue:
9309 case CMD_break:
9310 case CMD_while:
9311 case CMD_endwhile:
9312 case CMD_try:
9313 case CMD_catch:
9314 case CMD_finally:
9315 case CMD_endtry:
9316 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9317 goto erret;
9318 default: break;
9319 }
9320
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009321 // ":legacy return expr" needs to be handled differently.
9322 if (checkforcmd(&start, "return", 4))
9323 ea.cmdidx = CMD_return;
9324 else
9325 ea.cmdidx = CMD_legacy;
9326 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009328 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9329 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009330 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009331 {
9332 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009333 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009334 }
9335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009336 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009337 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009338 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009339 // CMD_var cannot happen, compile_assignment() above would be
9340 // used. Most likely an assignment to a non-existing variable.
9341 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009342 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009343 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009344 }
9345
Bram Moolenaar3988f642020-08-27 22:43:03 +02009346 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009347 && ea.cmdidx != CMD_elseif
9348 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009349 && ea.cmdidx != CMD_endif
9350 && ea.cmdidx != CMD_endfor
9351 && ea.cmdidx != CMD_endwhile
9352 && ea.cmdidx != CMD_catch
9353 && ea.cmdidx != CMD_finally
9354 && ea.cmdidx != CMD_endtry)
9355 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009356 emsg(_(e_unreachable_code_after_return));
9357 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009358 }
9359
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009360 p = skipwhite(p);
9361 if (ea.cmdidx != CMD_SIZE
9362 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9363 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009364 if (ea.cmdidx >= 0)
9365 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009366 if ((ea.argt & EX_BANG) && *p == '!')
9367 {
9368 ea.forceit = TRUE;
9369 p = skipwhite(p + 1);
9370 }
9371 }
9372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009373 switch (ea.cmdidx)
9374 {
9375 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009376 ea.arg = p;
9377 line = compile_nested_function(&ea, &cctx);
9378 break;
9379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009380 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009381 // TODO: should we allow this, e.g. to declare a global
9382 // function?
9383 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009384 goto erret;
9385
9386 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009387 line = compile_return(p, check_return_type,
9388 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009389 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009390 break;
9391
9392 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009393 emsg(_(e_cannot_use_let_in_vim9_script));
9394 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009395 case CMD_var:
9396 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009397 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009398 case CMD_increment:
9399 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009400 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009401 if (line == p)
9402 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009403 break;
9404
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009405 case CMD_unlet:
9406 case CMD_unlockvar:
9407 case CMD_lockvar:
9408 line = compile_unletlock(p, &ea, &cctx);
9409 break;
9410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009411 case CMD_import:
9412 line = compile_import(p, &cctx);
9413 break;
9414
9415 case CMD_if:
9416 line = compile_if(p, &cctx);
9417 break;
9418 case CMD_elseif:
9419 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009420 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009421 break;
9422 case CMD_else:
9423 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009424 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009425 break;
9426 case CMD_endif:
9427 line = compile_endif(p, &cctx);
9428 break;
9429
9430 case CMD_while:
9431 line = compile_while(p, &cctx);
9432 break;
9433 case CMD_endwhile:
9434 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009435 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009436 break;
9437
9438 case CMD_for:
9439 line = compile_for(p, &cctx);
9440 break;
9441 case CMD_endfor:
9442 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009443 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009444 break;
9445 case CMD_continue:
9446 line = compile_continue(p, &cctx);
9447 break;
9448 case CMD_break:
9449 line = compile_break(p, &cctx);
9450 break;
9451
9452 case CMD_try:
9453 line = compile_try(p, &cctx);
9454 break;
9455 case CMD_catch:
9456 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009457 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009458 break;
9459 case CMD_finally:
9460 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009461 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009462 break;
9463 case CMD_endtry:
9464 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009465 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009466 break;
9467 case CMD_throw:
9468 line = compile_throw(p, &cctx);
9469 break;
9470
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009471 case CMD_eval:
9472 if (compile_expr0(&p, &cctx) == FAIL)
9473 goto erret;
9474
Bram Moolenaar3988f642020-08-27 22:43:03 +02009475 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009476 generate_instr_drop(&cctx, ISN_DROP, 1);
9477
9478 line = skipwhite(p);
9479 break;
9480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009481 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009482 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009483 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009484 case CMD_echomsg:
9485 case CMD_echoerr:
9486 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009487 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009488
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009489 case CMD_put:
9490 ea.cmd = cmd;
9491 line = compile_put(p, &ea, &cctx);
9492 break;
9493
Bram Moolenaar4c137212021-04-19 16:48:48 +02009494 case CMD_substitute:
9495 if (cctx.ctx_skip == SKIP_YES)
9496 line = (char_u *)"";
9497 else
9498 {
9499 ea.arg = p;
9500 line = compile_substitute(line, &ea, &cctx);
9501 }
9502 break;
9503
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009504 case CMD_redir:
9505 ea.arg = p;
9506 line = compile_redir(line, &ea, &cctx);
9507 break;
9508
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009509 case CMD_cexpr:
9510 case CMD_lexpr:
9511 case CMD_caddexpr:
9512 case CMD_laddexpr:
9513 case CMD_cgetexpr:
9514 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009515#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009516 ea.arg = p;
9517 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009518#else
9519 ex_ni(&ea);
9520 line = NULL;
9521#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009522 break;
9523
Bram Moolenaar3988f642020-08-27 22:43:03 +02009524 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009525
Bram Moolenaarae616492020-07-28 20:07:27 +02009526 case CMD_append:
9527 case CMD_change:
9528 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009529 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009530 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009531 case CMD_xit:
9532 not_in_vim9(&ea);
9533 goto erret;
9534
Bram Moolenaar002262f2020-07-08 17:47:57 +02009535 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009536 if (cctx.ctx_skip != SKIP_YES)
9537 {
9538 semsg(_(e_invalid_command_str), ea.cmd);
9539 goto erret;
9540 }
9541 // We don't check for a next command here.
9542 line = (char_u *)"";
9543 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009544
Bram Moolenaar20677332021-06-06 17:02:53 +02009545 case CMD_lua:
9546 case CMD_mzscheme:
9547 case CMD_perl:
9548 case CMD_py3:
9549 case CMD_python3:
9550 case CMD_python:
9551 case CMD_pythonx:
9552 case CMD_ruby:
9553 case CMD_tcl:
9554 ea.arg = p;
9555 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009556 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009557 else
9558 // heredoc lines have been concatenated with NL
9559 // characters in get_function_body()
9560 line = compile_script(line, &cctx);
9561 break;
9562
9563 default:
9564 // Not recognized, execute with do_cmdline_cmd().
9565 ea.arg = p;
9566 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009567 break;
9568 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009569nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009570 if (line == NULL)
9571 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009572 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009573
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009574 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009575 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009577 if (cctx.ctx_type_stack.ga_len < 0)
9578 {
9579 iemsg("Type stack underflow");
9580 goto erret;
9581 }
9582 }
9583
9584 if (cctx.ctx_scope != NULL)
9585 {
9586 if (cctx.ctx_scope->se_type == IF_SCOPE)
9587 emsg(_(e_endif));
9588 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9589 emsg(_(e_endwhile));
9590 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9591 emsg(_(e_endfor));
9592 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009593 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009594 goto erret;
9595 }
9596
Bram Moolenaarefd88552020-06-18 20:50:10 +02009597 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009598 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009599 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9600 ufunc->uf_ret_type = &t_void;
9601 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009602 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009603 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009604 goto erret;
9605 }
9606
9607 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01009608 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009609 }
9610
Bram Moolenaar599410c2021-04-10 14:03:43 +02009611 // When compiled with ":silent!" and there was an error don't consider the
9612 // function compiled.
9613 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009614 {
9615 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9616 + ufunc->uf_dfunc_idx;
9617 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009618 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009619#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009620 if (cctx.ctx_profiling)
9621 {
9622 dfunc->df_instr_prof = instr->ga_data;
9623 dfunc->df_instr_prof_count = instr->ga_len;
9624 }
9625 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009626#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009627 {
9628 dfunc->df_instr = instr->ga_data;
9629 dfunc->df_instr_count = instr->ga_len;
9630 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009631 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009632 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009633 if (cctx.ctx_outer_used)
9634 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009635 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009637
9638 ret = OK;
9639
9640erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009641 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009642 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009643 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9644 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009645
Bram Moolenaar8238f082021-04-20 21:10:48 +02009646 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009647 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009648
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009649 // If using the last entry in the table and it was added above, we
9650 // might as well remove it.
9651 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009652 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009653 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009654 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009655 ufunc->uf_dfunc_idx = 0;
9656 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009657 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009658
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009659 while (cctx.ctx_scope != NULL)
9660 drop_scope(&cctx);
9661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009662 if (errormsg != NULL)
9663 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009664 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009665 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009666 }
9667
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009668 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9669 {
9670 if (ret == OK)
9671 {
9672 emsg(_(e_missing_redir_end));
9673 ret = FAIL;
9674 }
9675 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009676 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009677 }
9678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009679 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009680 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009681 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009682 if (do_estack_push)
9683 estack_pop();
9684
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009685 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009686 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009687 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009688 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009689 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009690}
9691
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009692 void
9693set_function_type(ufunc_T *ufunc)
9694{
9695 int varargs = ufunc->uf_va_name != NULL;
9696 int argcount = ufunc->uf_args.ga_len;
9697
9698 // Create a type for the function, with the return type and any
9699 // argument types.
9700 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9701 // The type is included in "tt_args".
9702 if (argcount > 0 || varargs)
9703 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009704 if (ufunc->uf_type_list.ga_itemsize == 0)
9705 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009706 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9707 argcount, &ufunc->uf_type_list);
9708 // Add argument types to the function type.
9709 if (func_type_add_arg_types(ufunc->uf_func_type,
9710 argcount + varargs,
9711 &ufunc->uf_type_list) == FAIL)
9712 return;
9713 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9714 ufunc->uf_func_type->tt_min_argcount =
9715 argcount - ufunc->uf_def_args.ga_len;
9716 if (ufunc->uf_arg_types == NULL)
9717 {
9718 int i;
9719
9720 // lambda does not have argument types.
9721 for (i = 0; i < argcount; ++i)
9722 ufunc->uf_func_type->tt_args[i] = &t_any;
9723 }
9724 else
9725 mch_memmove(ufunc->uf_func_type->tt_args,
9726 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9727 if (varargs)
9728 {
9729 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009730 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009731 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9732 }
9733 }
9734 else
9735 // No arguments, can use a predefined type.
9736 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9737 argcount, &ufunc->uf_type_list);
9738}
9739
9740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009741/*
9742 * Delete an instruction, free what it contains.
9743 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009744 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009745delete_instr(isn_T *isn)
9746{
9747 switch (isn->isn_type)
9748 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009749 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009750 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009751 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009752 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009753 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009754 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009755 case ISN_LOADENV:
9756 case ISN_LOADG:
9757 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009758 case ISN_LOADT:
9759 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009760 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009761 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009762 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009763 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009764 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009765 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009766 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009767 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009768 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009769 case ISN_STOREW:
9770 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009771 vim_free(isn->isn_arg.string);
9772 break;
9773
Bram Moolenaar4c137212021-04-19 16:48:48 +02009774 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009775 {
9776 int idx;
9777 isn_T *list = isn->isn_arg.subs.subs_instr;
9778
9779 vim_free(isn->isn_arg.subs.subs_cmd);
9780 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9781 delete_instr(list + idx);
9782 vim_free(list);
9783 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009784 break;
9785
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009786 case ISN_INSTR:
9787 {
9788 int idx;
9789 isn_T *list = isn->isn_arg.instr;
9790
9791 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9792 delete_instr(list + idx);
9793 vim_free(list);
9794 }
9795 break;
9796
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009797 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009798 case ISN_STORES:
9799 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009800 break;
9801
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009802 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009803 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009804 vim_free(isn->isn_arg.unlet.ul_name);
9805 break;
9806
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009807 case ISN_STOREOPT:
9808 vim_free(isn->isn_arg.storeopt.so_name);
9809 break;
9810
9811 case ISN_PUSHBLOB: // push blob isn_arg.blob
9812 blob_unref(isn->isn_arg.blob);
9813 break;
9814
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009815 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009816#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009817 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009818#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009819 break;
9820
9821 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009822#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009823 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009824#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009825 break;
9826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009827 case ISN_UCALL:
9828 vim_free(isn->isn_arg.ufunc.cuf_name);
9829 break;
9830
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009831 case ISN_FUNCREF:
9832 {
9833 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9834 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009835 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009836
Bram Moolenaar077a4232020-12-22 18:33:27 +01009837 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9838 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009839 }
9840 break;
9841
9842 case ISN_DCALL:
9843 {
9844 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9845 + isn->isn_arg.dfunc.cdf_idx;
9846
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009847 if (dfunc->df_ufunc != NULL
9848 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009849 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009850 }
9851 break;
9852
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009853 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009854 {
9855 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9856 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9857
9858 if (ufunc != NULL)
9859 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009860 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009861 func_ptr_unref(ufunc);
9862 }
9863
9864 vim_free(lambda);
9865 vim_free(isn->isn_arg.newfunc.nf_global);
9866 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009867 break;
9868
Bram Moolenaar5e654232020-09-16 15:22:00 +02009869 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009870 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009871 free_type(isn->isn_arg.type.ct_type);
9872 break;
9873
Bram Moolenaar02194d22020-10-24 23:08:38 +02009874 case ISN_CMDMOD:
9875 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9876 ->cmod_filter_regmatch.regprog);
9877 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9878 break;
9879
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009880 case ISN_LOADSCRIPT:
9881 case ISN_STORESCRIPT:
9882 vim_free(isn->isn_arg.script.scriptref);
9883 break;
9884
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009885 case ISN_TRY:
9886 vim_free(isn->isn_arg.try.try_ref);
9887 break;
9888
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009889 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +02009890 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009891 vim_free(isn->isn_arg.cexpr.cexpr_ref);
9892 break;
9893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009894 case ISN_2BOOL:
9895 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009896 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009897 case ISN_ADDBLOB:
9898 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009899 case ISN_ANYINDEX:
9900 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009901 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009902 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009903 case ISN_BLOBINDEX:
9904 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009905 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009906 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009907 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009908 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009909 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009910 case ISN_COMPAREANY:
9911 case ISN_COMPAREBLOB:
9912 case ISN_COMPAREBOOL:
9913 case ISN_COMPAREDICT:
9914 case ISN_COMPAREFLOAT:
9915 case ISN_COMPAREFUNC:
9916 case ISN_COMPARELIST:
9917 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009918 case ISN_COMPARESPECIAL:
9919 case ISN_COMPARESTRING:
9920 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009921 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009922 case ISN_DROP:
9923 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009924 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009925 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009926 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009927 case ISN_EXECCONCAT:
9928 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009929 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009930 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009931 case ISN_GETITEM:
9932 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009933 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009934 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009935 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009936 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009937 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009938 case ISN_LOADBDICT:
9939 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009940 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009941 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009942 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009943 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009944 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009945 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009946 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009947 case ISN_NEGATENR:
9948 case ISN_NEWDICT:
9949 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009950 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009951 case ISN_OPFLOAT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009952 case ISN_FINISH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009953 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009954 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009955 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009956 case ISN_PROF_END:
9957 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009958 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009959 case ISN_PUSHF:
9960 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009961 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009962 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009963 case ISN_REDIREND:
9964 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009965 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009966 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009967 case ISN_SHUFFLE:
9968 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009969 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009970 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009971 case ISN_STORENR:
9972 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009973 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009974 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009975 case ISN_STOREV:
9976 case ISN_STRINDEX:
9977 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009978 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009979 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009980 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009981 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009982 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009983 // nothing allocated
9984 break;
9985 }
9986}
9987
9988/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009989 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009990 */
9991 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009992delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009993{
9994 int idx;
9995
9996 ga_clear(&dfunc->df_def_args_isn);
9997
9998 if (dfunc->df_instr != NULL)
9999 {
10000 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10001 delete_instr(dfunc->df_instr + idx);
10002 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010003 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010004 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010005#ifdef FEAT_PROFILE
10006 if (dfunc->df_instr_prof != NULL)
10007 {
10008 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10009 delete_instr(dfunc->df_instr_prof + idx);
10010 VIM_CLEAR(dfunc->df_instr_prof);
10011 dfunc->df_instr_prof = NULL;
10012 }
10013#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010014
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010015 if (mark_deleted)
10016 dfunc->df_deleted = TRUE;
10017 if (dfunc->df_ufunc != NULL)
10018 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010019}
10020
10021/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010022 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010023 * function, unless another user function still uses it.
10024 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010025 */
10026 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010027unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010028{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010029 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010030 {
10031 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10032 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010033
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010034 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010035 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010036 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010037 ufunc->uf_dfunc_idx = 0;
10038 if (dfunc->df_ufunc == ufunc)
10039 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010040 }
10041}
10042
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010043/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010044 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010045 */
10046 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010047link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010048{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010049 if (ufunc->uf_dfunc_idx > 0)
10050 {
10051 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10052 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010053
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010054 ++dfunc->df_refcount;
10055 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010056}
10057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010058#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010059/*
10060 * Free all functions defined with ":def".
10061 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010062 void
10063free_def_functions(void)
10064{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010065 int idx;
10066
10067 for (idx = 0; idx < def_functions.ga_len; ++idx)
10068 {
10069 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10070
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010071 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010072 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010073 }
10074
10075 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010076}
10077#endif
10078
10079
10080#endif // FEAT_EVAL