blob: 2ea487de78e0f2204ec7a311039dea17eb6da57a [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:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200618 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200619 to_string_error((*type)->tt_type);
620 return FAIL;
621 }
622
623 *type = &t_string;
624 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 return FAIL;
626 isn->isn_arg.number = offset;
627
628 return OK;
629}
630
631 static int
632check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
633{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200634 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200636 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 {
638 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200639 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100640 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200641 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 return FAIL;
643 }
644 return OK;
645}
646
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200647 static int
648generate_add_instr(
649 cctx_T *cctx,
650 vartype_T vartype,
651 type_T *type1,
652 type_T *type2)
653{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100654 garray_T *stack = &cctx->ctx_type_stack;
655 isn_T *isn = generate_instr_drop(cctx,
656 vartype == VAR_NUMBER ? ISN_OPNR
657 : vartype == VAR_LIST ? ISN_ADDLIST
658 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200659#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100660 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200661#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100662 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200663
664 if (vartype != VAR_LIST && vartype != VAR_BLOB
665 && type1->tt_type != VAR_ANY
666 && type2->tt_type != VAR_ANY
667 && check_number_or_float(
668 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
669 return FAIL;
670
671 if (isn != NULL)
672 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100673
674 // When concatenating two lists with different member types the member type
675 // becomes "any".
676 if (vartype == VAR_LIST
677 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
678 && type1->tt_member != type2->tt_member)
679 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
680
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200681 return isn == NULL ? FAIL : OK;
682}
683
684/*
685 * Get the type to use for an instruction for an operation on "type1" and
686 * "type2". If they are matching use a type-specific instruction. Otherwise
687 * fall back to runtime type checking.
688 */
689 static vartype_T
690operator_type(type_T *type1, type_T *type2)
691{
692 if (type1->tt_type == type2->tt_type
693 && (type1->tt_type == VAR_NUMBER
694 || type1->tt_type == VAR_LIST
695#ifdef FEAT_FLOAT
696 || type1->tt_type == VAR_FLOAT
697#endif
698 || type1->tt_type == VAR_BLOB))
699 return type1->tt_type;
700 return VAR_ANY;
701}
702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703/*
704 * Generate an instruction with two arguments. The instruction depends on the
705 * type of the arguments.
706 */
707 static int
708generate_two_op(cctx_T *cctx, char_u *op)
709{
710 garray_T *stack = &cctx->ctx_type_stack;
711 type_T *type1;
712 type_T *type2;
713 vartype_T vartype;
714 isn_T *isn;
715
Bram Moolenaar080457c2020-03-03 21:53:32 +0100716 RETURN_OK_IF_SKIP(cctx);
717
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200718 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
720 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200721 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722
723 switch (*op)
724 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200725 case '+':
726 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 break;
729
730 case '-':
731 case '*':
732 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
733 op) == FAIL)
734 return FAIL;
735 if (vartype == VAR_NUMBER)
736 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
737#ifdef FEAT_FLOAT
738 else if (vartype == VAR_FLOAT)
739 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
740#endif
741 else
742 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
743 if (isn != NULL)
744 isn->isn_arg.op.op_type = *op == '*'
745 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
746 break;
747
Bram Moolenaar4c683752020-04-05 21:38:23 +0200748 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200750 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751 && type2->tt_type != VAR_NUMBER))
752 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200753 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754 return FAIL;
755 }
756 isn = generate_instr_drop(cctx,
757 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
758 if (isn != NULL)
759 isn->isn_arg.op.op_type = EXPR_REM;
760 break;
761 }
762
763 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200764 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 {
766 type_T *type = &t_any;
767
768#ifdef FEAT_FLOAT
769 // float+number and number+float results in float
770 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
771 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
772 type = &t_float;
773#endif
774 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
775 }
776
777 return OK;
778}
779
780/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200781 * Get the instruction to use for comparing "type1" with "type2"
782 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200784 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100785get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786{
787 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788
Bram Moolenaar4c683752020-04-05 21:38:23 +0200789 if (type1 == VAR_UNKNOWN)
790 type1 = VAR_ANY;
791 if (type2 == VAR_UNKNOWN)
792 type2 = VAR_ANY;
793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 if (type1 == type2)
795 {
796 switch (type1)
797 {
798 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
799 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
800 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
801 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
802 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
803 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
804 case VAR_LIST: isntype = ISN_COMPARELIST; break;
805 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
806 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807 default: isntype = ISN_COMPAREANY; break;
808 }
809 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200810 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100812 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 isntype = ISN_COMPAREANY;
814
Bram Moolenaar657137c2021-01-09 15:45:23 +0100815 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816 && (isntype == ISN_COMPAREBOOL
817 || isntype == ISN_COMPARESPECIAL
818 || isntype == ISN_COMPARENR
819 || isntype == ISN_COMPAREFLOAT))
820 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200821 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100822 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200823 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 }
825 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100826 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
828 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100829 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
830 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831 && (type1 == VAR_BLOB || type2 == VAR_BLOB
832 || type1 == VAR_LIST || type2 == VAR_LIST))))
833 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200834 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200836 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200838 return isntype;
839}
840
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200841 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100842check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200843{
844 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
845 return FAIL;
846 return OK;
847}
848
Bram Moolenaara5565e42020-05-09 15:44:01 +0200849/*
850 * Generate an ISN_COMPARE* instruction with a boolean result.
851 */
852 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100853generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200854{
855 isntype_T isntype;
856 isn_T *isn;
857 garray_T *stack = &cctx->ctx_type_stack;
858 vartype_T type1;
859 vartype_T type2;
860
861 RETURN_OK_IF_SKIP(cctx);
862
863 // Get the known type of the two items on the stack. If they are matching
864 // use a type-specific instruction. Otherwise fall back to runtime type
865 // checking.
866 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
867 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100868 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200869 if (isntype == ISN_DROP)
870 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871
872 if ((isn = generate_instr(cctx, isntype)) == NULL)
873 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100874 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875 isn->isn_arg.op.op_ic = ic;
876
877 // takes two arguments, puts one bool back
878 if (stack->ga_len >= 2)
879 {
880 --stack->ga_len;
881 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
882 }
883
884 return OK;
885}
886
887/*
888 * Generate an ISN_2BOOL instruction.
889 */
890 static int
891generate_2BOOL(cctx_T *cctx, int invert)
892{
893 isn_T *isn;
894 garray_T *stack = &cctx->ctx_type_stack;
895
Bram Moolenaar080457c2020-03-03 21:53:32 +0100896 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
898 return FAIL;
899 isn->isn_arg.number = invert;
900
901 // type becomes bool
902 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
903
904 return OK;
905}
906
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200907/*
908 * Generate an ISN_COND2BOOL instruction.
909 */
910 static int
911generate_COND2BOOL(cctx_T *cctx)
912{
913 isn_T *isn;
914 garray_T *stack = &cctx->ctx_type_stack;
915
916 RETURN_OK_IF_SKIP(cctx);
917 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
918 return FAIL;
919
920 // type becomes bool
921 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
922
923 return OK;
924}
925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200927generate_TYPECHECK(
928 cctx_T *cctx,
929 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100930 int offset,
931 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932{
933 isn_T *isn;
934 garray_T *stack = &cctx->ctx_type_stack;
935
Bram Moolenaar080457c2020-03-03 21:53:32 +0100936 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
938 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200939 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100940 isn->isn_arg.type.ct_off = (int8_T)offset;
941 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942
Bram Moolenaar5e654232020-09-16 15:22:00 +0200943 // type becomes expected
944 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945
946 return OK;
947}
948
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100949 static int
950generate_SETTYPE(
951 cctx_T *cctx,
952 type_T *expected)
953{
954 isn_T *isn;
955
956 RETURN_OK_IF_SKIP(cctx);
957 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
958 return FAIL;
959 isn->isn_arg.type.ct_type = alloc_type(expected);
960 return OK;
961}
962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200964 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
965 * used. Return FALSE if the types will never match.
966 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100967 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200968use_typecheck(type_T *actual, type_T *expected)
969{
970 if (actual->tt_type == VAR_ANY
971 || actual->tt_type == VAR_UNKNOWN
972 || (actual->tt_type == VAR_FUNC
973 && (expected->tt_type == VAR_FUNC
974 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100975 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
976 && ((actual->tt_member == &t_void)
977 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200978 return TRUE;
979 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
980 && actual->tt_type == expected->tt_type)
981 // This takes care of a nested list or dict.
982 return use_typecheck(actual->tt_member, expected->tt_member);
983 return FALSE;
984}
985
986/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200987 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200988 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200989 * - "actual" is a type that can be "expected" type: add a runtime check; or
990 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200991 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
992 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200993 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100994 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200995need_type(
996 type_T *actual,
997 type_T *expected,
998 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100999 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001000 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001001 int silent,
1002 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001003{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001004 where_T where;
1005
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001006 if (expected == &t_bool && actual != &t_bool
1007 && (actual->tt_flags & TTFLAG_BOOL_OK))
1008 {
1009 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1010 // boolean is OK but requires a conversion.
1011 generate_2BOOL(cctx, FALSE);
1012 return OK;
1013 }
1014
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001015 where.wt_index = arg_idx;
1016 where.wt_variable = FALSE;
1017 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001018 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001019
1020 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001021 // If it's a constant a runtime check makes no sense.
1022 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001023 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001024 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001025 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001026 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001027
1028 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001029 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001030 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001031}
1032
1033/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001034 * Check that the top of the type stack has a type that can be used as a
1035 * condition. Give an error and return FAIL if not.
1036 */
1037 static int
1038bool_on_stack(cctx_T *cctx)
1039{
1040 garray_T *stack = &cctx->ctx_type_stack;
1041 type_T *type;
1042
1043 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1044 if (type == &t_bool)
1045 return OK;
1046
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001047 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001048 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1049 // This requires a runtime type check.
1050 return generate_COND2BOOL(cctx);
1051
Bram Moolenaar351ead02021-01-16 16:07:01 +01001052 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001053}
1054
1055/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 * Generate an ISN_PUSHNR instruction.
1057 */
1058 static int
1059generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1060{
1061 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001062 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063
Bram Moolenaar080457c2020-03-03 21:53:32 +01001064 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1066 return FAIL;
1067 isn->isn_arg.number = number;
1068
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001069 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001070 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001071 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 return OK;
1073}
1074
1075/*
1076 * Generate an ISN_PUSHBOOL instruction.
1077 */
1078 static int
1079generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1080{
1081 isn_T *isn;
1082
Bram Moolenaar080457c2020-03-03 21:53:32 +01001083 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1085 return FAIL;
1086 isn->isn_arg.number = number;
1087
1088 return OK;
1089}
1090
1091/*
1092 * Generate an ISN_PUSHSPEC instruction.
1093 */
1094 static int
1095generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1096{
1097 isn_T *isn;
1098
Bram Moolenaar080457c2020-03-03 21:53:32 +01001099 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1101 return FAIL;
1102 isn->isn_arg.number = number;
1103
1104 return OK;
1105}
1106
1107#ifdef FEAT_FLOAT
1108/*
1109 * Generate an ISN_PUSHF instruction.
1110 */
1111 static int
1112generate_PUSHF(cctx_T *cctx, float_T fnumber)
1113{
1114 isn_T *isn;
1115
Bram Moolenaar080457c2020-03-03 21:53:32 +01001116 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1118 return FAIL;
1119 isn->isn_arg.fnumber = fnumber;
1120
1121 return OK;
1122}
1123#endif
1124
1125/*
1126 * Generate an ISN_PUSHS instruction.
1127 * Consumes "str".
1128 */
1129 static int
1130generate_PUSHS(cctx_T *cctx, char_u *str)
1131{
1132 isn_T *isn;
1133
Bram Moolenaar508b5612021-01-02 18:17:26 +01001134 if (cctx->ctx_skip == SKIP_YES)
1135 {
1136 vim_free(str);
1137 return OK;
1138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.string = str;
1142
1143 return OK;
1144}
1145
1146/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001147 * Generate an ISN_PUSHCHANNEL instruction.
1148 * Consumes "channel".
1149 */
1150 static int
1151generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001156 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1157 return FAIL;
1158 isn->isn_arg.channel = channel;
1159
1160 return OK;
1161}
1162
1163/*
1164 * Generate an ISN_PUSHJOB instruction.
1165 * Consumes "job".
1166 */
1167 static int
1168generate_PUSHJOB(cctx_T *cctx, job_T *job)
1169{
1170 isn_T *isn;
1171
Bram Moolenaar080457c2020-03-03 21:53:32 +01001172 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001173 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001174 return FAIL;
1175 isn->isn_arg.job = job;
1176
1177 return OK;
1178}
1179
1180/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 * Generate an ISN_PUSHBLOB instruction.
1182 * Consumes "blob".
1183 */
1184 static int
1185generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1186{
1187 isn_T *isn;
1188
Bram Moolenaar080457c2020-03-03 21:53:32 +01001189 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1191 return FAIL;
1192 isn->isn_arg.blob = blob;
1193
1194 return OK;
1195}
1196
1197/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001198 * Generate an ISN_PUSHFUNC instruction with name "name".
1199 * Consumes "name".
1200 */
1201 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001202generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001203{
1204 isn_T *isn;
1205
Bram Moolenaar080457c2020-03-03 21:53:32 +01001206 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001207 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001208 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001209 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001210
1211 return OK;
1212}
1213
1214/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001215 * Generate an ISN_GETITEM instruction with "index".
1216 */
1217 static int
1218generate_GETITEM(cctx_T *cctx, int index)
1219{
1220 isn_T *isn;
1221 garray_T *stack = &cctx->ctx_type_stack;
1222 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1223 type_T *item_type = &t_any;
1224
1225 RETURN_OK_IF_SKIP(cctx);
1226
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001227 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001228 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001229 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001230 emsg(_(e_listreq));
1231 return FAIL;
1232 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001233 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001234 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1235 return FAIL;
1236 isn->isn_arg.number = index;
1237
1238 // add the item type to the type stack
1239 if (ga_grow(stack, 1) == FAIL)
1240 return FAIL;
1241 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1242 ++stack->ga_len;
1243 return OK;
1244}
1245
1246/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001247 * Generate an ISN_SLICE instruction with "count".
1248 */
1249 static int
1250generate_SLICE(cctx_T *cctx, int count)
1251{
1252 isn_T *isn;
1253
1254 RETURN_OK_IF_SKIP(cctx);
1255 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1256 return FAIL;
1257 isn->isn_arg.number = count;
1258 return OK;
1259}
1260
1261/*
1262 * Generate an ISN_CHECKLEN instruction with "min_len".
1263 */
1264 static int
1265generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1266{
1267 isn_T *isn;
1268
1269 RETURN_OK_IF_SKIP(cctx);
1270
1271 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1272 return FAIL;
1273 isn->isn_arg.checklen.cl_min_len = min_len;
1274 isn->isn_arg.checklen.cl_more_OK = more_OK;
1275
1276 return OK;
1277}
1278
1279/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 * Generate an ISN_STORE instruction.
1281 */
1282 static int
1283generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1284{
1285 isn_T *isn;
1286
Bram Moolenaar080457c2020-03-03 21:53:32 +01001287 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1289 return FAIL;
1290 if (name != NULL)
1291 isn->isn_arg.string = vim_strsave(name);
1292 else
1293 isn->isn_arg.number = idx;
1294
1295 return OK;
1296}
1297
1298/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001299 * Generate an ISN_STOREOUTER instruction.
1300 */
1301 static int
1302generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1303{
1304 isn_T *isn;
1305
1306 RETURN_OK_IF_SKIP(cctx);
1307 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.outer.outer_idx = idx;
1310 isn->isn_arg.outer.outer_depth = level;
1311
1312 return OK;
1313}
1314
1315/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1317 */
1318 static int
1319generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1320{
1321 isn_T *isn;
1322
Bram Moolenaar080457c2020-03-03 21:53:32 +01001323 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1325 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001326 isn->isn_arg.storenr.stnr_idx = idx;
1327 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328
1329 return OK;
1330}
1331
1332/*
1333 * Generate an ISN_STOREOPT instruction
1334 */
1335 static int
1336generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1337{
1338 isn_T *isn;
1339
Bram Moolenaar080457c2020-03-03 21:53:32 +01001340 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001341 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 return FAIL;
1343 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1344 isn->isn_arg.storeopt.so_flags = opt_flags;
1345
1346 return OK;
1347}
1348
1349/*
1350 * Generate an ISN_LOAD or similar instruction.
1351 */
1352 static int
1353generate_LOAD(
1354 cctx_T *cctx,
1355 isntype_T isn_type,
1356 int idx,
1357 char_u *name,
1358 type_T *type)
1359{
1360 isn_T *isn;
1361
Bram Moolenaar080457c2020-03-03 21:53:32 +01001362 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1364 return FAIL;
1365 if (name != NULL)
1366 isn->isn_arg.string = vim_strsave(name);
1367 else
1368 isn->isn_arg.number = idx;
1369
1370 return OK;
1371}
1372
1373/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001374 * Generate an ISN_LOADOUTER instruction
1375 */
1376 static int
1377generate_LOADOUTER(
1378 cctx_T *cctx,
1379 int idx,
1380 int nesting,
1381 type_T *type)
1382{
1383 isn_T *isn;
1384
1385 RETURN_OK_IF_SKIP(cctx);
1386 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1387 return FAIL;
1388 isn->isn_arg.outer.outer_idx = idx;
1389 isn->isn_arg.outer.outer_depth = nesting;
1390
1391 return OK;
1392}
1393
1394/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001395 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001396 */
1397 static int
1398generate_LOADV(
1399 cctx_T *cctx,
1400 char_u *name,
1401 int error)
1402{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001403 int di_flags;
1404 int vidx = find_vim_var(name, &di_flags);
1405 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001406
Bram Moolenaar080457c2020-03-03 21:53:32 +01001407 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001408 if (vidx < 0)
1409 {
1410 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001411 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001412 return FAIL;
1413 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001414 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001415
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001416 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001417}
1418
1419/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001420 * Generate an ISN_UNLET instruction.
1421 */
1422 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001423generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001424{
1425 isn_T *isn;
1426
1427 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001428 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001429 return FAIL;
1430 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1431 isn->isn_arg.unlet.ul_forceit = forceit;
1432
1433 return OK;
1434}
1435
1436/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001437 * Generate an ISN_LOCKCONST instruction.
1438 */
1439 static int
1440generate_LOCKCONST(cctx_T *cctx)
1441{
1442 isn_T *isn;
1443
1444 RETURN_OK_IF_SKIP(cctx);
1445 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1446 return FAIL;
1447 return OK;
1448}
1449
1450/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 * Generate an ISN_LOADS instruction.
1452 */
1453 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001454generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001456 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001458 int sid,
1459 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460{
1461 isn_T *isn;
1462
Bram Moolenaar080457c2020-03-03 21:53:32 +01001463 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001464 if (isn_type == ISN_LOADS)
1465 isn = generate_instr_type(cctx, isn_type, type);
1466 else
1467 isn = generate_instr_drop(cctx, isn_type, 1);
1468 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001470 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1471 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472
1473 return OK;
1474}
1475
1476/*
1477 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1478 */
1479 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001480generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 cctx_T *cctx,
1482 isntype_T isn_type,
1483 int sid,
1484 int idx,
1485 type_T *type)
1486{
1487 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001488 scriptref_T *sref;
1489 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490
Bram Moolenaar080457c2020-03-03 21:53:32 +01001491 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 if (isn_type == ISN_LOADSCRIPT)
1493 isn = generate_instr_type(cctx, isn_type, type);
1494 else
1495 isn = generate_instr_drop(cctx, isn_type, 1);
1496 if (isn == NULL)
1497 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001498
1499 // This requires three arguments, which doesn't fit in an instruction, thus
1500 // we need to allocate a struct for this.
1501 sref = ALLOC_ONE(scriptref_T);
1502 if (sref == NULL)
1503 return FAIL;
1504 isn->isn_arg.script.scriptref = sref;
1505 sref->sref_sid = sid;
1506 sref->sref_idx = idx;
1507 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001508 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 return OK;
1510}
1511
1512/*
1513 * Generate an ISN_NEWLIST instruction.
1514 */
1515 static int
1516generate_NEWLIST(cctx_T *cctx, int count)
1517{
1518 isn_T *isn;
1519 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 type_T *type;
1521 type_T *member;
1522
Bram Moolenaar080457c2020-03-03 21:53:32 +01001523 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001524 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1525 return FAIL;
1526 isn->isn_arg.number = count;
1527
Bram Moolenaar127542b2020-08-09 17:22:04 +02001528 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001529 if (count == 0)
1530 member = &t_void;
1531 else
1532 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001533 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1534 cctx->ctx_type_list);
1535 type = get_list_type(member, cctx->ctx_type_list);
1536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 // drop the value types
1538 stack->ga_len -= count;
1539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 // add the list type to the type stack
1541 if (ga_grow(stack, 1) == FAIL)
1542 return FAIL;
1543 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1544 ++stack->ga_len;
1545
1546 return OK;
1547}
1548
1549/*
1550 * Generate an ISN_NEWDICT instruction.
1551 */
1552 static int
1553generate_NEWDICT(cctx_T *cctx, int count)
1554{
1555 isn_T *isn;
1556 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 type_T *type;
1558 type_T *member;
1559
Bram Moolenaar080457c2020-03-03 21:53:32 +01001560 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1562 return FAIL;
1563 isn->isn_arg.number = count;
1564
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001565 if (count == 0)
1566 member = &t_void;
1567 else
1568 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001569 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1570 cctx->ctx_type_list);
1571 type = get_dict_type(member, cctx->ctx_type_list);
1572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 // drop the key and value types
1574 stack->ga_len -= 2 * count;
1575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 // add the dict type to the type stack
1577 if (ga_grow(stack, 1) == FAIL)
1578 return FAIL;
1579 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1580 ++stack->ga_len;
1581
1582 return OK;
1583}
1584
1585/*
1586 * Generate an ISN_FUNCREF instruction.
1587 */
1588 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001589generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590{
1591 isn_T *isn;
1592 garray_T *stack = &cctx->ctx_type_stack;
1593
Bram Moolenaar080457c2020-03-03 21:53:32 +01001594 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1596 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001597 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001598 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001600 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001601 // the nested context, including this one.
1602 if (ufunc->uf_flags & FC_CLOSURE)
1603 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 if (ga_grow(stack, 1) == FAIL)
1606 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001607 ((type_T **)stack->ga_data)[stack->ga_len] =
1608 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 ++stack->ga_len;
1610
1611 return OK;
1612}
1613
1614/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001615 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001616 * "lambda_name" and "func_name" must be in allocated memory and will be
1617 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001618 */
1619 static int
1620generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1621{
1622 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001623
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001624 if (cctx->ctx_skip == SKIP_YES)
1625 {
1626 vim_free(lambda_name);
1627 vim_free(func_name);
1628 return OK;
1629 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001630 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001631 {
1632 vim_free(lambda_name);
1633 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001634 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001635 }
1636 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001637 isn->isn_arg.newfunc.nf_global = func_name;
1638
1639 return OK;
1640}
1641
1642/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001643 * Generate an ISN_DEF instruction: list functions
1644 */
1645 static int
1646generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1647{
1648 isn_T *isn;
1649
1650 RETURN_OK_IF_SKIP(cctx);
1651 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1652 return FAIL;
1653 if (len > 0)
1654 {
1655 isn->isn_arg.string = vim_strnsave(name, len);
1656 if (isn->isn_arg.string == NULL)
1657 return FAIL;
1658 }
1659 return OK;
1660}
1661
1662/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 * Generate an ISN_JUMP instruction.
1664 */
1665 static int
1666generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1667{
1668 isn_T *isn;
1669 garray_T *stack = &cctx->ctx_type_stack;
1670
Bram Moolenaar080457c2020-03-03 21:53:32 +01001671 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1673 return FAIL;
1674 isn->isn_arg.jump.jump_when = when;
1675 isn->isn_arg.jump.jump_where = where;
1676
1677 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1678 --stack->ga_len;
1679
1680 return OK;
1681}
1682
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001683/*
1684 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1685 */
1686 static int
1687generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1688{
1689 isn_T *isn;
1690
1691 RETURN_OK_IF_SKIP(cctx);
1692 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1693 return FAIL;
1694 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1695 // jump_where is set later
1696 return OK;
1697}
1698
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 static int
1700generate_FOR(cctx_T *cctx, int loop_idx)
1701{
1702 isn_T *isn;
1703 garray_T *stack = &cctx->ctx_type_stack;
1704
Bram Moolenaar080457c2020-03-03 21:53:32 +01001705 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1707 return FAIL;
1708 isn->isn_arg.forloop.for_idx = loop_idx;
1709
1710 if (ga_grow(stack, 1) == FAIL)
1711 return FAIL;
1712 // type doesn't matter, will be stored next
1713 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1714 ++stack->ga_len;
1715
1716 return OK;
1717}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001718/*
1719 * Generate an ISN_TRYCONT instruction.
1720 */
1721 static int
1722generate_TRYCONT(cctx_T *cctx, int levels, int where)
1723{
1724 isn_T *isn;
1725
1726 RETURN_OK_IF_SKIP(cctx);
1727 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1728 return FAIL;
1729 isn->isn_arg.trycont.tct_levels = levels;
1730 isn->isn_arg.trycont.tct_where = where;
1731
1732 return OK;
1733}
1734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735
1736/*
1737 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001738 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 * Return FAIL if the number of arguments is wrong.
1740 */
1741 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001742generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743{
1744 isn_T *isn;
1745 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001746 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001747 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001748 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749
Bram Moolenaar080457c2020-03-03 21:53:32 +01001750 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001751 argoff = check_internal_func(func_idx, argcount);
1752 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 return FAIL;
1754
Bram Moolenaar389df252020-07-09 21:20:47 +02001755 if (method_call && argoff > 1)
1756 {
1757 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1758 return FAIL;
1759 isn->isn_arg.shuffle.shfl_item = argcount;
1760 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1761 }
1762
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001763 if (argcount > 0)
1764 {
1765 // Check the types of the arguments.
1766 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001767 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1768 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001769 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001770 if (internal_func_is_map(func_idx))
1771 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001772 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1775 return FAIL;
1776 isn->isn_arg.bfunc.cbf_idx = func_idx;
1777 isn->isn_arg.bfunc.cbf_argcount = argcount;
1778
Bram Moolenaar94738d82020-10-21 14:25:07 +02001779 // Drop the argument types and push the return type.
1780 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 if (ga_grow(stack, 1) == FAIL)
1782 return FAIL;
1783 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001784 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001785 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001787 if (maptype != NULL && maptype->tt_member != NULL
1788 && maptype->tt_member != &t_any)
1789 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001790 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 return OK;
1793}
1794
1795/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001796 * Generate an ISN_LISTAPPEND instruction. Works like add().
1797 * Argument count is already checked.
1798 */
1799 static int
1800generate_LISTAPPEND(cctx_T *cctx)
1801{
1802 garray_T *stack = &cctx->ctx_type_stack;
1803 type_T *list_type;
1804 type_T *item_type;
1805 type_T *expected;
1806
1807 // Caller already checked that list_type is a list.
1808 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1809 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1810 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001811 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001812 return FAIL;
1813
1814 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1815 return FAIL;
1816
1817 --stack->ga_len; // drop the argument
1818 return OK;
1819}
1820
1821/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001822 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1823 * Argument count is already checked.
1824 */
1825 static int
1826generate_BLOBAPPEND(cctx_T *cctx)
1827{
1828 garray_T *stack = &cctx->ctx_type_stack;
1829 type_T *item_type;
1830
1831 // Caller already checked that blob_type is a blob.
1832 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001833 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001834 return FAIL;
1835
1836 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1837 return FAIL;
1838
1839 --stack->ga_len; // drop the argument
1840 return OK;
1841}
1842
1843/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001844 * Return TRUE if "ufunc" should be compiled, taking into account whether
1845 * "profile" indicates profiling is to be done.
1846 */
1847 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001848func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001849{
1850 switch (ufunc->uf_def_status)
1851 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001852 case UF_TO_BE_COMPILED:
1853 return TRUE;
1854
Bram Moolenaarb2049902021-01-24 12:53:53 +01001855 case UF_COMPILED:
1856 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001857#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001858 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1859 + ufunc->uf_dfunc_idx;
1860
1861 return profile ? dfunc->df_instr_prof == NULL
1862 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001863#else
1864 break;
1865#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001866 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001867
1868 case UF_NOT_COMPILED:
1869 case UF_COMPILE_ERROR:
1870 case UF_COMPILING:
1871 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001872 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001873 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001874}
1875
1876/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 * Generate an ISN_DCALL or ISN_UCALL instruction.
1878 * Return FAIL if the number of arguments is wrong.
1879 */
1880 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001881generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882{
1883 isn_T *isn;
1884 garray_T *stack = &cctx->ctx_type_stack;
1885 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001886 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887
Bram Moolenaar080457c2020-03-03 21:53:32 +01001888 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 if (argcount > regular_args && !has_varargs(ufunc))
1890 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001891 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 return FAIL;
1893 }
1894 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1895 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001896 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897 return FAIL;
1898 }
1899
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001900 if (ufunc->uf_def_status != UF_NOT_COMPILED
1901 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001902 {
1903 int i;
1904
1905 for (i = 0; i < argcount; ++i)
1906 {
1907 type_T *expected;
1908 type_T *actual;
1909
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001910 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1911 if (actual == &t_special
1912 && i >= regular_args - ufunc->uf_def_args.ga_len)
1913 {
1914 // assume v:none used for default argument value
1915 continue;
1916 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001917 if (i < regular_args)
1918 {
1919 if (ufunc->uf_arg_types == NULL)
1920 continue;
1921 expected = ufunc->uf_arg_types[i];
1922 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001923 else if (ufunc->uf_va_type == NULL
1924 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001925 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001926 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001927 else
1928 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001929 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001930 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001931 {
1932 arg_type_mismatch(expected, actual, i + 1);
1933 return FAIL;
1934 }
1935 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001936 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001937 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001938 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001939 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001940 }
1941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001943 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001944 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001946 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 {
1948 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1949 isn->isn_arg.dfunc.cdf_argcount = argcount;
1950 }
1951 else
1952 {
1953 // A user function may be deleted and redefined later, can't use the
1954 // ufunc pointer, need to look it up again at runtime.
1955 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1956 isn->isn_arg.ufunc.cuf_argcount = argcount;
1957 }
1958
1959 stack->ga_len -= argcount; // drop the arguments
1960 if (ga_grow(stack, 1) == FAIL)
1961 return FAIL;
1962 // add return value
1963 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1964 ++stack->ga_len;
1965
1966 return OK;
1967}
1968
1969/*
1970 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1971 */
1972 static int
1973generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1974{
1975 isn_T *isn;
1976 garray_T *stack = &cctx->ctx_type_stack;
1977
Bram Moolenaar080457c2020-03-03 21:53:32 +01001978 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1980 return FAIL;
1981 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1982 isn->isn_arg.ufunc.cuf_argcount = argcount;
1983
1984 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001985 if (ga_grow(stack, 1) == FAIL)
1986 return FAIL;
1987 // add return value
1988 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1989 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990
1991 return OK;
1992}
1993
1994/*
1995 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001996 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 */
1998 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001999generate_PCALL(
2000 cctx_T *cctx,
2001 int argcount,
2002 char_u *name,
2003 type_T *type,
2004 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005{
2006 isn_T *isn;
2007 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002008 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009
Bram Moolenaar080457c2020-03-03 21:53:32 +01002010 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002011
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002012 if (type->tt_type == VAR_ANY)
2013 ret_type = &t_any;
2014 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002015 {
2016 if (type->tt_argcount != -1)
2017 {
2018 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2019
2020 if (argcount < type->tt_min_argcount - varargs)
2021 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002022 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002023 return FAIL;
2024 }
2025 if (!varargs && argcount > type->tt_argcount)
2026 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002027 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002028 return FAIL;
2029 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002030 if (type->tt_args != NULL)
2031 {
2032 int i;
2033
2034 for (i = 0; i < argcount; ++i)
2035 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002036 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002037 type_T *actual = ((type_T **)stack->ga_data)[
2038 stack->ga_len + offset];
2039 type_T *expected;
2040
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002041 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002042 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002043 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002044 else if (i >= type->tt_min_argcount
2045 && actual == &t_special)
2046 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002047 else
2048 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002049 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002050 cctx, TRUE, FALSE) == FAIL)
2051 {
2052 arg_type_mismatch(expected, actual, i + 1);
2053 return FAIL;
2054 }
2055 }
2056 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002057 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002058 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002059 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002060 else
2061 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002062 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002063 return FAIL;
2064 }
2065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2067 return FAIL;
2068 isn->isn_arg.pfunc.cpf_top = at_top;
2069 isn->isn_arg.pfunc.cpf_argcount = argcount;
2070
2071 stack->ga_len -= argcount; // drop the arguments
2072
2073 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002074 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002076 // If partial is above the arguments it must be cleared and replaced with
2077 // the return value.
2078 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2079 return FAIL;
2080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 return OK;
2082}
2083
2084/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002085 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 */
2087 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002088generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089{
2090 isn_T *isn;
2091 garray_T *stack = &cctx->ctx_type_stack;
2092 type_T *type;
2093
Bram Moolenaar080457c2020-03-03 21:53:32 +01002094 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002095 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002097 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002099 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002101 if (type->tt_type != VAR_DICT && type != &t_any)
2102 {
2103 emsg(_(e_dictreq));
2104 return FAIL;
2105 }
2106 // change dict type to dict member type
2107 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002108 {
2109 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2110 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112
2113 return OK;
2114}
2115
2116/*
2117 * Generate an ISN_ECHO instruction.
2118 */
2119 static int
2120generate_ECHO(cctx_T *cctx, int with_white, int count)
2121{
2122 isn_T *isn;
2123
Bram Moolenaar080457c2020-03-03 21:53:32 +01002124 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2126 return FAIL;
2127 isn->isn_arg.echo.echo_with_white = with_white;
2128 isn->isn_arg.echo.echo_count = count;
2129
2130 return OK;
2131}
2132
Bram Moolenaarad39c092020-02-26 18:23:43 +01002133/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002134 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002135 */
2136 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002137generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002138{
2139 isn_T *isn;
2140
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002141 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002142 return FAIL;
2143 isn->isn_arg.number = count;
2144
2145 return OK;
2146}
2147
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002148/*
2149 * Generate an ISN_PUT instruction.
2150 */
2151 static int
2152generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2153{
2154 isn_T *isn;
2155
2156 RETURN_OK_IF_SKIP(cctx);
2157 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2158 return FAIL;
2159 isn->isn_arg.put.put_regname = regname;
2160 isn->isn_arg.put.put_lnum = lnum;
2161 return OK;
2162}
2163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 static int
2165generate_EXEC(cctx_T *cctx, char_u *line)
2166{
2167 isn_T *isn;
2168
Bram Moolenaar080457c2020-03-03 21:53:32 +01002169 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2171 return FAIL;
2172 isn->isn_arg.string = vim_strsave(line);
2173 return OK;
2174}
2175
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002176 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002177generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2178{
2179 isn_T *isn;
2180 garray_T *stack = &cctx->ctx_type_stack;
2181
2182 RETURN_OK_IF_SKIP(cctx);
2183 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2184 return FAIL;
2185 isn->isn_arg.string = vim_strsave(line);
2186
2187 if (ga_grow(stack, 1) == FAIL)
2188 return FAIL;
2189 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2190 ++stack->ga_len;
2191
2192 return OK;
2193}
2194
2195 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002196generate_EXECCONCAT(cctx_T *cctx, int count)
2197{
2198 isn_T *isn;
2199
2200 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2201 return FAIL;
2202 isn->isn_arg.number = count;
2203 return OK;
2204}
2205
Bram Moolenaar08597872020-12-10 19:43:40 +01002206/*
2207 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2208 */
2209 static int
2210generate_RANGE(cctx_T *cctx, char_u *range)
2211{
2212 isn_T *isn;
2213 garray_T *stack = &cctx->ctx_type_stack;
2214
2215 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2216 return FAIL;
2217 isn->isn_arg.string = range;
2218
2219 if (ga_grow(stack, 1) == FAIL)
2220 return FAIL;
2221 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2222 ++stack->ga_len;
2223 return OK;
2224}
2225
Bram Moolenaar792f7862020-11-23 08:31:18 +01002226 static int
2227generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2228{
2229 isn_T *isn;
2230
2231 RETURN_OK_IF_SKIP(cctx);
2232 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2233 return FAIL;
2234 isn->isn_arg.unpack.unp_count = var_count;
2235 isn->isn_arg.unpack.unp_semicolon = semicolon;
2236 return OK;
2237}
2238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002240 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002241 */
2242 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002243generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002244{
2245 isn_T *isn;
2246
Bram Moolenaarfa984412021-03-25 22:15:28 +01002247 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002248 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002249 cctx->ctx_has_cmdmod = TRUE;
2250
2251 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002252 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002253 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2254 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2255 return FAIL;
2256 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002257 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002258 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002259 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002260
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002261 return OK;
2262}
2263
2264 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002265generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002266{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002267 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2268 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002269 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002270 return OK;
2271}
2272
Bram Moolenaarfa984412021-03-25 22:15:28 +01002273 static int
2274misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002275{
2276 garray_T *instr = &cctx->ctx_instr;
2277
Bram Moolenaara91a7132021-03-25 21:12:15 +01002278 if (cctx->ctx_has_cmdmod
2279 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2280 == ISN_CMDMOD)
2281 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002282 emsg(_(e_misplaced_command_modifier));
2283 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002284 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002285 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002286}
2287
2288/*
2289 * Get the index of the current instruction.
2290 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2291 */
2292 static int
2293current_instr_idx(cctx_T *cctx)
2294{
2295 garray_T *instr = &cctx->ctx_instr;
2296 int idx = instr->ga_len;
2297
2298 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2299 .isn_type == ISN_CMDMOD)
2300 --idx;
2301#ifdef FEAT_PROFILE
2302 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2303 .isn_type == ISN_PROF_START)
2304 --idx;
2305#endif
2306 return idx;
2307}
2308
Bram Moolenaarf002a412021-01-24 13:34:18 +01002309#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002310 static void
2311may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2312{
2313 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002314 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002315}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002316#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002317
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002318/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002320 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002322 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002323reserve_local(
2324 cctx_T *cctx,
2325 char_u *name,
2326 size_t len,
2327 int isConst,
2328 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 lvar_T *lvar;
2331
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002332 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002334 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002335 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336 }
2337
2338 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002339 return NULL;
2340 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002341 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002343 // Every local variable uses the next entry on the stack. We could re-use
2344 // the last ones when leaving a scope, but then variables used in a closure
2345 // might get overwritten. To keep things simple do not re-use stack
2346 // entries. This is less efficient, but memory is cheap these days.
2347 lvar->lv_idx = cctx->ctx_locals_count++;
2348
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002349 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 lvar->lv_const = isConst;
2351 lvar->lv_type = type;
2352
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002353 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354}
2355
2356/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002357 * Remove local variables above "new_top".
2358 */
2359 static void
2360unwind_locals(cctx_T *cctx, int new_top)
2361{
2362 if (cctx->ctx_locals.ga_len > new_top)
2363 {
2364 int idx;
2365 lvar_T *lvar;
2366
2367 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2368 {
2369 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2370 vim_free(lvar->lv_name);
2371 }
2372 }
2373 cctx->ctx_locals.ga_len = new_top;
2374}
2375
2376/*
2377 * Free all local variables.
2378 */
2379 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002380free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002381{
2382 unwind_locals(cctx, 0);
2383 ga_clear(&cctx->ctx_locals);
2384}
2385
2386/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002387 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2388 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2389 * error if the variable was defined with :const.
2390 */
2391 static int
2392check_item_writable(svar_T *sv, int check_writable, char_u *name)
2393{
2394 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2395 || (check_writable == ASSIGN_FINAL
2396 && sv->sv_const == ASSIGN_CONST))
2397 {
2398 semsg(_(e_readonlyvar), name);
2399 return FAIL;
2400 }
2401 return OK;
2402}
2403
2404/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002406 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 * Returns the index in "sn_var_vals" if found.
2408 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002409 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 */
2411 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002412get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413{
2414 hashtab_T *ht;
2415 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002416 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002417 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 int idx;
2419
Bram Moolenaare3d46852020-08-29 13:39:17 +02002420 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002422 if (sid == current_sctx.sc_sid)
2423 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002424 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002425
2426 if (sav == NULL)
2427 return -2;
2428 idx = sav->sav_var_vals_idx;
2429 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002430 if (check_item_writable(sv, check_writable, name) == FAIL)
2431 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002432 return idx;
2433 }
2434
2435 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 ht = &SCRIPT_VARS(sid);
2437 di = find_var_in_ht(ht, 0, name, TRUE);
2438 if (di == NULL)
2439 return -2;
2440
2441 // Now find the svar_T index in sn_var_vals.
2442 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2443 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002444 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 if (sv->sv_tv == &di->di_tv)
2446 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002447 if (check_item_writable(sv, check_writable, name) == FAIL)
2448 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 return idx;
2450 }
2451 }
2452 return -1;
2453}
2454
2455/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002456 * Find "name" in imported items of the current script or in "cctx" if not
2457 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 */
2459 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002460find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 int idx;
2463
Bram Moolenaare3d46852020-08-29 13:39:17 +02002464 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002465 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 if (cctx != NULL)
2467 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2468 {
2469 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2470 + idx;
2471
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002472 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2473 : STRLEN(import->imp_name) == len
2474 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 return import;
2476 }
2477
Bram Moolenaarefa94442020-08-08 22:16:00 +02002478 return find_imported_in_script(name, len, current_sctx.sc_sid);
2479}
2480
2481 imported_T *
2482find_imported_in_script(char_u *name, size_t len, int sid)
2483{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002484 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002485 int idx;
2486
Bram Moolenaare3d46852020-08-29 13:39:17 +02002487 if (!SCRIPT_ID_VALID(sid))
2488 return NULL;
2489 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2491 {
2492 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2493
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002494 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2495 : STRLEN(import->imp_name) == len
2496 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 return import;
2498 }
2499 return NULL;
2500}
2501
2502/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002503 * Free all imported variables.
2504 */
2505 static void
2506free_imported(cctx_T *cctx)
2507{
2508 int idx;
2509
2510 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2511 {
2512 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2513
2514 vim_free(import->imp_name);
2515 }
2516 ga_clear(&cctx->ctx_imports);
2517}
2518
2519/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002520 * Return a pointer to the next line that isn't empty or only contains a
2521 * comment. Skips over white space.
2522 * Returns NULL if there is none.
2523 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002524 char_u *
2525peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002526{
2527 int lnum = cctx->ctx_lnum;
2528
2529 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2530 {
2531 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002532 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002533
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002534 // ignore NULLs inserted for continuation lines
2535 if (line != NULL)
2536 {
2537 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002538 if (vim9_bad_comment(p))
2539 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002540 if (*p != NUL && !vim9_comment_start(p))
2541 return p;
2542 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002543 }
2544 return NULL;
2545}
2546
2547/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002548 * Called when checking for a following operator at "arg". When the rest of
2549 * the line is empty or only a comment, peek the next line. If there is a next
2550 * line return a pointer to it and set "nextp".
2551 * Otherwise skip over white space.
2552 */
2553 static char_u *
2554may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2555{
2556 char_u *p = skipwhite(arg);
2557
2558 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002559 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002560 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002561 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002562 if (*nextp != NULL)
2563 return *nextp;
2564 }
2565 return p;
2566}
2567
2568/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002569 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002570 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002571 * Returns NULL when at the end.
2572 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002573 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002574next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002575{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002576 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002577
2578 do
2579 {
2580 ++cctx->ctx_lnum;
2581 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002582 {
2583 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002584 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002585 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002586 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002587 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002588 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002589 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002590 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002591 return line;
2592}
2593
2594/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002595 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002596 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002597 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002598 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2599 */
2600 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002601may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002602{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002603 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002604 if (vim9_bad_comment(*arg))
2605 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002606 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002607 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002608 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002609
2610 if (next == NULL)
2611 return FAIL;
2612 *arg = skipwhite(next);
2613 }
2614 return OK;
2615}
2616
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002617/*
2618 * Idem, and give an error when failed.
2619 */
2620 static int
2621may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2622{
2623 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2624 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002625 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002626 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002627 return FAIL;
2628 }
2629 return OK;
2630}
2631
2632
Bram Moolenaara5565e42020-05-09 15:44:01 +02002633// Structure passed between the compile_expr* functions to keep track of
2634// constants that have been parsed but for which no code was produced yet. If
2635// possible expressions on these constants are applied at compile time. If
2636// that is not possible, the code to push the constants needs to be generated
2637// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002638// Using 50 should be more than enough of 5 levels of ().
2639#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002640typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002641 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002642 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002643 int pp_is_const; // all generated code was constants, used for a
2644 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002645} ppconst_T;
2646
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002647static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002648static int compile_expr0(char_u **arg, cctx_T *cctx);
2649static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2650
Bram Moolenaara5565e42020-05-09 15:44:01 +02002651/*
2652 * Generate a PUSH instruction for "tv".
2653 * "tv" will be consumed or cleared.
2654 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2655 */
2656 static int
2657generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2658{
2659 if (tv != NULL)
2660 {
2661 switch (tv->v_type)
2662 {
2663 case VAR_UNKNOWN:
2664 break;
2665 case VAR_BOOL:
2666 generate_PUSHBOOL(cctx, tv->vval.v_number);
2667 break;
2668 case VAR_SPECIAL:
2669 generate_PUSHSPEC(cctx, tv->vval.v_number);
2670 break;
2671 case VAR_NUMBER:
2672 generate_PUSHNR(cctx, tv->vval.v_number);
2673 break;
2674#ifdef FEAT_FLOAT
2675 case VAR_FLOAT:
2676 generate_PUSHF(cctx, tv->vval.v_float);
2677 break;
2678#endif
2679 case VAR_BLOB:
2680 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2681 tv->vval.v_blob = NULL;
2682 break;
2683 case VAR_STRING:
2684 generate_PUSHS(cctx, tv->vval.v_string);
2685 tv->vval.v_string = NULL;
2686 break;
2687 default:
2688 iemsg("constant type not supported");
2689 clear_tv(tv);
2690 return FAIL;
2691 }
2692 tv->v_type = VAR_UNKNOWN;
2693 }
2694 return OK;
2695}
2696
2697/*
2698 * Generate code for any ppconst entries.
2699 */
2700 static int
2701generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2702{
2703 int i;
2704 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002705 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002706
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002707 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002708 for (i = 0; i < ppconst->pp_used; ++i)
2709 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2710 ret = FAIL;
2711 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002712 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002713 return ret;
2714}
2715
2716/*
2717 * Clear ppconst constants. Used when failing.
2718 */
2719 static void
2720clear_ppconst(ppconst_T *ppconst)
2721{
2722 int i;
2723
2724 for (i = 0; i < ppconst->pp_used; ++i)
2725 clear_tv(&ppconst->pp_tv[i]);
2726 ppconst->pp_used = 0;
2727}
2728
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002729/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002730 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002731 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002732 */
2733 static int
2734compile_member(int is_slice, cctx_T *cctx)
2735{
2736 type_T **typep;
2737 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002738 vartype_T vartype;
2739 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002740
Bram Moolenaar261417b2021-05-06 21:04:55 +02002741 // We can index a list, dict and blob. If we don't know the type
2742 // we can use the index value type. If we still don't know use an "ANY"
2743 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002744 typep = ((type_T **)stack->ga_data) + stack->ga_len
2745 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002746 vartype = (*typep)->tt_type;
2747 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002748 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002749 if (*typep == &t_any && idxtype == &t_string)
2750 vartype = VAR_DICT;
2751 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002752 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002753 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002754 return FAIL;
2755 if (is_slice)
2756 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002757 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2758 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002759 FALSE, FALSE) == FAIL)
2760 return FAIL;
2761 }
2762 }
2763
Bram Moolenaar261417b2021-05-06 21:04:55 +02002764 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002765 {
2766 if (is_slice)
2767 {
2768 emsg(_(e_cannot_slice_dictionary));
2769 return FAIL;
2770 }
2771 if ((*typep)->tt_type == VAR_DICT)
2772 {
2773 *typep = (*typep)->tt_member;
2774 if (*typep == &t_unknown)
2775 // empty dict was used
2776 *typep = &t_any;
2777 }
2778 else
2779 {
2780 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2781 FALSE, FALSE) == FAIL)
2782 return FAIL;
2783 *typep = &t_any;
2784 }
2785 if (may_generate_2STRING(-1, cctx) == FAIL)
2786 return FAIL;
2787 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2788 return FAIL;
2789 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002790 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002791 {
2792 *typep = &t_string;
2793 if ((is_slice
2794 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2795 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2796 return FAIL;
2797 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002798 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002799 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002800 if (is_slice)
2801 {
2802 *typep = &t_blob;
2803 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2804 return FAIL;
2805 }
2806 else
2807 {
2808 *typep = &t_number;
2809 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2810 return FAIL;
2811 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002812 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002813 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002814 {
2815 if (is_slice)
2816 {
2817 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002818 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002819 2) == FAIL)
2820 return FAIL;
2821 }
2822 else
2823 {
2824 if ((*typep)->tt_type == VAR_LIST)
2825 {
2826 *typep = (*typep)->tt_member;
2827 if (*typep == &t_unknown)
2828 // empty list was used
2829 *typep = &t_any;
2830 }
2831 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002832 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2833 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002834 return FAIL;
2835 }
2836 }
2837 else
2838 {
2839 emsg(_(e_string_list_dict_or_blob_required));
2840 return FAIL;
2841 }
2842 return OK;
2843}
2844
2845/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002846 * Generate an instruction to load script-local variable "name", without the
2847 * leading "s:".
2848 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 */
2850 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002851compile_load_scriptvar(
2852 cctx_T *cctx,
2853 char_u *name, // variable NUL terminated
2854 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002855 char_u **end, // end of variable
2856 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002858 scriptitem_T *si;
2859 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 imported_T *import;
2861
Bram Moolenaare3d46852020-08-29 13:39:17 +02002862 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2863 return FAIL;
2864 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002865 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002866 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002868 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002869 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2870 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 }
2872 if (idx >= 0)
2873 {
2874 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2875
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002876 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 current_sctx.sc_sid, idx, sv->sv_type);
2878 return OK;
2879 }
2880
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002881 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 if (import != NULL)
2883 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002884 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002885 {
2886 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002887 char_u *exp_name;
2888 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002889 ufunc_T *ufunc;
2890 type_T *type;
2891
2892 // Used "import * as Name", need to lookup the member.
2893 if (*p != '.')
2894 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002895 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002896 return FAIL;
2897 }
2898 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002899 if (VIM_ISWHITE(*p))
2900 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002901 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002902 return FAIL;
2903 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002904
Bram Moolenaar1c991142020-07-04 13:15:31 +02002905 // isolate one name
2906 exp_name = p;
2907 while (eval_isnamec(*p))
2908 ++p;
2909 cc = *p;
2910 *p = NUL;
2911
Bram Moolenaaredba7072021-03-13 21:14:18 +01002912 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2913 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002914 *p = cc;
2915 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002916 *end = p;
2917
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002918 if (idx < 0)
2919 {
2920 if (*p == '(' && ufunc != NULL)
2921 {
2922 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2923 return OK;
2924 }
2925 return FAIL;
2926 }
2927
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002928 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2929 import->imp_sid,
2930 idx,
2931 type);
2932 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002933 else if (import->imp_funcname != NULL)
2934 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002935 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002936 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2937 import->imp_sid,
2938 import->imp_var_vals_idx,
2939 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 return OK;
2941 }
2942
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002943 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002944 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 return FAIL;
2946}
2947
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002948 static int
2949generate_funcref(cctx_T *cctx, char_u *name)
2950{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002951 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002952
2953 if (ufunc == NULL)
2954 return FAIL;
2955
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002956 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002957 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2958 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002959 == FAIL)
2960 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002961 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002962}
2963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964/*
2965 * Compile a variable name into a load instruction.
2966 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002967 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 * When "error" is FALSE do not give an error when not found.
2969 */
2970 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002971compile_load(
2972 char_u **arg,
2973 char_u *end_arg,
2974 cctx_T *cctx,
2975 int is_expr,
2976 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977{
2978 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002979 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002980 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002982 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983
2984 if (*(*arg + 1) == ':')
2985 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002986 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002987 {
2988 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989
Bram Moolenaarfa596382021-04-07 21:58:16 +02002990 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002991 switch (**arg)
2992 {
2993 case 'g': isn_type = ISN_LOADGDICT; break;
2994 case 'w': isn_type = ISN_LOADWDICT; break;
2995 case 't': isn_type = ISN_LOADTDICT; break;
2996 case 'b': isn_type = ISN_LOADBDICT; break;
2997 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002998 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002999 goto theend;
3000 }
3001 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3002 goto theend;
3003 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003004 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 else
3006 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003007 isntype_T isn_type = ISN_DROP;
3008
Bram Moolenaarfa596382021-04-07 21:58:16 +02003009 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003010 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3011 if (name == NULL)
3012 return FAIL;
3013
3014 switch (**arg)
3015 {
3016 case 'v': res = generate_LOADV(cctx, name, error);
3017 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003018 case 's': if (is_expr && ASCII_ISUPPER(*name)
3019 && find_func(name, FALSE, cctx) != NULL)
3020 res = generate_funcref(cctx, name);
3021 else
3022 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003023 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003024 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003025 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003026 {
3027 if (is_expr && ASCII_ISUPPER(*name)
3028 && find_func(name, FALSE, cctx) != NULL)
3029 res = generate_funcref(cctx, name);
3030 else
3031 isn_type = ISN_LOADG;
3032 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003033 else
3034 {
3035 isn_type = ISN_LOADAUTO;
3036 vim_free(name);
3037 name = vim_strnsave(*arg, end - *arg);
3038 if (name == NULL)
3039 return FAIL;
3040 }
3041 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003042 case 'w': isn_type = ISN_LOADW; break;
3043 case 't': isn_type = ISN_LOADT; break;
3044 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003045 default: // cannot happen, just in case
3046 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003047 goto theend;
3048 }
3049 if (isn_type != ISN_DROP)
3050 {
3051 // Global, Buffer-local, Window-local and Tabpage-local
3052 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003053 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003054 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 }
3057 }
3058 else
3059 {
3060 size_t len = end - *arg;
3061 int idx;
3062 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003063 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064
3065 name = vim_strnsave(*arg, end - *arg);
3066 if (name == NULL)
3067 return FAIL;
3068
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003069 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003071 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003072 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 }
3074 else
3075 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003076 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003077
Bram Moolenaar709664c2020-12-12 14:33:41 +01003078 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003080 type = lvar.lv_type;
3081 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003082 if (lvar.lv_from_outer != 0)
3083 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003084 else
3085 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 }
3087 else
3088 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003089 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003090 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003091 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003092 || find_imported(name, 0, cctx) != NULL)
3093 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003094
Bram Moolenaar0f769812020-09-12 18:32:34 +02003095 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003096 // uppercase letter it can be a user defined function.
3097 // generate_funcref() will fail if the function can't be found.
3098 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003099 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 }
3101 }
3102 if (gen_load)
3103 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003104 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003105 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003106 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003107 cctx->ctx_outer_used = TRUE;
3108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 }
3110
3111 *arg = end;
3112
3113theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003114 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003115 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 vim_free(name);
3117 return res;
3118}
3119
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003120 static void
3121clear_instr_ga(garray_T *gap)
3122{
3123 int idx;
3124
3125 for (idx = 0; idx < gap->ga_len; ++idx)
3126 delete_instr(((isn_T *)gap->ga_data) + idx);
3127 ga_clear(gap);
3128}
3129
3130/*
3131 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3132 * Returns FAIL if compilation fails.
3133 */
3134 static int
3135compile_string(isn_T *isn, cctx_T *cctx)
3136{
3137 char_u *s = isn->isn_arg.string;
3138 garray_T save_ga = cctx->ctx_instr;
3139 int expr_res;
3140 int trailing_error;
3141 int instr_count;
3142 isn_T *instr = NULL;
3143
3144 // Temporarily reset the list of instructions so that the jump labels are
3145 // correct.
3146 cctx->ctx_instr.ga_len = 0;
3147 cctx->ctx_instr.ga_maxlen = 0;
3148 cctx->ctx_instr.ga_data = NULL;
3149 expr_res = compile_expr0(&s, cctx);
3150 s = skipwhite(s);
3151 trailing_error = *s != NUL;
3152
Bram Moolenaarff652882021-05-16 15:24:49 +02003153 if (expr_res == FAIL || trailing_error
3154 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003155 {
3156 if (trailing_error)
3157 semsg(_(e_trailing_arg), s);
3158 clear_instr_ga(&cctx->ctx_instr);
3159 cctx->ctx_instr = save_ga;
3160 return FAIL;
3161 }
3162
3163 // Move the generated instructions into the ISN_INSTR instruction, then
3164 // restore the list of instructions.
3165 instr_count = cctx->ctx_instr.ga_len;
3166 instr = cctx->ctx_instr.ga_data;
3167 instr[instr_count].isn_type = ISN_FINISH;
3168
3169 cctx->ctx_instr = save_ga;
3170 vim_free(isn->isn_arg.string);
3171 isn->isn_type = ISN_INSTR;
3172 isn->isn_arg.instr = instr;
3173 return OK;
3174}
3175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176/*
3177 * Compile the argument expressions.
3178 * "arg" points to just after the "(" and is advanced to after the ")"
3179 */
3180 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003181compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003183 char_u *p = *arg;
3184 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003185 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003186 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187
Bram Moolenaare6085c52020-04-12 20:19:16 +02003188 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003190 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3191 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003192 if (*p == ')')
3193 {
3194 *arg = p + 1;
3195 return OK;
3196 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003197 if (must_end)
3198 {
3199 semsg(_(e_missing_comma_before_argument_str), p);
3200 return FAIL;
3201 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003202
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003203 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003204 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 return FAIL;
3206 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003207
Bram Moolenaarff652882021-05-16 15:24:49 +02003208 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003209 && cctx->ctx_instr.ga_len == instr_count + 1)
3210 {
3211 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3212
3213 // {skip} argument of searchpair() can be compiled if not empty
3214 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3215 compile_string(isn, cctx);
3216 }
3217
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003218 if (*p != ',' && *skipwhite(p) == ',')
3219 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003220 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003221 p = skipwhite(p);
3222 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003224 {
3225 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003226 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003227 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003228 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003229 else
3230 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003231 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003232 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003234failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003235 emsg(_(e_missing_close));
3236 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237}
3238
3239/*
3240 * Compile a function call: name(arg1, arg2)
3241 * "arg" points to "name", "arg + varlen" to the "(".
3242 * "argcount_init" is 1 for "value->method()"
3243 * Instructions:
3244 * EVAL arg1
3245 * EVAL arg2
3246 * BCALL / DCALL / UCALL
3247 */
3248 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003249compile_call(
3250 char_u **arg,
3251 size_t varlen,
3252 cctx_T *cctx,
3253 ppconst_T *ppconst,
3254 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255{
3256 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003257 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 int argcount = argcount_init;
3259 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003260 char_u fname_buf[FLEN_FIXED + 1];
3261 char_u *tofree = NULL;
3262 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003263 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003264 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003265 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003266 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267
Bram Moolenaara5565e42020-05-09 15:44:01 +02003268 // we can evaluate "has('name')" at compile time
3269 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3270 {
3271 char_u *s = skipwhite(*arg + varlen + 1);
3272 typval_T argvars[2];
3273
3274 argvars[0].v_type = VAR_UNKNOWN;
3275 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003276 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003277 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003278 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003279 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003280 if (*s == ')' && argvars[0].v_type == VAR_STRING
3281 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003282 {
3283 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3284
3285 *arg = s + 1;
3286 argvars[1].v_type = VAR_UNKNOWN;
3287 tv->v_type = VAR_NUMBER;
3288 tv->vval.v_number = 0;
3289 f_has(argvars, tv);
3290 clear_tv(&argvars[0]);
3291 ++ppconst->pp_used;
3292 return OK;
3293 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003294 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003295 }
3296
3297 if (generate_ppconst(cctx, ppconst) == FAIL)
3298 return FAIL;
3299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 if (varlen >= sizeof(namebuf))
3301 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003302 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 return FAIL;
3304 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003305 vim_strncpy(namebuf, *arg, varlen);
3306 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003308 // We handle the "skip" argument of searchpair() and searchpairpos()
3309 // differently.
3310 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3311 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3312 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3313 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003316 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003317 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318
Bram Moolenaar03290b82020-12-19 16:30:44 +01003319 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003320 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 {
3322 int idx;
3323
3324 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003325 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003327 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003328 if (STRCMP(name, "flatten") == 0)
3329 {
3330 emsg(_(e_cannot_use_flatten_in_vim9_script));
3331 goto theend;
3332 }
3333
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003334 if (STRCMP(name, "add") == 0 && argcount == 2)
3335 {
3336 garray_T *stack = &cctx->ctx_type_stack;
3337 type_T *type = ((type_T **)stack->ga_data)[
3338 stack->ga_len - 2];
3339
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003340 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003341 if (type->tt_type == VAR_LIST)
3342 {
3343 // inline "add(list, item)" so that the type can be checked
3344 res = generate_LISTAPPEND(cctx);
3345 idx = -1;
3346 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003347 else if (type->tt_type == VAR_BLOB)
3348 {
3349 // inline "add(blob, nr)" so that the type can be checked
3350 res = generate_BLOBAPPEND(cctx);
3351 idx = -1;
3352 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003353 }
3354
3355 if (idx >= 0)
3356 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3357 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003358 else
3359 semsg(_(e_unknownfunc), namebuf);
3360 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 }
3362
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003363 // An argument or local variable can be a function reference, this
3364 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003365 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003366 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003367 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003368 // If we can find the function by name generate the right call.
3369 // Skip global functions here, a local funcref takes precedence.
3370 ufunc = find_func(name, FALSE, cctx);
3371 if (ufunc != NULL && !func_is_global(ufunc))
3372 {
3373 res = generate_CALL(cctx, ufunc, argcount);
3374 goto theend;
3375 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377
3378 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003379 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003380 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003382 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003383 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003384 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003385 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003386 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003387
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003388 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003389 goto theend;
3390 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391
Bram Moolenaar0f769812020-09-12 18:32:34 +02003392 // If we can find a global function by name generate the right call.
3393 if (ufunc != NULL)
3394 {
3395 res = generate_CALL(cctx, ufunc, argcount);
3396 goto theend;
3397 }
3398
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003399 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003400 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003401 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003402 res = generate_UCALL(cctx, name, argcount);
3403 else
3404 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003405
3406theend:
3407 vim_free(tofree);
3408 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409}
3410
3411// like NAMESPACE_CHAR but with 'a' and 'l'.
3412#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3413
3414/*
3415 * Find the end of a variable or function name. Unlike find_name_end() this
3416 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003417 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 * Return a pointer to just after the name. Equal to "arg" if there is no
3419 * valid name.
3420 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003421 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003422to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423{
3424 char_u *p;
3425
3426 // Quick check for valid starting character.
3427 if (!eval_isnamec1(*arg))
3428 return arg;
3429
3430 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3431 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3432 // and can be used in slice "[n:]".
3433 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003434 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3436 break;
3437 return p;
3438}
3439
3440/*
3441 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003442 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 */
3444 char_u *
3445to_name_const_end(char_u *arg)
3446{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003447 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 typval_T rettv;
3449
3450 if (p == arg && *arg == '[')
3451 {
3452
3453 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003454 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 p = arg;
3456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 return p;
3458}
3459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460/*
3461 * parse a list: [expr, expr]
3462 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003463 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 */
3465 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003466compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467{
3468 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003469 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003471 int is_const;
3472 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003474 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003476 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003477 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003478 semsg(_(e_list_end), *arg);
3479 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003480 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003481 if (*p == ',')
3482 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003483 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003484 return FAIL;
3485 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003486 if (*p == ']')
3487 {
3488 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003489 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003490 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003491 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003492 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003493 if (!is_const)
3494 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 ++count;
3496 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003497 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003499 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3500 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003501 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003502 return FAIL;
3503 }
3504 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003505 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 p = skipwhite(p);
3507 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003508 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003510 ppconst->pp_is_const = is_all_const;
3511 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512}
3513
3514/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003515 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003516 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003517 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 */
3519 static int
3520compile_lambda(char_u **arg, cctx_T *cctx)
3521{
Bram Moolenaare462f522020-12-27 14:43:30 +01003522 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 typval_T rettv;
3524 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003525 evalarg_T evalarg;
3526
3527 CLEAR_FIELD(evalarg);
3528 evalarg.eval_flags = EVAL_EVALUATE;
3529 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530
3531 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003532 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3533 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003534 {
3535 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003536 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003537 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003538
Bram Moolenaar65c44152020-12-24 15:14:01 +01003539 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003541 ++ufunc->uf_refcount;
3542 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543
Bram Moolenaar65c44152020-12-24 15:14:01 +01003544 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003545 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003547 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3548 // points into it. Point to the original line to avoid a dangling pointer.
3549 if (evalarg.eval_tofree_cmdline != NULL)
3550 {
3551 size_t off = *arg - evalarg.eval_tofree_cmdline;
3552
3553 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3554 + off;
3555 }
3556
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003557 clear_evalarg(&evalarg, NULL);
3558
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003559 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003560 {
3561 // The return type will now be known.
3562 set_function_type(ufunc);
3563
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003564 // The function reference count will be 1. When the ISN_FUNCREF
3565 // instruction is deleted the reference count is decremented and the
3566 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003567 return generate_FUNCREF(cctx, ufunc);
3568 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003569
3570 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 return FAIL;
3572}
3573
3574/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003575 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003577 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 */
3579 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003580compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581{
3582 garray_T *instr = &cctx->ctx_instr;
3583 int count = 0;
3584 dict_T *d = dict_alloc();
3585 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003586 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003587 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003588 int is_const;
3589 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590
3591 if (d == NULL)
3592 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003593 if (generate_ppconst(cctx, ppconst) == FAIL)
3594 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003595 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003597 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598
Bram Moolenaar23c55272020-06-21 16:58:13 +02003599 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003600 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003601 *arg = NULL;
3602 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003603 }
3604
3605 if (**arg == '}')
3606 break;
3607
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003608 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003610 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003612 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003613 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003614 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003617 if (isn->isn_type == ISN_PUSHNR)
3618 {
3619 char buf[NUMBUFLEN];
3620
3621 // Convert to string at compile time.
3622 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3623 isn->isn_type = ISN_PUSHS;
3624 isn->isn_arg.string = vim_strsave((char_u *)buf);
3625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 if (isn->isn_type == ISN_PUSHS)
3627 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003628 else if (may_generate_2STRING(-1, cctx) == FAIL)
3629 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003630 *arg = skipwhite(*arg);
3631 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003632 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003633 emsg(_(e_missing_matching_bracket_after_dict_key));
3634 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003635 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003636 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003638 else
3639 {
3640 // {"name": value},
3641 // {'name': value},
3642 // {name: value} use "name" as a literal key
3643 key = get_literal_key(arg);
3644 if (key == NULL)
3645 return FAIL;
3646 if (generate_PUSHS(cctx, key) == FAIL)
3647 return FAIL;
3648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649
3650 // Check for duplicate keys, if using string keys.
3651 if (key != NULL)
3652 {
3653 item = dict_find(d, key, -1);
3654 if (item != NULL)
3655 {
3656 semsg(_(e_duplicate_key), key);
3657 goto failret;
3658 }
3659 item = dictitem_alloc(key);
3660 if (item != NULL)
3661 {
3662 item->di_tv.v_type = VAR_UNKNOWN;
3663 item->di_tv.v_lock = 0;
3664 if (dict_add(d, item) == FAIL)
3665 dictitem_free(item);
3666 }
3667 }
3668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 if (**arg != ':')
3670 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003671 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003672 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003673 else
3674 semsg(_(e_missing_dict_colon), *arg);
3675 return FAIL;
3676 }
3677 whitep = *arg + 1;
3678 if (!IS_WHITE_OR_NUL(*whitep))
3679 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003680 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 return FAIL;
3682 }
3683
Bram Moolenaar23c55272020-06-21 16:58:13 +02003684 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003685 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003686 *arg = NULL;
3687 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003688 }
3689
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003690 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003692 if (!is_const)
3693 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 ++count;
3695
Bram Moolenaar2c330432020-04-13 14:41:35 +02003696 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003697 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003698 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003699 *arg = NULL;
3700 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003701 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 if (**arg == '}')
3703 break;
3704 if (**arg != ',')
3705 {
3706 semsg(_(e_missing_dict_comma), *arg);
3707 goto failret;
3708 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003709 if (IS_WHITE_OR_NUL(*whitep))
3710 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003711 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003712 return FAIL;
3713 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003714 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003715 if (!IS_WHITE_OR_NUL(*whitep))
3716 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003717 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003718 return FAIL;
3719 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003720 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 }
3722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 *arg = *arg + 1;
3724
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003725 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003726 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003727 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003728 *arg += STRLEN(*arg);
3729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003731 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 return generate_NEWDICT(cctx, count);
3733
3734failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003735 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003736 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003737 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003738 *arg = (char_u *)"";
3739 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 dict_unref(d);
3741 return FAIL;
3742}
3743
3744/*
3745 * Compile "&option".
3746 */
3747 static int
3748compile_get_option(char_u **arg, cctx_T *cctx)
3749{
3750 typval_T rettv;
3751 char_u *start = *arg;
3752 int ret;
3753
3754 // parse the option and get the current value to get the type.
3755 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003756 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 if (ret == OK)
3758 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003759 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003760 char_u *name = vim_strnsave(start, *arg - start);
3761 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3762 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763
3764 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3765 vim_free(name);
3766 }
3767 clear_tv(&rettv);
3768
3769 return ret;
3770}
3771
3772/*
3773 * Compile "$VAR".
3774 */
3775 static int
3776compile_get_env(char_u **arg, cctx_T *cctx)
3777{
3778 char_u *start = *arg;
3779 int len;
3780 int ret;
3781 char_u *name;
3782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 ++*arg;
3784 len = get_env_len(arg);
3785 if (len == 0)
3786 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003787 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 return FAIL;
3789 }
3790
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003791 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 name = vim_strnsave(start, len + 1);
3793 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3794 vim_free(name);
3795 return ret;
3796}
3797
3798/*
3799 * Compile "@r".
3800 */
3801 static int
3802compile_get_register(char_u **arg, cctx_T *cctx)
3803{
3804 int ret;
3805
3806 ++*arg;
3807 if (**arg == NUL)
3808 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003809 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 return FAIL;
3811 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003812 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 {
3814 emsg_invreg(**arg);
3815 return FAIL;
3816 }
3817 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3818 ++*arg;
3819 return ret;
3820}
3821
3822/*
3823 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003824 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 */
3826 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003827apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003829 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830
3831 // this works from end to start
3832 while (p > start)
3833 {
3834 --p;
3835 if (*p == '-' || *p == '+')
3836 {
3837 // only '-' has an effect, for '+' we only check the type
3838#ifdef FEAT_FLOAT
3839 if (rettv->v_type == VAR_FLOAT)
3840 {
3841 if (*p == '-')
3842 rettv->vval.v_float = -rettv->vval.v_float;
3843 }
3844 else
3845#endif
3846 {
3847 varnumber_T val;
3848 int error = FALSE;
3849
3850 // tv_get_number_chk() accepts a string, but we don't want that
3851 // here
3852 if (check_not_string(rettv) == FAIL)
3853 return FAIL;
3854 val = tv_get_number_chk(rettv, &error);
3855 clear_tv(rettv);
3856 if (error)
3857 return FAIL;
3858 if (*p == '-')
3859 val = -val;
3860 rettv->v_type = VAR_NUMBER;
3861 rettv->vval.v_number = val;
3862 }
3863 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003864 else if (numeric_only)
3865 {
3866 ++p;
3867 break;
3868 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003869 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 {
3871 int v = tv2bool(rettv);
3872
3873 // '!' is permissive in the type.
3874 clear_tv(rettv);
3875 rettv->v_type = VAR_BOOL;
3876 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3877 }
3878 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003879 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 return OK;
3881}
3882
3883/*
3884 * Recognize v: variables that are constants and set "rettv".
3885 */
3886 static void
3887get_vim_constant(char_u **arg, typval_T *rettv)
3888{
3889 if (STRNCMP(*arg, "v:true", 6) == 0)
3890 {
3891 rettv->v_type = VAR_BOOL;
3892 rettv->vval.v_number = VVAL_TRUE;
3893 *arg += 6;
3894 }
3895 else if (STRNCMP(*arg, "v:false", 7) == 0)
3896 {
3897 rettv->v_type = VAR_BOOL;
3898 rettv->vval.v_number = VVAL_FALSE;
3899 *arg += 7;
3900 }
3901 else if (STRNCMP(*arg, "v:null", 6) == 0)
3902 {
3903 rettv->v_type = VAR_SPECIAL;
3904 rettv->vval.v_number = VVAL_NULL;
3905 *arg += 6;
3906 }
3907 else if (STRNCMP(*arg, "v:none", 6) == 0)
3908 {
3909 rettv->v_type = VAR_SPECIAL;
3910 rettv->vval.v_number = VVAL_NONE;
3911 *arg += 6;
3912 }
3913}
3914
Bram Moolenaar657137c2021-01-09 15:45:23 +01003915 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003916get_compare_type(char_u *p, int *len, int *type_is)
3917{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003918 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003919 int i;
3920
3921 switch (p[0])
3922 {
3923 case '=': if (p[1] == '=')
3924 type = EXPR_EQUAL;
3925 else if (p[1] == '~')
3926 type = EXPR_MATCH;
3927 break;
3928 case '!': if (p[1] == '=')
3929 type = EXPR_NEQUAL;
3930 else if (p[1] == '~')
3931 type = EXPR_NOMATCH;
3932 break;
3933 case '>': if (p[1] != '=')
3934 {
3935 type = EXPR_GREATER;
3936 *len = 1;
3937 }
3938 else
3939 type = EXPR_GEQUAL;
3940 break;
3941 case '<': if (p[1] != '=')
3942 {
3943 type = EXPR_SMALLER;
3944 *len = 1;
3945 }
3946 else
3947 type = EXPR_SEQUAL;
3948 break;
3949 case 'i': if (p[1] == 's')
3950 {
3951 // "is" and "isnot"; but not a prefix of a name
3952 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3953 *len = 5;
3954 i = p[*len];
3955 if (!isalnum(i) && i != '_')
3956 {
3957 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3958 *type_is = TRUE;
3959 }
3960 }
3961 break;
3962 }
3963 return type;
3964}
3965
3966/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003967 * Skip over an expression, ignoring most errors.
3968 */
3969 static void
3970skip_expr_cctx(char_u **arg, cctx_T *cctx)
3971{
3972 evalarg_T evalarg;
3973
3974 CLEAR_FIELD(evalarg);
3975 evalarg.eval_cctx = cctx;
3976 skip_expr(arg, &evalarg);
3977}
3978
3979/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003981 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 */
3983 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003984compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003986 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987
3988 // this works from end to start
3989 while (p > start)
3990 {
3991 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003992 while (VIM_ISWHITE(*p))
3993 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 if (*p == '-' || *p == '+')
3995 {
3996 int negate = *p == '-';
3997 isn_T *isn;
3998
3999 // TODO: check type
4000 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4001 {
4002 --p;
4003 if (*p == '-')
4004 negate = !negate;
4005 }
4006 // only '-' has an effect, for '+' we only check the type
4007 if (negate)
4008 isn = generate_instr(cctx, ISN_NEGATENR);
4009 else
4010 isn = generate_instr(cctx, ISN_CHECKNR);
4011 if (isn == NULL)
4012 return FAIL;
4013 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004014 else if (numeric_only)
4015 {
4016 ++p;
4017 break;
4018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 else
4020 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004021 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004023 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004025 if (p[-1] == '!')
4026 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 }
4029 if (generate_2BOOL(cctx, invert) == FAIL)
4030 return FAIL;
4031 }
4032 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004033 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 return OK;
4035}
4036
4037/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004038 * Compile "(expression)": recursive!
4039 * Return FAIL/OK.
4040 */
4041 static int
4042compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4043{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004044 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004045 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004046
Bram Moolenaar24156692021-01-14 20:35:49 +01004047 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4048 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004049 if (ppconst->pp_used <= PPSIZE - 10)
4050 {
4051 ret = compile_expr1(arg, cctx, ppconst);
4052 }
4053 else
4054 {
4055 // Not enough space in ppconst, flush constants.
4056 if (generate_ppconst(cctx, ppconst) == FAIL)
4057 return FAIL;
4058 ret = compile_expr0(arg, cctx);
4059 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004060 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4061 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004062 if (**arg == ')')
4063 ++*arg;
4064 else if (ret == OK)
4065 {
4066 emsg(_(e_missing_close));
4067 ret = FAIL;
4068 }
4069 return ret;
4070}
4071
4072/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004074 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 */
4076 static int
4077compile_subscript(
4078 char_u **arg,
4079 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004080 char_u *start_leader,
4081 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004082 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004084 char_u *name_start = *end_leader;
4085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 for (;;)
4087 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004088 char_u *p = skipwhite(*arg);
4089
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004090 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004091 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004092 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004093
4094 // If a following line starts with "->{" or "->X" advance to that
4095 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004096 // Also if a following line starts with ".x".
4097 if (next != NULL &&
4098 ((next[0] == '-' && next[1] == '>'
4099 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004100 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004101 {
4102 next = next_line_from_context(cctx, TRUE);
4103 if (next == NULL)
4104 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004105 *arg = next;
4106 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004107 }
4108 }
4109
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004110 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004111 // not a function call.
4112 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004114 garray_T *stack = &cctx->ctx_type_stack;
4115 type_T *type;
4116 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004118 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004119 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004120 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004123 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4124
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004125 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004126 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004128 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 return FAIL;
4130 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004131 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004133 char_u *pstart = p;
4134
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004135 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004136 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004137 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 // something->method()
4140 // Apply the '!', '-' and '+' first:
4141 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004142 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004145 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004146 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004147 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004148 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004149 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004150 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004151 garray_T *stack = &cctx->ctx_type_stack;
4152 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004153 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004154 int expr_isn_start = cctx->ctx_instr.ga_len;
4155 int expr_isn_end;
4156 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004157
4158 // Funcref call: list->(Refs[2])(arg)
4159 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004160 //
4161 // Fist compile the function expression.
4162 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004163 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004164
4165 // Remember the next instruction index, where the instructions
4166 // for arguments are being written.
4167 expr_isn_end = cctx->ctx_instr.ga_len;
4168
4169 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004170 if (**arg != '(')
4171 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004172 if (*skipwhite(*arg) == '(')
4173 emsg(_(e_nowhitespace));
4174 else
4175 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004176 return FAIL;
4177 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004178 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004179 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004180 return FAIL;
4181
Bram Moolenaar2927c072021-04-05 19:41:21 +02004182 // Move the instructions for the arguments to before the
4183 // instructions of the expression and move the type of the
4184 // expression after the argument types. This is what ISN_PCALL
4185 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004186 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004187 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4188 if (arg_isn_count > 0)
4189 {
4190 int expr_isn_count = expr_isn_end - expr_isn_start;
4191 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4192
4193 if (isn == NULL)
4194 return FAIL;
4195 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4196 + expr_isn_start,
4197 sizeof(isn_T) * expr_isn_count);
4198 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4199 + expr_isn_start,
4200 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4201 sizeof(isn_T) * arg_isn_count);
4202 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4203 + expr_isn_start + arg_isn_count,
4204 isn, sizeof(isn_T) * expr_isn_count);
4205 vim_free(isn);
4206
4207 type = ((type_T **)stack->ga_data)[type_idx_start];
4208 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4209 ((type_T **)stack->ga_data) + type_idx_start + 1,
4210 sizeof(type_T *)
4211 * (stack->ga_len - type_idx_start - 1));
4212 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4213 }
4214
Bram Moolenaar7e368202020-12-25 21:56:57 +01004215 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4216 if (generate_PCALL(cctx, argcount,
4217 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 return FAIL;
4219 }
4220 else
4221 {
4222 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004223 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004224 if (!eval_isnamec1(*p))
4225 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004226 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004227 return FAIL;
4228 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004229 if (ASCII_ISALPHA(*p) && p[1] == ':')
4230 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004231 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 ;
4233 if (*p != '(')
4234 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004235 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 return FAIL;
4237 }
4238 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004239 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 return FAIL;
4241 }
4242 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004243 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004245 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004246
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004247 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004248 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004249 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004250 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004251 // TODO: more arguments
4252 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004253 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004254 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004255 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004256
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004257 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004258 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004259 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004260 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004261 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004262 // missing first index is equal to zero
4263 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004264 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004265 else
4266 {
4267 if (compile_expr0(arg, cctx) == FAIL)
4268 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004269 if (**arg == ':')
4270 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004271 semsg(_(e_white_space_required_before_and_after_str_at_str),
4272 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004273 return FAIL;
4274 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004275 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004276 return FAIL;
4277 *arg = skipwhite(*arg);
4278 }
4279 if (**arg == ':')
4280 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004281 is_slice = TRUE;
4282 ++*arg;
4283 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4284 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004285 semsg(_(e_white_space_required_before_and_after_str_at_str),
4286 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004287 return FAIL;
4288 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004289 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004290 return FAIL;
4291 if (**arg == ']')
4292 // missing second index is equal to end of string
4293 generate_PUSHNR(cctx, -1);
4294 else
4295 {
4296 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004297 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004298 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004299 return FAIL;
4300 *arg = skipwhite(*arg);
4301 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303
4304 if (**arg != ']')
4305 {
4306 emsg(_(e_missbrac));
4307 return FAIL;
4308 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004309 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310
Bram Moolenaare42939a2021-04-05 17:11:17 +02004311 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004312 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004314 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004316 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004317 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004318 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004319 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004320
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004321 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004322 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004323 {
4324 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004325 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004326 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004327 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004328 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 while (eval_isnamec(*p))
4330 MB_PTR_ADV(p);
4331 if (p == *arg)
4332 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004333 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 return FAIL;
4335 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004336 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 return FAIL;
4338 *arg = p;
4339 }
4340 else
4341 break;
4342 }
4343
4344 // TODO - see handle_subscript():
4345 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4346 // Don't do this when "Func" is already a partial that was bound
4347 // explicitly (pt_auto is FALSE).
4348
4349 return OK;
4350}
4351
4352/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004353 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4354 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004356 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004357 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004358 *
4359 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 */
4361
4362/*
4363 * number number constant
4364 * 0zFFFFFFFF Blob constant
4365 * "string" string constant
4366 * 'string' literal string constant
4367 * &option-name option value
4368 * @r register contents
4369 * identifier variable value
4370 * function() function call
4371 * $VAR environment variable
4372 * (expression) nested expression
4373 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004374 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 *
4376 * Also handle:
4377 * ! in front logical NOT
4378 * - in front unary minus
4379 * + in front unary plus (ignored)
4380 * trailing (arg) funcref/partial call
4381 * trailing [] subscript in String or List
4382 * trailing .name entry in Dictionary
4383 * trailing ->name() method call
4384 */
4385 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004386compile_expr7(
4387 char_u **arg,
4388 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004389 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 char_u *start_leader, *end_leader;
4392 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004393 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004394 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004396 ppconst->pp_is_const = FALSE;
4397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 /*
4399 * Skip '!', '-' and '+' characters. They are handled later.
4400 */
4401 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004402 if (eval_leader(arg, TRUE) == FAIL)
4403 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 end_leader = *arg;
4405
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004406 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 switch (**arg)
4408 {
4409 /*
4410 * Number constant.
4411 */
4412 case '0': // also for blob starting with 0z
4413 case '1':
4414 case '2':
4415 case '3':
4416 case '4':
4417 case '5':
4418 case '6':
4419 case '7':
4420 case '8':
4421 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004422 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004424 // Apply "-" and "+" just before the number now, right to
4425 // left. Matters especially when "->" follows. Stops at
4426 // '!'.
4427 if (apply_leader(rettv, TRUE,
4428 start_leader, &end_leader) == FAIL)
4429 {
4430 clear_tv(rettv);
4431 return FAIL;
4432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 break;
4434
4435 /*
4436 * String constant: "string".
4437 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004438 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 return FAIL;
4440 break;
4441
4442 /*
4443 * Literal string constant: 'str''ing'.
4444 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004445 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 return FAIL;
4447 break;
4448
4449 /*
4450 * Constant Vim variable.
4451 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004452 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 ret = NOTDONE;
4454 break;
4455
4456 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004457 * "true" constant
4458 */
4459 case 't': if (STRNCMP(*arg, "true", 4) == 0
4460 && !eval_isnamec((*arg)[4]))
4461 {
4462 *arg += 4;
4463 rettv->v_type = VAR_BOOL;
4464 rettv->vval.v_number = VVAL_TRUE;
4465 }
4466 else
4467 ret = NOTDONE;
4468 break;
4469
4470 /*
4471 * "false" constant
4472 */
4473 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4474 && !eval_isnamec((*arg)[5]))
4475 {
4476 *arg += 5;
4477 rettv->v_type = VAR_BOOL;
4478 rettv->vval.v_number = VVAL_FALSE;
4479 }
4480 else
4481 ret = NOTDONE;
4482 break;
4483
4484 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004485 * "null" constant
4486 */
4487 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004488 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004489 {
4490 *arg += 4;
4491 rettv->v_type = VAR_SPECIAL;
4492 rettv->vval.v_number = VVAL_NULL;
4493 }
4494 else
4495 ret = NOTDONE;
4496 break;
4497
4498 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 * List: [expr, expr]
4500 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004501 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4502 return FAIL;
4503 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504 break;
4505
4506 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 * Dictionary: {'key': val, 'key': val}
4508 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004509 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4510 return FAIL;
4511 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 break;
4513
4514 /*
4515 * Option value: &name
4516 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004517 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4518 return FAIL;
4519 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 break;
4521
4522 /*
4523 * Environment variable: $VAR.
4524 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004525 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4526 return FAIL;
4527 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 break;
4529
4530 /*
4531 * Register contents: @r.
4532 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004533 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4534 return FAIL;
4535 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 break;
4537 /*
4538 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004539 * lambda: (arg, arg) => expr
4540 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004542 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4543 ret = compile_lambda(arg, cctx);
4544 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004545 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 break;
4547
4548 default: ret = NOTDONE;
4549 break;
4550 }
4551 if (ret == FAIL)
4552 return FAIL;
4553
Bram Moolenaar1c747212020-05-09 18:28:34 +02004554 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004556 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004557 clear_tv(rettv);
4558 else
4559 // A constant expression can possibly be handled compile time,
4560 // return the value instead of generating code.
4561 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 }
4563 else if (ret == NOTDONE)
4564 {
4565 char_u *p;
4566 int r;
4567
4568 if (!eval_isnamec1(**arg))
4569 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004570 if (!vim9_bad_comment(*arg))
4571 {
4572 if (ends_excmd(*skipwhite(*arg)))
4573 semsg(_(e_empty_expression_str), *arg);
4574 else
4575 semsg(_(e_name_expected_str), *arg);
4576 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 return FAIL;
4578 }
4579
4580 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004581 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004582 if (p - *arg == (size_t)1 && **arg == '_')
4583 {
4584 emsg(_(e_cannot_use_underscore_here));
4585 return FAIL;
4586 }
4587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004589 {
4590 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4591 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004593 {
4594 if (generate_ppconst(cctx, ppconst) == FAIL)
4595 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004596 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 if (r == FAIL)
4599 return FAIL;
4600 }
4601
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004602 // Handle following "[]", ".member", etc.
4603 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004604 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004605 ppconst) == FAIL)
4606 return FAIL;
4607 if (ppconst->pp_used > 0)
4608 {
4609 // apply the '!', '-' and '+' before the constant
4610 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004611 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004612 return FAIL;
4613 return OK;
4614 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004615 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004617 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618}
4619
4620/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004621 * Give the "white on both sides" error, taking the operator from "p[len]".
4622 */
4623 void
4624error_white_both(char_u *op, int len)
4625{
4626 char_u buf[10];
4627
4628 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004629 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004630}
4631
4632/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004633 * <type>expr7: runtime type check / conversion
4634 */
4635 static int
4636compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4637{
4638 type_T *want_type = NULL;
4639
4640 // Recognize <type>
4641 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4642 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004643 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004644 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4645 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004646 return FAIL;
4647
4648 if (**arg != '>')
4649 {
4650 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004651 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004652 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004653 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004654 return FAIL;
4655 }
4656 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004657 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004658 return FAIL;
4659 }
4660
4661 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4662 return FAIL;
4663
4664 if (want_type != NULL)
4665 {
4666 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004667 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004668 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004669
Bram Moolenaard1103582020-08-14 22:44:25 +02004670 generate_ppconst(cctx, ppconst);
4671 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004672 where.wt_index = 0;
4673 where.wt_variable = FALSE;
4674 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004675 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004676 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004677 return FAIL;
4678 }
4679 }
4680
4681 return OK;
4682}
4683
4684/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 * * number multiplication
4686 * / number division
4687 * % number modulo
4688 */
4689 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004690compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691{
4692 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004693 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004694 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004696 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004697 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 return FAIL;
4699
4700 /*
4701 * Repeat computing, until no "*", "/" or "%" is following.
4702 */
4703 for (;;)
4704 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004705 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 if (*op != '*' && *op != '/' && *op != '%')
4707 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004708 if (next != NULL)
4709 {
4710 *arg = next_line_from_context(cctx, TRUE);
4711 op = skipwhite(*arg);
4712 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004713
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004714 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004716 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004717 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004719 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004720 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004722 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004723 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004725
4726 if (ppconst->pp_used == ppconst_used + 2
4727 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4728 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004729 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004730 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4731 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4732 varnumber_T res = 0;
4733 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004735 // both are numbers: compute the result
4736 switch (*op)
4737 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004738 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004739 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004740 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004741 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004742 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004743 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004744 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004745 break;
4746 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004747 if (failed)
4748 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004749 tv1->vval.v_number = res;
4750 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004751 }
4752 else
4753 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004754 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004755 generate_two_op(cctx, op);
4756 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 }
4758
4759 return OK;
4760}
4761
4762/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004763 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 * - number subtraction
4765 * .. string concatenation
4766 */
4767 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004768compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769{
4770 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004771 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004773 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774
4775 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004776 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777 return FAIL;
4778
4779 /*
4780 * Repeat computing, until no "+", "-" or ".." is following.
4781 */
4782 for (;;)
4783 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004784 op = may_peek_next_line(cctx, *arg, &next);
4785 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004787 if (op[0] == op[1] && *op != '.' && next)
4788 // Finding "++" or "--" on the next line is a separate command.
4789 // But ".." is concatenation.
4790 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004792 if (next != NULL)
4793 {
4794 *arg = next_line_from_context(cctx, TRUE);
4795 op = skipwhite(*arg);
4796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004798 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004800 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004801 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 }
4803
Bram Moolenaare0de1712020-12-02 17:36:54 +01004804 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004805 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004807 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004808 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 return FAIL;
4810
Bram Moolenaara5565e42020-05-09 15:44:01 +02004811 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004812 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004813 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4814 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4815 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4816 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004818 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4819 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004820
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004821 // concat/subtract/add constant numbers
4822 if (*op == '+')
4823 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4824 else if (*op == '-')
4825 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4826 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004827 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004828 // concatenate constant strings
4829 char_u *s1 = tv1->vval.v_string;
4830 char_u *s2 = tv2->vval.v_string;
4831 size_t len1 = STRLEN(s1);
4832
4833 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4834 if (tv1->vval.v_string == NULL)
4835 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004836 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004837 return FAIL;
4838 }
4839 mch_memmove(tv1->vval.v_string, s1, len1);
4840 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004841 vim_free(s1);
4842 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004843 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004844 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 }
4846 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004847 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004848 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004849 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004850 if (*op == '.')
4851 {
4852 if (may_generate_2STRING(-2, cctx) == FAIL
4853 || may_generate_2STRING(-1, cctx) == FAIL)
4854 return FAIL;
4855 generate_instr_drop(cctx, ISN_CONCAT, 1);
4856 }
4857 else
4858 generate_two_op(cctx, op);
4859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 }
4861
4862 return OK;
4863}
4864
4865/*
4866 * expr5a == expr5b
4867 * expr5a =~ expr5b
4868 * expr5a != expr5b
4869 * expr5a !~ expr5b
4870 * expr5a > expr5b
4871 * expr5a >= expr5b
4872 * expr5a < expr5b
4873 * expr5a <= expr5b
4874 * expr5a is expr5b
4875 * expr5a isnot expr5b
4876 *
4877 * Produces instructions:
4878 * EVAL expr5a Push result of "expr5a"
4879 * EVAL expr5b Push result of "expr5b"
4880 * COMPARE one of the compare instructions
4881 */
4882 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004883compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004885 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004887 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004890 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891
4892 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004893 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 return FAIL;
4895
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004896 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004897 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898
4899 /*
4900 * If there is a comparative operator, use it.
4901 */
4902 if (type != EXPR_UNKNOWN)
4903 {
4904 int ic = FALSE; // Default: do not ignore case
4905
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004906 if (next != NULL)
4907 {
4908 *arg = next_line_from_context(cctx, TRUE);
4909 p = skipwhite(*arg);
4910 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 if (type_is && (p[len] == '?' || p[len] == '#'))
4912 {
4913 semsg(_(e_invexpr2), *arg);
4914 return FAIL;
4915 }
4916 // extra question mark appended: ignore case
4917 if (p[len] == '?')
4918 {
4919 ic = TRUE;
4920 ++len;
4921 }
4922 // extra '#' appended: match case (ignored)
4923 else if (p[len] == '#')
4924 ++len;
4925 // nothing appended: match case
4926
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004927 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004929 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004930 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 }
4932
4933 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004934 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004935 return FAIL;
4936
Bram Moolenaara5565e42020-05-09 15:44:01 +02004937 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 return FAIL;
4939
Bram Moolenaara5565e42020-05-09 15:44:01 +02004940 if (ppconst->pp_used == ppconst_used + 2)
4941 {
4942 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4943 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4944 int ret;
4945
4946 // Both sides are a constant, compute the result now.
4947 // First check for a valid combination of types, this is more
4948 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004949 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004950 ret = FAIL;
4951 else
4952 {
4953 ret = typval_compare(tv1, tv2, type, ic);
4954 tv1->v_type = VAR_BOOL;
4955 tv1->vval.v_number = tv1->vval.v_number
4956 ? VVAL_TRUE : VVAL_FALSE;
4957 clear_tv(tv2);
4958 --ppconst->pp_used;
4959 }
4960 return ret;
4961 }
4962
4963 generate_ppconst(cctx, ppconst);
4964 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 }
4966
4967 return OK;
4968}
4969
Bram Moolenaar7f141552020-05-09 17:35:53 +02004970static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972/*
4973 * Compile || or &&.
4974 */
4975 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004976compile_and_or(
4977 char_u **arg,
4978 cctx_T *cctx,
4979 char *op,
4980 ppconst_T *ppconst,
4981 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004983 char_u *next;
4984 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 int opchar = *op;
4986
4987 if (p[0] == opchar && p[1] == opchar)
4988 {
4989 garray_T *instr = &cctx->ctx_instr;
4990 garray_T end_ga;
4991
4992 /*
4993 * Repeat until there is no following "||" or "&&"
4994 */
4995 ga_init2(&end_ga, sizeof(int), 10);
4996 while (p[0] == opchar && p[1] == opchar)
4997 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004998 long start_lnum = SOURCING_LNUM;
4999 int start_ctx_lnum = cctx->ctx_lnum;
5000 int save_lnum;
5001
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005002 if (next != NULL)
5003 {
5004 *arg = next_line_from_context(cctx, TRUE);
5005 p = skipwhite(*arg);
5006 }
5007
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005008 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5009 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005010 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005011 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005012 return FAIL;
5013 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005015 // TODO: use ppconst if the value is a constant and check
5016 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005017 generate_ppconst(cctx, ppconst);
5018
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005019 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005020 SOURCING_LNUM = start_lnum;
5021 save_lnum = cctx->ctx_lnum;
5022 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005023 if (bool_on_stack(cctx) == FAIL)
5024 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005025 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005026 ga_clear(&end_ga);
5027 return FAIL;
5028 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005029 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 if (ga_grow(&end_ga, 1) == FAIL)
5032 {
5033 ga_clear(&end_ga);
5034 return FAIL;
5035 }
5036 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5037 ++end_ga.ga_len;
5038 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005039 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040
5041 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005042 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005043 {
5044 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005045 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005046 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005047
Bram Moolenaara5565e42020-05-09 15:44:01 +02005048 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5049 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 {
5051 ga_clear(&end_ga);
5052 return FAIL;
5053 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005054
5055 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005057 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005059 // Every part must evaluate to a bool.
5060 if (bool_on_stack(cctx) == FAIL)
5061 {
5062 ga_clear(&end_ga);
5063 return FAIL;
5064 }
5065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 // Fill in the end label in all jumps.
5067 while (end_ga.ga_len > 0)
5068 {
5069 isn_T *isn;
5070
5071 --end_ga.ga_len;
5072 isn = ((isn_T *)instr->ga_data)
5073 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5074 isn->isn_arg.jump.jump_where = instr->ga_len;
5075 }
5076 ga_clear(&end_ga);
5077 }
5078
5079 return OK;
5080}
5081
5082/*
5083 * expr4a && expr4a && expr4a logical AND
5084 *
5085 * Produces instructions:
5086 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005087 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005088 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005090 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 * EVAL expr4c Push result of "expr4c"
5092 * end:
5093 */
5094 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005095compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005097 int ppconst_used = ppconst->pp_used;
5098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005100 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 return FAIL;
5102
5103 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005104 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105}
5106
5107/*
5108 * expr3a || expr3b || expr3c logical OR
5109 *
5110 * Produces instructions:
5111 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005112 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005113 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005115 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 * EVAL expr3c Push result of "expr3c"
5117 * end:
5118 */
5119 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005120compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005122 int ppconst_used = ppconst->pp_used;
5123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005125 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126 return FAIL;
5127
5128 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005129 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130}
5131
5132/*
5133 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005135 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005136 * JUMP_IF_FALSE alt jump if false
5137 * EVAL expr1a
5138 * JUMP_ALWAYS end
5139 * alt: EVAL expr1b
5140 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005141 *
5142 * Toplevel expression: expr2 ?? expr1
5143 * Produces instructions:
5144 * EVAL expr2 Push result of "expr2"
5145 * JUMP_AND_KEEP_IF_TRUE end jump if true
5146 * EVAL expr1
5147 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 */
5149 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005150compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151{
5152 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005153 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005154 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155
Bram Moolenaar3988f642020-08-27 22:43:03 +02005156 // Ignore all kinds of errors when not producing code.
5157 if (cctx->ctx_skip == SKIP_YES)
5158 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005159 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005160 return OK;
5161 }
5162
Bram Moolenaar61a89812020-05-07 16:58:17 +02005163 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005164 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005165 return FAIL;
5166
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005167 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168 if (*p == '?')
5169 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005170 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171 garray_T *instr = &cctx->ctx_instr;
5172 garray_T *stack = &cctx->ctx_type_stack;
5173 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005174 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005175 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005176 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005177 int has_const_expr = FALSE;
5178 int const_value = FALSE;
5179 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005181 if (next != NULL)
5182 {
5183 *arg = next_line_from_context(cctx, TRUE);
5184 p = skipwhite(*arg);
5185 }
5186
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005187 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005188 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005189 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005190 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005191 return FAIL;
5192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193
Bram Moolenaara5565e42020-05-09 15:44:01 +02005194 if (ppconst->pp_used == ppconst_used + 1)
5195 {
5196 // the condition is a constant, we know whether the ? or the :
5197 // expression is to be evaluated.
5198 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005199 if (op_falsy)
5200 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5201 else
5202 {
5203 int error = FALSE;
5204
5205 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5206 &error);
5207 if (error)
5208 return FAIL;
5209 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005210 cctx->ctx_skip = save_skip == SKIP_YES ||
5211 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5212
5213 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5214 // "left ?? right" and "left" is truthy: produce "left"
5215 generate_ppconst(cctx, ppconst);
5216 else
5217 {
5218 clear_tv(&ppconst->pp_tv[ppconst_used]);
5219 --ppconst->pp_used;
5220 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005221 }
5222 else
5223 {
5224 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005225 if (op_falsy)
5226 end_idx = instr->ga_len;
5227 generate_JUMP(cctx, op_falsy
5228 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5229 if (op_falsy)
5230 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005232
5233 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005234 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005235 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005236 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005237 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005238
Bram Moolenaara5565e42020-05-09 15:44:01 +02005239 if (!has_const_expr)
5240 {
5241 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005242
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005243 if (!op_falsy)
5244 {
5245 // remember the type and drop it
5246 --stack->ga_len;
5247 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005249 end_idx = instr->ga_len;
5250 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005251
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005252 // jump here from JUMP_IF_FALSE
5253 isn = ((isn_T *)instr->ga_data) + alt_idx;
5254 isn->isn_arg.jump.jump_where = instr->ga_len;
5255 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005257
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005258 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005260 // Check for the ":".
5261 p = may_peek_next_line(cctx, *arg, &next);
5262 if (*p != ':')
5263 {
5264 emsg(_(e_missing_colon));
5265 return FAIL;
5266 }
5267 if (next != NULL)
5268 {
5269 *arg = next_line_from_context(cctx, TRUE);
5270 p = skipwhite(*arg);
5271 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005272
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005273 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5274 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005275 semsg(_(e_white_space_required_before_and_after_str_at_str),
5276 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005277 return FAIL;
5278 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005280 // evaluate the third expression
5281 if (has_const_expr)
5282 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005283 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005284 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005285 return FAIL;
5286 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5287 return FAIL;
5288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289
Bram Moolenaara5565e42020-05-09 15:44:01 +02005290 if (!has_const_expr)
5291 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005292 type_T **typep;
5293
Bram Moolenaara5565e42020-05-09 15:44:01 +02005294 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295
Bram Moolenaara5565e42020-05-09 15:44:01 +02005296 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005297 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5298 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005299
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005300 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005301 isn = ((isn_T *)instr->ga_data) + end_idx;
5302 isn->isn_arg.jump.jump_where = instr->ga_len;
5303 }
5304
5305 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306 }
5307 return OK;
5308}
5309
5310/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005311 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005312 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5313 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005314 */
5315 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005316compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005317{
5318 ppconst_T ppconst;
5319
5320 CLEAR_FIELD(ppconst);
5321 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5322 {
5323 clear_ppconst(&ppconst);
5324 return FAIL;
5325 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005326 if (is_const != NULL)
5327 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005328 if (generate_ppconst(cctx, &ppconst) == FAIL)
5329 return FAIL;
5330 return OK;
5331}
5332
5333/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005334 * Toplevel expression.
5335 */
5336 static int
5337compile_expr0(char_u **arg, cctx_T *cctx)
5338{
5339 return compile_expr0_ext(arg, cctx, NULL);
5340}
5341
5342/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005343 * Compile "return [expr]".
5344 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345 */
5346 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005347compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005348{
5349 char_u *p = arg;
5350 garray_T *stack = &cctx->ctx_type_stack;
5351 type_T *stack_type;
5352
5353 if (*p != NUL && *p != '|' && *p != '\n')
5354 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005355 if (legacy)
5356 {
5357 int save_flags = cmdmod.cmod_flags;
5358
5359 generate_LEGACY_EVAL(cctx, p);
5360 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5361 0, cctx, FALSE, FALSE) == FAIL)
5362 return NULL;
5363 cmdmod.cmod_flags |= CMOD_LEGACY;
5364 (void)skip_expr(&p, NULL);
5365 cmdmod.cmod_flags = save_flags;
5366 }
5367 else
5368 {
5369 // compile return argument into instructions
5370 if (compile_expr0(&p, cctx) == FAIL)
5371 return NULL;
5372 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005374 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005375 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005376 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005377 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005378 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5379 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005380 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005381 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005382 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005383 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005384 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005385 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5386 && stack_type->tt_type != VAR_VOID
5387 && stack_type->tt_type != VAR_UNKNOWN)
5388 {
5389 emsg(_(e_returning_value_in_function_without_return_type));
5390 return NULL;
5391 }
5392 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005393 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005394 return NULL;
5395 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005397 }
5398 else
5399 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005400 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005401 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005402 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5403 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005404 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005405 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005406 return NULL;
5407 }
5408
5409 // No argument, return zero.
5410 generate_PUSHNR(cctx, 0);
5411 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005412
5413 // Undo any command modifiers.
5414 generate_undo_cmdmods(cctx);
5415
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005416 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005417 return NULL;
5418
5419 // "return val | endif" is possible
5420 return skipwhite(p);
5421}
5422
5423/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005424 * Get a line from the compilation context, compatible with exarg_T getline().
5425 * Return a pointer to the line in allocated memory.
5426 * Return NULL for end-of-file or some error.
5427 */
5428 static char_u *
5429exarg_getline(
5430 int c UNUSED,
5431 void *cookie,
5432 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005433 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005434{
5435 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005436 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005437
Bram Moolenaar66250c92020-08-20 15:02:42 +02005438 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005439 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005440 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005441 return NULL;
5442 ++cctx->ctx_lnum;
5443 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5444 // Comment lines result in NULL pointers, skip them.
5445 if (p != NULL)
5446 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005447 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005448}
5449
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005450 void
5451fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5452{
5453 eap->getline = exarg_getline;
5454 eap->cookie = cctx;
5455}
5456
Bram Moolenaar04b12692020-05-04 23:24:44 +02005457/*
5458 * Compile a nested :def command.
5459 */
5460 static char_u *
5461compile_nested_function(exarg_T *eap, cctx_T *cctx)
5462{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005463 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005464 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005465 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005466 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005467 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005468 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005469
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005470 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005471 {
5472 emsg(_(e_cannot_use_bang_with_nested_def));
5473 return NULL;
5474 }
5475
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005476 if (*name_start == '/')
5477 {
5478 name_end = skip_regexp(name_start + 1, '/', TRUE);
5479 if (*name_end == '/')
5480 ++name_end;
5481 eap->nextcmd = check_nextcmd(name_end);
5482 }
5483 if (name_end == name_start || *skipwhite(name_end) != '(')
5484 {
5485 if (!ends_excmd2(name_start, name_end))
5486 {
5487 semsg(_(e_invalid_command_str), eap->cmd);
5488 return NULL;
5489 }
5490
5491 // "def" or "def Name": list functions
5492 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5493 return NULL;
5494 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5495 }
5496
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005497 // Only g:Func() can use a namespace.
5498 if (name_start[1] == ':' && !is_global)
5499 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005500 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005501 return NULL;
5502 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005503 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005504 return NULL;
5505
Bram Moolenaar04b12692020-05-04 23:24:44 +02005506 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005507 fill_exarg_from_cctx(eap, cctx);
5508
Bram Moolenaar04b12692020-05-04 23:24:44 +02005509 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005510 lambda_name = vim_strsave(get_lambda_name());
5511 if (lambda_name == NULL)
5512 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005513 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005514
Bram Moolenaar822ba242020-05-24 23:00:18 +02005515 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005516 {
5517 r = eap->skip ? OK : FAIL;
5518 goto theend;
5519 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005520
5521 // copy over the block scope IDs before compiling
5522 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5523 {
5524 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5525
5526 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5527 if (ufunc->uf_block_ids != NULL)
5528 {
5529 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5530 sizeof(int) * block_depth);
5531 ufunc->uf_block_depth = block_depth;
5532 }
5533 }
5534
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005535 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5536 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005537 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005538 {
5539 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005540 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005541 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005542
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005543 if (is_global)
5544 {
5545 char_u *func_name = vim_strnsave(name_start + 2,
5546 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005547
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005548 if (func_name == NULL)
5549 r = FAIL;
5550 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005551 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005552 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005553 lambda_name = NULL;
5554 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005555 }
5556 else
5557 {
5558 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005559 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005560 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005561
Bram Moolenaareef21022020-08-01 22:16:43 +02005562 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005563 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005564 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005565 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005566 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5567 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005568 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005569
5570theend:
5571 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005572 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005573}
5574
5575/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005576 * Return the length of an assignment operator, or zero if there isn't one.
5577 */
5578 int
5579assignment_len(char_u *p, int *heredoc)
5580{
5581 if (*p == '=')
5582 {
5583 if (p[1] == '<' && p[2] == '<')
5584 {
5585 *heredoc = TRUE;
5586 return 3;
5587 }
5588 return 1;
5589 }
5590 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5591 return 2;
5592 if (STRNCMP(p, "..=", 3) == 0)
5593 return 3;
5594 return 0;
5595}
5596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005597/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005598 * Generate the load instruction for "name".
5599 */
5600 static void
5601generate_loadvar(
5602 cctx_T *cctx,
5603 assign_dest_T dest,
5604 char_u *name,
5605 lvar_T *lvar,
5606 type_T *type)
5607{
5608 switch (dest)
5609 {
5610 case dest_option:
5611 // TODO: check the option exists
5612 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5613 break;
5614 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005615 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5616 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5617 else
5618 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005619 break;
5620 case dest_buffer:
5621 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5622 break;
5623 case dest_window:
5624 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5625 break;
5626 case dest_tab:
5627 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5628 break;
5629 case dest_script:
5630 compile_load_scriptvar(cctx,
5631 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5632 break;
5633 case dest_env:
5634 // Include $ in the name here
5635 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5636 break;
5637 case dest_reg:
5638 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5639 break;
5640 case dest_vimvar:
5641 generate_LOADV(cctx, name + 2, TRUE);
5642 break;
5643 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005644 if (lvar->lv_from_outer > 0)
5645 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5646 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005647 else
5648 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5649 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005650 case dest_expr:
5651 // list or dict value should already be on the stack.
5652 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005653 }
5654}
5655
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005656/*
5657 * Skip over "[expr]" or ".member".
5658 * Does not check for any errors.
5659 */
5660 static char_u *
5661skip_index(char_u *start)
5662{
5663 char_u *p = start;
5664
5665 if (*p == '[')
5666 {
5667 p = skipwhite(p + 1);
5668 (void)skip_expr(&p, NULL);
5669 p = skipwhite(p);
5670 if (*p == ']')
5671 return p + 1;
5672 return p;
5673 }
5674 // if (*p == '.')
5675 return to_name_end(p + 1, TRUE);
5676}
5677
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005678 void
5679vim9_declare_error(char_u *name)
5680{
5681 char *scope = "";
5682
5683 switch (*name)
5684 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005685 case 'g': scope = _("global"); break;
5686 case 'b': scope = _("buffer"); break;
5687 case 'w': scope = _("window"); break;
5688 case 't': scope = _("tab"); break;
5689 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005690 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005691 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005692 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005693 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005694 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005695 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005696 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005697 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005698 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005699}
5700
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005701/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005702 * For one assignment figure out the type of destination. Return it in "dest".
5703 * When not recognized "dest" is not set.
5704 * For an option "opt_flags" is set.
5705 * For a v:var "vimvaridx" is set.
5706 * "type" is set to the destination type if known, unchanted otherwise.
5707 * Return FAIL if an error message was given.
5708 */
5709 static int
5710get_var_dest(
5711 char_u *name,
5712 assign_dest_T *dest,
5713 int cmdidx,
5714 int *opt_flags,
5715 int *vimvaridx,
5716 type_T **type,
5717 cctx_T *cctx)
5718{
5719 char_u *p;
5720
5721 if (*name == '&')
5722 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005723 int cc;
5724 long numval;
5725 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005726
5727 *dest = dest_option;
5728 if (cmdidx == CMD_final || cmdidx == CMD_const)
5729 {
5730 emsg(_(e_const_option));
5731 return FAIL;
5732 }
5733 p = name;
5734 p = find_option_end(&p, opt_flags);
5735 if (p == NULL)
5736 {
5737 // cannot happen?
5738 emsg(_(e_letunexp));
5739 return FAIL;
5740 }
5741 cc = *p;
5742 *p = NUL;
5743 opt_type = get_option_value(skip_option_env_lead(name),
5744 &numval, NULL, *opt_flags);
5745 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005746 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005747 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005748 case gov_unknown:
5749 semsg(_(e_unknown_option), name);
5750 return FAIL;
5751 case gov_string:
5752 case gov_hidden_string:
5753 *type = &t_string;
5754 break;
5755 case gov_bool:
5756 case gov_hidden_bool:
5757 *type = &t_bool;
5758 break;
5759 case gov_number:
5760 case gov_hidden_number:
5761 *type = &t_number;
5762 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005763 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005764 }
5765 else if (*name == '$')
5766 {
5767 *dest = dest_env;
5768 *type = &t_string;
5769 }
5770 else if (*name == '@')
5771 {
5772 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5773 {
5774 emsg_invreg(name[1]);
5775 return FAIL;
5776 }
5777 *dest = dest_reg;
5778 *type = &t_string;
5779 }
5780 else if (STRNCMP(name, "g:", 2) == 0)
5781 {
5782 *dest = dest_global;
5783 }
5784 else if (STRNCMP(name, "b:", 2) == 0)
5785 {
5786 *dest = dest_buffer;
5787 }
5788 else if (STRNCMP(name, "w:", 2) == 0)
5789 {
5790 *dest = dest_window;
5791 }
5792 else if (STRNCMP(name, "t:", 2) == 0)
5793 {
5794 *dest = dest_tab;
5795 }
5796 else if (STRNCMP(name, "v:", 2) == 0)
5797 {
5798 typval_T *vtv;
5799 int di_flags;
5800
5801 *vimvaridx = find_vim_var(name + 2, &di_flags);
5802 if (*vimvaridx < 0)
5803 {
5804 semsg(_(e_variable_not_found_str), name);
5805 return FAIL;
5806 }
5807 // We use the current value of "sandbox" here, is that OK?
5808 if (var_check_ro(di_flags, name, FALSE))
5809 return FAIL;
5810 *dest = dest_vimvar;
5811 vtv = get_vim_var_tv(*vimvaridx);
5812 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5813 }
5814 return OK;
5815}
5816
5817/*
5818 * Generate a STORE instruction for "dest", not being "dest_local".
5819 * Return FAIL when out of memory.
5820 */
5821 static int
5822generate_store_var(
5823 cctx_T *cctx,
5824 assign_dest_T dest,
5825 int opt_flags,
5826 int vimvaridx,
5827 int scriptvar_idx,
5828 int scriptvar_sid,
5829 type_T *type,
5830 char_u *name)
5831{
5832 switch (dest)
5833 {
5834 case dest_option:
5835 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5836 opt_flags);
5837 case dest_global:
5838 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005839 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5840 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005841 case dest_buffer:
5842 // include b: with the name, easier to execute that way
5843 return generate_STORE(cctx, ISN_STOREB, 0, name);
5844 case dest_window:
5845 // include w: with the name, easier to execute that way
5846 return generate_STORE(cctx, ISN_STOREW, 0, name);
5847 case dest_tab:
5848 // include t: with the name, easier to execute that way
5849 return generate_STORE(cctx, ISN_STORET, 0, name);
5850 case dest_env:
5851 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5852 case dest_reg:
5853 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5854 case dest_vimvar:
5855 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5856 case dest_script:
5857 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005858 // "s:" may be included in the name.
5859 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005860 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005861 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5862 scriptvar_sid, scriptvar_idx, type);
5863 case dest_local:
5864 case dest_expr:
5865 // cannot happen
5866 break;
5867 }
5868 return FAIL;
5869}
5870
Bram Moolenaar752fc692021-01-04 21:57:11 +01005871 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005872generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5873{
5874 if (lhs->lhs_dest != dest_local)
5875 return generate_store_var(cctx, lhs->lhs_dest,
5876 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5877 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5878 lhs->lhs_type, lhs->lhs_name);
5879
5880 if (lhs->lhs_lvar != NULL)
5881 {
5882 garray_T *instr = &cctx->ctx_instr;
5883 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5884
5885 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5886 // ISN_STORENR
5887 if (lhs->lhs_lvar->lv_from_outer == 0
5888 && instr->ga_len == instr_count + 1
5889 && isn->isn_type == ISN_PUSHNR)
5890 {
5891 varnumber_T val = isn->isn_arg.number;
5892 garray_T *stack = &cctx->ctx_type_stack;
5893
5894 isn->isn_type = ISN_STORENR;
5895 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5896 isn->isn_arg.storenr.stnr_val = val;
5897 if (stack->ga_len > 0)
5898 --stack->ga_len;
5899 }
5900 else if (lhs->lhs_lvar->lv_from_outer > 0)
5901 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5902 lhs->lhs_lvar->lv_from_outer);
5903 else
5904 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5905 }
5906 return OK;
5907}
5908
5909 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005910is_decl_command(int cmdidx)
5911{
5912 return cmdidx == CMD_let || cmdidx == CMD_var
5913 || cmdidx == CMD_final || cmdidx == CMD_const;
5914}
5915
5916/*
5917 * Figure out the LHS type and other properties for an assignment or one item
5918 * of ":unlet" with an index.
5919 * Returns OK or FAIL.
5920 */
5921 static int
5922compile_lhs(
5923 char_u *var_start,
5924 lhs_T *lhs,
5925 int cmdidx,
5926 int heredoc,
5927 int oplen,
5928 cctx_T *cctx)
5929{
5930 char_u *var_end;
5931 int is_decl = is_decl_command(cmdidx);
5932
5933 CLEAR_POINTER(lhs);
5934 lhs->lhs_dest = dest_local;
5935 lhs->lhs_vimvaridx = -1;
5936 lhs->lhs_scriptvar_idx = -1;
5937
5938 // "dest_end" is the end of the destination, including "[expr]" or
5939 // ".name".
5940 // "var_end" is the end of the variable/option/etc. name.
5941 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5942 if (*var_start == '@')
5943 var_end = var_start + 2;
5944 else
5945 {
5946 // skip over the leading "&", "&l:", "&g:" and "$"
5947 var_end = skip_option_env_lead(var_start);
5948 var_end = to_name_end(var_end, TRUE);
5949 }
5950
5951 // "a: type" is declaring variable "a" with a type, not dict "a:".
5952 if (is_decl && lhs->lhs_dest_end == var_start + 2
5953 && lhs->lhs_dest_end[-1] == ':')
5954 --lhs->lhs_dest_end;
5955 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5956 --var_end;
5957
5958 // compute the length of the destination without "[expr]" or ".name"
5959 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02005960 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005961 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5962 if (lhs->lhs_name == NULL)
5963 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005964
5965 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5966 // Something follows after the variable: "var[idx]" or "var.key".
5967 lhs->lhs_has_index = TRUE;
5968
Bram Moolenaar752fc692021-01-04 21:57:11 +01005969 if (heredoc)
5970 lhs->lhs_type = &t_list_string;
5971 else
5972 lhs->lhs_type = &t_any;
5973
5974 if (cctx->ctx_skip != SKIP_YES)
5975 {
5976 int declare_error = FALSE;
5977
5978 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5979 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5980 &lhs->lhs_type, cctx) == FAIL)
5981 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005982 if (lhs->lhs_dest != dest_local
5983 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005984 {
5985 // Specific kind of variable recognized.
5986 declare_error = is_decl;
5987 }
5988 else
5989 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01005990 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02005991 if (check_reserved_name(lhs->lhs_name) == FAIL)
5992 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005993
5994 if (lookup_local(var_start, lhs->lhs_varlen,
5995 &lhs->lhs_local_lvar, cctx) == OK)
5996 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5997 else
5998 {
5999 CLEAR_FIELD(lhs->lhs_arg_lvar);
6000 if (arg_exists(var_start, lhs->lhs_varlen,
6001 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6002 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6003 {
6004 if (is_decl)
6005 {
6006 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6007 return FAIL;
6008 }
6009 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6010 }
6011 }
6012 if (lhs->lhs_lvar != NULL)
6013 {
6014 if (is_decl)
6015 {
6016 semsg(_(e_variable_already_declared), lhs->lhs_name);
6017 return FAIL;
6018 }
6019 }
6020 else
6021 {
6022 int script_namespace = lhs->lhs_varlen > 1
6023 && STRNCMP(var_start, "s:", 2) == 0;
6024 int script_var = (script_namespace
6025 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006026 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006027 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006028 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006029 imported_T *import =
6030 find_imported(var_start, lhs->lhs_varlen, cctx);
6031
6032 if (script_namespace || script_var || import != NULL)
6033 {
6034 char_u *rawname = lhs->lhs_name
6035 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6036
6037 if (is_decl)
6038 {
6039 if (script_namespace)
6040 semsg(_(e_cannot_declare_script_variable_in_function),
6041 lhs->lhs_name);
6042 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006043 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006044 lhs->lhs_name);
6045 return FAIL;
6046 }
6047 else if (cctx->ctx_ufunc->uf_script_ctx_version
6048 == SCRIPT_VERSION_VIM9
6049 && script_namespace
6050 && !script_var && import == NULL)
6051 {
6052 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6053 return FAIL;
6054 }
6055
6056 lhs->lhs_dest = dest_script;
6057
6058 // existing script-local variables should have a type
6059 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6060 if (import != NULL)
6061 lhs->lhs_scriptvar_sid = import->imp_sid;
6062 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6063 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006064 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006065 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006066 lhs->lhs_scriptvar_sid, rawname,
6067 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6068 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006069 if (lhs->lhs_scriptvar_idx >= 0)
6070 {
6071 scriptitem_T *si = SCRIPT_ITEM(
6072 lhs->lhs_scriptvar_sid);
6073 svar_T *sv =
6074 ((svar_T *)si->sn_var_vals.ga_data)
6075 + lhs->lhs_scriptvar_idx;
6076 lhs->lhs_type = sv->sv_type;
6077 }
6078 }
6079 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006080 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006081 == FAIL)
6082 return FAIL;
6083 }
6084 }
6085
6086 if (declare_error)
6087 {
6088 vim9_declare_error(lhs->lhs_name);
6089 return FAIL;
6090 }
6091 }
6092
6093 // handle "a:name" as a name, not index "name" on "a"
6094 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6095 var_end = lhs->lhs_dest_end;
6096
6097 if (lhs->lhs_dest != dest_option)
6098 {
6099 if (is_decl && *var_end == ':')
6100 {
6101 char_u *p;
6102
6103 // parse optional type: "let var: type = expr"
6104 if (!VIM_ISWHITE(var_end[1]))
6105 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006106 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006107 return FAIL;
6108 }
6109 p = skipwhite(var_end + 1);
6110 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6111 if (lhs->lhs_type == NULL)
6112 return FAIL;
6113 lhs->lhs_has_type = TRUE;
6114 }
6115 else if (lhs->lhs_lvar != NULL)
6116 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6117 }
6118
Bram Moolenaare42939a2021-04-05 17:11:17 +02006119 if (oplen == 3 && !heredoc
6120 && lhs->lhs_dest != dest_global
6121 && !lhs->lhs_has_index
6122 && lhs->lhs_type->tt_type != VAR_STRING
6123 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006124 {
6125 emsg(_(e_can_only_concatenate_to_string));
6126 return FAIL;
6127 }
6128
6129 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6130 && cctx->ctx_skip != SKIP_YES)
6131 {
6132 if (oplen > 1 && !heredoc)
6133 {
6134 // +=, /=, etc. require an existing variable
6135 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6136 return FAIL;
6137 }
6138 if (!is_decl)
6139 {
6140 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6141 return FAIL;
6142 }
6143
Bram Moolenaar3f327882021-03-17 20:56:38 +01006144 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006145 if ((lhs->lhs_type->tt_type == VAR_FUNC
6146 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006147 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006148 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006149
6150 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006151 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6152 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6153 if (lhs->lhs_lvar == NULL)
6154 return FAIL;
6155 lhs->lhs_new_local = TRUE;
6156 }
6157
6158 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006159 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006160 {
6161 // Something follows after the variable: "var[idx]" or "var.key".
6162 // TODO: should we also handle "->func()" here?
6163 if (is_decl)
6164 {
6165 emsg(_(e_cannot_use_index_when_declaring_variable));
6166 return FAIL;
6167 }
6168
6169 if (var_start[lhs->lhs_varlen] == '['
6170 || var_start[lhs->lhs_varlen] == '.')
6171 {
6172 char_u *after = var_start + lhs->lhs_varlen;
6173 char_u *p;
6174
6175 // Only the last index is used below, if there are others
6176 // before it generate code for the expression. Thus for
6177 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6178 for (;;)
6179 {
6180 p = skip_index(after);
6181 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006182 {
6183 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006184 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006185 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006186 after = p;
6187 }
6188 if (after > var_start + lhs->lhs_varlen)
6189 {
6190 lhs->lhs_varlen = after - var_start;
6191 lhs->lhs_dest = dest_expr;
6192 // We don't know the type before evaluating the expression,
6193 // use "any" until then.
6194 lhs->lhs_type = &t_any;
6195 }
6196
Bram Moolenaar752fc692021-01-04 21:57:11 +01006197 if (lhs->lhs_type->tt_member == NULL)
6198 lhs->lhs_member_type = &t_any;
6199 else
6200 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6201 }
6202 else
6203 {
6204 semsg("Not supported yet: %s", var_start);
6205 return FAIL;
6206 }
6207 }
6208 return OK;
6209}
6210
6211/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006212 * Figure out the LHS and check a few errors.
6213 */
6214 static int
6215compile_assign_lhs(
6216 char_u *var_start,
6217 lhs_T *lhs,
6218 int cmdidx,
6219 int is_decl,
6220 int heredoc,
6221 int oplen,
6222 cctx_T *cctx)
6223{
6224 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6225 return FAIL;
6226
6227 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6228 {
6229 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6230 return FAIL;
6231 }
6232 if (!is_decl && lhs->lhs_lvar != NULL
6233 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6234 {
6235 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6236 return FAIL;
6237 }
6238 return OK;
6239}
6240
6241/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006242 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6243 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006244 */
6245 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006246compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006247 char_u *var_start,
6248 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006249 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006250 cctx_T *cctx)
6251{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006252 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006253 char_u *p;
6254 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006255 int need_white_before = TRUE;
6256 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006257
Bram Moolenaar752fc692021-01-04 21:57:11 +01006258 p = var_start + varlen;
6259 if (*p == '[')
6260 {
6261 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006262 if (*p == ':')
6263 {
6264 // empty first index, push zero
6265 r = generate_PUSHNR(cctx, 0);
6266 need_white_before = FALSE;
6267 }
6268 else
6269 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006270
6271 if (r == OK && *skipwhite(p) == ':')
6272 {
6273 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006274 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006275 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006276 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006277 empty_second = *skipwhite(p + 1) == ']';
6278 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6279 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006280 {
6281 semsg(_(e_white_space_required_before_and_after_str_at_str),
6282 ":", p);
6283 return FAIL;
6284 }
6285 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006286 if (*p == ']')
6287 // empty second index, push "none"
6288 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6289 else
6290 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006291 }
6292
Bram Moolenaar752fc692021-01-04 21:57:11 +01006293 if (r == OK && *skipwhite(p) != ']')
6294 {
6295 // this should not happen
6296 emsg(_(e_missbrac));
6297 r = FAIL;
6298 }
6299 }
6300 else // if (*p == '.')
6301 {
6302 char_u *key_end = to_name_end(p + 1, TRUE);
6303 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6304
6305 r = generate_PUSHS(cctx, key);
6306 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006307 return r;
6308}
6309
6310/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006311 * For a LHS with an index, load the variable to be indexed.
6312 */
6313 static int
6314compile_load_lhs(
6315 lhs_T *lhs,
6316 char_u *var_start,
6317 type_T *rhs_type,
6318 cctx_T *cctx)
6319{
6320 if (lhs->lhs_dest == dest_expr)
6321 {
6322 size_t varlen = lhs->lhs_varlen;
6323 int c = var_start[varlen];
6324 char_u *p = var_start;
6325 garray_T *stack = &cctx->ctx_type_stack;
6326
6327 // Evaluate "ll[expr]" of "ll[expr][idx]"
6328 var_start[varlen] = NUL;
6329 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6330 {
6331 // this should not happen
6332 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006333 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006334 return FAIL;
6335 }
6336 var_start[varlen] = c;
6337
6338 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6339 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6340 // now we can properly check the type
6341 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6342 && rhs_type != &t_void
6343 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6344 FALSE, FALSE) == FAIL)
6345 return FAIL;
6346 }
6347 else
6348 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6349 lhs->lhs_lvar, lhs->lhs_type);
6350 return OK;
6351}
6352
6353/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006354 * Produce code for loading "lhs" and also take care of an index.
6355 * Return OK/FAIL.
6356 */
6357 static int
6358compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6359{
6360 compile_load_lhs(lhs, var_start, NULL, cctx);
6361
6362 if (lhs->lhs_has_index)
6363 {
6364 int range = FALSE;
6365
6366 // Get member from list or dict. First compile the
6367 // index value.
6368 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6369 return FAIL;
6370 if (range)
6371 {
6372 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6373 var_start);
6374 return FAIL;
6375 }
6376
6377 // Get the member.
6378 if (compile_member(FALSE, cctx) == FAIL)
6379 return FAIL;
6380 }
6381 return OK;
6382}
6383
6384/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006385 * Assignment to a list or dict member, or ":unlet" for the item, using the
6386 * information in "lhs".
6387 * Returns OK or FAIL.
6388 */
6389 static int
6390compile_assign_unlet(
6391 char_u *var_start,
6392 lhs_T *lhs,
6393 int is_assign,
6394 type_T *rhs_type,
6395 cctx_T *cctx)
6396{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006397 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006398 garray_T *stack = &cctx->ctx_type_stack;
6399 int range = FALSE;
6400
Bram Moolenaar68452172021-04-12 21:21:02 +02006401 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006402 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006403 if (is_assign && range && lhs->lhs_type != &t_blob
6404 && lhs->lhs_type != &t_any)
6405 {
6406 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6407 return FAIL;
6408 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006409
6410 if (lhs->lhs_type == &t_any)
6411 {
6412 // Index on variable of unknown type: check at runtime.
6413 dest_type = VAR_ANY;
6414 }
6415 else
6416 {
6417 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006418 if (dest_type == VAR_DICT && range)
6419 {
6420 emsg(e_cannot_use_range_with_dictionary);
6421 return FAIL;
6422 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006423 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6424 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006425 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006426 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006427 type_T *type;
6428
6429 if (range)
6430 {
6431 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6432 if (need_type(type, &t_number,
6433 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006434 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006435 }
6436 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6437 if ((dest_type != VAR_BLOB || type != &t_special)
6438 && need_type(type, &t_number,
6439 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006440 return FAIL;
6441 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006442 }
6443
6444 // Load the dict or list. On the stack we then have:
6445 // - value (for assignment, not for :unlet)
6446 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006447 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006448 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006449 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6450 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006451
Bram Moolenaar68452172021-04-12 21:21:02 +02006452 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6453 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006454 {
6455 if (is_assign)
6456 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006457 if (range)
6458 {
6459 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6460 return FAIL;
6461 }
6462 else
6463 {
6464 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006465
Bram Moolenaar68452172021-04-12 21:21:02 +02006466 if (isn == NULL)
6467 return FAIL;
6468 isn->isn_arg.vartype = dest_type;
6469 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006470 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006471 else if (range)
6472 {
6473 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6474 return FAIL;
6475 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006476 else
6477 {
6478 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6479 return FAIL;
6480 }
6481 }
6482 else
6483 {
6484 emsg(_(e_indexable_type_required));
6485 return FAIL;
6486 }
6487
6488 return OK;
6489}
6490
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006491/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006492 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006493 * "let name"
6494 * "var name = expr"
6495 * "final name = expr"
6496 * "const name = expr"
6497 * "name = expr"
6498 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006499 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006500 * Return NULL for an error.
6501 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006502 */
6503 static char_u *
6504compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6505{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006506 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006507 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006508 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509 char_u *ret = NULL;
6510 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006511 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006512 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006514 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006515 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516 int oplen = 0;
6517 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006518 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006519 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006521 int is_decl = is_decl_command(cmdidx);
6522 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006523 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006525 // Skip over the "var" or "[var, var]" to get to any "=".
6526 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6527 if (p == NULL)
6528 return *arg == '[' ? arg : NULL;
6529
6530 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006531 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006532 // TODO: should we allow this, and figure out type inference from list
6533 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006534 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006535 return NULL;
6536 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006537 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 sp = p;
6540 p = skipwhite(p);
6541 op = p;
6542 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006543
6544 if (var_count > 0 && oplen == 0)
6545 // can be something like "[1, 2]->func()"
6546 return arg;
6547
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006548 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006549 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006550 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006551 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006552 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006553 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6554 {
6555 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6556 oplen = 2;
6557 incdec = TRUE;
6558 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560 if (heredoc)
6561 {
6562 list_T *l;
6563 listitem_T *li;
6564
6565 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006566 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006567 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006568 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006569 if (l == NULL)
6570 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571
Bram Moolenaar078269b2020-09-21 20:35:55 +02006572 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006574 // Push each line and the create the list.
6575 FOR_ALL_LIST_ITEMS(l, li)
6576 {
6577 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6578 li->li_tv.vval.v_string = NULL;
6579 }
6580 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582 list_free(l);
6583 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006584 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006585 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006586 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006588 char_u *wp;
6589
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006590 // for "[var, var] = expr" evaluate the expression here, loop over the
6591 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006592 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006593
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006594 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006595 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006596 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006597 if (compile_expr0(&p, cctx) == FAIL)
6598 return NULL;
6599 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006601 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006603 type_T *stacktype;
6604
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006605 stacktype = stack->ga_len == 0 ? &t_void
6606 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006607 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006609 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006610 goto theend;
6611 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006612 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006613 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006614 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006615 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006616 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6617 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006618 if (stacktype->tt_member != NULL)
6619 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006620 }
6621 }
6622
6623 /*
6624 * Loop over variables in "[var, var] = expr".
6625 * For "var = expr" and "let var: type" this is done only once.
6626 */
6627 if (var_count > 0)
6628 var_start = skipwhite(arg + 1); // skip over the "["
6629 else
6630 var_start = arg;
6631 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6632 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006633 int instr_count = -1;
6634
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006635 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6636 {
6637 // Ignore underscore in "[a, _, b] = list".
6638 if (var_count > 0)
6639 {
6640 var_start = skipwhite(var_start + 2);
6641 continue;
6642 }
6643 emsg(_(e_cannot_use_underscore_here));
6644 goto theend;
6645 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006646 vim_free(lhs.lhs_name);
6647
6648 /*
6649 * Figure out the LHS type and other properties.
6650 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006651 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6652 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006653 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006654 if (!heredoc)
6655 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006656 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006657 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006658 if (oplen > 0 && var_count == 0)
6659 {
6660 // skip over the "=" and the expression
6661 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006662 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006663 }
6664 }
6665 else if (oplen > 0)
6666 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006667 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006668 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006669
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006670 // For "var = expr" evaluate the expression.
6671 if (var_count == 0)
6672 {
6673 int r;
6674
6675 // for "+=", "*=", "..=" etc. first load the current value
6676 if (*op != '=')
6677 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006678 if (compile_load_lhs_with_index(&lhs, var_start,
6679 cctx) == FAIL)
6680 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006681 }
6682
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006683 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006684 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006685 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006686 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006687 r = generate_PUSHNR(cctx, 1);
6688 }
6689 else
6690 {
6691 // Temporarily hide the new local variable here, it is
6692 // not available to this expression.
6693 if (lhs.lhs_new_local)
6694 --cctx->ctx_locals.ga_len;
6695 wp = op + oplen;
6696 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6697 {
6698 if (lhs.lhs_new_local)
6699 ++cctx->ctx_locals.ga_len;
6700 goto theend;
6701 }
6702 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006703 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006704 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006705 if (r == FAIL)
6706 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006707 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006708 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006709 else if (semicolon && var_idx == var_count - 1)
6710 {
6711 // For "[var; var] = expr" get the rest of the list
6712 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6713 goto theend;
6714 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006715 else
6716 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006717 // For "[var, var] = expr" get the "var_idx" item from the
6718 // list.
6719 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006720 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006721 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006722
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006723 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006724 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006725 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006726 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006727 if ((rhs_type->tt_type == VAR_FUNC
6728 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006729 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006730 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006731 goto theend;
6732
Bram Moolenaar752fc692021-01-04 21:57:11 +01006733 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006734 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006735 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006736 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006737 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006738 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006739 }
6740 else
6741 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006742 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006743 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006744 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006745 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006746 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006747 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006748 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006749 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006750 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006751 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006752 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006753 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006754 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006755 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006756 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006757
Bram Moolenaar77709b12021-04-03 21:01:01 +02006758 // Without operator check type here, otherwise below.
6759 // Use the line number of the assignment.
6760 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006761 if (lhs.lhs_has_index)
6762 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006763 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006764 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006765 goto theend;
6766 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006767 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006768 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006769 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006770 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006771 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006772 else if (cmdidx == CMD_final)
6773 {
6774 emsg(_(e_final_requires_a_value));
6775 goto theend;
6776 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006777 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006779 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006780 goto theend;
6781 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006782 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006783 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006784 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006785 goto theend;
6786 }
6787 else
6788 {
6789 // variables are always initialized
6790 if (ga_grow(instr, 1) == FAIL)
6791 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006792 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006793 {
6794 case VAR_BOOL:
6795 generate_PUSHBOOL(cctx, VVAL_FALSE);
6796 break;
6797 case VAR_FLOAT:
6798#ifdef FEAT_FLOAT
6799 generate_PUSHF(cctx, 0.0);
6800#endif
6801 break;
6802 case VAR_STRING:
6803 generate_PUSHS(cctx, NULL);
6804 break;
6805 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006806 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006807 break;
6808 case VAR_FUNC:
6809 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6810 break;
6811 case VAR_LIST:
6812 generate_NEWLIST(cctx, 0);
6813 break;
6814 case VAR_DICT:
6815 generate_NEWDICT(cctx, 0);
6816 break;
6817 case VAR_JOB:
6818 generate_PUSHJOB(cctx, NULL);
6819 break;
6820 case VAR_CHANNEL:
6821 generate_PUSHCHANNEL(cctx, NULL);
6822 break;
6823 case VAR_NUMBER:
6824 case VAR_UNKNOWN:
6825 case VAR_ANY:
6826 case VAR_PARTIAL:
6827 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006828 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006829 case VAR_SPECIAL: // cannot happen
6830 generate_PUSHNR(cctx, 0);
6831 break;
6832 }
6833 }
6834 if (var_count == 0)
6835 end = p;
6836 }
6837
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006838 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006839 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006840 break;
6841
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006842 if (oplen > 0 && *op != '=')
6843 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006844 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006845 type_T *stacktype;
6846
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006847 if (*op == '.')
6848 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006849 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006850 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006851 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006852 if (
6853#ifdef FEAT_FLOAT
6854 // If variable is float operation with number is OK.
6855 !(expected == &t_float && stacktype == &t_number) &&
6856#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006857 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006858 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006859 goto theend;
6860
6861 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006862 {
6863 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6864 goto theend;
6865 }
6866 else if (*op == '+')
6867 {
6868 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006869 operator_type(lhs.lhs_member_type, stacktype),
6870 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006871 goto theend;
6872 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006873 else if (generate_two_op(cctx, op) == FAIL)
6874 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006876
Bram Moolenaar752fc692021-01-04 21:57:11 +01006877 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006878 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006879 // Use the info in "lhs" to store the value at the index in the
6880 // list or dict.
6881 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6882 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006883 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006884 }
6885 else
6886 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006887 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006888 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006889 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006890 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006891 generate_LOCKCONST(cctx);
6892
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006893 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006894 && (lhs.lhs_type->tt_type == VAR_DICT
6895 || lhs.lhs_type->tt_type == VAR_LIST)
6896 && lhs.lhs_type->tt_member != NULL
6897 && lhs.lhs_type->tt_member != &t_any
6898 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006899 // Set the type in the list or dict, so that it can be checked,
6900 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006901 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006902
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006903 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6904 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006905 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006906
6907 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006908 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006910
6911 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006912 if (var_count > 0 && !semicolon)
6913 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006914 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006915 goto theend;
6916 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006917
Bram Moolenaarb2097502020-07-19 17:17:02 +02006918 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919
6920theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006921 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922 return ret;
6923}
6924
6925/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006926 * Check for an assignment at "eap->cmd", compile it if found.
6927 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6928 */
6929 static int
6930may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6931{
6932 char_u *pskip;
6933 char_u *p;
6934
6935 // Assuming the command starts with a variable or function name,
6936 // find what follows.
6937 // Skip over "var.member", "var[idx]" and the like.
6938 // Also "&opt = val", "$ENV = val" and "@r = val".
6939 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6940 ? eap->cmd + 1 : eap->cmd;
6941 p = to_name_end(pskip, TRUE);
6942 if (p > eap->cmd && *p != NUL)
6943 {
6944 char_u *var_end;
6945 int oplen;
6946 int heredoc;
6947
6948 if (eap->cmd[0] == '@')
6949 var_end = eap->cmd + 2;
6950 else
6951 var_end = find_name_end(pskip, NULL, NULL,
6952 FNE_CHECK_START | FNE_INCL_BR);
6953 oplen = assignment_len(skipwhite(var_end), &heredoc);
6954 if (oplen > 0)
6955 {
6956 size_t len = p - eap->cmd;
6957
6958 // Recognize an assignment if we recognize the variable
6959 // name:
6960 // "g:var = expr"
6961 // "local = expr" where "local" is a local var.
6962 // "script = expr" where "script" is a script-local var.
6963 // "import = expr" where "import" is an imported var
6964 // "&opt = expr"
6965 // "$ENV = expr"
6966 // "@r = expr"
6967 if (*eap->cmd == '&'
6968 || *eap->cmd == '$'
6969 || *eap->cmd == '@'
6970 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006971 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006972 {
6973 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6974 if (*line == NULL || *line == eap->cmd)
6975 return FAIL;
6976 return OK;
6977 }
6978 }
6979 }
6980
6981 if (*eap->cmd == '[')
6982 {
6983 // [var, var] = expr
6984 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6985 if (*line == NULL)
6986 return FAIL;
6987 if (*line != eap->cmd)
6988 return OK;
6989 }
6990 return NOTDONE;
6991}
6992
6993/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006994 * Check if "name" can be "unlet".
6995 */
6996 int
6997check_vim9_unlet(char_u *name)
6998{
6999 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7000 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007001 // "unlet s:var" is allowed in legacy script.
7002 if (*name == 's' && !script_is_vim9())
7003 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007004 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007005 return FAIL;
7006 }
7007 return OK;
7008}
7009
7010/*
7011 * Callback passed to ex_unletlock().
7012 */
7013 static int
7014compile_unlet(
7015 lval_T *lvp,
7016 char_u *name_end,
7017 exarg_T *eap,
7018 int deep UNUSED,
7019 void *coookie)
7020{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007021 cctx_T *cctx = coookie;
7022 char_u *p = lvp->ll_name;
7023 int cc = *name_end;
7024 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007025
Bram Moolenaar752fc692021-01-04 21:57:11 +01007026 if (cctx->ctx_skip == SKIP_YES)
7027 return OK;
7028
7029 *name_end = NUL;
7030 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007031 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007032 // :unlet $ENV_VAR
7033 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7034 }
7035 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7036 {
7037 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007038
Bram Moolenaar752fc692021-01-04 21:57:11 +01007039 // This is similar to assigning: lookup the list/dict, compile the
7040 // idx/key. Then instead of storing the value unlet the item.
7041 // unlet {list}[idx]
7042 // unlet {dict}[key] dict.key
7043 //
7044 // Figure out the LHS type and other properties.
7045 //
7046 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7047
7048 // : unlet an indexed item
7049 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007050 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007051 iemsg("called compile_lhs() without an index");
7052 ret = FAIL;
7053 }
7054 else
7055 {
7056 // Use the info in "lhs" to unlet the item at the index in the
7057 // list or dict.
7058 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007059 }
7060
Bram Moolenaar752fc692021-01-04 21:57:11 +01007061 vim_free(lhs.lhs_name);
7062 }
7063 else if (check_vim9_unlet(p) == FAIL)
7064 {
7065 ret = FAIL;
7066 }
7067 else
7068 {
7069 // Normal name. Only supports g:, w:, t: and b: namespaces.
7070 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007071 }
7072
Bram Moolenaar752fc692021-01-04 21:57:11 +01007073 *name_end = cc;
7074 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007075}
7076
7077/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007078 * Callback passed to ex_unletlock().
7079 */
7080 static int
7081compile_lock_unlock(
7082 lval_T *lvp,
7083 char_u *name_end,
7084 exarg_T *eap,
7085 int deep UNUSED,
7086 void *coookie)
7087{
7088 cctx_T *cctx = coookie;
7089 int cc = *name_end;
7090 char_u *p = lvp->ll_name;
7091 int ret = OK;
7092 size_t len;
7093 char_u *buf;
7094
7095 if (cctx->ctx_skip == SKIP_YES)
7096 return OK;
7097
7098 // Cannot use :lockvar and :unlockvar on local variables.
7099 if (p[1] != ':')
7100 {
7101 char_u *end = skip_var_one(p, FALSE);
7102
7103 if (lookup_local(p, end - p, NULL, cctx) == OK)
7104 {
7105 emsg(_(e_cannot_lock_unlock_local_variable));
7106 return FAIL;
7107 }
7108 }
7109
7110 // Checking is done at runtime.
7111 *name_end = NUL;
7112 len = name_end - p + 20;
7113 buf = alloc(len);
7114 if (buf == NULL)
7115 ret = FAIL;
7116 else
7117 {
7118 vim_snprintf((char *)buf, len, "%s %s",
7119 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7120 p);
7121 ret = generate_EXEC(cctx, buf);
7122
7123 vim_free(buf);
7124 *name_end = cc;
7125 }
7126 return ret;
7127}
7128
7129/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007130 * compile "unlet var", "lock var" and "unlock var"
7131 * "arg" points to "var".
7132 */
7133 static char_u *
7134compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7135{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007136 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7137 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7138 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007139 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7140}
7141
7142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 * Compile an :import command.
7144 */
7145 static char_u *
7146compile_import(char_u *arg, cctx_T *cctx)
7147{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007148 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149}
7150
7151/*
7152 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7153 */
7154 static int
7155compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7156{
7157 garray_T *instr = &cctx->ctx_instr;
7158 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7159
7160 if (endlabel == NULL)
7161 return FAIL;
7162 endlabel->el_next = *el;
7163 *el = endlabel;
7164 endlabel->el_end_label = instr->ga_len;
7165
7166 generate_JUMP(cctx, when, 0);
7167 return OK;
7168}
7169
7170 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007171compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007172{
7173 garray_T *instr = &cctx->ctx_instr;
7174
7175 while (*el != NULL)
7176 {
7177 endlabel_T *cur = (*el);
7178 isn_T *isn;
7179
7180 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007181 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007182 *el = cur->el_next;
7183 vim_free(cur);
7184 }
7185}
7186
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007187 static void
7188compile_free_jump_to_end(endlabel_T **el)
7189{
7190 while (*el != NULL)
7191 {
7192 endlabel_T *cur = (*el);
7193
7194 *el = cur->el_next;
7195 vim_free(cur);
7196 }
7197}
7198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007199/*
7200 * Create a new scope and set up the generic items.
7201 */
7202 static scope_T *
7203new_scope(cctx_T *cctx, scopetype_T type)
7204{
7205 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7206
7207 if (scope == NULL)
7208 return NULL;
7209 scope->se_outer = cctx->ctx_scope;
7210 cctx->ctx_scope = scope;
7211 scope->se_type = type;
7212 scope->se_local_count = cctx->ctx_locals.ga_len;
7213 return scope;
7214}
7215
7216/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007217 * Free the current scope and go back to the outer scope.
7218 */
7219 static void
7220drop_scope(cctx_T *cctx)
7221{
7222 scope_T *scope = cctx->ctx_scope;
7223
7224 if (scope == NULL)
7225 {
7226 iemsg("calling drop_scope() without a scope");
7227 return;
7228 }
7229 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007230 switch (scope->se_type)
7231 {
7232 case IF_SCOPE:
7233 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7234 case FOR_SCOPE:
7235 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7236 case WHILE_SCOPE:
7237 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7238 case TRY_SCOPE:
7239 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7240 case NO_SCOPE:
7241 case BLOCK_SCOPE:
7242 break;
7243 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007244 vim_free(scope);
7245}
7246
7247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007248 * compile "if expr"
7249 *
7250 * "if expr" Produces instructions:
7251 * EVAL expr Push result of "expr"
7252 * JUMP_IF_FALSE end
7253 * ... body ...
7254 * end:
7255 *
7256 * "if expr | else" Produces instructions:
7257 * EVAL expr Push result of "expr"
7258 * JUMP_IF_FALSE else
7259 * ... body ...
7260 * JUMP_ALWAYS end
7261 * else:
7262 * ... body ...
7263 * end:
7264 *
7265 * "if expr1 | elseif expr2 | else" Produces instructions:
7266 * EVAL expr Push result of "expr"
7267 * JUMP_IF_FALSE elseif
7268 * ... body ...
7269 * JUMP_ALWAYS end
7270 * elseif:
7271 * EVAL expr Push result of "expr"
7272 * JUMP_IF_FALSE else
7273 * ... body ...
7274 * JUMP_ALWAYS end
7275 * else:
7276 * ... body ...
7277 * end:
7278 */
7279 static char_u *
7280compile_if(char_u *arg, cctx_T *cctx)
7281{
7282 char_u *p = arg;
7283 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007284 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007285 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007286 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007287 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007288
Bram Moolenaara5565e42020-05-09 15:44:01 +02007289 CLEAR_FIELD(ppconst);
7290 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007291 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007292 clear_ppconst(&ppconst);
7293 return NULL;
7294 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007295 if (!ends_excmd2(arg, skipwhite(p)))
7296 {
7297 semsg(_(e_trailing_arg), p);
7298 return NULL;
7299 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007300 if (cctx->ctx_skip == SKIP_YES)
7301 clear_ppconst(&ppconst);
7302 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007303 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007304 int error = FALSE;
7305 int v;
7306
Bram Moolenaara5565e42020-05-09 15:44:01 +02007307 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007308 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007309 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007310 if (error)
7311 return NULL;
7312 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007313 }
7314 else
7315 {
7316 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007317 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007318 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007319 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007320 if (bool_on_stack(cctx) == FAIL)
7321 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007322 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323
Bram Moolenaara91a7132021-03-25 21:12:15 +01007324 // CMDMOD_REV must come before the jump
7325 generate_undo_cmdmods(cctx);
7326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007327 scope = new_scope(cctx, IF_SCOPE);
7328 if (scope == NULL)
7329 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007330 scope->se_skip_save = skip_save;
7331 // "is_had_return" will be reset if any block does not end in :return
7332 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007333
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007334 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007335 {
7336 // "where" is set when ":elseif", "else" or ":endif" is found
7337 scope->se_u.se_if.is_if_label = instr->ga_len;
7338 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7339 }
7340 else
7341 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007342
Bram Moolenaarced68a02021-01-24 17:53:47 +01007343#ifdef FEAT_PROFILE
7344 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7345 && skip_save != SKIP_YES)
7346 {
7347 // generated a profile start, need to generate a profile end, since it
7348 // won't be done after returning
7349 cctx->ctx_skip = SKIP_NOT;
7350 generate_instr(cctx, ISN_PROF_END);
7351 cctx->ctx_skip = SKIP_YES;
7352 }
7353#endif
7354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355 return p;
7356}
7357
7358 static char_u *
7359compile_elseif(char_u *arg, cctx_T *cctx)
7360{
7361 char_u *p = arg;
7362 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007363 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364 isn_T *isn;
7365 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007366 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007367 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368
7369 if (scope == NULL || scope->se_type != IF_SCOPE)
7370 {
7371 emsg(_(e_elseif_without_if));
7372 return NULL;
7373 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007374 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007375 if (!cctx->ctx_had_return)
7376 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377
Bram Moolenaarced68a02021-01-24 17:53:47 +01007378 if (cctx->ctx_skip == SKIP_NOT)
7379 {
7380 // previous block was executed, this one and following will not
7381 cctx->ctx_skip = SKIP_YES;
7382 scope->se_u.se_if.is_seen_skip_not = TRUE;
7383 }
7384 if (scope->se_u.se_if.is_seen_skip_not)
7385 {
7386 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007387 // Do not count the "elseif" for profiling and cmdmod
7388 instr->ga_len = current_instr_idx(cctx);
7389
Bram Moolenaarced68a02021-01-24 17:53:47 +01007390 skip_expr_cctx(&p, cctx);
7391 return p;
7392 }
7393
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007394 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007395 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007396 int moved_cmdmod = FALSE;
7397
7398 // Move any CMDMOD instruction to after the jump
7399 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7400 {
7401 if (ga_grow(instr, 1) == FAIL)
7402 return NULL;
7403 ((isn_T *)instr->ga_data)[instr->ga_len] =
7404 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7405 --instr->ga_len;
7406 moved_cmdmod = TRUE;
7407 }
7408
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007409 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007411 return NULL;
7412 // previous "if" or "elseif" jumps here
7413 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7414 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007415 if (moved_cmdmod)
7416 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007417 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007418
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007419 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007420 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007421 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007422 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007423 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007424#ifdef FEAT_PROFILE
7425 if (cctx->ctx_profiling)
7426 {
7427 // the previous block was skipped, need to profile this line
7428 generate_instr(cctx, ISN_PROF_START);
7429 instr_count = instr->ga_len;
7430 }
7431#endif
7432 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007433 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007434 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007435 clear_ppconst(&ppconst);
7436 return NULL;
7437 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007438 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007439 if (!ends_excmd2(arg, skipwhite(p)))
7440 {
7441 semsg(_(e_trailing_arg), p);
7442 return NULL;
7443 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007444 if (scope->se_skip_save == SKIP_YES)
7445 clear_ppconst(&ppconst);
7446 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007447 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007448 int error = FALSE;
7449 int v;
7450
Bram Moolenaar7f141552020-05-09 17:35:53 +02007451 // The expression results in a constant.
7452 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007453 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7454 if (error)
7455 return NULL;
7456 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007457 clear_ppconst(&ppconst);
7458 scope->se_u.se_if.is_if_label = -1;
7459 }
7460 else
7461 {
7462 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007463 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007464 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007465 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007466 if (bool_on_stack(cctx) == FAIL)
7467 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468
Bram Moolenaara91a7132021-03-25 21:12:15 +01007469 // CMDMOD_REV must come before the jump
7470 generate_undo_cmdmods(cctx);
7471
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007472 // "where" is set when ":elseif", "else" or ":endif" is found
7473 scope->se_u.se_if.is_if_label = instr->ga_len;
7474 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7475 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476
7477 return p;
7478}
7479
7480 static char_u *
7481compile_else(char_u *arg, cctx_T *cctx)
7482{
7483 char_u *p = arg;
7484 garray_T *instr = &cctx->ctx_instr;
7485 isn_T *isn;
7486 scope_T *scope = cctx->ctx_scope;
7487
7488 if (scope == NULL || scope->se_type != IF_SCOPE)
7489 {
7490 emsg(_(e_else_without_if));
7491 return NULL;
7492 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007493 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007494 if (!cctx->ctx_had_return)
7495 scope->se_u.se_if.is_had_return = FALSE;
7496 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007497
Bram Moolenaarced68a02021-01-24 17:53:47 +01007498#ifdef FEAT_PROFILE
7499 if (cctx->ctx_profiling)
7500 {
7501 if (cctx->ctx_skip == SKIP_NOT
7502 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7503 .isn_type == ISN_PROF_START)
7504 // the previous block was executed, do not count "else" for profiling
7505 --instr->ga_len;
7506 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7507 {
7508 // the previous block was not executed, this one will, do count the
7509 // "else" for profiling
7510 cctx->ctx_skip = SKIP_NOT;
7511 generate_instr(cctx, ISN_PROF_END);
7512 generate_instr(cctx, ISN_PROF_START);
7513 cctx->ctx_skip = SKIP_YES;
7514 }
7515 }
7516#endif
7517
7518 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007519 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007520 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007521 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007522 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007523 if (!cctx->ctx_had_return
7524 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7525 JUMP_ALWAYS, cctx) == FAIL)
7526 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007527 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007528
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007529 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007530 {
7531 if (scope->se_u.se_if.is_if_label >= 0)
7532 {
7533 // previous "if" or "elseif" jumps here
7534 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7535 isn->isn_arg.jump.jump_where = instr->ga_len;
7536 scope->se_u.se_if.is_if_label = -1;
7537 }
7538 }
7539
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007540 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007541 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007543
7544 return p;
7545}
7546
7547 static char_u *
7548compile_endif(char_u *arg, cctx_T *cctx)
7549{
7550 scope_T *scope = cctx->ctx_scope;
7551 ifscope_T *ifscope;
7552 garray_T *instr = &cctx->ctx_instr;
7553 isn_T *isn;
7554
Bram Moolenaarfa984412021-03-25 22:15:28 +01007555 if (misplaced_cmdmod(cctx))
7556 return NULL;
7557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007558 if (scope == NULL || scope->se_type != IF_SCOPE)
7559 {
7560 emsg(_(e_endif_without_if));
7561 return NULL;
7562 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007563 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007564 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007565 if (!cctx->ctx_had_return)
7566 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007568 if (scope->se_u.se_if.is_if_label >= 0)
7569 {
7570 // previous "if" or "elseif" jumps here
7571 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7572 isn->isn_arg.jump.jump_where = instr->ga_len;
7573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007575 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007576
7577#ifdef FEAT_PROFILE
7578 // even when skipping we count the endif as executed, unless the block it's
7579 // in is skipped
7580 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7581 && scope->se_skip_save != SKIP_YES)
7582 {
7583 cctx->ctx_skip = SKIP_NOT;
7584 generate_instr(cctx, ISN_PROF_START);
7585 }
7586#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007587 cctx->ctx_skip = scope->se_skip_save;
7588
7589 // If all the blocks end in :return and there is an :else then the
7590 // had_return flag is set.
7591 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007592
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007593 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594 return arg;
7595}
7596
7597/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007598 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007599 *
7600 * Produces instructions:
7601 * PUSHNR -1
7602 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007603 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7605 * - if beyond end, jump to "end"
7606 * - otherwise get item from list and push it
7607 * STORE var Store item in "var"
7608 * ... body ...
7609 * JUMP top Jump back to repeat
7610 * end: DROP Drop the result of "expr"
7611 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007612 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7613 * UNPACK 2 Split item in 2
7614 * STORE var1 Store item in "var1"
7615 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007616 */
7617 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007618compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007619{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007620 char_u *arg;
7621 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007622 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007623 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007624 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007625 int var_count = 0;
7626 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007628 garray_T *stack = &cctx->ctx_type_stack;
7629 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007630 lvar_T *loop_lvar; // loop iteration variable
7631 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007632 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007633 type_T *item_type = &t_any;
7634 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635
Bram Moolenaar792f7862020-11-23 08:31:18 +01007636 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007637 if (p == NULL)
7638 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007639 if (var_count == 0)
7640 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007641
7642 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007643 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007644 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7645 return NULL;
7646 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007647 {
7648 emsg(_(e_missing_in));
7649 return NULL;
7650 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007651 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007652 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7653 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007655 scope = new_scope(cctx, FOR_SCOPE);
7656 if (scope == NULL)
7657 return NULL;
7658
Bram Moolenaar792f7862020-11-23 08:31:18 +01007659 // Reserve a variable to store the loop iteration counter and initialize it
7660 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007661 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7662 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007663 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007664 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007665 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007666 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007667 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007668 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007669
7670 // compile "expr", it remains on the stack until "endfor"
7671 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007672 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007673 {
7674 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007675 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007676 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007677 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007678
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007679 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007680 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007682 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007683 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007685 semsg(_(e_for_loop_on_str_not_supported),
7686 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007687 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007688 return NULL;
7689 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007690
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007691 if (vartype->tt_type == VAR_STRING)
7692 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007693 else if (vartype->tt_type == VAR_BLOB)
7694 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007695 else if (vartype->tt_type == VAR_LIST
7696 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007697 {
7698 if (var_count == 1)
7699 item_type = vartype->tt_member;
7700 else if (vartype->tt_member->tt_type == VAR_LIST
7701 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007702 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007703 item_type = vartype->tt_member->tt_member;
7704 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705
Bram Moolenaara91a7132021-03-25 21:12:15 +01007706 // CMDMOD_REV must come before the FOR instruction
7707 generate_undo_cmdmods(cctx);
7708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007709 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007710 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007711 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007712
Bram Moolenaar792f7862020-11-23 08:31:18 +01007713 arg = arg_start;
7714 if (var_count > 1)
7715 {
7716 generate_UNPACK(cctx, var_count, semicolon);
7717 arg = skipwhite(arg + 1); // skip white after '['
7718
7719 // the list item is replaced by a number of items
7720 if (ga_grow(stack, var_count - 1) == FAIL)
7721 {
7722 drop_scope(cctx);
7723 return NULL;
7724 }
7725 --stack->ga_len;
7726 for (idx = 0; idx < var_count; ++idx)
7727 {
7728 ((type_T **)stack->ga_data)[stack->ga_len] =
7729 (semicolon && idx == 0) ? vartype : item_type;
7730 ++stack->ga_len;
7731 }
7732 }
7733
7734 for (idx = 0; idx < var_count; ++idx)
7735 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007736 assign_dest_T dest = dest_local;
7737 int opt_flags = 0;
7738 int vimvaridx = -1;
7739 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007740 type_T *lhs_type = &t_any;
7741 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007742
7743 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007744 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007745 name = vim_strnsave(arg, varlen);
7746 if (name == NULL)
7747 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007748 if (*p == ':')
7749 {
7750 p = skipwhite(p + 1);
7751 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7752 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007753
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007754 // TODO: script var not supported?
7755 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7756 &vimvaridx, &type, cctx) == FAIL)
7757 goto failed;
7758 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007759 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007760 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7761 0, 0, type, name) == FAIL)
7762 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007763 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007764 else if (varlen == 1 && *arg == '_')
7765 {
7766 // Assigning to "_": drop the value.
7767 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7768 goto failed;
7769 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007770 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007771 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007772 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007773 {
7774 semsg(_(e_variable_already_declared), arg);
7775 goto failed;
7776 }
7777
Bram Moolenaarea870692020-12-02 14:24:30 +01007778 if (STRNCMP(name, "s:", 2) == 0)
7779 {
7780 semsg(_(e_cannot_declare_script_variable_in_function), name);
7781 goto failed;
7782 }
7783
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007784 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007785 where.wt_index = var_count > 1 ? idx + 1 : 0;
7786 where.wt_variable = TRUE;
7787 if (lhs_type == &t_any)
7788 lhs_type = item_type;
7789 else if (item_type != &t_unknown
7790 && !(var_count > 1 && item_type == &t_any)
7791 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7792 goto failed;
7793 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007794 if (var_lvar == NULL)
7795 // out of memory or used as an argument
7796 goto failed;
7797
7798 if (semicolon && idx == var_count - 1)
7799 var_lvar->lv_type = vartype;
7800 else
7801 var_lvar->lv_type = item_type;
7802 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7803 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007804
7805 if (*p == ',' || *p == ';')
7806 ++p;
7807 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007808 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007809 }
7810
7811 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007812
7813failed:
7814 vim_free(name);
7815 drop_scope(cctx);
7816 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007817}
7818
7819/*
7820 * compile "endfor"
7821 */
7822 static char_u *
7823compile_endfor(char_u *arg, cctx_T *cctx)
7824{
7825 garray_T *instr = &cctx->ctx_instr;
7826 scope_T *scope = cctx->ctx_scope;
7827 forscope_T *forscope;
7828 isn_T *isn;
7829
Bram Moolenaarfa984412021-03-25 22:15:28 +01007830 if (misplaced_cmdmod(cctx))
7831 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007833 if (scope == NULL || scope->se_type != FOR_SCOPE)
7834 {
7835 emsg(_(e_for));
7836 return NULL;
7837 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007838 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007839 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007840 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007841
7842 // At end of ":for" scope jump back to the FOR instruction.
7843 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7844
7845 // Fill in the "end" label in the FOR statement so it can jump here
7846 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7847 isn->isn_arg.forloop.for_end = instr->ga_len;
7848
7849 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007850 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007851
7852 // Below the ":for" scope drop the "expr" list from the stack.
7853 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7854 return NULL;
7855
7856 vim_free(scope);
7857
7858 return arg;
7859}
7860
7861/*
7862 * compile "while expr"
7863 *
7864 * Produces instructions:
7865 * top: EVAL expr Push result of "expr"
7866 * JUMP_IF_FALSE end jump if false
7867 * ... body ...
7868 * JUMP top Jump back to repeat
7869 * end:
7870 *
7871 */
7872 static char_u *
7873compile_while(char_u *arg, cctx_T *cctx)
7874{
7875 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007876 scope_T *scope;
7877
7878 scope = new_scope(cctx, WHILE_SCOPE);
7879 if (scope == NULL)
7880 return NULL;
7881
Bram Moolenaara91a7132021-03-25 21:12:15 +01007882 // "endwhile" jumps back here, one before when profiling or using cmdmods
7883 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007884
7885 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007886 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007887 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007888 if (!ends_excmd2(arg, skipwhite(p)))
7889 {
7890 semsg(_(e_trailing_arg), p);
7891 return NULL;
7892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007893
Bram Moolenaar13106602020-10-04 16:06:05 +02007894 if (bool_on_stack(cctx) == FAIL)
7895 return FAIL;
7896
Bram Moolenaara91a7132021-03-25 21:12:15 +01007897 // CMDMOD_REV must come before the jump
7898 generate_undo_cmdmods(cctx);
7899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007900 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007901 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007902 JUMP_IF_FALSE, cctx) == FAIL)
7903 return FAIL;
7904
7905 return p;
7906}
7907
7908/*
7909 * compile "endwhile"
7910 */
7911 static char_u *
7912compile_endwhile(char_u *arg, cctx_T *cctx)
7913{
7914 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007915 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007916
Bram Moolenaarfa984412021-03-25 22:15:28 +01007917 if (misplaced_cmdmod(cctx))
7918 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007919 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7920 {
7921 emsg(_(e_while));
7922 return NULL;
7923 }
7924 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007925 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926
Bram Moolenaarf002a412021-01-24 13:34:18 +01007927#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007928 // count the endwhile before jumping
7929 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007930#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007933 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007934
7935 // Fill in the "end" label in the WHILE statement so it can jump here.
7936 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007937 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7938 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007939
7940 vim_free(scope);
7941
7942 return arg;
7943}
7944
7945/*
7946 * compile "continue"
7947 */
7948 static char_u *
7949compile_continue(char_u *arg, cctx_T *cctx)
7950{
7951 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007952 int try_scopes = 0;
7953 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954
7955 for (;;)
7956 {
7957 if (scope == NULL)
7958 {
7959 emsg(_(e_continue));
7960 return NULL;
7961 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007962 if (scope->se_type == FOR_SCOPE)
7963 {
7964 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007965 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007966 }
7967 if (scope->se_type == WHILE_SCOPE)
7968 {
7969 loop_label = scope->se_u.se_while.ws_top_label;
7970 break;
7971 }
7972 if (scope->se_type == TRY_SCOPE)
7973 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007974 scope = scope->se_outer;
7975 }
7976
Bram Moolenaarc150c092021-02-13 15:02:46 +01007977 if (try_scopes > 0)
7978 // Inside one or more try/catch blocks we first need to jump to the
7979 // "finally" or "endtry" to cleanup.
7980 generate_TRYCONT(cctx, try_scopes, loop_label);
7981 else
7982 // Jump back to the FOR or WHILE instruction.
7983 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007985 return arg;
7986}
7987
7988/*
7989 * compile "break"
7990 */
7991 static char_u *
7992compile_break(char_u *arg, cctx_T *cctx)
7993{
7994 scope_T *scope = cctx->ctx_scope;
7995 endlabel_T **el;
7996
7997 for (;;)
7998 {
7999 if (scope == NULL)
8000 {
8001 emsg(_(e_break));
8002 return NULL;
8003 }
8004 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8005 break;
8006 scope = scope->se_outer;
8007 }
8008
8009 // Jump to the end of the FOR or WHILE loop.
8010 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008011 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008012 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008013 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008014 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8015 return FAIL;
8016
8017 return arg;
8018}
8019
8020/*
8021 * compile "{" start of block
8022 */
8023 static char_u *
8024compile_block(char_u *arg, cctx_T *cctx)
8025{
8026 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8027 return NULL;
8028 return skipwhite(arg + 1);
8029}
8030
8031/*
8032 * compile end of block: drop one scope
8033 */
8034 static void
8035compile_endblock(cctx_T *cctx)
8036{
8037 scope_T *scope = cctx->ctx_scope;
8038
8039 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008040 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008041 vim_free(scope);
8042}
8043
8044/*
8045 * compile "try"
8046 * Creates a new scope for the try-endtry, pointing to the first catch and
8047 * finally.
8048 * Creates another scope for the "try" block itself.
8049 * TRY instruction sets up exception handling at runtime.
8050 *
8051 * "try"
8052 * TRY -> catch1, -> finally push trystack entry
8053 * ... try block
8054 * "throw {exception}"
8055 * EVAL {exception}
8056 * THROW create exception
8057 * ... try block
8058 * " catch {expr}"
8059 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008060 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008061 * EVAL {expr}
8062 * MATCH
8063 * JUMP nomatch -> catch2
8064 * CATCH remove exception
8065 * ... catch block
8066 * " catch"
8067 * JUMP -> finally
8068 * catch2: CATCH remove exception
8069 * ... catch block
8070 * " finally"
8071 * finally:
8072 * ... finally block
8073 * " endtry"
8074 * ENDTRY pop trystack entry, may rethrow
8075 */
8076 static char_u *
8077compile_try(char_u *arg, cctx_T *cctx)
8078{
8079 garray_T *instr = &cctx->ctx_instr;
8080 scope_T *try_scope;
8081 scope_T *scope;
8082
Bram Moolenaarfa984412021-03-25 22:15:28 +01008083 if (misplaced_cmdmod(cctx))
8084 return NULL;
8085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008086 // scope that holds the jumps that go to catch/finally/endtry
8087 try_scope = new_scope(cctx, TRY_SCOPE);
8088 if (try_scope == NULL)
8089 return NULL;
8090
Bram Moolenaar69f70502021-01-01 16:10:46 +01008091 if (cctx->ctx_skip != SKIP_YES)
8092 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008093 isn_T *isn;
8094
8095 // "try_catch" is set when the first ":catch" is found or when no catch
8096 // is found and ":finally" is found.
8097 // "try_finally" is set when ":finally" is found
8098 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008099 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008100 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8101 return NULL;
8102 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8103 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008104 return NULL;
8105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008106
8107 // scope for the try block itself
8108 scope = new_scope(cctx, BLOCK_SCOPE);
8109 if (scope == NULL)
8110 return NULL;
8111
8112 return arg;
8113}
8114
8115/*
8116 * compile "catch {expr}"
8117 */
8118 static char_u *
8119compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8120{
8121 scope_T *scope = cctx->ctx_scope;
8122 garray_T *instr = &cctx->ctx_instr;
8123 char_u *p;
8124 isn_T *isn;
8125
Bram Moolenaarfa984412021-03-25 22:15:28 +01008126 if (misplaced_cmdmod(cctx))
8127 return NULL;
8128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129 // end block scope from :try or :catch
8130 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8131 compile_endblock(cctx);
8132 scope = cctx->ctx_scope;
8133
8134 // Error if not in a :try scope
8135 if (scope == NULL || scope->se_type != TRY_SCOPE)
8136 {
8137 emsg(_(e_catch));
8138 return NULL;
8139 }
8140
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008141 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008142 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008143 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008144 return NULL;
8145 }
8146
Bram Moolenaar69f70502021-01-01 16:10:46 +01008147 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008148 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008149#ifdef FEAT_PROFILE
8150 // the profile-start should be after the jump
8151 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8152 .isn_type == ISN_PROF_START)
8153 --instr->ga_len;
8154#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008155 // Jump from end of previous block to :finally or :endtry
8156 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8157 JUMP_ALWAYS, cctx) == FAIL)
8158 return NULL;
8159
8160 // End :try or :catch scope: set value in ISN_TRY instruction
8161 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008162 if (isn->isn_arg.try.try_ref->try_catch == 0)
8163 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008164 if (scope->se_u.se_try.ts_catch_label != 0)
8165 {
8166 // Previous catch without match jumps here
8167 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8168 isn->isn_arg.jump.jump_where = instr->ga_len;
8169 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008170#ifdef FEAT_PROFILE
8171 if (cctx->ctx_profiling)
8172 {
8173 // a "throw" that jumps here needs to be counted
8174 generate_instr(cctx, ISN_PROF_END);
8175 // the "catch" is also counted
8176 generate_instr(cctx, ISN_PROF_START);
8177 }
8178#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008179 }
8180
8181 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008182 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008183 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008184 scope->se_u.se_try.ts_caught_all = TRUE;
8185 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008186 }
8187 else
8188 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008189 char_u *end;
8190 char_u *pat;
8191 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008192 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008193 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008195 // Push v:exception, push {expr} and MATCH
8196 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8197
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008198 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008199 if (*end != *p)
8200 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008201 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008202 vim_free(tofree);
8203 return FAIL;
8204 }
8205 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008206 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008207 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008208 len = (int)(end - tofree);
8209 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008210 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008211 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008212 if (pat == NULL)
8213 return FAIL;
8214 if (generate_PUSHS(cctx, pat) == FAIL)
8215 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008217 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8218 return NULL;
8219
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008220 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008221 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8222 return NULL;
8223 }
8224
Bram Moolenaar69f70502021-01-01 16:10:46 +01008225 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008226 return NULL;
8227
8228 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8229 return NULL;
8230 return p;
8231}
8232
8233 static char_u *
8234compile_finally(char_u *arg, cctx_T *cctx)
8235{
8236 scope_T *scope = cctx->ctx_scope;
8237 garray_T *instr = &cctx->ctx_instr;
8238 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008239 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008240
Bram Moolenaarfa984412021-03-25 22:15:28 +01008241 if (misplaced_cmdmod(cctx))
8242 return NULL;
8243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008244 // end block scope from :try or :catch
8245 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8246 compile_endblock(cctx);
8247 scope = cctx->ctx_scope;
8248
8249 // Error if not in a :try scope
8250 if (scope == NULL || scope->se_type != TRY_SCOPE)
8251 {
8252 emsg(_(e_finally));
8253 return NULL;
8254 }
8255
8256 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008257 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008258 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008259 {
8260 emsg(_(e_finally_dup));
8261 return NULL;
8262 }
8263
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008264 this_instr = instr->ga_len;
8265#ifdef FEAT_PROFILE
8266 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8267 .isn_type == ISN_PROF_START)
8268 // jump to the profile start of the "finally"
8269 --this_instr;
8270#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008271
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008272 // Fill in the "end" label in jumps at the end of the blocks.
8273 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8274 this_instr, cctx);
8275
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008276 // If there is no :catch then an exception jumps to :finally.
8277 if (isn->isn_arg.try.try_ref->try_catch == 0)
8278 isn->isn_arg.try.try_ref->try_catch = this_instr;
8279 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008280 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008281 {
8282 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008283 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008284 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008285 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008286 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008287 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8288 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008290 // TODO: set index in ts_finally_label jumps
8291
8292 return arg;
8293}
8294
8295 static char_u *
8296compile_endtry(char_u *arg, cctx_T *cctx)
8297{
8298 scope_T *scope = cctx->ctx_scope;
8299 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008300 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008301
Bram Moolenaarfa984412021-03-25 22:15:28 +01008302 if (misplaced_cmdmod(cctx))
8303 return NULL;
8304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008305 // end block scope from :catch or :finally
8306 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8307 compile_endblock(cctx);
8308 scope = cctx->ctx_scope;
8309
8310 // Error if not in a :try scope
8311 if (scope == NULL || scope->se_type != TRY_SCOPE)
8312 {
8313 if (scope == NULL)
8314 emsg(_(e_no_endtry));
8315 else if (scope->se_type == WHILE_SCOPE)
8316 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008317 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008318 emsg(_(e_endfor));
8319 else
8320 emsg(_(e_endif));
8321 return NULL;
8322 }
8323
Bram Moolenaarc150c092021-02-13 15:02:46 +01008324 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008325 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008326 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008327 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8328 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008329 {
8330 emsg(_(e_missing_catch_or_finally));
8331 return NULL;
8332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008333
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008334#ifdef FEAT_PROFILE
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008335 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8336 .isn_type == ISN_PROF_START)
8337 // move the profile start after "endtry" so that it's not counted when
8338 // the exception is rethrown.
8339 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008340#endif
8341
Bram Moolenaar69f70502021-01-01 16:10:46 +01008342 // Fill in the "end" label in jumps at the end of the blocks, if not
8343 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008344 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8345 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008346
Bram Moolenaar69f70502021-01-01 16:10:46 +01008347 if (scope->se_u.se_try.ts_catch_label != 0)
8348 {
8349 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008350 isn_T *isn = ((isn_T *)instr->ga_data)
8351 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008352 isn->isn_arg.jump.jump_where = instr->ga_len;
8353 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008354 }
8355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008356 compile_endblock(cctx);
8357
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008358 if (cctx->ctx_skip != SKIP_YES)
8359 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008360 // End :catch or :finally scope: set instruction index in ISN_TRY
8361 // instruction
8362 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008363 if (cctx->ctx_skip != SKIP_YES
8364 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8365 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008366#ifdef FEAT_PROFILE
8367 if (cctx->ctx_profiling)
8368 generate_instr(cctx, ISN_PROF_START);
8369#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008371 return arg;
8372}
8373
8374/*
8375 * compile "throw {expr}"
8376 */
8377 static char_u *
8378compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8379{
8380 char_u *p = skipwhite(arg);
8381
Bram Moolenaara5565e42020-05-09 15:44:01 +02008382 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008383 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008384 if (cctx->ctx_skip == SKIP_YES)
8385 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008386 if (may_generate_2STRING(-1, cctx) == FAIL)
8387 return NULL;
8388 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8389 return NULL;
8390
8391 return p;
8392}
8393
8394/*
8395 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008396 * compile "echomsg expr"
8397 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008398 * compile "execute expr"
8399 */
8400 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008401compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008402{
8403 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008404 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008405 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008406 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008407 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008408 garray_T *stack = &cctx->ctx_type_stack;
8409 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008410
8411 for (;;)
8412 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008413 if (ends_excmd2(prev, p))
8414 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008415 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008416 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008417 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008418
8419 if (cctx->ctx_skip != SKIP_YES)
8420 {
8421 // check for non-void type
8422 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8423 if (type->tt_type == VAR_VOID)
8424 {
8425 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8426 return NULL;
8427 }
8428 }
8429
Bram Moolenaarad39c092020-02-26 18:23:43 +01008430 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008431 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008432 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008433 }
8434
Bram Moolenaare4984292020-12-13 14:19:25 +01008435 if (count > 0)
8436 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008437 long save_lnum = cctx->ctx_lnum;
8438
8439 // Use the line number where the command started.
8440 cctx->ctx_lnum = start_ctx_lnum;
8441
Bram Moolenaare4984292020-12-13 14:19:25 +01008442 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8443 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8444 else if (cmdidx == CMD_execute)
8445 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8446 else if (cmdidx == CMD_echomsg)
8447 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8448 else
8449 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008450
8451 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008453 return p;
8454}
8455
8456/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008457 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008458 * instruction to compute it and return OK.
8459 * Otherwise return FAIL, the caller must deal with any range.
8460 */
8461 static int
8462compile_variable_range(exarg_T *eap, cctx_T *cctx)
8463{
8464 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8465 char_u *p = skipdigits(eap->cmd);
8466
8467 if (p == range_end)
8468 return FAIL;
8469 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8470}
8471
8472/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008473 * :put r
8474 * :put ={expr}
8475 */
8476 static char_u *
8477compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8478{
8479 char_u *line = arg;
8480 linenr_T lnum;
8481 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008482 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008483
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008484 eap->regname = *line;
8485
8486 if (eap->regname == '=')
8487 {
8488 char_u *p = line + 1;
8489
8490 if (compile_expr0(&p, cctx) == FAIL)
8491 return NULL;
8492 line = p;
8493 }
8494 else if (eap->regname != NUL)
8495 ++line;
8496
Bram Moolenaar08597872020-12-10 19:43:40 +01008497 if (compile_variable_range(eap, cctx) == OK)
8498 {
8499 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8500 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008501 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008502 {
8503 // Either no range or a number.
8504 // "errormsg" will not be set because the range is ADDR_LINES.
8505 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008506 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008507 return NULL;
8508 if (eap->addr_count == 0)
8509 lnum = -1;
8510 else
8511 lnum = eap->line2;
8512 if (above)
8513 --lnum;
8514 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008515
8516 generate_PUT(cctx, eap->regname, lnum);
8517 return line;
8518}
8519
8520/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008521 * A command that is not compiled, execute with legacy code.
8522 */
8523 static char_u *
8524compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8525{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008526 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008527 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008528 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008529
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008530 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008531 goto theend;
8532
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008533 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008534 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008535 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008536 int usefilter = FALSE;
8537
8538 has_expr = argt & (EX_XFILE | EX_EXPAND);
8539
8540 // If the command can be followed by a bar, find the bar and truncate
8541 // it, so that the following command can be compiled.
8542 // The '|' is overwritten with a NUL, it is put back below.
8543 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8544 && *eap->arg == '!')
8545 // :w !filter or :r !filter or :r! filter
8546 usefilter = TRUE;
8547 if ((argt & EX_TRLBAR) && !usefilter)
8548 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008549 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008550 separate_nextcmd(eap);
8551 if (eap->nextcmd != NULL)
8552 nextcmd = eap->nextcmd;
8553 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008554 else if (eap->cmdidx == CMD_wincmd)
8555 {
8556 p = eap->arg;
8557 if (*p != NUL)
8558 ++p;
8559 if (*p == 'g' || *p == Ctrl_G)
8560 ++p;
8561 p = skipwhite(p);
8562 if (*p == '|')
8563 {
8564 *p = NUL;
8565 nextcmd = p + 1;
8566 }
8567 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008568 }
8569
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008570 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8571 {
8572 // expand filename in "syntax include [@group] filename"
8573 has_expr = TRUE;
8574 eap->arg = skipwhite(eap->arg + 7);
8575 if (*eap->arg == '@')
8576 eap->arg = skiptowhite(eap->arg);
8577 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008578
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008579 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8580 && STRLEN(eap->arg) > 4)
8581 {
8582 int delim = *eap->arg;
8583
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008584 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008585 if (*p == delim)
8586 {
8587 eap->arg = p + 1;
8588 has_expr = TRUE;
8589 }
8590 }
8591
Bram Moolenaarecac5912021-01-05 19:23:28 +01008592 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8593 {
8594 // TODO: should only expand when appropriate for the command
8595 eap->arg = skiptowhite(eap->arg);
8596 has_expr = TRUE;
8597 }
8598
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008599 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008600 {
8601 int count = 0;
8602 char_u *start = skipwhite(line);
8603
8604 // :cmd xxx`=expr1`yyy`=expr2`zzz
8605 // PUSHS ":cmd xxx"
8606 // eval expr1
8607 // PUSHS "yyy"
8608 // eval expr2
8609 // PUSHS "zzz"
8610 // EXECCONCAT 5
8611 for (;;)
8612 {
8613 if (p > start)
8614 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008615 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008616 ++count;
8617 }
8618 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008619 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008620 return NULL;
8621 may_generate_2STRING(-1, cctx);
8622 ++count;
8623 p = skipwhite(p);
8624 if (*p != '`')
8625 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008626 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008627 return NULL;
8628 }
8629 start = p + 1;
8630
8631 p = (char_u *)strstr((char *)start, "`=");
8632 if (p == NULL)
8633 {
8634 if (*skipwhite(start) != NUL)
8635 {
8636 generate_PUSHS(cctx, vim_strsave(start));
8637 ++count;
8638 }
8639 break;
8640 }
8641 }
8642 generate_EXECCONCAT(cctx, count);
8643 }
8644 else
8645 generate_EXEC(cctx, line);
8646
8647theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008648 if (*nextcmd != NUL)
8649 {
8650 // the parser expects a pointer to the bar, put it back
8651 --nextcmd;
8652 *nextcmd = '|';
8653 }
8654
8655 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008656}
8657
Bram Moolenaar8238f082021-04-20 21:10:48 +02008658
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008659/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008660 * :s/pat/repl/
8661 */
8662 static char_u *
8663compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8664{
8665 char_u *cmd = eap->arg;
8666 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8667
8668 if (expr != NULL)
8669 {
8670 int delimiter = *cmd++;
8671
8672 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008673 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008674 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8675 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008676 garray_T save_ga = cctx->ctx_instr;
8677 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008678 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008679 int trailing_error;
8680 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008681 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008682 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008683
8684 cmd += 3;
8685 end = skip_substitute(cmd, delimiter);
8686
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008687 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008688 // labels are correct.
8689 cctx->ctx_instr.ga_len = 0;
8690 cctx->ctx_instr.ga_maxlen = 0;
8691 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008692 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008693 if (end[-1] == NUL)
8694 end[-1] = delimiter;
8695 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008696 trailing_error = *cmd != delimiter && *cmd != NUL;
8697
Bram Moolenaar169502f2021-04-21 12:19:35 +02008698 if (expr_res == FAIL || trailing_error
8699 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008700 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008701 if (trailing_error)
8702 semsg(_(e_trailing_arg), cmd);
8703 clear_instr_ga(&cctx->ctx_instr);
8704 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008705 return NULL;
8706 }
8707
Bram Moolenaar8238f082021-04-20 21:10:48 +02008708 // Move the generated instructions into the ISN_SUBSTITUTE
8709 // instructions, then restore the list of instructions before
8710 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008711 instr_count = cctx->ctx_instr.ga_len;
8712 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008713 instr[instr_count].isn_type = ISN_FINISH;
8714
8715 cctx->ctx_instr = save_ga;
8716 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8717 {
8718 int idx;
8719
8720 for (idx = 0; idx < instr_count; ++idx)
8721 delete_instr(instr + idx);
8722 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008723 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008724 }
8725 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8726 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008727
8728 // skip over flags
8729 if (*end == '&')
8730 ++end;
8731 while (ASCII_ISALPHA(*end) || *end == '#')
8732 ++end;
8733 return end;
8734 }
8735 }
8736
8737 return compile_exec(arg, eap, cctx);
8738}
8739
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008740 static char_u *
8741compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8742{
8743 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008744 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008745
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008746 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008747 {
8748 if (STRNCMP(arg, "END", 3) == 0)
8749 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008750 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008751 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008752 // First load the current variable value.
8753 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8754 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008755 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008756 }
8757
8758 // Gets the redirected text and put it on the stack, then store it
8759 // in the variable.
8760 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8761
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008762 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008763 generate_instr_drop(cctx, ISN_CONCAT, 1);
8764
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008765 if (lhs->lhs_has_index)
8766 {
8767 // Use the info in "lhs" to store the value at the index in the
8768 // list or dict.
8769 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8770 &t_string, cctx) == FAIL)
8771 return NULL;
8772 }
8773 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008774 return NULL;
8775
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008776 VIM_CLEAR(lhs->lhs_name);
8777 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008778 return arg + 3;
8779 }
8780 emsg(_(e_cannot_nest_redir));
8781 return NULL;
8782 }
8783
8784 if (arg[0] == '=' && arg[1] == '>')
8785 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008786 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008787
8788 // redirect to a variable is compiled
8789 arg += 2;
8790 if (*arg == '>')
8791 {
8792 ++arg;
8793 append = TRUE;
8794 }
8795 arg = skipwhite(arg);
8796
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008797 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008798 FALSE, FALSE, 1, cctx) == FAIL)
8799 return NULL;
8800 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008801 lhs->lhs_append = append;
8802 if (lhs->lhs_has_index)
8803 {
8804 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8805 if (lhs->lhs_whole == NULL)
8806 return NULL;
8807 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008808
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008809 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008810 }
8811
8812 // other redirects are handled like at script level
8813 return compile_exec(line, eap, cctx);
8814}
8815
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008816#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008817 static char_u *
8818compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8819{
8820 isn_T *isn;
8821 char_u *p;
8822
8823 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8824 if (isn == NULL)
8825 return NULL;
8826 isn->isn_arg.number = eap->cmdidx;
8827
8828 p = eap->arg;
8829 if (compile_expr0(&p, cctx) == FAIL)
8830 return NULL;
8831
8832 isn = generate_instr(cctx, ISN_CEXPR_CORE);
8833 if (isn == NULL)
8834 return NULL;
8835 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
8836 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
8837 return NULL;
8838 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
8839 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
8840 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
8841
8842 return p;
8843}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008844#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008845
Bram Moolenaar4c137212021-04-19 16:48:48 +02008846/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008847 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008848 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008849 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008850 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008851add_def_function(ufunc_T *ufunc)
8852{
8853 dfunc_T *dfunc;
8854
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008855 if (def_functions.ga_len == 0)
8856 {
8857 // The first position is not used, so that a zero uf_dfunc_idx means it
8858 // wasn't set.
8859 if (ga_grow(&def_functions, 1) == FAIL)
8860 return FAIL;
8861 ++def_functions.ga_len;
8862 }
8863
Bram Moolenaar09689a02020-05-09 22:50:08 +02008864 // Add the function to "def_functions".
8865 if (ga_grow(&def_functions, 1) == FAIL)
8866 return FAIL;
8867 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8868 CLEAR_POINTER(dfunc);
8869 dfunc->df_idx = def_functions.ga_len;
8870 ufunc->uf_dfunc_idx = dfunc->df_idx;
8871 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008872 dfunc->df_name = vim_strsave(ufunc->uf_name);
8873 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008874 ++def_functions.ga_len;
8875 return OK;
8876}
8877
8878/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008879 * After ex_function() has collected all the function lines: parse and compile
8880 * the lines into instructions.
8881 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008882 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8883 * the return statement (used for lambda). When uf_ret_type is already set
8884 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008885 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008886 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008887 * This can be used recursively through compile_lambda(), which may reallocate
8888 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008889 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008890 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008891 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008892compile_def_function(
8893 ufunc_T *ufunc,
8894 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008895 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008896 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008897{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008898 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008899 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008900 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008901 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008902 cctx_T cctx;
8903 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008904 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008905 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008906 int ret = FAIL;
8907 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008908 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02008909 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008910 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008911 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008912#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008913 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008914#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008915
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008916 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008917 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008918 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008919 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008920 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8921 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008922 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008923 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008924 else
8925 {
8926 if (add_def_function(ufunc) == FAIL)
8927 return FAIL;
8928 new_def_function = TRUE;
8929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008930
Bram Moolenaar985116a2020-07-12 17:31:09 +02008931 ufunc->uf_def_status = UF_COMPILING;
8932
Bram Moolenaara80faa82020-04-12 19:37:17 +02008933 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008934
Bram Moolenaarf002a412021-01-24 13:34:18 +01008935#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008936 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008937#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008938 cctx.ctx_ufunc = ufunc;
8939 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008940 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008941 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8942 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8943 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8944 cctx.ctx_type_list = &ufunc->uf_type_list;
8945 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8946 instr = &cctx.ctx_instr;
8947
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008948 // Set the context to the function, it may be compiled when called from
8949 // another script. Set the script version to the most modern one.
8950 // The line number will be set in next_line_from_context().
8951 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008952 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8953
Bram Moolenaardc4c2302021-04-25 13:54:42 +02008954 // Don't use the flag from ":legacy" here.
8955 cmdmod.cmod_flags &= ~CMOD_LEGACY;
8956
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008957 // Make sure error messages are OK.
8958 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8959 if (do_estack_push)
8960 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008961 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008962
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008963 if (ufunc->uf_def_args.ga_len > 0)
8964 {
8965 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008966 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008967 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008968 int i;
8969 char_u *arg;
8970 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008971 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008972
8973 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008974 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008975 for (i = 0; i < count; ++i)
8976 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008977 garray_T *stack = &cctx.ctx_type_stack;
8978 type_T *val_type;
8979 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008980 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008981 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008982 int jump_instr_idx = instr->ga_len;
8983 isn_T *isn;
8984
8985 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8986 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8987 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008988
8989 // Make sure later arguments are not found.
8990 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008991
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008992 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008993 r = compile_expr0(&arg, &cctx);
8994
8995 ufunc->uf_args.ga_len = uf_args_len;
8996 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008997 goto erret;
8998
8999 // If no type specified use the type of the default value.
9000 // Otherwise check that the default value type matches the
9001 // specified type.
9002 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009003 where.wt_index = arg_idx + 1;
9004 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009005 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009006 {
9007 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009008 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009009 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009010 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009011 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009012 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009013
9014 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009015 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009016
9017 // set instruction index in JUMP_IF_ARG_SET to here
9018 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9019 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009020 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009021
9022 if (did_set_arg_type)
9023 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009024 }
9025
9026 /*
9027 * Loop over all the lines of the function and generate instructions.
9028 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009029 for (;;)
9030 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009031 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009032 int starts_with_colon = FALSE;
9033 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009034 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009035
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009036 // Bail out on the first error to avoid a flood of errors and report
9037 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009038 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009039 goto erret;
9040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009041 if (line != NULL && *line == '|')
9042 // the line continues after a '|'
9043 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009044 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009045 && !(*line == '#' && (line == cctx.ctx_line_start
9046 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009047 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009048 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009049 goto erret;
9050 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009051 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9052 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009053 else
9054 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009055 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009056 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009057 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009058 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009059#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009060 if (cctx.ctx_skip != SKIP_YES)
9061 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009062#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009063 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009064 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009065 // Make a copy, splitting off nextcmd and removing trailing spaces
9066 // may change it.
9067 if (line != NULL)
9068 {
9069 line = vim_strsave(line);
9070 vim_free(line_to_free);
9071 line_to_free = line;
9072 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009073 }
9074
Bram Moolenaara80faa82020-04-12 19:37:17 +02009075 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009076 ea.cmdlinep = &line;
9077 ea.cmd = skipwhite(line);
9078
Bram Moolenaarb2049902021-01-24 12:53:53 +01009079 if (*ea.cmd == '#')
9080 {
9081 // "#" starts a comment
9082 line = (char_u *)"";
9083 continue;
9084 }
9085
Bram Moolenaarf002a412021-01-24 13:34:18 +01009086#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009087 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
9088 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009089 {
9090 may_generate_prof_end(&cctx, prof_lnum);
9091
9092 prof_lnum = cctx.ctx_lnum;
9093 generate_instr(&cctx, ISN_PROF_START);
9094 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009095#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009096
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009097 // Some things can be recognized by the first character.
9098 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009099 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009100 case '}':
9101 {
9102 // "}" ends a block scope
9103 scopetype_T stype = cctx.ctx_scope == NULL
9104 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009105
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009106 if (stype == BLOCK_SCOPE)
9107 {
9108 compile_endblock(&cctx);
9109 line = ea.cmd;
9110 }
9111 else
9112 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009113 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009114 goto erret;
9115 }
9116 if (line != NULL)
9117 line = skipwhite(ea.cmd + 1);
9118 continue;
9119 }
9120
9121 case '{':
9122 // "{" starts a block scope
9123 // "{'a': 1}->func() is something else
9124 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9125 {
9126 line = compile_block(ea.cmd, &cctx);
9127 continue;
9128 }
9129 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009130 }
9131
9132 /*
9133 * COMMAND MODIFIERS
9134 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009135 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009136 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9137 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009138 {
9139 if (errormsg != NULL)
9140 goto erret;
9141 // empty line or comment
9142 line = (char_u *)"";
9143 continue;
9144 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009145 generate_cmdmods(&cctx, &local_cmdmod);
9146 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009147
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009148 // Check if there was a colon after the last command modifier or before
9149 // the current position.
9150 for (p = ea.cmd; p >= line; --p)
9151 {
9152 if (*p == ':')
9153 starts_with_colon = TRUE;
9154 if (p < ea.cmd && !VIM_ISWHITE(*p))
9155 break;
9156 }
9157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009158 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009159 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009160 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009161 {
9162 if (*ea.cmd == '(')
9163 // not for "call()"
9164 ea.cmd = p;
9165 else
9166 ea.cmd = skipwhite(ea.cmd);
9167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009168
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009169 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009170 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009171 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009172
Bram Moolenaar17126b12021-01-07 22:03:02 +01009173 // Check for assignment after command modifiers.
9174 assign = may_compile_assignment(&ea, &line, &cctx);
9175 if (assign == OK)
9176 goto nextline;
9177 if (assign == FAIL)
9178 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009179 }
9180
9181 /*
9182 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009183 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009184 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009185 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009186 cmd = ea.cmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009187 if (starts_with_colon || !(*cmd == '\''
9188 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009189 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009190 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009191 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009192 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009193 if (!starts_with_colon)
9194 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009195 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009196 goto erret;
9197 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009198 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009199 if (ends_excmd2(line, ea.cmd))
9200 {
9201 // A range without a command: jump to the line.
9202 // TODO: compile to a more efficient command, possibly
9203 // calling parse_cmd_address().
9204 ea.cmdidx = CMD_SIZE;
9205 line = compile_exec(line, &ea, &cctx);
9206 goto nextline;
9207 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009208 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009209 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009210 p = find_ex_command(&ea, NULL, starts_with_colon
9211 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009212
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009213 if (p == NULL)
9214 {
9215 if (cctx.ctx_skip != SKIP_YES)
9216 emsg(_(e_ambiguous_use_of_user_defined_command));
9217 goto erret;
9218 }
9219
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009220 // When using ":legacy cmd" always use compile_exec().
9221 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009222 {
9223 char_u *start = ea.cmd;
9224
9225 // ":legacy return expr" needs to be handled differently.
9226 if (checkforcmd(&start, "return", 4))
9227 ea.cmdidx = CMD_return;
9228 else
9229 ea.cmdidx = CMD_legacy;
9230 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009232 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9233 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009234 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009235 {
9236 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009237 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009238 }
9239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009240 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009241 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009242 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009243 // CMD_var cannot happen, compile_assignment() above would be
9244 // used. Most likely an assignment to a non-existing variable.
9245 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009246 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009248 }
9249
Bram Moolenaar3988f642020-08-27 22:43:03 +02009250 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009251 && ea.cmdidx != CMD_elseif
9252 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009253 && ea.cmdidx != CMD_endif
9254 && ea.cmdidx != CMD_endfor
9255 && ea.cmdidx != CMD_endwhile
9256 && ea.cmdidx != CMD_catch
9257 && ea.cmdidx != CMD_finally
9258 && ea.cmdidx != CMD_endtry)
9259 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009260 emsg(_(e_unreachable_code_after_return));
9261 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009262 }
9263
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009264 p = skipwhite(p);
9265 if (ea.cmdidx != CMD_SIZE
9266 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9267 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009268 if (ea.cmdidx >= 0)
9269 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009270 if ((ea.argt & EX_BANG) && *p == '!')
9271 {
9272 ea.forceit = TRUE;
9273 p = skipwhite(p + 1);
9274 }
9275 }
9276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009277 switch (ea.cmdidx)
9278 {
9279 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009280 ea.arg = p;
9281 line = compile_nested_function(&ea, &cctx);
9282 break;
9283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009284 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009285 // TODO: should we allow this, e.g. to declare a global
9286 // function?
9287 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009288 goto erret;
9289
9290 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009291 line = compile_return(p, check_return_type,
9292 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009293 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009294 break;
9295
9296 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009297 emsg(_(e_cannot_use_let_in_vim9_script));
9298 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009299 case CMD_var:
9300 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009301 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009302 case CMD_increment:
9303 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009304 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009305 if (line == p)
9306 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009307 break;
9308
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009309 case CMD_unlet:
9310 case CMD_unlockvar:
9311 case CMD_lockvar:
9312 line = compile_unletlock(p, &ea, &cctx);
9313 break;
9314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009315 case CMD_import:
9316 line = compile_import(p, &cctx);
9317 break;
9318
9319 case CMD_if:
9320 line = compile_if(p, &cctx);
9321 break;
9322 case CMD_elseif:
9323 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009324 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009325 break;
9326 case CMD_else:
9327 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009328 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009329 break;
9330 case CMD_endif:
9331 line = compile_endif(p, &cctx);
9332 break;
9333
9334 case CMD_while:
9335 line = compile_while(p, &cctx);
9336 break;
9337 case CMD_endwhile:
9338 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009339 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009340 break;
9341
9342 case CMD_for:
9343 line = compile_for(p, &cctx);
9344 break;
9345 case CMD_endfor:
9346 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009347 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009348 break;
9349 case CMD_continue:
9350 line = compile_continue(p, &cctx);
9351 break;
9352 case CMD_break:
9353 line = compile_break(p, &cctx);
9354 break;
9355
9356 case CMD_try:
9357 line = compile_try(p, &cctx);
9358 break;
9359 case CMD_catch:
9360 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009361 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009362 break;
9363 case CMD_finally:
9364 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009365 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009366 break;
9367 case CMD_endtry:
9368 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009369 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009370 break;
9371 case CMD_throw:
9372 line = compile_throw(p, &cctx);
9373 break;
9374
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009375 case CMD_eval:
9376 if (compile_expr0(&p, &cctx) == FAIL)
9377 goto erret;
9378
Bram Moolenaar3988f642020-08-27 22:43:03 +02009379 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009380 generate_instr_drop(&cctx, ISN_DROP, 1);
9381
9382 line = skipwhite(p);
9383 break;
9384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009385 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009386 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009387 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009388 case CMD_echomsg:
9389 case CMD_echoerr:
9390 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009391 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009392
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009393 case CMD_put:
9394 ea.cmd = cmd;
9395 line = compile_put(p, &ea, &cctx);
9396 break;
9397
Bram Moolenaar4c137212021-04-19 16:48:48 +02009398 case CMD_substitute:
9399 if (cctx.ctx_skip == SKIP_YES)
9400 line = (char_u *)"";
9401 else
9402 {
9403 ea.arg = p;
9404 line = compile_substitute(line, &ea, &cctx);
9405 }
9406 break;
9407
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009408 case CMD_redir:
9409 ea.arg = p;
9410 line = compile_redir(line, &ea, &cctx);
9411 break;
9412
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009413 case CMD_cexpr:
9414 case CMD_lexpr:
9415 case CMD_caddexpr:
9416 case CMD_laddexpr:
9417 case CMD_cgetexpr:
9418 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009419#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009420 ea.arg = p;
9421 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009422#else
9423 ex_ni(&ea);
9424 line = NULL;
9425#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009426 break;
9427
Bram Moolenaar3988f642020-08-27 22:43:03 +02009428 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009429
Bram Moolenaarae616492020-07-28 20:07:27 +02009430 case CMD_append:
9431 case CMD_change:
9432 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009433 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009434 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009435 case CMD_xit:
9436 not_in_vim9(&ea);
9437 goto erret;
9438
Bram Moolenaar002262f2020-07-08 17:47:57 +02009439 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009440 if (cctx.ctx_skip != SKIP_YES)
9441 {
9442 semsg(_(e_invalid_command_str), ea.cmd);
9443 goto erret;
9444 }
9445 // We don't check for a next command here.
9446 line = (char_u *)"";
9447 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009449 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009450 if (cctx.ctx_skip == SKIP_YES)
9451 {
9452 // We don't check for a next command here.
9453 line = (char_u *)"";
9454 }
9455 else
9456 {
9457 // Not recognized, execute with do_cmdline_cmd().
9458 ea.arg = p;
9459 line = compile_exec(line, &ea, &cctx);
9460 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009461 break;
9462 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009463nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009464 if (line == NULL)
9465 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009466 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009467
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009468 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009469 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009471 if (cctx.ctx_type_stack.ga_len < 0)
9472 {
9473 iemsg("Type stack underflow");
9474 goto erret;
9475 }
9476 }
9477
9478 if (cctx.ctx_scope != NULL)
9479 {
9480 if (cctx.ctx_scope->se_type == IF_SCOPE)
9481 emsg(_(e_endif));
9482 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9483 emsg(_(e_endwhile));
9484 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9485 emsg(_(e_endfor));
9486 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009487 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009488 goto erret;
9489 }
9490
Bram Moolenaarefd88552020-06-18 20:50:10 +02009491 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009492 {
9493 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
9494 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009495 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009496 goto erret;
9497 }
9498
9499 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01009500 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009501 }
9502
Bram Moolenaar599410c2021-04-10 14:03:43 +02009503 // When compiled with ":silent!" and there was an error don't consider the
9504 // function compiled.
9505 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009506 {
9507 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9508 + ufunc->uf_dfunc_idx;
9509 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009510 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009511#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009512 if (cctx.ctx_profiling)
9513 {
9514 dfunc->df_instr_prof = instr->ga_data;
9515 dfunc->df_instr_prof_count = instr->ga_len;
9516 }
9517 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009518#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009519 {
9520 dfunc->df_instr = instr->ga_data;
9521 dfunc->df_instr_count = instr->ga_len;
9522 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009523 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009524 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009525 if (cctx.ctx_outer_used)
9526 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009527 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009528 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009529
9530 ret = OK;
9531
9532erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009533 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009534 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009535 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9536 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009537
Bram Moolenaar8238f082021-04-20 21:10:48 +02009538 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009539 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009540
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009541 // If using the last entry in the table and it was added above, we
9542 // might as well remove it.
9543 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009544 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009545 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009546 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009547 ufunc->uf_dfunc_idx = 0;
9548 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009549 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009550
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009551 while (cctx.ctx_scope != NULL)
9552 drop_scope(&cctx);
9553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009554 if (errormsg != NULL)
9555 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009556 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009557 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009558 }
9559
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009560 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9561 {
9562 if (ret == OK)
9563 {
9564 emsg(_(e_missing_redir_end));
9565 ret = FAIL;
9566 }
9567 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009568 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009569 }
9570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009571 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009572 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009573 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009574 if (do_estack_push)
9575 estack_pop();
9576
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009577 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009578 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009579 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009580 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009581 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009582}
9583
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009584 void
9585set_function_type(ufunc_T *ufunc)
9586{
9587 int varargs = ufunc->uf_va_name != NULL;
9588 int argcount = ufunc->uf_args.ga_len;
9589
9590 // Create a type for the function, with the return type and any
9591 // argument types.
9592 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9593 // The type is included in "tt_args".
9594 if (argcount > 0 || varargs)
9595 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009596 if (ufunc->uf_type_list.ga_itemsize == 0)
9597 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009598 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9599 argcount, &ufunc->uf_type_list);
9600 // Add argument types to the function type.
9601 if (func_type_add_arg_types(ufunc->uf_func_type,
9602 argcount + varargs,
9603 &ufunc->uf_type_list) == FAIL)
9604 return;
9605 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9606 ufunc->uf_func_type->tt_min_argcount =
9607 argcount - ufunc->uf_def_args.ga_len;
9608 if (ufunc->uf_arg_types == NULL)
9609 {
9610 int i;
9611
9612 // lambda does not have argument types.
9613 for (i = 0; i < argcount; ++i)
9614 ufunc->uf_func_type->tt_args[i] = &t_any;
9615 }
9616 else
9617 mch_memmove(ufunc->uf_func_type->tt_args,
9618 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9619 if (varargs)
9620 {
9621 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009622 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009623 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9624 }
9625 }
9626 else
9627 // No arguments, can use a predefined type.
9628 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9629 argcount, &ufunc->uf_type_list);
9630}
9631
9632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009633/*
9634 * Delete an instruction, free what it contains.
9635 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009636 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009637delete_instr(isn_T *isn)
9638{
9639 switch (isn->isn_type)
9640 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009641 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009642 case ISN_EXEC:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009643 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009644 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009645 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009646 case ISN_LOADENV:
9647 case ISN_LOADG:
9648 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009649 case ISN_LOADT:
9650 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009651 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009652 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009653 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009654 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009655 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009656 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009657 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009658 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009659 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009660 case ISN_STOREW:
9661 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009662 vim_free(isn->isn_arg.string);
9663 break;
9664
Bram Moolenaar4c137212021-04-19 16:48:48 +02009665 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009666 {
9667 int idx;
9668 isn_T *list = isn->isn_arg.subs.subs_instr;
9669
9670 vim_free(isn->isn_arg.subs.subs_cmd);
9671 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9672 delete_instr(list + idx);
9673 vim_free(list);
9674 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009675 break;
9676
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009677 case ISN_INSTR:
9678 {
9679 int idx;
9680 isn_T *list = isn->isn_arg.instr;
9681
9682 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9683 delete_instr(list + idx);
9684 vim_free(list);
9685 }
9686 break;
9687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009688 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009689 case ISN_STORES:
9690 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009691 break;
9692
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009693 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009694 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009695 vim_free(isn->isn_arg.unlet.ul_name);
9696 break;
9697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009698 case ISN_STOREOPT:
9699 vim_free(isn->isn_arg.storeopt.so_name);
9700 break;
9701
9702 case ISN_PUSHBLOB: // push blob isn_arg.blob
9703 blob_unref(isn->isn_arg.blob);
9704 break;
9705
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009706 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009707#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009708 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009709#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009710 break;
9711
9712 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009713#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009714 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009715#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009716 break;
9717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009718 case ISN_UCALL:
9719 vim_free(isn->isn_arg.ufunc.cuf_name);
9720 break;
9721
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009722 case ISN_FUNCREF:
9723 {
9724 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9725 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009726 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009727
Bram Moolenaar077a4232020-12-22 18:33:27 +01009728 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9729 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009730 }
9731 break;
9732
9733 case ISN_DCALL:
9734 {
9735 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9736 + isn->isn_arg.dfunc.cdf_idx;
9737
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009738 if (dfunc->df_ufunc != NULL
9739 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009740 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009741 }
9742 break;
9743
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009744 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009745 {
9746 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9747 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9748
9749 if (ufunc != NULL)
9750 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009751 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009752 func_ptr_unref(ufunc);
9753 }
9754
9755 vim_free(lambda);
9756 vim_free(isn->isn_arg.newfunc.nf_global);
9757 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009758 break;
9759
Bram Moolenaar5e654232020-09-16 15:22:00 +02009760 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009761 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009762 free_type(isn->isn_arg.type.ct_type);
9763 break;
9764
Bram Moolenaar02194d22020-10-24 23:08:38 +02009765 case ISN_CMDMOD:
9766 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9767 ->cmod_filter_regmatch.regprog);
9768 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9769 break;
9770
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009771 case ISN_LOADSCRIPT:
9772 case ISN_STORESCRIPT:
9773 vim_free(isn->isn_arg.script.scriptref);
9774 break;
9775
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009776 case ISN_TRY:
9777 vim_free(isn->isn_arg.try.try_ref);
9778 break;
9779
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009780 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +02009781 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009782 vim_free(isn->isn_arg.cexpr.cexpr_ref);
9783 break;
9784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009785 case ISN_2BOOL:
9786 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009787 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009788 case ISN_ADDBLOB:
9789 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009790 case ISN_ANYINDEX:
9791 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009792 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009793 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009794 case ISN_BLOBINDEX:
9795 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009796 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009797 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009798 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009799 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009800 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009801 case ISN_COMPAREANY:
9802 case ISN_COMPAREBLOB:
9803 case ISN_COMPAREBOOL:
9804 case ISN_COMPAREDICT:
9805 case ISN_COMPAREFLOAT:
9806 case ISN_COMPAREFUNC:
9807 case ISN_COMPARELIST:
9808 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009809 case ISN_COMPARESPECIAL:
9810 case ISN_COMPARESTRING:
9811 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009812 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009813 case ISN_DROP:
9814 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009815 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009816 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009817 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009818 case ISN_EXECCONCAT:
9819 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009820 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009821 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009822 case ISN_GETITEM:
9823 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009824 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009825 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009826 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009827 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009828 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009829 case ISN_LOADBDICT:
9830 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009831 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009832 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009833 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009834 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009835 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009836 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009837 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009838 case ISN_NEGATENR:
9839 case ISN_NEWDICT:
9840 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009841 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009842 case ISN_OPFLOAT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009843 case ISN_FINISH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009844 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009845 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009846 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009847 case ISN_PROF_END:
9848 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009849 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009850 case ISN_PUSHF:
9851 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009852 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009853 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009854 case ISN_REDIREND:
9855 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009856 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009857 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009858 case ISN_SHUFFLE:
9859 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009860 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009861 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009862 case ISN_STORENR:
9863 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009864 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009865 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009866 case ISN_STOREV:
9867 case ISN_STRINDEX:
9868 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009869 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009870 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009871 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009872 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009873 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009874 // nothing allocated
9875 break;
9876 }
9877}
9878
9879/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009880 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009881 */
9882 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009883delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009884{
9885 int idx;
9886
9887 ga_clear(&dfunc->df_def_args_isn);
9888
9889 if (dfunc->df_instr != NULL)
9890 {
9891 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9892 delete_instr(dfunc->df_instr + idx);
9893 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009894 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009895 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009896#ifdef FEAT_PROFILE
9897 if (dfunc->df_instr_prof != NULL)
9898 {
9899 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9900 delete_instr(dfunc->df_instr_prof + idx);
9901 VIM_CLEAR(dfunc->df_instr_prof);
9902 dfunc->df_instr_prof = NULL;
9903 }
9904#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009905
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009906 if (mark_deleted)
9907 dfunc->df_deleted = TRUE;
9908 if (dfunc->df_ufunc != NULL)
9909 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009910}
9911
9912/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009913 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009914 * function, unless another user function still uses it.
9915 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009916 */
9917 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009918unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009919{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009920 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009921 {
9922 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9923 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009924
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009925 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009926 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009927 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009928 ufunc->uf_dfunc_idx = 0;
9929 if (dfunc->df_ufunc == ufunc)
9930 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009931 }
9932}
9933
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009934/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009935 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009936 */
9937 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009938link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009939{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009940 if (ufunc->uf_dfunc_idx > 0)
9941 {
9942 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9943 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009944
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009945 ++dfunc->df_refcount;
9946 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009947}
9948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009949#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009950/*
9951 * Free all functions defined with ":def".
9952 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009953 void
9954free_def_functions(void)
9955{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009956 int idx;
9957
9958 for (idx = 0; idx < def_functions.ga_len; ++idx)
9959 {
9960 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9961
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009962 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009963 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009964 }
9965
9966 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009967}
9968#endif
9969
9970
9971#endif // FEAT_EVAL