blob: c74bb6e5cd41f74ef6575de6ebdb652a5e4633fa [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 Moolenaara369c3d2021-04-21 16:00:10 +02006253 * Produce code for loading "lhs" and also take care of an index.
6254 * Return OK/FAIL.
6255 */
6256 static int
6257compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6258{
6259 compile_load_lhs(lhs, var_start, NULL, cctx);
6260
6261 if (lhs->lhs_has_index)
6262 {
6263 int range = FALSE;
6264
6265 // Get member from list or dict. First compile the
6266 // index value.
6267 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6268 return FAIL;
6269 if (range)
6270 {
6271 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6272 var_start);
6273 return FAIL;
6274 }
6275
6276 // Get the member.
6277 if (compile_member(FALSE, cctx) == FAIL)
6278 return FAIL;
6279 }
6280 return OK;
6281}
6282
6283/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006284 * Assignment to a list or dict member, or ":unlet" for the item, using the
6285 * information in "lhs".
6286 * Returns OK or FAIL.
6287 */
6288 static int
6289compile_assign_unlet(
6290 char_u *var_start,
6291 lhs_T *lhs,
6292 int is_assign,
6293 type_T *rhs_type,
6294 cctx_T *cctx)
6295{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006296 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006297 garray_T *stack = &cctx->ctx_type_stack;
6298 int range = FALSE;
6299
Bram Moolenaar68452172021-04-12 21:21:02 +02006300 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006301 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006302 if (is_assign && range && lhs->lhs_type != &t_blob
6303 && lhs->lhs_type != &t_any)
6304 {
6305 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6306 return FAIL;
6307 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006308
6309 if (lhs->lhs_type == &t_any)
6310 {
6311 // Index on variable of unknown type: check at runtime.
6312 dest_type = VAR_ANY;
6313 }
6314 else
6315 {
6316 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006317 if (dest_type == VAR_DICT && range)
6318 {
6319 emsg(e_cannot_use_range_with_dictionary);
6320 return FAIL;
6321 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006322 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6323 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006324 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006325 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006326 type_T *type;
6327
6328 if (range)
6329 {
6330 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6331 if (need_type(type, &t_number,
6332 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006333 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006334 }
6335 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6336 if ((dest_type != VAR_BLOB || type != &t_special)
6337 && need_type(type, &t_number,
6338 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006339 return FAIL;
6340 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006341 }
6342
6343 // Load the dict or list. On the stack we then have:
6344 // - value (for assignment, not for :unlet)
6345 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006346 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006347 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006348 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6349 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006350
Bram Moolenaar68452172021-04-12 21:21:02 +02006351 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6352 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006353 {
6354 if (is_assign)
6355 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006356 if (range)
6357 {
6358 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6359 return FAIL;
6360 }
6361 else
6362 {
6363 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006364
Bram Moolenaar68452172021-04-12 21:21:02 +02006365 if (isn == NULL)
6366 return FAIL;
6367 isn->isn_arg.vartype = dest_type;
6368 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006369 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006370 else if (range)
6371 {
6372 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6373 return FAIL;
6374 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006375 else
6376 {
6377 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6378 return FAIL;
6379 }
6380 }
6381 else
6382 {
6383 emsg(_(e_indexable_type_required));
6384 return FAIL;
6385 }
6386
6387 return OK;
6388}
6389
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006390/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006391 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006392 * "let name"
6393 * "var name = expr"
6394 * "final name = expr"
6395 * "const name = expr"
6396 * "name = expr"
6397 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006398 * Return NULL for an error.
6399 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 */
6401 static char_u *
6402compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6403{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006404 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006405 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006406 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006407 char_u *ret = NULL;
6408 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006409 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006410 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006411 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006412 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006413 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006414 int oplen = 0;
6415 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006416 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006417 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006418 int is_decl = is_decl_command(cmdidx);
6419 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006420 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006421
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006422 // Skip over the "var" or "[var, var]" to get to any "=".
6423 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6424 if (p == NULL)
6425 return *arg == '[' ? arg : NULL;
6426
6427 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006428 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006429 // TODO: should we allow this, and figure out type inference from list
6430 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006431 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006432 return NULL;
6433 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006434 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436 sp = p;
6437 p = skipwhite(p);
6438 op = p;
6439 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006440
6441 if (var_count > 0 && oplen == 0)
6442 // can be something like "[1, 2]->func()"
6443 return arg;
6444
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006445 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006447 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006448 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006449 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006451 if (heredoc)
6452 {
6453 list_T *l;
6454 listitem_T *li;
6455
6456 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006457 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006458 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006459 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006460 if (l == NULL)
6461 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006462
Bram Moolenaar078269b2020-09-21 20:35:55 +02006463 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006465 // Push each line and the create the list.
6466 FOR_ALL_LIST_ITEMS(l, li)
6467 {
6468 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6469 li->li_tv.vval.v_string = NULL;
6470 }
6471 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006472 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006473 list_free(l);
6474 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006475 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006476 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006477 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006478 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006479 char_u *wp;
6480
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006481 // for "[var, var] = expr" evaluate the expression here, loop over the
6482 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006483 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006484
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006485 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006486 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006487 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006488 if (compile_expr0(&p, cctx) == FAIL)
6489 return NULL;
6490 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006491
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006492 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006493 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006494 type_T *stacktype;
6495
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006496 stacktype = stack->ga_len == 0 ? &t_void
6497 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006498 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006499 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006500 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006501 goto theend;
6502 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006503 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006504 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006505 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006506 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006507 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6508 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006509 if (stacktype->tt_member != NULL)
6510 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006511 }
6512 }
6513
6514 /*
6515 * Loop over variables in "[var, var] = expr".
6516 * For "var = expr" and "let var: type" this is done only once.
6517 */
6518 if (var_count > 0)
6519 var_start = skipwhite(arg + 1); // skip over the "["
6520 else
6521 var_start = arg;
6522 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6523 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006524 int instr_count = -1;
6525
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006526 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6527 {
6528 // Ignore underscore in "[a, _, b] = list".
6529 if (var_count > 0)
6530 {
6531 var_start = skipwhite(var_start + 2);
6532 continue;
6533 }
6534 emsg(_(e_cannot_use_underscore_here));
6535 goto theend;
6536 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006537 vim_free(lhs.lhs_name);
6538
6539 /*
6540 * Figure out the LHS type and other properties.
6541 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006542 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6543 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006544 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006545 if (!heredoc)
6546 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006547 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006548 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006549 if (oplen > 0 && var_count == 0)
6550 {
6551 // skip over the "=" and the expression
6552 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006553 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006554 }
6555 }
6556 else if (oplen > 0)
6557 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006558 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006559 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006560
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006561 // For "var = expr" evaluate the expression.
6562 if (var_count == 0)
6563 {
6564 int r;
6565
6566 // for "+=", "*=", "..=" etc. first load the current value
6567 if (*op != '=')
6568 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006569 if (compile_load_lhs_with_index(&lhs, var_start,
6570 cctx) == FAIL)
6571 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006572 }
6573
6574 // Compile the expression. Temporarily hide the new local
6575 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006576 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006577 --cctx->ctx_locals.ga_len;
6578 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006579 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006580 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006581 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006582 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006583 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006584 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006585 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006586 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006587 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006588 ++cctx->ctx_locals.ga_len;
6589 if (r == FAIL)
6590 goto theend;
6591 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006592 else if (semicolon && var_idx == var_count - 1)
6593 {
6594 // For "[var; var] = expr" get the rest of the list
6595 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6596 goto theend;
6597 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006598 else
6599 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006600 // For "[var, var] = expr" get the "var_idx" item from the
6601 // list.
6602 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006603 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006604 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006605
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006606 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006607 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006608 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006609 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006610 if ((rhs_type->tt_type == VAR_FUNC
6611 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006612 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006613 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006614 goto theend;
6615
Bram Moolenaar752fc692021-01-04 21:57:11 +01006616 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006617 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006618 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006619 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006620 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006621 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006622 }
6623 else
6624 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006625 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006626 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006627 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006628 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006629 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006630 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006631 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006632 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006633 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006634 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006635 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006636 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006637 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006638 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006639 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006640
Bram Moolenaar77709b12021-04-03 21:01:01 +02006641 // Without operator check type here, otherwise below.
6642 // Use the line number of the assignment.
6643 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006644 if (lhs.lhs_has_index)
6645 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006646 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006647 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006648 goto theend;
6649 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006650 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006651 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006652 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006653 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006655 else if (cmdidx == CMD_final)
6656 {
6657 emsg(_(e_final_requires_a_value));
6658 goto theend;
6659 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006660 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006661 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006662 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006663 goto theend;
6664 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006665 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006666 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006667 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006668 goto theend;
6669 }
6670 else
6671 {
6672 // variables are always initialized
6673 if (ga_grow(instr, 1) == FAIL)
6674 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006675 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006676 {
6677 case VAR_BOOL:
6678 generate_PUSHBOOL(cctx, VVAL_FALSE);
6679 break;
6680 case VAR_FLOAT:
6681#ifdef FEAT_FLOAT
6682 generate_PUSHF(cctx, 0.0);
6683#endif
6684 break;
6685 case VAR_STRING:
6686 generate_PUSHS(cctx, NULL);
6687 break;
6688 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006689 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006690 break;
6691 case VAR_FUNC:
6692 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6693 break;
6694 case VAR_LIST:
6695 generate_NEWLIST(cctx, 0);
6696 break;
6697 case VAR_DICT:
6698 generate_NEWDICT(cctx, 0);
6699 break;
6700 case VAR_JOB:
6701 generate_PUSHJOB(cctx, NULL);
6702 break;
6703 case VAR_CHANNEL:
6704 generate_PUSHCHANNEL(cctx, NULL);
6705 break;
6706 case VAR_NUMBER:
6707 case VAR_UNKNOWN:
6708 case VAR_ANY:
6709 case VAR_PARTIAL:
6710 case VAR_VOID:
6711 case VAR_SPECIAL: // cannot happen
6712 generate_PUSHNR(cctx, 0);
6713 break;
6714 }
6715 }
6716 if (var_count == 0)
6717 end = p;
6718 }
6719
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006720 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006721 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006722 break;
6723
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006724 if (oplen > 0 && *op != '=')
6725 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006726 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006727 type_T *stacktype;
6728
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006729 if (*op == '.')
6730 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006731 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006732 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006733 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006734 if (
6735#ifdef FEAT_FLOAT
6736 // If variable is float operation with number is OK.
6737 !(expected == &t_float && stacktype == &t_number) &&
6738#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006739 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006740 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006741 goto theend;
6742
6743 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006744 {
6745 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6746 goto theend;
6747 }
6748 else if (*op == '+')
6749 {
6750 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006751 operator_type(lhs.lhs_member_type, stacktype),
6752 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006753 goto theend;
6754 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006755 else if (generate_two_op(cctx, op) == FAIL)
6756 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006758
Bram Moolenaar752fc692021-01-04 21:57:11 +01006759 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006760 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006761 // Use the info in "lhs" to store the value at the index in the
6762 // list or dict.
6763 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6764 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006765 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006766 }
6767 else
6768 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006769 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006770 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006771 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006772 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006773 generate_LOCKCONST(cctx);
6774
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006775 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006776 && (lhs.lhs_type->tt_type == VAR_DICT
6777 || lhs.lhs_type->tt_type == VAR_LIST)
6778 && lhs.lhs_type->tt_member != NULL
6779 && lhs.lhs_type->tt_member != &t_any
6780 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006781 // Set the type in the list or dict, so that it can be checked,
6782 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006783 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006784
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006785 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6786 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006787 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006788
6789 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006790 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006792
6793 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006794 if (var_count > 0 && !semicolon)
6795 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006796 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006797 goto theend;
6798 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006799
Bram Moolenaarb2097502020-07-19 17:17:02 +02006800 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801
6802theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006803 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 return ret;
6805}
6806
6807/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006808 * Check for an assignment at "eap->cmd", compile it if found.
6809 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6810 */
6811 static int
6812may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6813{
6814 char_u *pskip;
6815 char_u *p;
6816
6817 // Assuming the command starts with a variable or function name,
6818 // find what follows.
6819 // Skip over "var.member", "var[idx]" and the like.
6820 // Also "&opt = val", "$ENV = val" and "@r = val".
6821 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6822 ? eap->cmd + 1 : eap->cmd;
6823 p = to_name_end(pskip, TRUE);
6824 if (p > eap->cmd && *p != NUL)
6825 {
6826 char_u *var_end;
6827 int oplen;
6828 int heredoc;
6829
6830 if (eap->cmd[0] == '@')
6831 var_end = eap->cmd + 2;
6832 else
6833 var_end = find_name_end(pskip, NULL, NULL,
6834 FNE_CHECK_START | FNE_INCL_BR);
6835 oplen = assignment_len(skipwhite(var_end), &heredoc);
6836 if (oplen > 0)
6837 {
6838 size_t len = p - eap->cmd;
6839
6840 // Recognize an assignment if we recognize the variable
6841 // name:
6842 // "g:var = expr"
6843 // "local = expr" where "local" is a local var.
6844 // "script = expr" where "script" is a script-local var.
6845 // "import = expr" where "import" is an imported var
6846 // "&opt = expr"
6847 // "$ENV = expr"
6848 // "@r = expr"
6849 if (*eap->cmd == '&'
6850 || *eap->cmd == '$'
6851 || *eap->cmd == '@'
6852 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006853 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006854 {
6855 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6856 if (*line == NULL || *line == eap->cmd)
6857 return FAIL;
6858 return OK;
6859 }
6860 }
6861 }
6862
6863 if (*eap->cmd == '[')
6864 {
6865 // [var, var] = expr
6866 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6867 if (*line == NULL)
6868 return FAIL;
6869 if (*line != eap->cmd)
6870 return OK;
6871 }
6872 return NOTDONE;
6873}
6874
6875/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006876 * Check if "name" can be "unlet".
6877 */
6878 int
6879check_vim9_unlet(char_u *name)
6880{
6881 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6882 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006883 // "unlet s:var" is allowed in legacy script.
6884 if (*name == 's' && !script_is_vim9())
6885 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006886 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006887 return FAIL;
6888 }
6889 return OK;
6890}
6891
6892/*
6893 * Callback passed to ex_unletlock().
6894 */
6895 static int
6896compile_unlet(
6897 lval_T *lvp,
6898 char_u *name_end,
6899 exarg_T *eap,
6900 int deep UNUSED,
6901 void *coookie)
6902{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006903 cctx_T *cctx = coookie;
6904 char_u *p = lvp->ll_name;
6905 int cc = *name_end;
6906 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006907
Bram Moolenaar752fc692021-01-04 21:57:11 +01006908 if (cctx->ctx_skip == SKIP_YES)
6909 return OK;
6910
6911 *name_end = NUL;
6912 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006913 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006914 // :unlet $ENV_VAR
6915 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6916 }
6917 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6918 {
6919 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006920
Bram Moolenaar752fc692021-01-04 21:57:11 +01006921 // This is similar to assigning: lookup the list/dict, compile the
6922 // idx/key. Then instead of storing the value unlet the item.
6923 // unlet {list}[idx]
6924 // unlet {dict}[key] dict.key
6925 //
6926 // Figure out the LHS type and other properties.
6927 //
6928 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6929
6930 // : unlet an indexed item
6931 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006932 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006933 iemsg("called compile_lhs() without an index");
6934 ret = FAIL;
6935 }
6936 else
6937 {
6938 // Use the info in "lhs" to unlet the item at the index in the
6939 // list or dict.
6940 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006941 }
6942
Bram Moolenaar752fc692021-01-04 21:57:11 +01006943 vim_free(lhs.lhs_name);
6944 }
6945 else if (check_vim9_unlet(p) == FAIL)
6946 {
6947 ret = FAIL;
6948 }
6949 else
6950 {
6951 // Normal name. Only supports g:, w:, t: and b: namespaces.
6952 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006953 }
6954
Bram Moolenaar752fc692021-01-04 21:57:11 +01006955 *name_end = cc;
6956 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006957}
6958
6959/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006960 * Callback passed to ex_unletlock().
6961 */
6962 static int
6963compile_lock_unlock(
6964 lval_T *lvp,
6965 char_u *name_end,
6966 exarg_T *eap,
6967 int deep UNUSED,
6968 void *coookie)
6969{
6970 cctx_T *cctx = coookie;
6971 int cc = *name_end;
6972 char_u *p = lvp->ll_name;
6973 int ret = OK;
6974 size_t len;
6975 char_u *buf;
6976
6977 if (cctx->ctx_skip == SKIP_YES)
6978 return OK;
6979
6980 // Cannot use :lockvar and :unlockvar on local variables.
6981 if (p[1] != ':')
6982 {
6983 char_u *end = skip_var_one(p, FALSE);
6984
6985 if (lookup_local(p, end - p, NULL, cctx) == OK)
6986 {
6987 emsg(_(e_cannot_lock_unlock_local_variable));
6988 return FAIL;
6989 }
6990 }
6991
6992 // Checking is done at runtime.
6993 *name_end = NUL;
6994 len = name_end - p + 20;
6995 buf = alloc(len);
6996 if (buf == NULL)
6997 ret = FAIL;
6998 else
6999 {
7000 vim_snprintf((char *)buf, len, "%s %s",
7001 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7002 p);
7003 ret = generate_EXEC(cctx, buf);
7004
7005 vim_free(buf);
7006 *name_end = cc;
7007 }
7008 return ret;
7009}
7010
7011/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007012 * compile "unlet var", "lock var" and "unlock var"
7013 * "arg" points to "var".
7014 */
7015 static char_u *
7016compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7017{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007018 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7019 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7020 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007021 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7022}
7023
7024/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 * Compile an :import command.
7026 */
7027 static char_u *
7028compile_import(char_u *arg, cctx_T *cctx)
7029{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007030 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031}
7032
7033/*
7034 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7035 */
7036 static int
7037compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7038{
7039 garray_T *instr = &cctx->ctx_instr;
7040 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7041
7042 if (endlabel == NULL)
7043 return FAIL;
7044 endlabel->el_next = *el;
7045 *el = endlabel;
7046 endlabel->el_end_label = instr->ga_len;
7047
7048 generate_JUMP(cctx, when, 0);
7049 return OK;
7050}
7051
7052 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007053compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054{
7055 garray_T *instr = &cctx->ctx_instr;
7056
7057 while (*el != NULL)
7058 {
7059 endlabel_T *cur = (*el);
7060 isn_T *isn;
7061
7062 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007063 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 *el = cur->el_next;
7065 vim_free(cur);
7066 }
7067}
7068
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007069 static void
7070compile_free_jump_to_end(endlabel_T **el)
7071{
7072 while (*el != NULL)
7073 {
7074 endlabel_T *cur = (*el);
7075
7076 *el = cur->el_next;
7077 vim_free(cur);
7078 }
7079}
7080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081/*
7082 * Create a new scope and set up the generic items.
7083 */
7084 static scope_T *
7085new_scope(cctx_T *cctx, scopetype_T type)
7086{
7087 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7088
7089 if (scope == NULL)
7090 return NULL;
7091 scope->se_outer = cctx->ctx_scope;
7092 cctx->ctx_scope = scope;
7093 scope->se_type = type;
7094 scope->se_local_count = cctx->ctx_locals.ga_len;
7095 return scope;
7096}
7097
7098/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007099 * Free the current scope and go back to the outer scope.
7100 */
7101 static void
7102drop_scope(cctx_T *cctx)
7103{
7104 scope_T *scope = cctx->ctx_scope;
7105
7106 if (scope == NULL)
7107 {
7108 iemsg("calling drop_scope() without a scope");
7109 return;
7110 }
7111 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007112 switch (scope->se_type)
7113 {
7114 case IF_SCOPE:
7115 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7116 case FOR_SCOPE:
7117 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7118 case WHILE_SCOPE:
7119 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7120 case TRY_SCOPE:
7121 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7122 case NO_SCOPE:
7123 case BLOCK_SCOPE:
7124 break;
7125 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007126 vim_free(scope);
7127}
7128
7129/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130 * compile "if expr"
7131 *
7132 * "if expr" Produces instructions:
7133 * EVAL expr Push result of "expr"
7134 * JUMP_IF_FALSE end
7135 * ... body ...
7136 * end:
7137 *
7138 * "if expr | else" Produces instructions:
7139 * EVAL expr Push result of "expr"
7140 * JUMP_IF_FALSE else
7141 * ... body ...
7142 * JUMP_ALWAYS end
7143 * else:
7144 * ... body ...
7145 * end:
7146 *
7147 * "if expr1 | elseif expr2 | else" Produces instructions:
7148 * EVAL expr Push result of "expr"
7149 * JUMP_IF_FALSE elseif
7150 * ... body ...
7151 * JUMP_ALWAYS end
7152 * elseif:
7153 * EVAL expr Push result of "expr"
7154 * JUMP_IF_FALSE else
7155 * ... body ...
7156 * JUMP_ALWAYS end
7157 * else:
7158 * ... body ...
7159 * end:
7160 */
7161 static char_u *
7162compile_if(char_u *arg, cctx_T *cctx)
7163{
7164 char_u *p = arg;
7165 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007166 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007167 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007168 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007169 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007170
Bram Moolenaara5565e42020-05-09 15:44:01 +02007171 CLEAR_FIELD(ppconst);
7172 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007173 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007174 clear_ppconst(&ppconst);
7175 return NULL;
7176 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007177 if (!ends_excmd2(arg, skipwhite(p)))
7178 {
7179 semsg(_(e_trailing_arg), p);
7180 return NULL;
7181 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007182 if (cctx->ctx_skip == SKIP_YES)
7183 clear_ppconst(&ppconst);
7184 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007185 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007186 int error = FALSE;
7187 int v;
7188
Bram Moolenaara5565e42020-05-09 15:44:01 +02007189 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007190 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007191 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007192 if (error)
7193 return NULL;
7194 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007195 }
7196 else
7197 {
7198 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007199 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007200 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007201 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007202 if (bool_on_stack(cctx) == FAIL)
7203 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007205
Bram Moolenaara91a7132021-03-25 21:12:15 +01007206 // CMDMOD_REV must come before the jump
7207 generate_undo_cmdmods(cctx);
7208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007209 scope = new_scope(cctx, IF_SCOPE);
7210 if (scope == NULL)
7211 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007212 scope->se_skip_save = skip_save;
7213 // "is_had_return" will be reset if any block does not end in :return
7214 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007216 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007217 {
7218 // "where" is set when ":elseif", "else" or ":endif" is found
7219 scope->se_u.se_if.is_if_label = instr->ga_len;
7220 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7221 }
7222 else
7223 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007224
Bram Moolenaarced68a02021-01-24 17:53:47 +01007225#ifdef FEAT_PROFILE
7226 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7227 && skip_save != SKIP_YES)
7228 {
7229 // generated a profile start, need to generate a profile end, since it
7230 // won't be done after returning
7231 cctx->ctx_skip = SKIP_NOT;
7232 generate_instr(cctx, ISN_PROF_END);
7233 cctx->ctx_skip = SKIP_YES;
7234 }
7235#endif
7236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007237 return p;
7238}
7239
7240 static char_u *
7241compile_elseif(char_u *arg, cctx_T *cctx)
7242{
7243 char_u *p = arg;
7244 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007245 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007246 isn_T *isn;
7247 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007248 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007249 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007250
7251 if (scope == NULL || scope->se_type != IF_SCOPE)
7252 {
7253 emsg(_(e_elseif_without_if));
7254 return NULL;
7255 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007256 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007257 if (!cctx->ctx_had_return)
7258 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007259
Bram Moolenaarced68a02021-01-24 17:53:47 +01007260 if (cctx->ctx_skip == SKIP_NOT)
7261 {
7262 // previous block was executed, this one and following will not
7263 cctx->ctx_skip = SKIP_YES;
7264 scope->se_u.se_if.is_seen_skip_not = TRUE;
7265 }
7266 if (scope->se_u.se_if.is_seen_skip_not)
7267 {
7268 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007269 // Do not count the "elseif" for profiling and cmdmod
7270 instr->ga_len = current_instr_idx(cctx);
7271
Bram Moolenaarced68a02021-01-24 17:53:47 +01007272 skip_expr_cctx(&p, cctx);
7273 return p;
7274 }
7275
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007276 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007277 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007278 int moved_cmdmod = FALSE;
7279
7280 // Move any CMDMOD instruction to after the jump
7281 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7282 {
7283 if (ga_grow(instr, 1) == FAIL)
7284 return NULL;
7285 ((isn_T *)instr->ga_data)[instr->ga_len] =
7286 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7287 --instr->ga_len;
7288 moved_cmdmod = TRUE;
7289 }
7290
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007291 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007292 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007293 return NULL;
7294 // previous "if" or "elseif" jumps here
7295 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7296 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007297 if (moved_cmdmod)
7298 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007300
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007301 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007302 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007303 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007304 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007305 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007306#ifdef FEAT_PROFILE
7307 if (cctx->ctx_profiling)
7308 {
7309 // the previous block was skipped, need to profile this line
7310 generate_instr(cctx, ISN_PROF_START);
7311 instr_count = instr->ga_len;
7312 }
7313#endif
7314 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007315 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007316 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007317 clear_ppconst(&ppconst);
7318 return NULL;
7319 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007320 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007321 if (!ends_excmd2(arg, skipwhite(p)))
7322 {
7323 semsg(_(e_trailing_arg), p);
7324 return NULL;
7325 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007326 if (scope->se_skip_save == SKIP_YES)
7327 clear_ppconst(&ppconst);
7328 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007329 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007330 int error = FALSE;
7331 int v;
7332
Bram Moolenaar7f141552020-05-09 17:35:53 +02007333 // The expression results in a constant.
7334 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007335 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7336 if (error)
7337 return NULL;
7338 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007339 clear_ppconst(&ppconst);
7340 scope->se_u.se_if.is_if_label = -1;
7341 }
7342 else
7343 {
7344 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007345 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007346 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007347 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007348 if (bool_on_stack(cctx) == FAIL)
7349 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350
Bram Moolenaara91a7132021-03-25 21:12:15 +01007351 // CMDMOD_REV must come before the jump
7352 generate_undo_cmdmods(cctx);
7353
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007354 // "where" is set when ":elseif", "else" or ":endif" is found
7355 scope->se_u.se_if.is_if_label = instr->ga_len;
7356 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007358
7359 return p;
7360}
7361
7362 static char_u *
7363compile_else(char_u *arg, cctx_T *cctx)
7364{
7365 char_u *p = arg;
7366 garray_T *instr = &cctx->ctx_instr;
7367 isn_T *isn;
7368 scope_T *scope = cctx->ctx_scope;
7369
7370 if (scope == NULL || scope->se_type != IF_SCOPE)
7371 {
7372 emsg(_(e_else_without_if));
7373 return NULL;
7374 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007375 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007376 if (!cctx->ctx_had_return)
7377 scope->se_u.se_if.is_had_return = FALSE;
7378 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379
Bram Moolenaarced68a02021-01-24 17:53:47 +01007380#ifdef FEAT_PROFILE
7381 if (cctx->ctx_profiling)
7382 {
7383 if (cctx->ctx_skip == SKIP_NOT
7384 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7385 .isn_type == ISN_PROF_START)
7386 // the previous block was executed, do not count "else" for profiling
7387 --instr->ga_len;
7388 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7389 {
7390 // the previous block was not executed, this one will, do count the
7391 // "else" for profiling
7392 cctx->ctx_skip = SKIP_NOT;
7393 generate_instr(cctx, ISN_PROF_END);
7394 generate_instr(cctx, ISN_PROF_START);
7395 cctx->ctx_skip = SKIP_YES;
7396 }
7397 }
7398#endif
7399
7400 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007401 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007402 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007403 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007404 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007405 if (!cctx->ctx_had_return
7406 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7407 JUMP_ALWAYS, cctx) == FAIL)
7408 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007409 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007410
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007411 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007412 {
7413 if (scope->se_u.se_if.is_if_label >= 0)
7414 {
7415 // previous "if" or "elseif" jumps here
7416 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7417 isn->isn_arg.jump.jump_where = instr->ga_len;
7418 scope->se_u.se_if.is_if_label = -1;
7419 }
7420 }
7421
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007422 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007423 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7424 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425
7426 return p;
7427}
7428
7429 static char_u *
7430compile_endif(char_u *arg, cctx_T *cctx)
7431{
7432 scope_T *scope = cctx->ctx_scope;
7433 ifscope_T *ifscope;
7434 garray_T *instr = &cctx->ctx_instr;
7435 isn_T *isn;
7436
Bram Moolenaarfa984412021-03-25 22:15:28 +01007437 if (misplaced_cmdmod(cctx))
7438 return NULL;
7439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007440 if (scope == NULL || scope->se_type != IF_SCOPE)
7441 {
7442 emsg(_(e_endif_without_if));
7443 return NULL;
7444 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007445 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007446 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007447 if (!cctx->ctx_had_return)
7448 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007450 if (scope->se_u.se_if.is_if_label >= 0)
7451 {
7452 // previous "if" or "elseif" jumps here
7453 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7454 isn->isn_arg.jump.jump_where = instr->ga_len;
7455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007457 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007458
7459#ifdef FEAT_PROFILE
7460 // even when skipping we count the endif as executed, unless the block it's
7461 // in is skipped
7462 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7463 && scope->se_skip_save != SKIP_YES)
7464 {
7465 cctx->ctx_skip = SKIP_NOT;
7466 generate_instr(cctx, ISN_PROF_START);
7467 }
7468#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007469 cctx->ctx_skip = scope->se_skip_save;
7470
7471 // If all the blocks end in :return and there is an :else then the
7472 // had_return flag is set.
7473 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007475 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476 return arg;
7477}
7478
7479/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007480 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481 *
7482 * Produces instructions:
7483 * PUSHNR -1
7484 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007485 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7487 * - if beyond end, jump to "end"
7488 * - otherwise get item from list and push it
7489 * STORE var Store item in "var"
7490 * ... body ...
7491 * JUMP top Jump back to repeat
7492 * end: DROP Drop the result of "expr"
7493 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007494 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7495 * UNPACK 2 Split item in 2
7496 * STORE var1 Store item in "var1"
7497 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007498 */
7499 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007500compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007501{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007502 char_u *arg;
7503 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007504 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007505 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007506 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007507 int var_count = 0;
7508 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510 garray_T *stack = &cctx->ctx_type_stack;
7511 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007512 lvar_T *loop_lvar; // loop iteration variable
7513 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007514 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007515 type_T *item_type = &t_any;
7516 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517
Bram Moolenaar792f7862020-11-23 08:31:18 +01007518 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007519 if (p == NULL)
7520 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007521 if (var_count == 0)
7522 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007523
7524 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007525 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007526 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7527 return NULL;
7528 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529 {
7530 emsg(_(e_missing_in));
7531 return NULL;
7532 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007533 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007534 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7535 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537 scope = new_scope(cctx, FOR_SCOPE);
7538 if (scope == NULL)
7539 return NULL;
7540
Bram Moolenaar792f7862020-11-23 08:31:18 +01007541 // Reserve a variable to store the loop iteration counter and initialize it
7542 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007543 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7544 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007545 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007546 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007547 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007549 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007550 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551
7552 // compile "expr", it remains on the stack until "endfor"
7553 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007554 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007555 {
7556 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007557 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007558 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007559 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007560
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007561 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007562 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007564 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007565 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007566 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007567 semsg(_(e_for_loop_on_str_not_supported),
7568 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007569 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007570 return NULL;
7571 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007572
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007573 if (vartype->tt_type == VAR_STRING)
7574 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007575 else if (vartype->tt_type == VAR_BLOB)
7576 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007577 else if (vartype->tt_type == VAR_LIST
7578 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007579 {
7580 if (var_count == 1)
7581 item_type = vartype->tt_member;
7582 else if (vartype->tt_member->tt_type == VAR_LIST
7583 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007584 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007585 item_type = vartype->tt_member->tt_member;
7586 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007587
Bram Moolenaara91a7132021-03-25 21:12:15 +01007588 // CMDMOD_REV must come before the FOR instruction
7589 generate_undo_cmdmods(cctx);
7590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007591 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007592 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007593 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594
Bram Moolenaar792f7862020-11-23 08:31:18 +01007595 arg = arg_start;
7596 if (var_count > 1)
7597 {
7598 generate_UNPACK(cctx, var_count, semicolon);
7599 arg = skipwhite(arg + 1); // skip white after '['
7600
7601 // the list item is replaced by a number of items
7602 if (ga_grow(stack, var_count - 1) == FAIL)
7603 {
7604 drop_scope(cctx);
7605 return NULL;
7606 }
7607 --stack->ga_len;
7608 for (idx = 0; idx < var_count; ++idx)
7609 {
7610 ((type_T **)stack->ga_data)[stack->ga_len] =
7611 (semicolon && idx == 0) ? vartype : item_type;
7612 ++stack->ga_len;
7613 }
7614 }
7615
7616 for (idx = 0; idx < var_count; ++idx)
7617 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007618 assign_dest_T dest = dest_local;
7619 int opt_flags = 0;
7620 int vimvaridx = -1;
7621 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007622 type_T *lhs_type = &t_any;
7623 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007624
7625 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007626 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007627 name = vim_strnsave(arg, varlen);
7628 if (name == NULL)
7629 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007630 if (*p == ':')
7631 {
7632 p = skipwhite(p + 1);
7633 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7634 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007635
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007636 // TODO: script var not supported?
7637 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7638 &vimvaridx, &type, cctx) == FAIL)
7639 goto failed;
7640 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007641 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007642 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7643 0, 0, type, name) == FAIL)
7644 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007645 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007646 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007647 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007648 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007649 {
7650 semsg(_(e_variable_already_declared), arg);
7651 goto failed;
7652 }
7653
Bram Moolenaarea870692020-12-02 14:24:30 +01007654 if (STRNCMP(name, "s:", 2) == 0)
7655 {
7656 semsg(_(e_cannot_declare_script_variable_in_function), name);
7657 goto failed;
7658 }
7659
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007660 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007661 where.wt_index = var_count > 1 ? idx + 1 : 0;
7662 where.wt_variable = TRUE;
7663 if (lhs_type == &t_any)
7664 lhs_type = item_type;
7665 else if (item_type != &t_unknown
7666 && !(var_count > 1 && item_type == &t_any)
7667 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7668 goto failed;
7669 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007670 if (var_lvar == NULL)
7671 // out of memory or used as an argument
7672 goto failed;
7673
7674 if (semicolon && idx == var_count - 1)
7675 var_lvar->lv_type = vartype;
7676 else
7677 var_lvar->lv_type = item_type;
7678 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7679 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007680
7681 if (*p == ',' || *p == ';')
7682 ++p;
7683 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007684 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007685 }
7686
7687 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007688
7689failed:
7690 vim_free(name);
7691 drop_scope(cctx);
7692 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007693}
7694
7695/*
7696 * compile "endfor"
7697 */
7698 static char_u *
7699compile_endfor(char_u *arg, cctx_T *cctx)
7700{
7701 garray_T *instr = &cctx->ctx_instr;
7702 scope_T *scope = cctx->ctx_scope;
7703 forscope_T *forscope;
7704 isn_T *isn;
7705
Bram Moolenaarfa984412021-03-25 22:15:28 +01007706 if (misplaced_cmdmod(cctx))
7707 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007709 if (scope == NULL || scope->se_type != FOR_SCOPE)
7710 {
7711 emsg(_(e_for));
7712 return NULL;
7713 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007714 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007716 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717
7718 // At end of ":for" scope jump back to the FOR instruction.
7719 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7720
7721 // Fill in the "end" label in the FOR statement so it can jump here
7722 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7723 isn->isn_arg.forloop.for_end = instr->ga_len;
7724
7725 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007726 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727
7728 // Below the ":for" scope drop the "expr" list from the stack.
7729 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7730 return NULL;
7731
7732 vim_free(scope);
7733
7734 return arg;
7735}
7736
7737/*
7738 * compile "while expr"
7739 *
7740 * Produces instructions:
7741 * top: EVAL expr Push result of "expr"
7742 * JUMP_IF_FALSE end jump if false
7743 * ... body ...
7744 * JUMP top Jump back to repeat
7745 * end:
7746 *
7747 */
7748 static char_u *
7749compile_while(char_u *arg, cctx_T *cctx)
7750{
7751 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007752 scope_T *scope;
7753
7754 scope = new_scope(cctx, WHILE_SCOPE);
7755 if (scope == NULL)
7756 return NULL;
7757
Bram Moolenaara91a7132021-03-25 21:12:15 +01007758 // "endwhile" jumps back here, one before when profiling or using cmdmods
7759 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007760
7761 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007762 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007763 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007764 if (!ends_excmd2(arg, skipwhite(p)))
7765 {
7766 semsg(_(e_trailing_arg), p);
7767 return NULL;
7768 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007769
Bram Moolenaar13106602020-10-04 16:06:05 +02007770 if (bool_on_stack(cctx) == FAIL)
7771 return FAIL;
7772
Bram Moolenaara91a7132021-03-25 21:12:15 +01007773 // CMDMOD_REV must come before the jump
7774 generate_undo_cmdmods(cctx);
7775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007776 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007777 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007778 JUMP_IF_FALSE, cctx) == FAIL)
7779 return FAIL;
7780
7781 return p;
7782}
7783
7784/*
7785 * compile "endwhile"
7786 */
7787 static char_u *
7788compile_endwhile(char_u *arg, cctx_T *cctx)
7789{
7790 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007791 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007792
Bram Moolenaarfa984412021-03-25 22:15:28 +01007793 if (misplaced_cmdmod(cctx))
7794 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007795 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7796 {
7797 emsg(_(e_while));
7798 return NULL;
7799 }
7800 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007801 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007802
Bram Moolenaarf002a412021-01-24 13:34:18 +01007803#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007804 // count the endwhile before jumping
7805 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007806#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007808 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007809 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810
7811 // Fill in the "end" label in the WHILE statement so it can jump here.
7812 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007813 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7814 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815
7816 vim_free(scope);
7817
7818 return arg;
7819}
7820
7821/*
7822 * compile "continue"
7823 */
7824 static char_u *
7825compile_continue(char_u *arg, cctx_T *cctx)
7826{
7827 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007828 int try_scopes = 0;
7829 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830
7831 for (;;)
7832 {
7833 if (scope == NULL)
7834 {
7835 emsg(_(e_continue));
7836 return NULL;
7837 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007838 if (scope->se_type == FOR_SCOPE)
7839 {
7840 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007841 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007842 }
7843 if (scope->se_type == WHILE_SCOPE)
7844 {
7845 loop_label = scope->se_u.se_while.ws_top_label;
7846 break;
7847 }
7848 if (scope->se_type == TRY_SCOPE)
7849 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850 scope = scope->se_outer;
7851 }
7852
Bram Moolenaarc150c092021-02-13 15:02:46 +01007853 if (try_scopes > 0)
7854 // Inside one or more try/catch blocks we first need to jump to the
7855 // "finally" or "endtry" to cleanup.
7856 generate_TRYCONT(cctx, try_scopes, loop_label);
7857 else
7858 // Jump back to the FOR or WHILE instruction.
7859 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007861 return arg;
7862}
7863
7864/*
7865 * compile "break"
7866 */
7867 static char_u *
7868compile_break(char_u *arg, cctx_T *cctx)
7869{
7870 scope_T *scope = cctx->ctx_scope;
7871 endlabel_T **el;
7872
7873 for (;;)
7874 {
7875 if (scope == NULL)
7876 {
7877 emsg(_(e_break));
7878 return NULL;
7879 }
7880 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7881 break;
7882 scope = scope->se_outer;
7883 }
7884
7885 // Jump to the end of the FOR or WHILE loop.
7886 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007887 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007888 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007889 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007890 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7891 return FAIL;
7892
7893 return arg;
7894}
7895
7896/*
7897 * compile "{" start of block
7898 */
7899 static char_u *
7900compile_block(char_u *arg, cctx_T *cctx)
7901{
7902 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7903 return NULL;
7904 return skipwhite(arg + 1);
7905}
7906
7907/*
7908 * compile end of block: drop one scope
7909 */
7910 static void
7911compile_endblock(cctx_T *cctx)
7912{
7913 scope_T *scope = cctx->ctx_scope;
7914
7915 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007916 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007917 vim_free(scope);
7918}
7919
7920/*
7921 * compile "try"
7922 * Creates a new scope for the try-endtry, pointing to the first catch and
7923 * finally.
7924 * Creates another scope for the "try" block itself.
7925 * TRY instruction sets up exception handling at runtime.
7926 *
7927 * "try"
7928 * TRY -> catch1, -> finally push trystack entry
7929 * ... try block
7930 * "throw {exception}"
7931 * EVAL {exception}
7932 * THROW create exception
7933 * ... try block
7934 * " catch {expr}"
7935 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007936 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007937 * EVAL {expr}
7938 * MATCH
7939 * JUMP nomatch -> catch2
7940 * CATCH remove exception
7941 * ... catch block
7942 * " catch"
7943 * JUMP -> finally
7944 * catch2: CATCH remove exception
7945 * ... catch block
7946 * " finally"
7947 * finally:
7948 * ... finally block
7949 * " endtry"
7950 * ENDTRY pop trystack entry, may rethrow
7951 */
7952 static char_u *
7953compile_try(char_u *arg, cctx_T *cctx)
7954{
7955 garray_T *instr = &cctx->ctx_instr;
7956 scope_T *try_scope;
7957 scope_T *scope;
7958
Bram Moolenaarfa984412021-03-25 22:15:28 +01007959 if (misplaced_cmdmod(cctx))
7960 return NULL;
7961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007962 // scope that holds the jumps that go to catch/finally/endtry
7963 try_scope = new_scope(cctx, TRY_SCOPE);
7964 if (try_scope == NULL)
7965 return NULL;
7966
Bram Moolenaar69f70502021-01-01 16:10:46 +01007967 if (cctx->ctx_skip != SKIP_YES)
7968 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007969 isn_T *isn;
7970
7971 // "try_catch" is set when the first ":catch" is found or when no catch
7972 // is found and ":finally" is found.
7973 // "try_finally" is set when ":finally" is found
7974 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007975 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007976 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7977 return NULL;
7978 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7979 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007980 return NULL;
7981 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007982
7983 // scope for the try block itself
7984 scope = new_scope(cctx, BLOCK_SCOPE);
7985 if (scope == NULL)
7986 return NULL;
7987
7988 return arg;
7989}
7990
7991/*
7992 * compile "catch {expr}"
7993 */
7994 static char_u *
7995compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7996{
7997 scope_T *scope = cctx->ctx_scope;
7998 garray_T *instr = &cctx->ctx_instr;
7999 char_u *p;
8000 isn_T *isn;
8001
Bram Moolenaarfa984412021-03-25 22:15:28 +01008002 if (misplaced_cmdmod(cctx))
8003 return NULL;
8004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008005 // end block scope from :try or :catch
8006 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8007 compile_endblock(cctx);
8008 scope = cctx->ctx_scope;
8009
8010 // Error if not in a :try scope
8011 if (scope == NULL || scope->se_type != TRY_SCOPE)
8012 {
8013 emsg(_(e_catch));
8014 return NULL;
8015 }
8016
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008017 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008018 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008019 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008020 return NULL;
8021 }
8022
Bram Moolenaar69f70502021-01-01 16:10:46 +01008023 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008024 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008025#ifdef FEAT_PROFILE
8026 // the profile-start should be after the jump
8027 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8028 .isn_type == ISN_PROF_START)
8029 --instr->ga_len;
8030#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008031 // Jump from end of previous block to :finally or :endtry
8032 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8033 JUMP_ALWAYS, cctx) == FAIL)
8034 return NULL;
8035
8036 // End :try or :catch scope: set value in ISN_TRY instruction
8037 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008038 if (isn->isn_arg.try.try_ref->try_catch == 0)
8039 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008040 if (scope->se_u.se_try.ts_catch_label != 0)
8041 {
8042 // Previous catch without match jumps here
8043 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8044 isn->isn_arg.jump.jump_where = instr->ga_len;
8045 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008046#ifdef FEAT_PROFILE
8047 if (cctx->ctx_profiling)
8048 {
8049 // a "throw" that jumps here needs to be counted
8050 generate_instr(cctx, ISN_PROF_END);
8051 // the "catch" is also counted
8052 generate_instr(cctx, ISN_PROF_START);
8053 }
8054#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008055 }
8056
8057 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008058 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008059 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008060 scope->se_u.se_try.ts_caught_all = TRUE;
8061 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008062 }
8063 else
8064 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008065 char_u *end;
8066 char_u *pat;
8067 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008068 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008069 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008071 // Push v:exception, push {expr} and MATCH
8072 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8073
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008074 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008075 if (*end != *p)
8076 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008077 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008078 vim_free(tofree);
8079 return FAIL;
8080 }
8081 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008082 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008083 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008084 len = (int)(end - tofree);
8085 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008086 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008087 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008088 if (pat == NULL)
8089 return FAIL;
8090 if (generate_PUSHS(cctx, pat) == FAIL)
8091 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008093 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8094 return NULL;
8095
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008096 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8098 return NULL;
8099 }
8100
Bram Moolenaar69f70502021-01-01 16:10:46 +01008101 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008102 return NULL;
8103
8104 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8105 return NULL;
8106 return p;
8107}
8108
8109 static char_u *
8110compile_finally(char_u *arg, cctx_T *cctx)
8111{
8112 scope_T *scope = cctx->ctx_scope;
8113 garray_T *instr = &cctx->ctx_instr;
8114 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008115 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008116
Bram Moolenaarfa984412021-03-25 22:15:28 +01008117 if (misplaced_cmdmod(cctx))
8118 return NULL;
8119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008120 // end block scope from :try or :catch
8121 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8122 compile_endblock(cctx);
8123 scope = cctx->ctx_scope;
8124
8125 // Error if not in a :try scope
8126 if (scope == NULL || scope->se_type != TRY_SCOPE)
8127 {
8128 emsg(_(e_finally));
8129 return NULL;
8130 }
8131
8132 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008133 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008134 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008135 {
8136 emsg(_(e_finally_dup));
8137 return NULL;
8138 }
8139
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008140 this_instr = instr->ga_len;
8141#ifdef FEAT_PROFILE
8142 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8143 .isn_type == ISN_PROF_START)
8144 // jump to the profile start of the "finally"
8145 --this_instr;
8146#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008147
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008148 // Fill in the "end" label in jumps at the end of the blocks.
8149 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8150 this_instr, cctx);
8151
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008152 // If there is no :catch then an exception jumps to :finally.
8153 if (isn->isn_arg.try.try_ref->try_catch == 0)
8154 isn->isn_arg.try.try_ref->try_catch = this_instr;
8155 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008156 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008157 {
8158 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008159 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008160 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008161 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008162 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008163 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8164 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166 // TODO: set index in ts_finally_label jumps
8167
8168 return arg;
8169}
8170
8171 static char_u *
8172compile_endtry(char_u *arg, cctx_T *cctx)
8173{
8174 scope_T *scope = cctx->ctx_scope;
8175 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008176 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177
Bram Moolenaarfa984412021-03-25 22:15:28 +01008178 if (misplaced_cmdmod(cctx))
8179 return NULL;
8180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008181 // end block scope from :catch or :finally
8182 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8183 compile_endblock(cctx);
8184 scope = cctx->ctx_scope;
8185
8186 // Error if not in a :try scope
8187 if (scope == NULL || scope->se_type != TRY_SCOPE)
8188 {
8189 if (scope == NULL)
8190 emsg(_(e_no_endtry));
8191 else if (scope->se_type == WHILE_SCOPE)
8192 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008193 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008194 emsg(_(e_endfor));
8195 else
8196 emsg(_(e_endif));
8197 return NULL;
8198 }
8199
Bram Moolenaarc150c092021-02-13 15:02:46 +01008200 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008201 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008202 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008203 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8204 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008205 {
8206 emsg(_(e_missing_catch_or_finally));
8207 return NULL;
8208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008209
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008210#ifdef FEAT_PROFILE
8211 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8212 .isn_type == ISN_PROF_START)
8213 // move the profile start after "endtry" so that it's not counted when
8214 // the exception is rethrown.
8215 --instr->ga_len;
8216#endif
8217
Bram Moolenaar69f70502021-01-01 16:10:46 +01008218 // Fill in the "end" label in jumps at the end of the blocks, if not
8219 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008220 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8221 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008222
Bram Moolenaar69f70502021-01-01 16:10:46 +01008223 if (scope->se_u.se_try.ts_catch_label != 0)
8224 {
8225 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008226 isn_T *isn = ((isn_T *)instr->ga_data)
8227 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008228 isn->isn_arg.jump.jump_where = instr->ga_len;
8229 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008230 }
8231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008232 compile_endblock(cctx);
8233
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008234 if (cctx->ctx_skip != SKIP_YES)
8235 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008236 // End :catch or :finally scope: set instruction index in ISN_TRY
8237 // instruction
8238 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008239 if (cctx->ctx_skip != SKIP_YES
8240 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8241 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008242#ifdef FEAT_PROFILE
8243 if (cctx->ctx_profiling)
8244 generate_instr(cctx, ISN_PROF_START);
8245#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008246 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008247 return arg;
8248}
8249
8250/*
8251 * compile "throw {expr}"
8252 */
8253 static char_u *
8254compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8255{
8256 char_u *p = skipwhite(arg);
8257
Bram Moolenaara5565e42020-05-09 15:44:01 +02008258 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008259 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008260 if (cctx->ctx_skip == SKIP_YES)
8261 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008262 if (may_generate_2STRING(-1, cctx) == FAIL)
8263 return NULL;
8264 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8265 return NULL;
8266
8267 return p;
8268}
8269
8270/*
8271 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008272 * compile "echomsg expr"
8273 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008274 * compile "execute expr"
8275 */
8276 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008277compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008278{
8279 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008280 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008281 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008282 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008283
8284 for (;;)
8285 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008286 if (ends_excmd2(prev, p))
8287 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008288 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008289 return NULL;
8290 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008291 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008292 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008293 }
8294
Bram Moolenaare4984292020-12-13 14:19:25 +01008295 if (count > 0)
8296 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008297 long save_lnum = cctx->ctx_lnum;
8298
8299 // Use the line number where the command started.
8300 cctx->ctx_lnum = start_ctx_lnum;
8301
Bram Moolenaare4984292020-12-13 14:19:25 +01008302 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8303 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8304 else if (cmdidx == CMD_execute)
8305 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8306 else if (cmdidx == CMD_echomsg)
8307 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8308 else
8309 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008310
8311 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008313 return p;
8314}
8315
8316/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008317 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008318 * instruction to compute it and return OK.
8319 * Otherwise return FAIL, the caller must deal with any range.
8320 */
8321 static int
8322compile_variable_range(exarg_T *eap, cctx_T *cctx)
8323{
8324 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8325 char_u *p = skipdigits(eap->cmd);
8326
8327 if (p == range_end)
8328 return FAIL;
8329 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8330}
8331
8332/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008333 * :put r
8334 * :put ={expr}
8335 */
8336 static char_u *
8337compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8338{
8339 char_u *line = arg;
8340 linenr_T lnum;
8341 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008342 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008343
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008344 eap->regname = *line;
8345
8346 if (eap->regname == '=')
8347 {
8348 char_u *p = line + 1;
8349
8350 if (compile_expr0(&p, cctx) == FAIL)
8351 return NULL;
8352 line = p;
8353 }
8354 else if (eap->regname != NUL)
8355 ++line;
8356
Bram Moolenaar08597872020-12-10 19:43:40 +01008357 if (compile_variable_range(eap, cctx) == OK)
8358 {
8359 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8360 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008361 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008362 {
8363 // Either no range or a number.
8364 // "errormsg" will not be set because the range is ADDR_LINES.
8365 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008366 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008367 return NULL;
8368 if (eap->addr_count == 0)
8369 lnum = -1;
8370 else
8371 lnum = eap->line2;
8372 if (above)
8373 --lnum;
8374 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008375
8376 generate_PUT(cctx, eap->regname, lnum);
8377 return line;
8378}
8379
8380/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008381 * A command that is not compiled, execute with legacy code.
8382 */
8383 static char_u *
8384compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8385{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008386 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008387 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008388 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008389
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008390 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008391 goto theend;
8392
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008393 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008394 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008395 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008396 int usefilter = FALSE;
8397
8398 has_expr = argt & (EX_XFILE | EX_EXPAND);
8399
8400 // If the command can be followed by a bar, find the bar and truncate
8401 // it, so that the following command can be compiled.
8402 // The '|' is overwritten with a NUL, it is put back below.
8403 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8404 && *eap->arg == '!')
8405 // :w !filter or :r !filter or :r! filter
8406 usefilter = TRUE;
8407 if ((argt & EX_TRLBAR) && !usefilter)
8408 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008409 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008410 separate_nextcmd(eap);
8411 if (eap->nextcmd != NULL)
8412 nextcmd = eap->nextcmd;
8413 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008414 else if (eap->cmdidx == CMD_wincmd)
8415 {
8416 p = eap->arg;
8417 if (*p != NUL)
8418 ++p;
8419 if (*p == 'g' || *p == Ctrl_G)
8420 ++p;
8421 p = skipwhite(p);
8422 if (*p == '|')
8423 {
8424 *p = NUL;
8425 nextcmd = p + 1;
8426 }
8427 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008428 }
8429
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008430 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8431 {
8432 // expand filename in "syntax include [@group] filename"
8433 has_expr = TRUE;
8434 eap->arg = skipwhite(eap->arg + 7);
8435 if (*eap->arg == '@')
8436 eap->arg = skiptowhite(eap->arg);
8437 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008438
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008439 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8440 && STRLEN(eap->arg) > 4)
8441 {
8442 int delim = *eap->arg;
8443
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008444 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008445 if (*p == delim)
8446 {
8447 eap->arg = p + 1;
8448 has_expr = TRUE;
8449 }
8450 }
8451
Bram Moolenaarecac5912021-01-05 19:23:28 +01008452 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8453 {
8454 // TODO: should only expand when appropriate for the command
8455 eap->arg = skiptowhite(eap->arg);
8456 has_expr = TRUE;
8457 }
8458
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008459 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008460 {
8461 int count = 0;
8462 char_u *start = skipwhite(line);
8463
8464 // :cmd xxx`=expr1`yyy`=expr2`zzz
8465 // PUSHS ":cmd xxx"
8466 // eval expr1
8467 // PUSHS "yyy"
8468 // eval expr2
8469 // PUSHS "zzz"
8470 // EXECCONCAT 5
8471 for (;;)
8472 {
8473 if (p > start)
8474 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008475 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008476 ++count;
8477 }
8478 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008479 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008480 return NULL;
8481 may_generate_2STRING(-1, cctx);
8482 ++count;
8483 p = skipwhite(p);
8484 if (*p != '`')
8485 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008486 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008487 return NULL;
8488 }
8489 start = p + 1;
8490
8491 p = (char_u *)strstr((char *)start, "`=");
8492 if (p == NULL)
8493 {
8494 if (*skipwhite(start) != NUL)
8495 {
8496 generate_PUSHS(cctx, vim_strsave(start));
8497 ++count;
8498 }
8499 break;
8500 }
8501 }
8502 generate_EXECCONCAT(cctx, count);
8503 }
8504 else
8505 generate_EXEC(cctx, line);
8506
8507theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008508 if (*nextcmd != NUL)
8509 {
8510 // the parser expects a pointer to the bar, put it back
8511 --nextcmd;
8512 *nextcmd = '|';
8513 }
8514
8515 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008516}
8517
Bram Moolenaar8238f082021-04-20 21:10:48 +02008518
8519 static void
8520clear_instr_ga(garray_T *gap)
8521{
8522 int idx;
8523
8524 for (idx = 0; idx < gap->ga_len; ++idx)
8525 delete_instr(((isn_T *)gap->ga_data) + idx);
8526 ga_clear(gap);
8527}
8528
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008529/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008530 * :s/pat/repl/
8531 */
8532 static char_u *
8533compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8534{
8535 char_u *cmd = eap->arg;
8536 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8537
8538 if (expr != NULL)
8539 {
8540 int delimiter = *cmd++;
8541
8542 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008543 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008544 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8545 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008546 garray_T save_ga = cctx->ctx_instr;
8547 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008548 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008549 int trailing_error;
8550 int instr_count;
8551 isn_T *instr = NULL;
8552 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008553
8554 cmd += 3;
8555 end = skip_substitute(cmd, delimiter);
8556
Bram Moolenaar8238f082021-04-20 21:10:48 +02008557 // Temporarily reset the list of instructions so that the jumps
8558 // labels are correct.
8559 cctx->ctx_instr.ga_len = 0;
8560 cctx->ctx_instr.ga_maxlen = 0;
8561 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008562 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008563 if (end[-1] == NUL)
8564 end[-1] = delimiter;
8565 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008566 trailing_error = *cmd != delimiter && *cmd != NUL;
8567
Bram Moolenaar169502f2021-04-21 12:19:35 +02008568 if (expr_res == FAIL || trailing_error
8569 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008570 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008571 if (trailing_error)
8572 semsg(_(e_trailing_arg), cmd);
8573 clear_instr_ga(&cctx->ctx_instr);
8574 cctx->ctx_instr = save_ga;
8575 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008576 return NULL;
8577 }
8578
Bram Moolenaar8238f082021-04-20 21:10:48 +02008579 // Move the generated instructions into the ISN_SUBSTITUTE
8580 // instructions, then restore the list of instructions before
8581 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008582 instr_count = cctx->ctx_instr.ga_len;
8583 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008584 instr[instr_count].isn_type = ISN_FINISH;
8585
8586 cctx->ctx_instr = save_ga;
8587 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8588 {
8589 int idx;
8590
8591 for (idx = 0; idx < instr_count; ++idx)
8592 delete_instr(instr + idx);
8593 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008594 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008595 }
8596 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8597 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008598
8599 // skip over flags
8600 if (*end == '&')
8601 ++end;
8602 while (ASCII_ISALPHA(*end) || *end == '#')
8603 ++end;
8604 return end;
8605 }
8606 }
8607
8608 return compile_exec(arg, eap, cctx);
8609}
8610
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008611 static char_u *
8612compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8613{
8614 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008615 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008616
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008617 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008618 {
8619 if (STRNCMP(arg, "END", 3) == 0)
8620 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008621 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008622 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008623 // First load the current variable value.
8624 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8625 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008626 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008627 }
8628
8629 // Gets the redirected text and put it on the stack, then store it
8630 // in the variable.
8631 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8632
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008633 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008634 generate_instr_drop(cctx, ISN_CONCAT, 1);
8635
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008636 if (lhs->lhs_has_index)
8637 {
8638 // Use the info in "lhs" to store the value at the index in the
8639 // list or dict.
8640 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8641 &t_string, cctx) == FAIL)
8642 return NULL;
8643 }
8644 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008645 return NULL;
8646
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008647 VIM_CLEAR(lhs->lhs_name);
8648 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008649 return arg + 3;
8650 }
8651 emsg(_(e_cannot_nest_redir));
8652 return NULL;
8653 }
8654
8655 if (arg[0] == '=' && arg[1] == '>')
8656 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008657 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008658
8659 // redirect to a variable is compiled
8660 arg += 2;
8661 if (*arg == '>')
8662 {
8663 ++arg;
8664 append = TRUE;
8665 }
8666 arg = skipwhite(arg);
8667
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008668 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008669 FALSE, FALSE, 1, cctx) == FAIL)
8670 return NULL;
8671 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008672 lhs->lhs_append = append;
8673 if (lhs->lhs_has_index)
8674 {
8675 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8676 if (lhs->lhs_whole == NULL)
8677 return NULL;
8678 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008679
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008680 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008681 }
8682
8683 // other redirects are handled like at script level
8684 return compile_exec(line, eap, cctx);
8685}
8686
Bram Moolenaar4c137212021-04-19 16:48:48 +02008687/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008688 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008689 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008690 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008691 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008692add_def_function(ufunc_T *ufunc)
8693{
8694 dfunc_T *dfunc;
8695
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008696 if (def_functions.ga_len == 0)
8697 {
8698 // The first position is not used, so that a zero uf_dfunc_idx means it
8699 // wasn't set.
8700 if (ga_grow(&def_functions, 1) == FAIL)
8701 return FAIL;
8702 ++def_functions.ga_len;
8703 }
8704
Bram Moolenaar09689a02020-05-09 22:50:08 +02008705 // Add the function to "def_functions".
8706 if (ga_grow(&def_functions, 1) == FAIL)
8707 return FAIL;
8708 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8709 CLEAR_POINTER(dfunc);
8710 dfunc->df_idx = def_functions.ga_len;
8711 ufunc->uf_dfunc_idx = dfunc->df_idx;
8712 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008713 dfunc->df_name = vim_strsave(ufunc->uf_name);
8714 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008715 ++def_functions.ga_len;
8716 return OK;
8717}
8718
8719/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008720 * After ex_function() has collected all the function lines: parse and compile
8721 * the lines into instructions.
8722 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008723 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8724 * the return statement (used for lambda). When uf_ret_type is already set
8725 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008726 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008727 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008728 * This can be used recursively through compile_lambda(), which may reallocate
8729 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008730 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008731 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008732 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008733compile_def_function(
8734 ufunc_T *ufunc,
8735 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008736 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008737 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008738{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008739 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008740 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008741 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008742 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008743 cctx_T cctx;
8744 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008745 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008746 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008747 int ret = FAIL;
8748 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008749 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008750 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008751 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008752#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008753 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008754#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008755
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008756 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008757 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008758 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008759 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008760 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8761 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008762 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008763 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008764 else
8765 {
8766 if (add_def_function(ufunc) == FAIL)
8767 return FAIL;
8768 new_def_function = TRUE;
8769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008770
Bram Moolenaar985116a2020-07-12 17:31:09 +02008771 ufunc->uf_def_status = UF_COMPILING;
8772
Bram Moolenaara80faa82020-04-12 19:37:17 +02008773 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008774
Bram Moolenaarf002a412021-01-24 13:34:18 +01008775#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008776 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008777#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008778 cctx.ctx_ufunc = ufunc;
8779 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008780 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008781 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8782 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8783 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8784 cctx.ctx_type_list = &ufunc->uf_type_list;
8785 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8786 instr = &cctx.ctx_instr;
8787
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008788 // Set the context to the function, it may be compiled when called from
8789 // another script. Set the script version to the most modern one.
8790 // The line number will be set in next_line_from_context().
8791 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008792 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8793
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008794 // Make sure error messages are OK.
8795 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8796 if (do_estack_push)
8797 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008798 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008799
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008800 if (ufunc->uf_def_args.ga_len > 0)
8801 {
8802 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008803 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008804 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008805 int i;
8806 char_u *arg;
8807 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008808 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008809
8810 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008811 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008812 for (i = 0; i < count; ++i)
8813 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008814 garray_T *stack = &cctx.ctx_type_stack;
8815 type_T *val_type;
8816 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008817 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008818 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008819 int jump_instr_idx = instr->ga_len;
8820 isn_T *isn;
8821
8822 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8823 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8824 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008825
8826 // Make sure later arguments are not found.
8827 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008828
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008829 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008830 r = compile_expr0(&arg, &cctx);
8831
8832 ufunc->uf_args.ga_len = uf_args_len;
8833 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008834 goto erret;
8835
8836 // If no type specified use the type of the default value.
8837 // Otherwise check that the default value type matches the
8838 // specified type.
8839 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008840 where.wt_index = arg_idx + 1;
8841 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008842 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008843 {
8844 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008845 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008846 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008847 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008848 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008849 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008850
8851 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008852 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008853
8854 // set instruction index in JUMP_IF_ARG_SET to here
8855 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8856 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008857 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008858
8859 if (did_set_arg_type)
8860 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008861 }
8862
8863 /*
8864 * Loop over all the lines of the function and generate instructions.
8865 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008866 for (;;)
8867 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008868 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008869 int starts_with_colon = FALSE;
8870 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008871 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008872
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008873 // Bail out on the first error to avoid a flood of errors and report
8874 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008875 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008876 goto erret;
8877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008878 if (line != NULL && *line == '|')
8879 // the line continues after a '|'
8880 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008881 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008882 && !(*line == '#' && (line == cctx.ctx_line_start
8883 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008884 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008885 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008886 goto erret;
8887 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008888 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8889 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008890 else
8891 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008892 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008893 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008894 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008895 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008896#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008897 if (cctx.ctx_skip != SKIP_YES)
8898 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008899#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008900 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008901 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008902 // Make a copy, splitting off nextcmd and removing trailing spaces
8903 // may change it.
8904 if (line != NULL)
8905 {
8906 line = vim_strsave(line);
8907 vim_free(line_to_free);
8908 line_to_free = line;
8909 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008910 }
8911
Bram Moolenaara80faa82020-04-12 19:37:17 +02008912 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008913 ea.cmdlinep = &line;
8914 ea.cmd = skipwhite(line);
8915
Bram Moolenaarb2049902021-01-24 12:53:53 +01008916 if (*ea.cmd == '#')
8917 {
8918 // "#" starts a comment
8919 line = (char_u *)"";
8920 continue;
8921 }
8922
Bram Moolenaarf002a412021-01-24 13:34:18 +01008923#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008924 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8925 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008926 {
8927 may_generate_prof_end(&cctx, prof_lnum);
8928
8929 prof_lnum = cctx.ctx_lnum;
8930 generate_instr(&cctx, ISN_PROF_START);
8931 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008932#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008933
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008934 // Some things can be recognized by the first character.
8935 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008936 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008937 case '}':
8938 {
8939 // "}" ends a block scope
8940 scopetype_T stype = cctx.ctx_scope == NULL
8941 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008942
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008943 if (stype == BLOCK_SCOPE)
8944 {
8945 compile_endblock(&cctx);
8946 line = ea.cmd;
8947 }
8948 else
8949 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008950 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008951 goto erret;
8952 }
8953 if (line != NULL)
8954 line = skipwhite(ea.cmd + 1);
8955 continue;
8956 }
8957
8958 case '{':
8959 // "{" starts a block scope
8960 // "{'a': 1}->func() is something else
8961 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8962 {
8963 line = compile_block(ea.cmd, &cctx);
8964 continue;
8965 }
8966 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008967 }
8968
8969 /*
8970 * COMMAND MODIFIERS
8971 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008972 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008973 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8974 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008975 {
8976 if (errormsg != NULL)
8977 goto erret;
8978 // empty line or comment
8979 line = (char_u *)"";
8980 continue;
8981 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008982 generate_cmdmods(&cctx, &local_cmdmod);
8983 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008984
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008985 // Check if there was a colon after the last command modifier or before
8986 // the current position.
8987 for (p = ea.cmd; p >= line; --p)
8988 {
8989 if (*p == ':')
8990 starts_with_colon = TRUE;
8991 if (p < ea.cmd && !VIM_ISWHITE(*p))
8992 break;
8993 }
8994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008995 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008996 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008997 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008998 {
8999 if (*ea.cmd == '(')
9000 // not for "call()"
9001 ea.cmd = p;
9002 else
9003 ea.cmd = skipwhite(ea.cmd);
9004 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009005
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009006 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009007 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009008 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009009
Bram Moolenaar17126b12021-01-07 22:03:02 +01009010 // Check for assignment after command modifiers.
9011 assign = may_compile_assignment(&ea, &line, &cctx);
9012 if (assign == OK)
9013 goto nextline;
9014 if (assign == FAIL)
9015 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009016 }
9017
9018 /*
9019 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009020 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009021 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009022 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02009023 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009024 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009025 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009026 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009027 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009028 if (!starts_with_colon)
9029 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009030 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009031 goto erret;
9032 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009033 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009034 if (ends_excmd2(line, ea.cmd))
9035 {
9036 // A range without a command: jump to the line.
9037 // TODO: compile to a more efficient command, possibly
9038 // calling parse_cmd_address().
9039 ea.cmdidx = CMD_SIZE;
9040 line = compile_exec(line, &ea, &cctx);
9041 goto nextline;
9042 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009043 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009044 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009045 p = find_ex_command(&ea, NULL, starts_with_colon
9046 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009047
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009048 if (p == NULL)
9049 {
9050 if (cctx.ctx_skip != SKIP_YES)
9051 emsg(_(e_ambiguous_use_of_user_defined_command));
9052 goto erret;
9053 }
9054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009055 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9056 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009057 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009058 {
9059 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009060 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009061 }
9062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009063 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009064 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009065 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009066 // CMD_var cannot happen, compile_assignment() above would be
9067 // used. Most likely an assignment to a non-existing variable.
9068 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009069 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009071 }
9072
Bram Moolenaar3988f642020-08-27 22:43:03 +02009073 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009074 && ea.cmdidx != CMD_elseif
9075 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009076 && ea.cmdidx != CMD_endif
9077 && ea.cmdidx != CMD_endfor
9078 && ea.cmdidx != CMD_endwhile
9079 && ea.cmdidx != CMD_catch
9080 && ea.cmdidx != CMD_finally
9081 && ea.cmdidx != CMD_endtry)
9082 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009083 emsg(_(e_unreachable_code_after_return));
9084 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009085 }
9086
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009087 p = skipwhite(p);
9088 if (ea.cmdidx != CMD_SIZE
9089 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9090 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009091 if (ea.cmdidx >= 0)
9092 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009093 if ((ea.argt & EX_BANG) && *p == '!')
9094 {
9095 ea.forceit = TRUE;
9096 p = skipwhite(p + 1);
9097 }
9098 }
9099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009100 switch (ea.cmdidx)
9101 {
9102 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009103 ea.arg = p;
9104 line = compile_nested_function(&ea, &cctx);
9105 break;
9106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009107 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009108 // TODO: should we allow this, e.g. to declare a global
9109 // function?
9110 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009111 goto erret;
9112
9113 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009114 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009115 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009116 break;
9117
9118 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009119 emsg(_(e_cannot_use_let_in_vim9_script));
9120 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009121 case CMD_var:
9122 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009123 case CMD_const:
9124 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009125 if (line == p)
9126 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009127 break;
9128
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009129 case CMD_unlet:
9130 case CMD_unlockvar:
9131 case CMD_lockvar:
9132 line = compile_unletlock(p, &ea, &cctx);
9133 break;
9134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009135 case CMD_import:
9136 line = compile_import(p, &cctx);
9137 break;
9138
9139 case CMD_if:
9140 line = compile_if(p, &cctx);
9141 break;
9142 case CMD_elseif:
9143 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009144 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009145 break;
9146 case CMD_else:
9147 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009148 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009149 break;
9150 case CMD_endif:
9151 line = compile_endif(p, &cctx);
9152 break;
9153
9154 case CMD_while:
9155 line = compile_while(p, &cctx);
9156 break;
9157 case CMD_endwhile:
9158 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009159 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009160 break;
9161
9162 case CMD_for:
9163 line = compile_for(p, &cctx);
9164 break;
9165 case CMD_endfor:
9166 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009167 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009168 break;
9169 case CMD_continue:
9170 line = compile_continue(p, &cctx);
9171 break;
9172 case CMD_break:
9173 line = compile_break(p, &cctx);
9174 break;
9175
9176 case CMD_try:
9177 line = compile_try(p, &cctx);
9178 break;
9179 case CMD_catch:
9180 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009181 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009182 break;
9183 case CMD_finally:
9184 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009185 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009186 break;
9187 case CMD_endtry:
9188 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009189 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009190 break;
9191 case CMD_throw:
9192 line = compile_throw(p, &cctx);
9193 break;
9194
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009195 case CMD_eval:
9196 if (compile_expr0(&p, &cctx) == FAIL)
9197 goto erret;
9198
Bram Moolenaar3988f642020-08-27 22:43:03 +02009199 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009200 generate_instr_drop(&cctx, ISN_DROP, 1);
9201
9202 line = skipwhite(p);
9203 break;
9204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009205 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009206 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009207 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009208 case CMD_echomsg:
9209 case CMD_echoerr:
9210 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009211 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009212
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009213 case CMD_put:
9214 ea.cmd = cmd;
9215 line = compile_put(p, &ea, &cctx);
9216 break;
9217
Bram Moolenaar4c137212021-04-19 16:48:48 +02009218 case CMD_substitute:
9219 if (cctx.ctx_skip == SKIP_YES)
9220 line = (char_u *)"";
9221 else
9222 {
9223 ea.arg = p;
9224 line = compile_substitute(line, &ea, &cctx);
9225 }
9226 break;
9227
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009228 case CMD_redir:
9229 ea.arg = p;
9230 line = compile_redir(line, &ea, &cctx);
9231 break;
9232
Bram Moolenaar3988f642020-08-27 22:43:03 +02009233 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009234
Bram Moolenaarae616492020-07-28 20:07:27 +02009235 case CMD_append:
9236 case CMD_change:
9237 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009238 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009239 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009240 case CMD_xit:
9241 not_in_vim9(&ea);
9242 goto erret;
9243
Bram Moolenaar002262f2020-07-08 17:47:57 +02009244 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009245 if (cctx.ctx_skip != SKIP_YES)
9246 {
9247 semsg(_(e_invalid_command_str), ea.cmd);
9248 goto erret;
9249 }
9250 // We don't check for a next command here.
9251 line = (char_u *)"";
9252 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009254 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009255 if (cctx.ctx_skip == SKIP_YES)
9256 {
9257 // We don't check for a next command here.
9258 line = (char_u *)"";
9259 }
9260 else
9261 {
9262 // Not recognized, execute with do_cmdline_cmd().
9263 ea.arg = p;
9264 line = compile_exec(line, &ea, &cctx);
9265 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009266 break;
9267 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009268nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009269 if (line == NULL)
9270 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009271 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009272
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009273 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009274 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009276 if (cctx.ctx_type_stack.ga_len < 0)
9277 {
9278 iemsg("Type stack underflow");
9279 goto erret;
9280 }
9281 }
9282
9283 if (cctx.ctx_scope != NULL)
9284 {
9285 if (cctx.ctx_scope->se_type == IF_SCOPE)
9286 emsg(_(e_endif));
9287 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9288 emsg(_(e_endwhile));
9289 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9290 emsg(_(e_endfor));
9291 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009292 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009293 goto erret;
9294 }
9295
Bram Moolenaarefd88552020-06-18 20:50:10 +02009296 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009297 {
9298 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
9299 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009300 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009301 goto erret;
9302 }
9303
9304 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01009305 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009306 }
9307
Bram Moolenaar599410c2021-04-10 14:03:43 +02009308 // When compiled with ":silent!" and there was an error don't consider the
9309 // function compiled.
9310 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009311 {
9312 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9313 + ufunc->uf_dfunc_idx;
9314 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009315 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009316#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009317 if (cctx.ctx_profiling)
9318 {
9319 dfunc->df_instr_prof = instr->ga_data;
9320 dfunc->df_instr_prof_count = instr->ga_len;
9321 }
9322 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009323#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009324 {
9325 dfunc->df_instr = instr->ga_data;
9326 dfunc->df_instr_count = instr->ga_len;
9327 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009328 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009329 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009330 if (cctx.ctx_outer_used)
9331 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009332 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009333 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009334
9335 ret = OK;
9336
9337erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009338 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009339 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009340 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9341 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009342
Bram Moolenaar8238f082021-04-20 21:10:48 +02009343 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009344 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009345
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009346 // If using the last entry in the table and it was added above, we
9347 // might as well remove it.
9348 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009349 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009350 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009351 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009352 ufunc->uf_dfunc_idx = 0;
9353 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009354 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009355
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009356 while (cctx.ctx_scope != NULL)
9357 drop_scope(&cctx);
9358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009359 if (errormsg != NULL)
9360 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009361 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009362 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009363 }
9364
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009365 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9366 {
9367 if (ret == OK)
9368 {
9369 emsg(_(e_missing_redir_end));
9370 ret = FAIL;
9371 }
9372 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009373 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009374 }
9375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009376 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009377 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009378 if (do_estack_push)
9379 estack_pop();
9380
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009381 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009382 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009383 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009384 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009385 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009386}
9387
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009388 void
9389set_function_type(ufunc_T *ufunc)
9390{
9391 int varargs = ufunc->uf_va_name != NULL;
9392 int argcount = ufunc->uf_args.ga_len;
9393
9394 // Create a type for the function, with the return type and any
9395 // argument types.
9396 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9397 // The type is included in "tt_args".
9398 if (argcount > 0 || varargs)
9399 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009400 if (ufunc->uf_type_list.ga_itemsize == 0)
9401 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009402 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9403 argcount, &ufunc->uf_type_list);
9404 // Add argument types to the function type.
9405 if (func_type_add_arg_types(ufunc->uf_func_type,
9406 argcount + varargs,
9407 &ufunc->uf_type_list) == FAIL)
9408 return;
9409 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9410 ufunc->uf_func_type->tt_min_argcount =
9411 argcount - ufunc->uf_def_args.ga_len;
9412 if (ufunc->uf_arg_types == NULL)
9413 {
9414 int i;
9415
9416 // lambda does not have argument types.
9417 for (i = 0; i < argcount; ++i)
9418 ufunc->uf_func_type->tt_args[i] = &t_any;
9419 }
9420 else
9421 mch_memmove(ufunc->uf_func_type->tt_args,
9422 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9423 if (varargs)
9424 {
9425 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009426 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009427 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9428 }
9429 }
9430 else
9431 // No arguments, can use a predefined type.
9432 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9433 argcount, &ufunc->uf_type_list);
9434}
9435
9436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009437/*
9438 * Delete an instruction, free what it contains.
9439 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009440 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009441delete_instr(isn_T *isn)
9442{
9443 switch (isn->isn_type)
9444 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009445 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009446 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009447 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009448 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009449 case ISN_LOADENV:
9450 case ISN_LOADG:
9451 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009452 case ISN_LOADT:
9453 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009454 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009455 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009456 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009457 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009458 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009459 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009460 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009461 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009462 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009463 case ISN_STOREW:
9464 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009465 vim_free(isn->isn_arg.string);
9466 break;
9467
Bram Moolenaar4c137212021-04-19 16:48:48 +02009468 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009469 {
9470 int idx;
9471 isn_T *list = isn->isn_arg.subs.subs_instr;
9472
9473 vim_free(isn->isn_arg.subs.subs_cmd);
9474 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9475 delete_instr(list + idx);
9476 vim_free(list);
9477 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009478 break;
9479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009480 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009481 case ISN_STORES:
9482 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009483 break;
9484
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009485 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009486 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009487 vim_free(isn->isn_arg.unlet.ul_name);
9488 break;
9489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009490 case ISN_STOREOPT:
9491 vim_free(isn->isn_arg.storeopt.so_name);
9492 break;
9493
9494 case ISN_PUSHBLOB: // push blob isn_arg.blob
9495 blob_unref(isn->isn_arg.blob);
9496 break;
9497
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009498 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009499#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009500 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009501#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009502 break;
9503
9504 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009505#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009506 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009507#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009508 break;
9509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009510 case ISN_UCALL:
9511 vim_free(isn->isn_arg.ufunc.cuf_name);
9512 break;
9513
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009514 case ISN_FUNCREF:
9515 {
9516 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9517 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009518 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009519
Bram Moolenaar077a4232020-12-22 18:33:27 +01009520 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9521 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009522 }
9523 break;
9524
9525 case ISN_DCALL:
9526 {
9527 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9528 + isn->isn_arg.dfunc.cdf_idx;
9529
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009530 if (dfunc->df_ufunc != NULL
9531 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009532 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009533 }
9534 break;
9535
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009536 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009537 {
9538 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9539 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9540
9541 if (ufunc != NULL)
9542 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009543 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009544 func_ptr_unref(ufunc);
9545 }
9546
9547 vim_free(lambda);
9548 vim_free(isn->isn_arg.newfunc.nf_global);
9549 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009550 break;
9551
Bram Moolenaar5e654232020-09-16 15:22:00 +02009552 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009553 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009554 free_type(isn->isn_arg.type.ct_type);
9555 break;
9556
Bram Moolenaar02194d22020-10-24 23:08:38 +02009557 case ISN_CMDMOD:
9558 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9559 ->cmod_filter_regmatch.regprog);
9560 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9561 break;
9562
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009563 case ISN_LOADSCRIPT:
9564 case ISN_STORESCRIPT:
9565 vim_free(isn->isn_arg.script.scriptref);
9566 break;
9567
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009568 case ISN_TRY:
9569 vim_free(isn->isn_arg.try.try_ref);
9570 break;
9571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009572 case ISN_2BOOL:
9573 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009574 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009575 case ISN_ADDBLOB:
9576 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009577 case ISN_ANYINDEX:
9578 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009579 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009580 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009581 case ISN_BLOBINDEX:
9582 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009583 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009584 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009585 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009586 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009587 case ISN_COMPAREANY:
9588 case ISN_COMPAREBLOB:
9589 case ISN_COMPAREBOOL:
9590 case ISN_COMPAREDICT:
9591 case ISN_COMPAREFLOAT:
9592 case ISN_COMPAREFUNC:
9593 case ISN_COMPARELIST:
9594 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009595 case ISN_COMPARESPECIAL:
9596 case ISN_COMPARESTRING:
9597 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009598 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009599 case ISN_DROP:
9600 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009601 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009602 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009603 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009604 case ISN_EXECCONCAT:
9605 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009606 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009607 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009608 case ISN_GETITEM:
9609 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009610 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009611 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009612 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009613 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009614 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009615 case ISN_LOADBDICT:
9616 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009617 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009618 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009619 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009620 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009621 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009622 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009623 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009624 case ISN_NEGATENR:
9625 case ISN_NEWDICT:
9626 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009627 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009628 case ISN_OPFLOAT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009629 case ISN_FINISH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009630 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009631 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009632 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009633 case ISN_PROF_END:
9634 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009635 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009636 case ISN_PUSHF:
9637 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009638 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009639 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009640 case ISN_REDIREND:
9641 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009642 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009643 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009644 case ISN_SHUFFLE:
9645 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009646 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009647 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009648 case ISN_STORENR:
9649 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009650 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009651 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009652 case ISN_STOREV:
9653 case ISN_STRINDEX:
9654 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009655 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009656 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009657 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009658 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009659 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009660 // nothing allocated
9661 break;
9662 }
9663}
9664
9665/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009666 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009667 */
9668 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009669delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009670{
9671 int idx;
9672
9673 ga_clear(&dfunc->df_def_args_isn);
9674
9675 if (dfunc->df_instr != NULL)
9676 {
9677 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9678 delete_instr(dfunc->df_instr + idx);
9679 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009680 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009681 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009682#ifdef FEAT_PROFILE
9683 if (dfunc->df_instr_prof != NULL)
9684 {
9685 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9686 delete_instr(dfunc->df_instr_prof + idx);
9687 VIM_CLEAR(dfunc->df_instr_prof);
9688 dfunc->df_instr_prof = NULL;
9689 }
9690#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009691
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009692 if (mark_deleted)
9693 dfunc->df_deleted = TRUE;
9694 if (dfunc->df_ufunc != NULL)
9695 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009696}
9697
9698/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009699 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009700 * function, unless another user function still uses it.
9701 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009702 */
9703 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009704unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009705{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009706 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009707 {
9708 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9709 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009710
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009711 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009712 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009713 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009714 ufunc->uf_dfunc_idx = 0;
9715 if (dfunc->df_ufunc == ufunc)
9716 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009717 }
9718}
9719
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009720/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009721 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009722 */
9723 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009724link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009725{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009726 if (ufunc->uf_dfunc_idx > 0)
9727 {
9728 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9729 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009730
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009731 ++dfunc->df_refcount;
9732 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009733}
9734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009735#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009736/*
9737 * Free all functions defined with ":def".
9738 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009739 void
9740free_def_functions(void)
9741{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009742 int idx;
9743
9744 for (idx = 0; idx < def_functions.ga_len; ++idx)
9745 {
9746 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9747
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009748 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009749 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009750 }
9751
9752 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009753}
9754#endif
9755
9756
9757#endif // FEAT_EVAL