blob: 44c082d415addc4ceb8c705e9ea38cfde48fb0c3 [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 Moolenaar65c44152020-12-24 15:14:01 +01003568 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003569 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003571 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3572 // points into it. Point to the original line to avoid a dangling pointer.
3573 if (evalarg.eval_tofree_cmdline != NULL)
3574 {
3575 size_t off = *arg - evalarg.eval_tofree_cmdline;
3576
3577 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3578 + off;
3579 }
3580
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003581 clear_evalarg(&evalarg, NULL);
3582
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003583 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003584 {
3585 // The return type will now be known.
3586 set_function_type(ufunc);
3587
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003588 // The function reference count will be 1. When the ISN_FUNCREF
3589 // instruction is deleted the reference count is decremented and the
3590 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003591 return generate_FUNCREF(cctx, ufunc);
3592 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003593
3594 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 return FAIL;
3596}
3597
3598/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003599 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003601 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 */
3603 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003604compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605{
3606 garray_T *instr = &cctx->ctx_instr;
3607 int count = 0;
3608 dict_T *d = dict_alloc();
3609 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003610 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003611 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003612 int is_const;
3613 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614
3615 if (d == NULL)
3616 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003617 if (generate_ppconst(cctx, ppconst) == FAIL)
3618 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003619 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003621 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622
Bram Moolenaar23c55272020-06-21 16:58:13 +02003623 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003624 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003625 *arg = NULL;
3626 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003627 }
3628
3629 if (**arg == '}')
3630 break;
3631
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003632 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003634 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003636 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003637 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003638 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003641 if (isn->isn_type == ISN_PUSHNR)
3642 {
3643 char buf[NUMBUFLEN];
3644
3645 // Convert to string at compile time.
3646 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3647 isn->isn_type = ISN_PUSHS;
3648 isn->isn_arg.string = vim_strsave((char_u *)buf);
3649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 if (isn->isn_type == ISN_PUSHS)
3651 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003652 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003653 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003654 *arg = skipwhite(*arg);
3655 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003656 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003657 emsg(_(e_missing_matching_bracket_after_dict_key));
3658 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003659 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003660 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003662 else
3663 {
3664 // {"name": value},
3665 // {'name': value},
3666 // {name: value} use "name" as a literal key
3667 key = get_literal_key(arg);
3668 if (key == NULL)
3669 return FAIL;
3670 if (generate_PUSHS(cctx, key) == FAIL)
3671 return FAIL;
3672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673
3674 // Check for duplicate keys, if using string keys.
3675 if (key != NULL)
3676 {
3677 item = dict_find(d, key, -1);
3678 if (item != NULL)
3679 {
3680 semsg(_(e_duplicate_key), key);
3681 goto failret;
3682 }
3683 item = dictitem_alloc(key);
3684 if (item != NULL)
3685 {
3686 item->di_tv.v_type = VAR_UNKNOWN;
3687 item->di_tv.v_lock = 0;
3688 if (dict_add(d, item) == FAIL)
3689 dictitem_free(item);
3690 }
3691 }
3692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 if (**arg != ':')
3694 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003695 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003696 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003697 else
3698 semsg(_(e_missing_dict_colon), *arg);
3699 return FAIL;
3700 }
3701 whitep = *arg + 1;
3702 if (!IS_WHITE_OR_NUL(*whitep))
3703 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003704 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 return FAIL;
3706 }
3707
Bram Moolenaar23c55272020-06-21 16:58:13 +02003708 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003709 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003710 *arg = NULL;
3711 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003712 }
3713
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003714 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003716 if (!is_const)
3717 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 ++count;
3719
Bram Moolenaar2c330432020-04-13 14:41:35 +02003720 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003721 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003722 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003723 *arg = NULL;
3724 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 if (**arg == '}')
3727 break;
3728 if (**arg != ',')
3729 {
3730 semsg(_(e_missing_dict_comma), *arg);
3731 goto failret;
3732 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003733 if (IS_WHITE_OR_NUL(*whitep))
3734 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003735 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003736 return FAIL;
3737 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003738 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003739 if (!IS_WHITE_OR_NUL(*whitep))
3740 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003741 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003742 return FAIL;
3743 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003744 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 }
3746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 *arg = *arg + 1;
3748
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003749 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003750 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003751 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003752 *arg += STRLEN(*arg);
3753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003755 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 return generate_NEWDICT(cctx, count);
3757
3758failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003759 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003760 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003761 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003762 *arg = (char_u *)"";
3763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 dict_unref(d);
3765 return FAIL;
3766}
3767
3768/*
3769 * Compile "&option".
3770 */
3771 static int
3772compile_get_option(char_u **arg, cctx_T *cctx)
3773{
3774 typval_T rettv;
3775 char_u *start = *arg;
3776 int ret;
3777
3778 // parse the option and get the current value to get the type.
3779 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003780 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 if (ret == OK)
3782 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003783 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003784 char_u *name = vim_strnsave(start, *arg - start);
3785 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3786 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787
3788 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3789 vim_free(name);
3790 }
3791 clear_tv(&rettv);
3792
3793 return ret;
3794}
3795
3796/*
3797 * Compile "$VAR".
3798 */
3799 static int
3800compile_get_env(char_u **arg, cctx_T *cctx)
3801{
3802 char_u *start = *arg;
3803 int len;
3804 int ret;
3805 char_u *name;
3806
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 ++*arg;
3808 len = get_env_len(arg);
3809 if (len == 0)
3810 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003811 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 return FAIL;
3813 }
3814
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003815 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 name = vim_strnsave(start, len + 1);
3817 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3818 vim_free(name);
3819 return ret;
3820}
3821
3822/*
3823 * Compile "@r".
3824 */
3825 static int
3826compile_get_register(char_u **arg, cctx_T *cctx)
3827{
3828 int ret;
3829
3830 ++*arg;
3831 if (**arg == NUL)
3832 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003833 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 return FAIL;
3835 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003836 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 {
3838 emsg_invreg(**arg);
3839 return FAIL;
3840 }
3841 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3842 ++*arg;
3843 return ret;
3844}
3845
3846/*
3847 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003848 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 */
3850 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003851apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003853 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854
3855 // this works from end to start
3856 while (p > start)
3857 {
3858 --p;
3859 if (*p == '-' || *p == '+')
3860 {
3861 // only '-' has an effect, for '+' we only check the type
3862#ifdef FEAT_FLOAT
3863 if (rettv->v_type == VAR_FLOAT)
3864 {
3865 if (*p == '-')
3866 rettv->vval.v_float = -rettv->vval.v_float;
3867 }
3868 else
3869#endif
3870 {
3871 varnumber_T val;
3872 int error = FALSE;
3873
3874 // tv_get_number_chk() accepts a string, but we don't want that
3875 // here
3876 if (check_not_string(rettv) == FAIL)
3877 return FAIL;
3878 val = tv_get_number_chk(rettv, &error);
3879 clear_tv(rettv);
3880 if (error)
3881 return FAIL;
3882 if (*p == '-')
3883 val = -val;
3884 rettv->v_type = VAR_NUMBER;
3885 rettv->vval.v_number = val;
3886 }
3887 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003888 else if (numeric_only)
3889 {
3890 ++p;
3891 break;
3892 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003893 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 {
3895 int v = tv2bool(rettv);
3896
3897 // '!' is permissive in the type.
3898 clear_tv(rettv);
3899 rettv->v_type = VAR_BOOL;
3900 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3901 }
3902 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003903 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 return OK;
3905}
3906
3907/*
3908 * Recognize v: variables that are constants and set "rettv".
3909 */
3910 static void
3911get_vim_constant(char_u **arg, typval_T *rettv)
3912{
3913 if (STRNCMP(*arg, "v:true", 6) == 0)
3914 {
3915 rettv->v_type = VAR_BOOL;
3916 rettv->vval.v_number = VVAL_TRUE;
3917 *arg += 6;
3918 }
3919 else if (STRNCMP(*arg, "v:false", 7) == 0)
3920 {
3921 rettv->v_type = VAR_BOOL;
3922 rettv->vval.v_number = VVAL_FALSE;
3923 *arg += 7;
3924 }
3925 else if (STRNCMP(*arg, "v:null", 6) == 0)
3926 {
3927 rettv->v_type = VAR_SPECIAL;
3928 rettv->vval.v_number = VVAL_NULL;
3929 *arg += 6;
3930 }
3931 else if (STRNCMP(*arg, "v:none", 6) == 0)
3932 {
3933 rettv->v_type = VAR_SPECIAL;
3934 rettv->vval.v_number = VVAL_NONE;
3935 *arg += 6;
3936 }
3937}
3938
Bram Moolenaar657137c2021-01-09 15:45:23 +01003939 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003940get_compare_type(char_u *p, int *len, int *type_is)
3941{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003942 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003943 int i;
3944
3945 switch (p[0])
3946 {
3947 case '=': if (p[1] == '=')
3948 type = EXPR_EQUAL;
3949 else if (p[1] == '~')
3950 type = EXPR_MATCH;
3951 break;
3952 case '!': if (p[1] == '=')
3953 type = EXPR_NEQUAL;
3954 else if (p[1] == '~')
3955 type = EXPR_NOMATCH;
3956 break;
3957 case '>': if (p[1] != '=')
3958 {
3959 type = EXPR_GREATER;
3960 *len = 1;
3961 }
3962 else
3963 type = EXPR_GEQUAL;
3964 break;
3965 case '<': if (p[1] != '=')
3966 {
3967 type = EXPR_SMALLER;
3968 *len = 1;
3969 }
3970 else
3971 type = EXPR_SEQUAL;
3972 break;
3973 case 'i': if (p[1] == 's')
3974 {
3975 // "is" and "isnot"; but not a prefix of a name
3976 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3977 *len = 5;
3978 i = p[*len];
3979 if (!isalnum(i) && i != '_')
3980 {
3981 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3982 *type_is = TRUE;
3983 }
3984 }
3985 break;
3986 }
3987 return type;
3988}
3989
3990/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003991 * Skip over an expression, ignoring most errors.
3992 */
3993 static void
3994skip_expr_cctx(char_u **arg, cctx_T *cctx)
3995{
3996 evalarg_T evalarg;
3997
3998 CLEAR_FIELD(evalarg);
3999 evalarg.eval_cctx = cctx;
4000 skip_expr(arg, &evalarg);
4001}
4002
4003/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004005 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 */
4007 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004008compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004010 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011
4012 // this works from end to start
4013 while (p > start)
4014 {
4015 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004016 while (VIM_ISWHITE(*p))
4017 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 if (*p == '-' || *p == '+')
4019 {
4020 int negate = *p == '-';
4021 isn_T *isn;
4022
4023 // TODO: check type
4024 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4025 {
4026 --p;
4027 if (*p == '-')
4028 negate = !negate;
4029 }
4030 // only '-' has an effect, for '+' we only check the type
4031 if (negate)
4032 isn = generate_instr(cctx, ISN_NEGATENR);
4033 else
4034 isn = generate_instr(cctx, ISN_CHECKNR);
4035 if (isn == NULL)
4036 return FAIL;
4037 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004038 else if (numeric_only)
4039 {
4040 ++p;
4041 break;
4042 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 else
4044 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004045 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004047 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004049 if (p[-1] == '!')
4050 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004053 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 return FAIL;
4055 }
4056 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004057 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 return OK;
4059}
4060
4061/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004062 * Compile "(expression)": recursive!
4063 * Return FAIL/OK.
4064 */
4065 static int
4066compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4067{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004068 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004069 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004070
Bram Moolenaar24156692021-01-14 20:35:49 +01004071 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4072 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004073 if (ppconst->pp_used <= PPSIZE - 10)
4074 {
4075 ret = compile_expr1(arg, cctx, ppconst);
4076 }
4077 else
4078 {
4079 // Not enough space in ppconst, flush constants.
4080 if (generate_ppconst(cctx, ppconst) == FAIL)
4081 return FAIL;
4082 ret = compile_expr0(arg, cctx);
4083 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004084 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4085 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004086 if (**arg == ')')
4087 ++*arg;
4088 else if (ret == OK)
4089 {
4090 emsg(_(e_missing_close));
4091 ret = FAIL;
4092 }
4093 return ret;
4094}
4095
4096/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004098 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 */
4100 static int
4101compile_subscript(
4102 char_u **arg,
4103 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004104 char_u *start_leader,
4105 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004106 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004108 char_u *name_start = *end_leader;
4109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 for (;;)
4111 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004112 char_u *p = skipwhite(*arg);
4113
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004114 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004115 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004116 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004117
4118 // If a following line starts with "->{" or "->X" advance to that
4119 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004120 // Also if a following line starts with ".x".
4121 if (next != NULL &&
4122 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004123 && (next[2] == '{'
4124 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004125 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004126 {
4127 next = next_line_from_context(cctx, TRUE);
4128 if (next == NULL)
4129 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004130 *arg = next;
4131 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004132 }
4133 }
4134
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004135 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004136 // not a function call.
4137 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004139 garray_T *stack = &cctx->ctx_type_stack;
4140 type_T *type;
4141 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004143 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004144 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004145 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004148 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4149
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004150 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004151 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004153 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 return FAIL;
4155 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004156 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004158 char_u *pstart = p;
4159
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004160 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004161 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004162 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 // something->method()
4165 // Apply the '!', '-' and '+' first:
4166 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004167 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004170 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004171 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004172 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004173 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004174 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004175 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004176 garray_T *stack = &cctx->ctx_type_stack;
4177 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004178 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004179 int expr_isn_start = cctx->ctx_instr.ga_len;
4180 int expr_isn_end;
4181 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004182
4183 // Funcref call: list->(Refs[2])(arg)
4184 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004185 //
4186 // Fist compile the function expression.
4187 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004188 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004189
4190 // Remember the next instruction index, where the instructions
4191 // for arguments are being written.
4192 expr_isn_end = cctx->ctx_instr.ga_len;
4193
4194 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004195 if (**arg != '(')
4196 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004197 if (*skipwhite(*arg) == '(')
4198 emsg(_(e_nowhitespace));
4199 else
4200 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004201 return FAIL;
4202 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004203 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004204 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004205 return FAIL;
4206
Bram Moolenaar2927c072021-04-05 19:41:21 +02004207 // Move the instructions for the arguments to before the
4208 // instructions of the expression and move the type of the
4209 // expression after the argument types. This is what ISN_PCALL
4210 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004211 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004212 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4213 if (arg_isn_count > 0)
4214 {
4215 int expr_isn_count = expr_isn_end - expr_isn_start;
4216 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4217
4218 if (isn == NULL)
4219 return FAIL;
4220 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4221 + expr_isn_start,
4222 sizeof(isn_T) * expr_isn_count);
4223 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4224 + expr_isn_start,
4225 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4226 sizeof(isn_T) * arg_isn_count);
4227 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4228 + expr_isn_start + arg_isn_count,
4229 isn, sizeof(isn_T) * expr_isn_count);
4230 vim_free(isn);
4231
4232 type = ((type_T **)stack->ga_data)[type_idx_start];
4233 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4234 ((type_T **)stack->ga_data) + type_idx_start + 1,
4235 sizeof(type_T *)
4236 * (stack->ga_len - type_idx_start - 1));
4237 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4238 }
4239
Bram Moolenaar7e368202020-12-25 21:56:57 +01004240 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4241 if (generate_PCALL(cctx, argcount,
4242 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 return FAIL;
4244 }
4245 else
4246 {
4247 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004248 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004249 if (!eval_isnamec1(*p))
4250 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004251 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004252 return FAIL;
4253 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004254 if (ASCII_ISALPHA(*p) && p[1] == ':')
4255 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004256 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 ;
4258 if (*p != '(')
4259 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004260 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 return FAIL;
4262 }
4263 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004264 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 return FAIL;
4266 }
4267 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004268 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004270 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004271
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004272 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004273 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004274 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004275 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004276 // TODO: more arguments
4277 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004278 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004279 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004280 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004281
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004282 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004283 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004284 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004285 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004286 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004287 // missing first index is equal to zero
4288 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004289 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004290 else
4291 {
4292 if (compile_expr0(arg, cctx) == FAIL)
4293 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004294 if (**arg == ':')
4295 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004296 semsg(_(e_white_space_required_before_and_after_str_at_str),
4297 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004298 return FAIL;
4299 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004300 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004301 return FAIL;
4302 *arg = skipwhite(*arg);
4303 }
4304 if (**arg == ':')
4305 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004306 is_slice = TRUE;
4307 ++*arg;
4308 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4309 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004310 semsg(_(e_white_space_required_before_and_after_str_at_str),
4311 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004312 return FAIL;
4313 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004314 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004315 return FAIL;
4316 if (**arg == ']')
4317 // missing second index is equal to end of string
4318 generate_PUSHNR(cctx, -1);
4319 else
4320 {
4321 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004322 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004323 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004324 return FAIL;
4325 *arg = skipwhite(*arg);
4326 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328
4329 if (**arg != ']')
4330 {
4331 emsg(_(e_missbrac));
4332 return FAIL;
4333 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004334 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335
Bram Moolenaare42939a2021-04-05 17:11:17 +02004336 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004337 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004339 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004341 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004342 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004343 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004344 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004345
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004346 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004347 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004348 {
4349 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004350 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004351 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004352 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004353 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 while (eval_isnamec(*p))
4355 MB_PTR_ADV(p);
4356 if (p == *arg)
4357 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004358 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 return FAIL;
4360 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004361 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 return FAIL;
4363 *arg = p;
4364 }
4365 else
4366 break;
4367 }
4368
4369 // TODO - see handle_subscript():
4370 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4371 // Don't do this when "Func" is already a partial that was bound
4372 // explicitly (pt_auto is FALSE).
4373
4374 return OK;
4375}
4376
4377/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004378 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4379 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004381 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004382 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004383 *
4384 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 */
4386
4387/*
4388 * number number constant
4389 * 0zFFFFFFFF Blob constant
4390 * "string" string constant
4391 * 'string' literal string constant
4392 * &option-name option value
4393 * @r register contents
4394 * identifier variable value
4395 * function() function call
4396 * $VAR environment variable
4397 * (expression) nested expression
4398 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004399 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 *
4401 * Also handle:
4402 * ! in front logical NOT
4403 * - in front unary minus
4404 * + in front unary plus (ignored)
4405 * trailing (arg) funcref/partial call
4406 * trailing [] subscript in String or List
4407 * trailing .name entry in Dictionary
4408 * trailing ->name() method call
4409 */
4410 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004411compile_expr7(
4412 char_u **arg,
4413 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004414 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 char_u *start_leader, *end_leader;
4417 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004418 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004419 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004421 ppconst->pp_is_const = FALSE;
4422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 /*
4424 * Skip '!', '-' and '+' characters. They are handled later.
4425 */
4426 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004427 if (eval_leader(arg, TRUE) == FAIL)
4428 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 end_leader = *arg;
4430
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004431 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 switch (**arg)
4433 {
4434 /*
4435 * Number constant.
4436 */
4437 case '0': // also for blob starting with 0z
4438 case '1':
4439 case '2':
4440 case '3':
4441 case '4':
4442 case '5':
4443 case '6':
4444 case '7':
4445 case '8':
4446 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004447 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004449 // Apply "-" and "+" just before the number now, right to
4450 // left. Matters especially when "->" follows. Stops at
4451 // '!'.
4452 if (apply_leader(rettv, TRUE,
4453 start_leader, &end_leader) == FAIL)
4454 {
4455 clear_tv(rettv);
4456 return FAIL;
4457 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 break;
4459
4460 /*
4461 * String constant: "string".
4462 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004463 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 return FAIL;
4465 break;
4466
4467 /*
4468 * Literal string constant: 'str''ing'.
4469 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004470 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471 return FAIL;
4472 break;
4473
4474 /*
4475 * Constant Vim variable.
4476 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004477 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 ret = NOTDONE;
4479 break;
4480
4481 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004482 * "true" constant
4483 */
4484 case 't': if (STRNCMP(*arg, "true", 4) == 0
4485 && !eval_isnamec((*arg)[4]))
4486 {
4487 *arg += 4;
4488 rettv->v_type = VAR_BOOL;
4489 rettv->vval.v_number = VVAL_TRUE;
4490 }
4491 else
4492 ret = NOTDONE;
4493 break;
4494
4495 /*
4496 * "false" constant
4497 */
4498 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4499 && !eval_isnamec((*arg)[5]))
4500 {
4501 *arg += 5;
4502 rettv->v_type = VAR_BOOL;
4503 rettv->vval.v_number = VVAL_FALSE;
4504 }
4505 else
4506 ret = NOTDONE;
4507 break;
4508
4509 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004510 * "null" constant
4511 */
4512 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004513 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004514 {
4515 *arg += 4;
4516 rettv->v_type = VAR_SPECIAL;
4517 rettv->vval.v_number = VVAL_NULL;
4518 }
4519 else
4520 ret = NOTDONE;
4521 break;
4522
4523 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 * List: [expr, expr]
4525 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004526 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4527 return FAIL;
4528 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 break;
4530
4531 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 * Dictionary: {'key': val, 'key': val}
4533 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004534 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4535 return FAIL;
4536 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 break;
4538
4539 /*
4540 * Option value: &name
4541 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004542 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4543 return FAIL;
4544 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 break;
4546
4547 /*
4548 * Environment variable: $VAR.
4549 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004550 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4551 return FAIL;
4552 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 break;
4554
4555 /*
4556 * Register contents: @r.
4557 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004558 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4559 return FAIL;
4560 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 break;
4562 /*
4563 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004564 * lambda: (arg, arg) => expr
4565 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004567 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4568 ret = compile_lambda(arg, cctx);
4569 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004570 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 break;
4572
4573 default: ret = NOTDONE;
4574 break;
4575 }
4576 if (ret == FAIL)
4577 return FAIL;
4578
Bram Moolenaar1c747212020-05-09 18:28:34 +02004579 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004581 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004582 clear_tv(rettv);
4583 else
4584 // A constant expression can possibly be handled compile time,
4585 // return the value instead of generating code.
4586 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 }
4588 else if (ret == NOTDONE)
4589 {
4590 char_u *p;
4591 int r;
4592
4593 if (!eval_isnamec1(**arg))
4594 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004595 if (!vim9_bad_comment(*arg))
4596 {
4597 if (ends_excmd(*skipwhite(*arg)))
4598 semsg(_(e_empty_expression_str), *arg);
4599 else
4600 semsg(_(e_name_expected_str), *arg);
4601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 return FAIL;
4603 }
4604
4605 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004606 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004607 if (p - *arg == (size_t)1 && **arg == '_')
4608 {
4609 emsg(_(e_cannot_use_underscore_here));
4610 return FAIL;
4611 }
4612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004614 {
4615 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004618 {
4619 if (generate_ppconst(cctx, ppconst) == FAIL)
4620 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004621 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004622 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 if (r == FAIL)
4624 return FAIL;
4625 }
4626
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004627 // Handle following "[]", ".member", etc.
4628 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004629 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004630 ppconst) == FAIL)
4631 return FAIL;
4632 if (ppconst->pp_used > 0)
4633 {
4634 // apply the '!', '-' and '+' before the constant
4635 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004636 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004637 return FAIL;
4638 return OK;
4639 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004640 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004642 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643}
4644
4645/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004646 * Give the "white on both sides" error, taking the operator from "p[len]".
4647 */
4648 void
4649error_white_both(char_u *op, int len)
4650{
4651 char_u buf[10];
4652
4653 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004654 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004655}
4656
4657/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004658 * <type>expr7: runtime type check / conversion
4659 */
4660 static int
4661compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4662{
4663 type_T *want_type = NULL;
4664
4665 // Recognize <type>
4666 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4667 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004668 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004669 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4670 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004671 return FAIL;
4672
4673 if (**arg != '>')
4674 {
4675 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004676 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004677 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004678 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004679 return FAIL;
4680 }
4681 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004682 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004683 return FAIL;
4684 }
4685
4686 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4687 return FAIL;
4688
4689 if (want_type != NULL)
4690 {
4691 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004692 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004693 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004694
Bram Moolenaard1103582020-08-14 22:44:25 +02004695 generate_ppconst(cctx, ppconst);
4696 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004697 where.wt_index = 0;
4698 where.wt_variable = FALSE;
4699 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004700 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004701 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004702 return FAIL;
4703 }
4704 }
4705
4706 return OK;
4707}
4708
4709/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 * * number multiplication
4711 * / number division
4712 * % number modulo
4713 */
4714 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004715compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716{
4717 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004718 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004719 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004721 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004722 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 return FAIL;
4724
4725 /*
4726 * Repeat computing, until no "*", "/" or "%" is following.
4727 */
4728 for (;;)
4729 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004730 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 if (*op != '*' && *op != '/' && *op != '%')
4732 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004733 if (next != NULL)
4734 {
4735 *arg = next_line_from_context(cctx, TRUE);
4736 op = skipwhite(*arg);
4737 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004738
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004739 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004741 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004742 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004744 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004745 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004747 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004748 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004750
4751 if (ppconst->pp_used == ppconst_used + 2
4752 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4753 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004754 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004755 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4756 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4757 varnumber_T res = 0;
4758 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004760 // both are numbers: compute the result
4761 switch (*op)
4762 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004763 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004764 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004765 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004766 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004767 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004768 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004769 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004770 break;
4771 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004772 if (failed)
4773 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004774 tv1->vval.v_number = res;
4775 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004776 }
4777 else
4778 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004779 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004780 generate_two_op(cctx, op);
4781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782 }
4783
4784 return OK;
4785}
4786
4787/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004788 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 * - number subtraction
4790 * .. string concatenation
4791 */
4792 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004793compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794{
4795 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004796 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004798 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799
4800 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004801 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 return FAIL;
4803
4804 /*
4805 * Repeat computing, until no "+", "-" or ".." is following.
4806 */
4807 for (;;)
4808 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004809 op = may_peek_next_line(cctx, *arg, &next);
4810 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004812 if (op[0] == op[1] && *op != '.' && next)
4813 // Finding "++" or "--" on the next line is a separate command.
4814 // But ".." is concatenation.
4815 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004817 if (next != NULL)
4818 {
4819 *arg = next_line_from_context(cctx, TRUE);
4820 op = skipwhite(*arg);
4821 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004823 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004825 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004826 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827 }
4828
Bram Moolenaare0de1712020-12-02 17:36:54 +01004829 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004830 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004832 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004833 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 return FAIL;
4835
Bram Moolenaara5565e42020-05-09 15:44:01 +02004836 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004837 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004838 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4839 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4840 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4841 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004843 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4844 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004845
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004846 // concat/subtract/add constant numbers
4847 if (*op == '+')
4848 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4849 else if (*op == '-')
4850 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4851 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004852 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004853 // concatenate constant strings
4854 char_u *s1 = tv1->vval.v_string;
4855 char_u *s2 = tv2->vval.v_string;
4856 size_t len1 = STRLEN(s1);
4857
4858 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4859 if (tv1->vval.v_string == NULL)
4860 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004861 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004862 return FAIL;
4863 }
4864 mch_memmove(tv1->vval.v_string, s1, len1);
4865 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004866 vim_free(s1);
4867 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004868 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004869 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 }
4871 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004872 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004873 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004874 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004875 if (*op == '.')
4876 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004877 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4878 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004879 return FAIL;
4880 generate_instr_drop(cctx, ISN_CONCAT, 1);
4881 }
4882 else
4883 generate_two_op(cctx, op);
4884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 }
4886
4887 return OK;
4888}
4889
4890/*
4891 * expr5a == expr5b
4892 * expr5a =~ expr5b
4893 * expr5a != expr5b
4894 * expr5a !~ expr5b
4895 * expr5a > expr5b
4896 * expr5a >= expr5b
4897 * expr5a < expr5b
4898 * expr5a <= expr5b
4899 * expr5a is expr5b
4900 * expr5a isnot expr5b
4901 *
4902 * Produces instructions:
4903 * EVAL expr5a Push result of "expr5a"
4904 * EVAL expr5b Push result of "expr5b"
4905 * COMPARE one of the compare instructions
4906 */
4907 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004908compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004910 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004912 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004915 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916
4917 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004918 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919 return FAIL;
4920
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004921 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004922 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923
4924 /*
4925 * If there is a comparative operator, use it.
4926 */
4927 if (type != EXPR_UNKNOWN)
4928 {
4929 int ic = FALSE; // Default: do not ignore case
4930
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004931 if (next != NULL)
4932 {
4933 *arg = next_line_from_context(cctx, TRUE);
4934 p = skipwhite(*arg);
4935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 if (type_is && (p[len] == '?' || p[len] == '#'))
4937 {
4938 semsg(_(e_invexpr2), *arg);
4939 return FAIL;
4940 }
4941 // extra question mark appended: ignore case
4942 if (p[len] == '?')
4943 {
4944 ic = TRUE;
4945 ++len;
4946 }
4947 // extra '#' appended: match case (ignored)
4948 else if (p[len] == '#')
4949 ++len;
4950 // nothing appended: match case
4951
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004952 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004954 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004955 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 }
4957
4958 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004959 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004960 return FAIL;
4961
Bram Moolenaara5565e42020-05-09 15:44:01 +02004962 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 return FAIL;
4964
Bram Moolenaara5565e42020-05-09 15:44:01 +02004965 if (ppconst->pp_used == ppconst_used + 2)
4966 {
4967 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4968 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4969 int ret;
4970
4971 // Both sides are a constant, compute the result now.
4972 // First check for a valid combination of types, this is more
4973 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004974 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004975 ret = FAIL;
4976 else
4977 {
4978 ret = typval_compare(tv1, tv2, type, ic);
4979 tv1->v_type = VAR_BOOL;
4980 tv1->vval.v_number = tv1->vval.v_number
4981 ? VVAL_TRUE : VVAL_FALSE;
4982 clear_tv(tv2);
4983 --ppconst->pp_used;
4984 }
4985 return ret;
4986 }
4987
4988 generate_ppconst(cctx, ppconst);
4989 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 }
4991
4992 return OK;
4993}
4994
Bram Moolenaar7f141552020-05-09 17:35:53 +02004995static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997/*
4998 * Compile || or &&.
4999 */
5000 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005001compile_and_or(
5002 char_u **arg,
5003 cctx_T *cctx,
5004 char *op,
5005 ppconst_T *ppconst,
5006 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005008 char_u *next;
5009 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 int opchar = *op;
5011
5012 if (p[0] == opchar && p[1] == opchar)
5013 {
5014 garray_T *instr = &cctx->ctx_instr;
5015 garray_T end_ga;
5016
5017 /*
5018 * Repeat until there is no following "||" or "&&"
5019 */
5020 ga_init2(&end_ga, sizeof(int), 10);
5021 while (p[0] == opchar && p[1] == opchar)
5022 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005023 long start_lnum = SOURCING_LNUM;
5024 int start_ctx_lnum = cctx->ctx_lnum;
5025 int save_lnum;
5026
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005027 if (next != NULL)
5028 {
5029 *arg = next_line_from_context(cctx, TRUE);
5030 p = skipwhite(*arg);
5031 }
5032
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005033 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5034 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005035 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005036 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005037 return FAIL;
5038 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005040 // TODO: use ppconst if the value is a constant and check
5041 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005042 generate_ppconst(cctx, ppconst);
5043
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005044 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005045 SOURCING_LNUM = start_lnum;
5046 save_lnum = cctx->ctx_lnum;
5047 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005048 if (bool_on_stack(cctx) == FAIL)
5049 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005050 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005051 ga_clear(&end_ga);
5052 return FAIL;
5053 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005054 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005055
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 if (ga_grow(&end_ga, 1) == FAIL)
5057 {
5058 ga_clear(&end_ga);
5059 return FAIL;
5060 }
5061 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5062 ++end_ga.ga_len;
5063 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005064 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065
5066 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005067 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005068 {
5069 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005070 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005071 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005072
Bram Moolenaara5565e42020-05-09 15:44:01 +02005073 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5074 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 {
5076 ga_clear(&end_ga);
5077 return FAIL;
5078 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005079
5080 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005082 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005084 // Every part must evaluate to a bool.
5085 if (bool_on_stack(cctx) == FAIL)
5086 {
5087 ga_clear(&end_ga);
5088 return FAIL;
5089 }
5090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 // Fill in the end label in all jumps.
5092 while (end_ga.ga_len > 0)
5093 {
5094 isn_T *isn;
5095
5096 --end_ga.ga_len;
5097 isn = ((isn_T *)instr->ga_data)
5098 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5099 isn->isn_arg.jump.jump_where = instr->ga_len;
5100 }
5101 ga_clear(&end_ga);
5102 }
5103
5104 return OK;
5105}
5106
5107/*
5108 * expr4a && expr4a && expr4a logical AND
5109 *
5110 * Produces instructions:
5111 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005112 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005113 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005115 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 * EVAL expr4c Push result of "expr4c"
5117 * end:
5118 */
5119 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005120compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005122 int ppconst_used = ppconst->pp_used;
5123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005125 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126 return FAIL;
5127
5128 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005129 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130}
5131
5132/*
5133 * expr3a || expr3b || expr3c logical OR
5134 *
5135 * Produces instructions:
5136 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005137 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005138 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005140 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141 * EVAL expr3c Push result of "expr3c"
5142 * end:
5143 */
5144 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005145compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005147 int ppconst_used = ppconst->pp_used;
5148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005150 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151 return FAIL;
5152
5153 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005154 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155}
5156
5157/*
5158 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005159 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005160 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161 * JUMP_IF_FALSE alt jump if false
5162 * EVAL expr1a
5163 * JUMP_ALWAYS end
5164 * alt: EVAL expr1b
5165 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005166 *
5167 * Toplevel expression: expr2 ?? expr1
5168 * Produces instructions:
5169 * EVAL expr2 Push result of "expr2"
5170 * JUMP_AND_KEEP_IF_TRUE end jump if true
5171 * EVAL expr1
5172 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 */
5174 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005175compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176{
5177 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005178 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005179 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180
Bram Moolenaar3988f642020-08-27 22:43:03 +02005181 // Ignore all kinds of errors when not producing code.
5182 if (cctx->ctx_skip == SKIP_YES)
5183 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005184 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005185 return OK;
5186 }
5187
Bram Moolenaar61a89812020-05-07 16:58:17 +02005188 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005189 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 return FAIL;
5191
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005192 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 if (*p == '?')
5194 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005195 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196 garray_T *instr = &cctx->ctx_instr;
5197 garray_T *stack = &cctx->ctx_type_stack;
5198 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005199 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005201 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005202 int has_const_expr = FALSE;
5203 int const_value = FALSE;
5204 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005206 if (next != NULL)
5207 {
5208 *arg = next_line_from_context(cctx, TRUE);
5209 p = skipwhite(*arg);
5210 }
5211
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005212 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005213 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005214 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005215 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005216 return FAIL;
5217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218
Bram Moolenaara5565e42020-05-09 15:44:01 +02005219 if (ppconst->pp_used == ppconst_used + 1)
5220 {
5221 // the condition is a constant, we know whether the ? or the :
5222 // expression is to be evaluated.
5223 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005224 if (op_falsy)
5225 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5226 else
5227 {
5228 int error = FALSE;
5229
5230 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5231 &error);
5232 if (error)
5233 return FAIL;
5234 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005235 cctx->ctx_skip = save_skip == SKIP_YES ||
5236 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5237
5238 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5239 // "left ?? right" and "left" is truthy: produce "left"
5240 generate_ppconst(cctx, ppconst);
5241 else
5242 {
5243 clear_tv(&ppconst->pp_tv[ppconst_used]);
5244 --ppconst->pp_used;
5245 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005246 }
5247 else
5248 {
5249 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005250 if (op_falsy)
5251 end_idx = instr->ga_len;
5252 generate_JUMP(cctx, op_falsy
5253 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5254 if (op_falsy)
5255 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005257
5258 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005259 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005260 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005261 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005262 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263
Bram Moolenaara5565e42020-05-09 15:44:01 +02005264 if (!has_const_expr)
5265 {
5266 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005268 if (!op_falsy)
5269 {
5270 // remember the type and drop it
5271 --stack->ga_len;
5272 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005273
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005274 end_idx = instr->ga_len;
5275 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005276
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005277 // jump here from JUMP_IF_FALSE
5278 isn = ((isn_T *)instr->ga_data) + alt_idx;
5279 isn->isn_arg.jump.jump_where = instr->ga_len;
5280 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005282
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005283 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005285 // Check for the ":".
5286 p = may_peek_next_line(cctx, *arg, &next);
5287 if (*p != ':')
5288 {
5289 emsg(_(e_missing_colon));
5290 return FAIL;
5291 }
5292 if (next != NULL)
5293 {
5294 *arg = next_line_from_context(cctx, TRUE);
5295 p = skipwhite(*arg);
5296 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005297
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005298 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5299 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005300 semsg(_(e_white_space_required_before_and_after_str_at_str),
5301 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005302 return FAIL;
5303 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005305 // evaluate the third expression
5306 if (has_const_expr)
5307 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005308 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005309 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005310 return FAIL;
5311 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5312 return FAIL;
5313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005314
Bram Moolenaara5565e42020-05-09 15:44:01 +02005315 if (!has_const_expr)
5316 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005317 type_T **typep;
5318
Bram Moolenaara5565e42020-05-09 15:44:01 +02005319 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320
Bram Moolenaara5565e42020-05-09 15:44:01 +02005321 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005322 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5323 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005324
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005325 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005326 isn = ((isn_T *)instr->ga_data) + end_idx;
5327 isn->isn_arg.jump.jump_where = instr->ga_len;
5328 }
5329
5330 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 }
5332 return OK;
5333}
5334
5335/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005336 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005337 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5338 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005339 */
5340 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005341compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005342{
5343 ppconst_T ppconst;
5344
5345 CLEAR_FIELD(ppconst);
5346 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5347 {
5348 clear_ppconst(&ppconst);
5349 return FAIL;
5350 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005351 if (is_const != NULL)
5352 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005353 if (generate_ppconst(cctx, &ppconst) == FAIL)
5354 return FAIL;
5355 return OK;
5356}
5357
5358/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005359 * Toplevel expression.
5360 */
5361 static int
5362compile_expr0(char_u **arg, cctx_T *cctx)
5363{
5364 return compile_expr0_ext(arg, cctx, NULL);
5365}
5366
5367/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005368 * Compile "return [expr]".
5369 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370 */
5371 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005372compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373{
5374 char_u *p = arg;
5375 garray_T *stack = &cctx->ctx_type_stack;
5376 type_T *stack_type;
5377
5378 if (*p != NUL && *p != '|' && *p != '\n')
5379 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005380 if (legacy)
5381 {
5382 int save_flags = cmdmod.cmod_flags;
5383
5384 generate_LEGACY_EVAL(cctx, p);
5385 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5386 0, cctx, FALSE, FALSE) == FAIL)
5387 return NULL;
5388 cmdmod.cmod_flags |= CMOD_LEGACY;
5389 (void)skip_expr(&p, NULL);
5390 cmdmod.cmod_flags = save_flags;
5391 }
5392 else
5393 {
5394 // compile return argument into instructions
5395 if (compile_expr0(&p, cctx) == FAIL)
5396 return NULL;
5397 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005399 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005400 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005401 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005402 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005403 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5404 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005405 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005406 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005407 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005408 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005409 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005410 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5411 && stack_type->tt_type != VAR_VOID
5412 && stack_type->tt_type != VAR_UNKNOWN)
5413 {
5414 emsg(_(e_returning_value_in_function_without_return_type));
5415 return NULL;
5416 }
5417 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005418 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005419 return NULL;
5420 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422 }
5423 else
5424 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005425 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005426 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005427 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5428 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005430 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 return NULL;
5432 }
5433
5434 // No argument, return zero.
5435 generate_PUSHNR(cctx, 0);
5436 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005437
5438 // Undo any command modifiers.
5439 generate_undo_cmdmods(cctx);
5440
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005441 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 return NULL;
5443
5444 // "return val | endif" is possible
5445 return skipwhite(p);
5446}
5447
5448/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005449 * Get a line from the compilation context, compatible with exarg_T getline().
5450 * Return a pointer to the line in allocated memory.
5451 * Return NULL for end-of-file or some error.
5452 */
5453 static char_u *
5454exarg_getline(
5455 int c UNUSED,
5456 void *cookie,
5457 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005458 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005459{
5460 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005461 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005462
Bram Moolenaar66250c92020-08-20 15:02:42 +02005463 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005464 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005465 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005466 return NULL;
5467 ++cctx->ctx_lnum;
5468 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5469 // Comment lines result in NULL pointers, skip them.
5470 if (p != NULL)
5471 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005472 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005473}
5474
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005475 void
5476fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5477{
5478 eap->getline = exarg_getline;
5479 eap->cookie = cctx;
5480}
5481
Bram Moolenaar04b12692020-05-04 23:24:44 +02005482/*
5483 * Compile a nested :def command.
5484 */
5485 static char_u *
5486compile_nested_function(exarg_T *eap, cctx_T *cctx)
5487{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005488 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005489 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005490 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005491 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005492 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005493 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005494
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005495 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005496 {
5497 emsg(_(e_cannot_use_bang_with_nested_def));
5498 return NULL;
5499 }
5500
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005501 if (*name_start == '/')
5502 {
5503 name_end = skip_regexp(name_start + 1, '/', TRUE);
5504 if (*name_end == '/')
5505 ++name_end;
5506 eap->nextcmd = check_nextcmd(name_end);
5507 }
5508 if (name_end == name_start || *skipwhite(name_end) != '(')
5509 {
5510 if (!ends_excmd2(name_start, name_end))
5511 {
5512 semsg(_(e_invalid_command_str), eap->cmd);
5513 return NULL;
5514 }
5515
5516 // "def" or "def Name": list functions
5517 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5518 return NULL;
5519 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5520 }
5521
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005522 // Only g:Func() can use a namespace.
5523 if (name_start[1] == ':' && !is_global)
5524 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005525 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005526 return NULL;
5527 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005528 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005529 return NULL;
5530
Bram Moolenaar04b12692020-05-04 23:24:44 +02005531 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005532 fill_exarg_from_cctx(eap, cctx);
5533
Bram Moolenaar04b12692020-05-04 23:24:44 +02005534 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005535 lambda_name = vim_strsave(get_lambda_name());
5536 if (lambda_name == NULL)
5537 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005538 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005539
Bram Moolenaar822ba242020-05-24 23:00:18 +02005540 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005541 {
5542 r = eap->skip ? OK : FAIL;
5543 goto theend;
5544 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005545
5546 // copy over the block scope IDs before compiling
5547 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5548 {
5549 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5550
5551 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5552 if (ufunc->uf_block_ids != NULL)
5553 {
5554 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5555 sizeof(int) * block_depth);
5556 ufunc->uf_block_depth = block_depth;
5557 }
5558 }
5559
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005560 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5561 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005562 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005563 {
5564 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005565 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005566 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005567
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005568 if (is_global)
5569 {
5570 char_u *func_name = vim_strnsave(name_start + 2,
5571 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005572
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005573 if (func_name == NULL)
5574 r = FAIL;
5575 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005576 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005577 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005578 lambda_name = NULL;
5579 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005580 }
5581 else
5582 {
5583 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005584 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005585 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005586
Bram Moolenaareef21022020-08-01 22:16:43 +02005587 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005588 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005589 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005590 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005591 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5592 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005593 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005594
5595theend:
5596 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005597 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005598}
5599
5600/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005601 * Return the length of an assignment operator, or zero if there isn't one.
5602 */
5603 int
5604assignment_len(char_u *p, int *heredoc)
5605{
5606 if (*p == '=')
5607 {
5608 if (p[1] == '<' && p[2] == '<')
5609 {
5610 *heredoc = TRUE;
5611 return 3;
5612 }
5613 return 1;
5614 }
5615 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5616 return 2;
5617 if (STRNCMP(p, "..=", 3) == 0)
5618 return 3;
5619 return 0;
5620}
5621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005622/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005623 * Generate the load instruction for "name".
5624 */
5625 static void
5626generate_loadvar(
5627 cctx_T *cctx,
5628 assign_dest_T dest,
5629 char_u *name,
5630 lvar_T *lvar,
5631 type_T *type)
5632{
5633 switch (dest)
5634 {
5635 case dest_option:
5636 // TODO: check the option exists
5637 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5638 break;
5639 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005640 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5641 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5642 else
5643 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005644 break;
5645 case dest_buffer:
5646 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5647 break;
5648 case dest_window:
5649 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5650 break;
5651 case dest_tab:
5652 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5653 break;
5654 case dest_script:
5655 compile_load_scriptvar(cctx,
5656 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5657 break;
5658 case dest_env:
5659 // Include $ in the name here
5660 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5661 break;
5662 case dest_reg:
5663 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5664 break;
5665 case dest_vimvar:
5666 generate_LOADV(cctx, name + 2, TRUE);
5667 break;
5668 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005669 if (lvar->lv_from_outer > 0)
5670 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5671 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005672 else
5673 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5674 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005675 case dest_expr:
5676 // list or dict value should already be on the stack.
5677 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005678 }
5679}
5680
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005681/*
5682 * Skip over "[expr]" or ".member".
5683 * Does not check for any errors.
5684 */
5685 static char_u *
5686skip_index(char_u *start)
5687{
5688 char_u *p = start;
5689
5690 if (*p == '[')
5691 {
5692 p = skipwhite(p + 1);
5693 (void)skip_expr(&p, NULL);
5694 p = skipwhite(p);
5695 if (*p == ']')
5696 return p + 1;
5697 return p;
5698 }
5699 // if (*p == '.')
5700 return to_name_end(p + 1, TRUE);
5701}
5702
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005703 void
5704vim9_declare_error(char_u *name)
5705{
5706 char *scope = "";
5707
5708 switch (*name)
5709 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005710 case 'g': scope = _("global"); break;
5711 case 'b': scope = _("buffer"); break;
5712 case 'w': scope = _("window"); break;
5713 case 't': scope = _("tab"); break;
5714 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005715 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005716 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005717 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005718 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005719 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005720 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005721 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005722 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005723 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005724}
5725
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005726/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005727 * For one assignment figure out the type of destination. Return it in "dest".
5728 * When not recognized "dest" is not set.
5729 * For an option "opt_flags" is set.
5730 * For a v:var "vimvaridx" is set.
5731 * "type" is set to the destination type if known, unchanted otherwise.
5732 * Return FAIL if an error message was given.
5733 */
5734 static int
5735get_var_dest(
5736 char_u *name,
5737 assign_dest_T *dest,
5738 int cmdidx,
5739 int *opt_flags,
5740 int *vimvaridx,
5741 type_T **type,
5742 cctx_T *cctx)
5743{
5744 char_u *p;
5745
5746 if (*name == '&')
5747 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005748 int cc;
5749 long numval;
5750 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005751
5752 *dest = dest_option;
5753 if (cmdidx == CMD_final || cmdidx == CMD_const)
5754 {
5755 emsg(_(e_const_option));
5756 return FAIL;
5757 }
5758 p = name;
5759 p = find_option_end(&p, opt_flags);
5760 if (p == NULL)
5761 {
5762 // cannot happen?
5763 emsg(_(e_letunexp));
5764 return FAIL;
5765 }
5766 cc = *p;
5767 *p = NUL;
5768 opt_type = get_option_value(skip_option_env_lead(name),
5769 &numval, NULL, *opt_flags);
5770 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005771 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005772 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005773 case gov_unknown:
5774 semsg(_(e_unknown_option), name);
5775 return FAIL;
5776 case gov_string:
5777 case gov_hidden_string:
5778 *type = &t_string;
5779 break;
5780 case gov_bool:
5781 case gov_hidden_bool:
5782 *type = &t_bool;
5783 break;
5784 case gov_number:
5785 case gov_hidden_number:
5786 *type = &t_number;
5787 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005788 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005789 }
5790 else if (*name == '$')
5791 {
5792 *dest = dest_env;
5793 *type = &t_string;
5794 }
5795 else if (*name == '@')
5796 {
5797 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5798 {
5799 emsg_invreg(name[1]);
5800 return FAIL;
5801 }
5802 *dest = dest_reg;
5803 *type = &t_string;
5804 }
5805 else if (STRNCMP(name, "g:", 2) == 0)
5806 {
5807 *dest = dest_global;
5808 }
5809 else if (STRNCMP(name, "b:", 2) == 0)
5810 {
5811 *dest = dest_buffer;
5812 }
5813 else if (STRNCMP(name, "w:", 2) == 0)
5814 {
5815 *dest = dest_window;
5816 }
5817 else if (STRNCMP(name, "t:", 2) == 0)
5818 {
5819 *dest = dest_tab;
5820 }
5821 else if (STRNCMP(name, "v:", 2) == 0)
5822 {
5823 typval_T *vtv;
5824 int di_flags;
5825
5826 *vimvaridx = find_vim_var(name + 2, &di_flags);
5827 if (*vimvaridx < 0)
5828 {
5829 semsg(_(e_variable_not_found_str), name);
5830 return FAIL;
5831 }
5832 // We use the current value of "sandbox" here, is that OK?
5833 if (var_check_ro(di_flags, name, FALSE))
5834 return FAIL;
5835 *dest = dest_vimvar;
5836 vtv = get_vim_var_tv(*vimvaridx);
5837 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5838 }
5839 return OK;
5840}
5841
5842/*
5843 * Generate a STORE instruction for "dest", not being "dest_local".
5844 * Return FAIL when out of memory.
5845 */
5846 static int
5847generate_store_var(
5848 cctx_T *cctx,
5849 assign_dest_T dest,
5850 int opt_flags,
5851 int vimvaridx,
5852 int scriptvar_idx,
5853 int scriptvar_sid,
5854 type_T *type,
5855 char_u *name)
5856{
5857 switch (dest)
5858 {
5859 case dest_option:
5860 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5861 opt_flags);
5862 case dest_global:
5863 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005864 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5865 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005866 case dest_buffer:
5867 // include b: with the name, easier to execute that way
5868 return generate_STORE(cctx, ISN_STOREB, 0, name);
5869 case dest_window:
5870 // include w: with the name, easier to execute that way
5871 return generate_STORE(cctx, ISN_STOREW, 0, name);
5872 case dest_tab:
5873 // include t: with the name, easier to execute that way
5874 return generate_STORE(cctx, ISN_STORET, 0, name);
5875 case dest_env:
5876 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5877 case dest_reg:
5878 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5879 case dest_vimvar:
5880 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5881 case dest_script:
5882 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005883 // "s:" may be included in the name.
5884 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005885 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005886 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5887 scriptvar_sid, scriptvar_idx, type);
5888 case dest_local:
5889 case dest_expr:
5890 // cannot happen
5891 break;
5892 }
5893 return FAIL;
5894}
5895
Bram Moolenaar752fc692021-01-04 21:57:11 +01005896 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005897generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5898{
5899 if (lhs->lhs_dest != dest_local)
5900 return generate_store_var(cctx, lhs->lhs_dest,
5901 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5902 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5903 lhs->lhs_type, lhs->lhs_name);
5904
5905 if (lhs->lhs_lvar != NULL)
5906 {
5907 garray_T *instr = &cctx->ctx_instr;
5908 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5909
5910 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5911 // ISN_STORENR
5912 if (lhs->lhs_lvar->lv_from_outer == 0
5913 && instr->ga_len == instr_count + 1
5914 && isn->isn_type == ISN_PUSHNR)
5915 {
5916 varnumber_T val = isn->isn_arg.number;
5917 garray_T *stack = &cctx->ctx_type_stack;
5918
5919 isn->isn_type = ISN_STORENR;
5920 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5921 isn->isn_arg.storenr.stnr_val = val;
5922 if (stack->ga_len > 0)
5923 --stack->ga_len;
5924 }
5925 else if (lhs->lhs_lvar->lv_from_outer > 0)
5926 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5927 lhs->lhs_lvar->lv_from_outer);
5928 else
5929 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5930 }
5931 return OK;
5932}
5933
5934 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005935is_decl_command(int cmdidx)
5936{
5937 return cmdidx == CMD_let || cmdidx == CMD_var
5938 || cmdidx == CMD_final || cmdidx == CMD_const;
5939}
5940
5941/*
5942 * Figure out the LHS type and other properties for an assignment or one item
5943 * of ":unlet" with an index.
5944 * Returns OK or FAIL.
5945 */
5946 static int
5947compile_lhs(
5948 char_u *var_start,
5949 lhs_T *lhs,
5950 int cmdidx,
5951 int heredoc,
5952 int oplen,
5953 cctx_T *cctx)
5954{
5955 char_u *var_end;
5956 int is_decl = is_decl_command(cmdidx);
5957
5958 CLEAR_POINTER(lhs);
5959 lhs->lhs_dest = dest_local;
5960 lhs->lhs_vimvaridx = -1;
5961 lhs->lhs_scriptvar_idx = -1;
5962
5963 // "dest_end" is the end of the destination, including "[expr]" or
5964 // ".name".
5965 // "var_end" is the end of the variable/option/etc. name.
5966 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5967 if (*var_start == '@')
5968 var_end = var_start + 2;
5969 else
5970 {
5971 // skip over the leading "&", "&l:", "&g:" and "$"
5972 var_end = skip_option_env_lead(var_start);
5973 var_end = to_name_end(var_end, TRUE);
5974 }
5975
5976 // "a: type" is declaring variable "a" with a type, not dict "a:".
5977 if (is_decl && lhs->lhs_dest_end == var_start + 2
5978 && lhs->lhs_dest_end[-1] == ':')
5979 --lhs->lhs_dest_end;
5980 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5981 --var_end;
5982
5983 // compute the length of the destination without "[expr]" or ".name"
5984 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02005985 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005986 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5987 if (lhs->lhs_name == NULL)
5988 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005989
5990 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5991 // Something follows after the variable: "var[idx]" or "var.key".
5992 lhs->lhs_has_index = TRUE;
5993
Bram Moolenaar752fc692021-01-04 21:57:11 +01005994 if (heredoc)
5995 lhs->lhs_type = &t_list_string;
5996 else
5997 lhs->lhs_type = &t_any;
5998
5999 if (cctx->ctx_skip != SKIP_YES)
6000 {
6001 int declare_error = FALSE;
6002
6003 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6004 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6005 &lhs->lhs_type, cctx) == FAIL)
6006 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006007 if (lhs->lhs_dest != dest_local
6008 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006009 {
6010 // Specific kind of variable recognized.
6011 declare_error = is_decl;
6012 }
6013 else
6014 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006015 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006016 if (check_reserved_name(lhs->lhs_name) == FAIL)
6017 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006018
6019 if (lookup_local(var_start, lhs->lhs_varlen,
6020 &lhs->lhs_local_lvar, cctx) == OK)
6021 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6022 else
6023 {
6024 CLEAR_FIELD(lhs->lhs_arg_lvar);
6025 if (arg_exists(var_start, lhs->lhs_varlen,
6026 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6027 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6028 {
6029 if (is_decl)
6030 {
6031 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6032 return FAIL;
6033 }
6034 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6035 }
6036 }
6037 if (lhs->lhs_lvar != NULL)
6038 {
6039 if (is_decl)
6040 {
6041 semsg(_(e_variable_already_declared), lhs->lhs_name);
6042 return FAIL;
6043 }
6044 }
6045 else
6046 {
6047 int script_namespace = lhs->lhs_varlen > 1
6048 && STRNCMP(var_start, "s:", 2) == 0;
6049 int script_var = (script_namespace
6050 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006051 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006052 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006053 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006054 imported_T *import =
6055 find_imported(var_start, lhs->lhs_varlen, cctx);
6056
6057 if (script_namespace || script_var || import != NULL)
6058 {
6059 char_u *rawname = lhs->lhs_name
6060 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6061
6062 if (is_decl)
6063 {
6064 if (script_namespace)
6065 semsg(_(e_cannot_declare_script_variable_in_function),
6066 lhs->lhs_name);
6067 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006068 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006069 lhs->lhs_name);
6070 return FAIL;
6071 }
6072 else if (cctx->ctx_ufunc->uf_script_ctx_version
6073 == SCRIPT_VERSION_VIM9
6074 && script_namespace
6075 && !script_var && import == NULL)
6076 {
6077 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6078 return FAIL;
6079 }
6080
6081 lhs->lhs_dest = dest_script;
6082
6083 // existing script-local variables should have a type
6084 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6085 if (import != NULL)
6086 lhs->lhs_scriptvar_sid = import->imp_sid;
6087 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6088 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006089 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006090 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006091 lhs->lhs_scriptvar_sid, rawname,
6092 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6093 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006094 if (lhs->lhs_scriptvar_idx >= 0)
6095 {
6096 scriptitem_T *si = SCRIPT_ITEM(
6097 lhs->lhs_scriptvar_sid);
6098 svar_T *sv =
6099 ((svar_T *)si->sn_var_vals.ga_data)
6100 + lhs->lhs_scriptvar_idx;
6101 lhs->lhs_type = sv->sv_type;
6102 }
6103 }
6104 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006105 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006106 == FAIL)
6107 return FAIL;
6108 }
6109 }
6110
6111 if (declare_error)
6112 {
6113 vim9_declare_error(lhs->lhs_name);
6114 return FAIL;
6115 }
6116 }
6117
6118 // handle "a:name" as a name, not index "name" on "a"
6119 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6120 var_end = lhs->lhs_dest_end;
6121
6122 if (lhs->lhs_dest != dest_option)
6123 {
6124 if (is_decl && *var_end == ':')
6125 {
6126 char_u *p;
6127
6128 // parse optional type: "let var: type = expr"
6129 if (!VIM_ISWHITE(var_end[1]))
6130 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006131 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006132 return FAIL;
6133 }
6134 p = skipwhite(var_end + 1);
6135 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6136 if (lhs->lhs_type == NULL)
6137 return FAIL;
6138 lhs->lhs_has_type = TRUE;
6139 }
6140 else if (lhs->lhs_lvar != NULL)
6141 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6142 }
6143
Bram Moolenaare42939a2021-04-05 17:11:17 +02006144 if (oplen == 3 && !heredoc
6145 && lhs->lhs_dest != dest_global
6146 && !lhs->lhs_has_index
6147 && lhs->lhs_type->tt_type != VAR_STRING
6148 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006149 {
6150 emsg(_(e_can_only_concatenate_to_string));
6151 return FAIL;
6152 }
6153
6154 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6155 && cctx->ctx_skip != SKIP_YES)
6156 {
6157 if (oplen > 1 && !heredoc)
6158 {
6159 // +=, /=, etc. require an existing variable
6160 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6161 return FAIL;
6162 }
6163 if (!is_decl)
6164 {
6165 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6166 return FAIL;
6167 }
6168
Bram Moolenaar3f327882021-03-17 20:56:38 +01006169 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006170 if ((lhs->lhs_type->tt_type == VAR_FUNC
6171 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006172 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006173 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006174
6175 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006176 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6177 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6178 if (lhs->lhs_lvar == NULL)
6179 return FAIL;
6180 lhs->lhs_new_local = TRUE;
6181 }
6182
6183 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006184 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006185 {
6186 // Something follows after the variable: "var[idx]" or "var.key".
6187 // TODO: should we also handle "->func()" here?
6188 if (is_decl)
6189 {
6190 emsg(_(e_cannot_use_index_when_declaring_variable));
6191 return FAIL;
6192 }
6193
6194 if (var_start[lhs->lhs_varlen] == '['
6195 || var_start[lhs->lhs_varlen] == '.')
6196 {
6197 char_u *after = var_start + lhs->lhs_varlen;
6198 char_u *p;
6199
6200 // Only the last index is used below, if there are others
6201 // before it generate code for the expression. Thus for
6202 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6203 for (;;)
6204 {
6205 p = skip_index(after);
6206 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006207 {
6208 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006209 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006210 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006211 after = p;
6212 }
6213 if (after > var_start + lhs->lhs_varlen)
6214 {
6215 lhs->lhs_varlen = after - var_start;
6216 lhs->lhs_dest = dest_expr;
6217 // We don't know the type before evaluating the expression,
6218 // use "any" until then.
6219 lhs->lhs_type = &t_any;
6220 }
6221
Bram Moolenaar752fc692021-01-04 21:57:11 +01006222 if (lhs->lhs_type->tt_member == NULL)
6223 lhs->lhs_member_type = &t_any;
6224 else
6225 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6226 }
6227 else
6228 {
6229 semsg("Not supported yet: %s", var_start);
6230 return FAIL;
6231 }
6232 }
6233 return OK;
6234}
6235
6236/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006237 * Figure out the LHS and check a few errors.
6238 */
6239 static int
6240compile_assign_lhs(
6241 char_u *var_start,
6242 lhs_T *lhs,
6243 int cmdidx,
6244 int is_decl,
6245 int heredoc,
6246 int oplen,
6247 cctx_T *cctx)
6248{
6249 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6250 return FAIL;
6251
6252 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6253 {
6254 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6255 return FAIL;
6256 }
6257 if (!is_decl && lhs->lhs_lvar != NULL
6258 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6259 {
6260 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6261 return FAIL;
6262 }
6263 return OK;
6264}
6265
6266/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006267 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6268 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006269 */
6270 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006271compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006272 char_u *var_start,
6273 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006274 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006275 cctx_T *cctx)
6276{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006277 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006278 char_u *p;
6279 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006280 int need_white_before = TRUE;
6281 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006282
Bram Moolenaar752fc692021-01-04 21:57:11 +01006283 p = var_start + varlen;
6284 if (*p == '[')
6285 {
6286 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006287 if (*p == ':')
6288 {
6289 // empty first index, push zero
6290 r = generate_PUSHNR(cctx, 0);
6291 need_white_before = FALSE;
6292 }
6293 else
6294 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006295
6296 if (r == OK && *skipwhite(p) == ':')
6297 {
6298 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006299 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006300 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006301 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006302 empty_second = *skipwhite(p + 1) == ']';
6303 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6304 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006305 {
6306 semsg(_(e_white_space_required_before_and_after_str_at_str),
6307 ":", p);
6308 return FAIL;
6309 }
6310 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006311 if (*p == ']')
6312 // empty second index, push "none"
6313 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6314 else
6315 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006316 }
6317
Bram Moolenaar752fc692021-01-04 21:57:11 +01006318 if (r == OK && *skipwhite(p) != ']')
6319 {
6320 // this should not happen
6321 emsg(_(e_missbrac));
6322 r = FAIL;
6323 }
6324 }
6325 else // if (*p == '.')
6326 {
6327 char_u *key_end = to_name_end(p + 1, TRUE);
6328 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6329
6330 r = generate_PUSHS(cctx, key);
6331 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006332 return r;
6333}
6334
6335/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006336 * For a LHS with an index, load the variable to be indexed.
6337 */
6338 static int
6339compile_load_lhs(
6340 lhs_T *lhs,
6341 char_u *var_start,
6342 type_T *rhs_type,
6343 cctx_T *cctx)
6344{
6345 if (lhs->lhs_dest == dest_expr)
6346 {
6347 size_t varlen = lhs->lhs_varlen;
6348 int c = var_start[varlen];
6349 char_u *p = var_start;
6350 garray_T *stack = &cctx->ctx_type_stack;
6351
6352 // Evaluate "ll[expr]" of "ll[expr][idx]"
6353 var_start[varlen] = NUL;
6354 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6355 {
6356 // this should not happen
6357 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006358 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006359 return FAIL;
6360 }
6361 var_start[varlen] = c;
6362
6363 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6364 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6365 // now we can properly check the type
6366 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6367 && rhs_type != &t_void
6368 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6369 FALSE, FALSE) == FAIL)
6370 return FAIL;
6371 }
6372 else
6373 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6374 lhs->lhs_lvar, lhs->lhs_type);
6375 return OK;
6376}
6377
6378/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006379 * Produce code for loading "lhs" and also take care of an index.
6380 * Return OK/FAIL.
6381 */
6382 static int
6383compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6384{
6385 compile_load_lhs(lhs, var_start, NULL, cctx);
6386
6387 if (lhs->lhs_has_index)
6388 {
6389 int range = FALSE;
6390
6391 // Get member from list or dict. First compile the
6392 // index value.
6393 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6394 return FAIL;
6395 if (range)
6396 {
6397 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6398 var_start);
6399 return FAIL;
6400 }
6401
6402 // Get the member.
6403 if (compile_member(FALSE, cctx) == FAIL)
6404 return FAIL;
6405 }
6406 return OK;
6407}
6408
6409/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006410 * Assignment to a list or dict member, or ":unlet" for the item, using the
6411 * information in "lhs".
6412 * Returns OK or FAIL.
6413 */
6414 static int
6415compile_assign_unlet(
6416 char_u *var_start,
6417 lhs_T *lhs,
6418 int is_assign,
6419 type_T *rhs_type,
6420 cctx_T *cctx)
6421{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006422 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006423 garray_T *stack = &cctx->ctx_type_stack;
6424 int range = FALSE;
6425
Bram Moolenaar68452172021-04-12 21:21:02 +02006426 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006427 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006428 if (is_assign && range && lhs->lhs_type != &t_blob
6429 && lhs->lhs_type != &t_any)
6430 {
6431 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6432 return FAIL;
6433 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006434
6435 if (lhs->lhs_type == &t_any)
6436 {
6437 // Index on variable of unknown type: check at runtime.
6438 dest_type = VAR_ANY;
6439 }
6440 else
6441 {
6442 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006443 if (dest_type == VAR_DICT && range)
6444 {
6445 emsg(e_cannot_use_range_with_dictionary);
6446 return FAIL;
6447 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006448 if (dest_type == VAR_DICT
6449 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006450 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006451 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006452 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006453 type_T *type;
6454
6455 if (range)
6456 {
6457 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6458 if (need_type(type, &t_number,
6459 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006460 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006461 }
6462 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6463 if ((dest_type != VAR_BLOB || type != &t_special)
6464 && need_type(type, &t_number,
6465 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006466 return FAIL;
6467 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006468 }
6469
6470 // Load the dict or list. On the stack we then have:
6471 // - value (for assignment, not for :unlet)
6472 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006473 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006474 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006475 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6476 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006477
Bram Moolenaar68452172021-04-12 21:21:02 +02006478 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6479 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006480 {
6481 if (is_assign)
6482 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006483 if (range)
6484 {
6485 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6486 return FAIL;
6487 }
6488 else
6489 {
6490 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006491
Bram Moolenaar68452172021-04-12 21:21:02 +02006492 if (isn == NULL)
6493 return FAIL;
6494 isn->isn_arg.vartype = dest_type;
6495 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006496 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006497 else if (range)
6498 {
6499 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6500 return FAIL;
6501 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006502 else
6503 {
6504 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6505 return FAIL;
6506 }
6507 }
6508 else
6509 {
6510 emsg(_(e_indexable_type_required));
6511 return FAIL;
6512 }
6513
6514 return OK;
6515}
6516
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006517/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006518 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006519 * "let name"
6520 * "var name = expr"
6521 * "final name = expr"
6522 * "const name = expr"
6523 * "name = expr"
6524 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006525 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006526 * Return NULL for an error.
6527 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 */
6529 static char_u *
6530compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6531{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006532 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006534 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006535 char_u *ret = NULL;
6536 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006537 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006540 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006541 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 int oplen = 0;
6543 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006544 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006545 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006546 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006547 int is_decl = is_decl_command(cmdidx);
6548 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006549 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006551 // Skip over the "var" or "[var, var]" to get to any "=".
6552 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6553 if (p == NULL)
6554 return *arg == '[' ? arg : NULL;
6555
6556 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006557 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006558 // TODO: should we allow this, and figure out type inference from list
6559 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006560 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006561 return NULL;
6562 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006563 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006565 sp = p;
6566 p = skipwhite(p);
6567 op = p;
6568 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006569
6570 if (var_count > 0 && oplen == 0)
6571 // can be something like "[1, 2]->func()"
6572 return arg;
6573
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006574 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006576 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006577 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006578 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006579 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6580 {
6581 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6582 oplen = 2;
6583 incdec = TRUE;
6584 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 if (heredoc)
6587 {
6588 list_T *l;
6589 listitem_T *li;
6590
6591 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006592 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006593 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006594 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006595 if (l == NULL)
6596 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597
Bram Moolenaar078269b2020-09-21 20:35:55 +02006598 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006600 // Push each line and the create the list.
6601 FOR_ALL_LIST_ITEMS(l, li)
6602 {
6603 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6604 li->li_tv.vval.v_string = NULL;
6605 }
6606 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 list_free(l);
6609 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006610 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006612 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006614 char_u *wp;
6615
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006616 // for "[var, var] = expr" evaluate the expression here, loop over the
6617 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006618 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006619
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006620 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006621 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006622 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006623 if (compile_expr0(&p, cctx) == FAIL)
6624 return NULL;
6625 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006627 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006629 type_T *stacktype;
6630
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006631 stacktype = stack->ga_len == 0 ? &t_void
6632 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006633 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006634 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006635 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006636 goto theend;
6637 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006638 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006639 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006640 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006641 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006642 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6643 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006644 if (stacktype->tt_member != NULL)
6645 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006646 }
6647 }
6648
6649 /*
6650 * Loop over variables in "[var, var] = expr".
6651 * For "var = expr" and "let var: type" this is done only once.
6652 */
6653 if (var_count > 0)
6654 var_start = skipwhite(arg + 1); // skip over the "["
6655 else
6656 var_start = arg;
6657 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6658 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006659 int instr_count = -1;
6660
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006661 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6662 {
6663 // Ignore underscore in "[a, _, b] = list".
6664 if (var_count > 0)
6665 {
6666 var_start = skipwhite(var_start + 2);
6667 continue;
6668 }
6669 emsg(_(e_cannot_use_underscore_here));
6670 goto theend;
6671 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006672 vim_free(lhs.lhs_name);
6673
6674 /*
6675 * Figure out the LHS type and other properties.
6676 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006677 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6678 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006679 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006680 if (!heredoc)
6681 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006682 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006683 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006684 if (oplen > 0 && var_count == 0)
6685 {
6686 // skip over the "=" and the expression
6687 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006688 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006689 }
6690 }
6691 else if (oplen > 0)
6692 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006693 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006694 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006695
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006696 // For "var = expr" evaluate the expression.
6697 if (var_count == 0)
6698 {
6699 int r;
6700
6701 // for "+=", "*=", "..=" etc. first load the current value
6702 if (*op != '=')
6703 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006704 if (compile_load_lhs_with_index(&lhs, var_start,
6705 cctx) == FAIL)
6706 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006707 }
6708
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006709 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006710 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006711 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006712 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006713 r = generate_PUSHNR(cctx, 1);
6714 }
6715 else
6716 {
6717 // Temporarily hide the new local variable here, it is
6718 // not available to this expression.
6719 if (lhs.lhs_new_local)
6720 --cctx->ctx_locals.ga_len;
6721 wp = op + oplen;
6722 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6723 {
6724 if (lhs.lhs_new_local)
6725 ++cctx->ctx_locals.ga_len;
6726 goto theend;
6727 }
6728 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006729 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006730 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006731 if (r == FAIL)
6732 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006733 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006734 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006735 else if (semicolon && var_idx == var_count - 1)
6736 {
6737 // For "[var; var] = expr" get the rest of the list
6738 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6739 goto theend;
6740 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006741 else
6742 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006743 // For "[var, var] = expr" get the "var_idx" item from the
6744 // list.
6745 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006746 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006747 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006748
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006749 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006750 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006751 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006752 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006753 if ((rhs_type->tt_type == VAR_FUNC
6754 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006755 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006756 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006757 goto theend;
6758
Bram Moolenaar752fc692021-01-04 21:57:11 +01006759 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006760 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006761 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006762 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006763 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006764 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006765 }
6766 else
6767 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006768 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006769 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006770 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006771 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006772 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006773 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006774 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006775 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006776 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006777 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006778 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006779 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006780 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006781 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006782 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006783
Bram Moolenaar77709b12021-04-03 21:01:01 +02006784 // Without operator check type here, otherwise below.
6785 // Use the line number of the assignment.
6786 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006787 if (lhs.lhs_has_index)
6788 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006789 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006790 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006791 goto theend;
6792 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006793 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006794 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006795 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006796 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006798 else if (cmdidx == CMD_final)
6799 {
6800 emsg(_(e_final_requires_a_value));
6801 goto theend;
6802 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006803 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006805 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006806 goto theend;
6807 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006808 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006809 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006810 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006811 goto theend;
6812 }
6813 else
6814 {
6815 // variables are always initialized
6816 if (ga_grow(instr, 1) == FAIL)
6817 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006818 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006819 {
6820 case VAR_BOOL:
6821 generate_PUSHBOOL(cctx, VVAL_FALSE);
6822 break;
6823 case VAR_FLOAT:
6824#ifdef FEAT_FLOAT
6825 generate_PUSHF(cctx, 0.0);
6826#endif
6827 break;
6828 case VAR_STRING:
6829 generate_PUSHS(cctx, NULL);
6830 break;
6831 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006832 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006833 break;
6834 case VAR_FUNC:
6835 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6836 break;
6837 case VAR_LIST:
6838 generate_NEWLIST(cctx, 0);
6839 break;
6840 case VAR_DICT:
6841 generate_NEWDICT(cctx, 0);
6842 break;
6843 case VAR_JOB:
6844 generate_PUSHJOB(cctx, NULL);
6845 break;
6846 case VAR_CHANNEL:
6847 generate_PUSHCHANNEL(cctx, NULL);
6848 break;
6849 case VAR_NUMBER:
6850 case VAR_UNKNOWN:
6851 case VAR_ANY:
6852 case VAR_PARTIAL:
6853 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006854 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006855 case VAR_SPECIAL: // cannot happen
6856 generate_PUSHNR(cctx, 0);
6857 break;
6858 }
6859 }
6860 if (var_count == 0)
6861 end = p;
6862 }
6863
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006864 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006865 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006866 break;
6867
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006868 if (oplen > 0 && *op != '=')
6869 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006870 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006871 type_T *stacktype;
6872
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006873 if (*op == '.')
6874 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006875 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006876 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006877 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006878 if (
6879#ifdef FEAT_FLOAT
6880 // If variable is float operation with number is OK.
6881 !(expected == &t_float && stacktype == &t_number) &&
6882#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006883 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006884 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006885 goto theend;
6886
6887 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006888 {
6889 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6890 goto theend;
6891 }
6892 else if (*op == '+')
6893 {
6894 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006895 operator_type(lhs.lhs_member_type, stacktype),
6896 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006897 goto theend;
6898 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006899 else if (generate_two_op(cctx, op) == FAIL)
6900 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006902
Bram Moolenaar752fc692021-01-04 21:57:11 +01006903 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006904 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006905 // Use the info in "lhs" to store the value at the index in the
6906 // list or dict.
6907 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6908 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006909 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006910 }
6911 else
6912 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006913 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006914 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006915 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006916 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006917 generate_LOCKCONST(cctx);
6918
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006919 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006920 && (lhs.lhs_type->tt_type == VAR_DICT
6921 || lhs.lhs_type->tt_type == VAR_LIST)
6922 && lhs.lhs_type->tt_member != NULL
6923 && lhs.lhs_type->tt_member != &t_any
6924 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006925 // Set the type in the list or dict, so that it can be checked,
6926 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006927 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006928
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006929 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6930 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006931 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006932
6933 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006934 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006936
6937 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006938 if (var_count > 0 && !semicolon)
6939 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006940 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006941 goto theend;
6942 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006943
Bram Moolenaarb2097502020-07-19 17:17:02 +02006944 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945
6946theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006947 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 return ret;
6949}
6950
6951/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006952 * Check for an assignment at "eap->cmd", compile it if found.
6953 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6954 */
6955 static int
6956may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6957{
6958 char_u *pskip;
6959 char_u *p;
6960
6961 // Assuming the command starts with a variable or function name,
6962 // find what follows.
6963 // Skip over "var.member", "var[idx]" and the like.
6964 // Also "&opt = val", "$ENV = val" and "@r = val".
6965 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6966 ? eap->cmd + 1 : eap->cmd;
6967 p = to_name_end(pskip, TRUE);
6968 if (p > eap->cmd && *p != NUL)
6969 {
6970 char_u *var_end;
6971 int oplen;
6972 int heredoc;
6973
6974 if (eap->cmd[0] == '@')
6975 var_end = eap->cmd + 2;
6976 else
6977 var_end = find_name_end(pskip, NULL, NULL,
6978 FNE_CHECK_START | FNE_INCL_BR);
6979 oplen = assignment_len(skipwhite(var_end), &heredoc);
6980 if (oplen > 0)
6981 {
6982 size_t len = p - eap->cmd;
6983
6984 // Recognize an assignment if we recognize the variable
6985 // name:
6986 // "g:var = expr"
6987 // "local = expr" where "local" is a local var.
6988 // "script = expr" where "script" is a script-local var.
6989 // "import = expr" where "import" is an imported var
6990 // "&opt = expr"
6991 // "$ENV = expr"
6992 // "@r = expr"
6993 if (*eap->cmd == '&'
6994 || *eap->cmd == '$'
6995 || *eap->cmd == '@'
6996 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006997 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006998 {
6999 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7000 if (*line == NULL || *line == eap->cmd)
7001 return FAIL;
7002 return OK;
7003 }
7004 }
7005 }
7006
7007 if (*eap->cmd == '[')
7008 {
7009 // [var, var] = expr
7010 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7011 if (*line == NULL)
7012 return FAIL;
7013 if (*line != eap->cmd)
7014 return OK;
7015 }
7016 return NOTDONE;
7017}
7018
7019/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007020 * Check if "name" can be "unlet".
7021 */
7022 int
7023check_vim9_unlet(char_u *name)
7024{
7025 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7026 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007027 // "unlet s:var" is allowed in legacy script.
7028 if (*name == 's' && !script_is_vim9())
7029 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007030 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007031 return FAIL;
7032 }
7033 return OK;
7034}
7035
7036/*
7037 * Callback passed to ex_unletlock().
7038 */
7039 static int
7040compile_unlet(
7041 lval_T *lvp,
7042 char_u *name_end,
7043 exarg_T *eap,
7044 int deep UNUSED,
7045 void *coookie)
7046{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007047 cctx_T *cctx = coookie;
7048 char_u *p = lvp->ll_name;
7049 int cc = *name_end;
7050 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007051
Bram Moolenaar752fc692021-01-04 21:57:11 +01007052 if (cctx->ctx_skip == SKIP_YES)
7053 return OK;
7054
7055 *name_end = NUL;
7056 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007057 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007058 // :unlet $ENV_VAR
7059 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7060 }
7061 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7062 {
7063 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007064
Bram Moolenaar752fc692021-01-04 21:57:11 +01007065 // This is similar to assigning: lookup the list/dict, compile the
7066 // idx/key. Then instead of storing the value unlet the item.
7067 // unlet {list}[idx]
7068 // unlet {dict}[key] dict.key
7069 //
7070 // Figure out the LHS type and other properties.
7071 //
7072 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7073
7074 // : unlet an indexed item
7075 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007076 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007077 iemsg("called compile_lhs() without an index");
7078 ret = FAIL;
7079 }
7080 else
7081 {
7082 // Use the info in "lhs" to unlet the item at the index in the
7083 // list or dict.
7084 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007085 }
7086
Bram Moolenaar752fc692021-01-04 21:57:11 +01007087 vim_free(lhs.lhs_name);
7088 }
7089 else if (check_vim9_unlet(p) == FAIL)
7090 {
7091 ret = FAIL;
7092 }
7093 else
7094 {
7095 // Normal name. Only supports g:, w:, t: and b: namespaces.
7096 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007097 }
7098
Bram Moolenaar752fc692021-01-04 21:57:11 +01007099 *name_end = cc;
7100 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007101}
7102
7103/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007104 * Callback passed to ex_unletlock().
7105 */
7106 static int
7107compile_lock_unlock(
7108 lval_T *lvp,
7109 char_u *name_end,
7110 exarg_T *eap,
7111 int deep UNUSED,
7112 void *coookie)
7113{
7114 cctx_T *cctx = coookie;
7115 int cc = *name_end;
7116 char_u *p = lvp->ll_name;
7117 int ret = OK;
7118 size_t len;
7119 char_u *buf;
7120
7121 if (cctx->ctx_skip == SKIP_YES)
7122 return OK;
7123
7124 // Cannot use :lockvar and :unlockvar on local variables.
7125 if (p[1] != ':')
7126 {
7127 char_u *end = skip_var_one(p, FALSE);
7128
7129 if (lookup_local(p, end - p, NULL, cctx) == OK)
7130 {
7131 emsg(_(e_cannot_lock_unlock_local_variable));
7132 return FAIL;
7133 }
7134 }
7135
7136 // Checking is done at runtime.
7137 *name_end = NUL;
7138 len = name_end - p + 20;
7139 buf = alloc(len);
7140 if (buf == NULL)
7141 ret = FAIL;
7142 else
7143 {
7144 vim_snprintf((char *)buf, len, "%s %s",
7145 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7146 p);
7147 ret = generate_EXEC(cctx, buf);
7148
7149 vim_free(buf);
7150 *name_end = cc;
7151 }
7152 return ret;
7153}
7154
7155/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007156 * compile "unlet var", "lock var" and "unlock var"
7157 * "arg" points to "var".
7158 */
7159 static char_u *
7160compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7161{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007162 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7163 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7164 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007165 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7166}
7167
7168/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169 * Compile an :import command.
7170 */
7171 static char_u *
7172compile_import(char_u *arg, cctx_T *cctx)
7173{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007174 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175}
7176
7177/*
7178 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7179 */
7180 static int
7181compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7182{
7183 garray_T *instr = &cctx->ctx_instr;
7184 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7185
7186 if (endlabel == NULL)
7187 return FAIL;
7188 endlabel->el_next = *el;
7189 *el = endlabel;
7190 endlabel->el_end_label = instr->ga_len;
7191
7192 generate_JUMP(cctx, when, 0);
7193 return OK;
7194}
7195
7196 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007197compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007198{
7199 garray_T *instr = &cctx->ctx_instr;
7200
7201 while (*el != NULL)
7202 {
7203 endlabel_T *cur = (*el);
7204 isn_T *isn;
7205
7206 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007207 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007208 *el = cur->el_next;
7209 vim_free(cur);
7210 }
7211}
7212
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007213 static void
7214compile_free_jump_to_end(endlabel_T **el)
7215{
7216 while (*el != NULL)
7217 {
7218 endlabel_T *cur = (*el);
7219
7220 *el = cur->el_next;
7221 vim_free(cur);
7222 }
7223}
7224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225/*
7226 * Create a new scope and set up the generic items.
7227 */
7228 static scope_T *
7229new_scope(cctx_T *cctx, scopetype_T type)
7230{
7231 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7232
7233 if (scope == NULL)
7234 return NULL;
7235 scope->se_outer = cctx->ctx_scope;
7236 cctx->ctx_scope = scope;
7237 scope->se_type = type;
7238 scope->se_local_count = cctx->ctx_locals.ga_len;
7239 return scope;
7240}
7241
7242/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007243 * Free the current scope and go back to the outer scope.
7244 */
7245 static void
7246drop_scope(cctx_T *cctx)
7247{
7248 scope_T *scope = cctx->ctx_scope;
7249
7250 if (scope == NULL)
7251 {
7252 iemsg("calling drop_scope() without a scope");
7253 return;
7254 }
7255 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007256 switch (scope->se_type)
7257 {
7258 case IF_SCOPE:
7259 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7260 case FOR_SCOPE:
7261 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7262 case WHILE_SCOPE:
7263 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7264 case TRY_SCOPE:
7265 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7266 case NO_SCOPE:
7267 case BLOCK_SCOPE:
7268 break;
7269 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007270 vim_free(scope);
7271}
7272
7273/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 * compile "if expr"
7275 *
7276 * "if expr" Produces instructions:
7277 * EVAL expr Push result of "expr"
7278 * JUMP_IF_FALSE end
7279 * ... body ...
7280 * end:
7281 *
7282 * "if expr | else" Produces instructions:
7283 * EVAL expr Push result of "expr"
7284 * JUMP_IF_FALSE else
7285 * ... body ...
7286 * JUMP_ALWAYS end
7287 * else:
7288 * ... body ...
7289 * end:
7290 *
7291 * "if expr1 | elseif expr2 | else" Produces instructions:
7292 * EVAL expr Push result of "expr"
7293 * JUMP_IF_FALSE elseif
7294 * ... body ...
7295 * JUMP_ALWAYS end
7296 * elseif:
7297 * EVAL expr Push result of "expr"
7298 * JUMP_IF_FALSE else
7299 * ... body ...
7300 * JUMP_ALWAYS end
7301 * else:
7302 * ... body ...
7303 * end:
7304 */
7305 static char_u *
7306compile_if(char_u *arg, cctx_T *cctx)
7307{
7308 char_u *p = arg;
7309 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007310 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007312 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007313 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314
Bram Moolenaara5565e42020-05-09 15:44:01 +02007315 CLEAR_FIELD(ppconst);
7316 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007317 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007318 clear_ppconst(&ppconst);
7319 return NULL;
7320 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007321 if (!ends_excmd2(arg, skipwhite(p)))
7322 {
7323 semsg(_(e_trailing_arg), p);
7324 return NULL;
7325 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007326 if (cctx->ctx_skip == SKIP_YES)
7327 clear_ppconst(&ppconst);
7328 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007329 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007330 int error = FALSE;
7331 int v;
7332
Bram Moolenaara5565e42020-05-09 15:44:01 +02007333 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007334 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007335 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007336 if (error)
7337 return NULL;
7338 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007339 }
7340 else
7341 {
7342 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007343 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007344 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007345 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007346 if (bool_on_stack(cctx) == FAIL)
7347 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349
Bram Moolenaara91a7132021-03-25 21:12:15 +01007350 // CMDMOD_REV must come before the jump
7351 generate_undo_cmdmods(cctx);
7352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 scope = new_scope(cctx, IF_SCOPE);
7354 if (scope == NULL)
7355 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007356 scope->se_skip_save = skip_save;
7357 // "is_had_return" will be reset if any block does not end in :return
7358 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007359
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007360 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007361 {
7362 // "where" is set when ":elseif", "else" or ":endif" is found
7363 scope->se_u.se_if.is_if_label = instr->ga_len;
7364 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7365 }
7366 else
7367 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368
Bram Moolenaarced68a02021-01-24 17:53:47 +01007369#ifdef FEAT_PROFILE
7370 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7371 && skip_save != SKIP_YES)
7372 {
7373 // generated a profile start, need to generate a profile end, since it
7374 // won't be done after returning
7375 cctx->ctx_skip = SKIP_NOT;
7376 generate_instr(cctx, ISN_PROF_END);
7377 cctx->ctx_skip = SKIP_YES;
7378 }
7379#endif
7380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 return p;
7382}
7383
7384 static char_u *
7385compile_elseif(char_u *arg, cctx_T *cctx)
7386{
7387 char_u *p = arg;
7388 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007389 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 isn_T *isn;
7391 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007392 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007393 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394
7395 if (scope == NULL || scope->se_type != IF_SCOPE)
7396 {
7397 emsg(_(e_elseif_without_if));
7398 return NULL;
7399 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007400 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007401 if (!cctx->ctx_had_return)
7402 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403
Bram Moolenaarced68a02021-01-24 17:53:47 +01007404 if (cctx->ctx_skip == SKIP_NOT)
7405 {
7406 // previous block was executed, this one and following will not
7407 cctx->ctx_skip = SKIP_YES;
7408 scope->se_u.se_if.is_seen_skip_not = TRUE;
7409 }
7410 if (scope->se_u.se_if.is_seen_skip_not)
7411 {
7412 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007413 // Do not count the "elseif" for profiling and cmdmod
7414 instr->ga_len = current_instr_idx(cctx);
7415
Bram Moolenaarced68a02021-01-24 17:53:47 +01007416 skip_expr_cctx(&p, cctx);
7417 return p;
7418 }
7419
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007420 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007421 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007422 int moved_cmdmod = FALSE;
7423
7424 // Move any CMDMOD instruction to after the jump
7425 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7426 {
7427 if (ga_grow(instr, 1) == FAIL)
7428 return NULL;
7429 ((isn_T *)instr->ga_data)[instr->ga_len] =
7430 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7431 --instr->ga_len;
7432 moved_cmdmod = TRUE;
7433 }
7434
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007435 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007436 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007437 return NULL;
7438 // previous "if" or "elseif" jumps here
7439 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7440 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007441 if (moved_cmdmod)
7442 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007445 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007446 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007447 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007448 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007449 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007450#ifdef FEAT_PROFILE
7451 if (cctx->ctx_profiling)
7452 {
7453 // the previous block was skipped, need to profile this line
7454 generate_instr(cctx, ISN_PROF_START);
7455 instr_count = instr->ga_len;
7456 }
7457#endif
7458 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007459 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007460 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007461 clear_ppconst(&ppconst);
7462 return NULL;
7463 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007464 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007465 if (!ends_excmd2(arg, skipwhite(p)))
7466 {
7467 semsg(_(e_trailing_arg), p);
7468 return NULL;
7469 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007470 if (scope->se_skip_save == SKIP_YES)
7471 clear_ppconst(&ppconst);
7472 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007473 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007474 int error = FALSE;
7475 int v;
7476
Bram Moolenaar7f141552020-05-09 17:35:53 +02007477 // The expression results in a constant.
7478 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007479 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7480 if (error)
7481 return NULL;
7482 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007483 clear_ppconst(&ppconst);
7484 scope->se_u.se_if.is_if_label = -1;
7485 }
7486 else
7487 {
7488 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007489 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007490 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007491 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007492 if (bool_on_stack(cctx) == FAIL)
7493 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494
Bram Moolenaara91a7132021-03-25 21:12:15 +01007495 // CMDMOD_REV must come before the jump
7496 generate_undo_cmdmods(cctx);
7497
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007498 // "where" is set when ":elseif", "else" or ":endif" is found
7499 scope->se_u.se_if.is_if_label = instr->ga_len;
7500 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7501 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502
7503 return p;
7504}
7505
7506 static char_u *
7507compile_else(char_u *arg, cctx_T *cctx)
7508{
7509 char_u *p = arg;
7510 garray_T *instr = &cctx->ctx_instr;
7511 isn_T *isn;
7512 scope_T *scope = cctx->ctx_scope;
7513
7514 if (scope == NULL || scope->se_type != IF_SCOPE)
7515 {
7516 emsg(_(e_else_without_if));
7517 return NULL;
7518 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007519 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007520 if (!cctx->ctx_had_return)
7521 scope->se_u.se_if.is_had_return = FALSE;
7522 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007523
Bram Moolenaarced68a02021-01-24 17:53:47 +01007524#ifdef FEAT_PROFILE
7525 if (cctx->ctx_profiling)
7526 {
7527 if (cctx->ctx_skip == SKIP_NOT
7528 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7529 .isn_type == ISN_PROF_START)
7530 // the previous block was executed, do not count "else" for profiling
7531 --instr->ga_len;
7532 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7533 {
7534 // the previous block was not executed, this one will, do count the
7535 // "else" for profiling
7536 cctx->ctx_skip = SKIP_NOT;
7537 generate_instr(cctx, ISN_PROF_END);
7538 generate_instr(cctx, ISN_PROF_START);
7539 cctx->ctx_skip = SKIP_YES;
7540 }
7541 }
7542#endif
7543
7544 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007545 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007546 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007547 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007548 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007549 if (!cctx->ctx_had_return
7550 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7551 JUMP_ALWAYS, cctx) == FAIL)
7552 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007553 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007554
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007555 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007556 {
7557 if (scope->se_u.se_if.is_if_label >= 0)
7558 {
7559 // previous "if" or "elseif" jumps here
7560 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7561 isn->isn_arg.jump.jump_where = instr->ga_len;
7562 scope->se_u.se_if.is_if_label = -1;
7563 }
7564 }
7565
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007566 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007567 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007569
7570 return p;
7571}
7572
7573 static char_u *
7574compile_endif(char_u *arg, cctx_T *cctx)
7575{
7576 scope_T *scope = cctx->ctx_scope;
7577 ifscope_T *ifscope;
7578 garray_T *instr = &cctx->ctx_instr;
7579 isn_T *isn;
7580
Bram Moolenaarfa984412021-03-25 22:15:28 +01007581 if (misplaced_cmdmod(cctx))
7582 return NULL;
7583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007584 if (scope == NULL || scope->se_type != IF_SCOPE)
7585 {
7586 emsg(_(e_endif_without_if));
7587 return NULL;
7588 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007589 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007590 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007591 if (!cctx->ctx_had_return)
7592 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007593
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007594 if (scope->se_u.se_if.is_if_label >= 0)
7595 {
7596 // previous "if" or "elseif" jumps here
7597 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7598 isn->isn_arg.jump.jump_where = instr->ga_len;
7599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007600 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007601 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007602
7603#ifdef FEAT_PROFILE
7604 // even when skipping we count the endif as executed, unless the block it's
7605 // in is skipped
7606 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7607 && scope->se_skip_save != SKIP_YES)
7608 {
7609 cctx->ctx_skip = SKIP_NOT;
7610 generate_instr(cctx, ISN_PROF_START);
7611 }
7612#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007613 cctx->ctx_skip = scope->se_skip_save;
7614
7615 // If all the blocks end in :return and there is an :else then the
7616 // had_return flag is set.
7617 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007618
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007619 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007620 return arg;
7621}
7622
7623/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007624 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007625 *
7626 * Produces instructions:
7627 * PUSHNR -1
7628 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007629 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007630 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7631 * - if beyond end, jump to "end"
7632 * - otherwise get item from list and push it
7633 * STORE var Store item in "var"
7634 * ... body ...
7635 * JUMP top Jump back to repeat
7636 * end: DROP Drop the result of "expr"
7637 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007638 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7639 * UNPACK 2 Split item in 2
7640 * STORE var1 Store item in "var1"
7641 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 */
7643 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007644compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007646 char_u *arg;
7647 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007648 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007650 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007651 int var_count = 0;
7652 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007653 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654 garray_T *stack = &cctx->ctx_type_stack;
7655 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007656 lvar_T *loop_lvar; // loop iteration variable
7657 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007658 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007659 type_T *item_type = &t_any;
7660 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007661
Bram Moolenaar792f7862020-11-23 08:31:18 +01007662 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007663 if (p == NULL)
7664 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007665 if (var_count == 0)
7666 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667
7668 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007669 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007670 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7671 return NULL;
7672 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007673 {
7674 emsg(_(e_missing_in));
7675 return NULL;
7676 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007677 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007678 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7679 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681 scope = new_scope(cctx, FOR_SCOPE);
7682 if (scope == NULL)
7683 return NULL;
7684
Bram Moolenaar792f7862020-11-23 08:31:18 +01007685 // Reserve a variable to store the loop iteration counter and initialize it
7686 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007687 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7688 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007689 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007690 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007691 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007692 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007693 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007694 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007695
7696 // compile "expr", it remains on the stack until "endfor"
7697 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007698 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007699 {
7700 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007701 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007702 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007703 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007705 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007706 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007707 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007708 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007709 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007710 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007711 semsg(_(e_for_loop_on_str_not_supported),
7712 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007713 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 return NULL;
7715 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007716
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007717 if (vartype->tt_type == VAR_STRING)
7718 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007719 else if (vartype->tt_type == VAR_BLOB)
7720 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007721 else if (vartype->tt_type == VAR_LIST
7722 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007723 {
7724 if (var_count == 1)
7725 item_type = vartype->tt_member;
7726 else if (vartype->tt_member->tt_type == VAR_LIST
7727 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007728 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007729 item_type = vartype->tt_member->tt_member;
7730 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731
Bram Moolenaara91a7132021-03-25 21:12:15 +01007732 // CMDMOD_REV must come before the FOR instruction
7733 generate_undo_cmdmods(cctx);
7734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007736 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007737 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007738
Bram Moolenaar792f7862020-11-23 08:31:18 +01007739 arg = arg_start;
7740 if (var_count > 1)
7741 {
7742 generate_UNPACK(cctx, var_count, semicolon);
7743 arg = skipwhite(arg + 1); // skip white after '['
7744
7745 // the list item is replaced by a number of items
7746 if (ga_grow(stack, var_count - 1) == FAIL)
7747 {
7748 drop_scope(cctx);
7749 return NULL;
7750 }
7751 --stack->ga_len;
7752 for (idx = 0; idx < var_count; ++idx)
7753 {
7754 ((type_T **)stack->ga_data)[stack->ga_len] =
7755 (semicolon && idx == 0) ? vartype : item_type;
7756 ++stack->ga_len;
7757 }
7758 }
7759
7760 for (idx = 0; idx < var_count; ++idx)
7761 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007762 assign_dest_T dest = dest_local;
7763 int opt_flags = 0;
7764 int vimvaridx = -1;
7765 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007766 type_T *lhs_type = &t_any;
7767 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007768
7769 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007770 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007771 name = vim_strnsave(arg, varlen);
7772 if (name == NULL)
7773 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007774 if (*p == ':')
7775 {
7776 p = skipwhite(p + 1);
7777 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7778 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007779
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007780 // TODO: script var not supported?
7781 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7782 &vimvaridx, &type, cctx) == FAIL)
7783 goto failed;
7784 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007785 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007786 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7787 0, 0, type, name) == FAIL)
7788 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007789 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007790 else if (varlen == 1 && *arg == '_')
7791 {
7792 // Assigning to "_": drop the value.
7793 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7794 goto failed;
7795 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007796 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007797 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007798 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007799 {
7800 semsg(_(e_variable_already_declared), arg);
7801 goto failed;
7802 }
7803
Bram Moolenaarea870692020-12-02 14:24:30 +01007804 if (STRNCMP(name, "s:", 2) == 0)
7805 {
7806 semsg(_(e_cannot_declare_script_variable_in_function), name);
7807 goto failed;
7808 }
7809
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007810 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007811 where.wt_index = var_count > 1 ? idx + 1 : 0;
7812 where.wt_variable = TRUE;
7813 if (lhs_type == &t_any)
7814 lhs_type = item_type;
7815 else if (item_type != &t_unknown
7816 && !(var_count > 1 && item_type == &t_any)
7817 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7818 goto failed;
7819 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007820 if (var_lvar == NULL)
7821 // out of memory or used as an argument
7822 goto failed;
7823
7824 if (semicolon && idx == var_count - 1)
7825 var_lvar->lv_type = vartype;
7826 else
7827 var_lvar->lv_type = item_type;
7828 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7829 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007830
7831 if (*p == ',' || *p == ';')
7832 ++p;
7833 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007834 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007835 }
7836
7837 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007838
7839failed:
7840 vim_free(name);
7841 drop_scope(cctx);
7842 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007843}
7844
7845/*
7846 * compile "endfor"
7847 */
7848 static char_u *
7849compile_endfor(char_u *arg, cctx_T *cctx)
7850{
7851 garray_T *instr = &cctx->ctx_instr;
7852 scope_T *scope = cctx->ctx_scope;
7853 forscope_T *forscope;
7854 isn_T *isn;
7855
Bram Moolenaarfa984412021-03-25 22:15:28 +01007856 if (misplaced_cmdmod(cctx))
7857 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007859 if (scope == NULL || scope->se_type != FOR_SCOPE)
7860 {
7861 emsg(_(e_for));
7862 return NULL;
7863 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007864 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007865 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007866 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007867
7868 // At end of ":for" scope jump back to the FOR instruction.
7869 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7870
7871 // Fill in the "end" label in the FOR statement so it can jump here
7872 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7873 isn->isn_arg.forloop.for_end = instr->ga_len;
7874
7875 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007876 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007877
7878 // Below the ":for" scope drop the "expr" list from the stack.
7879 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7880 return NULL;
7881
7882 vim_free(scope);
7883
7884 return arg;
7885}
7886
7887/*
7888 * compile "while expr"
7889 *
7890 * Produces instructions:
7891 * top: EVAL expr Push result of "expr"
7892 * JUMP_IF_FALSE end jump if false
7893 * ... body ...
7894 * JUMP top Jump back to repeat
7895 * end:
7896 *
7897 */
7898 static char_u *
7899compile_while(char_u *arg, cctx_T *cctx)
7900{
7901 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007902 scope_T *scope;
7903
7904 scope = new_scope(cctx, WHILE_SCOPE);
7905 if (scope == NULL)
7906 return NULL;
7907
Bram Moolenaara91a7132021-03-25 21:12:15 +01007908 // "endwhile" jumps back here, one before when profiling or using cmdmods
7909 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007910
7911 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007912 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007913 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007914 if (!ends_excmd2(arg, skipwhite(p)))
7915 {
7916 semsg(_(e_trailing_arg), p);
7917 return NULL;
7918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007919
Bram Moolenaar13106602020-10-04 16:06:05 +02007920 if (bool_on_stack(cctx) == FAIL)
7921 return FAIL;
7922
Bram Moolenaara91a7132021-03-25 21:12:15 +01007923 // CMDMOD_REV must come before the jump
7924 generate_undo_cmdmods(cctx);
7925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007927 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007928 JUMP_IF_FALSE, cctx) == FAIL)
7929 return FAIL;
7930
7931 return p;
7932}
7933
7934/*
7935 * compile "endwhile"
7936 */
7937 static char_u *
7938compile_endwhile(char_u *arg, cctx_T *cctx)
7939{
7940 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007941 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007942
Bram Moolenaarfa984412021-03-25 22:15:28 +01007943 if (misplaced_cmdmod(cctx))
7944 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007945 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7946 {
7947 emsg(_(e_while));
7948 return NULL;
7949 }
7950 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007951 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007952
Bram Moolenaarf002a412021-01-24 13:34:18 +01007953#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007954 // count the endwhile before jumping
7955 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007956#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007958 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007959 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007960
7961 // Fill in the "end" label in the WHILE statement so it can jump here.
7962 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007963 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7964 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007965
7966 vim_free(scope);
7967
7968 return arg;
7969}
7970
7971/*
7972 * compile "continue"
7973 */
7974 static char_u *
7975compile_continue(char_u *arg, cctx_T *cctx)
7976{
7977 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007978 int try_scopes = 0;
7979 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007980
7981 for (;;)
7982 {
7983 if (scope == NULL)
7984 {
7985 emsg(_(e_continue));
7986 return NULL;
7987 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007988 if (scope->se_type == FOR_SCOPE)
7989 {
7990 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007991 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007992 }
7993 if (scope->se_type == WHILE_SCOPE)
7994 {
7995 loop_label = scope->se_u.se_while.ws_top_label;
7996 break;
7997 }
7998 if (scope->se_type == TRY_SCOPE)
7999 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008000 scope = scope->se_outer;
8001 }
8002
Bram Moolenaarc150c092021-02-13 15:02:46 +01008003 if (try_scopes > 0)
8004 // Inside one or more try/catch blocks we first need to jump to the
8005 // "finally" or "endtry" to cleanup.
8006 generate_TRYCONT(cctx, try_scopes, loop_label);
8007 else
8008 // Jump back to the FOR or WHILE instruction.
8009 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011 return arg;
8012}
8013
8014/*
8015 * compile "break"
8016 */
8017 static char_u *
8018compile_break(char_u *arg, cctx_T *cctx)
8019{
8020 scope_T *scope = cctx->ctx_scope;
8021 endlabel_T **el;
8022
8023 for (;;)
8024 {
8025 if (scope == NULL)
8026 {
8027 emsg(_(e_break));
8028 return NULL;
8029 }
8030 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8031 break;
8032 scope = scope->se_outer;
8033 }
8034
8035 // Jump to the end of the FOR or WHILE loop.
8036 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008037 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008038 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008039 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008040 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8041 return FAIL;
8042
8043 return arg;
8044}
8045
8046/*
8047 * compile "{" start of block
8048 */
8049 static char_u *
8050compile_block(char_u *arg, cctx_T *cctx)
8051{
8052 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8053 return NULL;
8054 return skipwhite(arg + 1);
8055}
8056
8057/*
8058 * compile end of block: drop one scope
8059 */
8060 static void
8061compile_endblock(cctx_T *cctx)
8062{
8063 scope_T *scope = cctx->ctx_scope;
8064
8065 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008066 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008067 vim_free(scope);
8068}
8069
8070/*
8071 * compile "try"
8072 * Creates a new scope for the try-endtry, pointing to the first catch and
8073 * finally.
8074 * Creates another scope for the "try" block itself.
8075 * TRY instruction sets up exception handling at runtime.
8076 *
8077 * "try"
8078 * TRY -> catch1, -> finally push trystack entry
8079 * ... try block
8080 * "throw {exception}"
8081 * EVAL {exception}
8082 * THROW create exception
8083 * ... try block
8084 * " catch {expr}"
8085 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008086 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008087 * EVAL {expr}
8088 * MATCH
8089 * JUMP nomatch -> catch2
8090 * CATCH remove exception
8091 * ... catch block
8092 * " catch"
8093 * JUMP -> finally
8094 * catch2: CATCH remove exception
8095 * ... catch block
8096 * " finally"
8097 * finally:
8098 * ... finally block
8099 * " endtry"
8100 * ENDTRY pop trystack entry, may rethrow
8101 */
8102 static char_u *
8103compile_try(char_u *arg, cctx_T *cctx)
8104{
8105 garray_T *instr = &cctx->ctx_instr;
8106 scope_T *try_scope;
8107 scope_T *scope;
8108
Bram Moolenaarfa984412021-03-25 22:15:28 +01008109 if (misplaced_cmdmod(cctx))
8110 return NULL;
8111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008112 // scope that holds the jumps that go to catch/finally/endtry
8113 try_scope = new_scope(cctx, TRY_SCOPE);
8114 if (try_scope == NULL)
8115 return NULL;
8116
Bram Moolenaar69f70502021-01-01 16:10:46 +01008117 if (cctx->ctx_skip != SKIP_YES)
8118 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008119 isn_T *isn;
8120
8121 // "try_catch" is set when the first ":catch" is found or when no catch
8122 // is found and ":finally" is found.
8123 // "try_finally" is set when ":finally" is found
8124 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008125 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008126 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8127 return NULL;
8128 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8129 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008130 return NULL;
8131 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008132
8133 // scope for the try block itself
8134 scope = new_scope(cctx, BLOCK_SCOPE);
8135 if (scope == NULL)
8136 return NULL;
8137
8138 return arg;
8139}
8140
8141/*
8142 * compile "catch {expr}"
8143 */
8144 static char_u *
8145compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8146{
8147 scope_T *scope = cctx->ctx_scope;
8148 garray_T *instr = &cctx->ctx_instr;
8149 char_u *p;
8150 isn_T *isn;
8151
Bram Moolenaarfa984412021-03-25 22:15:28 +01008152 if (misplaced_cmdmod(cctx))
8153 return NULL;
8154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155 // end block scope from :try or :catch
8156 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8157 compile_endblock(cctx);
8158 scope = cctx->ctx_scope;
8159
8160 // Error if not in a :try scope
8161 if (scope == NULL || scope->se_type != TRY_SCOPE)
8162 {
8163 emsg(_(e_catch));
8164 return NULL;
8165 }
8166
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008167 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008168 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008169 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008170 return NULL;
8171 }
8172
Bram Moolenaar69f70502021-01-01 16:10:46 +01008173 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008174 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008175#ifdef FEAT_PROFILE
8176 // the profile-start should be after the jump
8177 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8178 .isn_type == ISN_PROF_START)
8179 --instr->ga_len;
8180#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008181 // Jump from end of previous block to :finally or :endtry
8182 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8183 JUMP_ALWAYS, cctx) == FAIL)
8184 return NULL;
8185
8186 // End :try or :catch scope: set value in ISN_TRY instruction
8187 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008188 if (isn->isn_arg.try.try_ref->try_catch == 0)
8189 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008190 if (scope->se_u.se_try.ts_catch_label != 0)
8191 {
8192 // Previous catch without match jumps here
8193 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8194 isn->isn_arg.jump.jump_where = instr->ga_len;
8195 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008196#ifdef FEAT_PROFILE
8197 if (cctx->ctx_profiling)
8198 {
8199 // a "throw" that jumps here needs to be counted
8200 generate_instr(cctx, ISN_PROF_END);
8201 // the "catch" is also counted
8202 generate_instr(cctx, ISN_PROF_START);
8203 }
8204#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008205 }
8206
8207 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008208 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008209 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008210 scope->se_u.se_try.ts_caught_all = TRUE;
8211 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008212 }
8213 else
8214 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008215 char_u *end;
8216 char_u *pat;
8217 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008218 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008219 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008221 // Push v:exception, push {expr} and MATCH
8222 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8223
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008224 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008225 if (*end != *p)
8226 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008227 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008228 vim_free(tofree);
8229 return FAIL;
8230 }
8231 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008232 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008233 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008234 len = (int)(end - tofree);
8235 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008236 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008237 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008238 if (pat == NULL)
8239 return FAIL;
8240 if (generate_PUSHS(cctx, pat) == FAIL)
8241 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008242
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008243 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8244 return NULL;
8245
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008246 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008247 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8248 return NULL;
8249 }
8250
Bram Moolenaar69f70502021-01-01 16:10:46 +01008251 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008252 return NULL;
8253
8254 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8255 return NULL;
8256 return p;
8257}
8258
8259 static char_u *
8260compile_finally(char_u *arg, cctx_T *cctx)
8261{
8262 scope_T *scope = cctx->ctx_scope;
8263 garray_T *instr = &cctx->ctx_instr;
8264 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008265 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008266
Bram Moolenaarfa984412021-03-25 22:15:28 +01008267 if (misplaced_cmdmod(cctx))
8268 return NULL;
8269
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008270 // end block scope from :try or :catch
8271 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8272 compile_endblock(cctx);
8273 scope = cctx->ctx_scope;
8274
8275 // Error if not in a :try scope
8276 if (scope == NULL || scope->se_type != TRY_SCOPE)
8277 {
8278 emsg(_(e_finally));
8279 return NULL;
8280 }
8281
8282 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008283 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008284 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008285 {
8286 emsg(_(e_finally_dup));
8287 return NULL;
8288 }
8289
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008290 this_instr = instr->ga_len;
8291#ifdef FEAT_PROFILE
8292 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8293 .isn_type == ISN_PROF_START)
8294 // jump to the profile start of the "finally"
8295 --this_instr;
8296#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008297
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008298 // Fill in the "end" label in jumps at the end of the blocks.
8299 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8300 this_instr, cctx);
8301
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008302 // If there is no :catch then an exception jumps to :finally.
8303 if (isn->isn_arg.try.try_ref->try_catch == 0)
8304 isn->isn_arg.try.try_ref->try_catch = this_instr;
8305 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008306 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008307 {
8308 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008309 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008310 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008311 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008312 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008313 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8314 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008316 // TODO: set index in ts_finally_label jumps
8317
8318 return arg;
8319}
8320
8321 static char_u *
8322compile_endtry(char_u *arg, cctx_T *cctx)
8323{
8324 scope_T *scope = cctx->ctx_scope;
8325 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008326 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008327
Bram Moolenaarfa984412021-03-25 22:15:28 +01008328 if (misplaced_cmdmod(cctx))
8329 return NULL;
8330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008331 // end block scope from :catch or :finally
8332 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8333 compile_endblock(cctx);
8334 scope = cctx->ctx_scope;
8335
8336 // Error if not in a :try scope
8337 if (scope == NULL || scope->se_type != TRY_SCOPE)
8338 {
8339 if (scope == NULL)
8340 emsg(_(e_no_endtry));
8341 else if (scope->se_type == WHILE_SCOPE)
8342 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008343 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008344 emsg(_(e_endfor));
8345 else
8346 emsg(_(e_endif));
8347 return NULL;
8348 }
8349
Bram Moolenaarc150c092021-02-13 15:02:46 +01008350 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008351 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008352 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008353 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8354 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008355 {
8356 emsg(_(e_missing_catch_or_finally));
8357 return NULL;
8358 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008359
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008360#ifdef FEAT_PROFILE
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008361 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8362 .isn_type == ISN_PROF_START)
8363 // move the profile start after "endtry" so that it's not counted when
8364 // the exception is rethrown.
8365 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008366#endif
8367
Bram Moolenaar69f70502021-01-01 16:10:46 +01008368 // Fill in the "end" label in jumps at the end of the blocks, if not
8369 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008370 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8371 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008372
Bram Moolenaar69f70502021-01-01 16:10:46 +01008373 if (scope->se_u.se_try.ts_catch_label != 0)
8374 {
8375 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008376 isn_T *isn = ((isn_T *)instr->ga_data)
8377 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008378 isn->isn_arg.jump.jump_where = instr->ga_len;
8379 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008380 }
8381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008382 compile_endblock(cctx);
8383
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008384 if (cctx->ctx_skip != SKIP_YES)
8385 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008386 // End :catch or :finally scope: set instruction index in ISN_TRY
8387 // instruction
8388 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008389 if (cctx->ctx_skip != SKIP_YES
8390 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8391 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008392#ifdef FEAT_PROFILE
8393 if (cctx->ctx_profiling)
8394 generate_instr(cctx, ISN_PROF_START);
8395#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008397 return arg;
8398}
8399
8400/*
8401 * compile "throw {expr}"
8402 */
8403 static char_u *
8404compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8405{
8406 char_u *p = skipwhite(arg);
8407
Bram Moolenaara5565e42020-05-09 15:44:01 +02008408 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008409 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008410 if (cctx->ctx_skip == SKIP_YES)
8411 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008412 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008413 return NULL;
8414 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8415 return NULL;
8416
8417 return p;
8418}
8419
8420/*
8421 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008422 * compile "echomsg expr"
8423 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008424 * compile "execute expr"
8425 */
8426 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008427compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008428{
8429 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008430 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008431 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008432 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008433 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008434 garray_T *stack = &cctx->ctx_type_stack;
8435 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008436
8437 for (;;)
8438 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008439 if (ends_excmd2(prev, p))
8440 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008441 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008442 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008443 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008444
8445 if (cctx->ctx_skip != SKIP_YES)
8446 {
8447 // check for non-void type
8448 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8449 if (type->tt_type == VAR_VOID)
8450 {
8451 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8452 return NULL;
8453 }
8454 }
8455
Bram Moolenaarad39c092020-02-26 18:23:43 +01008456 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008457 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008458 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008459 }
8460
Bram Moolenaare4984292020-12-13 14:19:25 +01008461 if (count > 0)
8462 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008463 long save_lnum = cctx->ctx_lnum;
8464
8465 // Use the line number where the command started.
8466 cctx->ctx_lnum = start_ctx_lnum;
8467
Bram Moolenaare4984292020-12-13 14:19:25 +01008468 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8469 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8470 else if (cmdidx == CMD_execute)
8471 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8472 else if (cmdidx == CMD_echomsg)
8473 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8474 else
8475 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008476
8477 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008479 return p;
8480}
8481
8482/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008483 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008484 * instruction to compute it and return OK.
8485 * Otherwise return FAIL, the caller must deal with any range.
8486 */
8487 static int
8488compile_variable_range(exarg_T *eap, cctx_T *cctx)
8489{
8490 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8491 char_u *p = skipdigits(eap->cmd);
8492
8493 if (p == range_end)
8494 return FAIL;
8495 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8496}
8497
8498/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008499 * :put r
8500 * :put ={expr}
8501 */
8502 static char_u *
8503compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8504{
8505 char_u *line = arg;
8506 linenr_T lnum;
8507 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008508 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008509
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008510 eap->regname = *line;
8511
8512 if (eap->regname == '=')
8513 {
8514 char_u *p = line + 1;
8515
8516 if (compile_expr0(&p, cctx) == FAIL)
8517 return NULL;
8518 line = p;
8519 }
8520 else if (eap->regname != NUL)
8521 ++line;
8522
Bram Moolenaar08597872020-12-10 19:43:40 +01008523 if (compile_variable_range(eap, cctx) == OK)
8524 {
8525 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8526 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008527 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008528 {
8529 // Either no range or a number.
8530 // "errormsg" will not be set because the range is ADDR_LINES.
8531 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008532 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008533 return NULL;
8534 if (eap->addr_count == 0)
8535 lnum = -1;
8536 else
8537 lnum = eap->line2;
8538 if (above)
8539 --lnum;
8540 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008541
8542 generate_PUT(cctx, eap->regname, lnum);
8543 return line;
8544}
8545
8546/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008547 * A command that is not compiled, execute with legacy code.
8548 */
8549 static char_u *
8550compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8551{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008552 char_u *p;
8553 int has_expr = FALSE;
8554 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008555
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008556 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008557 goto theend;
8558
Bram Moolenaare729ce22021-06-06 21:38:09 +02008559 // If there was a prececing command modifier, drop it and include it in the
8560 // EXEC command.
8561 if (cctx->ctx_has_cmdmod)
8562 {
8563 garray_T *instr = &cctx->ctx_instr;
8564 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8565
8566 if (isn->isn_type == ISN_CMDMOD)
8567 {
8568 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8569 ->cmod_filter_regmatch.regprog);
8570 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8571 --instr->ga_len;
8572 cctx->ctx_has_cmdmod = FALSE;
8573 }
8574 }
8575
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008576 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008577 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008578 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008579 int usefilter = FALSE;
8580
8581 has_expr = argt & (EX_XFILE | EX_EXPAND);
8582
8583 // If the command can be followed by a bar, find the bar and truncate
8584 // it, so that the following command can be compiled.
8585 // The '|' is overwritten with a NUL, it is put back below.
8586 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8587 && *eap->arg == '!')
8588 // :w !filter or :r !filter or :r! filter
8589 usefilter = TRUE;
8590 if ((argt & EX_TRLBAR) && !usefilter)
8591 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008592 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008593 separate_nextcmd(eap);
8594 if (eap->nextcmd != NULL)
8595 nextcmd = eap->nextcmd;
8596 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008597 else if (eap->cmdidx == CMD_wincmd)
8598 {
8599 p = eap->arg;
8600 if (*p != NUL)
8601 ++p;
8602 if (*p == 'g' || *p == Ctrl_G)
8603 ++p;
8604 p = skipwhite(p);
8605 if (*p == '|')
8606 {
8607 *p = NUL;
8608 nextcmd = p + 1;
8609 }
8610 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008611 }
8612
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008613 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8614 {
8615 // expand filename in "syntax include [@group] filename"
8616 has_expr = TRUE;
8617 eap->arg = skipwhite(eap->arg + 7);
8618 if (*eap->arg == '@')
8619 eap->arg = skiptowhite(eap->arg);
8620 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008621
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008622 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8623 && STRLEN(eap->arg) > 4)
8624 {
8625 int delim = *eap->arg;
8626
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008627 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008628 if (*p == delim)
8629 {
8630 eap->arg = p + 1;
8631 has_expr = TRUE;
8632 }
8633 }
8634
Bram Moolenaarecac5912021-01-05 19:23:28 +01008635 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8636 {
8637 // TODO: should only expand when appropriate for the command
8638 eap->arg = skiptowhite(eap->arg);
8639 has_expr = TRUE;
8640 }
8641
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008642 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008643 {
8644 int count = 0;
8645 char_u *start = skipwhite(line);
8646
8647 // :cmd xxx`=expr1`yyy`=expr2`zzz
8648 // PUSHS ":cmd xxx"
8649 // eval expr1
8650 // PUSHS "yyy"
8651 // eval expr2
8652 // PUSHS "zzz"
8653 // EXECCONCAT 5
8654 for (;;)
8655 {
8656 if (p > start)
8657 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008658 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008659 ++count;
8660 }
8661 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008662 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008663 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008664 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008665 ++count;
8666 p = skipwhite(p);
8667 if (*p != '`')
8668 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008669 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008670 return NULL;
8671 }
8672 start = p + 1;
8673
8674 p = (char_u *)strstr((char *)start, "`=");
8675 if (p == NULL)
8676 {
8677 if (*skipwhite(start) != NUL)
8678 {
8679 generate_PUSHS(cctx, vim_strsave(start));
8680 ++count;
8681 }
8682 break;
8683 }
8684 }
8685 generate_EXECCONCAT(cctx, count);
8686 }
8687 else
8688 generate_EXEC(cctx, line);
8689
8690theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008691 if (*nextcmd != NUL)
8692 {
8693 // the parser expects a pointer to the bar, put it back
8694 --nextcmd;
8695 *nextcmd = '|';
8696 }
8697
8698 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008699}
8700
Bram Moolenaar20677332021-06-06 17:02:53 +02008701/*
8702 * A script command with heredoc, e.g.
8703 * ruby << EOF
8704 * command
8705 * EOF
8706 * Has been turned into one long line with NL characters by
8707 * get_function_body():
8708 * ruby << EOF<NL> command<NL>EOF
8709 */
8710 static char_u *
8711compile_script(char_u *line, cctx_T *cctx)
8712{
8713 if (cctx->ctx_skip != SKIP_YES)
8714 {
8715 isn_T *isn;
8716
8717 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8718 return NULL;
8719 isn->isn_arg.string = vim_strsave(line);
8720 }
8721 return (char_u *)"";
8722}
8723
Bram Moolenaar8238f082021-04-20 21:10:48 +02008724
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008725/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008726 * :s/pat/repl/
8727 */
8728 static char_u *
8729compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8730{
8731 char_u *cmd = eap->arg;
8732 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8733
8734 if (expr != NULL)
8735 {
8736 int delimiter = *cmd++;
8737
8738 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008739 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008740 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8741 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008742 garray_T save_ga = cctx->ctx_instr;
8743 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008744 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008745 int trailing_error;
8746 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008747 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008748 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008749
8750 cmd += 3;
8751 end = skip_substitute(cmd, delimiter);
8752
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008753 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008754 // labels are correct.
8755 cctx->ctx_instr.ga_len = 0;
8756 cctx->ctx_instr.ga_maxlen = 0;
8757 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008758 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008759 if (end[-1] == NUL)
8760 end[-1] = delimiter;
8761 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008762 trailing_error = *cmd != delimiter && *cmd != NUL;
8763
Bram Moolenaar169502f2021-04-21 12:19:35 +02008764 if (expr_res == FAIL || trailing_error
8765 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008766 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008767 if (trailing_error)
8768 semsg(_(e_trailing_arg), cmd);
8769 clear_instr_ga(&cctx->ctx_instr);
8770 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008771 return NULL;
8772 }
8773
Bram Moolenaar8238f082021-04-20 21:10:48 +02008774 // Move the generated instructions into the ISN_SUBSTITUTE
8775 // instructions, then restore the list of instructions before
8776 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008777 instr_count = cctx->ctx_instr.ga_len;
8778 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008779 instr[instr_count].isn_type = ISN_FINISH;
8780
8781 cctx->ctx_instr = save_ga;
8782 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8783 {
8784 int idx;
8785
8786 for (idx = 0; idx < instr_count; ++idx)
8787 delete_instr(instr + idx);
8788 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008789 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008790 }
8791 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8792 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008793
8794 // skip over flags
8795 if (*end == '&')
8796 ++end;
8797 while (ASCII_ISALPHA(*end) || *end == '#')
8798 ++end;
8799 return end;
8800 }
8801 }
8802
8803 return compile_exec(arg, eap, cctx);
8804}
8805
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008806 static char_u *
8807compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8808{
8809 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008810 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008811
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008812 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008813 {
8814 if (STRNCMP(arg, "END", 3) == 0)
8815 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008816 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008817 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008818 // First load the current variable value.
8819 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8820 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008821 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008822 }
8823
8824 // Gets the redirected text and put it on the stack, then store it
8825 // in the variable.
8826 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8827
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008828 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008829 generate_instr_drop(cctx, ISN_CONCAT, 1);
8830
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008831 if (lhs->lhs_has_index)
8832 {
8833 // Use the info in "lhs" to store the value at the index in the
8834 // list or dict.
8835 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8836 &t_string, cctx) == FAIL)
8837 return NULL;
8838 }
8839 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008840 return NULL;
8841
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008842 VIM_CLEAR(lhs->lhs_name);
8843 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008844 return arg + 3;
8845 }
8846 emsg(_(e_cannot_nest_redir));
8847 return NULL;
8848 }
8849
8850 if (arg[0] == '=' && arg[1] == '>')
8851 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008852 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008853
8854 // redirect to a variable is compiled
8855 arg += 2;
8856 if (*arg == '>')
8857 {
8858 ++arg;
8859 append = TRUE;
8860 }
8861 arg = skipwhite(arg);
8862
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008863 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008864 FALSE, FALSE, 1, cctx) == FAIL)
8865 return NULL;
8866 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008867 lhs->lhs_append = append;
8868 if (lhs->lhs_has_index)
8869 {
8870 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8871 if (lhs->lhs_whole == NULL)
8872 return NULL;
8873 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008874
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008875 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008876 }
8877
8878 // other redirects are handled like at script level
8879 return compile_exec(line, eap, cctx);
8880}
8881
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008882#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008883 static char_u *
8884compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8885{
8886 isn_T *isn;
8887 char_u *p;
8888
8889 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8890 if (isn == NULL)
8891 return NULL;
8892 isn->isn_arg.number = eap->cmdidx;
8893
8894 p = eap->arg;
8895 if (compile_expr0(&p, cctx) == FAIL)
8896 return NULL;
8897
8898 isn = generate_instr(cctx, ISN_CEXPR_CORE);
8899 if (isn == NULL)
8900 return NULL;
8901 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
8902 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
8903 return NULL;
8904 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
8905 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
8906 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
8907
8908 return p;
8909}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008910#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008911
Bram Moolenaar4c137212021-04-19 16:48:48 +02008912/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008913 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008914 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008915 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008916 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008917add_def_function(ufunc_T *ufunc)
8918{
8919 dfunc_T *dfunc;
8920
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008921 if (def_functions.ga_len == 0)
8922 {
8923 // The first position is not used, so that a zero uf_dfunc_idx means it
8924 // wasn't set.
8925 if (ga_grow(&def_functions, 1) == FAIL)
8926 return FAIL;
8927 ++def_functions.ga_len;
8928 }
8929
Bram Moolenaar09689a02020-05-09 22:50:08 +02008930 // Add the function to "def_functions".
8931 if (ga_grow(&def_functions, 1) == FAIL)
8932 return FAIL;
8933 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8934 CLEAR_POINTER(dfunc);
8935 dfunc->df_idx = def_functions.ga_len;
8936 ufunc->uf_dfunc_idx = dfunc->df_idx;
8937 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008938 dfunc->df_name = vim_strsave(ufunc->uf_name);
8939 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008940 ++def_functions.ga_len;
8941 return OK;
8942}
8943
8944/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008945 * After ex_function() has collected all the function lines: parse and compile
8946 * the lines into instructions.
8947 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008948 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8949 * the return statement (used for lambda). When uf_ret_type is already set
8950 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008951 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008952 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008953 * This can be used recursively through compile_lambda(), which may reallocate
8954 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008955 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008956 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008957 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008958compile_def_function(
8959 ufunc_T *ufunc,
8960 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008961 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008962 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008963{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008964 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008965 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008966 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008967 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008968 cctx_T cctx;
8969 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008970 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008971 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008972 int ret = FAIL;
8973 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008974 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02008975 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008976 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008977 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008978#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008979 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008980#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008981
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008982 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008983 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008984 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008985 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008986 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8987 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008988 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008989 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008990 else
8991 {
8992 if (add_def_function(ufunc) == FAIL)
8993 return FAIL;
8994 new_def_function = TRUE;
8995 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008996
Bram Moolenaar985116a2020-07-12 17:31:09 +02008997 ufunc->uf_def_status = UF_COMPILING;
8998
Bram Moolenaara80faa82020-04-12 19:37:17 +02008999 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009000
Bram Moolenaarf002a412021-01-24 13:34:18 +01009001#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009002 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009003#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009004 cctx.ctx_ufunc = ufunc;
9005 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009006 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009007 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9008 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9009 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9010 cctx.ctx_type_list = &ufunc->uf_type_list;
9011 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9012 instr = &cctx.ctx_instr;
9013
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009014 // Set the context to the function, it may be compiled when called from
9015 // another script. Set the script version to the most modern one.
9016 // The line number will be set in next_line_from_context().
9017 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009018 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9019
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009020 // Don't use the flag from ":legacy" here.
9021 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9022
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009023 // Make sure error messages are OK.
9024 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9025 if (do_estack_push)
9026 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009027 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009028
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009029 if (ufunc->uf_def_args.ga_len > 0)
9030 {
9031 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009032 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009033 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009034 int i;
9035 char_u *arg;
9036 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009037 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009038
9039 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009040 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009041 for (i = 0; i < count; ++i)
9042 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009043 garray_T *stack = &cctx.ctx_type_stack;
9044 type_T *val_type;
9045 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009046 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009047 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009048 int jump_instr_idx = instr->ga_len;
9049 isn_T *isn;
9050
9051 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9052 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9053 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009054
9055 // Make sure later arguments are not found.
9056 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009057
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009058 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009059 r = compile_expr0(&arg, &cctx);
9060
9061 ufunc->uf_args.ga_len = uf_args_len;
9062 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009063 goto erret;
9064
9065 // If no type specified use the type of the default value.
9066 // Otherwise check that the default value type matches the
9067 // specified type.
9068 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009069 where.wt_index = arg_idx + 1;
9070 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009071 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009072 {
9073 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009074 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009075 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009076 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009077 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009078 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009079
9080 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009081 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009082
9083 // set instruction index in JUMP_IF_ARG_SET to here
9084 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9085 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009086 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009087
9088 if (did_set_arg_type)
9089 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009090 }
9091
9092 /*
9093 * Loop over all the lines of the function and generate instructions.
9094 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009095 for (;;)
9096 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009097 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009098 int starts_with_colon = FALSE;
9099 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009100 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009101
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009102 // Bail out on the first error to avoid a flood of errors and report
9103 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009104 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009105 goto erret;
9106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009107 if (line != NULL && *line == '|')
9108 // the line continues after a '|'
9109 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009110 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009111 && !(*line == '#' && (line == cctx.ctx_line_start
9112 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009113 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009114 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009115 goto erret;
9116 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009117 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9118 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009119 else
9120 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009121 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009122 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009123 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009124 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009125#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009126 if (cctx.ctx_skip != SKIP_YES)
9127 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009128#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009129 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009130 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009131 // Make a copy, splitting off nextcmd and removing trailing spaces
9132 // may change it.
9133 if (line != NULL)
9134 {
9135 line = vim_strsave(line);
9136 vim_free(line_to_free);
9137 line_to_free = line;
9138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009139 }
9140
Bram Moolenaara80faa82020-04-12 19:37:17 +02009141 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009142 ea.cmdlinep = &line;
9143 ea.cmd = skipwhite(line);
9144
Bram Moolenaarb2049902021-01-24 12:53:53 +01009145 if (*ea.cmd == '#')
9146 {
9147 // "#" starts a comment
9148 line = (char_u *)"";
9149 continue;
9150 }
9151
Bram Moolenaarf002a412021-01-24 13:34:18 +01009152#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009153 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
9154 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009155 {
9156 may_generate_prof_end(&cctx, prof_lnum);
9157
9158 prof_lnum = cctx.ctx_lnum;
9159 generate_instr(&cctx, ISN_PROF_START);
9160 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009161#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009162
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009163 // Some things can be recognized by the first character.
9164 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009165 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009166 case '}':
9167 {
9168 // "}" ends a block scope
9169 scopetype_T stype = cctx.ctx_scope == NULL
9170 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009171
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009172 if (stype == BLOCK_SCOPE)
9173 {
9174 compile_endblock(&cctx);
9175 line = ea.cmd;
9176 }
9177 else
9178 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009179 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009180 goto erret;
9181 }
9182 if (line != NULL)
9183 line = skipwhite(ea.cmd + 1);
9184 continue;
9185 }
9186
9187 case '{':
9188 // "{" starts a block scope
9189 // "{'a': 1}->func() is something else
9190 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9191 {
9192 line = compile_block(ea.cmd, &cctx);
9193 continue;
9194 }
9195 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009196 }
9197
9198 /*
9199 * COMMAND MODIFIERS
9200 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009201 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009202 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9203 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009204 {
9205 if (errormsg != NULL)
9206 goto erret;
9207 // empty line or comment
9208 line = (char_u *)"";
9209 continue;
9210 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009211 generate_cmdmods(&cctx, &local_cmdmod);
9212 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009213
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009214 // Check if there was a colon after the last command modifier or before
9215 // the current position.
9216 for (p = ea.cmd; p >= line; --p)
9217 {
9218 if (*p == ':')
9219 starts_with_colon = TRUE;
9220 if (p < ea.cmd && !VIM_ISWHITE(*p))
9221 break;
9222 }
9223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009224 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009225 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009226 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009227 {
9228 if (*ea.cmd == '(')
9229 // not for "call()"
9230 ea.cmd = p;
9231 else
9232 ea.cmd = skipwhite(ea.cmd);
9233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009234
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009235 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009236 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009237 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009238
Bram Moolenaar17126b12021-01-07 22:03:02 +01009239 // Check for assignment after command modifiers.
9240 assign = may_compile_assignment(&ea, &line, &cctx);
9241 if (assign == OK)
9242 goto nextline;
9243 if (assign == FAIL)
9244 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009245 }
9246
9247 /*
9248 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009249 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009250 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009251 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009252 cmd = ea.cmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009253 if (starts_with_colon || !(*cmd == '\''
9254 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009255 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009256 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009257 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009258 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009259 if (!starts_with_colon)
9260 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009261 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009262 goto erret;
9263 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009264 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009265 if (ends_excmd2(line, ea.cmd))
9266 {
9267 // A range without a command: jump to the line.
9268 // TODO: compile to a more efficient command, possibly
9269 // calling parse_cmd_address().
9270 ea.cmdidx = CMD_SIZE;
9271 line = compile_exec(line, &ea, &cctx);
9272 goto nextline;
9273 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009274 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009275 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009276 p = find_ex_command(&ea, NULL, starts_with_colon
9277 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009278
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009279 if (p == NULL)
9280 {
9281 if (cctx.ctx_skip != SKIP_YES)
9282 emsg(_(e_ambiguous_use_of_user_defined_command));
9283 goto erret;
9284 }
9285
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009286 // When using ":legacy cmd" always use compile_exec().
9287 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009288 {
9289 char_u *start = ea.cmd;
9290
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009291 switch (ea.cmdidx)
9292 {
9293 case CMD_if:
9294 case CMD_elseif:
9295 case CMD_else:
9296 case CMD_endif:
9297 case CMD_for:
9298 case CMD_endfor:
9299 case CMD_continue:
9300 case CMD_break:
9301 case CMD_while:
9302 case CMD_endwhile:
9303 case CMD_try:
9304 case CMD_catch:
9305 case CMD_finally:
9306 case CMD_endtry:
9307 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9308 goto erret;
9309 default: break;
9310 }
9311
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009312 // ":legacy return expr" needs to be handled differently.
9313 if (checkforcmd(&start, "return", 4))
9314 ea.cmdidx = CMD_return;
9315 else
9316 ea.cmdidx = CMD_legacy;
9317 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009319 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9320 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009321 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009322 {
9323 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009324 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009325 }
9326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009327 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009328 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009329 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009330 // CMD_var cannot happen, compile_assignment() above would be
9331 // used. Most likely an assignment to a non-existing variable.
9332 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009333 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009335 }
9336
Bram Moolenaar3988f642020-08-27 22:43:03 +02009337 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009338 && ea.cmdidx != CMD_elseif
9339 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009340 && ea.cmdidx != CMD_endif
9341 && ea.cmdidx != CMD_endfor
9342 && ea.cmdidx != CMD_endwhile
9343 && ea.cmdidx != CMD_catch
9344 && ea.cmdidx != CMD_finally
9345 && ea.cmdidx != CMD_endtry)
9346 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009347 emsg(_(e_unreachable_code_after_return));
9348 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009349 }
9350
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009351 p = skipwhite(p);
9352 if (ea.cmdidx != CMD_SIZE
9353 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9354 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009355 if (ea.cmdidx >= 0)
9356 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009357 if ((ea.argt & EX_BANG) && *p == '!')
9358 {
9359 ea.forceit = TRUE;
9360 p = skipwhite(p + 1);
9361 }
9362 }
9363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009364 switch (ea.cmdidx)
9365 {
9366 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009367 ea.arg = p;
9368 line = compile_nested_function(&ea, &cctx);
9369 break;
9370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009371 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009372 // TODO: should we allow this, e.g. to declare a global
9373 // function?
9374 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009375 goto erret;
9376
9377 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009378 line = compile_return(p, check_return_type,
9379 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009380 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009381 break;
9382
9383 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009384 emsg(_(e_cannot_use_let_in_vim9_script));
9385 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009386 case CMD_var:
9387 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009388 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009389 case CMD_increment:
9390 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009391 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009392 if (line == p)
9393 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009394 break;
9395
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009396 case CMD_unlet:
9397 case CMD_unlockvar:
9398 case CMD_lockvar:
9399 line = compile_unletlock(p, &ea, &cctx);
9400 break;
9401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009402 case CMD_import:
9403 line = compile_import(p, &cctx);
9404 break;
9405
9406 case CMD_if:
9407 line = compile_if(p, &cctx);
9408 break;
9409 case CMD_elseif:
9410 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009411 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009412 break;
9413 case CMD_else:
9414 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009415 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009416 break;
9417 case CMD_endif:
9418 line = compile_endif(p, &cctx);
9419 break;
9420
9421 case CMD_while:
9422 line = compile_while(p, &cctx);
9423 break;
9424 case CMD_endwhile:
9425 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009426 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009427 break;
9428
9429 case CMD_for:
9430 line = compile_for(p, &cctx);
9431 break;
9432 case CMD_endfor:
9433 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009434 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009435 break;
9436 case CMD_continue:
9437 line = compile_continue(p, &cctx);
9438 break;
9439 case CMD_break:
9440 line = compile_break(p, &cctx);
9441 break;
9442
9443 case CMD_try:
9444 line = compile_try(p, &cctx);
9445 break;
9446 case CMD_catch:
9447 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009448 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009449 break;
9450 case CMD_finally:
9451 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009452 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009453 break;
9454 case CMD_endtry:
9455 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009456 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009457 break;
9458 case CMD_throw:
9459 line = compile_throw(p, &cctx);
9460 break;
9461
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009462 case CMD_eval:
9463 if (compile_expr0(&p, &cctx) == FAIL)
9464 goto erret;
9465
Bram Moolenaar3988f642020-08-27 22:43:03 +02009466 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009467 generate_instr_drop(&cctx, ISN_DROP, 1);
9468
9469 line = skipwhite(p);
9470 break;
9471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009472 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009473 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009474 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009475 case CMD_echomsg:
9476 case CMD_echoerr:
9477 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009478 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009479
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009480 case CMD_put:
9481 ea.cmd = cmd;
9482 line = compile_put(p, &ea, &cctx);
9483 break;
9484
Bram Moolenaar4c137212021-04-19 16:48:48 +02009485 case CMD_substitute:
9486 if (cctx.ctx_skip == SKIP_YES)
9487 line = (char_u *)"";
9488 else
9489 {
9490 ea.arg = p;
9491 line = compile_substitute(line, &ea, &cctx);
9492 }
9493 break;
9494
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009495 case CMD_redir:
9496 ea.arg = p;
9497 line = compile_redir(line, &ea, &cctx);
9498 break;
9499
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009500 case CMD_cexpr:
9501 case CMD_lexpr:
9502 case CMD_caddexpr:
9503 case CMD_laddexpr:
9504 case CMD_cgetexpr:
9505 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009506#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009507 ea.arg = p;
9508 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009509#else
9510 ex_ni(&ea);
9511 line = NULL;
9512#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009513 break;
9514
Bram Moolenaar3988f642020-08-27 22:43:03 +02009515 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009516
Bram Moolenaarae616492020-07-28 20:07:27 +02009517 case CMD_append:
9518 case CMD_change:
9519 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009520 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009521 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009522 case CMD_xit:
9523 not_in_vim9(&ea);
9524 goto erret;
9525
Bram Moolenaar002262f2020-07-08 17:47:57 +02009526 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009527 if (cctx.ctx_skip != SKIP_YES)
9528 {
9529 semsg(_(e_invalid_command_str), ea.cmd);
9530 goto erret;
9531 }
9532 // We don't check for a next command here.
9533 line = (char_u *)"";
9534 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009535
Bram Moolenaar20677332021-06-06 17:02:53 +02009536 case CMD_lua:
9537 case CMD_mzscheme:
9538 case CMD_perl:
9539 case CMD_py3:
9540 case CMD_python3:
9541 case CMD_python:
9542 case CMD_pythonx:
9543 case CMD_ruby:
9544 case CMD_tcl:
9545 ea.arg = p;
9546 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009547 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009548 else
9549 // heredoc lines have been concatenated with NL
9550 // characters in get_function_body()
9551 line = compile_script(line, &cctx);
9552 break;
9553
9554 default:
9555 // Not recognized, execute with do_cmdline_cmd().
9556 ea.arg = p;
9557 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009558 break;
9559 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009560nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009561 if (line == NULL)
9562 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009563 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009564
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009565 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009566 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009568 if (cctx.ctx_type_stack.ga_len < 0)
9569 {
9570 iemsg("Type stack underflow");
9571 goto erret;
9572 }
9573 }
9574
9575 if (cctx.ctx_scope != NULL)
9576 {
9577 if (cctx.ctx_scope->se_type == IF_SCOPE)
9578 emsg(_(e_endif));
9579 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9580 emsg(_(e_endwhile));
9581 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9582 emsg(_(e_endfor));
9583 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009584 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009585 goto erret;
9586 }
9587
Bram Moolenaarefd88552020-06-18 20:50:10 +02009588 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009589 {
9590 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
9591 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009592 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009593 goto erret;
9594 }
9595
9596 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01009597 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009598 }
9599
Bram Moolenaar599410c2021-04-10 14:03:43 +02009600 // When compiled with ":silent!" and there was an error don't consider the
9601 // function compiled.
9602 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009603 {
9604 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9605 + ufunc->uf_dfunc_idx;
9606 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009607 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009608#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009609 if (cctx.ctx_profiling)
9610 {
9611 dfunc->df_instr_prof = instr->ga_data;
9612 dfunc->df_instr_prof_count = instr->ga_len;
9613 }
9614 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009615#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009616 {
9617 dfunc->df_instr = instr->ga_data;
9618 dfunc->df_instr_count = instr->ga_len;
9619 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009620 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009621 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009622 if (cctx.ctx_outer_used)
9623 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009624 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009626
9627 ret = OK;
9628
9629erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009630 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009631 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009632 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9633 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009634
Bram Moolenaar8238f082021-04-20 21:10:48 +02009635 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009636 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009637
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009638 // If using the last entry in the table and it was added above, we
9639 // might as well remove it.
9640 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009641 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009642 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009643 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009644 ufunc->uf_dfunc_idx = 0;
9645 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009646 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009647
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009648 while (cctx.ctx_scope != NULL)
9649 drop_scope(&cctx);
9650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009651 if (errormsg != NULL)
9652 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009653 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009654 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009655 }
9656
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009657 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9658 {
9659 if (ret == OK)
9660 {
9661 emsg(_(e_missing_redir_end));
9662 ret = FAIL;
9663 }
9664 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009665 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009666 }
9667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009668 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009669 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009670 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009671 if (do_estack_push)
9672 estack_pop();
9673
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009674 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009675 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009676 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009677 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009678 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009679}
9680
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009681 void
9682set_function_type(ufunc_T *ufunc)
9683{
9684 int varargs = ufunc->uf_va_name != NULL;
9685 int argcount = ufunc->uf_args.ga_len;
9686
9687 // Create a type for the function, with the return type and any
9688 // argument types.
9689 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9690 // The type is included in "tt_args".
9691 if (argcount > 0 || varargs)
9692 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009693 if (ufunc->uf_type_list.ga_itemsize == 0)
9694 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009695 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9696 argcount, &ufunc->uf_type_list);
9697 // Add argument types to the function type.
9698 if (func_type_add_arg_types(ufunc->uf_func_type,
9699 argcount + varargs,
9700 &ufunc->uf_type_list) == FAIL)
9701 return;
9702 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9703 ufunc->uf_func_type->tt_min_argcount =
9704 argcount - ufunc->uf_def_args.ga_len;
9705 if (ufunc->uf_arg_types == NULL)
9706 {
9707 int i;
9708
9709 // lambda does not have argument types.
9710 for (i = 0; i < argcount; ++i)
9711 ufunc->uf_func_type->tt_args[i] = &t_any;
9712 }
9713 else
9714 mch_memmove(ufunc->uf_func_type->tt_args,
9715 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9716 if (varargs)
9717 {
9718 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009719 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009720 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9721 }
9722 }
9723 else
9724 // No arguments, can use a predefined type.
9725 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9726 argcount, &ufunc->uf_type_list);
9727}
9728
9729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009730/*
9731 * Delete an instruction, free what it contains.
9732 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009733 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009734delete_instr(isn_T *isn)
9735{
9736 switch (isn->isn_type)
9737 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009738 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009739 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009740 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009741 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009742 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009743 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009744 case ISN_LOADENV:
9745 case ISN_LOADG:
9746 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009747 case ISN_LOADT:
9748 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009749 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009750 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009751 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009752 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009753 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009754 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009755 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009756 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009757 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009758 case ISN_STOREW:
9759 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009760 vim_free(isn->isn_arg.string);
9761 break;
9762
Bram Moolenaar4c137212021-04-19 16:48:48 +02009763 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009764 {
9765 int idx;
9766 isn_T *list = isn->isn_arg.subs.subs_instr;
9767
9768 vim_free(isn->isn_arg.subs.subs_cmd);
9769 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9770 delete_instr(list + idx);
9771 vim_free(list);
9772 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009773 break;
9774
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009775 case ISN_INSTR:
9776 {
9777 int idx;
9778 isn_T *list = isn->isn_arg.instr;
9779
9780 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9781 delete_instr(list + idx);
9782 vim_free(list);
9783 }
9784 break;
9785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009786 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009787 case ISN_STORES:
9788 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009789 break;
9790
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009791 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009792 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009793 vim_free(isn->isn_arg.unlet.ul_name);
9794 break;
9795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009796 case ISN_STOREOPT:
9797 vim_free(isn->isn_arg.storeopt.so_name);
9798 break;
9799
9800 case ISN_PUSHBLOB: // push blob isn_arg.blob
9801 blob_unref(isn->isn_arg.blob);
9802 break;
9803
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009804 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009805#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009806 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009807#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009808 break;
9809
9810 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009811#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009812 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009813#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009814 break;
9815
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009816 case ISN_UCALL:
9817 vim_free(isn->isn_arg.ufunc.cuf_name);
9818 break;
9819
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009820 case ISN_FUNCREF:
9821 {
9822 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9823 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009824 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009825
Bram Moolenaar077a4232020-12-22 18:33:27 +01009826 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9827 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009828 }
9829 break;
9830
9831 case ISN_DCALL:
9832 {
9833 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9834 + isn->isn_arg.dfunc.cdf_idx;
9835
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009836 if (dfunc->df_ufunc != NULL
9837 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009838 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009839 }
9840 break;
9841
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009842 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009843 {
9844 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9845 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9846
9847 if (ufunc != NULL)
9848 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009849 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009850 func_ptr_unref(ufunc);
9851 }
9852
9853 vim_free(lambda);
9854 vim_free(isn->isn_arg.newfunc.nf_global);
9855 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009856 break;
9857
Bram Moolenaar5e654232020-09-16 15:22:00 +02009858 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009859 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009860 free_type(isn->isn_arg.type.ct_type);
9861 break;
9862
Bram Moolenaar02194d22020-10-24 23:08:38 +02009863 case ISN_CMDMOD:
9864 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9865 ->cmod_filter_regmatch.regprog);
9866 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9867 break;
9868
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009869 case ISN_LOADSCRIPT:
9870 case ISN_STORESCRIPT:
9871 vim_free(isn->isn_arg.script.scriptref);
9872 break;
9873
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009874 case ISN_TRY:
9875 vim_free(isn->isn_arg.try.try_ref);
9876 break;
9877
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009878 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +02009879 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009880 vim_free(isn->isn_arg.cexpr.cexpr_ref);
9881 break;
9882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009883 case ISN_2BOOL:
9884 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009885 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009886 case ISN_ADDBLOB:
9887 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009888 case ISN_ANYINDEX:
9889 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009890 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009891 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009892 case ISN_BLOBINDEX:
9893 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009894 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009895 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009896 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009897 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009898 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009899 case ISN_COMPAREANY:
9900 case ISN_COMPAREBLOB:
9901 case ISN_COMPAREBOOL:
9902 case ISN_COMPAREDICT:
9903 case ISN_COMPAREFLOAT:
9904 case ISN_COMPAREFUNC:
9905 case ISN_COMPARELIST:
9906 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009907 case ISN_COMPARESPECIAL:
9908 case ISN_COMPARESTRING:
9909 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009910 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009911 case ISN_DROP:
9912 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009913 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009914 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009915 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009916 case ISN_EXECCONCAT:
9917 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009918 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009919 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009920 case ISN_GETITEM:
9921 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009922 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009923 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009924 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009925 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009926 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009927 case ISN_LOADBDICT:
9928 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009929 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009930 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009931 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009932 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009933 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009934 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009935 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009936 case ISN_NEGATENR:
9937 case ISN_NEWDICT:
9938 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009939 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009940 case ISN_OPFLOAT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009941 case ISN_FINISH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009942 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009943 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009944 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009945 case ISN_PROF_END:
9946 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009947 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009948 case ISN_PUSHF:
9949 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009950 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009951 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009952 case ISN_REDIREND:
9953 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009954 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009955 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009956 case ISN_SHUFFLE:
9957 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009958 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009959 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009960 case ISN_STORENR:
9961 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009962 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009963 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009964 case ISN_STOREV:
9965 case ISN_STRINDEX:
9966 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009967 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009968 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009969 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009970 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009971 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009972 // nothing allocated
9973 break;
9974 }
9975}
9976
9977/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009978 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009979 */
9980 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009981delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009982{
9983 int idx;
9984
9985 ga_clear(&dfunc->df_def_args_isn);
9986
9987 if (dfunc->df_instr != NULL)
9988 {
9989 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9990 delete_instr(dfunc->df_instr + idx);
9991 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009992 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009993 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009994#ifdef FEAT_PROFILE
9995 if (dfunc->df_instr_prof != NULL)
9996 {
9997 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9998 delete_instr(dfunc->df_instr_prof + idx);
9999 VIM_CLEAR(dfunc->df_instr_prof);
10000 dfunc->df_instr_prof = NULL;
10001 }
10002#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010003
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010004 if (mark_deleted)
10005 dfunc->df_deleted = TRUE;
10006 if (dfunc->df_ufunc != NULL)
10007 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010008}
10009
10010/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010011 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010012 * function, unless another user function still uses it.
10013 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010014 */
10015 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010016unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010017{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010018 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010019 {
10020 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10021 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010022
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010023 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010024 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010025 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010026 ufunc->uf_dfunc_idx = 0;
10027 if (dfunc->df_ufunc == ufunc)
10028 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010029 }
10030}
10031
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010032/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010033 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010034 */
10035 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010036link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010037{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010038 if (ufunc->uf_dfunc_idx > 0)
10039 {
10040 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10041 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010042
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010043 ++dfunc->df_refcount;
10044 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010045}
10046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010047#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010048/*
10049 * Free all functions defined with ":def".
10050 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010051 void
10052free_def_functions(void)
10053{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010054 int idx;
10055
10056 for (idx = 0; idx < def_functions.ga_len; ++idx)
10057 {
10058 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10059
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010060 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010061 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010062 }
10063
10064 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010065}
10066#endif
10067
10068
10069#endif // FEAT_EVAL