blob: d6ea322d3f26bef479b8f9a04f3e0b5e8baea7a1 [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100580 */
581 static int
582may_generate_2STRING(int offset, cctx_T *cctx)
583{
584 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200585 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100587 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100589 RETURN_OK_IF_SKIP(cctx);
590 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200591 switch ((*type)->tt_type)
592 {
593 // nothing to be done
594 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200596 // conversion possible
597 case VAR_SPECIAL:
598 case VAR_BOOL:
599 case VAR_NUMBER:
600 case VAR_FLOAT:
601 break;
602
603 // conversion possible (with runtime check)
604 case VAR_ANY:
605 case VAR_UNKNOWN:
606 isntype = ISN_2STRING_ANY;
607 break;
608
609 // conversion not possible
610 case VAR_VOID:
611 case VAR_BLOB:
612 case VAR_FUNC:
613 case VAR_PARTIAL:
614 case VAR_LIST:
615 case VAR_DICT:
616 case VAR_JOB:
617 case VAR_CHANNEL:
618 to_string_error((*type)->tt_type);
619 return FAIL;
620 }
621
622 *type = &t_string;
623 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 return FAIL;
625 isn->isn_arg.number = offset;
626
627 return OK;
628}
629
630 static int
631check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
632{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200633 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200635 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 {
637 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200638 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200640 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 return FAIL;
642 }
643 return OK;
644}
645
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200646 static int
647generate_add_instr(
648 cctx_T *cctx,
649 vartype_T vartype,
650 type_T *type1,
651 type_T *type2)
652{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100653 garray_T *stack = &cctx->ctx_type_stack;
654 isn_T *isn = generate_instr_drop(cctx,
655 vartype == VAR_NUMBER ? ISN_OPNR
656 : vartype == VAR_LIST ? ISN_ADDLIST
657 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200658#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100659 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200660#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100661 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200662
663 if (vartype != VAR_LIST && vartype != VAR_BLOB
664 && type1->tt_type != VAR_ANY
665 && type2->tt_type != VAR_ANY
666 && check_number_or_float(
667 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
668 return FAIL;
669
670 if (isn != NULL)
671 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100672
673 // When concatenating two lists with different member types the member type
674 // becomes "any".
675 if (vartype == VAR_LIST
676 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
677 && type1->tt_member != type2->tt_member)
678 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
679
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200680 return isn == NULL ? FAIL : OK;
681}
682
683/*
684 * Get the type to use for an instruction for an operation on "type1" and
685 * "type2". If they are matching use a type-specific instruction. Otherwise
686 * fall back to runtime type checking.
687 */
688 static vartype_T
689operator_type(type_T *type1, type_T *type2)
690{
691 if (type1->tt_type == type2->tt_type
692 && (type1->tt_type == VAR_NUMBER
693 || type1->tt_type == VAR_LIST
694#ifdef FEAT_FLOAT
695 || type1->tt_type == VAR_FLOAT
696#endif
697 || type1->tt_type == VAR_BLOB))
698 return type1->tt_type;
699 return VAR_ANY;
700}
701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702/*
703 * Generate an instruction with two arguments. The instruction depends on the
704 * type of the arguments.
705 */
706 static int
707generate_two_op(cctx_T *cctx, char_u *op)
708{
709 garray_T *stack = &cctx->ctx_type_stack;
710 type_T *type1;
711 type_T *type2;
712 vartype_T vartype;
713 isn_T *isn;
714
Bram Moolenaar080457c2020-03-03 21:53:32 +0100715 RETURN_OK_IF_SKIP(cctx);
716
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200717 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
719 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200720 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721
722 switch (*op)
723 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200724 case '+':
725 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 break;
728
729 case '-':
730 case '*':
731 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
732 op) == FAIL)
733 return FAIL;
734 if (vartype == VAR_NUMBER)
735 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
736#ifdef FEAT_FLOAT
737 else if (vartype == VAR_FLOAT)
738 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
739#endif
740 else
741 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
742 if (isn != NULL)
743 isn->isn_arg.op.op_type = *op == '*'
744 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
745 break;
746
Bram Moolenaar4c683752020-04-05 21:38:23 +0200747 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200749 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 && type2->tt_type != VAR_NUMBER))
751 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200752 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 return FAIL;
754 }
755 isn = generate_instr_drop(cctx,
756 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
757 if (isn != NULL)
758 isn->isn_arg.op.op_type = EXPR_REM;
759 break;
760 }
761
762 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200763 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 {
765 type_T *type = &t_any;
766
767#ifdef FEAT_FLOAT
768 // float+number and number+float results in float
769 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
770 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
771 type = &t_float;
772#endif
773 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
774 }
775
776 return OK;
777}
778
779/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200780 * Get the instruction to use for comparing "type1" with "type2"
781 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200783 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100784get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785{
786 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787
Bram Moolenaar4c683752020-04-05 21:38:23 +0200788 if (type1 == VAR_UNKNOWN)
789 type1 = VAR_ANY;
790 if (type2 == VAR_UNKNOWN)
791 type2 = VAR_ANY;
792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 if (type1 == type2)
794 {
795 switch (type1)
796 {
797 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
798 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
799 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
800 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
801 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
802 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
803 case VAR_LIST: isntype = ISN_COMPARELIST; break;
804 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
805 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 default: isntype = ISN_COMPAREANY; break;
807 }
808 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200809 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100811 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 isntype = ISN_COMPAREANY;
813
Bram Moolenaar657137c2021-01-09 15:45:23 +0100814 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815 && (isntype == ISN_COMPAREBOOL
816 || isntype == ISN_COMPARESPECIAL
817 || isntype == ISN_COMPARENR
818 || isntype == ISN_COMPAREFLOAT))
819 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200820 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100821 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200822 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 }
824 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100825 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
827 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100828 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
829 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 && (type1 == VAR_BLOB || type2 == VAR_BLOB
831 || type1 == VAR_LIST || type2 == VAR_LIST))))
832 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200833 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200835 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200837 return isntype;
838}
839
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200840 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100841check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200842{
843 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
844 return FAIL;
845 return OK;
846}
847
Bram Moolenaara5565e42020-05-09 15:44:01 +0200848/*
849 * Generate an ISN_COMPARE* instruction with a boolean result.
850 */
851 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100852generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200853{
854 isntype_T isntype;
855 isn_T *isn;
856 garray_T *stack = &cctx->ctx_type_stack;
857 vartype_T type1;
858 vartype_T type2;
859
860 RETURN_OK_IF_SKIP(cctx);
861
862 // Get the known type of the two items on the stack. If they are matching
863 // use a type-specific instruction. Otherwise fall back to runtime type
864 // checking.
865 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
866 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100867 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200868 if (isntype == ISN_DROP)
869 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870
871 if ((isn = generate_instr(cctx, isntype)) == NULL)
872 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100873 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 isn->isn_arg.op.op_ic = ic;
875
876 // takes two arguments, puts one bool back
877 if (stack->ga_len >= 2)
878 {
879 --stack->ga_len;
880 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
881 }
882
883 return OK;
884}
885
886/*
887 * Generate an ISN_2BOOL instruction.
888 */
889 static int
890generate_2BOOL(cctx_T *cctx, int invert)
891{
892 isn_T *isn;
893 garray_T *stack = &cctx->ctx_type_stack;
894
Bram Moolenaar080457c2020-03-03 21:53:32 +0100895 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
897 return FAIL;
898 isn->isn_arg.number = invert;
899
900 // type becomes bool
901 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
902
903 return OK;
904}
905
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200906/*
907 * Generate an ISN_COND2BOOL instruction.
908 */
909 static int
910generate_COND2BOOL(cctx_T *cctx)
911{
912 isn_T *isn;
913 garray_T *stack = &cctx->ctx_type_stack;
914
915 RETURN_OK_IF_SKIP(cctx);
916 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
917 return FAIL;
918
919 // type becomes bool
920 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
921
922 return OK;
923}
924
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200926generate_TYPECHECK(
927 cctx_T *cctx,
928 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100929 int offset,
930 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931{
932 isn_T *isn;
933 garray_T *stack = &cctx->ctx_type_stack;
934
Bram Moolenaar080457c2020-03-03 21:53:32 +0100935 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
937 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200938 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100939 isn->isn_arg.type.ct_off = (int8_T)offset;
940 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941
Bram Moolenaar5e654232020-09-16 15:22:00 +0200942 // type becomes expected
943 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944
945 return OK;
946}
947
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100948 static int
949generate_SETTYPE(
950 cctx_T *cctx,
951 type_T *expected)
952{
953 isn_T *isn;
954
955 RETURN_OK_IF_SKIP(cctx);
956 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
957 return FAIL;
958 isn->isn_arg.type.ct_type = alloc_type(expected);
959 return OK;
960}
961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200963 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
964 * used. Return FALSE if the types will never match.
965 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100966 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200967use_typecheck(type_T *actual, type_T *expected)
968{
969 if (actual->tt_type == VAR_ANY
970 || actual->tt_type == VAR_UNKNOWN
971 || (actual->tt_type == VAR_FUNC
972 && (expected->tt_type == VAR_FUNC
973 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100974 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
975 && ((actual->tt_member == &t_void)
976 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200977 return TRUE;
978 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
979 && actual->tt_type == expected->tt_type)
980 // This takes care of a nested list or dict.
981 return use_typecheck(actual->tt_member, expected->tt_member);
982 return FALSE;
983}
984
985/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200986 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200987 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200988 * - "actual" is a type that can be "expected" type: add a runtime check; or
989 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200990 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
991 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200992 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100993 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200994need_type(
995 type_T *actual,
996 type_T *expected,
997 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100998 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200999 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001000 int silent,
1001 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001002{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001003 where_T where;
1004
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001005 if (expected == &t_bool && actual != &t_bool
1006 && (actual->tt_flags & TTFLAG_BOOL_OK))
1007 {
1008 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1009 // boolean is OK but requires a conversion.
1010 generate_2BOOL(cctx, FALSE);
1011 return OK;
1012 }
1013
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001014 where.wt_index = arg_idx;
1015 where.wt_variable = FALSE;
1016 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001017 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001018
1019 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001020 // If it's a constant a runtime check makes no sense.
1021 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001022 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001023 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001024 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001025 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001026
1027 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001028 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001029 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001030}
1031
1032/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001033 * Check that the top of the type stack has a type that can be used as a
1034 * condition. Give an error and return FAIL if not.
1035 */
1036 static int
1037bool_on_stack(cctx_T *cctx)
1038{
1039 garray_T *stack = &cctx->ctx_type_stack;
1040 type_T *type;
1041
1042 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1043 if (type == &t_bool)
1044 return OK;
1045
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001046 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001047 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1048 // This requires a runtime type check.
1049 return generate_COND2BOOL(cctx);
1050
Bram Moolenaar351ead02021-01-16 16:07:01 +01001051 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001052}
1053
1054/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 * Generate an ISN_PUSHNR instruction.
1056 */
1057 static int
1058generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1059{
1060 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001061 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062
Bram Moolenaar080457c2020-03-03 21:53:32 +01001063 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1065 return FAIL;
1066 isn->isn_arg.number = number;
1067
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001068 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001069 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001070 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071 return OK;
1072}
1073
1074/*
1075 * Generate an ISN_PUSHBOOL instruction.
1076 */
1077 static int
1078generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1079{
1080 isn_T *isn;
1081
Bram Moolenaar080457c2020-03-03 21:53:32 +01001082 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1084 return FAIL;
1085 isn->isn_arg.number = number;
1086
1087 return OK;
1088}
1089
1090/*
1091 * Generate an ISN_PUSHSPEC instruction.
1092 */
1093 static int
1094generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1095{
1096 isn_T *isn;
1097
Bram Moolenaar080457c2020-03-03 21:53:32 +01001098 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1100 return FAIL;
1101 isn->isn_arg.number = number;
1102
1103 return OK;
1104}
1105
1106#ifdef FEAT_FLOAT
1107/*
1108 * Generate an ISN_PUSHF instruction.
1109 */
1110 static int
1111generate_PUSHF(cctx_T *cctx, float_T fnumber)
1112{
1113 isn_T *isn;
1114
Bram Moolenaar080457c2020-03-03 21:53:32 +01001115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1117 return FAIL;
1118 isn->isn_arg.fnumber = fnumber;
1119
1120 return OK;
1121}
1122#endif
1123
1124/*
1125 * Generate an ISN_PUSHS instruction.
1126 * Consumes "str".
1127 */
1128 static int
1129generate_PUSHS(cctx_T *cctx, char_u *str)
1130{
1131 isn_T *isn;
1132
Bram Moolenaar508b5612021-01-02 18:17:26 +01001133 if (cctx->ctx_skip == SKIP_YES)
1134 {
1135 vim_free(str);
1136 return OK;
1137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1139 return FAIL;
1140 isn->isn_arg.string = str;
1141
1142 return OK;
1143}
1144
1145/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001146 * Generate an ISN_PUSHCHANNEL instruction.
1147 * Consumes "channel".
1148 */
1149 static int
1150generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1151{
1152 isn_T *isn;
1153
Bram Moolenaar080457c2020-03-03 21:53:32 +01001154 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001155 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1156 return FAIL;
1157 isn->isn_arg.channel = channel;
1158
1159 return OK;
1160}
1161
1162/*
1163 * Generate an ISN_PUSHJOB instruction.
1164 * Consumes "job".
1165 */
1166 static int
1167generate_PUSHJOB(cctx_T *cctx, job_T *job)
1168{
1169 isn_T *isn;
1170
Bram Moolenaar080457c2020-03-03 21:53:32 +01001171 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001172 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001173 return FAIL;
1174 isn->isn_arg.job = job;
1175
1176 return OK;
1177}
1178
1179/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 * Generate an ISN_PUSHBLOB instruction.
1181 * Consumes "blob".
1182 */
1183 static int
1184generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1185{
1186 isn_T *isn;
1187
Bram Moolenaar080457c2020-03-03 21:53:32 +01001188 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1190 return FAIL;
1191 isn->isn_arg.blob = blob;
1192
1193 return OK;
1194}
1195
1196/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001197 * Generate an ISN_PUSHFUNC instruction with name "name".
1198 * Consumes "name".
1199 */
1200 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001201generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001202{
1203 isn_T *isn;
1204
Bram Moolenaar080457c2020-03-03 21:53:32 +01001205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001206 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001207 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001208 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001209
1210 return OK;
1211}
1212
1213/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001214 * Generate an ISN_GETITEM instruction with "index".
1215 */
1216 static int
1217generate_GETITEM(cctx_T *cctx, int index)
1218{
1219 isn_T *isn;
1220 garray_T *stack = &cctx->ctx_type_stack;
1221 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1222 type_T *item_type = &t_any;
1223
1224 RETURN_OK_IF_SKIP(cctx);
1225
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001226 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001227 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001228 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001229 emsg(_(e_listreq));
1230 return FAIL;
1231 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001232 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001233 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1234 return FAIL;
1235 isn->isn_arg.number = index;
1236
1237 // add the item type to the type stack
1238 if (ga_grow(stack, 1) == FAIL)
1239 return FAIL;
1240 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1241 ++stack->ga_len;
1242 return OK;
1243}
1244
1245/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001246 * Generate an ISN_SLICE instruction with "count".
1247 */
1248 static int
1249generate_SLICE(cctx_T *cctx, int count)
1250{
1251 isn_T *isn;
1252
1253 RETURN_OK_IF_SKIP(cctx);
1254 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1255 return FAIL;
1256 isn->isn_arg.number = count;
1257 return OK;
1258}
1259
1260/*
1261 * Generate an ISN_CHECKLEN instruction with "min_len".
1262 */
1263 static int
1264generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1265{
1266 isn_T *isn;
1267
1268 RETURN_OK_IF_SKIP(cctx);
1269
1270 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1271 return FAIL;
1272 isn->isn_arg.checklen.cl_min_len = min_len;
1273 isn->isn_arg.checklen.cl_more_OK = more_OK;
1274
1275 return OK;
1276}
1277
1278/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 * Generate an ISN_STORE instruction.
1280 */
1281 static int
1282generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1283{
1284 isn_T *isn;
1285
Bram Moolenaar080457c2020-03-03 21:53:32 +01001286 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1288 return FAIL;
1289 if (name != NULL)
1290 isn->isn_arg.string = vim_strsave(name);
1291 else
1292 isn->isn_arg.number = idx;
1293
1294 return OK;
1295}
1296
1297/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001298 * Generate an ISN_STOREOUTER instruction.
1299 */
1300 static int
1301generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1302{
1303 isn_T *isn;
1304
1305 RETURN_OK_IF_SKIP(cctx);
1306 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1307 return FAIL;
1308 isn->isn_arg.outer.outer_idx = idx;
1309 isn->isn_arg.outer.outer_depth = level;
1310
1311 return OK;
1312}
1313
1314/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1316 */
1317 static int
1318generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1319{
1320 isn_T *isn;
1321
Bram Moolenaar080457c2020-03-03 21:53:32 +01001322 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1324 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001325 isn->isn_arg.storenr.stnr_idx = idx;
1326 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
1328 return OK;
1329}
1330
1331/*
1332 * Generate an ISN_STOREOPT instruction
1333 */
1334 static int
1335generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1336{
1337 isn_T *isn;
1338
Bram Moolenaar080457c2020-03-03 21:53:32 +01001339 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001340 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 return FAIL;
1342 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1343 isn->isn_arg.storeopt.so_flags = opt_flags;
1344
1345 return OK;
1346}
1347
1348/*
1349 * Generate an ISN_LOAD or similar instruction.
1350 */
1351 static int
1352generate_LOAD(
1353 cctx_T *cctx,
1354 isntype_T isn_type,
1355 int idx,
1356 char_u *name,
1357 type_T *type)
1358{
1359 isn_T *isn;
1360
Bram Moolenaar080457c2020-03-03 21:53:32 +01001361 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1363 return FAIL;
1364 if (name != NULL)
1365 isn->isn_arg.string = vim_strsave(name);
1366 else
1367 isn->isn_arg.number = idx;
1368
1369 return OK;
1370}
1371
1372/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001373 * Generate an ISN_LOADOUTER instruction
1374 */
1375 static int
1376generate_LOADOUTER(
1377 cctx_T *cctx,
1378 int idx,
1379 int nesting,
1380 type_T *type)
1381{
1382 isn_T *isn;
1383
1384 RETURN_OK_IF_SKIP(cctx);
1385 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1386 return FAIL;
1387 isn->isn_arg.outer.outer_idx = idx;
1388 isn->isn_arg.outer.outer_depth = nesting;
1389
1390 return OK;
1391}
1392
1393/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001394 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001395 */
1396 static int
1397generate_LOADV(
1398 cctx_T *cctx,
1399 char_u *name,
1400 int error)
1401{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001402 int di_flags;
1403 int vidx = find_vim_var(name, &di_flags);
1404 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001405
Bram Moolenaar080457c2020-03-03 21:53:32 +01001406 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001407 if (vidx < 0)
1408 {
1409 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001410 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001411 return FAIL;
1412 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001413 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001414
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001415 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001416}
1417
1418/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001419 * Generate an ISN_UNLET instruction.
1420 */
1421 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001422generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001423{
1424 isn_T *isn;
1425
1426 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001427 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001428 return FAIL;
1429 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1430 isn->isn_arg.unlet.ul_forceit = forceit;
1431
1432 return OK;
1433}
1434
1435/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001436 * Generate an ISN_LOCKCONST instruction.
1437 */
1438 static int
1439generate_LOCKCONST(cctx_T *cctx)
1440{
1441 isn_T *isn;
1442
1443 RETURN_OK_IF_SKIP(cctx);
1444 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1445 return FAIL;
1446 return OK;
1447}
1448
1449/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 * Generate an ISN_LOADS instruction.
1451 */
1452 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001453generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001455 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001457 int sid,
1458 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459{
1460 isn_T *isn;
1461
Bram Moolenaar080457c2020-03-03 21:53:32 +01001462 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001463 if (isn_type == ISN_LOADS)
1464 isn = generate_instr_type(cctx, isn_type, type);
1465 else
1466 isn = generate_instr_drop(cctx, isn_type, 1);
1467 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001469 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1470 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471
1472 return OK;
1473}
1474
1475/*
1476 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1477 */
1478 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001479generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 cctx_T *cctx,
1481 isntype_T isn_type,
1482 int sid,
1483 int idx,
1484 type_T *type)
1485{
1486 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001487 scriptref_T *sref;
1488 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489
Bram Moolenaar080457c2020-03-03 21:53:32 +01001490 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 if (isn_type == ISN_LOADSCRIPT)
1492 isn = generate_instr_type(cctx, isn_type, type);
1493 else
1494 isn = generate_instr_drop(cctx, isn_type, 1);
1495 if (isn == NULL)
1496 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001497
1498 // This requires three arguments, which doesn't fit in an instruction, thus
1499 // we need to allocate a struct for this.
1500 sref = ALLOC_ONE(scriptref_T);
1501 if (sref == NULL)
1502 return FAIL;
1503 isn->isn_arg.script.scriptref = sref;
1504 sref->sref_sid = sid;
1505 sref->sref_idx = idx;
1506 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001507 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 return OK;
1509}
1510
1511/*
1512 * Generate an ISN_NEWLIST instruction.
1513 */
1514 static int
1515generate_NEWLIST(cctx_T *cctx, int count)
1516{
1517 isn_T *isn;
1518 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 type_T *type;
1520 type_T *member;
1521
Bram Moolenaar080457c2020-03-03 21:53:32 +01001522 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1524 return FAIL;
1525 isn->isn_arg.number = count;
1526
Bram Moolenaar127542b2020-08-09 17:22:04 +02001527 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001528 if (count == 0)
1529 member = &t_void;
1530 else
1531 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001532 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1533 cctx->ctx_type_list);
1534 type = get_list_type(member, cctx->ctx_type_list);
1535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 // drop the value types
1537 stack->ga_len -= count;
1538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 // add the list type to the type stack
1540 if (ga_grow(stack, 1) == FAIL)
1541 return FAIL;
1542 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1543 ++stack->ga_len;
1544
1545 return OK;
1546}
1547
1548/*
1549 * Generate an ISN_NEWDICT instruction.
1550 */
1551 static int
1552generate_NEWDICT(cctx_T *cctx, int count)
1553{
1554 isn_T *isn;
1555 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 type_T *type;
1557 type_T *member;
1558
Bram Moolenaar080457c2020-03-03 21:53:32 +01001559 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1561 return FAIL;
1562 isn->isn_arg.number = count;
1563
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001564 if (count == 0)
1565 member = &t_void;
1566 else
1567 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001568 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1569 cctx->ctx_type_list);
1570 type = get_dict_type(member, cctx->ctx_type_list);
1571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 // drop the key and value types
1573 stack->ga_len -= 2 * count;
1574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 // add the dict type to the type stack
1576 if (ga_grow(stack, 1) == FAIL)
1577 return FAIL;
1578 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1579 ++stack->ga_len;
1580
1581 return OK;
1582}
1583
1584/*
1585 * Generate an ISN_FUNCREF instruction.
1586 */
1587 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001588generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589{
1590 isn_T *isn;
1591 garray_T *stack = &cctx->ctx_type_stack;
1592
Bram Moolenaar080457c2020-03-03 21:53:32 +01001593 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1595 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001596 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001597 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001599 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001600 // the nested context, including this one.
1601 if (ufunc->uf_flags & FC_CLOSURE)
1602 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 if (ga_grow(stack, 1) == FAIL)
1605 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001606 ((type_T **)stack->ga_data)[stack->ga_len] =
1607 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 ++stack->ga_len;
1609
1610 return OK;
1611}
1612
1613/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001614 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001615 * "lambda_name" and "func_name" must be in allocated memory and will be
1616 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001617 */
1618 static int
1619generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1620{
1621 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001622
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001623 if (cctx->ctx_skip == SKIP_YES)
1624 {
1625 vim_free(lambda_name);
1626 vim_free(func_name);
1627 return OK;
1628 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001629 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001630 {
1631 vim_free(lambda_name);
1632 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001633 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001634 }
1635 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001636 isn->isn_arg.newfunc.nf_global = func_name;
1637
1638 return OK;
1639}
1640
1641/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001642 * Generate an ISN_DEF instruction: list functions
1643 */
1644 static int
1645generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1646{
1647 isn_T *isn;
1648
1649 RETURN_OK_IF_SKIP(cctx);
1650 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1651 return FAIL;
1652 if (len > 0)
1653 {
1654 isn->isn_arg.string = vim_strnsave(name, len);
1655 if (isn->isn_arg.string == NULL)
1656 return FAIL;
1657 }
1658 return OK;
1659}
1660
1661/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 * Generate an ISN_JUMP instruction.
1663 */
1664 static int
1665generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1666{
1667 isn_T *isn;
1668 garray_T *stack = &cctx->ctx_type_stack;
1669
Bram Moolenaar080457c2020-03-03 21:53:32 +01001670 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1672 return FAIL;
1673 isn->isn_arg.jump.jump_when = when;
1674 isn->isn_arg.jump.jump_where = where;
1675
1676 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1677 --stack->ga_len;
1678
1679 return OK;
1680}
1681
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001682/*
1683 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1684 */
1685 static int
1686generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1687{
1688 isn_T *isn;
1689
1690 RETURN_OK_IF_SKIP(cctx);
1691 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1692 return FAIL;
1693 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1694 // jump_where is set later
1695 return OK;
1696}
1697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 static int
1699generate_FOR(cctx_T *cctx, int loop_idx)
1700{
1701 isn_T *isn;
1702 garray_T *stack = &cctx->ctx_type_stack;
1703
Bram Moolenaar080457c2020-03-03 21:53:32 +01001704 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1706 return FAIL;
1707 isn->isn_arg.forloop.for_idx = loop_idx;
1708
1709 if (ga_grow(stack, 1) == FAIL)
1710 return FAIL;
1711 // type doesn't matter, will be stored next
1712 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1713 ++stack->ga_len;
1714
1715 return OK;
1716}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001717/*
1718 * Generate an ISN_TRYCONT instruction.
1719 */
1720 static int
1721generate_TRYCONT(cctx_T *cctx, int levels, int where)
1722{
1723 isn_T *isn;
1724
1725 RETURN_OK_IF_SKIP(cctx);
1726 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1727 return FAIL;
1728 isn->isn_arg.trycont.tct_levels = levels;
1729 isn->isn_arg.trycont.tct_where = where;
1730
1731 return OK;
1732}
1733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734
1735/*
1736 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001737 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 * Return FAIL if the number of arguments is wrong.
1739 */
1740 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001741generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742{
1743 isn_T *isn;
1744 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001745 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001746 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001747 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748
Bram Moolenaar080457c2020-03-03 21:53:32 +01001749 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001750 argoff = check_internal_func(func_idx, argcount);
1751 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 return FAIL;
1753
Bram Moolenaar389df252020-07-09 21:20:47 +02001754 if (method_call && argoff > 1)
1755 {
1756 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1757 return FAIL;
1758 isn->isn_arg.shuffle.shfl_item = argcount;
1759 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1760 }
1761
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001762 if (argcount > 0)
1763 {
1764 // Check the types of the arguments.
1765 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001766 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1767 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001768 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001769 if (internal_func_is_map(func_idx))
1770 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001771 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1774 return FAIL;
1775 isn->isn_arg.bfunc.cbf_idx = func_idx;
1776 isn->isn_arg.bfunc.cbf_argcount = argcount;
1777
Bram Moolenaar94738d82020-10-21 14:25:07 +02001778 // Drop the argument types and push the return type.
1779 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 if (ga_grow(stack, 1) == FAIL)
1781 return FAIL;
1782 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001783 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001784 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001786 if (maptype != NULL && maptype->tt_member != NULL
1787 && maptype->tt_member != &t_any)
1788 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001789 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 return OK;
1792}
1793
1794/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001795 * Generate an ISN_LISTAPPEND instruction. Works like add().
1796 * Argument count is already checked.
1797 */
1798 static int
1799generate_LISTAPPEND(cctx_T *cctx)
1800{
1801 garray_T *stack = &cctx->ctx_type_stack;
1802 type_T *list_type;
1803 type_T *item_type;
1804 type_T *expected;
1805
1806 // Caller already checked that list_type is a list.
1807 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1808 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1809 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001810 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001811 return FAIL;
1812
1813 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1814 return FAIL;
1815
1816 --stack->ga_len; // drop the argument
1817 return OK;
1818}
1819
1820/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001821 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1822 * Argument count is already checked.
1823 */
1824 static int
1825generate_BLOBAPPEND(cctx_T *cctx)
1826{
1827 garray_T *stack = &cctx->ctx_type_stack;
1828 type_T *item_type;
1829
1830 // Caller already checked that blob_type is a blob.
1831 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001832 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001833 return FAIL;
1834
1835 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1836 return FAIL;
1837
1838 --stack->ga_len; // drop the argument
1839 return OK;
1840}
1841
1842/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001843 * Return TRUE if "ufunc" should be compiled, taking into account whether
1844 * "profile" indicates profiling is to be done.
1845 */
1846 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001847func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001848{
1849 switch (ufunc->uf_def_status)
1850 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001851 case UF_TO_BE_COMPILED:
1852 return TRUE;
1853
Bram Moolenaarb2049902021-01-24 12:53:53 +01001854 case UF_COMPILED:
1855 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001856#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001857 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1858 + ufunc->uf_dfunc_idx;
1859
1860 return profile ? dfunc->df_instr_prof == NULL
1861 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001862#else
1863 break;
1864#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001865 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001866
1867 case UF_NOT_COMPILED:
1868 case UF_COMPILE_ERROR:
1869 case UF_COMPILING:
1870 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001871 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001872 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001873}
1874
1875/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 * Generate an ISN_DCALL or ISN_UCALL instruction.
1877 * Return FAIL if the number of arguments is wrong.
1878 */
1879 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001880generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881{
1882 isn_T *isn;
1883 garray_T *stack = &cctx->ctx_type_stack;
1884 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001885 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886
Bram Moolenaar080457c2020-03-03 21:53:32 +01001887 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 if (argcount > regular_args && !has_varargs(ufunc))
1889 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001890 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 return FAIL;
1892 }
1893 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1894 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001895 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 return FAIL;
1897 }
1898
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001899 if (ufunc->uf_def_status != UF_NOT_COMPILED
1900 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001901 {
1902 int i;
1903
1904 for (i = 0; i < argcount; ++i)
1905 {
1906 type_T *expected;
1907 type_T *actual;
1908
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001909 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1910 if (actual == &t_special
1911 && i >= regular_args - ufunc->uf_def_args.ga_len)
1912 {
1913 // assume v:none used for default argument value
1914 continue;
1915 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001916 if (i < regular_args)
1917 {
1918 if (ufunc->uf_arg_types == NULL)
1919 continue;
1920 expected = ufunc->uf_arg_types[i];
1921 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001922 else if (ufunc->uf_va_type == NULL
1923 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001924 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001925 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001926 else
1927 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001928 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001929 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001930 {
1931 arg_type_mismatch(expected, actual, i + 1);
1932 return FAIL;
1933 }
1934 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001935 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001936 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001937 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001938 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001939 }
1940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001942 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001943 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001945 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 {
1947 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1948 isn->isn_arg.dfunc.cdf_argcount = argcount;
1949 }
1950 else
1951 {
1952 // A user function may be deleted and redefined later, can't use the
1953 // ufunc pointer, need to look it up again at runtime.
1954 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1955 isn->isn_arg.ufunc.cuf_argcount = argcount;
1956 }
1957
1958 stack->ga_len -= argcount; // drop the arguments
1959 if (ga_grow(stack, 1) == FAIL)
1960 return FAIL;
1961 // add return value
1962 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1963 ++stack->ga_len;
1964
1965 return OK;
1966}
1967
1968/*
1969 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1970 */
1971 static int
1972generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1973{
1974 isn_T *isn;
1975 garray_T *stack = &cctx->ctx_type_stack;
1976
Bram Moolenaar080457c2020-03-03 21:53:32 +01001977 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1979 return FAIL;
1980 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1981 isn->isn_arg.ufunc.cuf_argcount = argcount;
1982
1983 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001984 if (ga_grow(stack, 1) == FAIL)
1985 return FAIL;
1986 // add return value
1987 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1988 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989
1990 return OK;
1991}
1992
1993/*
1994 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001995 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 */
1997 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001998generate_PCALL(
1999 cctx_T *cctx,
2000 int argcount,
2001 char_u *name,
2002 type_T *type,
2003 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004{
2005 isn_T *isn;
2006 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002007 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008
Bram Moolenaar080457c2020-03-03 21:53:32 +01002009 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002010
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002011 if (type->tt_type == VAR_ANY)
2012 ret_type = &t_any;
2013 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002014 {
2015 if (type->tt_argcount != -1)
2016 {
2017 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2018
2019 if (argcount < type->tt_min_argcount - varargs)
2020 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002021 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002022 return FAIL;
2023 }
2024 if (!varargs && argcount > type->tt_argcount)
2025 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002026 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002027 return FAIL;
2028 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002029 if (type->tt_args != NULL)
2030 {
2031 int i;
2032
2033 for (i = 0; i < argcount; ++i)
2034 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002035 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002036 type_T *actual = ((type_T **)stack->ga_data)[
2037 stack->ga_len + offset];
2038 type_T *expected;
2039
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002040 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002041 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002042 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002043 else if (i >= type->tt_min_argcount
2044 && actual == &t_special)
2045 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002046 else
2047 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002048 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002049 cctx, TRUE, FALSE) == FAIL)
2050 {
2051 arg_type_mismatch(expected, actual, i + 1);
2052 return FAIL;
2053 }
2054 }
2055 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002056 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002057 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002058 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002059 else
2060 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002061 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002062 return FAIL;
2063 }
2064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2066 return FAIL;
2067 isn->isn_arg.pfunc.cpf_top = at_top;
2068 isn->isn_arg.pfunc.cpf_argcount = argcount;
2069
2070 stack->ga_len -= argcount; // drop the arguments
2071
2072 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002073 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002075 // If partial is above the arguments it must be cleared and replaced with
2076 // the return value.
2077 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2078 return FAIL;
2079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 return OK;
2081}
2082
2083/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002084 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 */
2086 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002087generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088{
2089 isn_T *isn;
2090 garray_T *stack = &cctx->ctx_type_stack;
2091 type_T *type;
2092
Bram Moolenaar080457c2020-03-03 21:53:32 +01002093 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002094 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002095 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002096 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002098 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002100 if (type->tt_type != VAR_DICT && type != &t_any)
2101 {
2102 emsg(_(e_dictreq));
2103 return FAIL;
2104 }
2105 // change dict type to dict member type
2106 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002107 {
2108 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2109 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111
2112 return OK;
2113}
2114
2115/*
2116 * Generate an ISN_ECHO instruction.
2117 */
2118 static int
2119generate_ECHO(cctx_T *cctx, int with_white, int count)
2120{
2121 isn_T *isn;
2122
Bram Moolenaar080457c2020-03-03 21:53:32 +01002123 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2125 return FAIL;
2126 isn->isn_arg.echo.echo_with_white = with_white;
2127 isn->isn_arg.echo.echo_count = count;
2128
2129 return OK;
2130}
2131
Bram Moolenaarad39c092020-02-26 18:23:43 +01002132/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002133 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002134 */
2135 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002136generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002137{
2138 isn_T *isn;
2139
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002140 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002141 return FAIL;
2142 isn->isn_arg.number = count;
2143
2144 return OK;
2145}
2146
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002147/*
2148 * Generate an ISN_PUT instruction.
2149 */
2150 static int
2151generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2152{
2153 isn_T *isn;
2154
2155 RETURN_OK_IF_SKIP(cctx);
2156 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2157 return FAIL;
2158 isn->isn_arg.put.put_regname = regname;
2159 isn->isn_arg.put.put_lnum = lnum;
2160 return OK;
2161}
2162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 static int
2164generate_EXEC(cctx_T *cctx, char_u *line)
2165{
2166 isn_T *isn;
2167
Bram Moolenaar080457c2020-03-03 21:53:32 +01002168 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2170 return FAIL;
2171 isn->isn_arg.string = vim_strsave(line);
2172 return OK;
2173}
2174
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002175 static int
2176generate_EXECCONCAT(cctx_T *cctx, int count)
2177{
2178 isn_T *isn;
2179
2180 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2181 return FAIL;
2182 isn->isn_arg.number = count;
2183 return OK;
2184}
2185
Bram Moolenaar08597872020-12-10 19:43:40 +01002186/*
2187 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2188 */
2189 static int
2190generate_RANGE(cctx_T *cctx, char_u *range)
2191{
2192 isn_T *isn;
2193 garray_T *stack = &cctx->ctx_type_stack;
2194
2195 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2196 return FAIL;
2197 isn->isn_arg.string = range;
2198
2199 if (ga_grow(stack, 1) == FAIL)
2200 return FAIL;
2201 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2202 ++stack->ga_len;
2203 return OK;
2204}
2205
Bram Moolenaar792f7862020-11-23 08:31:18 +01002206 static int
2207generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2208{
2209 isn_T *isn;
2210
2211 RETURN_OK_IF_SKIP(cctx);
2212 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2213 return FAIL;
2214 isn->isn_arg.unpack.unp_count = var_count;
2215 isn->isn_arg.unpack.unp_semicolon = semicolon;
2216 return OK;
2217}
2218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002220 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002221 */
2222 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002223generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002224{
2225 isn_T *isn;
2226
Bram Moolenaarfa984412021-03-25 22:15:28 +01002227 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002228 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002229 cctx->ctx_has_cmdmod = TRUE;
2230
2231 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002232 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002233 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2234 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2235 return FAIL;
2236 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002237 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002238 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002239 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002240
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002241 return OK;
2242}
2243
2244 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002245generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002246{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002247 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2248 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002249 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002250 return OK;
2251}
2252
Bram Moolenaarfa984412021-03-25 22:15:28 +01002253 static int
2254misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002255{
2256 garray_T *instr = &cctx->ctx_instr;
2257
Bram Moolenaara91a7132021-03-25 21:12:15 +01002258 if (cctx->ctx_has_cmdmod
2259 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2260 == ISN_CMDMOD)
2261 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002262 emsg(_(e_misplaced_command_modifier));
2263 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002264 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002265 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002266}
2267
2268/*
2269 * Get the index of the current instruction.
2270 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2271 */
2272 static int
2273current_instr_idx(cctx_T *cctx)
2274{
2275 garray_T *instr = &cctx->ctx_instr;
2276 int idx = instr->ga_len;
2277
2278 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2279 .isn_type == ISN_CMDMOD)
2280 --idx;
2281#ifdef FEAT_PROFILE
2282 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2283 .isn_type == ISN_PROF_START)
2284 --idx;
2285#endif
2286 return idx;
2287}
2288
Bram Moolenaarf002a412021-01-24 13:34:18 +01002289#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002290 static void
2291may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2292{
2293 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002294 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002295}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002296#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002297
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002298/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002300 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002302 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002303reserve_local(
2304 cctx_T *cctx,
2305 char_u *name,
2306 size_t len,
2307 int isConst,
2308 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 lvar_T *lvar;
2311
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002312 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002314 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002315 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 }
2317
2318 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002319 return NULL;
2320 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002321 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002323 // Every local variable uses the next entry on the stack. We could re-use
2324 // the last ones when leaving a scope, but then variables used in a closure
2325 // might get overwritten. To keep things simple do not re-use stack
2326 // entries. This is less efficient, but memory is cheap these days.
2327 lvar->lv_idx = cctx->ctx_locals_count++;
2328
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002329 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 lvar->lv_const = isConst;
2331 lvar->lv_type = type;
2332
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002333 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334}
2335
2336/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002337 * Remove local variables above "new_top".
2338 */
2339 static void
2340unwind_locals(cctx_T *cctx, int new_top)
2341{
2342 if (cctx->ctx_locals.ga_len > new_top)
2343 {
2344 int idx;
2345 lvar_T *lvar;
2346
2347 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2348 {
2349 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2350 vim_free(lvar->lv_name);
2351 }
2352 }
2353 cctx->ctx_locals.ga_len = new_top;
2354}
2355
2356/*
2357 * Free all local variables.
2358 */
2359 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002360free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002361{
2362 unwind_locals(cctx, 0);
2363 ga_clear(&cctx->ctx_locals);
2364}
2365
2366/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002367 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2368 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2369 * error if the variable was defined with :const.
2370 */
2371 static int
2372check_item_writable(svar_T *sv, int check_writable, char_u *name)
2373{
2374 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2375 || (check_writable == ASSIGN_FINAL
2376 && sv->sv_const == ASSIGN_CONST))
2377 {
2378 semsg(_(e_readonlyvar), name);
2379 return FAIL;
2380 }
2381 return OK;
2382}
2383
2384/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002386 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 * Returns the index in "sn_var_vals" if found.
2388 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002389 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 */
2391 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002392get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393{
2394 hashtab_T *ht;
2395 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002396 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002397 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 int idx;
2399
Bram Moolenaare3d46852020-08-29 13:39:17 +02002400 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002402 if (sid == current_sctx.sc_sid)
2403 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002404 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002405
2406 if (sav == NULL)
2407 return -2;
2408 idx = sav->sav_var_vals_idx;
2409 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002410 if (check_item_writable(sv, check_writable, name) == FAIL)
2411 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002412 return idx;
2413 }
2414
2415 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 ht = &SCRIPT_VARS(sid);
2417 di = find_var_in_ht(ht, 0, name, TRUE);
2418 if (di == NULL)
2419 return -2;
2420
2421 // Now find the svar_T index in sn_var_vals.
2422 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2423 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002424 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 if (sv->sv_tv == &di->di_tv)
2426 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002427 if (check_item_writable(sv, check_writable, name) == FAIL)
2428 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 return idx;
2430 }
2431 }
2432 return -1;
2433}
2434
2435/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002436 * Find "name" in imported items of the current script or in "cctx" if not
2437 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 */
2439 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002440find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 int idx;
2443
Bram Moolenaare3d46852020-08-29 13:39:17 +02002444 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002445 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 if (cctx != NULL)
2447 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2448 {
2449 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2450 + idx;
2451
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002452 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2453 : STRLEN(import->imp_name) == len
2454 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455 return import;
2456 }
2457
Bram Moolenaarefa94442020-08-08 22:16:00 +02002458 return find_imported_in_script(name, len, current_sctx.sc_sid);
2459}
2460
2461 imported_T *
2462find_imported_in_script(char_u *name, size_t len, int sid)
2463{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002464 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002465 int idx;
2466
Bram Moolenaare3d46852020-08-29 13:39:17 +02002467 if (!SCRIPT_ID_VALID(sid))
2468 return NULL;
2469 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2471 {
2472 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2473
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002474 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2475 : STRLEN(import->imp_name) == len
2476 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 return import;
2478 }
2479 return NULL;
2480}
2481
2482/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002483 * Free all imported variables.
2484 */
2485 static void
2486free_imported(cctx_T *cctx)
2487{
2488 int idx;
2489
2490 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2491 {
2492 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2493
2494 vim_free(import->imp_name);
2495 }
2496 ga_clear(&cctx->ctx_imports);
2497}
2498
2499/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002500 * Return a pointer to the next line that isn't empty or only contains a
2501 * comment. Skips over white space.
2502 * Returns NULL if there is none.
2503 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002504 char_u *
2505peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002506{
2507 int lnum = cctx->ctx_lnum;
2508
2509 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2510 {
2511 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002512 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002513
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002514 // ignore NULLs inserted for continuation lines
2515 if (line != NULL)
2516 {
2517 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002518 if (vim9_bad_comment(p))
2519 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002520 if (*p != NUL && !vim9_comment_start(p))
2521 return p;
2522 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002523 }
2524 return NULL;
2525}
2526
2527/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002528 * Called when checking for a following operator at "arg". When the rest of
2529 * the line is empty or only a comment, peek the next line. If there is a next
2530 * line return a pointer to it and set "nextp".
2531 * Otherwise skip over white space.
2532 */
2533 static char_u *
2534may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2535{
2536 char_u *p = skipwhite(arg);
2537
2538 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002539 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002540 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002541 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002542 if (*nextp != NULL)
2543 return *nextp;
2544 }
2545 return p;
2546}
2547
2548/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002549 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002550 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002551 * Returns NULL when at the end.
2552 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002553 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002554next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002555{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002556 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002557
2558 do
2559 {
2560 ++cctx->ctx_lnum;
2561 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002562 {
2563 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002564 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002565 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002566 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002567 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002568 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002569 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002570 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002571 return line;
2572}
2573
2574/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002575 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002576 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002577 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002578 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2579 */
2580 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002581may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002582{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002583 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002584 if (vim9_bad_comment(*arg))
2585 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002586 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002587 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002588 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002589
2590 if (next == NULL)
2591 return FAIL;
2592 *arg = skipwhite(next);
2593 }
2594 return OK;
2595}
2596
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002597/*
2598 * Idem, and give an error when failed.
2599 */
2600 static int
2601may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2602{
2603 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2604 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002605 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002606 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002607 return FAIL;
2608 }
2609 return OK;
2610}
2611
2612
Bram Moolenaara5565e42020-05-09 15:44:01 +02002613// Structure passed between the compile_expr* functions to keep track of
2614// constants that have been parsed but for which no code was produced yet. If
2615// possible expressions on these constants are applied at compile time. If
2616// that is not possible, the code to push the constants needs to be generated
2617// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002618// Using 50 should be more than enough of 5 levels of ().
2619#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002620typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002621 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002622 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002623 int pp_is_const; // all generated code was constants, used for a
2624 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002625} ppconst_T;
2626
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002627static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002628static int compile_expr0(char_u **arg, cctx_T *cctx);
2629static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2630
Bram Moolenaara5565e42020-05-09 15:44:01 +02002631/*
2632 * Generate a PUSH instruction for "tv".
2633 * "tv" will be consumed or cleared.
2634 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2635 */
2636 static int
2637generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2638{
2639 if (tv != NULL)
2640 {
2641 switch (tv->v_type)
2642 {
2643 case VAR_UNKNOWN:
2644 break;
2645 case VAR_BOOL:
2646 generate_PUSHBOOL(cctx, tv->vval.v_number);
2647 break;
2648 case VAR_SPECIAL:
2649 generate_PUSHSPEC(cctx, tv->vval.v_number);
2650 break;
2651 case VAR_NUMBER:
2652 generate_PUSHNR(cctx, tv->vval.v_number);
2653 break;
2654#ifdef FEAT_FLOAT
2655 case VAR_FLOAT:
2656 generate_PUSHF(cctx, tv->vval.v_float);
2657 break;
2658#endif
2659 case VAR_BLOB:
2660 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2661 tv->vval.v_blob = NULL;
2662 break;
2663 case VAR_STRING:
2664 generate_PUSHS(cctx, tv->vval.v_string);
2665 tv->vval.v_string = NULL;
2666 break;
2667 default:
2668 iemsg("constant type not supported");
2669 clear_tv(tv);
2670 return FAIL;
2671 }
2672 tv->v_type = VAR_UNKNOWN;
2673 }
2674 return OK;
2675}
2676
2677/*
2678 * Generate code for any ppconst entries.
2679 */
2680 static int
2681generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2682{
2683 int i;
2684 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002685 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002686
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002687 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002688 for (i = 0; i < ppconst->pp_used; ++i)
2689 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2690 ret = FAIL;
2691 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002692 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002693 return ret;
2694}
2695
2696/*
2697 * Clear ppconst constants. Used when failing.
2698 */
2699 static void
2700clear_ppconst(ppconst_T *ppconst)
2701{
2702 int i;
2703
2704 for (i = 0; i < ppconst->pp_used; ++i)
2705 clear_tv(&ppconst->pp_tv[i]);
2706 ppconst->pp_used = 0;
2707}
2708
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002709/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002710 * Compile getting a member from a list/dict/string/blob. Stack has the
2711 * indexable value and the index.
2712 */
2713 static int
2714compile_member(int is_slice, cctx_T *cctx)
2715{
2716 type_T **typep;
2717 garray_T *stack = &cctx->ctx_type_stack;
2718 vartype_T vtype;
2719 type_T *valtype;
2720
2721 // We can index a list and a dict. If we don't know the type
2722 // we can use the index value type.
2723 // TODO: If we don't know use an instruction to figure it out at
2724 // runtime.
2725 typep = ((type_T **)stack->ga_data) + stack->ga_len
2726 - (is_slice ? 3 : 2);
2727 vtype = (*typep)->tt_type;
2728 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2729 // If the index is a string, the variable must be a Dict.
2730 if (*typep == &t_any && valtype == &t_string)
2731 vtype = VAR_DICT;
2732 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
2733 {
2734 if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2735 return FAIL;
2736 if (is_slice)
2737 {
2738 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2739 if (need_type(valtype, &t_number, -2, 0, cctx,
2740 FALSE, FALSE) == FAIL)
2741 return FAIL;
2742 }
2743 }
2744
2745 if (vtype == VAR_DICT)
2746 {
2747 if (is_slice)
2748 {
2749 emsg(_(e_cannot_slice_dictionary));
2750 return FAIL;
2751 }
2752 if ((*typep)->tt_type == VAR_DICT)
2753 {
2754 *typep = (*typep)->tt_member;
2755 if (*typep == &t_unknown)
2756 // empty dict was used
2757 *typep = &t_any;
2758 }
2759 else
2760 {
2761 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2762 FALSE, FALSE) == FAIL)
2763 return FAIL;
2764 *typep = &t_any;
2765 }
2766 if (may_generate_2STRING(-1, cctx) == FAIL)
2767 return FAIL;
2768 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2769 return FAIL;
2770 }
2771 else if (vtype == VAR_STRING)
2772 {
2773 *typep = &t_string;
2774 if ((is_slice
2775 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2776 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2777 return FAIL;
2778 }
2779 else if (vtype == VAR_BLOB)
2780 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002781 if (is_slice)
2782 {
2783 *typep = &t_blob;
2784 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2785 return FAIL;
2786 }
2787 else
2788 {
2789 *typep = &t_number;
2790 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2791 return FAIL;
2792 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002793 }
2794 else if (vtype == VAR_LIST || *typep == &t_any)
2795 {
2796 if (is_slice)
2797 {
2798 if (generate_instr_drop(cctx,
2799 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2800 2) == FAIL)
2801 return FAIL;
2802 }
2803 else
2804 {
2805 if ((*typep)->tt_type == VAR_LIST)
2806 {
2807 *typep = (*typep)->tt_member;
2808 if (*typep == &t_unknown)
2809 // empty list was used
2810 *typep = &t_any;
2811 }
2812 if (generate_instr_drop(cctx,
2813 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
2814 return FAIL;
2815 }
2816 }
2817 else
2818 {
2819 emsg(_(e_string_list_dict_or_blob_required));
2820 return FAIL;
2821 }
2822 return OK;
2823}
2824
2825/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002826 * Generate an instruction to load script-local variable "name", without the
2827 * leading "s:".
2828 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 */
2830 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002831compile_load_scriptvar(
2832 cctx_T *cctx,
2833 char_u *name, // variable NUL terminated
2834 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002835 char_u **end, // end of variable
2836 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002838 scriptitem_T *si;
2839 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 imported_T *import;
2841
Bram Moolenaare3d46852020-08-29 13:39:17 +02002842 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2843 return FAIL;
2844 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002845 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002846 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002848 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002849 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2850 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 }
2852 if (idx >= 0)
2853 {
2854 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2855
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002856 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 current_sctx.sc_sid, idx, sv->sv_type);
2858 return OK;
2859 }
2860
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002861 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 if (import != NULL)
2863 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002864 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002865 {
2866 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002867 char_u *exp_name;
2868 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002869 ufunc_T *ufunc;
2870 type_T *type;
2871
2872 // Used "import * as Name", need to lookup the member.
2873 if (*p != '.')
2874 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002875 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002876 return FAIL;
2877 }
2878 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002879 if (VIM_ISWHITE(*p))
2880 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002881 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002882 return FAIL;
2883 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002884
Bram Moolenaar1c991142020-07-04 13:15:31 +02002885 // isolate one name
2886 exp_name = p;
2887 while (eval_isnamec(*p))
2888 ++p;
2889 cc = *p;
2890 *p = NUL;
2891
Bram Moolenaaredba7072021-03-13 21:14:18 +01002892 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2893 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002894 *p = cc;
2895 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002896 *end = p;
2897
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002898 if (idx < 0)
2899 {
2900 if (*p == '(' && ufunc != NULL)
2901 {
2902 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2903 return OK;
2904 }
2905 return FAIL;
2906 }
2907
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002908 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2909 import->imp_sid,
2910 idx,
2911 type);
2912 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002913 else if (import->imp_funcname != NULL)
2914 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002915 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002916 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2917 import->imp_sid,
2918 import->imp_var_vals_idx,
2919 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 return OK;
2921 }
2922
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002923 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002924 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 return FAIL;
2926}
2927
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002928 static int
2929generate_funcref(cctx_T *cctx, char_u *name)
2930{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002931 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002932
2933 if (ufunc == NULL)
2934 return FAIL;
2935
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002936 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002937 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2938 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002939 == FAIL)
2940 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002941 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002942}
2943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944/*
2945 * Compile a variable name into a load instruction.
2946 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002947 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 * When "error" is FALSE do not give an error when not found.
2949 */
2950 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002951compile_load(
2952 char_u **arg,
2953 char_u *end_arg,
2954 cctx_T *cctx,
2955 int is_expr,
2956 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957{
2958 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002959 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002960 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002962 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963
2964 if (*(*arg + 1) == ':')
2965 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002966 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002967 {
2968 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969
Bram Moolenaarfa596382021-04-07 21:58:16 +02002970 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002971 switch (**arg)
2972 {
2973 case 'g': isn_type = ISN_LOADGDICT; break;
2974 case 'w': isn_type = ISN_LOADWDICT; break;
2975 case 't': isn_type = ISN_LOADTDICT; break;
2976 case 'b': isn_type = ISN_LOADBDICT; break;
2977 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002978 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002979 goto theend;
2980 }
2981 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2982 goto theend;
2983 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002984 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 else
2986 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002987 isntype_T isn_type = ISN_DROP;
2988
Bram Moolenaarfa596382021-04-07 21:58:16 +02002989 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002990 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2991 if (name == NULL)
2992 return FAIL;
2993
2994 switch (**arg)
2995 {
2996 case 'v': res = generate_LOADV(cctx, name, error);
2997 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02002998 case 's': if (is_expr && ASCII_ISUPPER(*name)
2999 && find_func(name, FALSE, cctx) != NULL)
3000 res = generate_funcref(cctx, name);
3001 else
3002 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003003 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003004 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003005 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003006 {
3007 if (is_expr && ASCII_ISUPPER(*name)
3008 && find_func(name, FALSE, cctx) != NULL)
3009 res = generate_funcref(cctx, name);
3010 else
3011 isn_type = ISN_LOADG;
3012 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003013 else
3014 {
3015 isn_type = ISN_LOADAUTO;
3016 vim_free(name);
3017 name = vim_strnsave(*arg, end - *arg);
3018 if (name == NULL)
3019 return FAIL;
3020 }
3021 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003022 case 'w': isn_type = ISN_LOADW; break;
3023 case 't': isn_type = ISN_LOADT; break;
3024 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003025 default: // cannot happen, just in case
3026 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003027 goto theend;
3028 }
3029 if (isn_type != ISN_DROP)
3030 {
3031 // Global, Buffer-local, Window-local and Tabpage-local
3032 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003033 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003034 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 }
3037 }
3038 else
3039 {
3040 size_t len = end - *arg;
3041 int idx;
3042 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003043 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044
3045 name = vim_strnsave(*arg, end - *arg);
3046 if (name == NULL)
3047 return FAIL;
3048
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003049 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003051 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003052 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 }
3054 else
3055 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003056 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003057
Bram Moolenaar709664c2020-12-12 14:33:41 +01003058 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003060 type = lvar.lv_type;
3061 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003062 if (lvar.lv_from_outer != 0)
3063 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003064 else
3065 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 }
3067 else
3068 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003069 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003070 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003071 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003072 || find_imported(name, 0, cctx) != NULL)
3073 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003074
Bram Moolenaar0f769812020-09-12 18:32:34 +02003075 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003076 // uppercase letter it can be a user defined function.
3077 // generate_funcref() will fail if the function can't be found.
3078 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003079 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 }
3081 }
3082 if (gen_load)
3083 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003084 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003085 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003086 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003087 cctx->ctx_outer_used = TRUE;
3088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 }
3090
3091 *arg = end;
3092
3093theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003094 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003095 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 vim_free(name);
3097 return res;
3098}
3099
3100/*
3101 * Compile the argument expressions.
3102 * "arg" points to just after the "(" and is advanced to after the ")"
3103 */
3104 static int
3105compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3106{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003107 char_u *p = *arg;
3108 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003109 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110
Bram Moolenaare6085c52020-04-12 20:19:16 +02003111 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003113 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3114 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003115 if (*p == ')')
3116 {
3117 *arg = p + 1;
3118 return OK;
3119 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003120 if (must_end)
3121 {
3122 semsg(_(e_missing_comma_before_argument_str), p);
3123 return FAIL;
3124 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003125
Bram Moolenaara5565e42020-05-09 15:44:01 +02003126 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 return FAIL;
3128 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003129
3130 if (*p != ',' && *skipwhite(p) == ',')
3131 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003132 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003133 p = skipwhite(p);
3134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003136 {
3137 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003138 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003139 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003140 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003141 else
3142 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003143 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003144 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003146failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003147 emsg(_(e_missing_close));
3148 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149}
3150
3151/*
3152 * Compile a function call: name(arg1, arg2)
3153 * "arg" points to "name", "arg + varlen" to the "(".
3154 * "argcount_init" is 1 for "value->method()"
3155 * Instructions:
3156 * EVAL arg1
3157 * EVAL arg2
3158 * BCALL / DCALL / UCALL
3159 */
3160 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003161compile_call(
3162 char_u **arg,
3163 size_t varlen,
3164 cctx_T *cctx,
3165 ppconst_T *ppconst,
3166 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167{
3168 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003169 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 int argcount = argcount_init;
3171 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003172 char_u fname_buf[FLEN_FIXED + 1];
3173 char_u *tofree = NULL;
3174 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003175 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003176 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003177 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178
Bram Moolenaara5565e42020-05-09 15:44:01 +02003179 // we can evaluate "has('name')" at compile time
3180 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3181 {
3182 char_u *s = skipwhite(*arg + varlen + 1);
3183 typval_T argvars[2];
3184
3185 argvars[0].v_type = VAR_UNKNOWN;
3186 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003187 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003188 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003189 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003190 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003191 if (*s == ')' && argvars[0].v_type == VAR_STRING
3192 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003193 {
3194 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3195
3196 *arg = s + 1;
3197 argvars[1].v_type = VAR_UNKNOWN;
3198 tv->v_type = VAR_NUMBER;
3199 tv->vval.v_number = 0;
3200 f_has(argvars, tv);
3201 clear_tv(&argvars[0]);
3202 ++ppconst->pp_used;
3203 return OK;
3204 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003205 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003206 }
3207
3208 if (generate_ppconst(cctx, ppconst) == FAIL)
3209 return FAIL;
3210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 if (varlen >= sizeof(namebuf))
3212 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003213 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 return FAIL;
3215 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003216 vim_strncpy(namebuf, *arg, varlen);
3217 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218
3219 *arg = skipwhite(*arg + varlen + 1);
3220 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003221 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222
Bram Moolenaar03290b82020-12-19 16:30:44 +01003223 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003224 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 {
3226 int idx;
3227
3228 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003229 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003231 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003232 if (STRCMP(name, "flatten") == 0)
3233 {
3234 emsg(_(e_cannot_use_flatten_in_vim9_script));
3235 goto theend;
3236 }
3237
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003238 if (STRCMP(name, "add") == 0 && argcount == 2)
3239 {
3240 garray_T *stack = &cctx->ctx_type_stack;
3241 type_T *type = ((type_T **)stack->ga_data)[
3242 stack->ga_len - 2];
3243
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003244 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003245 if (type->tt_type == VAR_LIST)
3246 {
3247 // inline "add(list, item)" so that the type can be checked
3248 res = generate_LISTAPPEND(cctx);
3249 idx = -1;
3250 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003251 else if (type->tt_type == VAR_BLOB)
3252 {
3253 // inline "add(blob, nr)" so that the type can be checked
3254 res = generate_BLOBAPPEND(cctx);
3255 idx = -1;
3256 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003257 }
3258
3259 if (idx >= 0)
3260 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3261 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003262 else
3263 semsg(_(e_unknownfunc), namebuf);
3264 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 }
3266
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003267 // An argument or local variable can be a function reference, this
3268 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003269 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003270 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003271 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003272 // If we can find the function by name generate the right call.
3273 // Skip global functions here, a local funcref takes precedence.
3274 ufunc = find_func(name, FALSE, cctx);
3275 if (ufunc != NULL && !func_is_global(ufunc))
3276 {
3277 res = generate_CALL(cctx, ufunc, argcount);
3278 goto theend;
3279 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281
3282 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003283 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003284 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003286 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003287 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003288 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003289 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003290 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003291
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003292 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003293 goto theend;
3294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295
Bram Moolenaar0f769812020-09-12 18:32:34 +02003296 // If we can find a global function by name generate the right call.
3297 if (ufunc != NULL)
3298 {
3299 res = generate_CALL(cctx, ufunc, argcount);
3300 goto theend;
3301 }
3302
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003303 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003304 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003305 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003306 res = generate_UCALL(cctx, name, argcount);
3307 else
3308 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003309
3310theend:
3311 vim_free(tofree);
3312 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313}
3314
3315// like NAMESPACE_CHAR but with 'a' and 'l'.
3316#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3317
3318/*
3319 * Find the end of a variable or function name. Unlike find_name_end() this
3320 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003321 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 * Return a pointer to just after the name. Equal to "arg" if there is no
3323 * valid name.
3324 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003325 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003326to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327{
3328 char_u *p;
3329
3330 // Quick check for valid starting character.
3331 if (!eval_isnamec1(*arg))
3332 return arg;
3333
3334 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3335 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3336 // and can be used in slice "[n:]".
3337 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003338 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3340 break;
3341 return p;
3342}
3343
3344/*
3345 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003346 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 */
3348 char_u *
3349to_name_const_end(char_u *arg)
3350{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003351 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 typval_T rettv;
3353
3354 if (p == arg && *arg == '[')
3355 {
3356
3357 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003358 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 p = arg;
3360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 return p;
3362}
3363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364/*
3365 * parse a list: [expr, expr]
3366 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003367 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 */
3369 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003370compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371{
3372 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003373 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003375 int is_const;
3376 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003378 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003380 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003381 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003382 semsg(_(e_list_end), *arg);
3383 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003384 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003385 if (*p == ',')
3386 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003387 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003388 return FAIL;
3389 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003390 if (*p == ']')
3391 {
3392 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003393 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003394 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003395 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003396 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003397 if (!is_const)
3398 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 ++count;
3400 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003401 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003403 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3404 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003405 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003406 return FAIL;
3407 }
3408 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003409 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 p = skipwhite(p);
3411 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003412 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003414 ppconst->pp_is_const = is_all_const;
3415 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416}
3417
3418/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003419 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003420 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003421 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 */
3423 static int
3424compile_lambda(char_u **arg, cctx_T *cctx)
3425{
Bram Moolenaare462f522020-12-27 14:43:30 +01003426 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 typval_T rettv;
3428 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003429 evalarg_T evalarg;
3430
3431 CLEAR_FIELD(evalarg);
3432 evalarg.eval_flags = EVAL_EVALUATE;
3433 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434
3435 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003436 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3437 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003438 {
3439 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003440 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003441 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003442
Bram Moolenaar65c44152020-12-24 15:14:01 +01003443 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003445 ++ufunc->uf_refcount;
3446 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447
Bram Moolenaar65c44152020-12-24 15:14:01 +01003448 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003449 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003451 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3452 // points into it. Point to the original line to avoid a dangling pointer.
3453 if (evalarg.eval_tofree_cmdline != NULL)
3454 {
3455 size_t off = *arg - evalarg.eval_tofree_cmdline;
3456
3457 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3458 + off;
3459 }
3460
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003461 clear_evalarg(&evalarg, NULL);
3462
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003463 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003464 {
3465 // The return type will now be known.
3466 set_function_type(ufunc);
3467
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003468 // The function reference count will be 1. When the ISN_FUNCREF
3469 // instruction is deleted the reference count is decremented and the
3470 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003471 return generate_FUNCREF(cctx, ufunc);
3472 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003473
3474 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 return FAIL;
3476}
3477
3478/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003479 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003481 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 */
3483 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003484compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485{
3486 garray_T *instr = &cctx->ctx_instr;
3487 int count = 0;
3488 dict_T *d = dict_alloc();
3489 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003490 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003491 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003492 int is_const;
3493 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494
3495 if (d == NULL)
3496 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003497 if (generate_ppconst(cctx, ppconst) == FAIL)
3498 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003499 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003501 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502
Bram Moolenaar23c55272020-06-21 16:58:13 +02003503 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003504 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003505 *arg = NULL;
3506 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003507 }
3508
3509 if (**arg == '}')
3510 break;
3511
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003512 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003514 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003516 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003517 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003518 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003521 if (isn->isn_type == ISN_PUSHNR)
3522 {
3523 char buf[NUMBUFLEN];
3524
3525 // Convert to string at compile time.
3526 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3527 isn->isn_type = ISN_PUSHS;
3528 isn->isn_arg.string = vim_strsave((char_u *)buf);
3529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 if (isn->isn_type == ISN_PUSHS)
3531 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003532 else if (may_generate_2STRING(-1, cctx) == FAIL)
3533 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003534 *arg = skipwhite(*arg);
3535 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003536 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003537 emsg(_(e_missing_matching_bracket_after_dict_key));
3538 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003539 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003540 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003542 else
3543 {
3544 // {"name": value},
3545 // {'name': value},
3546 // {name: value} use "name" as a literal key
3547 key = get_literal_key(arg);
3548 if (key == NULL)
3549 return FAIL;
3550 if (generate_PUSHS(cctx, key) == FAIL)
3551 return FAIL;
3552 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553
3554 // Check for duplicate keys, if using string keys.
3555 if (key != NULL)
3556 {
3557 item = dict_find(d, key, -1);
3558 if (item != NULL)
3559 {
3560 semsg(_(e_duplicate_key), key);
3561 goto failret;
3562 }
3563 item = dictitem_alloc(key);
3564 if (item != NULL)
3565 {
3566 item->di_tv.v_type = VAR_UNKNOWN;
3567 item->di_tv.v_lock = 0;
3568 if (dict_add(d, item) == FAIL)
3569 dictitem_free(item);
3570 }
3571 }
3572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 if (**arg != ':')
3574 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003575 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003576 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003577 else
3578 semsg(_(e_missing_dict_colon), *arg);
3579 return FAIL;
3580 }
3581 whitep = *arg + 1;
3582 if (!IS_WHITE_OR_NUL(*whitep))
3583 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003584 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 return FAIL;
3586 }
3587
Bram Moolenaar23c55272020-06-21 16:58:13 +02003588 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003589 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003590 *arg = NULL;
3591 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003592 }
3593
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003594 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003596 if (!is_const)
3597 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 ++count;
3599
Bram Moolenaar2c330432020-04-13 14:41:35 +02003600 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003601 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003602 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003603 *arg = NULL;
3604 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 if (**arg == '}')
3607 break;
3608 if (**arg != ',')
3609 {
3610 semsg(_(e_missing_dict_comma), *arg);
3611 goto failret;
3612 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003613 if (IS_WHITE_OR_NUL(*whitep))
3614 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003615 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003616 return FAIL;
3617 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003618 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003619 if (!IS_WHITE_OR_NUL(*whitep))
3620 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003621 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003622 return FAIL;
3623 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003624 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 }
3626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 *arg = *arg + 1;
3628
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003629 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003630 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003631 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003632 *arg += STRLEN(*arg);
3633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003635 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 return generate_NEWDICT(cctx, count);
3637
3638failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003639 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003640 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003641 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003642 *arg = (char_u *)"";
3643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 dict_unref(d);
3645 return FAIL;
3646}
3647
3648/*
3649 * Compile "&option".
3650 */
3651 static int
3652compile_get_option(char_u **arg, cctx_T *cctx)
3653{
3654 typval_T rettv;
3655 char_u *start = *arg;
3656 int ret;
3657
3658 // parse the option and get the current value to get the type.
3659 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003660 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 if (ret == OK)
3662 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003663 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003664 char_u *name = vim_strnsave(start, *arg - start);
3665 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3666 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667
3668 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3669 vim_free(name);
3670 }
3671 clear_tv(&rettv);
3672
3673 return ret;
3674}
3675
3676/*
3677 * Compile "$VAR".
3678 */
3679 static int
3680compile_get_env(char_u **arg, cctx_T *cctx)
3681{
3682 char_u *start = *arg;
3683 int len;
3684 int ret;
3685 char_u *name;
3686
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 ++*arg;
3688 len = get_env_len(arg);
3689 if (len == 0)
3690 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003691 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 return FAIL;
3693 }
3694
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003695 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 name = vim_strnsave(start, len + 1);
3697 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3698 vim_free(name);
3699 return ret;
3700}
3701
3702/*
3703 * Compile "@r".
3704 */
3705 static int
3706compile_get_register(char_u **arg, cctx_T *cctx)
3707{
3708 int ret;
3709
3710 ++*arg;
3711 if (**arg == NUL)
3712 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003713 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 return FAIL;
3715 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003716 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 {
3718 emsg_invreg(**arg);
3719 return FAIL;
3720 }
3721 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3722 ++*arg;
3723 return ret;
3724}
3725
3726/*
3727 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003728 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 */
3730 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003731apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003733 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734
3735 // this works from end to start
3736 while (p > start)
3737 {
3738 --p;
3739 if (*p == '-' || *p == '+')
3740 {
3741 // only '-' has an effect, for '+' we only check the type
3742#ifdef FEAT_FLOAT
3743 if (rettv->v_type == VAR_FLOAT)
3744 {
3745 if (*p == '-')
3746 rettv->vval.v_float = -rettv->vval.v_float;
3747 }
3748 else
3749#endif
3750 {
3751 varnumber_T val;
3752 int error = FALSE;
3753
3754 // tv_get_number_chk() accepts a string, but we don't want that
3755 // here
3756 if (check_not_string(rettv) == FAIL)
3757 return FAIL;
3758 val = tv_get_number_chk(rettv, &error);
3759 clear_tv(rettv);
3760 if (error)
3761 return FAIL;
3762 if (*p == '-')
3763 val = -val;
3764 rettv->v_type = VAR_NUMBER;
3765 rettv->vval.v_number = val;
3766 }
3767 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003768 else if (numeric_only)
3769 {
3770 ++p;
3771 break;
3772 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003773 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 {
3775 int v = tv2bool(rettv);
3776
3777 // '!' is permissive in the type.
3778 clear_tv(rettv);
3779 rettv->v_type = VAR_BOOL;
3780 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3781 }
3782 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003783 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 return OK;
3785}
3786
3787/*
3788 * Recognize v: variables that are constants and set "rettv".
3789 */
3790 static void
3791get_vim_constant(char_u **arg, typval_T *rettv)
3792{
3793 if (STRNCMP(*arg, "v:true", 6) == 0)
3794 {
3795 rettv->v_type = VAR_BOOL;
3796 rettv->vval.v_number = VVAL_TRUE;
3797 *arg += 6;
3798 }
3799 else if (STRNCMP(*arg, "v:false", 7) == 0)
3800 {
3801 rettv->v_type = VAR_BOOL;
3802 rettv->vval.v_number = VVAL_FALSE;
3803 *arg += 7;
3804 }
3805 else if (STRNCMP(*arg, "v:null", 6) == 0)
3806 {
3807 rettv->v_type = VAR_SPECIAL;
3808 rettv->vval.v_number = VVAL_NULL;
3809 *arg += 6;
3810 }
3811 else if (STRNCMP(*arg, "v:none", 6) == 0)
3812 {
3813 rettv->v_type = VAR_SPECIAL;
3814 rettv->vval.v_number = VVAL_NONE;
3815 *arg += 6;
3816 }
3817}
3818
Bram Moolenaar657137c2021-01-09 15:45:23 +01003819 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003820get_compare_type(char_u *p, int *len, int *type_is)
3821{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003822 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003823 int i;
3824
3825 switch (p[0])
3826 {
3827 case '=': if (p[1] == '=')
3828 type = EXPR_EQUAL;
3829 else if (p[1] == '~')
3830 type = EXPR_MATCH;
3831 break;
3832 case '!': if (p[1] == '=')
3833 type = EXPR_NEQUAL;
3834 else if (p[1] == '~')
3835 type = EXPR_NOMATCH;
3836 break;
3837 case '>': if (p[1] != '=')
3838 {
3839 type = EXPR_GREATER;
3840 *len = 1;
3841 }
3842 else
3843 type = EXPR_GEQUAL;
3844 break;
3845 case '<': if (p[1] != '=')
3846 {
3847 type = EXPR_SMALLER;
3848 *len = 1;
3849 }
3850 else
3851 type = EXPR_SEQUAL;
3852 break;
3853 case 'i': if (p[1] == 's')
3854 {
3855 // "is" and "isnot"; but not a prefix of a name
3856 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3857 *len = 5;
3858 i = p[*len];
3859 if (!isalnum(i) && i != '_')
3860 {
3861 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3862 *type_is = TRUE;
3863 }
3864 }
3865 break;
3866 }
3867 return type;
3868}
3869
3870/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003871 * Skip over an expression, ignoring most errors.
3872 */
3873 static void
3874skip_expr_cctx(char_u **arg, cctx_T *cctx)
3875{
3876 evalarg_T evalarg;
3877
3878 CLEAR_FIELD(evalarg);
3879 evalarg.eval_cctx = cctx;
3880 skip_expr(arg, &evalarg);
3881}
3882
3883/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003885 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 */
3887 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003888compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003890 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891
3892 // this works from end to start
3893 while (p > start)
3894 {
3895 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003896 while (VIM_ISWHITE(*p))
3897 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 if (*p == '-' || *p == '+')
3899 {
3900 int negate = *p == '-';
3901 isn_T *isn;
3902
3903 // TODO: check type
3904 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3905 {
3906 --p;
3907 if (*p == '-')
3908 negate = !negate;
3909 }
3910 // only '-' has an effect, for '+' we only check the type
3911 if (negate)
3912 isn = generate_instr(cctx, ISN_NEGATENR);
3913 else
3914 isn = generate_instr(cctx, ISN_CHECKNR);
3915 if (isn == NULL)
3916 return FAIL;
3917 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003918 else if (numeric_only)
3919 {
3920 ++p;
3921 break;
3922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 else
3924 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003925 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003927 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003929 if (p[-1] == '!')
3930 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 }
3933 if (generate_2BOOL(cctx, invert) == FAIL)
3934 return FAIL;
3935 }
3936 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003937 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 return OK;
3939}
3940
3941/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003942 * Compile "(expression)": recursive!
3943 * Return FAIL/OK.
3944 */
3945 static int
3946compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3947{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003948 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003949 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003950
Bram Moolenaar24156692021-01-14 20:35:49 +01003951 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3952 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003953 if (ppconst->pp_used <= PPSIZE - 10)
3954 {
3955 ret = compile_expr1(arg, cctx, ppconst);
3956 }
3957 else
3958 {
3959 // Not enough space in ppconst, flush constants.
3960 if (generate_ppconst(cctx, ppconst) == FAIL)
3961 return FAIL;
3962 ret = compile_expr0(arg, cctx);
3963 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003964 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3965 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003966 if (**arg == ')')
3967 ++*arg;
3968 else if (ret == OK)
3969 {
3970 emsg(_(e_missing_close));
3971 ret = FAIL;
3972 }
3973 return ret;
3974}
3975
3976/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003978 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 */
3980 static int
3981compile_subscript(
3982 char_u **arg,
3983 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003984 char_u *start_leader,
3985 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003986 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003988 char_u *name_start = *end_leader;
3989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 for (;;)
3991 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003992 char_u *p = skipwhite(*arg);
3993
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003994 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003995 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003996 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003997
3998 // If a following line starts with "->{" or "->X" advance to that
3999 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004000 // Also if a following line starts with ".x".
4001 if (next != NULL &&
4002 ((next[0] == '-' && next[1] == '>'
4003 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004004 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004005 {
4006 next = next_line_from_context(cctx, TRUE);
4007 if (next == NULL)
4008 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004009 *arg = next;
4010 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004011 }
4012 }
4013
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004014 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004015 // not a function call.
4016 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004018 garray_T *stack = &cctx->ctx_type_stack;
4019 type_T *type;
4020 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004022 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004023 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004024 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004027 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4028
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004029 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4031 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004032 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 return FAIL;
4034 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004035 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004037 char_u *pstart = p;
4038
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004039 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004040 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004041 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 // something->method()
4044 // Apply the '!', '-' and '+' first:
4045 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004046 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004049 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004050 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004051 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004052 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004053 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004054 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004055 garray_T *stack = &cctx->ctx_type_stack;
4056 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004057 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004058 int expr_isn_start = cctx->ctx_instr.ga_len;
4059 int expr_isn_end;
4060 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004061
4062 // Funcref call: list->(Refs[2])(arg)
4063 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004064 //
4065 // Fist compile the function expression.
4066 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004067 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004068
4069 // Remember the next instruction index, where the instructions
4070 // for arguments are being written.
4071 expr_isn_end = cctx->ctx_instr.ga_len;
4072
4073 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004074 if (**arg != '(')
4075 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004076 if (*skipwhite(*arg) == '(')
4077 emsg(_(e_nowhitespace));
4078 else
4079 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004080 return FAIL;
4081 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004082 *arg = skipwhite(*arg + 1);
4083 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4084 return FAIL;
4085
Bram Moolenaar2927c072021-04-05 19:41:21 +02004086 // Move the instructions for the arguments to before the
4087 // instructions of the expression and move the type of the
4088 // expression after the argument types. This is what ISN_PCALL
4089 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004090 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004091 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4092 if (arg_isn_count > 0)
4093 {
4094 int expr_isn_count = expr_isn_end - expr_isn_start;
4095 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4096
4097 if (isn == NULL)
4098 return FAIL;
4099 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4100 + expr_isn_start,
4101 sizeof(isn_T) * expr_isn_count);
4102 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4103 + expr_isn_start,
4104 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4105 sizeof(isn_T) * arg_isn_count);
4106 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4107 + expr_isn_start + arg_isn_count,
4108 isn, sizeof(isn_T) * expr_isn_count);
4109 vim_free(isn);
4110
4111 type = ((type_T **)stack->ga_data)[type_idx_start];
4112 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4113 ((type_T **)stack->ga_data) + type_idx_start + 1,
4114 sizeof(type_T *)
4115 * (stack->ga_len - type_idx_start - 1));
4116 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4117 }
4118
Bram Moolenaar7e368202020-12-25 21:56:57 +01004119 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4120 if (generate_PCALL(cctx, argcount,
4121 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 return FAIL;
4123 }
4124 else
4125 {
4126 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004127 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004128 if (!eval_isnamec1(*p))
4129 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004130 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004131 return FAIL;
4132 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004133 if (ASCII_ISALPHA(*p) && p[1] == ':')
4134 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004135 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 ;
4137 if (*p != '(')
4138 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004139 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 return FAIL;
4141 }
4142 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004143 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 return FAIL;
4145 }
4146 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004147 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004149 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004150
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004151 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004152 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004153 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004154 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004155 // TODO: more arguments
4156 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004157 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004158 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004159 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004160
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004161 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004162 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004163 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004164 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004165 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004166 // missing first index is equal to zero
4167 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004168 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004169 else
4170 {
4171 if (compile_expr0(arg, cctx) == FAIL)
4172 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004173 if (**arg == ':')
4174 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004175 semsg(_(e_white_space_required_before_and_after_str_at_str),
4176 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004177 return FAIL;
4178 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004179 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004180 return FAIL;
4181 *arg = skipwhite(*arg);
4182 }
4183 if (**arg == ':')
4184 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004185 is_slice = TRUE;
4186 ++*arg;
4187 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4188 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004189 semsg(_(e_white_space_required_before_and_after_str_at_str),
4190 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004191 return FAIL;
4192 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004193 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004194 return FAIL;
4195 if (**arg == ']')
4196 // missing second index is equal to end of string
4197 generate_PUSHNR(cctx, -1);
4198 else
4199 {
4200 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004201 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004202 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004203 return FAIL;
4204 *arg = skipwhite(*arg);
4205 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207
4208 if (**arg != ']')
4209 {
4210 emsg(_(e_missbrac));
4211 return FAIL;
4212 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004213 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214
Bram Moolenaare42939a2021-04-05 17:11:17 +02004215 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004216 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004218 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004220 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004221 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004222 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004223 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004224
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004225 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004226 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004227 {
4228 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004229 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004230 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004231 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004232 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 while (eval_isnamec(*p))
4234 MB_PTR_ADV(p);
4235 if (p == *arg)
4236 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004237 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 return FAIL;
4239 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004240 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 return FAIL;
4242 *arg = p;
4243 }
4244 else
4245 break;
4246 }
4247
4248 // TODO - see handle_subscript():
4249 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4250 // Don't do this when "Func" is already a partial that was bound
4251 // explicitly (pt_auto is FALSE).
4252
4253 return OK;
4254}
4255
4256/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004257 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4258 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004260 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004261 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004262 *
4263 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 */
4265
4266/*
4267 * number number constant
4268 * 0zFFFFFFFF Blob constant
4269 * "string" string constant
4270 * 'string' literal string constant
4271 * &option-name option value
4272 * @r register contents
4273 * identifier variable value
4274 * function() function call
4275 * $VAR environment variable
4276 * (expression) nested expression
4277 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004278 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 *
4280 * Also handle:
4281 * ! in front logical NOT
4282 * - in front unary minus
4283 * + in front unary plus (ignored)
4284 * trailing (arg) funcref/partial call
4285 * trailing [] subscript in String or List
4286 * trailing .name entry in Dictionary
4287 * trailing ->name() method call
4288 */
4289 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004290compile_expr7(
4291 char_u **arg,
4292 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004293 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 char_u *start_leader, *end_leader;
4296 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004297 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004298 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004300 ppconst->pp_is_const = FALSE;
4301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 /*
4303 * Skip '!', '-' and '+' characters. They are handled later.
4304 */
4305 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004306 if (eval_leader(arg, TRUE) == FAIL)
4307 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 end_leader = *arg;
4309
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004310 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 switch (**arg)
4312 {
4313 /*
4314 * Number constant.
4315 */
4316 case '0': // also for blob starting with 0z
4317 case '1':
4318 case '2':
4319 case '3':
4320 case '4':
4321 case '5':
4322 case '6':
4323 case '7':
4324 case '8':
4325 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004326 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004328 // Apply "-" and "+" just before the number now, right to
4329 // left. Matters especially when "->" follows. Stops at
4330 // '!'.
4331 if (apply_leader(rettv, TRUE,
4332 start_leader, &end_leader) == FAIL)
4333 {
4334 clear_tv(rettv);
4335 return FAIL;
4336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 break;
4338
4339 /*
4340 * String constant: "string".
4341 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004342 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 return FAIL;
4344 break;
4345
4346 /*
4347 * Literal string constant: 'str''ing'.
4348 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004349 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 return FAIL;
4351 break;
4352
4353 /*
4354 * Constant Vim variable.
4355 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004356 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 ret = NOTDONE;
4358 break;
4359
4360 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004361 * "true" constant
4362 */
4363 case 't': if (STRNCMP(*arg, "true", 4) == 0
4364 && !eval_isnamec((*arg)[4]))
4365 {
4366 *arg += 4;
4367 rettv->v_type = VAR_BOOL;
4368 rettv->vval.v_number = VVAL_TRUE;
4369 }
4370 else
4371 ret = NOTDONE;
4372 break;
4373
4374 /*
4375 * "false" constant
4376 */
4377 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4378 && !eval_isnamec((*arg)[5]))
4379 {
4380 *arg += 5;
4381 rettv->v_type = VAR_BOOL;
4382 rettv->vval.v_number = VVAL_FALSE;
4383 }
4384 else
4385 ret = NOTDONE;
4386 break;
4387
4388 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004389 * "null" constant
4390 */
4391 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004392 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004393 {
4394 *arg += 4;
4395 rettv->v_type = VAR_SPECIAL;
4396 rettv->vval.v_number = VVAL_NULL;
4397 }
4398 else
4399 ret = NOTDONE;
4400 break;
4401
4402 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 * List: [expr, expr]
4404 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004405 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4406 return FAIL;
4407 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 break;
4409
4410 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 * Dictionary: {'key': val, 'key': val}
4412 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004413 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4414 return FAIL;
4415 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 break;
4417
4418 /*
4419 * Option value: &name
4420 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004421 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4422 return FAIL;
4423 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 break;
4425
4426 /*
4427 * Environment variable: $VAR.
4428 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004429 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4430 return FAIL;
4431 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 break;
4433
4434 /*
4435 * Register contents: @r.
4436 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004437 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4438 return FAIL;
4439 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 break;
4441 /*
4442 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004443 * lambda: (arg, arg) => expr
4444 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004446 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4447 ret = compile_lambda(arg, cctx);
4448 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004449 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 break;
4451
4452 default: ret = NOTDONE;
4453 break;
4454 }
4455 if (ret == FAIL)
4456 return FAIL;
4457
Bram Moolenaar1c747212020-05-09 18:28:34 +02004458 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004460 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004461 clear_tv(rettv);
4462 else
4463 // A constant expression can possibly be handled compile time,
4464 // return the value instead of generating code.
4465 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 }
4467 else if (ret == NOTDONE)
4468 {
4469 char_u *p;
4470 int r;
4471
4472 if (!eval_isnamec1(**arg))
4473 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004474 if (!vim9_bad_comment(*arg))
4475 {
4476 if (ends_excmd(*skipwhite(*arg)))
4477 semsg(_(e_empty_expression_str), *arg);
4478 else
4479 semsg(_(e_name_expected_str), *arg);
4480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 return FAIL;
4482 }
4483
4484 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004485 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004486 if (p - *arg == (size_t)1 && **arg == '_')
4487 {
4488 emsg(_(e_cannot_use_underscore_here));
4489 return FAIL;
4490 }
4491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004493 {
4494 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004497 {
4498 if (generate_ppconst(cctx, ppconst) == FAIL)
4499 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004500 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004501 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 if (r == FAIL)
4503 return FAIL;
4504 }
4505
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004506 // Handle following "[]", ".member", etc.
4507 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004508 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004509 ppconst) == FAIL)
4510 return FAIL;
4511 if (ppconst->pp_used > 0)
4512 {
4513 // apply the '!', '-' and '+' before the constant
4514 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004515 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004516 return FAIL;
4517 return OK;
4518 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004519 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004521 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522}
4523
4524/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004525 * Give the "white on both sides" error, taking the operator from "p[len]".
4526 */
4527 void
4528error_white_both(char_u *op, int len)
4529{
4530 char_u buf[10];
4531
4532 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004533 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004534}
4535
4536/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004537 * <type>expr7: runtime type check / conversion
4538 */
4539 static int
4540compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4541{
4542 type_T *want_type = NULL;
4543
4544 // Recognize <type>
4545 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4546 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004547 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004548 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4549 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004550 return FAIL;
4551
4552 if (**arg != '>')
4553 {
4554 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004555 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004556 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004557 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004558 return FAIL;
4559 }
4560 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004561 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004562 return FAIL;
4563 }
4564
4565 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4566 return FAIL;
4567
4568 if (want_type != NULL)
4569 {
4570 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004571 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004572 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004573
Bram Moolenaard1103582020-08-14 22:44:25 +02004574 generate_ppconst(cctx, ppconst);
4575 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004576 where.wt_index = 0;
4577 where.wt_variable = FALSE;
4578 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004579 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004580 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004581 return FAIL;
4582 }
4583 }
4584
4585 return OK;
4586}
4587
4588/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 * * number multiplication
4590 * / number division
4591 * % number modulo
4592 */
4593 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004594compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595{
4596 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004597 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004598 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004600 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004601 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 return FAIL;
4603
4604 /*
4605 * Repeat computing, until no "*", "/" or "%" is following.
4606 */
4607 for (;;)
4608 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004609 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 if (*op != '*' && *op != '/' && *op != '%')
4611 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004612 if (next != NULL)
4613 {
4614 *arg = next_line_from_context(cctx, TRUE);
4615 op = skipwhite(*arg);
4616 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004617
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004618 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004620 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004621 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004623 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004624 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004626 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004627 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004629
4630 if (ppconst->pp_used == ppconst_used + 2
4631 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4632 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004633 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004634 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4635 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4636 varnumber_T res = 0;
4637 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004639 // both are numbers: compute the result
4640 switch (*op)
4641 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004642 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004643 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004644 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004645 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004646 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004647 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004648 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004649 break;
4650 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004651 if (failed)
4652 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004653 tv1->vval.v_number = res;
4654 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004655 }
4656 else
4657 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004658 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004659 generate_two_op(cctx, op);
4660 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 }
4662
4663 return OK;
4664}
4665
4666/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004667 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 * - number subtraction
4669 * .. string concatenation
4670 */
4671 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004672compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673{
4674 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004675 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004677 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678
4679 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004680 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 return FAIL;
4682
4683 /*
4684 * Repeat computing, until no "+", "-" or ".." is following.
4685 */
4686 for (;;)
4687 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004688 op = may_peek_next_line(cctx, *arg, &next);
4689 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 break;
4691 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004692 if (next != NULL)
4693 {
4694 *arg = next_line_from_context(cctx, TRUE);
4695 op = skipwhite(*arg);
4696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004698 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004700 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004701 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 }
4703
Bram Moolenaare0de1712020-12-02 17:36:54 +01004704 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004705 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004707 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004708 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 return FAIL;
4710
Bram Moolenaara5565e42020-05-09 15:44:01 +02004711 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004712 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004713 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4714 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4715 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4716 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4719 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004720
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004721 // concat/subtract/add constant numbers
4722 if (*op == '+')
4723 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4724 else if (*op == '-')
4725 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4726 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004727 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004728 // concatenate constant strings
4729 char_u *s1 = tv1->vval.v_string;
4730 char_u *s2 = tv2->vval.v_string;
4731 size_t len1 = STRLEN(s1);
4732
4733 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4734 if (tv1->vval.v_string == NULL)
4735 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004736 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004737 return FAIL;
4738 }
4739 mch_memmove(tv1->vval.v_string, s1, len1);
4740 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004741 vim_free(s1);
4742 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004743 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004744 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 }
4746 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004747 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004748 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004749 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004750 if (*op == '.')
4751 {
4752 if (may_generate_2STRING(-2, cctx) == FAIL
4753 || may_generate_2STRING(-1, cctx) == FAIL)
4754 return FAIL;
4755 generate_instr_drop(cctx, ISN_CONCAT, 1);
4756 }
4757 else
4758 generate_two_op(cctx, op);
4759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 }
4761
4762 return OK;
4763}
4764
4765/*
4766 * expr5a == expr5b
4767 * expr5a =~ expr5b
4768 * expr5a != expr5b
4769 * expr5a !~ expr5b
4770 * expr5a > expr5b
4771 * expr5a >= expr5b
4772 * expr5a < expr5b
4773 * expr5a <= expr5b
4774 * expr5a is expr5b
4775 * expr5a isnot expr5b
4776 *
4777 * Produces instructions:
4778 * EVAL expr5a Push result of "expr5a"
4779 * EVAL expr5b Push result of "expr5b"
4780 * COMPARE one of the compare instructions
4781 */
4782 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004783compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004785 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004787 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004790 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791
4792 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004793 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 return FAIL;
4795
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004796 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004797 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798
4799 /*
4800 * If there is a comparative operator, use it.
4801 */
4802 if (type != EXPR_UNKNOWN)
4803 {
4804 int ic = FALSE; // Default: do not ignore case
4805
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004806 if (next != NULL)
4807 {
4808 *arg = next_line_from_context(cctx, TRUE);
4809 p = skipwhite(*arg);
4810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 if (type_is && (p[len] == '?' || p[len] == '#'))
4812 {
4813 semsg(_(e_invexpr2), *arg);
4814 return FAIL;
4815 }
4816 // extra question mark appended: ignore case
4817 if (p[len] == '?')
4818 {
4819 ic = TRUE;
4820 ++len;
4821 }
4822 // extra '#' appended: match case (ignored)
4823 else if (p[len] == '#')
4824 ++len;
4825 // nothing appended: match case
4826
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004827 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004829 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004830 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831 }
4832
4833 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004834 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004835 return FAIL;
4836
Bram Moolenaara5565e42020-05-09 15:44:01 +02004837 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 return FAIL;
4839
Bram Moolenaara5565e42020-05-09 15:44:01 +02004840 if (ppconst->pp_used == ppconst_used + 2)
4841 {
4842 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4843 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4844 int ret;
4845
4846 // Both sides are a constant, compute the result now.
4847 // First check for a valid combination of types, this is more
4848 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004849 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004850 ret = FAIL;
4851 else
4852 {
4853 ret = typval_compare(tv1, tv2, type, ic);
4854 tv1->v_type = VAR_BOOL;
4855 tv1->vval.v_number = tv1->vval.v_number
4856 ? VVAL_TRUE : VVAL_FALSE;
4857 clear_tv(tv2);
4858 --ppconst->pp_used;
4859 }
4860 return ret;
4861 }
4862
4863 generate_ppconst(cctx, ppconst);
4864 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 }
4866
4867 return OK;
4868}
4869
Bram Moolenaar7f141552020-05-09 17:35:53 +02004870static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872/*
4873 * Compile || or &&.
4874 */
4875 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004876compile_and_or(
4877 char_u **arg,
4878 cctx_T *cctx,
4879 char *op,
4880 ppconst_T *ppconst,
4881 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004883 char_u *next;
4884 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 int opchar = *op;
4886
4887 if (p[0] == opchar && p[1] == opchar)
4888 {
4889 garray_T *instr = &cctx->ctx_instr;
4890 garray_T end_ga;
4891
4892 /*
4893 * Repeat until there is no following "||" or "&&"
4894 */
4895 ga_init2(&end_ga, sizeof(int), 10);
4896 while (p[0] == opchar && p[1] == opchar)
4897 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004898 long start_lnum = SOURCING_LNUM;
4899 int start_ctx_lnum = cctx->ctx_lnum;
4900 int save_lnum;
4901
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004902 if (next != NULL)
4903 {
4904 *arg = next_line_from_context(cctx, TRUE);
4905 p = skipwhite(*arg);
4906 }
4907
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004908 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4909 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004910 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004911 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004912 return FAIL;
4913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004915 // TODO: use ppconst if the value is a constant and check
4916 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004917 generate_ppconst(cctx, ppconst);
4918
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004919 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004920 SOURCING_LNUM = start_lnum;
4921 save_lnum = cctx->ctx_lnum;
4922 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004923 if (bool_on_stack(cctx) == FAIL)
4924 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004925 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004926 ga_clear(&end_ga);
4927 return FAIL;
4928 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004929 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 if (ga_grow(&end_ga, 1) == FAIL)
4932 {
4933 ga_clear(&end_ga);
4934 return FAIL;
4935 }
4936 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4937 ++end_ga.ga_len;
4938 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004939 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940
4941 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004942 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004943 {
4944 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004945 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004946 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004947
Bram Moolenaara5565e42020-05-09 15:44:01 +02004948 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4949 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 {
4951 ga_clear(&end_ga);
4952 return FAIL;
4953 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004954
4955 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004957 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004959 // Every part must evaluate to a bool.
4960 if (bool_on_stack(cctx) == FAIL)
4961 {
4962 ga_clear(&end_ga);
4963 return FAIL;
4964 }
4965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 // Fill in the end label in all jumps.
4967 while (end_ga.ga_len > 0)
4968 {
4969 isn_T *isn;
4970
4971 --end_ga.ga_len;
4972 isn = ((isn_T *)instr->ga_data)
4973 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4974 isn->isn_arg.jump.jump_where = instr->ga_len;
4975 }
4976 ga_clear(&end_ga);
4977 }
4978
4979 return OK;
4980}
4981
4982/*
4983 * expr4a && expr4a && expr4a logical AND
4984 *
4985 * Produces instructions:
4986 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004987 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004988 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004990 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 * EVAL expr4c Push result of "expr4c"
4992 * end:
4993 */
4994 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004995compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004997 int ppconst_used = ppconst->pp_used;
4998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005000 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 return FAIL;
5002
5003 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005004 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005}
5006
5007/*
5008 * expr3a || expr3b || expr3c logical OR
5009 *
5010 * Produces instructions:
5011 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005012 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005013 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005015 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 * EVAL expr3c Push result of "expr3c"
5017 * end:
5018 */
5019 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005020compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005022 int ppconst_used = ppconst->pp_used;
5023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005025 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026 return FAIL;
5027
5028 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005029 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030}
5031
5032/*
5033 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005035 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 * JUMP_IF_FALSE alt jump if false
5037 * EVAL expr1a
5038 * JUMP_ALWAYS end
5039 * alt: EVAL expr1b
5040 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005041 *
5042 * Toplevel expression: expr2 ?? expr1
5043 * Produces instructions:
5044 * EVAL expr2 Push result of "expr2"
5045 * JUMP_AND_KEEP_IF_TRUE end jump if true
5046 * EVAL expr1
5047 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048 */
5049 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005050compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051{
5052 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005053 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005054 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055
Bram Moolenaar3988f642020-08-27 22:43:03 +02005056 // Ignore all kinds of errors when not producing code.
5057 if (cctx->ctx_skip == SKIP_YES)
5058 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005059 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005060 return OK;
5061 }
5062
Bram Moolenaar61a89812020-05-07 16:58:17 +02005063 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005064 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 return FAIL;
5066
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005067 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 if (*p == '?')
5069 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005070 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071 garray_T *instr = &cctx->ctx_instr;
5072 garray_T *stack = &cctx->ctx_type_stack;
5073 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005074 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005076 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005077 int has_const_expr = FALSE;
5078 int const_value = FALSE;
5079 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005081 if (next != NULL)
5082 {
5083 *arg = next_line_from_context(cctx, TRUE);
5084 p = skipwhite(*arg);
5085 }
5086
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005087 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005088 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005089 semsg(_(e_white_space_required_before_and_after_str_at_str),
5090 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005091 return FAIL;
5092 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093
Bram Moolenaara5565e42020-05-09 15:44:01 +02005094 if (ppconst->pp_used == ppconst_used + 1)
5095 {
5096 // the condition is a constant, we know whether the ? or the :
5097 // expression is to be evaluated.
5098 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005099 if (op_falsy)
5100 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5101 else
5102 {
5103 int error = FALSE;
5104
5105 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5106 &error);
5107 if (error)
5108 return FAIL;
5109 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005110 cctx->ctx_skip = save_skip == SKIP_YES ||
5111 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5112
5113 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5114 // "left ?? right" and "left" is truthy: produce "left"
5115 generate_ppconst(cctx, ppconst);
5116 else
5117 {
5118 clear_tv(&ppconst->pp_tv[ppconst_used]);
5119 --ppconst->pp_used;
5120 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005121 }
5122 else
5123 {
5124 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005125 if (op_falsy)
5126 end_idx = instr->ga_len;
5127 generate_JUMP(cctx, op_falsy
5128 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5129 if (op_falsy)
5130 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005131 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132
5133 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005134 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005135 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005136 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005137 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138
Bram Moolenaara5565e42020-05-09 15:44:01 +02005139 if (!has_const_expr)
5140 {
5141 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005143 if (!op_falsy)
5144 {
5145 // remember the type and drop it
5146 --stack->ga_len;
5147 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005149 end_idx = instr->ga_len;
5150 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005151
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005152 // jump here from JUMP_IF_FALSE
5153 isn = ((isn_T *)instr->ga_data) + alt_idx;
5154 isn->isn_arg.jump.jump_where = instr->ga_len;
5155 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005157
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005158 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005159 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005160 // Check for the ":".
5161 p = may_peek_next_line(cctx, *arg, &next);
5162 if (*p != ':')
5163 {
5164 emsg(_(e_missing_colon));
5165 return FAIL;
5166 }
5167 if (next != NULL)
5168 {
5169 *arg = next_line_from_context(cctx, TRUE);
5170 p = skipwhite(*arg);
5171 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005172
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005173 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5174 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005175 semsg(_(e_white_space_required_before_and_after_str_at_str),
5176 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005177 return FAIL;
5178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005180 // evaluate the third expression
5181 if (has_const_expr)
5182 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005183 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005184 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005185 return FAIL;
5186 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5187 return FAIL;
5188 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189
Bram Moolenaara5565e42020-05-09 15:44:01 +02005190 if (!has_const_expr)
5191 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005192 type_T **typep;
5193
Bram Moolenaara5565e42020-05-09 15:44:01 +02005194 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195
Bram Moolenaara5565e42020-05-09 15:44:01 +02005196 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005197 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5198 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005199
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005200 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005201 isn = ((isn_T *)instr->ga_data) + end_idx;
5202 isn->isn_arg.jump.jump_where = instr->ga_len;
5203 }
5204
5205 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 }
5207 return OK;
5208}
5209
5210/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005211 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005212 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5213 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005214 */
5215 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005216compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005217{
5218 ppconst_T ppconst;
5219
5220 CLEAR_FIELD(ppconst);
5221 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5222 {
5223 clear_ppconst(&ppconst);
5224 return FAIL;
5225 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005226 if (is_const != NULL)
5227 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005228 if (generate_ppconst(cctx, &ppconst) == FAIL)
5229 return FAIL;
5230 return OK;
5231}
5232
5233/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005234 * Toplevel expression.
5235 */
5236 static int
5237compile_expr0(char_u **arg, cctx_T *cctx)
5238{
5239 return compile_expr0_ext(arg, cctx, NULL);
5240}
5241
5242/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243 * compile "return [expr]"
5244 */
5245 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005246compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247{
5248 char_u *p = arg;
5249 garray_T *stack = &cctx->ctx_type_stack;
5250 type_T *stack_type;
5251
5252 if (*p != NUL && *p != '|' && *p != '\n')
5253 {
5254 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005255 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 return NULL;
5257
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005258 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005259 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005260 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005261 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005262 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5263 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005264 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005265 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005266 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005267 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005268 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005269 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5270 && stack_type->tt_type != VAR_VOID
5271 && stack_type->tt_type != VAR_UNKNOWN)
5272 {
5273 emsg(_(e_returning_value_in_function_without_return_type));
5274 return NULL;
5275 }
5276 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005277 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005278 return NULL;
5279 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005281 }
5282 else
5283 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005284 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005285 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005286 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5287 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005289 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005290 return NULL;
5291 }
5292
5293 // No argument, return zero.
5294 generate_PUSHNR(cctx, 0);
5295 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005296
5297 // Undo any command modifiers.
5298 generate_undo_cmdmods(cctx);
5299
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005300 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301 return NULL;
5302
5303 // "return val | endif" is possible
5304 return skipwhite(p);
5305}
5306
5307/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005308 * Get a line from the compilation context, compatible with exarg_T getline().
5309 * Return a pointer to the line in allocated memory.
5310 * Return NULL for end-of-file or some error.
5311 */
5312 static char_u *
5313exarg_getline(
5314 int c UNUSED,
5315 void *cookie,
5316 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005317 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005318{
5319 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005320 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005321
Bram Moolenaar66250c92020-08-20 15:02:42 +02005322 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005323 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005324 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005325 return NULL;
5326 ++cctx->ctx_lnum;
5327 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5328 // Comment lines result in NULL pointers, skip them.
5329 if (p != NULL)
5330 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005331 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005332}
5333
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005334 void
5335fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5336{
5337 eap->getline = exarg_getline;
5338 eap->cookie = cctx;
5339}
5340
Bram Moolenaar04b12692020-05-04 23:24:44 +02005341/*
5342 * Compile a nested :def command.
5343 */
5344 static char_u *
5345compile_nested_function(exarg_T *eap, cctx_T *cctx)
5346{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005347 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005348 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005349 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005350 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005351 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005352 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005353
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005354 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005355 {
5356 emsg(_(e_cannot_use_bang_with_nested_def));
5357 return NULL;
5358 }
5359
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005360 if (*name_start == '/')
5361 {
5362 name_end = skip_regexp(name_start + 1, '/', TRUE);
5363 if (*name_end == '/')
5364 ++name_end;
5365 eap->nextcmd = check_nextcmd(name_end);
5366 }
5367 if (name_end == name_start || *skipwhite(name_end) != '(')
5368 {
5369 if (!ends_excmd2(name_start, name_end))
5370 {
5371 semsg(_(e_invalid_command_str), eap->cmd);
5372 return NULL;
5373 }
5374
5375 // "def" or "def Name": list functions
5376 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5377 return NULL;
5378 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5379 }
5380
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005381 // Only g:Func() can use a namespace.
5382 if (name_start[1] == ':' && !is_global)
5383 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005384 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005385 return NULL;
5386 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005387 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005388 return NULL;
5389
Bram Moolenaar04b12692020-05-04 23:24:44 +02005390 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005391 fill_exarg_from_cctx(eap, cctx);
5392
Bram Moolenaar04b12692020-05-04 23:24:44 +02005393 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005394 lambda_name = vim_strsave(get_lambda_name());
5395 if (lambda_name == NULL)
5396 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005397 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005398
Bram Moolenaar822ba242020-05-24 23:00:18 +02005399 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005400 {
5401 r = eap->skip ? OK : FAIL;
5402 goto theend;
5403 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005404
5405 // copy over the block scope IDs before compiling
5406 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5407 {
5408 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5409
5410 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5411 if (ufunc->uf_block_ids != NULL)
5412 {
5413 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5414 sizeof(int) * block_depth);
5415 ufunc->uf_block_depth = block_depth;
5416 }
5417 }
5418
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005419 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5420 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005421 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005422 {
5423 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005424 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005425 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005426
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005427 if (is_global)
5428 {
5429 char_u *func_name = vim_strnsave(name_start + 2,
5430 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005431
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005432 if (func_name == NULL)
5433 r = FAIL;
5434 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005435 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005436 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005437 lambda_name = NULL;
5438 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005439 }
5440 else
5441 {
5442 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005443 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005444 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005445
Bram Moolenaareef21022020-08-01 22:16:43 +02005446 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005447 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005448 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005449 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005450 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5451 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005452 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005453
5454theend:
5455 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005456 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005457}
5458
5459/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005460 * Return the length of an assignment operator, or zero if there isn't one.
5461 */
5462 int
5463assignment_len(char_u *p, int *heredoc)
5464{
5465 if (*p == '=')
5466 {
5467 if (p[1] == '<' && p[2] == '<')
5468 {
5469 *heredoc = TRUE;
5470 return 3;
5471 }
5472 return 1;
5473 }
5474 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5475 return 2;
5476 if (STRNCMP(p, "..=", 3) == 0)
5477 return 3;
5478 return 0;
5479}
5480
5481// words that cannot be used as a variable
5482static char *reserved[] = {
5483 "true",
5484 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005485 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005486 NULL
5487};
5488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005490 * Generate the load instruction for "name".
5491 */
5492 static void
5493generate_loadvar(
5494 cctx_T *cctx,
5495 assign_dest_T dest,
5496 char_u *name,
5497 lvar_T *lvar,
5498 type_T *type)
5499{
5500 switch (dest)
5501 {
5502 case dest_option:
5503 // TODO: check the option exists
5504 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5505 break;
5506 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005507 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5508 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5509 else
5510 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005511 break;
5512 case dest_buffer:
5513 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5514 break;
5515 case dest_window:
5516 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5517 break;
5518 case dest_tab:
5519 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5520 break;
5521 case dest_script:
5522 compile_load_scriptvar(cctx,
5523 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5524 break;
5525 case dest_env:
5526 // Include $ in the name here
5527 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5528 break;
5529 case dest_reg:
5530 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5531 break;
5532 case dest_vimvar:
5533 generate_LOADV(cctx, name + 2, TRUE);
5534 break;
5535 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005536 if (lvar->lv_from_outer > 0)
5537 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5538 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005539 else
5540 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5541 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005542 case dest_expr:
5543 // list or dict value should already be on the stack.
5544 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005545 }
5546}
5547
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005548/*
5549 * Skip over "[expr]" or ".member".
5550 * Does not check for any errors.
5551 */
5552 static char_u *
5553skip_index(char_u *start)
5554{
5555 char_u *p = start;
5556
5557 if (*p == '[')
5558 {
5559 p = skipwhite(p + 1);
5560 (void)skip_expr(&p, NULL);
5561 p = skipwhite(p);
5562 if (*p == ']')
5563 return p + 1;
5564 return p;
5565 }
5566 // if (*p == '.')
5567 return to_name_end(p + 1, TRUE);
5568}
5569
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005570 void
5571vim9_declare_error(char_u *name)
5572{
5573 char *scope = "";
5574
5575 switch (*name)
5576 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005577 case 'g': scope = _("global"); break;
5578 case 'b': scope = _("buffer"); break;
5579 case 'w': scope = _("window"); break;
5580 case 't': scope = _("tab"); break;
5581 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005582 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005583 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005584 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005585 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005586 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005587 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005588 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005589 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005590 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005591}
5592
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005593/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005594 * For one assignment figure out the type of destination. Return it in "dest".
5595 * When not recognized "dest" is not set.
5596 * For an option "opt_flags" is set.
5597 * For a v:var "vimvaridx" is set.
5598 * "type" is set to the destination type if known, unchanted otherwise.
5599 * Return FAIL if an error message was given.
5600 */
5601 static int
5602get_var_dest(
5603 char_u *name,
5604 assign_dest_T *dest,
5605 int cmdidx,
5606 int *opt_flags,
5607 int *vimvaridx,
5608 type_T **type,
5609 cctx_T *cctx)
5610{
5611 char_u *p;
5612
5613 if (*name == '&')
5614 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005615 int cc;
5616 long numval;
5617 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005618
5619 *dest = dest_option;
5620 if (cmdidx == CMD_final || cmdidx == CMD_const)
5621 {
5622 emsg(_(e_const_option));
5623 return FAIL;
5624 }
5625 p = name;
5626 p = find_option_end(&p, opt_flags);
5627 if (p == NULL)
5628 {
5629 // cannot happen?
5630 emsg(_(e_letunexp));
5631 return FAIL;
5632 }
5633 cc = *p;
5634 *p = NUL;
5635 opt_type = get_option_value(skip_option_env_lead(name),
5636 &numval, NULL, *opt_flags);
5637 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005638 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005639 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005640 case gov_unknown:
5641 semsg(_(e_unknown_option), name);
5642 return FAIL;
5643 case gov_string:
5644 case gov_hidden_string:
5645 *type = &t_string;
5646 break;
5647 case gov_bool:
5648 case gov_hidden_bool:
5649 *type = &t_bool;
5650 break;
5651 case gov_number:
5652 case gov_hidden_number:
5653 *type = &t_number;
5654 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005655 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005656 }
5657 else if (*name == '$')
5658 {
5659 *dest = dest_env;
5660 *type = &t_string;
5661 }
5662 else if (*name == '@')
5663 {
5664 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5665 {
5666 emsg_invreg(name[1]);
5667 return FAIL;
5668 }
5669 *dest = dest_reg;
5670 *type = &t_string;
5671 }
5672 else if (STRNCMP(name, "g:", 2) == 0)
5673 {
5674 *dest = dest_global;
5675 }
5676 else if (STRNCMP(name, "b:", 2) == 0)
5677 {
5678 *dest = dest_buffer;
5679 }
5680 else if (STRNCMP(name, "w:", 2) == 0)
5681 {
5682 *dest = dest_window;
5683 }
5684 else if (STRNCMP(name, "t:", 2) == 0)
5685 {
5686 *dest = dest_tab;
5687 }
5688 else if (STRNCMP(name, "v:", 2) == 0)
5689 {
5690 typval_T *vtv;
5691 int di_flags;
5692
5693 *vimvaridx = find_vim_var(name + 2, &di_flags);
5694 if (*vimvaridx < 0)
5695 {
5696 semsg(_(e_variable_not_found_str), name);
5697 return FAIL;
5698 }
5699 // We use the current value of "sandbox" here, is that OK?
5700 if (var_check_ro(di_flags, name, FALSE))
5701 return FAIL;
5702 *dest = dest_vimvar;
5703 vtv = get_vim_var_tv(*vimvaridx);
5704 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5705 }
5706 return OK;
5707}
5708
5709/*
5710 * Generate a STORE instruction for "dest", not being "dest_local".
5711 * Return FAIL when out of memory.
5712 */
5713 static int
5714generate_store_var(
5715 cctx_T *cctx,
5716 assign_dest_T dest,
5717 int opt_flags,
5718 int vimvaridx,
5719 int scriptvar_idx,
5720 int scriptvar_sid,
5721 type_T *type,
5722 char_u *name)
5723{
5724 switch (dest)
5725 {
5726 case dest_option:
5727 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5728 opt_flags);
5729 case dest_global:
5730 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005731 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5732 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005733 case dest_buffer:
5734 // include b: with the name, easier to execute that way
5735 return generate_STORE(cctx, ISN_STOREB, 0, name);
5736 case dest_window:
5737 // include w: with the name, easier to execute that way
5738 return generate_STORE(cctx, ISN_STOREW, 0, name);
5739 case dest_tab:
5740 // include t: with the name, easier to execute that way
5741 return generate_STORE(cctx, ISN_STORET, 0, name);
5742 case dest_env:
5743 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5744 case dest_reg:
5745 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5746 case dest_vimvar:
5747 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5748 case dest_script:
5749 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005750 // "s:" may be included in the name.
5751 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005752 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005753 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5754 scriptvar_sid, scriptvar_idx, type);
5755 case dest_local:
5756 case dest_expr:
5757 // cannot happen
5758 break;
5759 }
5760 return FAIL;
5761}
5762
Bram Moolenaar752fc692021-01-04 21:57:11 +01005763 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005764generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5765{
5766 if (lhs->lhs_dest != dest_local)
5767 return generate_store_var(cctx, lhs->lhs_dest,
5768 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5769 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5770 lhs->lhs_type, lhs->lhs_name);
5771
5772 if (lhs->lhs_lvar != NULL)
5773 {
5774 garray_T *instr = &cctx->ctx_instr;
5775 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5776
5777 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5778 // ISN_STORENR
5779 if (lhs->lhs_lvar->lv_from_outer == 0
5780 && instr->ga_len == instr_count + 1
5781 && isn->isn_type == ISN_PUSHNR)
5782 {
5783 varnumber_T val = isn->isn_arg.number;
5784 garray_T *stack = &cctx->ctx_type_stack;
5785
5786 isn->isn_type = ISN_STORENR;
5787 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5788 isn->isn_arg.storenr.stnr_val = val;
5789 if (stack->ga_len > 0)
5790 --stack->ga_len;
5791 }
5792 else if (lhs->lhs_lvar->lv_from_outer > 0)
5793 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5794 lhs->lhs_lvar->lv_from_outer);
5795 else
5796 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5797 }
5798 return OK;
5799}
5800
5801 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005802is_decl_command(int cmdidx)
5803{
5804 return cmdidx == CMD_let || cmdidx == CMD_var
5805 || cmdidx == CMD_final || cmdidx == CMD_const;
5806}
5807
5808/*
5809 * Figure out the LHS type and other properties for an assignment or one item
5810 * of ":unlet" with an index.
5811 * Returns OK or FAIL.
5812 */
5813 static int
5814compile_lhs(
5815 char_u *var_start,
5816 lhs_T *lhs,
5817 int cmdidx,
5818 int heredoc,
5819 int oplen,
5820 cctx_T *cctx)
5821{
5822 char_u *var_end;
5823 int is_decl = is_decl_command(cmdidx);
5824
5825 CLEAR_POINTER(lhs);
5826 lhs->lhs_dest = dest_local;
5827 lhs->lhs_vimvaridx = -1;
5828 lhs->lhs_scriptvar_idx = -1;
5829
5830 // "dest_end" is the end of the destination, including "[expr]" or
5831 // ".name".
5832 // "var_end" is the end of the variable/option/etc. name.
5833 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5834 if (*var_start == '@')
5835 var_end = var_start + 2;
5836 else
5837 {
5838 // skip over the leading "&", "&l:", "&g:" and "$"
5839 var_end = skip_option_env_lead(var_start);
5840 var_end = to_name_end(var_end, TRUE);
5841 }
5842
5843 // "a: type" is declaring variable "a" with a type, not dict "a:".
5844 if (is_decl && lhs->lhs_dest_end == var_start + 2
5845 && lhs->lhs_dest_end[-1] == ':')
5846 --lhs->lhs_dest_end;
5847 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5848 --var_end;
5849
5850 // compute the length of the destination without "[expr]" or ".name"
5851 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02005852 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005853 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5854 if (lhs->lhs_name == NULL)
5855 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005856
5857 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5858 // Something follows after the variable: "var[idx]" or "var.key".
5859 lhs->lhs_has_index = TRUE;
5860
Bram Moolenaar752fc692021-01-04 21:57:11 +01005861 if (heredoc)
5862 lhs->lhs_type = &t_list_string;
5863 else
5864 lhs->lhs_type = &t_any;
5865
5866 if (cctx->ctx_skip != SKIP_YES)
5867 {
5868 int declare_error = FALSE;
5869
5870 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5871 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5872 &lhs->lhs_type, cctx) == FAIL)
5873 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005874 if (lhs->lhs_dest != dest_local
5875 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005876 {
5877 // Specific kind of variable recognized.
5878 declare_error = is_decl;
5879 }
5880 else
5881 {
5882 int idx;
5883
5884 // No specific kind of variable recognized, just a name.
5885 for (idx = 0; reserved[idx] != NULL; ++idx)
5886 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5887 {
5888 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5889 return FAIL;
5890 }
5891
5892
5893 if (lookup_local(var_start, lhs->lhs_varlen,
5894 &lhs->lhs_local_lvar, cctx) == OK)
5895 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5896 else
5897 {
5898 CLEAR_FIELD(lhs->lhs_arg_lvar);
5899 if (arg_exists(var_start, lhs->lhs_varlen,
5900 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5901 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5902 {
5903 if (is_decl)
5904 {
5905 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5906 return FAIL;
5907 }
5908 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5909 }
5910 }
5911 if (lhs->lhs_lvar != NULL)
5912 {
5913 if (is_decl)
5914 {
5915 semsg(_(e_variable_already_declared), lhs->lhs_name);
5916 return FAIL;
5917 }
5918 }
5919 else
5920 {
5921 int script_namespace = lhs->lhs_varlen > 1
5922 && STRNCMP(var_start, "s:", 2) == 0;
5923 int script_var = (script_namespace
5924 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005925 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005926 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005927 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005928 imported_T *import =
5929 find_imported(var_start, lhs->lhs_varlen, cctx);
5930
5931 if (script_namespace || script_var || import != NULL)
5932 {
5933 char_u *rawname = lhs->lhs_name
5934 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5935
5936 if (is_decl)
5937 {
5938 if (script_namespace)
5939 semsg(_(e_cannot_declare_script_variable_in_function),
5940 lhs->lhs_name);
5941 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005942 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005943 lhs->lhs_name);
5944 return FAIL;
5945 }
5946 else if (cctx->ctx_ufunc->uf_script_ctx_version
5947 == SCRIPT_VERSION_VIM9
5948 && script_namespace
5949 && !script_var && import == NULL)
5950 {
5951 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5952 return FAIL;
5953 }
5954
5955 lhs->lhs_dest = dest_script;
5956
5957 // existing script-local variables should have a type
5958 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5959 if (import != NULL)
5960 lhs->lhs_scriptvar_sid = import->imp_sid;
5961 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5962 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005963 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005964 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005965 lhs->lhs_scriptvar_sid, rawname,
5966 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5967 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005968 if (lhs->lhs_scriptvar_idx >= 0)
5969 {
5970 scriptitem_T *si = SCRIPT_ITEM(
5971 lhs->lhs_scriptvar_sid);
5972 svar_T *sv =
5973 ((svar_T *)si->sn_var_vals.ga_data)
5974 + lhs->lhs_scriptvar_idx;
5975 lhs->lhs_type = sv->sv_type;
5976 }
5977 }
5978 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005979 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005980 == FAIL)
5981 return FAIL;
5982 }
5983 }
5984
5985 if (declare_error)
5986 {
5987 vim9_declare_error(lhs->lhs_name);
5988 return FAIL;
5989 }
5990 }
5991
5992 // handle "a:name" as a name, not index "name" on "a"
5993 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5994 var_end = lhs->lhs_dest_end;
5995
5996 if (lhs->lhs_dest != dest_option)
5997 {
5998 if (is_decl && *var_end == ':')
5999 {
6000 char_u *p;
6001
6002 // parse optional type: "let var: type = expr"
6003 if (!VIM_ISWHITE(var_end[1]))
6004 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006005 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006006 return FAIL;
6007 }
6008 p = skipwhite(var_end + 1);
6009 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6010 if (lhs->lhs_type == NULL)
6011 return FAIL;
6012 lhs->lhs_has_type = TRUE;
6013 }
6014 else if (lhs->lhs_lvar != NULL)
6015 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6016 }
6017
Bram Moolenaare42939a2021-04-05 17:11:17 +02006018 if (oplen == 3 && !heredoc
6019 && lhs->lhs_dest != dest_global
6020 && !lhs->lhs_has_index
6021 && lhs->lhs_type->tt_type != VAR_STRING
6022 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006023 {
6024 emsg(_(e_can_only_concatenate_to_string));
6025 return FAIL;
6026 }
6027
6028 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6029 && cctx->ctx_skip != SKIP_YES)
6030 {
6031 if (oplen > 1 && !heredoc)
6032 {
6033 // +=, /=, etc. require an existing variable
6034 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6035 return FAIL;
6036 }
6037 if (!is_decl)
6038 {
6039 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6040 return FAIL;
6041 }
6042
Bram Moolenaar3f327882021-03-17 20:56:38 +01006043 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006044 if ((lhs->lhs_type->tt_type == VAR_FUNC
6045 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006046 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006047 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006048
6049 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006050 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6051 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6052 if (lhs->lhs_lvar == NULL)
6053 return FAIL;
6054 lhs->lhs_new_local = TRUE;
6055 }
6056
6057 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006058 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006059 {
6060 // Something follows after the variable: "var[idx]" or "var.key".
6061 // TODO: should we also handle "->func()" here?
6062 if (is_decl)
6063 {
6064 emsg(_(e_cannot_use_index_when_declaring_variable));
6065 return FAIL;
6066 }
6067
6068 if (var_start[lhs->lhs_varlen] == '['
6069 || var_start[lhs->lhs_varlen] == '.')
6070 {
6071 char_u *after = var_start + lhs->lhs_varlen;
6072 char_u *p;
6073
6074 // Only the last index is used below, if there are others
6075 // before it generate code for the expression. Thus for
6076 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6077 for (;;)
6078 {
6079 p = skip_index(after);
6080 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006081 {
6082 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006083 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006084 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006085 after = p;
6086 }
6087 if (after > var_start + lhs->lhs_varlen)
6088 {
6089 lhs->lhs_varlen = after - var_start;
6090 lhs->lhs_dest = dest_expr;
6091 // We don't know the type before evaluating the expression,
6092 // use "any" until then.
6093 lhs->lhs_type = &t_any;
6094 }
6095
Bram Moolenaar752fc692021-01-04 21:57:11 +01006096 if (lhs->lhs_type->tt_member == NULL)
6097 lhs->lhs_member_type = &t_any;
6098 else
6099 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6100 }
6101 else
6102 {
6103 semsg("Not supported yet: %s", var_start);
6104 return FAIL;
6105 }
6106 }
6107 return OK;
6108}
6109
6110/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006111 * Figure out the LHS and check a few errors.
6112 */
6113 static int
6114compile_assign_lhs(
6115 char_u *var_start,
6116 lhs_T *lhs,
6117 int cmdidx,
6118 int is_decl,
6119 int heredoc,
6120 int oplen,
6121 cctx_T *cctx)
6122{
6123 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6124 return FAIL;
6125
6126 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6127 {
6128 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6129 return FAIL;
6130 }
6131 if (!is_decl && lhs->lhs_lvar != NULL
6132 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6133 {
6134 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6135 return FAIL;
6136 }
6137 return OK;
6138}
6139
6140/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006141 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6142 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006143 */
6144 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006145compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006146 char_u *var_start,
6147 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006148 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006149 cctx_T *cctx)
6150{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006151 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006152 char_u *p;
6153 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006154 int need_white_before = TRUE;
6155 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006156
Bram Moolenaar752fc692021-01-04 21:57:11 +01006157 p = var_start + varlen;
6158 if (*p == '[')
6159 {
6160 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006161 if (*p == ':')
6162 {
6163 // empty first index, push zero
6164 r = generate_PUSHNR(cctx, 0);
6165 need_white_before = FALSE;
6166 }
6167 else
6168 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006169
6170 if (r == OK && *skipwhite(p) == ':')
6171 {
6172 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006173 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006174 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006175 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006176 empty_second = *skipwhite(p + 1) == ']';
6177 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6178 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006179 {
6180 semsg(_(e_white_space_required_before_and_after_str_at_str),
6181 ":", p);
6182 return FAIL;
6183 }
6184 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006185 if (*p == ']')
6186 // empty second index, push "none"
6187 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6188 else
6189 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006190 }
6191
Bram Moolenaar752fc692021-01-04 21:57:11 +01006192 if (r == OK && *skipwhite(p) != ']')
6193 {
6194 // this should not happen
6195 emsg(_(e_missbrac));
6196 r = FAIL;
6197 }
6198 }
6199 else // if (*p == '.')
6200 {
6201 char_u *key_end = to_name_end(p + 1, TRUE);
6202 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6203
6204 r = generate_PUSHS(cctx, key);
6205 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006206 return r;
6207}
6208
6209/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006210 * For a LHS with an index, load the variable to be indexed.
6211 */
6212 static int
6213compile_load_lhs(
6214 lhs_T *lhs,
6215 char_u *var_start,
6216 type_T *rhs_type,
6217 cctx_T *cctx)
6218{
6219 if (lhs->lhs_dest == dest_expr)
6220 {
6221 size_t varlen = lhs->lhs_varlen;
6222 int c = var_start[varlen];
6223 char_u *p = var_start;
6224 garray_T *stack = &cctx->ctx_type_stack;
6225
6226 // Evaluate "ll[expr]" of "ll[expr][idx]"
6227 var_start[varlen] = NUL;
6228 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6229 {
6230 // this should not happen
6231 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006232 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006233 return FAIL;
6234 }
6235 var_start[varlen] = c;
6236
6237 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6238 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6239 // now we can properly check the type
6240 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6241 && rhs_type != &t_void
6242 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6243 FALSE, FALSE) == FAIL)
6244 return FAIL;
6245 }
6246 else
6247 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6248 lhs->lhs_lvar, lhs->lhs_type);
6249 return OK;
6250}
6251
6252/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006253 * Assignment to a list or dict member, or ":unlet" for the item, using the
6254 * information in "lhs".
6255 * Returns OK or FAIL.
6256 */
6257 static int
6258compile_assign_unlet(
6259 char_u *var_start,
6260 lhs_T *lhs,
6261 int is_assign,
6262 type_T *rhs_type,
6263 cctx_T *cctx)
6264{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006265 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006266 garray_T *stack = &cctx->ctx_type_stack;
6267 int range = FALSE;
6268
Bram Moolenaar68452172021-04-12 21:21:02 +02006269 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006270 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006271 if (is_assign && range && lhs->lhs_type != &t_blob
6272 && lhs->lhs_type != &t_any)
6273 {
6274 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6275 return FAIL;
6276 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006277
6278 if (lhs->lhs_type == &t_any)
6279 {
6280 // Index on variable of unknown type: check at runtime.
6281 dest_type = VAR_ANY;
6282 }
6283 else
6284 {
6285 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006286 if (dest_type == VAR_DICT && range)
6287 {
6288 emsg(e_cannot_use_range_with_dictionary);
6289 return FAIL;
6290 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006291 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6292 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006293 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006294 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006295 type_T *type;
6296
6297 if (range)
6298 {
6299 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6300 if (need_type(type, &t_number,
6301 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006302 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006303 }
6304 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6305 if ((dest_type != VAR_BLOB || type != &t_special)
6306 && need_type(type, &t_number,
6307 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006308 return FAIL;
6309 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006310 }
6311
6312 // Load the dict or list. On the stack we then have:
6313 // - value (for assignment, not for :unlet)
6314 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006315 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006316 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006317 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6318 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006319
Bram Moolenaar68452172021-04-12 21:21:02 +02006320 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6321 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006322 {
6323 if (is_assign)
6324 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006325 if (range)
6326 {
6327 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6328 return FAIL;
6329 }
6330 else
6331 {
6332 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006333
Bram Moolenaar68452172021-04-12 21:21:02 +02006334 if (isn == NULL)
6335 return FAIL;
6336 isn->isn_arg.vartype = dest_type;
6337 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006338 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006339 else if (range)
6340 {
6341 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6342 return FAIL;
6343 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006344 else
6345 {
6346 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6347 return FAIL;
6348 }
6349 }
6350 else
6351 {
6352 emsg(_(e_indexable_type_required));
6353 return FAIL;
6354 }
6355
6356 return OK;
6357}
6358
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006359/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006360 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006361 * "let name"
6362 * "var name = expr"
6363 * "final name = expr"
6364 * "const name = expr"
6365 * "name = expr"
6366 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006367 * Return NULL for an error.
6368 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006369 */
6370 static char_u *
6371compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6372{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006373 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006374 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006375 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006376 char_u *ret = NULL;
6377 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006378 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006379 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006381 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383 int oplen = 0;
6384 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006385 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006386 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006387 int is_decl = is_decl_command(cmdidx);
6388 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006389 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006390
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006391 // Skip over the "var" or "[var, var]" to get to any "=".
6392 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6393 if (p == NULL)
6394 return *arg == '[' ? arg : NULL;
6395
6396 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006397 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006398 // TODO: should we allow this, and figure out type inference from list
6399 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006400 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401 return NULL;
6402 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006403 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006405 sp = p;
6406 p = skipwhite(p);
6407 op = p;
6408 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006409
6410 if (var_count > 0 && oplen == 0)
6411 // can be something like "[1, 2]->func()"
6412 return arg;
6413
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006414 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006416 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006417 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006418 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006420 if (heredoc)
6421 {
6422 list_T *l;
6423 listitem_T *li;
6424
6425 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006426 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006428 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006429 if (l == NULL)
6430 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431
Bram Moolenaar078269b2020-09-21 20:35:55 +02006432 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006433 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006434 // Push each line and the create the list.
6435 FOR_ALL_LIST_ITEMS(l, li)
6436 {
6437 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6438 li->li_tv.vval.v_string = NULL;
6439 }
6440 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442 list_free(l);
6443 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006444 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006445 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006446 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006447 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006448 char_u *wp;
6449
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006450 // for "[var, var] = expr" evaluate the expression here, loop over the
6451 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006452 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006453
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006454 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006455 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006456 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006457 if (compile_expr0(&p, cctx) == FAIL)
6458 return NULL;
6459 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006460
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006461 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006462 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006463 type_T *stacktype;
6464
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006465 stacktype = stack->ga_len == 0 ? &t_void
6466 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006467 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006469 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006470 goto theend;
6471 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006472 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006473 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006474 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006475 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006476 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6477 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006478 if (stacktype->tt_member != NULL)
6479 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006480 }
6481 }
6482
6483 /*
6484 * Loop over variables in "[var, var] = expr".
6485 * For "var = expr" and "let var: type" this is done only once.
6486 */
6487 if (var_count > 0)
6488 var_start = skipwhite(arg + 1); // skip over the "["
6489 else
6490 var_start = arg;
6491 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6492 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006493 int instr_count = -1;
6494
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006495 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6496 {
6497 // Ignore underscore in "[a, _, b] = list".
6498 if (var_count > 0)
6499 {
6500 var_start = skipwhite(var_start + 2);
6501 continue;
6502 }
6503 emsg(_(e_cannot_use_underscore_here));
6504 goto theend;
6505 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006506 vim_free(lhs.lhs_name);
6507
6508 /*
6509 * Figure out the LHS type and other properties.
6510 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006511 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6512 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006513 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006514 if (!heredoc)
6515 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006516 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006517 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006518 if (oplen > 0 && var_count == 0)
6519 {
6520 // skip over the "=" and the expression
6521 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006522 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006523 }
6524 }
6525 else if (oplen > 0)
6526 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006527 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006528 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006529
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006530 // For "var = expr" evaluate the expression.
6531 if (var_count == 0)
6532 {
6533 int r;
6534
6535 // for "+=", "*=", "..=" etc. first load the current value
6536 if (*op != '=')
6537 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006538 compile_load_lhs(&lhs, var_start, NULL, cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006539
Bram Moolenaar752fc692021-01-04 21:57:11 +01006540 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006541 {
Bram Moolenaare42939a2021-04-05 17:11:17 +02006542 int range = FALSE;
6543
6544 // Get member from list or dict. First compile the
6545 // index value.
6546 if (compile_assign_index(var_start, &lhs,
Bram Moolenaar68452172021-04-12 21:21:02 +02006547 &range, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02006548 goto theend;
Bram Moolenaar68452172021-04-12 21:21:02 +02006549 if (range)
6550 {
Bram Moolenaarb1419262021-04-14 20:54:07 +02006551 semsg(_(e_cannot_use_range_with_assignment_operator_str),
Bram Moolenaar68452172021-04-12 21:21:02 +02006552 var_start);
Bram Moolenaarf387f5d2021-04-15 13:42:21 +02006553 goto theend;
Bram Moolenaar68452172021-04-12 21:21:02 +02006554 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006555
6556 // Get the member.
6557 if (compile_member(FALSE, cctx) == FAIL)
6558 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006559 }
6560 }
6561
6562 // Compile the expression. Temporarily hide the new local
6563 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006564 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006565 --cctx->ctx_locals.ga_len;
6566 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006567 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006568 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006569 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006570 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006571 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006572 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006573 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006574 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006575 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006576 ++cctx->ctx_locals.ga_len;
6577 if (r == FAIL)
6578 goto theend;
6579 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006580 else if (semicolon && var_idx == var_count - 1)
6581 {
6582 // For "[var; var] = expr" get the rest of the list
6583 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6584 goto theend;
6585 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006586 else
6587 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006588 // For "[var, var] = expr" get the "var_idx" item from the
6589 // list.
6590 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006591 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006592 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006593
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006594 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006595 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006596 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006597 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006598 if ((rhs_type->tt_type == VAR_FUNC
6599 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006600 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006601 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006602 goto theend;
6603
Bram Moolenaar752fc692021-01-04 21:57:11 +01006604 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006605 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006606 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006607 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006608 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006609 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006610 }
6611 else
6612 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006613 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006614 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006615 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006616 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006617 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006618 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006619 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006620 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006621 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006622 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006623 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006624 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006625 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006626 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006627 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006628
Bram Moolenaar77709b12021-04-03 21:01:01 +02006629 // Without operator check type here, otherwise below.
6630 // Use the line number of the assignment.
6631 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006632 if (lhs.lhs_has_index)
6633 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006634 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006635 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006636 goto theend;
6637 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006638 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006639 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006640 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006641 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006643 else if (cmdidx == CMD_final)
6644 {
6645 emsg(_(e_final_requires_a_value));
6646 goto theend;
6647 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006648 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006649 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006650 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006651 goto theend;
6652 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006653 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006654 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006655 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006656 goto theend;
6657 }
6658 else
6659 {
6660 // variables are always initialized
6661 if (ga_grow(instr, 1) == FAIL)
6662 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006663 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006664 {
6665 case VAR_BOOL:
6666 generate_PUSHBOOL(cctx, VVAL_FALSE);
6667 break;
6668 case VAR_FLOAT:
6669#ifdef FEAT_FLOAT
6670 generate_PUSHF(cctx, 0.0);
6671#endif
6672 break;
6673 case VAR_STRING:
6674 generate_PUSHS(cctx, NULL);
6675 break;
6676 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006677 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006678 break;
6679 case VAR_FUNC:
6680 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6681 break;
6682 case VAR_LIST:
6683 generate_NEWLIST(cctx, 0);
6684 break;
6685 case VAR_DICT:
6686 generate_NEWDICT(cctx, 0);
6687 break;
6688 case VAR_JOB:
6689 generate_PUSHJOB(cctx, NULL);
6690 break;
6691 case VAR_CHANNEL:
6692 generate_PUSHCHANNEL(cctx, NULL);
6693 break;
6694 case VAR_NUMBER:
6695 case VAR_UNKNOWN:
6696 case VAR_ANY:
6697 case VAR_PARTIAL:
6698 case VAR_VOID:
6699 case VAR_SPECIAL: // cannot happen
6700 generate_PUSHNR(cctx, 0);
6701 break;
6702 }
6703 }
6704 if (var_count == 0)
6705 end = p;
6706 }
6707
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006708 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006709 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006710 break;
6711
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006712 if (oplen > 0 && *op != '=')
6713 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006714 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006715 type_T *stacktype;
6716
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006717 if (*op == '.')
6718 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006719 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006720 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006721 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006722 if (
6723#ifdef FEAT_FLOAT
6724 // If variable is float operation with number is OK.
6725 !(expected == &t_float && stacktype == &t_number) &&
6726#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006727 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006728 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006729 goto theend;
6730
6731 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006732 {
6733 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6734 goto theend;
6735 }
6736 else if (*op == '+')
6737 {
6738 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006739 operator_type(lhs.lhs_member_type, stacktype),
6740 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006741 goto theend;
6742 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006743 else if (generate_two_op(cctx, op) == FAIL)
6744 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746
Bram Moolenaar752fc692021-01-04 21:57:11 +01006747 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006748 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006749 // Use the info in "lhs" to store the value at the index in the
6750 // list or dict.
6751 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6752 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006753 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006754 }
6755 else
6756 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006757 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006758 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006759 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006760 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006761 generate_LOCKCONST(cctx);
6762
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006763 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006764 && (lhs.lhs_type->tt_type == VAR_DICT
6765 || lhs.lhs_type->tt_type == VAR_LIST)
6766 && lhs.lhs_type->tt_member != NULL
6767 && lhs.lhs_type->tt_member != &t_any
6768 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006769 // Set the type in the list or dict, so that it can be checked,
6770 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006771 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006772
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006773 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6774 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006775 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006776
6777 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006778 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006780
6781 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006782 if (var_count > 0 && !semicolon)
6783 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006784 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006785 goto theend;
6786 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006787
Bram Moolenaarb2097502020-07-19 17:17:02 +02006788 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789
6790theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006791 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792 return ret;
6793}
6794
6795/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006796 * Check for an assignment at "eap->cmd", compile it if found.
6797 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6798 */
6799 static int
6800may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6801{
6802 char_u *pskip;
6803 char_u *p;
6804
6805 // Assuming the command starts with a variable or function name,
6806 // find what follows.
6807 // Skip over "var.member", "var[idx]" and the like.
6808 // Also "&opt = val", "$ENV = val" and "@r = val".
6809 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6810 ? eap->cmd + 1 : eap->cmd;
6811 p = to_name_end(pskip, TRUE);
6812 if (p > eap->cmd && *p != NUL)
6813 {
6814 char_u *var_end;
6815 int oplen;
6816 int heredoc;
6817
6818 if (eap->cmd[0] == '@')
6819 var_end = eap->cmd + 2;
6820 else
6821 var_end = find_name_end(pskip, NULL, NULL,
6822 FNE_CHECK_START | FNE_INCL_BR);
6823 oplen = assignment_len(skipwhite(var_end), &heredoc);
6824 if (oplen > 0)
6825 {
6826 size_t len = p - eap->cmd;
6827
6828 // Recognize an assignment if we recognize the variable
6829 // name:
6830 // "g:var = expr"
6831 // "local = expr" where "local" is a local var.
6832 // "script = expr" where "script" is a script-local var.
6833 // "import = expr" where "import" is an imported var
6834 // "&opt = expr"
6835 // "$ENV = expr"
6836 // "@r = expr"
6837 if (*eap->cmd == '&'
6838 || *eap->cmd == '$'
6839 || *eap->cmd == '@'
6840 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006841 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006842 {
6843 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6844 if (*line == NULL || *line == eap->cmd)
6845 return FAIL;
6846 return OK;
6847 }
6848 }
6849 }
6850
6851 if (*eap->cmd == '[')
6852 {
6853 // [var, var] = expr
6854 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6855 if (*line == NULL)
6856 return FAIL;
6857 if (*line != eap->cmd)
6858 return OK;
6859 }
6860 return NOTDONE;
6861}
6862
6863/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006864 * Check if "name" can be "unlet".
6865 */
6866 int
6867check_vim9_unlet(char_u *name)
6868{
6869 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6870 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006871 // "unlet s:var" is allowed in legacy script.
6872 if (*name == 's' && !script_is_vim9())
6873 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006874 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006875 return FAIL;
6876 }
6877 return OK;
6878}
6879
6880/*
6881 * Callback passed to ex_unletlock().
6882 */
6883 static int
6884compile_unlet(
6885 lval_T *lvp,
6886 char_u *name_end,
6887 exarg_T *eap,
6888 int deep UNUSED,
6889 void *coookie)
6890{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006891 cctx_T *cctx = coookie;
6892 char_u *p = lvp->ll_name;
6893 int cc = *name_end;
6894 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006895
Bram Moolenaar752fc692021-01-04 21:57:11 +01006896 if (cctx->ctx_skip == SKIP_YES)
6897 return OK;
6898
6899 *name_end = NUL;
6900 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006901 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006902 // :unlet $ENV_VAR
6903 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6904 }
6905 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6906 {
6907 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006908
Bram Moolenaar752fc692021-01-04 21:57:11 +01006909 // This is similar to assigning: lookup the list/dict, compile the
6910 // idx/key. Then instead of storing the value unlet the item.
6911 // unlet {list}[idx]
6912 // unlet {dict}[key] dict.key
6913 //
6914 // Figure out the LHS type and other properties.
6915 //
6916 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6917
6918 // : unlet an indexed item
6919 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006920 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006921 iemsg("called compile_lhs() without an index");
6922 ret = FAIL;
6923 }
6924 else
6925 {
6926 // Use the info in "lhs" to unlet the item at the index in the
6927 // list or dict.
6928 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006929 }
6930
Bram Moolenaar752fc692021-01-04 21:57:11 +01006931 vim_free(lhs.lhs_name);
6932 }
6933 else if (check_vim9_unlet(p) == FAIL)
6934 {
6935 ret = FAIL;
6936 }
6937 else
6938 {
6939 // Normal name. Only supports g:, w:, t: and b: namespaces.
6940 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006941 }
6942
Bram Moolenaar752fc692021-01-04 21:57:11 +01006943 *name_end = cc;
6944 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006945}
6946
6947/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006948 * Callback passed to ex_unletlock().
6949 */
6950 static int
6951compile_lock_unlock(
6952 lval_T *lvp,
6953 char_u *name_end,
6954 exarg_T *eap,
6955 int deep UNUSED,
6956 void *coookie)
6957{
6958 cctx_T *cctx = coookie;
6959 int cc = *name_end;
6960 char_u *p = lvp->ll_name;
6961 int ret = OK;
6962 size_t len;
6963 char_u *buf;
6964
6965 if (cctx->ctx_skip == SKIP_YES)
6966 return OK;
6967
6968 // Cannot use :lockvar and :unlockvar on local variables.
6969 if (p[1] != ':')
6970 {
6971 char_u *end = skip_var_one(p, FALSE);
6972
6973 if (lookup_local(p, end - p, NULL, cctx) == OK)
6974 {
6975 emsg(_(e_cannot_lock_unlock_local_variable));
6976 return FAIL;
6977 }
6978 }
6979
6980 // Checking is done at runtime.
6981 *name_end = NUL;
6982 len = name_end - p + 20;
6983 buf = alloc(len);
6984 if (buf == NULL)
6985 ret = FAIL;
6986 else
6987 {
6988 vim_snprintf((char *)buf, len, "%s %s",
6989 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6990 p);
6991 ret = generate_EXEC(cctx, buf);
6992
6993 vim_free(buf);
6994 *name_end = cc;
6995 }
6996 return ret;
6997}
6998
6999/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007000 * compile "unlet var", "lock var" and "unlock var"
7001 * "arg" points to "var".
7002 */
7003 static char_u *
7004compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7005{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007006 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7007 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7008 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007009 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7010}
7011
7012/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007013 * Compile an :import command.
7014 */
7015 static char_u *
7016compile_import(char_u *arg, cctx_T *cctx)
7017{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007018 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019}
7020
7021/*
7022 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7023 */
7024 static int
7025compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7026{
7027 garray_T *instr = &cctx->ctx_instr;
7028 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7029
7030 if (endlabel == NULL)
7031 return FAIL;
7032 endlabel->el_next = *el;
7033 *el = endlabel;
7034 endlabel->el_end_label = instr->ga_len;
7035
7036 generate_JUMP(cctx, when, 0);
7037 return OK;
7038}
7039
7040 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007041compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042{
7043 garray_T *instr = &cctx->ctx_instr;
7044
7045 while (*el != NULL)
7046 {
7047 endlabel_T *cur = (*el);
7048 isn_T *isn;
7049
7050 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007051 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052 *el = cur->el_next;
7053 vim_free(cur);
7054 }
7055}
7056
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007057 static void
7058compile_free_jump_to_end(endlabel_T **el)
7059{
7060 while (*el != NULL)
7061 {
7062 endlabel_T *cur = (*el);
7063
7064 *el = cur->el_next;
7065 vim_free(cur);
7066 }
7067}
7068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069/*
7070 * Create a new scope and set up the generic items.
7071 */
7072 static scope_T *
7073new_scope(cctx_T *cctx, scopetype_T type)
7074{
7075 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7076
7077 if (scope == NULL)
7078 return NULL;
7079 scope->se_outer = cctx->ctx_scope;
7080 cctx->ctx_scope = scope;
7081 scope->se_type = type;
7082 scope->se_local_count = cctx->ctx_locals.ga_len;
7083 return scope;
7084}
7085
7086/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007087 * Free the current scope and go back to the outer scope.
7088 */
7089 static void
7090drop_scope(cctx_T *cctx)
7091{
7092 scope_T *scope = cctx->ctx_scope;
7093
7094 if (scope == NULL)
7095 {
7096 iemsg("calling drop_scope() without a scope");
7097 return;
7098 }
7099 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007100 switch (scope->se_type)
7101 {
7102 case IF_SCOPE:
7103 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7104 case FOR_SCOPE:
7105 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7106 case WHILE_SCOPE:
7107 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7108 case TRY_SCOPE:
7109 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7110 case NO_SCOPE:
7111 case BLOCK_SCOPE:
7112 break;
7113 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007114 vim_free(scope);
7115}
7116
7117/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 * compile "if expr"
7119 *
7120 * "if expr" Produces instructions:
7121 * EVAL expr Push result of "expr"
7122 * JUMP_IF_FALSE end
7123 * ... body ...
7124 * end:
7125 *
7126 * "if expr | else" Produces instructions:
7127 * EVAL expr Push result of "expr"
7128 * JUMP_IF_FALSE else
7129 * ... body ...
7130 * JUMP_ALWAYS end
7131 * else:
7132 * ... body ...
7133 * end:
7134 *
7135 * "if expr1 | elseif expr2 | else" Produces instructions:
7136 * EVAL expr Push result of "expr"
7137 * JUMP_IF_FALSE elseif
7138 * ... body ...
7139 * JUMP_ALWAYS end
7140 * elseif:
7141 * EVAL expr Push result of "expr"
7142 * JUMP_IF_FALSE else
7143 * ... body ...
7144 * JUMP_ALWAYS end
7145 * else:
7146 * ... body ...
7147 * end:
7148 */
7149 static char_u *
7150compile_if(char_u *arg, cctx_T *cctx)
7151{
7152 char_u *p = arg;
7153 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007154 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007156 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007157 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158
Bram Moolenaara5565e42020-05-09 15:44:01 +02007159 CLEAR_FIELD(ppconst);
7160 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007161 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007162 clear_ppconst(&ppconst);
7163 return NULL;
7164 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007165 if (!ends_excmd2(arg, skipwhite(p)))
7166 {
7167 semsg(_(e_trailing_arg), p);
7168 return NULL;
7169 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007170 if (cctx->ctx_skip == SKIP_YES)
7171 clear_ppconst(&ppconst);
7172 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007173 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007174 int error = FALSE;
7175 int v;
7176
Bram Moolenaara5565e42020-05-09 15:44:01 +02007177 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007178 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007179 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007180 if (error)
7181 return NULL;
7182 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007183 }
7184 else
7185 {
7186 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007187 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007188 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007189 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007190 if (bool_on_stack(cctx) == FAIL)
7191 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007193
Bram Moolenaara91a7132021-03-25 21:12:15 +01007194 // CMDMOD_REV must come before the jump
7195 generate_undo_cmdmods(cctx);
7196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007197 scope = new_scope(cctx, IF_SCOPE);
7198 if (scope == NULL)
7199 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007200 scope->se_skip_save = skip_save;
7201 // "is_had_return" will be reset if any block does not end in :return
7202 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007203
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007204 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007205 {
7206 // "where" is set when ":elseif", "else" or ":endif" is found
7207 scope->se_u.se_if.is_if_label = instr->ga_len;
7208 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7209 }
7210 else
7211 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007212
Bram Moolenaarced68a02021-01-24 17:53:47 +01007213#ifdef FEAT_PROFILE
7214 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7215 && skip_save != SKIP_YES)
7216 {
7217 // generated a profile start, need to generate a profile end, since it
7218 // won't be done after returning
7219 cctx->ctx_skip = SKIP_NOT;
7220 generate_instr(cctx, ISN_PROF_END);
7221 cctx->ctx_skip = SKIP_YES;
7222 }
7223#endif
7224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225 return p;
7226}
7227
7228 static char_u *
7229compile_elseif(char_u *arg, cctx_T *cctx)
7230{
7231 char_u *p = arg;
7232 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007233 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234 isn_T *isn;
7235 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007236 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007237 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007238
7239 if (scope == NULL || scope->se_type != IF_SCOPE)
7240 {
7241 emsg(_(e_elseif_without_if));
7242 return NULL;
7243 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007244 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007245 if (!cctx->ctx_had_return)
7246 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007247
Bram Moolenaarced68a02021-01-24 17:53:47 +01007248 if (cctx->ctx_skip == SKIP_NOT)
7249 {
7250 // previous block was executed, this one and following will not
7251 cctx->ctx_skip = SKIP_YES;
7252 scope->se_u.se_if.is_seen_skip_not = TRUE;
7253 }
7254 if (scope->se_u.se_if.is_seen_skip_not)
7255 {
7256 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007257 // Do not count the "elseif" for profiling and cmdmod
7258 instr->ga_len = current_instr_idx(cctx);
7259
Bram Moolenaarced68a02021-01-24 17:53:47 +01007260 skip_expr_cctx(&p, cctx);
7261 return p;
7262 }
7263
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007264 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007265 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007266 int moved_cmdmod = FALSE;
7267
7268 // Move any CMDMOD instruction to after the jump
7269 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7270 {
7271 if (ga_grow(instr, 1) == FAIL)
7272 return NULL;
7273 ((isn_T *)instr->ga_data)[instr->ga_len] =
7274 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7275 --instr->ga_len;
7276 moved_cmdmod = TRUE;
7277 }
7278
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007279 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007280 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007281 return NULL;
7282 // previous "if" or "elseif" jumps here
7283 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7284 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007285 if (moved_cmdmod)
7286 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007287 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007288
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007289 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007290 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007291 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007292 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007293 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007294#ifdef FEAT_PROFILE
7295 if (cctx->ctx_profiling)
7296 {
7297 // the previous block was skipped, need to profile this line
7298 generate_instr(cctx, ISN_PROF_START);
7299 instr_count = instr->ga_len;
7300 }
7301#endif
7302 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007303 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007304 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007305 clear_ppconst(&ppconst);
7306 return NULL;
7307 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007308 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007309 if (!ends_excmd2(arg, skipwhite(p)))
7310 {
7311 semsg(_(e_trailing_arg), p);
7312 return NULL;
7313 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007314 if (scope->se_skip_save == SKIP_YES)
7315 clear_ppconst(&ppconst);
7316 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007317 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007318 int error = FALSE;
7319 int v;
7320
Bram Moolenaar7f141552020-05-09 17:35:53 +02007321 // The expression results in a constant.
7322 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007323 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7324 if (error)
7325 return NULL;
7326 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007327 clear_ppconst(&ppconst);
7328 scope->se_u.se_if.is_if_label = -1;
7329 }
7330 else
7331 {
7332 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007333 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007334 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007335 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007336 if (bool_on_stack(cctx) == FAIL)
7337 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338
Bram Moolenaara91a7132021-03-25 21:12:15 +01007339 // CMDMOD_REV must come before the jump
7340 generate_undo_cmdmods(cctx);
7341
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007342 // "where" is set when ":elseif", "else" or ":endif" is found
7343 scope->se_u.se_if.is_if_label = instr->ga_len;
7344 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7345 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346
7347 return p;
7348}
7349
7350 static char_u *
7351compile_else(char_u *arg, cctx_T *cctx)
7352{
7353 char_u *p = arg;
7354 garray_T *instr = &cctx->ctx_instr;
7355 isn_T *isn;
7356 scope_T *scope = cctx->ctx_scope;
7357
7358 if (scope == NULL || scope->se_type != IF_SCOPE)
7359 {
7360 emsg(_(e_else_without_if));
7361 return NULL;
7362 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007363 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007364 if (!cctx->ctx_had_return)
7365 scope->se_u.se_if.is_had_return = FALSE;
7366 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367
Bram Moolenaarced68a02021-01-24 17:53:47 +01007368#ifdef FEAT_PROFILE
7369 if (cctx->ctx_profiling)
7370 {
7371 if (cctx->ctx_skip == SKIP_NOT
7372 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7373 .isn_type == ISN_PROF_START)
7374 // the previous block was executed, do not count "else" for profiling
7375 --instr->ga_len;
7376 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7377 {
7378 // the previous block was not executed, this one will, do count the
7379 // "else" for profiling
7380 cctx->ctx_skip = SKIP_NOT;
7381 generate_instr(cctx, ISN_PROF_END);
7382 generate_instr(cctx, ISN_PROF_START);
7383 cctx->ctx_skip = SKIP_YES;
7384 }
7385 }
7386#endif
7387
7388 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007389 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007390 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007391 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007392 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007393 if (!cctx->ctx_had_return
7394 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7395 JUMP_ALWAYS, cctx) == FAIL)
7396 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007397 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007398
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007399 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007400 {
7401 if (scope->se_u.se_if.is_if_label >= 0)
7402 {
7403 // previous "if" or "elseif" jumps here
7404 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7405 isn->isn_arg.jump.jump_where = instr->ga_len;
7406 scope->se_u.se_if.is_if_label = -1;
7407 }
7408 }
7409
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007410 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007411 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7412 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413
7414 return p;
7415}
7416
7417 static char_u *
7418compile_endif(char_u *arg, cctx_T *cctx)
7419{
7420 scope_T *scope = cctx->ctx_scope;
7421 ifscope_T *ifscope;
7422 garray_T *instr = &cctx->ctx_instr;
7423 isn_T *isn;
7424
Bram Moolenaarfa984412021-03-25 22:15:28 +01007425 if (misplaced_cmdmod(cctx))
7426 return NULL;
7427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428 if (scope == NULL || scope->se_type != IF_SCOPE)
7429 {
7430 emsg(_(e_endif_without_if));
7431 return NULL;
7432 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007433 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007434 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007435 if (!cctx->ctx_had_return)
7436 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007437
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007438 if (scope->se_u.se_if.is_if_label >= 0)
7439 {
7440 // previous "if" or "elseif" jumps here
7441 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7442 isn->isn_arg.jump.jump_where = instr->ga_len;
7443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007445 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007446
7447#ifdef FEAT_PROFILE
7448 // even when skipping we count the endif as executed, unless the block it's
7449 // in is skipped
7450 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7451 && scope->se_skip_save != SKIP_YES)
7452 {
7453 cctx->ctx_skip = SKIP_NOT;
7454 generate_instr(cctx, ISN_PROF_START);
7455 }
7456#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007457 cctx->ctx_skip = scope->se_skip_save;
7458
7459 // If all the blocks end in :return and there is an :else then the
7460 // had_return flag is set.
7461 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007463 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464 return arg;
7465}
7466
7467/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007468 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469 *
7470 * Produces instructions:
7471 * PUSHNR -1
7472 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007473 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7475 * - if beyond end, jump to "end"
7476 * - otherwise get item from list and push it
7477 * STORE var Store item in "var"
7478 * ... body ...
7479 * JUMP top Jump back to repeat
7480 * end: DROP Drop the result of "expr"
7481 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007482 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7483 * UNPACK 2 Split item in 2
7484 * STORE var1 Store item in "var1"
7485 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 */
7487 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007488compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007490 char_u *arg;
7491 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007492 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007493 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007494 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007495 int var_count = 0;
7496 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007497 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007498 garray_T *stack = &cctx->ctx_type_stack;
7499 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007500 lvar_T *loop_lvar; // loop iteration variable
7501 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007503 type_T *item_type = &t_any;
7504 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007505
Bram Moolenaar792f7862020-11-23 08:31:18 +01007506 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007507 if (p == NULL)
7508 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007509 if (var_count == 0)
7510 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511
7512 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007513 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007514 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7515 return NULL;
7516 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517 {
7518 emsg(_(e_missing_in));
7519 return NULL;
7520 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007521 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007522 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7523 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007525 scope = new_scope(cctx, FOR_SCOPE);
7526 if (scope == NULL)
7527 return NULL;
7528
Bram Moolenaar792f7862020-11-23 08:31:18 +01007529 // Reserve a variable to store the loop iteration counter and initialize it
7530 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007531 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7532 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007533 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007534 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007535 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007536 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007537 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007538 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007539
7540 // compile "expr", it remains on the stack until "endfor"
7541 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007542 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007543 {
7544 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007545 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007546 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007547 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007549 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007550 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007552 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007553 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007554 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007555 semsg(_(e_for_loop_on_str_not_supported),
7556 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007557 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007558 return NULL;
7559 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007560
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007561 if (vartype->tt_type == VAR_STRING)
7562 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007563 else if (vartype->tt_type == VAR_BLOB)
7564 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007565 else if (vartype->tt_type == VAR_LIST
7566 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007567 {
7568 if (var_count == 1)
7569 item_type = vartype->tt_member;
7570 else if (vartype->tt_member->tt_type == VAR_LIST
7571 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007572 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007573 item_type = vartype->tt_member->tt_member;
7574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007575
Bram Moolenaara91a7132021-03-25 21:12:15 +01007576 // CMDMOD_REV must come before the FOR instruction
7577 generate_undo_cmdmods(cctx);
7578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007579 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007580 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007581 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007582
Bram Moolenaar792f7862020-11-23 08:31:18 +01007583 arg = arg_start;
7584 if (var_count > 1)
7585 {
7586 generate_UNPACK(cctx, var_count, semicolon);
7587 arg = skipwhite(arg + 1); // skip white after '['
7588
7589 // the list item is replaced by a number of items
7590 if (ga_grow(stack, var_count - 1) == FAIL)
7591 {
7592 drop_scope(cctx);
7593 return NULL;
7594 }
7595 --stack->ga_len;
7596 for (idx = 0; idx < var_count; ++idx)
7597 {
7598 ((type_T **)stack->ga_data)[stack->ga_len] =
7599 (semicolon && idx == 0) ? vartype : item_type;
7600 ++stack->ga_len;
7601 }
7602 }
7603
7604 for (idx = 0; idx < var_count; ++idx)
7605 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007606 assign_dest_T dest = dest_local;
7607 int opt_flags = 0;
7608 int vimvaridx = -1;
7609 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007610 type_T *lhs_type = &t_any;
7611 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007612
7613 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007614 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007615 name = vim_strnsave(arg, varlen);
7616 if (name == NULL)
7617 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007618 if (*p == ':')
7619 {
7620 p = skipwhite(p + 1);
7621 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7622 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007623
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007624 // TODO: script var not supported?
7625 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7626 &vimvaridx, &type, cctx) == FAIL)
7627 goto failed;
7628 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007629 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007630 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7631 0, 0, type, name) == FAIL)
7632 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007633 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007634 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007635 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007636 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007637 {
7638 semsg(_(e_variable_already_declared), arg);
7639 goto failed;
7640 }
7641
Bram Moolenaarea870692020-12-02 14:24:30 +01007642 if (STRNCMP(name, "s:", 2) == 0)
7643 {
7644 semsg(_(e_cannot_declare_script_variable_in_function), name);
7645 goto failed;
7646 }
7647
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007648 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007649 where.wt_index = var_count > 1 ? idx + 1 : 0;
7650 where.wt_variable = TRUE;
7651 if (lhs_type == &t_any)
7652 lhs_type = item_type;
7653 else if (item_type != &t_unknown
7654 && !(var_count > 1 && item_type == &t_any)
7655 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7656 goto failed;
7657 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007658 if (var_lvar == NULL)
7659 // out of memory or used as an argument
7660 goto failed;
7661
7662 if (semicolon && idx == var_count - 1)
7663 var_lvar->lv_type = vartype;
7664 else
7665 var_lvar->lv_type = item_type;
7666 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7667 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007668
7669 if (*p == ',' || *p == ';')
7670 ++p;
7671 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007672 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007673 }
7674
7675 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007676
7677failed:
7678 vim_free(name);
7679 drop_scope(cctx);
7680 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681}
7682
7683/*
7684 * compile "endfor"
7685 */
7686 static char_u *
7687compile_endfor(char_u *arg, cctx_T *cctx)
7688{
7689 garray_T *instr = &cctx->ctx_instr;
7690 scope_T *scope = cctx->ctx_scope;
7691 forscope_T *forscope;
7692 isn_T *isn;
7693
Bram Moolenaarfa984412021-03-25 22:15:28 +01007694 if (misplaced_cmdmod(cctx))
7695 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007697 if (scope == NULL || scope->se_type != FOR_SCOPE)
7698 {
7699 emsg(_(e_for));
7700 return NULL;
7701 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007702 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007703 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007704 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705
7706 // At end of ":for" scope jump back to the FOR instruction.
7707 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7708
7709 // Fill in the "end" label in the FOR statement so it can jump here
7710 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7711 isn->isn_arg.forloop.for_end = instr->ga_len;
7712
7713 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007714 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715
7716 // Below the ":for" scope drop the "expr" list from the stack.
7717 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7718 return NULL;
7719
7720 vim_free(scope);
7721
7722 return arg;
7723}
7724
7725/*
7726 * compile "while expr"
7727 *
7728 * Produces instructions:
7729 * top: EVAL expr Push result of "expr"
7730 * JUMP_IF_FALSE end jump if false
7731 * ... body ...
7732 * JUMP top Jump back to repeat
7733 * end:
7734 *
7735 */
7736 static char_u *
7737compile_while(char_u *arg, cctx_T *cctx)
7738{
7739 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740 scope_T *scope;
7741
7742 scope = new_scope(cctx, WHILE_SCOPE);
7743 if (scope == NULL)
7744 return NULL;
7745
Bram Moolenaara91a7132021-03-25 21:12:15 +01007746 // "endwhile" jumps back here, one before when profiling or using cmdmods
7747 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007748
7749 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007750 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007751 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007752 if (!ends_excmd2(arg, skipwhite(p)))
7753 {
7754 semsg(_(e_trailing_arg), p);
7755 return NULL;
7756 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007757
Bram Moolenaar13106602020-10-04 16:06:05 +02007758 if (bool_on_stack(cctx) == FAIL)
7759 return FAIL;
7760
Bram Moolenaara91a7132021-03-25 21:12:15 +01007761 // CMDMOD_REV must come before the jump
7762 generate_undo_cmdmods(cctx);
7763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007764 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007765 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007766 JUMP_IF_FALSE, cctx) == FAIL)
7767 return FAIL;
7768
7769 return p;
7770}
7771
7772/*
7773 * compile "endwhile"
7774 */
7775 static char_u *
7776compile_endwhile(char_u *arg, cctx_T *cctx)
7777{
7778 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007779 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007780
Bram Moolenaarfa984412021-03-25 22:15:28 +01007781 if (misplaced_cmdmod(cctx))
7782 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007783 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7784 {
7785 emsg(_(e_while));
7786 return NULL;
7787 }
7788 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007789 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790
Bram Moolenaarf002a412021-01-24 13:34:18 +01007791#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007792 // count the endwhile before jumping
7793 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007794#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007796 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007797 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798
7799 // Fill in the "end" label in the WHILE statement so it can jump here.
7800 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007801 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7802 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803
7804 vim_free(scope);
7805
7806 return arg;
7807}
7808
7809/*
7810 * compile "continue"
7811 */
7812 static char_u *
7813compile_continue(char_u *arg, cctx_T *cctx)
7814{
7815 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007816 int try_scopes = 0;
7817 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818
7819 for (;;)
7820 {
7821 if (scope == NULL)
7822 {
7823 emsg(_(e_continue));
7824 return NULL;
7825 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007826 if (scope->se_type == FOR_SCOPE)
7827 {
7828 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007829 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007830 }
7831 if (scope->se_type == WHILE_SCOPE)
7832 {
7833 loop_label = scope->se_u.se_while.ws_top_label;
7834 break;
7835 }
7836 if (scope->se_type == TRY_SCOPE)
7837 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007838 scope = scope->se_outer;
7839 }
7840
Bram Moolenaarc150c092021-02-13 15:02:46 +01007841 if (try_scopes > 0)
7842 // Inside one or more try/catch blocks we first need to jump to the
7843 // "finally" or "endtry" to cleanup.
7844 generate_TRYCONT(cctx, try_scopes, loop_label);
7845 else
7846 // Jump back to the FOR or WHILE instruction.
7847 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849 return arg;
7850}
7851
7852/*
7853 * compile "break"
7854 */
7855 static char_u *
7856compile_break(char_u *arg, cctx_T *cctx)
7857{
7858 scope_T *scope = cctx->ctx_scope;
7859 endlabel_T **el;
7860
7861 for (;;)
7862 {
7863 if (scope == NULL)
7864 {
7865 emsg(_(e_break));
7866 return NULL;
7867 }
7868 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7869 break;
7870 scope = scope->se_outer;
7871 }
7872
7873 // Jump to the end of the FOR or WHILE loop.
7874 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007875 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007876 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007877 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7879 return FAIL;
7880
7881 return arg;
7882}
7883
7884/*
7885 * compile "{" start of block
7886 */
7887 static char_u *
7888compile_block(char_u *arg, cctx_T *cctx)
7889{
7890 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7891 return NULL;
7892 return skipwhite(arg + 1);
7893}
7894
7895/*
7896 * compile end of block: drop one scope
7897 */
7898 static void
7899compile_endblock(cctx_T *cctx)
7900{
7901 scope_T *scope = cctx->ctx_scope;
7902
7903 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007904 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007905 vim_free(scope);
7906}
7907
7908/*
7909 * compile "try"
7910 * Creates a new scope for the try-endtry, pointing to the first catch and
7911 * finally.
7912 * Creates another scope for the "try" block itself.
7913 * TRY instruction sets up exception handling at runtime.
7914 *
7915 * "try"
7916 * TRY -> catch1, -> finally push trystack entry
7917 * ... try block
7918 * "throw {exception}"
7919 * EVAL {exception}
7920 * THROW create exception
7921 * ... try block
7922 * " catch {expr}"
7923 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007924 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007925 * EVAL {expr}
7926 * MATCH
7927 * JUMP nomatch -> catch2
7928 * CATCH remove exception
7929 * ... catch block
7930 * " catch"
7931 * JUMP -> finally
7932 * catch2: CATCH remove exception
7933 * ... catch block
7934 * " finally"
7935 * finally:
7936 * ... finally block
7937 * " endtry"
7938 * ENDTRY pop trystack entry, may rethrow
7939 */
7940 static char_u *
7941compile_try(char_u *arg, cctx_T *cctx)
7942{
7943 garray_T *instr = &cctx->ctx_instr;
7944 scope_T *try_scope;
7945 scope_T *scope;
7946
Bram Moolenaarfa984412021-03-25 22:15:28 +01007947 if (misplaced_cmdmod(cctx))
7948 return NULL;
7949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950 // scope that holds the jumps that go to catch/finally/endtry
7951 try_scope = new_scope(cctx, TRY_SCOPE);
7952 if (try_scope == NULL)
7953 return NULL;
7954
Bram Moolenaar69f70502021-01-01 16:10:46 +01007955 if (cctx->ctx_skip != SKIP_YES)
7956 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007957 isn_T *isn;
7958
7959 // "try_catch" is set when the first ":catch" is found or when no catch
7960 // is found and ":finally" is found.
7961 // "try_finally" is set when ":finally" is found
7962 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007963 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007964 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7965 return NULL;
7966 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7967 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007968 return NULL;
7969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007970
7971 // scope for the try block itself
7972 scope = new_scope(cctx, BLOCK_SCOPE);
7973 if (scope == NULL)
7974 return NULL;
7975
7976 return arg;
7977}
7978
7979/*
7980 * compile "catch {expr}"
7981 */
7982 static char_u *
7983compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7984{
7985 scope_T *scope = cctx->ctx_scope;
7986 garray_T *instr = &cctx->ctx_instr;
7987 char_u *p;
7988 isn_T *isn;
7989
Bram Moolenaarfa984412021-03-25 22:15:28 +01007990 if (misplaced_cmdmod(cctx))
7991 return NULL;
7992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007993 // end block scope from :try or :catch
7994 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7995 compile_endblock(cctx);
7996 scope = cctx->ctx_scope;
7997
7998 // Error if not in a :try scope
7999 if (scope == NULL || scope->se_type != TRY_SCOPE)
8000 {
8001 emsg(_(e_catch));
8002 return NULL;
8003 }
8004
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008005 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008006 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008007 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008008 return NULL;
8009 }
8010
Bram Moolenaar69f70502021-01-01 16:10:46 +01008011 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008012 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008013#ifdef FEAT_PROFILE
8014 // the profile-start should be after the jump
8015 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8016 .isn_type == ISN_PROF_START)
8017 --instr->ga_len;
8018#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008019 // Jump from end of previous block to :finally or :endtry
8020 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8021 JUMP_ALWAYS, cctx) == FAIL)
8022 return NULL;
8023
8024 // End :try or :catch scope: set value in ISN_TRY instruction
8025 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008026 if (isn->isn_arg.try.try_ref->try_catch == 0)
8027 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008028 if (scope->se_u.se_try.ts_catch_label != 0)
8029 {
8030 // Previous catch without match jumps here
8031 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8032 isn->isn_arg.jump.jump_where = instr->ga_len;
8033 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008034#ifdef FEAT_PROFILE
8035 if (cctx->ctx_profiling)
8036 {
8037 // a "throw" that jumps here needs to be counted
8038 generate_instr(cctx, ISN_PROF_END);
8039 // the "catch" is also counted
8040 generate_instr(cctx, ISN_PROF_START);
8041 }
8042#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008043 }
8044
8045 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008046 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008047 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008048 scope->se_u.se_try.ts_caught_all = TRUE;
8049 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008050 }
8051 else
8052 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008053 char_u *end;
8054 char_u *pat;
8055 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008056 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008057 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008059 // Push v:exception, push {expr} and MATCH
8060 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8061
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008062 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008063 if (*end != *p)
8064 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008065 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008066 vim_free(tofree);
8067 return FAIL;
8068 }
8069 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008070 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008071 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008072 len = (int)(end - tofree);
8073 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008074 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008075 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008076 if (pat == NULL)
8077 return FAIL;
8078 if (generate_PUSHS(cctx, pat) == FAIL)
8079 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008081 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8082 return NULL;
8083
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008084 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008085 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8086 return NULL;
8087 }
8088
Bram Moolenaar69f70502021-01-01 16:10:46 +01008089 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008090 return NULL;
8091
8092 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8093 return NULL;
8094 return p;
8095}
8096
8097 static char_u *
8098compile_finally(char_u *arg, cctx_T *cctx)
8099{
8100 scope_T *scope = cctx->ctx_scope;
8101 garray_T *instr = &cctx->ctx_instr;
8102 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008103 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008104
Bram Moolenaarfa984412021-03-25 22:15:28 +01008105 if (misplaced_cmdmod(cctx))
8106 return NULL;
8107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008108 // end block scope from :try or :catch
8109 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8110 compile_endblock(cctx);
8111 scope = cctx->ctx_scope;
8112
8113 // Error if not in a :try scope
8114 if (scope == NULL || scope->se_type != TRY_SCOPE)
8115 {
8116 emsg(_(e_finally));
8117 return NULL;
8118 }
8119
8120 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008121 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008122 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008123 {
8124 emsg(_(e_finally_dup));
8125 return NULL;
8126 }
8127
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008128 this_instr = instr->ga_len;
8129#ifdef FEAT_PROFILE
8130 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8131 .isn_type == ISN_PROF_START)
8132 // jump to the profile start of the "finally"
8133 --this_instr;
8134#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008135
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008136 // Fill in the "end" label in jumps at the end of the blocks.
8137 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8138 this_instr, cctx);
8139
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008140 // If there is no :catch then an exception jumps to :finally.
8141 if (isn->isn_arg.try.try_ref->try_catch == 0)
8142 isn->isn_arg.try.try_ref->try_catch = this_instr;
8143 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008144 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008145 {
8146 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008147 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008148 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008149 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008150 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008151 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8152 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008154 // TODO: set index in ts_finally_label jumps
8155
8156 return arg;
8157}
8158
8159 static char_u *
8160compile_endtry(char_u *arg, cctx_T *cctx)
8161{
8162 scope_T *scope = cctx->ctx_scope;
8163 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008164 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008165
Bram Moolenaarfa984412021-03-25 22:15:28 +01008166 if (misplaced_cmdmod(cctx))
8167 return NULL;
8168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008169 // end block scope from :catch or :finally
8170 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8171 compile_endblock(cctx);
8172 scope = cctx->ctx_scope;
8173
8174 // Error if not in a :try scope
8175 if (scope == NULL || scope->se_type != TRY_SCOPE)
8176 {
8177 if (scope == NULL)
8178 emsg(_(e_no_endtry));
8179 else if (scope->se_type == WHILE_SCOPE)
8180 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008181 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008182 emsg(_(e_endfor));
8183 else
8184 emsg(_(e_endif));
8185 return NULL;
8186 }
8187
Bram Moolenaarc150c092021-02-13 15:02:46 +01008188 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008189 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008190 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008191 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8192 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008193 {
8194 emsg(_(e_missing_catch_or_finally));
8195 return NULL;
8196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008197
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008198#ifdef FEAT_PROFILE
8199 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8200 .isn_type == ISN_PROF_START)
8201 // move the profile start after "endtry" so that it's not counted when
8202 // the exception is rethrown.
8203 --instr->ga_len;
8204#endif
8205
Bram Moolenaar69f70502021-01-01 16:10:46 +01008206 // Fill in the "end" label in jumps at the end of the blocks, if not
8207 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008208 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8209 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008210
Bram Moolenaar69f70502021-01-01 16:10:46 +01008211 if (scope->se_u.se_try.ts_catch_label != 0)
8212 {
8213 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008214 isn_T *isn = ((isn_T *)instr->ga_data)
8215 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008216 isn->isn_arg.jump.jump_where = instr->ga_len;
8217 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008218 }
8219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008220 compile_endblock(cctx);
8221
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008222 if (cctx->ctx_skip != SKIP_YES)
8223 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008224 // End :catch or :finally scope: set instruction index in ISN_TRY
8225 // instruction
8226 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008227 if (cctx->ctx_skip != SKIP_YES
8228 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8229 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008230#ifdef FEAT_PROFILE
8231 if (cctx->ctx_profiling)
8232 generate_instr(cctx, ISN_PROF_START);
8233#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008235 return arg;
8236}
8237
8238/*
8239 * compile "throw {expr}"
8240 */
8241 static char_u *
8242compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8243{
8244 char_u *p = skipwhite(arg);
8245
Bram Moolenaara5565e42020-05-09 15:44:01 +02008246 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008247 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008248 if (cctx->ctx_skip == SKIP_YES)
8249 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008250 if (may_generate_2STRING(-1, cctx) == FAIL)
8251 return NULL;
8252 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8253 return NULL;
8254
8255 return p;
8256}
8257
8258/*
8259 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008260 * compile "echomsg expr"
8261 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008262 * compile "execute expr"
8263 */
8264 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008265compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008266{
8267 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008268 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008269 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008270 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008271
8272 for (;;)
8273 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008274 if (ends_excmd2(prev, p))
8275 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008276 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008277 return NULL;
8278 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008279 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008280 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008281 }
8282
Bram Moolenaare4984292020-12-13 14:19:25 +01008283 if (count > 0)
8284 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008285 long save_lnum = cctx->ctx_lnum;
8286
8287 // Use the line number where the command started.
8288 cctx->ctx_lnum = start_ctx_lnum;
8289
Bram Moolenaare4984292020-12-13 14:19:25 +01008290 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8291 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8292 else if (cmdidx == CMD_execute)
8293 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8294 else if (cmdidx == CMD_echomsg)
8295 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8296 else
8297 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008298
8299 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008300 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008301 return p;
8302}
8303
8304/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008305 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008306 * instruction to compute it and return OK.
8307 * Otherwise return FAIL, the caller must deal with any range.
8308 */
8309 static int
8310compile_variable_range(exarg_T *eap, cctx_T *cctx)
8311{
8312 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8313 char_u *p = skipdigits(eap->cmd);
8314
8315 if (p == range_end)
8316 return FAIL;
8317 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8318}
8319
8320/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008321 * :put r
8322 * :put ={expr}
8323 */
8324 static char_u *
8325compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8326{
8327 char_u *line = arg;
8328 linenr_T lnum;
8329 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008330 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008331
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008332 eap->regname = *line;
8333
8334 if (eap->regname == '=')
8335 {
8336 char_u *p = line + 1;
8337
8338 if (compile_expr0(&p, cctx) == FAIL)
8339 return NULL;
8340 line = p;
8341 }
8342 else if (eap->regname != NUL)
8343 ++line;
8344
Bram Moolenaar08597872020-12-10 19:43:40 +01008345 if (compile_variable_range(eap, cctx) == OK)
8346 {
8347 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8348 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008349 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008350 {
8351 // Either no range or a number.
8352 // "errormsg" will not be set because the range is ADDR_LINES.
8353 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008354 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008355 return NULL;
8356 if (eap->addr_count == 0)
8357 lnum = -1;
8358 else
8359 lnum = eap->line2;
8360 if (above)
8361 --lnum;
8362 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008363
8364 generate_PUT(cctx, eap->regname, lnum);
8365 return line;
8366}
8367
8368/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008369 * A command that is not compiled, execute with legacy code.
8370 */
8371 static char_u *
8372compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8373{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008374 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008375 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008376 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008377
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008378 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008379 goto theend;
8380
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008381 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008382 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008383 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008384 int usefilter = FALSE;
8385
8386 has_expr = argt & (EX_XFILE | EX_EXPAND);
8387
8388 // If the command can be followed by a bar, find the bar and truncate
8389 // it, so that the following command can be compiled.
8390 // The '|' is overwritten with a NUL, it is put back below.
8391 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8392 && *eap->arg == '!')
8393 // :w !filter or :r !filter or :r! filter
8394 usefilter = TRUE;
8395 if ((argt & EX_TRLBAR) && !usefilter)
8396 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008397 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008398 separate_nextcmd(eap);
8399 if (eap->nextcmd != NULL)
8400 nextcmd = eap->nextcmd;
8401 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008402 else if (eap->cmdidx == CMD_wincmd)
8403 {
8404 p = eap->arg;
8405 if (*p != NUL)
8406 ++p;
8407 if (*p == 'g' || *p == Ctrl_G)
8408 ++p;
8409 p = skipwhite(p);
8410 if (*p == '|')
8411 {
8412 *p = NUL;
8413 nextcmd = p + 1;
8414 }
8415 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008416 }
8417
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008418 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8419 {
8420 // expand filename in "syntax include [@group] filename"
8421 has_expr = TRUE;
8422 eap->arg = skipwhite(eap->arg + 7);
8423 if (*eap->arg == '@')
8424 eap->arg = skiptowhite(eap->arg);
8425 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008426
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008427 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8428 && STRLEN(eap->arg) > 4)
8429 {
8430 int delim = *eap->arg;
8431
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008432 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008433 if (*p == delim)
8434 {
8435 eap->arg = p + 1;
8436 has_expr = TRUE;
8437 }
8438 }
8439
Bram Moolenaarecac5912021-01-05 19:23:28 +01008440 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8441 {
8442 // TODO: should only expand when appropriate for the command
8443 eap->arg = skiptowhite(eap->arg);
8444 has_expr = TRUE;
8445 }
8446
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008447 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008448 {
8449 int count = 0;
8450 char_u *start = skipwhite(line);
8451
8452 // :cmd xxx`=expr1`yyy`=expr2`zzz
8453 // PUSHS ":cmd xxx"
8454 // eval expr1
8455 // PUSHS "yyy"
8456 // eval expr2
8457 // PUSHS "zzz"
8458 // EXECCONCAT 5
8459 for (;;)
8460 {
8461 if (p > start)
8462 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008463 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008464 ++count;
8465 }
8466 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008467 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008468 return NULL;
8469 may_generate_2STRING(-1, cctx);
8470 ++count;
8471 p = skipwhite(p);
8472 if (*p != '`')
8473 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008474 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008475 return NULL;
8476 }
8477 start = p + 1;
8478
8479 p = (char_u *)strstr((char *)start, "`=");
8480 if (p == NULL)
8481 {
8482 if (*skipwhite(start) != NUL)
8483 {
8484 generate_PUSHS(cctx, vim_strsave(start));
8485 ++count;
8486 }
8487 break;
8488 }
8489 }
8490 generate_EXECCONCAT(cctx, count);
8491 }
8492 else
8493 generate_EXEC(cctx, line);
8494
8495theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008496 if (*nextcmd != NUL)
8497 {
8498 // the parser expects a pointer to the bar, put it back
8499 --nextcmd;
8500 *nextcmd = '|';
8501 }
8502
8503 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008504}
8505
Bram Moolenaar8238f082021-04-20 21:10:48 +02008506
8507 static void
8508clear_instr_ga(garray_T *gap)
8509{
8510 int idx;
8511
8512 for (idx = 0; idx < gap->ga_len; ++idx)
8513 delete_instr(((isn_T *)gap->ga_data) + idx);
8514 ga_clear(gap);
8515}
8516
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008517/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008518 * :s/pat/repl/
8519 */
8520 static char_u *
8521compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8522{
8523 char_u *cmd = eap->arg;
8524 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8525
8526 if (expr != NULL)
8527 {
8528 int delimiter = *cmd++;
8529
8530 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008531 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008532 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8533 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008534 garray_T save_ga = cctx->ctx_instr;
8535 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008536 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008537 int trailing_error;
8538 int instr_count;
8539 isn_T *instr = NULL;
8540 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008541
8542 cmd += 3;
8543 end = skip_substitute(cmd, delimiter);
8544
Bram Moolenaar8238f082021-04-20 21:10:48 +02008545 // Temporarily reset the list of instructions so that the jumps
8546 // labels are correct.
8547 cctx->ctx_instr.ga_len = 0;
8548 cctx->ctx_instr.ga_maxlen = 0;
8549 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008550 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008551 if (end[-1] == NUL)
8552 end[-1] = delimiter;
8553 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008554 trailing_error = *cmd != delimiter && *cmd != NUL;
8555
Bram Moolenaar169502f2021-04-21 12:19:35 +02008556 if (expr_res == FAIL || trailing_error
8557 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008558 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008559 if (trailing_error)
8560 semsg(_(e_trailing_arg), cmd);
8561 clear_instr_ga(&cctx->ctx_instr);
8562 cctx->ctx_instr = save_ga;
8563 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008564 return NULL;
8565 }
8566
Bram Moolenaar8238f082021-04-20 21:10:48 +02008567 // Move the generated instructions into the ISN_SUBSTITUTE
8568 // instructions, then restore the list of instructions before
8569 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008570 instr_count = cctx->ctx_instr.ga_len;
8571 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008572 instr[instr_count].isn_type = ISN_FINISH;
8573
8574 cctx->ctx_instr = save_ga;
8575 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8576 {
8577 int idx;
8578
8579 for (idx = 0; idx < instr_count; ++idx)
8580 delete_instr(instr + idx);
8581 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008582 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008583 }
8584 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8585 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008586
8587 // skip over flags
8588 if (*end == '&')
8589 ++end;
8590 while (ASCII_ISALPHA(*end) || *end == '#')
8591 ++end;
8592 return end;
8593 }
8594 }
8595
8596 return compile_exec(arg, eap, cctx);
8597}
8598
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008599 static char_u *
8600compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8601{
8602 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008603 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008604
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008605 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008606 {
8607 if (STRNCMP(arg, "END", 3) == 0)
8608 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008609 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008610 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008611 if (compile_load_lhs(lhs, lhs->lhs_name, NULL, cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008612 return NULL;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008613 if (lhs->lhs_has_index)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008614 emsg("redir with index not implemented yet");
8615 }
8616
8617 // Gets the redirected text and put it on the stack, then store it
8618 // in the variable.
8619 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8620
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008621 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008622 generate_instr_drop(cctx, ISN_CONCAT, 1);
8623
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008624 if (lhs->lhs_has_index)
8625 {
8626 // Use the info in "lhs" to store the value at the index in the
8627 // list or dict.
8628 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8629 &t_string, cctx) == FAIL)
8630 return NULL;
8631 }
8632 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008633 return NULL;
8634
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008635 VIM_CLEAR(lhs->lhs_name);
8636 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008637 return arg + 3;
8638 }
8639 emsg(_(e_cannot_nest_redir));
8640 return NULL;
8641 }
8642
8643 if (arg[0] == '=' && arg[1] == '>')
8644 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008645 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008646
8647 // redirect to a variable is compiled
8648 arg += 2;
8649 if (*arg == '>')
8650 {
8651 ++arg;
8652 append = TRUE;
8653 }
8654 arg = skipwhite(arg);
8655
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008656 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008657 FALSE, FALSE, 1, cctx) == FAIL)
8658 return NULL;
8659 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008660 lhs->lhs_append = append;
8661 if (lhs->lhs_has_index)
8662 {
8663 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8664 if (lhs->lhs_whole == NULL)
8665 return NULL;
8666 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008667
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008668 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008669 }
8670
8671 // other redirects are handled like at script level
8672 return compile_exec(line, eap, cctx);
8673}
8674
Bram Moolenaar4c137212021-04-19 16:48:48 +02008675/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008676 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008677 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008678 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008679 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008680add_def_function(ufunc_T *ufunc)
8681{
8682 dfunc_T *dfunc;
8683
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008684 if (def_functions.ga_len == 0)
8685 {
8686 // The first position is not used, so that a zero uf_dfunc_idx means it
8687 // wasn't set.
8688 if (ga_grow(&def_functions, 1) == FAIL)
8689 return FAIL;
8690 ++def_functions.ga_len;
8691 }
8692
Bram Moolenaar09689a02020-05-09 22:50:08 +02008693 // Add the function to "def_functions".
8694 if (ga_grow(&def_functions, 1) == FAIL)
8695 return FAIL;
8696 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8697 CLEAR_POINTER(dfunc);
8698 dfunc->df_idx = def_functions.ga_len;
8699 ufunc->uf_dfunc_idx = dfunc->df_idx;
8700 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008701 dfunc->df_name = vim_strsave(ufunc->uf_name);
8702 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008703 ++def_functions.ga_len;
8704 return OK;
8705}
8706
8707/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008708 * After ex_function() has collected all the function lines: parse and compile
8709 * the lines into instructions.
8710 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008711 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8712 * the return statement (used for lambda). When uf_ret_type is already set
8713 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008714 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008715 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008716 * This can be used recursively through compile_lambda(), which may reallocate
8717 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008718 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008719 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008720 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008721compile_def_function(
8722 ufunc_T *ufunc,
8723 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008724 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008725 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008726{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008727 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008728 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008729 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008730 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008731 cctx_T cctx;
8732 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008733 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008734 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008735 int ret = FAIL;
8736 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008737 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008738 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008739 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008740#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008741 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008742#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008743
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008744 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008745 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008746 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008747 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008748 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8749 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008750 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008751 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008752 else
8753 {
8754 if (add_def_function(ufunc) == FAIL)
8755 return FAIL;
8756 new_def_function = TRUE;
8757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008758
Bram Moolenaar985116a2020-07-12 17:31:09 +02008759 ufunc->uf_def_status = UF_COMPILING;
8760
Bram Moolenaara80faa82020-04-12 19:37:17 +02008761 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008762
Bram Moolenaarf002a412021-01-24 13:34:18 +01008763#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008764 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008765#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008766 cctx.ctx_ufunc = ufunc;
8767 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008768 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008769 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8770 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8771 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8772 cctx.ctx_type_list = &ufunc->uf_type_list;
8773 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8774 instr = &cctx.ctx_instr;
8775
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008776 // Set the context to the function, it may be compiled when called from
8777 // another script. Set the script version to the most modern one.
8778 // The line number will be set in next_line_from_context().
8779 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008780 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8781
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008782 // Make sure error messages are OK.
8783 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8784 if (do_estack_push)
8785 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008786 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008787
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008788 if (ufunc->uf_def_args.ga_len > 0)
8789 {
8790 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008791 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008792 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008793 int i;
8794 char_u *arg;
8795 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008796 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008797
8798 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008799 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008800 for (i = 0; i < count; ++i)
8801 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008802 garray_T *stack = &cctx.ctx_type_stack;
8803 type_T *val_type;
8804 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008805 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008806 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008807 int jump_instr_idx = instr->ga_len;
8808 isn_T *isn;
8809
8810 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8811 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8812 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008813
8814 // Make sure later arguments are not found.
8815 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008816
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008817 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008818 r = compile_expr0(&arg, &cctx);
8819
8820 ufunc->uf_args.ga_len = uf_args_len;
8821 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008822 goto erret;
8823
8824 // If no type specified use the type of the default value.
8825 // Otherwise check that the default value type matches the
8826 // specified type.
8827 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008828 where.wt_index = arg_idx + 1;
8829 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008830 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008831 {
8832 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008833 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008834 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008835 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008836 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008837 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008838
8839 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008840 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008841
8842 // set instruction index in JUMP_IF_ARG_SET to here
8843 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8844 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008845 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008846
8847 if (did_set_arg_type)
8848 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008849 }
8850
8851 /*
8852 * Loop over all the lines of the function and generate instructions.
8853 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008854 for (;;)
8855 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008856 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008857 int starts_with_colon = FALSE;
8858 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008859 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008860
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008861 // Bail out on the first error to avoid a flood of errors and report
8862 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008863 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008864 goto erret;
8865
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008866 if (line != NULL && *line == '|')
8867 // the line continues after a '|'
8868 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008869 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008870 && !(*line == '#' && (line == cctx.ctx_line_start
8871 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008872 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008873 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008874 goto erret;
8875 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008876 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8877 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008878 else
8879 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008880 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008881 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008882 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008883 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008884#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008885 if (cctx.ctx_skip != SKIP_YES)
8886 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008887#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008888 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008889 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008890 // Make a copy, splitting off nextcmd and removing trailing spaces
8891 // may change it.
8892 if (line != NULL)
8893 {
8894 line = vim_strsave(line);
8895 vim_free(line_to_free);
8896 line_to_free = line;
8897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008898 }
8899
Bram Moolenaara80faa82020-04-12 19:37:17 +02008900 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008901 ea.cmdlinep = &line;
8902 ea.cmd = skipwhite(line);
8903
Bram Moolenaarb2049902021-01-24 12:53:53 +01008904 if (*ea.cmd == '#')
8905 {
8906 // "#" starts a comment
8907 line = (char_u *)"";
8908 continue;
8909 }
8910
Bram Moolenaarf002a412021-01-24 13:34:18 +01008911#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008912 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8913 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008914 {
8915 may_generate_prof_end(&cctx, prof_lnum);
8916
8917 prof_lnum = cctx.ctx_lnum;
8918 generate_instr(&cctx, ISN_PROF_START);
8919 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008920#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008921
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008922 // Some things can be recognized by the first character.
8923 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008924 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008925 case '}':
8926 {
8927 // "}" ends a block scope
8928 scopetype_T stype = cctx.ctx_scope == NULL
8929 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008930
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008931 if (stype == BLOCK_SCOPE)
8932 {
8933 compile_endblock(&cctx);
8934 line = ea.cmd;
8935 }
8936 else
8937 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008938 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008939 goto erret;
8940 }
8941 if (line != NULL)
8942 line = skipwhite(ea.cmd + 1);
8943 continue;
8944 }
8945
8946 case '{':
8947 // "{" starts a block scope
8948 // "{'a': 1}->func() is something else
8949 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8950 {
8951 line = compile_block(ea.cmd, &cctx);
8952 continue;
8953 }
8954 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008955 }
8956
8957 /*
8958 * COMMAND MODIFIERS
8959 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008960 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008961 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8962 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008963 {
8964 if (errormsg != NULL)
8965 goto erret;
8966 // empty line or comment
8967 line = (char_u *)"";
8968 continue;
8969 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008970 generate_cmdmods(&cctx, &local_cmdmod);
8971 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008972
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008973 // Check if there was a colon after the last command modifier or before
8974 // the current position.
8975 for (p = ea.cmd; p >= line; --p)
8976 {
8977 if (*p == ':')
8978 starts_with_colon = TRUE;
8979 if (p < ea.cmd && !VIM_ISWHITE(*p))
8980 break;
8981 }
8982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008983 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008984 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008985 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008986 {
8987 if (*ea.cmd == '(')
8988 // not for "call()"
8989 ea.cmd = p;
8990 else
8991 ea.cmd = skipwhite(ea.cmd);
8992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008993
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008994 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008995 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008996 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008997
Bram Moolenaar17126b12021-01-07 22:03:02 +01008998 // Check for assignment after command modifiers.
8999 assign = may_compile_assignment(&ea, &line, &cctx);
9000 if (assign == OK)
9001 goto nextline;
9002 if (assign == FAIL)
9003 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009004 }
9005
9006 /*
9007 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009008 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009009 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009010 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02009011 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009012 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009013 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009014 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009015 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009016 if (!starts_with_colon)
9017 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009018 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009019 goto erret;
9020 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009021 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009022 if (ends_excmd2(line, ea.cmd))
9023 {
9024 // A range without a command: jump to the line.
9025 // TODO: compile to a more efficient command, possibly
9026 // calling parse_cmd_address().
9027 ea.cmdidx = CMD_SIZE;
9028 line = compile_exec(line, &ea, &cctx);
9029 goto nextline;
9030 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009031 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009032 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009033 p = find_ex_command(&ea, NULL, starts_with_colon
9034 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009035
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009036 if (p == NULL)
9037 {
9038 if (cctx.ctx_skip != SKIP_YES)
9039 emsg(_(e_ambiguous_use_of_user_defined_command));
9040 goto erret;
9041 }
9042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009043 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9044 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009045 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009046 {
9047 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009048 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009049 }
9050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009051 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009052 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009053 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009054 // CMD_var cannot happen, compile_assignment() above would be
9055 // used. Most likely an assignment to a non-existing variable.
9056 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009057 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009059 }
9060
Bram Moolenaar3988f642020-08-27 22:43:03 +02009061 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009062 && ea.cmdidx != CMD_elseif
9063 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009064 && ea.cmdidx != CMD_endif
9065 && ea.cmdidx != CMD_endfor
9066 && ea.cmdidx != CMD_endwhile
9067 && ea.cmdidx != CMD_catch
9068 && ea.cmdidx != CMD_finally
9069 && ea.cmdidx != CMD_endtry)
9070 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009071 emsg(_(e_unreachable_code_after_return));
9072 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009073 }
9074
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009075 p = skipwhite(p);
9076 if (ea.cmdidx != CMD_SIZE
9077 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9078 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009079 if (ea.cmdidx >= 0)
9080 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009081 if ((ea.argt & EX_BANG) && *p == '!')
9082 {
9083 ea.forceit = TRUE;
9084 p = skipwhite(p + 1);
9085 }
9086 }
9087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009088 switch (ea.cmdidx)
9089 {
9090 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009091 ea.arg = p;
9092 line = compile_nested_function(&ea, &cctx);
9093 break;
9094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009095 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009096 // TODO: should we allow this, e.g. to declare a global
9097 // function?
9098 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009099 goto erret;
9100
9101 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009102 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009103 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009104 break;
9105
9106 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009107 emsg(_(e_cannot_use_let_in_vim9_script));
9108 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009109 case CMD_var:
9110 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009111 case CMD_const:
9112 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009113 if (line == p)
9114 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009115 break;
9116
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009117 case CMD_unlet:
9118 case CMD_unlockvar:
9119 case CMD_lockvar:
9120 line = compile_unletlock(p, &ea, &cctx);
9121 break;
9122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009123 case CMD_import:
9124 line = compile_import(p, &cctx);
9125 break;
9126
9127 case CMD_if:
9128 line = compile_if(p, &cctx);
9129 break;
9130 case CMD_elseif:
9131 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009132 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009133 break;
9134 case CMD_else:
9135 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009136 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009137 break;
9138 case CMD_endif:
9139 line = compile_endif(p, &cctx);
9140 break;
9141
9142 case CMD_while:
9143 line = compile_while(p, &cctx);
9144 break;
9145 case CMD_endwhile:
9146 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009147 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009148 break;
9149
9150 case CMD_for:
9151 line = compile_for(p, &cctx);
9152 break;
9153 case CMD_endfor:
9154 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009155 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009156 break;
9157 case CMD_continue:
9158 line = compile_continue(p, &cctx);
9159 break;
9160 case CMD_break:
9161 line = compile_break(p, &cctx);
9162 break;
9163
9164 case CMD_try:
9165 line = compile_try(p, &cctx);
9166 break;
9167 case CMD_catch:
9168 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009169 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009170 break;
9171 case CMD_finally:
9172 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009173 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009174 break;
9175 case CMD_endtry:
9176 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009177 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009178 break;
9179 case CMD_throw:
9180 line = compile_throw(p, &cctx);
9181 break;
9182
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009183 case CMD_eval:
9184 if (compile_expr0(&p, &cctx) == FAIL)
9185 goto erret;
9186
Bram Moolenaar3988f642020-08-27 22:43:03 +02009187 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009188 generate_instr_drop(&cctx, ISN_DROP, 1);
9189
9190 line = skipwhite(p);
9191 break;
9192
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009193 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009194 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009195 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009196 case CMD_echomsg:
9197 case CMD_echoerr:
9198 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009199 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009200
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009201 case CMD_put:
9202 ea.cmd = cmd;
9203 line = compile_put(p, &ea, &cctx);
9204 break;
9205
Bram Moolenaar4c137212021-04-19 16:48:48 +02009206 case CMD_substitute:
9207 if (cctx.ctx_skip == SKIP_YES)
9208 line = (char_u *)"";
9209 else
9210 {
9211 ea.arg = p;
9212 line = compile_substitute(line, &ea, &cctx);
9213 }
9214 break;
9215
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009216 case CMD_redir:
9217 ea.arg = p;
9218 line = compile_redir(line, &ea, &cctx);
9219 break;
9220
Bram Moolenaar3988f642020-08-27 22:43:03 +02009221 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009222
Bram Moolenaarae616492020-07-28 20:07:27 +02009223 case CMD_append:
9224 case CMD_change:
9225 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009226 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009227 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009228 case CMD_xit:
9229 not_in_vim9(&ea);
9230 goto erret;
9231
Bram Moolenaar002262f2020-07-08 17:47:57 +02009232 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009233 if (cctx.ctx_skip != SKIP_YES)
9234 {
9235 semsg(_(e_invalid_command_str), ea.cmd);
9236 goto erret;
9237 }
9238 // We don't check for a next command here.
9239 line = (char_u *)"";
9240 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009241
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009242 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009243 if (cctx.ctx_skip == SKIP_YES)
9244 {
9245 // We don't check for a next command here.
9246 line = (char_u *)"";
9247 }
9248 else
9249 {
9250 // Not recognized, execute with do_cmdline_cmd().
9251 ea.arg = p;
9252 line = compile_exec(line, &ea, &cctx);
9253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009254 break;
9255 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009256nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009257 if (line == NULL)
9258 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009259 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009260
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009261 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009262 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009264 if (cctx.ctx_type_stack.ga_len < 0)
9265 {
9266 iemsg("Type stack underflow");
9267 goto erret;
9268 }
9269 }
9270
9271 if (cctx.ctx_scope != NULL)
9272 {
9273 if (cctx.ctx_scope->se_type == IF_SCOPE)
9274 emsg(_(e_endif));
9275 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9276 emsg(_(e_endwhile));
9277 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9278 emsg(_(e_endfor));
9279 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009280 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009281 goto erret;
9282 }
9283
Bram Moolenaarefd88552020-06-18 20:50:10 +02009284 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009285 {
9286 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
9287 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009288 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009289 goto erret;
9290 }
9291
9292 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01009293 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009294 }
9295
Bram Moolenaar599410c2021-04-10 14:03:43 +02009296 // When compiled with ":silent!" and there was an error don't consider the
9297 // function compiled.
9298 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009299 {
9300 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9301 + ufunc->uf_dfunc_idx;
9302 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009303 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009304#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009305 if (cctx.ctx_profiling)
9306 {
9307 dfunc->df_instr_prof = instr->ga_data;
9308 dfunc->df_instr_prof_count = instr->ga_len;
9309 }
9310 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009311#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009312 {
9313 dfunc->df_instr = instr->ga_data;
9314 dfunc->df_instr_count = instr->ga_len;
9315 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009316 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009317 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009318 if (cctx.ctx_outer_used)
9319 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009320 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009321 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009322
9323 ret = OK;
9324
9325erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009326 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009327 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009328 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9329 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009330
Bram Moolenaar8238f082021-04-20 21:10:48 +02009331 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009332 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009333
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009334 // If using the last entry in the table and it was added above, we
9335 // might as well remove it.
9336 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009337 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009338 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009339 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009340 ufunc->uf_dfunc_idx = 0;
9341 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009342 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009343
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009344 while (cctx.ctx_scope != NULL)
9345 drop_scope(&cctx);
9346
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009347 if (errormsg != NULL)
9348 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009349 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009350 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009351 }
9352
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009353 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9354 {
9355 if (ret == OK)
9356 {
9357 emsg(_(e_missing_redir_end));
9358 ret = FAIL;
9359 }
9360 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009361 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009362 }
9363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009364 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009365 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009366 if (do_estack_push)
9367 estack_pop();
9368
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009369 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009370 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009371 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009372 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009373 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009374}
9375
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009376 void
9377set_function_type(ufunc_T *ufunc)
9378{
9379 int varargs = ufunc->uf_va_name != NULL;
9380 int argcount = ufunc->uf_args.ga_len;
9381
9382 // Create a type for the function, with the return type and any
9383 // argument types.
9384 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9385 // The type is included in "tt_args".
9386 if (argcount > 0 || varargs)
9387 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009388 if (ufunc->uf_type_list.ga_itemsize == 0)
9389 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009390 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9391 argcount, &ufunc->uf_type_list);
9392 // Add argument types to the function type.
9393 if (func_type_add_arg_types(ufunc->uf_func_type,
9394 argcount + varargs,
9395 &ufunc->uf_type_list) == FAIL)
9396 return;
9397 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9398 ufunc->uf_func_type->tt_min_argcount =
9399 argcount - ufunc->uf_def_args.ga_len;
9400 if (ufunc->uf_arg_types == NULL)
9401 {
9402 int i;
9403
9404 // lambda does not have argument types.
9405 for (i = 0; i < argcount; ++i)
9406 ufunc->uf_func_type->tt_args[i] = &t_any;
9407 }
9408 else
9409 mch_memmove(ufunc->uf_func_type->tt_args,
9410 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9411 if (varargs)
9412 {
9413 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009414 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009415 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9416 }
9417 }
9418 else
9419 // No arguments, can use a predefined type.
9420 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9421 argcount, &ufunc->uf_type_list);
9422}
9423
9424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009425/*
9426 * Delete an instruction, free what it contains.
9427 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009428 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009429delete_instr(isn_T *isn)
9430{
9431 switch (isn->isn_type)
9432 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009433 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009434 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009435 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009436 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009437 case ISN_LOADENV:
9438 case ISN_LOADG:
9439 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009440 case ISN_LOADT:
9441 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009442 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009443 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009444 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009445 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009446 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009447 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009448 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009449 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009450 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009451 case ISN_STOREW:
9452 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009453 vim_free(isn->isn_arg.string);
9454 break;
9455
Bram Moolenaar4c137212021-04-19 16:48:48 +02009456 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009457 {
9458 int idx;
9459 isn_T *list = isn->isn_arg.subs.subs_instr;
9460
9461 vim_free(isn->isn_arg.subs.subs_cmd);
9462 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9463 delete_instr(list + idx);
9464 vim_free(list);
9465 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009466 break;
9467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009468 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009469 case ISN_STORES:
9470 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009471 break;
9472
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009473 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009474 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009475 vim_free(isn->isn_arg.unlet.ul_name);
9476 break;
9477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009478 case ISN_STOREOPT:
9479 vim_free(isn->isn_arg.storeopt.so_name);
9480 break;
9481
9482 case ISN_PUSHBLOB: // push blob isn_arg.blob
9483 blob_unref(isn->isn_arg.blob);
9484 break;
9485
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009486 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009487#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009488 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009489#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009490 break;
9491
9492 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009493#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009494 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009495#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009496 break;
9497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009498 case ISN_UCALL:
9499 vim_free(isn->isn_arg.ufunc.cuf_name);
9500 break;
9501
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009502 case ISN_FUNCREF:
9503 {
9504 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9505 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009506 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009507
Bram Moolenaar077a4232020-12-22 18:33:27 +01009508 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9509 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009510 }
9511 break;
9512
9513 case ISN_DCALL:
9514 {
9515 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9516 + isn->isn_arg.dfunc.cdf_idx;
9517
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009518 if (dfunc->df_ufunc != NULL
9519 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009520 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009521 }
9522 break;
9523
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009524 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009525 {
9526 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9527 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9528
9529 if (ufunc != NULL)
9530 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009531 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009532 func_ptr_unref(ufunc);
9533 }
9534
9535 vim_free(lambda);
9536 vim_free(isn->isn_arg.newfunc.nf_global);
9537 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009538 break;
9539
Bram Moolenaar5e654232020-09-16 15:22:00 +02009540 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009541 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009542 free_type(isn->isn_arg.type.ct_type);
9543 break;
9544
Bram Moolenaar02194d22020-10-24 23:08:38 +02009545 case ISN_CMDMOD:
9546 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9547 ->cmod_filter_regmatch.regprog);
9548 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9549 break;
9550
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009551 case ISN_LOADSCRIPT:
9552 case ISN_STORESCRIPT:
9553 vim_free(isn->isn_arg.script.scriptref);
9554 break;
9555
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009556 case ISN_TRY:
9557 vim_free(isn->isn_arg.try.try_ref);
9558 break;
9559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009560 case ISN_2BOOL:
9561 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009562 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009563 case ISN_ADDBLOB:
9564 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009565 case ISN_ANYINDEX:
9566 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009567 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009568 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009569 case ISN_BLOBINDEX:
9570 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009571 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009572 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009573 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009574 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009575 case ISN_COMPAREANY:
9576 case ISN_COMPAREBLOB:
9577 case ISN_COMPAREBOOL:
9578 case ISN_COMPAREDICT:
9579 case ISN_COMPAREFLOAT:
9580 case ISN_COMPAREFUNC:
9581 case ISN_COMPARELIST:
9582 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009583 case ISN_COMPARESPECIAL:
9584 case ISN_COMPARESTRING:
9585 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009586 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009587 case ISN_DROP:
9588 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009589 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009590 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009591 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009592 case ISN_EXECCONCAT:
9593 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009594 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009595 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009596 case ISN_GETITEM:
9597 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009598 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009599 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009600 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009601 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009602 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009603 case ISN_LOADBDICT:
9604 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009605 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009606 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009607 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009608 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009609 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009610 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009611 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009612 case ISN_NEGATENR:
9613 case ISN_NEWDICT:
9614 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009615 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009616 case ISN_OPFLOAT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009617 case ISN_FINISH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009618 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009619 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009620 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009621 case ISN_PROF_END:
9622 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009623 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009624 case ISN_PUSHF:
9625 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009626 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009627 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009628 case ISN_REDIREND:
9629 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009630 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009631 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009632 case ISN_SHUFFLE:
9633 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009634 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009635 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009636 case ISN_STORENR:
9637 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009638 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009639 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009640 case ISN_STOREV:
9641 case ISN_STRINDEX:
9642 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009643 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009644 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009645 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009646 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009647 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009648 // nothing allocated
9649 break;
9650 }
9651}
9652
9653/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009654 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009655 */
9656 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009657delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009658{
9659 int idx;
9660
9661 ga_clear(&dfunc->df_def_args_isn);
9662
9663 if (dfunc->df_instr != NULL)
9664 {
9665 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9666 delete_instr(dfunc->df_instr + idx);
9667 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009668 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009669 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009670#ifdef FEAT_PROFILE
9671 if (dfunc->df_instr_prof != NULL)
9672 {
9673 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9674 delete_instr(dfunc->df_instr_prof + idx);
9675 VIM_CLEAR(dfunc->df_instr_prof);
9676 dfunc->df_instr_prof = NULL;
9677 }
9678#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009679
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009680 if (mark_deleted)
9681 dfunc->df_deleted = TRUE;
9682 if (dfunc->df_ufunc != NULL)
9683 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009684}
9685
9686/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009687 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009688 * function, unless another user function still uses it.
9689 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009690 */
9691 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009692unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009693{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009694 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009695 {
9696 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9697 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009698
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009699 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009700 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009701 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009702 ufunc->uf_dfunc_idx = 0;
9703 if (dfunc->df_ufunc == ufunc)
9704 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009705 }
9706}
9707
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009708/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009709 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009710 */
9711 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009712link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009713{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009714 if (ufunc->uf_dfunc_idx > 0)
9715 {
9716 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9717 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009718
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009719 ++dfunc->df_refcount;
9720 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009721}
9722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009723#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009724/*
9725 * Free all functions defined with ":def".
9726 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009727 void
9728free_def_functions(void)
9729{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009730 int idx;
9731
9732 for (idx = 0; idx < def_functions.ga_len; ++idx)
9733 {
9734 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9735
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009736 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009737 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009738 }
9739
9740 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009741}
9742#endif
9743
9744
9745#endif // FEAT_EVAL