blob: 607f651d9064d3452a5669b5f246df79fa3d4480 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200117// Destination for an assignment or ":unlet" with an index.
118typedef enum {
119 dest_local,
120 dest_option,
121 dest_env,
122 dest_global,
123 dest_buffer,
124 dest_window,
125 dest_tab,
126 dest_vimvar,
127 dest_script,
128 dest_reg,
129 dest_expr,
130} assign_dest_T;
131
132// Used by compile_lhs() to store information about the LHS of an assignment
133// and one argument of ":unlet" with an index.
134typedef struct {
135 assign_dest_T lhs_dest; // type of destination
136
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200137 char_u *lhs_name; // allocated name excluding the last
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200138 // "[expr]" or ".name".
139 size_t lhs_varlen; // length of the variable without
140 // "[expr]" or ".name"
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200141 char_u *lhs_whole; // allocated name including the last
142 // "[expr]" or ".name" for :redir
143 size_t lhs_varlen_total; // length of the variable including
144 // any "[expr]" or ".name"
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200145 char_u *lhs_dest_end; // end of the destination, including
146 // "[expr]" or ".name".
147
148 int lhs_has_index; // has "[expr]" or ".name"
149
150 int lhs_new_local; // create new local variable
151 int lhs_opt_flags; // for when destination is an option
152 int lhs_vimvaridx; // for when destination is a v:var
153
154 lvar_T lhs_local_lvar; // used for existing local destination
155 lvar_T lhs_arg_lvar; // used for argument destination
156 lvar_T *lhs_lvar; // points to destination lvar
157 int lhs_scriptvar_sid;
158 int lhs_scriptvar_idx;
159
160 int lhs_has_type; // type was specified
161 type_T *lhs_type;
162 type_T *lhs_member_type;
163
164 int lhs_append; // used by ISN_REDIREND
165} lhs_T;
166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167/*
168 * Context for compiling lines of Vim script.
169 * Stores info about the local variables and condition stack.
170 */
171struct cctx_S {
172 ufunc_T *ctx_ufunc; // current function
173 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200174 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 garray_T ctx_instr; // generated instructions
176
Bram Moolenaarb2049902021-01-24 12:53:53 +0100177 int ctx_profiling; // when TRUE generate ISN_PROF_START
178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200180 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200182 int ctx_has_closure; // set to one if a closures was created in
183 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100185 garray_T ctx_imports; // imported items
186
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200187 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200189 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200191 cctx_T *ctx_outer; // outer scope for lambda or nested
192 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200193 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200196 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200197
Bram Moolenaar02194d22020-10-24 23:08:38 +0200198 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200199
200 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
201 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202};
203
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100204static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205
206/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100207 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100208 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100209 * If "lvar" is NULL only check if the variable can be found.
210 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100212 static int
213lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214{
215 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100216 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100218 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100219 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200220
221 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
223 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100224 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
225 if (STRNCMP(name, lvp->lv_name, len) == 0
226 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200227 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100228 if (lvar != NULL)
229 {
230 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100231 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100232 }
233 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200236
237 // Find local in outer function scope.
238 if (cctx->ctx_outer != NULL)
239 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100240 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100242 if (lvar != NULL)
243 {
244 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100245 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100246 }
247 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200248 }
249 }
250
Bram Moolenaar709664c2020-12-12 14:33:41 +0100251 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252}
253
254/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200255 * Lookup an argument in the current function and an enclosing function.
256 * Returns the argument index in "idxp"
257 * Returns the argument type in "type"
258 * Sets "gen_load_outer" to TRUE if found in outer scope.
259 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260 */
261 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200262arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200263 char_u *name,
264 size_t len,
265 int *idxp,
266 type_T **type,
267 int *gen_load_outer,
268 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269{
270 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200271 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100273 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200274 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
276 {
277 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
278
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200279 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
280 {
281 if (idxp != NULL)
282 {
283 // Arguments are located above the frame pointer. One further
284 // if there is a vararg argument
285 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
286 + STACK_FRAME_SIZE)
287 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
288
289 if (cctx->ctx_ufunc->uf_arg_types != NULL)
290 *type = cctx->ctx_ufunc->uf_arg_types[idx];
291 else
292 *type = &t_any;
293 }
294 return OK;
295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200298 va_name = cctx->ctx_ufunc->uf_va_name;
299 if (va_name != NULL
300 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
301 {
302 if (idxp != NULL)
303 {
304 // varargs is always the last argument
305 *idxp = -STACK_FRAME_SIZE - 1;
306 *type = cctx->ctx_ufunc->uf_va_type;
307 }
308 return OK;
309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100310
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200311 if (cctx->ctx_outer != NULL)
312 {
313 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200314 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200315 == OK)
316 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100317 if (gen_load_outer != NULL)
318 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200319 return OK;
320 }
321 }
322
323 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100324}
325
326/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200327 * Lookup a script-local variable in the current script, possibly defined in a
328 * block that contains the function "cctx->ctx_ufunc".
329 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100330 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331 * Return NULL when not found.
332 */
333 static sallvar_T *
334find_script_var(char_u *name, size_t len, cctx_T *cctx)
335{
336 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
337 hashitem_T *hi;
338 int cc;
339 sallvar_T *sav;
340 ufunc_T *ufunc;
341
342 // Find the list of all script variables with the right name.
343 if (len > 0)
344 {
345 cc = name[len];
346 name[len] = NUL;
347 }
348 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
349 if (len > 0)
350 name[len] = cc;
351 if (HASHITEM_EMPTY(hi))
352 return NULL;
353
354 sav = HI2SAV(hi);
355 if (sav->sav_block_id == 0 || cctx == NULL)
356 // variable defined in the script scope or not in a function.
357 return sav;
358
359 // Go over the variables with this name and find one that was visible
360 // from the function.
361 ufunc = cctx->ctx_ufunc;
362 while (sav != NULL)
363 {
364 int idx;
365
366 // Go over the blocks that this function was defined in. If the
367 // variable block ID matches it was visible to the function.
368 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
369 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
370 return sav;
371 sav = sav->sav_next;
372 }
373
374 return NULL;
375}
376
377/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100378 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200379 */
380 static int
381script_is_vim9()
382{
383 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
384}
385
386/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200388 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389 * Returns OK or FAIL.
390 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200391 static int
392script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200394 if (current_sctx.sc_sid <= 0)
395 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200396 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200397 {
398 // Check script variables that were visible where the function was
399 // defined.
400 if (find_script_var(name, len, cctx) != NULL)
401 return OK;
402 }
403 else
404 {
405 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
406 dictitem_T *di;
407 int cc;
408
409 // Check script variables that are currently visible
410 cc = name[len];
411 name[len] = NUL;
412 di = find_var_in_ht(ht, 0, name, TRUE);
413 name[len] = cc;
414 if (di != NULL)
415 return OK;
416 }
417
418 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419}
420
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100421/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100422 * Return TRUE if "name" is a local variable, argument, script variable or
423 * imported.
424 */
425 static int
426variable_exists(char_u *name, size_t len, cctx_T *cctx)
427{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100428 return (cctx != NULL
429 && (lookup_local(name, len, NULL, cctx) == OK
430 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200431 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100432 || find_imported(name, len, cctx) != NULL;
433}
434
435/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100436 * Return TRUE if "name" is a local variable, argument, script variable,
437 * imported or function.
438 */
439 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100440item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100441{
442 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100443 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100444
445 if (variable_exists(name, len, cctx))
446 return TRUE;
447
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100448 // This is similar to what is in lookup_scriptitem():
449 // Find a function, so that a following "->" works.
450 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
451 // a function call.
452 p = skipwhite(name + len);
453
454 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
455 {
456 // Do not check for an internal function, since it might also be a
457 // valid command, such as ":split" versuse "split()".
458 // Skip "g:" before a function name.
459 is_global = (name[0] == 'g' && name[1] == ':');
460 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
461 }
462 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100463}
464
465/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100466 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200467 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200468 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100469 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100470 * Return FAIL and give an error if it defined.
471 */
472 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100473check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100474{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200475 int c = p[len];
476 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200477
Bram Moolenaarda479c72021-04-10 21:01:38 +0200478 // underscore argument is OK
479 if (len == 1 && *p == '_')
480 return OK;
481
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200482 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100483 {
484 if (is_arg)
485 semsg(_(e_argument_already_declared_in_script_str), p);
486 else
487 semsg(_(e_variable_already_declared_in_script_str), p);
488 return FAIL;
489 }
490
Bram Moolenaarad486a02020-08-01 23:22:18 +0200491 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100492 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100493 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200494 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200495 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200496 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100497 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200498 // A local or script-local function can shadow a global function.
499 if (ufunc == NULL || !func_is_global(ufunc)
500 || (p[0] == 'g' && p[1] == ':'))
501 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100502 if (is_arg)
503 semsg(_(e_argument_name_shadows_existing_variable_str), p);
504 else
505 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200506 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200507 return FAIL;
508 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100509 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200510 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100511 return OK;
512}
513
Bram Moolenaar65b95452020-07-19 14:03:09 +0200514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515/////////////////////////////////////////////////////////////////////
516// Following generate_ functions expect the caller to call ga_grow().
517
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200518#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
519#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521/*
522 * Generate an instruction without arguments.
523 * Returns a pointer to the new instruction, NULL if failed.
524 */
525 static isn_T *
526generate_instr(cctx_T *cctx, isntype_T isn_type)
527{
528 garray_T *instr = &cctx->ctx_instr;
529 isn_T *isn;
530
Bram Moolenaar080457c2020-03-03 21:53:32 +0100531 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 if (ga_grow(instr, 1) == FAIL)
533 return NULL;
534 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
535 isn->isn_type = isn_type;
536 isn->isn_lnum = cctx->ctx_lnum + 1;
537 ++instr->ga_len;
538
539 return isn;
540}
541
542/*
543 * Generate an instruction without arguments.
544 * "drop" will be removed from the stack.
545 * Returns a pointer to the new instruction, NULL if failed.
546 */
547 static isn_T *
548generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
549{
550 garray_T *stack = &cctx->ctx_type_stack;
551
Bram Moolenaar080457c2020-03-03 21:53:32 +0100552 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 stack->ga_len -= drop;
554 return generate_instr(cctx, isn_type);
555}
556
557/*
558 * Generate instruction "isn_type" and put "type" on the type stack.
559 */
560 static isn_T *
561generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
562{
563 isn_T *isn;
564 garray_T *stack = &cctx->ctx_type_stack;
565
566 if ((isn = generate_instr(cctx, isn_type)) == NULL)
567 return NULL;
568
569 if (ga_grow(stack, 1) == FAIL)
570 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200571 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 ++stack->ga_len;
573
574 return isn;
575}
576
577/*
578 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200579 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 */
581 static int
582may_generate_2STRING(int offset, cctx_T *cctx)
583{
584 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200585 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100587 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100589 RETURN_OK_IF_SKIP(cctx);
590 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200591 switch ((*type)->tt_type)
592 {
593 // nothing to be done
594 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200596 // conversion possible
597 case VAR_SPECIAL:
598 case VAR_BOOL:
599 case VAR_NUMBER:
600 case VAR_FLOAT:
601 break;
602
603 // conversion possible (with runtime check)
604 case VAR_ANY:
605 case VAR_UNKNOWN:
606 isntype = ISN_2STRING_ANY;
607 break;
608
609 // conversion not possible
610 case VAR_VOID:
611 case VAR_BLOB:
612 case VAR_FUNC:
613 case VAR_PARTIAL:
614 case VAR_LIST:
615 case VAR_DICT:
616 case VAR_JOB:
617 case VAR_CHANNEL:
618 to_string_error((*type)->tt_type);
619 return FAIL;
620 }
621
622 *type = &t_string;
623 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 return FAIL;
625 isn->isn_arg.number = offset;
626
627 return OK;
628}
629
630 static int
631check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
632{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200633 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200635 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 {
637 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200638 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200640 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 return FAIL;
642 }
643 return OK;
644}
645
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200646 static int
647generate_add_instr(
648 cctx_T *cctx,
649 vartype_T vartype,
650 type_T *type1,
651 type_T *type2)
652{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100653 garray_T *stack = &cctx->ctx_type_stack;
654 isn_T *isn = generate_instr_drop(cctx,
655 vartype == VAR_NUMBER ? ISN_OPNR
656 : vartype == VAR_LIST ? ISN_ADDLIST
657 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200658#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100659 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200660#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100661 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200662
663 if (vartype != VAR_LIST && vartype != VAR_BLOB
664 && type1->tt_type != VAR_ANY
665 && type2->tt_type != VAR_ANY
666 && check_number_or_float(
667 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
668 return FAIL;
669
670 if (isn != NULL)
671 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100672
673 // When concatenating two lists with different member types the member type
674 // becomes "any".
675 if (vartype == VAR_LIST
676 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
677 && type1->tt_member != type2->tt_member)
678 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
679
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200680 return isn == NULL ? FAIL : OK;
681}
682
683/*
684 * Get the type to use for an instruction for an operation on "type1" and
685 * "type2". If they are matching use a type-specific instruction. Otherwise
686 * fall back to runtime type checking.
687 */
688 static vartype_T
689operator_type(type_T *type1, type_T *type2)
690{
691 if (type1->tt_type == type2->tt_type
692 && (type1->tt_type == VAR_NUMBER
693 || type1->tt_type == VAR_LIST
694#ifdef FEAT_FLOAT
695 || type1->tt_type == VAR_FLOAT
696#endif
697 || type1->tt_type == VAR_BLOB))
698 return type1->tt_type;
699 return VAR_ANY;
700}
701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702/*
703 * Generate an instruction with two arguments. The instruction depends on the
704 * type of the arguments.
705 */
706 static int
707generate_two_op(cctx_T *cctx, char_u *op)
708{
709 garray_T *stack = &cctx->ctx_type_stack;
710 type_T *type1;
711 type_T *type2;
712 vartype_T vartype;
713 isn_T *isn;
714
Bram Moolenaar080457c2020-03-03 21:53:32 +0100715 RETURN_OK_IF_SKIP(cctx);
716
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200717 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
719 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200720 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721
722 switch (*op)
723 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200724 case '+':
725 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 break;
728
729 case '-':
730 case '*':
731 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
732 op) == FAIL)
733 return FAIL;
734 if (vartype == VAR_NUMBER)
735 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
736#ifdef FEAT_FLOAT
737 else if (vartype == VAR_FLOAT)
738 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
739#endif
740 else
741 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
742 if (isn != NULL)
743 isn->isn_arg.op.op_type = *op == '*'
744 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
745 break;
746
Bram Moolenaar4c683752020-04-05 21:38:23 +0200747 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200749 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 && type2->tt_type != VAR_NUMBER))
751 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200752 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 return FAIL;
754 }
755 isn = generate_instr_drop(cctx,
756 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
757 if (isn != NULL)
758 isn->isn_arg.op.op_type = EXPR_REM;
759 break;
760 }
761
762 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200763 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 {
765 type_T *type = &t_any;
766
767#ifdef FEAT_FLOAT
768 // float+number and number+float results in float
769 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
770 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
771 type = &t_float;
772#endif
773 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
774 }
775
776 return OK;
777}
778
779/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200780 * Get the instruction to use for comparing "type1" with "type2"
781 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200783 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100784get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785{
786 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787
Bram Moolenaar4c683752020-04-05 21:38:23 +0200788 if (type1 == VAR_UNKNOWN)
789 type1 = VAR_ANY;
790 if (type2 == VAR_UNKNOWN)
791 type2 = VAR_ANY;
792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 if (type1 == type2)
794 {
795 switch (type1)
796 {
797 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
798 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
799 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
800 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
801 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
802 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
803 case VAR_LIST: isntype = ISN_COMPARELIST; break;
804 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
805 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 default: isntype = ISN_COMPAREANY; break;
807 }
808 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200809 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100811 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 isntype = ISN_COMPAREANY;
813
Bram Moolenaar657137c2021-01-09 15:45:23 +0100814 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815 && (isntype == ISN_COMPAREBOOL
816 || isntype == ISN_COMPARESPECIAL
817 || isntype == ISN_COMPARENR
818 || isntype == ISN_COMPAREFLOAT))
819 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200820 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100821 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200822 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 }
824 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100825 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
827 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100828 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
829 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 && (type1 == VAR_BLOB || type2 == VAR_BLOB
831 || type1 == VAR_LIST || type2 == VAR_LIST))))
832 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200833 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200835 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200837 return isntype;
838}
839
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200840 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100841check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200842{
843 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
844 return FAIL;
845 return OK;
846}
847
Bram Moolenaara5565e42020-05-09 15:44:01 +0200848/*
849 * Generate an ISN_COMPARE* instruction with a boolean result.
850 */
851 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100852generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200853{
854 isntype_T isntype;
855 isn_T *isn;
856 garray_T *stack = &cctx->ctx_type_stack;
857 vartype_T type1;
858 vartype_T type2;
859
860 RETURN_OK_IF_SKIP(cctx);
861
862 // Get the known type of the two items on the stack. If they are matching
863 // use a type-specific instruction. Otherwise fall back to runtime type
864 // checking.
865 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
866 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100867 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200868 if (isntype == ISN_DROP)
869 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870
871 if ((isn = generate_instr(cctx, isntype)) == NULL)
872 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100873 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 isn->isn_arg.op.op_ic = ic;
875
876 // takes two arguments, puts one bool back
877 if (stack->ga_len >= 2)
878 {
879 --stack->ga_len;
880 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
881 }
882
883 return OK;
884}
885
886/*
887 * Generate an ISN_2BOOL instruction.
888 */
889 static int
890generate_2BOOL(cctx_T *cctx, int invert)
891{
892 isn_T *isn;
893 garray_T *stack = &cctx->ctx_type_stack;
894
Bram Moolenaar080457c2020-03-03 21:53:32 +0100895 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
897 return FAIL;
898 isn->isn_arg.number = invert;
899
900 // type becomes bool
901 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
902
903 return OK;
904}
905
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200906/*
907 * Generate an ISN_COND2BOOL instruction.
908 */
909 static int
910generate_COND2BOOL(cctx_T *cctx)
911{
912 isn_T *isn;
913 garray_T *stack = &cctx->ctx_type_stack;
914
915 RETURN_OK_IF_SKIP(cctx);
916 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
917 return FAIL;
918
919 // type becomes bool
920 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
921
922 return OK;
923}
924
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200926generate_TYPECHECK(
927 cctx_T *cctx,
928 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100929 int offset,
930 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931{
932 isn_T *isn;
933 garray_T *stack = &cctx->ctx_type_stack;
934
Bram Moolenaar080457c2020-03-03 21:53:32 +0100935 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
937 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200938 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100939 isn->isn_arg.type.ct_off = (int8_T)offset;
940 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941
Bram Moolenaar5e654232020-09-16 15:22:00 +0200942 // type becomes expected
943 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944
945 return OK;
946}
947
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100948 static int
949generate_SETTYPE(
950 cctx_T *cctx,
951 type_T *expected)
952{
953 isn_T *isn;
954
955 RETURN_OK_IF_SKIP(cctx);
956 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
957 return FAIL;
958 isn->isn_arg.type.ct_type = alloc_type(expected);
959 return OK;
960}
961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200963 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
964 * used. Return FALSE if the types will never match.
965 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100966 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200967use_typecheck(type_T *actual, type_T *expected)
968{
969 if (actual->tt_type == VAR_ANY
970 || actual->tt_type == VAR_UNKNOWN
971 || (actual->tt_type == VAR_FUNC
972 && (expected->tt_type == VAR_FUNC
973 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100974 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
975 && ((actual->tt_member == &t_void)
976 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200977 return TRUE;
978 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
979 && actual->tt_type == expected->tt_type)
980 // This takes care of a nested list or dict.
981 return use_typecheck(actual->tt_member, expected->tt_member);
982 return FALSE;
983}
984
985/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200986 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200987 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200988 * - "actual" is a type that can be "expected" type: add a runtime check; or
989 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200990 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
991 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200992 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100993 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200994need_type(
995 type_T *actual,
996 type_T *expected,
997 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100998 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200999 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001000 int silent,
1001 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001002{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001003 where_T where;
1004
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001005 if (expected == &t_bool && actual != &t_bool
1006 && (actual->tt_flags & TTFLAG_BOOL_OK))
1007 {
1008 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1009 // boolean is OK but requires a conversion.
1010 generate_2BOOL(cctx, FALSE);
1011 return OK;
1012 }
1013
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001014 where.wt_index = arg_idx;
1015 where.wt_variable = FALSE;
1016 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001017 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001018
1019 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001020 // If it's a constant a runtime check makes no sense.
1021 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001022 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001023 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001024 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001025 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001026
1027 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001028 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001029 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001030}
1031
1032/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001033 * Check that the top of the type stack has a type that can be used as a
1034 * condition. Give an error and return FAIL if not.
1035 */
1036 static int
1037bool_on_stack(cctx_T *cctx)
1038{
1039 garray_T *stack = &cctx->ctx_type_stack;
1040 type_T *type;
1041
1042 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1043 if (type == &t_bool)
1044 return OK;
1045
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001046 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001047 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1048 // This requires a runtime type check.
1049 return generate_COND2BOOL(cctx);
1050
Bram Moolenaar351ead02021-01-16 16:07:01 +01001051 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001052}
1053
1054/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 * Generate an ISN_PUSHNR instruction.
1056 */
1057 static int
1058generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1059{
1060 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001061 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062
Bram Moolenaar080457c2020-03-03 21:53:32 +01001063 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1065 return FAIL;
1066 isn->isn_arg.number = number;
1067
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001068 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001069 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001070 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071 return OK;
1072}
1073
1074/*
1075 * Generate an ISN_PUSHBOOL instruction.
1076 */
1077 static int
1078generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1079{
1080 isn_T *isn;
1081
Bram Moolenaar080457c2020-03-03 21:53:32 +01001082 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1084 return FAIL;
1085 isn->isn_arg.number = number;
1086
1087 return OK;
1088}
1089
1090/*
1091 * Generate an ISN_PUSHSPEC instruction.
1092 */
1093 static int
1094generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1095{
1096 isn_T *isn;
1097
Bram Moolenaar080457c2020-03-03 21:53:32 +01001098 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1100 return FAIL;
1101 isn->isn_arg.number = number;
1102
1103 return OK;
1104}
1105
1106#ifdef FEAT_FLOAT
1107/*
1108 * Generate an ISN_PUSHF instruction.
1109 */
1110 static int
1111generate_PUSHF(cctx_T *cctx, float_T fnumber)
1112{
1113 isn_T *isn;
1114
Bram Moolenaar080457c2020-03-03 21:53:32 +01001115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1117 return FAIL;
1118 isn->isn_arg.fnumber = fnumber;
1119
1120 return OK;
1121}
1122#endif
1123
1124/*
1125 * Generate an ISN_PUSHS instruction.
1126 * Consumes "str".
1127 */
1128 static int
1129generate_PUSHS(cctx_T *cctx, char_u *str)
1130{
1131 isn_T *isn;
1132
Bram Moolenaar508b5612021-01-02 18:17:26 +01001133 if (cctx->ctx_skip == SKIP_YES)
1134 {
1135 vim_free(str);
1136 return OK;
1137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1139 return FAIL;
1140 isn->isn_arg.string = str;
1141
1142 return OK;
1143}
1144
1145/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001146 * Generate an ISN_PUSHCHANNEL instruction.
1147 * Consumes "channel".
1148 */
1149 static int
1150generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1151{
1152 isn_T *isn;
1153
Bram Moolenaar080457c2020-03-03 21:53:32 +01001154 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001155 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1156 return FAIL;
1157 isn->isn_arg.channel = channel;
1158
1159 return OK;
1160}
1161
1162/*
1163 * Generate an ISN_PUSHJOB instruction.
1164 * Consumes "job".
1165 */
1166 static int
1167generate_PUSHJOB(cctx_T *cctx, job_T *job)
1168{
1169 isn_T *isn;
1170
Bram Moolenaar080457c2020-03-03 21:53:32 +01001171 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001172 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001173 return FAIL;
1174 isn->isn_arg.job = job;
1175
1176 return OK;
1177}
1178
1179/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 * Generate an ISN_PUSHBLOB instruction.
1181 * Consumes "blob".
1182 */
1183 static int
1184generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1185{
1186 isn_T *isn;
1187
Bram Moolenaar080457c2020-03-03 21:53:32 +01001188 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1190 return FAIL;
1191 isn->isn_arg.blob = blob;
1192
1193 return OK;
1194}
1195
1196/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001197 * Generate an ISN_PUSHFUNC instruction with name "name".
1198 * Consumes "name".
1199 */
1200 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001201generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001202{
1203 isn_T *isn;
1204
Bram Moolenaar080457c2020-03-03 21:53:32 +01001205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001206 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001207 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001208 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001209
1210 return OK;
1211}
1212
1213/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001214 * Generate an ISN_GETITEM instruction with "index".
1215 */
1216 static int
1217generate_GETITEM(cctx_T *cctx, int index)
1218{
1219 isn_T *isn;
1220 garray_T *stack = &cctx->ctx_type_stack;
1221 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1222 type_T *item_type = &t_any;
1223
1224 RETURN_OK_IF_SKIP(cctx);
1225
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001226 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001227 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001228 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001229 emsg(_(e_listreq));
1230 return FAIL;
1231 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001232 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001233 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1234 return FAIL;
1235 isn->isn_arg.number = index;
1236
1237 // add the item type to the type stack
1238 if (ga_grow(stack, 1) == FAIL)
1239 return FAIL;
1240 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1241 ++stack->ga_len;
1242 return OK;
1243}
1244
1245/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001246 * Generate an ISN_SLICE instruction with "count".
1247 */
1248 static int
1249generate_SLICE(cctx_T *cctx, int count)
1250{
1251 isn_T *isn;
1252
1253 RETURN_OK_IF_SKIP(cctx);
1254 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1255 return FAIL;
1256 isn->isn_arg.number = count;
1257 return OK;
1258}
1259
1260/*
1261 * Generate an ISN_CHECKLEN instruction with "min_len".
1262 */
1263 static int
1264generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1265{
1266 isn_T *isn;
1267
1268 RETURN_OK_IF_SKIP(cctx);
1269
1270 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1271 return FAIL;
1272 isn->isn_arg.checklen.cl_min_len = min_len;
1273 isn->isn_arg.checklen.cl_more_OK = more_OK;
1274
1275 return OK;
1276}
1277
1278/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 * Generate an ISN_STORE instruction.
1280 */
1281 static int
1282generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1283{
1284 isn_T *isn;
1285
Bram Moolenaar080457c2020-03-03 21:53:32 +01001286 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1288 return FAIL;
1289 if (name != NULL)
1290 isn->isn_arg.string = vim_strsave(name);
1291 else
1292 isn->isn_arg.number = idx;
1293
1294 return OK;
1295}
1296
1297/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001298 * Generate an ISN_STOREOUTER instruction.
1299 */
1300 static int
1301generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1302{
1303 isn_T *isn;
1304
1305 RETURN_OK_IF_SKIP(cctx);
1306 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1307 return FAIL;
1308 isn->isn_arg.outer.outer_idx = idx;
1309 isn->isn_arg.outer.outer_depth = level;
1310
1311 return OK;
1312}
1313
1314/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1316 */
1317 static int
1318generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1319{
1320 isn_T *isn;
1321
Bram Moolenaar080457c2020-03-03 21:53:32 +01001322 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1324 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001325 isn->isn_arg.storenr.stnr_idx = idx;
1326 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
1328 return OK;
1329}
1330
1331/*
1332 * Generate an ISN_STOREOPT instruction
1333 */
1334 static int
1335generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1336{
1337 isn_T *isn;
1338
Bram Moolenaar080457c2020-03-03 21:53:32 +01001339 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001340 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 return FAIL;
1342 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1343 isn->isn_arg.storeopt.so_flags = opt_flags;
1344
1345 return OK;
1346}
1347
1348/*
1349 * Generate an ISN_LOAD or similar instruction.
1350 */
1351 static int
1352generate_LOAD(
1353 cctx_T *cctx,
1354 isntype_T isn_type,
1355 int idx,
1356 char_u *name,
1357 type_T *type)
1358{
1359 isn_T *isn;
1360
Bram Moolenaar080457c2020-03-03 21:53:32 +01001361 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1363 return FAIL;
1364 if (name != NULL)
1365 isn->isn_arg.string = vim_strsave(name);
1366 else
1367 isn->isn_arg.number = idx;
1368
1369 return OK;
1370}
1371
1372/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001373 * Generate an ISN_LOADOUTER instruction
1374 */
1375 static int
1376generate_LOADOUTER(
1377 cctx_T *cctx,
1378 int idx,
1379 int nesting,
1380 type_T *type)
1381{
1382 isn_T *isn;
1383
1384 RETURN_OK_IF_SKIP(cctx);
1385 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1386 return FAIL;
1387 isn->isn_arg.outer.outer_idx = idx;
1388 isn->isn_arg.outer.outer_depth = nesting;
1389
1390 return OK;
1391}
1392
1393/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001394 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001395 */
1396 static int
1397generate_LOADV(
1398 cctx_T *cctx,
1399 char_u *name,
1400 int error)
1401{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001402 int di_flags;
1403 int vidx = find_vim_var(name, &di_flags);
1404 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001405
Bram Moolenaar080457c2020-03-03 21:53:32 +01001406 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001407 if (vidx < 0)
1408 {
1409 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001410 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001411 return FAIL;
1412 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001413 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001414
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001415 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001416}
1417
1418/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001419 * Generate an ISN_UNLET instruction.
1420 */
1421 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001422generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001423{
1424 isn_T *isn;
1425
1426 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001427 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001428 return FAIL;
1429 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1430 isn->isn_arg.unlet.ul_forceit = forceit;
1431
1432 return OK;
1433}
1434
1435/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001436 * Generate an ISN_LOCKCONST instruction.
1437 */
1438 static int
1439generate_LOCKCONST(cctx_T *cctx)
1440{
1441 isn_T *isn;
1442
1443 RETURN_OK_IF_SKIP(cctx);
1444 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1445 return FAIL;
1446 return OK;
1447}
1448
1449/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 * Generate an ISN_LOADS instruction.
1451 */
1452 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001453generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001455 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001457 int sid,
1458 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459{
1460 isn_T *isn;
1461
Bram Moolenaar080457c2020-03-03 21:53:32 +01001462 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001463 if (isn_type == ISN_LOADS)
1464 isn = generate_instr_type(cctx, isn_type, type);
1465 else
1466 isn = generate_instr_drop(cctx, isn_type, 1);
1467 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001469 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1470 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471
1472 return OK;
1473}
1474
1475/*
1476 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1477 */
1478 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001479generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 cctx_T *cctx,
1481 isntype_T isn_type,
1482 int sid,
1483 int idx,
1484 type_T *type)
1485{
1486 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001487 scriptref_T *sref;
1488 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489
Bram Moolenaar080457c2020-03-03 21:53:32 +01001490 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 if (isn_type == ISN_LOADSCRIPT)
1492 isn = generate_instr_type(cctx, isn_type, type);
1493 else
1494 isn = generate_instr_drop(cctx, isn_type, 1);
1495 if (isn == NULL)
1496 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001497
1498 // This requires three arguments, which doesn't fit in an instruction, thus
1499 // we need to allocate a struct for this.
1500 sref = ALLOC_ONE(scriptref_T);
1501 if (sref == NULL)
1502 return FAIL;
1503 isn->isn_arg.script.scriptref = sref;
1504 sref->sref_sid = sid;
1505 sref->sref_idx = idx;
1506 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001507 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 return OK;
1509}
1510
1511/*
1512 * Generate an ISN_NEWLIST instruction.
1513 */
1514 static int
1515generate_NEWLIST(cctx_T *cctx, int count)
1516{
1517 isn_T *isn;
1518 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 type_T *type;
1520 type_T *member;
1521
Bram Moolenaar080457c2020-03-03 21:53:32 +01001522 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1524 return FAIL;
1525 isn->isn_arg.number = count;
1526
Bram Moolenaar127542b2020-08-09 17:22:04 +02001527 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001528 if (count == 0)
1529 member = &t_void;
1530 else
1531 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001532 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1533 cctx->ctx_type_list);
1534 type = get_list_type(member, cctx->ctx_type_list);
1535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 // drop the value types
1537 stack->ga_len -= count;
1538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 // add the list type to the type stack
1540 if (ga_grow(stack, 1) == FAIL)
1541 return FAIL;
1542 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1543 ++stack->ga_len;
1544
1545 return OK;
1546}
1547
1548/*
1549 * Generate an ISN_NEWDICT instruction.
1550 */
1551 static int
1552generate_NEWDICT(cctx_T *cctx, int count)
1553{
1554 isn_T *isn;
1555 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 type_T *type;
1557 type_T *member;
1558
Bram Moolenaar080457c2020-03-03 21:53:32 +01001559 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1561 return FAIL;
1562 isn->isn_arg.number = count;
1563
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001564 if (count == 0)
1565 member = &t_void;
1566 else
1567 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001568 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1569 cctx->ctx_type_list);
1570 type = get_dict_type(member, cctx->ctx_type_list);
1571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 // drop the key and value types
1573 stack->ga_len -= 2 * count;
1574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 // add the dict type to the type stack
1576 if (ga_grow(stack, 1) == FAIL)
1577 return FAIL;
1578 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1579 ++stack->ga_len;
1580
1581 return OK;
1582}
1583
1584/*
1585 * Generate an ISN_FUNCREF instruction.
1586 */
1587 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001588generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589{
1590 isn_T *isn;
1591 garray_T *stack = &cctx->ctx_type_stack;
1592
Bram Moolenaar080457c2020-03-03 21:53:32 +01001593 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1595 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001596 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001597 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001599 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001600 // the nested context, including this one.
1601 if (ufunc->uf_flags & FC_CLOSURE)
1602 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 if (ga_grow(stack, 1) == FAIL)
1605 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001606 ((type_T **)stack->ga_data)[stack->ga_len] =
1607 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 ++stack->ga_len;
1609
1610 return OK;
1611}
1612
1613/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001614 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001615 * "lambda_name" and "func_name" must be in allocated memory and will be
1616 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001617 */
1618 static int
1619generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1620{
1621 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001622
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001623 if (cctx->ctx_skip == SKIP_YES)
1624 {
1625 vim_free(lambda_name);
1626 vim_free(func_name);
1627 return OK;
1628 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001629 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001630 {
1631 vim_free(lambda_name);
1632 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001633 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001634 }
1635 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001636 isn->isn_arg.newfunc.nf_global = func_name;
1637
1638 return OK;
1639}
1640
1641/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001642 * Generate an ISN_DEF instruction: list functions
1643 */
1644 static int
1645generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1646{
1647 isn_T *isn;
1648
1649 RETURN_OK_IF_SKIP(cctx);
1650 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1651 return FAIL;
1652 if (len > 0)
1653 {
1654 isn->isn_arg.string = vim_strnsave(name, len);
1655 if (isn->isn_arg.string == NULL)
1656 return FAIL;
1657 }
1658 return OK;
1659}
1660
1661/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 * Generate an ISN_JUMP instruction.
1663 */
1664 static int
1665generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1666{
1667 isn_T *isn;
1668 garray_T *stack = &cctx->ctx_type_stack;
1669
Bram Moolenaar080457c2020-03-03 21:53:32 +01001670 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1672 return FAIL;
1673 isn->isn_arg.jump.jump_when = when;
1674 isn->isn_arg.jump.jump_where = where;
1675
1676 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1677 --stack->ga_len;
1678
1679 return OK;
1680}
1681
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001682/*
1683 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1684 */
1685 static int
1686generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1687{
1688 isn_T *isn;
1689
1690 RETURN_OK_IF_SKIP(cctx);
1691 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1692 return FAIL;
1693 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1694 // jump_where is set later
1695 return OK;
1696}
1697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 static int
1699generate_FOR(cctx_T *cctx, int loop_idx)
1700{
1701 isn_T *isn;
1702 garray_T *stack = &cctx->ctx_type_stack;
1703
Bram Moolenaar080457c2020-03-03 21:53:32 +01001704 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1706 return FAIL;
1707 isn->isn_arg.forloop.for_idx = loop_idx;
1708
1709 if (ga_grow(stack, 1) == FAIL)
1710 return FAIL;
1711 // type doesn't matter, will be stored next
1712 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1713 ++stack->ga_len;
1714
1715 return OK;
1716}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001717/*
1718 * Generate an ISN_TRYCONT instruction.
1719 */
1720 static int
1721generate_TRYCONT(cctx_T *cctx, int levels, int where)
1722{
1723 isn_T *isn;
1724
1725 RETURN_OK_IF_SKIP(cctx);
1726 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1727 return FAIL;
1728 isn->isn_arg.trycont.tct_levels = levels;
1729 isn->isn_arg.trycont.tct_where = where;
1730
1731 return OK;
1732}
1733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734
1735/*
1736 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001737 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 * Return FAIL if the number of arguments is wrong.
1739 */
1740 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001741generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742{
1743 isn_T *isn;
1744 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001745 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001746 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001747 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748
Bram Moolenaar080457c2020-03-03 21:53:32 +01001749 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001750 argoff = check_internal_func(func_idx, argcount);
1751 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 return FAIL;
1753
Bram Moolenaar389df252020-07-09 21:20:47 +02001754 if (method_call && argoff > 1)
1755 {
1756 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1757 return FAIL;
1758 isn->isn_arg.shuffle.shfl_item = argcount;
1759 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1760 }
1761
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001762 if (argcount > 0)
1763 {
1764 // Check the types of the arguments.
1765 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001766 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1767 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001768 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001769 if (internal_func_is_map(func_idx))
1770 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001771 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1774 return FAIL;
1775 isn->isn_arg.bfunc.cbf_idx = func_idx;
1776 isn->isn_arg.bfunc.cbf_argcount = argcount;
1777
Bram Moolenaar94738d82020-10-21 14:25:07 +02001778 // Drop the argument types and push the return type.
1779 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 if (ga_grow(stack, 1) == FAIL)
1781 return FAIL;
1782 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001783 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001784 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001786 if (maptype != NULL && maptype->tt_member != NULL
1787 && maptype->tt_member != &t_any)
1788 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001789 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 return OK;
1792}
1793
1794/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001795 * Generate an ISN_LISTAPPEND instruction. Works like add().
1796 * Argument count is already checked.
1797 */
1798 static int
1799generate_LISTAPPEND(cctx_T *cctx)
1800{
1801 garray_T *stack = &cctx->ctx_type_stack;
1802 type_T *list_type;
1803 type_T *item_type;
1804 type_T *expected;
1805
1806 // Caller already checked that list_type is a list.
1807 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1808 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1809 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001810 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001811 return FAIL;
1812
1813 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1814 return FAIL;
1815
1816 --stack->ga_len; // drop the argument
1817 return OK;
1818}
1819
1820/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001821 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1822 * Argument count is already checked.
1823 */
1824 static int
1825generate_BLOBAPPEND(cctx_T *cctx)
1826{
1827 garray_T *stack = &cctx->ctx_type_stack;
1828 type_T *item_type;
1829
1830 // Caller already checked that blob_type is a blob.
1831 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001832 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001833 return FAIL;
1834
1835 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1836 return FAIL;
1837
1838 --stack->ga_len; // drop the argument
1839 return OK;
1840}
1841
1842/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001843 * Return TRUE if "ufunc" should be compiled, taking into account whether
1844 * "profile" indicates profiling is to be done.
1845 */
1846 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001847func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001848{
1849 switch (ufunc->uf_def_status)
1850 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001851 case UF_TO_BE_COMPILED:
1852 return TRUE;
1853
Bram Moolenaarb2049902021-01-24 12:53:53 +01001854 case UF_COMPILED:
1855 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001856#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001857 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1858 + ufunc->uf_dfunc_idx;
1859
1860 return profile ? dfunc->df_instr_prof == NULL
1861 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001862#else
1863 break;
1864#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001865 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001866
1867 case UF_NOT_COMPILED:
1868 case UF_COMPILE_ERROR:
1869 case UF_COMPILING:
1870 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001871 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001872 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001873}
1874
1875/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 * Generate an ISN_DCALL or ISN_UCALL instruction.
1877 * Return FAIL if the number of arguments is wrong.
1878 */
1879 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001880generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881{
1882 isn_T *isn;
1883 garray_T *stack = &cctx->ctx_type_stack;
1884 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001885 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886
Bram Moolenaar080457c2020-03-03 21:53:32 +01001887 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 if (argcount > regular_args && !has_varargs(ufunc))
1889 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001890 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 return FAIL;
1892 }
1893 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1894 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001895 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 return FAIL;
1897 }
1898
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001899 if (ufunc->uf_def_status != UF_NOT_COMPILED
1900 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001901 {
1902 int i;
1903
1904 for (i = 0; i < argcount; ++i)
1905 {
1906 type_T *expected;
1907 type_T *actual;
1908
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001909 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1910 if (actual == &t_special
1911 && i >= regular_args - ufunc->uf_def_args.ga_len)
1912 {
1913 // assume v:none used for default argument value
1914 continue;
1915 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001916 if (i < regular_args)
1917 {
1918 if (ufunc->uf_arg_types == NULL)
1919 continue;
1920 expected = ufunc->uf_arg_types[i];
1921 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001922 else if (ufunc->uf_va_type == NULL
1923 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001924 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001925 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001926 else
1927 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001928 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001929 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001930 {
1931 arg_type_mismatch(expected, actual, i + 1);
1932 return FAIL;
1933 }
1934 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001935 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001936 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001937 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001938 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001939 }
1940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001942 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001943 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001945 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 {
1947 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1948 isn->isn_arg.dfunc.cdf_argcount = argcount;
1949 }
1950 else
1951 {
1952 // A user function may be deleted and redefined later, can't use the
1953 // ufunc pointer, need to look it up again at runtime.
1954 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1955 isn->isn_arg.ufunc.cuf_argcount = argcount;
1956 }
1957
1958 stack->ga_len -= argcount; // drop the arguments
1959 if (ga_grow(stack, 1) == FAIL)
1960 return FAIL;
1961 // add return value
1962 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1963 ++stack->ga_len;
1964
1965 return OK;
1966}
1967
1968/*
1969 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1970 */
1971 static int
1972generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1973{
1974 isn_T *isn;
1975 garray_T *stack = &cctx->ctx_type_stack;
1976
Bram Moolenaar080457c2020-03-03 21:53:32 +01001977 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1979 return FAIL;
1980 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1981 isn->isn_arg.ufunc.cuf_argcount = argcount;
1982
1983 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001984 if (ga_grow(stack, 1) == FAIL)
1985 return FAIL;
1986 // add return value
1987 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1988 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989
1990 return OK;
1991}
1992
1993/*
1994 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001995 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 */
1997 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001998generate_PCALL(
1999 cctx_T *cctx,
2000 int argcount,
2001 char_u *name,
2002 type_T *type,
2003 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004{
2005 isn_T *isn;
2006 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002007 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008
Bram Moolenaar080457c2020-03-03 21:53:32 +01002009 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002010
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002011 if (type->tt_type == VAR_ANY)
2012 ret_type = &t_any;
2013 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002014 {
2015 if (type->tt_argcount != -1)
2016 {
2017 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2018
2019 if (argcount < type->tt_min_argcount - varargs)
2020 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002021 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002022 return FAIL;
2023 }
2024 if (!varargs && argcount > type->tt_argcount)
2025 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002026 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002027 return FAIL;
2028 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002029 if (type->tt_args != NULL)
2030 {
2031 int i;
2032
2033 for (i = 0; i < argcount; ++i)
2034 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002035 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002036 type_T *actual = ((type_T **)stack->ga_data)[
2037 stack->ga_len + offset];
2038 type_T *expected;
2039
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002040 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002041 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002042 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002043 else if (i >= type->tt_min_argcount
2044 && actual == &t_special)
2045 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002046 else
2047 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002048 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002049 cctx, TRUE, FALSE) == FAIL)
2050 {
2051 arg_type_mismatch(expected, actual, i + 1);
2052 return FAIL;
2053 }
2054 }
2055 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002056 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002057 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002058 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002059 else
2060 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002061 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002062 return FAIL;
2063 }
2064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2066 return FAIL;
2067 isn->isn_arg.pfunc.cpf_top = at_top;
2068 isn->isn_arg.pfunc.cpf_argcount = argcount;
2069
2070 stack->ga_len -= argcount; // drop the arguments
2071
2072 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002073 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002075 // If partial is above the arguments it must be cleared and replaced with
2076 // the return value.
2077 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2078 return FAIL;
2079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 return OK;
2081}
2082
2083/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002084 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 */
2086 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002087generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088{
2089 isn_T *isn;
2090 garray_T *stack = &cctx->ctx_type_stack;
2091 type_T *type;
2092
Bram Moolenaar080457c2020-03-03 21:53:32 +01002093 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002094 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002095 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002096 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002098 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002100 if (type->tt_type != VAR_DICT && type != &t_any)
2101 {
2102 emsg(_(e_dictreq));
2103 return FAIL;
2104 }
2105 // change dict type to dict member type
2106 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002107 {
2108 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2109 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111
2112 return OK;
2113}
2114
2115/*
2116 * Generate an ISN_ECHO instruction.
2117 */
2118 static int
2119generate_ECHO(cctx_T *cctx, int with_white, int count)
2120{
2121 isn_T *isn;
2122
Bram Moolenaar080457c2020-03-03 21:53:32 +01002123 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2125 return FAIL;
2126 isn->isn_arg.echo.echo_with_white = with_white;
2127 isn->isn_arg.echo.echo_count = count;
2128
2129 return OK;
2130}
2131
Bram Moolenaarad39c092020-02-26 18:23:43 +01002132/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002133 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002134 */
2135 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002136generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002137{
2138 isn_T *isn;
2139
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002140 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002141 return FAIL;
2142 isn->isn_arg.number = count;
2143
2144 return OK;
2145}
2146
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002147/*
2148 * Generate an ISN_PUT instruction.
2149 */
2150 static int
2151generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2152{
2153 isn_T *isn;
2154
2155 RETURN_OK_IF_SKIP(cctx);
2156 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2157 return FAIL;
2158 isn->isn_arg.put.put_regname = regname;
2159 isn->isn_arg.put.put_lnum = lnum;
2160 return OK;
2161}
2162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 static int
2164generate_EXEC(cctx_T *cctx, char_u *line)
2165{
2166 isn_T *isn;
2167
Bram Moolenaar080457c2020-03-03 21:53:32 +01002168 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2170 return FAIL;
2171 isn->isn_arg.string = vim_strsave(line);
2172 return OK;
2173}
2174
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002175 static int
2176generate_EXECCONCAT(cctx_T *cctx, int count)
2177{
2178 isn_T *isn;
2179
2180 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2181 return FAIL;
2182 isn->isn_arg.number = count;
2183 return OK;
2184}
2185
Bram Moolenaar08597872020-12-10 19:43:40 +01002186/*
2187 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2188 */
2189 static int
2190generate_RANGE(cctx_T *cctx, char_u *range)
2191{
2192 isn_T *isn;
2193 garray_T *stack = &cctx->ctx_type_stack;
2194
2195 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2196 return FAIL;
2197 isn->isn_arg.string = range;
2198
2199 if (ga_grow(stack, 1) == FAIL)
2200 return FAIL;
2201 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2202 ++stack->ga_len;
2203 return OK;
2204}
2205
Bram Moolenaar792f7862020-11-23 08:31:18 +01002206 static int
2207generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2208{
2209 isn_T *isn;
2210
2211 RETURN_OK_IF_SKIP(cctx);
2212 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2213 return FAIL;
2214 isn->isn_arg.unpack.unp_count = var_count;
2215 isn->isn_arg.unpack.unp_semicolon = semicolon;
2216 return OK;
2217}
2218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002220 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002221 */
2222 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002223generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002224{
2225 isn_T *isn;
2226
Bram Moolenaarfa984412021-03-25 22:15:28 +01002227 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002228 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002229 cctx->ctx_has_cmdmod = TRUE;
2230
2231 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002232 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002233 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2234 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2235 return FAIL;
2236 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002237 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002238 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002239 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002240
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002241 return OK;
2242}
2243
2244 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002245generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002246{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002247 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2248 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002249 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002250 return OK;
2251}
2252
Bram Moolenaarfa984412021-03-25 22:15:28 +01002253 static int
2254misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002255{
2256 garray_T *instr = &cctx->ctx_instr;
2257
Bram Moolenaara91a7132021-03-25 21:12:15 +01002258 if (cctx->ctx_has_cmdmod
2259 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2260 == ISN_CMDMOD)
2261 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002262 emsg(_(e_misplaced_command_modifier));
2263 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002264 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002265 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002266}
2267
2268/*
2269 * Get the index of the current instruction.
2270 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2271 */
2272 static int
2273current_instr_idx(cctx_T *cctx)
2274{
2275 garray_T *instr = &cctx->ctx_instr;
2276 int idx = instr->ga_len;
2277
2278 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2279 .isn_type == ISN_CMDMOD)
2280 --idx;
2281#ifdef FEAT_PROFILE
2282 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2283 .isn_type == ISN_PROF_START)
2284 --idx;
2285#endif
2286 return idx;
2287}
2288
Bram Moolenaarf002a412021-01-24 13:34:18 +01002289#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002290 static void
2291may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2292{
2293 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002294 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002295}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002296#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002297
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002298/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002300 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002302 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002303reserve_local(
2304 cctx_T *cctx,
2305 char_u *name,
2306 size_t len,
2307 int isConst,
2308 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 lvar_T *lvar;
2311
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002312 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002314 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002315 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 }
2317
2318 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002319 return NULL;
2320 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002321 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002323 // Every local variable uses the next entry on the stack. We could re-use
2324 // the last ones when leaving a scope, but then variables used in a closure
2325 // might get overwritten. To keep things simple do not re-use stack
2326 // entries. This is less efficient, but memory is cheap these days.
2327 lvar->lv_idx = cctx->ctx_locals_count++;
2328
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002329 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 lvar->lv_const = isConst;
2331 lvar->lv_type = type;
2332
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002333 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334}
2335
2336/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002337 * Remove local variables above "new_top".
2338 */
2339 static void
2340unwind_locals(cctx_T *cctx, int new_top)
2341{
2342 if (cctx->ctx_locals.ga_len > new_top)
2343 {
2344 int idx;
2345 lvar_T *lvar;
2346
2347 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2348 {
2349 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2350 vim_free(lvar->lv_name);
2351 }
2352 }
2353 cctx->ctx_locals.ga_len = new_top;
2354}
2355
2356/*
2357 * Free all local variables.
2358 */
2359 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002360free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002361{
2362 unwind_locals(cctx, 0);
2363 ga_clear(&cctx->ctx_locals);
2364}
2365
2366/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002367 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2368 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2369 * error if the variable was defined with :const.
2370 */
2371 static int
2372check_item_writable(svar_T *sv, int check_writable, char_u *name)
2373{
2374 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2375 || (check_writable == ASSIGN_FINAL
2376 && sv->sv_const == ASSIGN_CONST))
2377 {
2378 semsg(_(e_readonlyvar), name);
2379 return FAIL;
2380 }
2381 return OK;
2382}
2383
2384/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002386 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 * Returns the index in "sn_var_vals" if found.
2388 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002389 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 */
2391 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002392get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393{
2394 hashtab_T *ht;
2395 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002396 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002397 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 int idx;
2399
Bram Moolenaare3d46852020-08-29 13:39:17 +02002400 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002402 if (sid == current_sctx.sc_sid)
2403 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002404 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002405
2406 if (sav == NULL)
2407 return -2;
2408 idx = sav->sav_var_vals_idx;
2409 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002410 if (check_item_writable(sv, check_writable, name) == FAIL)
2411 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002412 return idx;
2413 }
2414
2415 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 ht = &SCRIPT_VARS(sid);
2417 di = find_var_in_ht(ht, 0, name, TRUE);
2418 if (di == NULL)
2419 return -2;
2420
2421 // Now find the svar_T index in sn_var_vals.
2422 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2423 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002424 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 if (sv->sv_tv == &di->di_tv)
2426 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002427 if (check_item_writable(sv, check_writable, name) == FAIL)
2428 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 return idx;
2430 }
2431 }
2432 return -1;
2433}
2434
2435/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002436 * Find "name" in imported items of the current script or in "cctx" if not
2437 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 */
2439 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002440find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 int idx;
2443
Bram Moolenaare3d46852020-08-29 13:39:17 +02002444 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002445 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 if (cctx != NULL)
2447 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2448 {
2449 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2450 + idx;
2451
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002452 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2453 : STRLEN(import->imp_name) == len
2454 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455 return import;
2456 }
2457
Bram Moolenaarefa94442020-08-08 22:16:00 +02002458 return find_imported_in_script(name, len, current_sctx.sc_sid);
2459}
2460
2461 imported_T *
2462find_imported_in_script(char_u *name, size_t len, int sid)
2463{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002464 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002465 int idx;
2466
Bram Moolenaare3d46852020-08-29 13:39:17 +02002467 if (!SCRIPT_ID_VALID(sid))
2468 return NULL;
2469 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2471 {
2472 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2473
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002474 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2475 : STRLEN(import->imp_name) == len
2476 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 return import;
2478 }
2479 return NULL;
2480}
2481
2482/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002483 * Free all imported variables.
2484 */
2485 static void
2486free_imported(cctx_T *cctx)
2487{
2488 int idx;
2489
2490 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2491 {
2492 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2493
2494 vim_free(import->imp_name);
2495 }
2496 ga_clear(&cctx->ctx_imports);
2497}
2498
2499/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002500 * Return a pointer to the next line that isn't empty or only contains a
2501 * comment. Skips over white space.
2502 * Returns NULL if there is none.
2503 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002504 char_u *
2505peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002506{
2507 int lnum = cctx->ctx_lnum;
2508
2509 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2510 {
2511 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002512 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002513
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002514 // ignore NULLs inserted for continuation lines
2515 if (line != NULL)
2516 {
2517 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002518 if (vim9_bad_comment(p))
2519 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002520 if (*p != NUL && !vim9_comment_start(p))
2521 return p;
2522 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002523 }
2524 return NULL;
2525}
2526
2527/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002528 * Called when checking for a following operator at "arg". When the rest of
2529 * the line is empty or only a comment, peek the next line. If there is a next
2530 * line return a pointer to it and set "nextp".
2531 * Otherwise skip over white space.
2532 */
2533 static char_u *
2534may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2535{
2536 char_u *p = skipwhite(arg);
2537
2538 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002539 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002540 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002541 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002542 if (*nextp != NULL)
2543 return *nextp;
2544 }
2545 return p;
2546}
2547
2548/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002549 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002550 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002551 * Returns NULL when at the end.
2552 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002553 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002554next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002555{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002556 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002557
2558 do
2559 {
2560 ++cctx->ctx_lnum;
2561 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002562 {
2563 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002564 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002565 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002566 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002567 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002568 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002569 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002570 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002571 return line;
2572}
2573
2574/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002575 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002576 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002577 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002578 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2579 */
2580 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002581may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002582{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002583 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002584 if (vim9_bad_comment(*arg))
2585 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002586 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002587 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002588 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002589
2590 if (next == NULL)
2591 return FAIL;
2592 *arg = skipwhite(next);
2593 }
2594 return OK;
2595}
2596
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002597/*
2598 * Idem, and give an error when failed.
2599 */
2600 static int
2601may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2602{
2603 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2604 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002605 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002606 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002607 return FAIL;
2608 }
2609 return OK;
2610}
2611
2612
Bram Moolenaara5565e42020-05-09 15:44:01 +02002613// Structure passed between the compile_expr* functions to keep track of
2614// constants that have been parsed but for which no code was produced yet. If
2615// possible expressions on these constants are applied at compile time. If
2616// that is not possible, the code to push the constants needs to be generated
2617// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002618// Using 50 should be more than enough of 5 levels of ().
2619#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002620typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002621 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002622 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002623 int pp_is_const; // all generated code was constants, used for a
2624 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002625} ppconst_T;
2626
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002627static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002628static int compile_expr0(char_u **arg, cctx_T *cctx);
2629static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2630
Bram Moolenaara5565e42020-05-09 15:44:01 +02002631/*
2632 * Generate a PUSH instruction for "tv".
2633 * "tv" will be consumed or cleared.
2634 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2635 */
2636 static int
2637generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2638{
2639 if (tv != NULL)
2640 {
2641 switch (tv->v_type)
2642 {
2643 case VAR_UNKNOWN:
2644 break;
2645 case VAR_BOOL:
2646 generate_PUSHBOOL(cctx, tv->vval.v_number);
2647 break;
2648 case VAR_SPECIAL:
2649 generate_PUSHSPEC(cctx, tv->vval.v_number);
2650 break;
2651 case VAR_NUMBER:
2652 generate_PUSHNR(cctx, tv->vval.v_number);
2653 break;
2654#ifdef FEAT_FLOAT
2655 case VAR_FLOAT:
2656 generate_PUSHF(cctx, tv->vval.v_float);
2657 break;
2658#endif
2659 case VAR_BLOB:
2660 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2661 tv->vval.v_blob = NULL;
2662 break;
2663 case VAR_STRING:
2664 generate_PUSHS(cctx, tv->vval.v_string);
2665 tv->vval.v_string = NULL;
2666 break;
2667 default:
2668 iemsg("constant type not supported");
2669 clear_tv(tv);
2670 return FAIL;
2671 }
2672 tv->v_type = VAR_UNKNOWN;
2673 }
2674 return OK;
2675}
2676
2677/*
2678 * Generate code for any ppconst entries.
2679 */
2680 static int
2681generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2682{
2683 int i;
2684 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002685 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002686
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002687 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002688 for (i = 0; i < ppconst->pp_used; ++i)
2689 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2690 ret = FAIL;
2691 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002692 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002693 return ret;
2694}
2695
2696/*
2697 * Clear ppconst constants. Used when failing.
2698 */
2699 static void
2700clear_ppconst(ppconst_T *ppconst)
2701{
2702 int i;
2703
2704 for (i = 0; i < ppconst->pp_used; ++i)
2705 clear_tv(&ppconst->pp_tv[i]);
2706 ppconst->pp_used = 0;
2707}
2708
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002709/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002710 * Compile getting a member from a list/dict/string/blob. Stack has the
2711 * indexable value and the index.
2712 */
2713 static int
2714compile_member(int is_slice, cctx_T *cctx)
2715{
2716 type_T **typep;
2717 garray_T *stack = &cctx->ctx_type_stack;
2718 vartype_T vtype;
2719 type_T *valtype;
2720
2721 // We can index a list and a dict. If we don't know the type
2722 // we can use the index value type.
2723 // TODO: If we don't know use an instruction to figure it out at
2724 // runtime.
2725 typep = ((type_T **)stack->ga_data) + stack->ga_len
2726 - (is_slice ? 3 : 2);
2727 vtype = (*typep)->tt_type;
2728 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2729 // If the index is a string, the variable must be a Dict.
2730 if (*typep == &t_any && valtype == &t_string)
2731 vtype = VAR_DICT;
2732 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
2733 {
2734 if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2735 return FAIL;
2736 if (is_slice)
2737 {
2738 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2739 if (need_type(valtype, &t_number, -2, 0, cctx,
2740 FALSE, FALSE) == FAIL)
2741 return FAIL;
2742 }
2743 }
2744
2745 if (vtype == VAR_DICT)
2746 {
2747 if (is_slice)
2748 {
2749 emsg(_(e_cannot_slice_dictionary));
2750 return FAIL;
2751 }
2752 if ((*typep)->tt_type == VAR_DICT)
2753 {
2754 *typep = (*typep)->tt_member;
2755 if (*typep == &t_unknown)
2756 // empty dict was used
2757 *typep = &t_any;
2758 }
2759 else
2760 {
2761 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2762 FALSE, FALSE) == FAIL)
2763 return FAIL;
2764 *typep = &t_any;
2765 }
2766 if (may_generate_2STRING(-1, cctx) == FAIL)
2767 return FAIL;
2768 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2769 return FAIL;
2770 }
2771 else if (vtype == VAR_STRING)
2772 {
2773 *typep = &t_string;
2774 if ((is_slice
2775 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2776 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2777 return FAIL;
2778 }
2779 else if (vtype == VAR_BLOB)
2780 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002781 if (is_slice)
2782 {
2783 *typep = &t_blob;
2784 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2785 return FAIL;
2786 }
2787 else
2788 {
2789 *typep = &t_number;
2790 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2791 return FAIL;
2792 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002793 }
2794 else if (vtype == VAR_LIST || *typep == &t_any)
2795 {
2796 if (is_slice)
2797 {
2798 if (generate_instr_drop(cctx,
2799 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2800 2) == FAIL)
2801 return FAIL;
2802 }
2803 else
2804 {
2805 if ((*typep)->tt_type == VAR_LIST)
2806 {
2807 *typep = (*typep)->tt_member;
2808 if (*typep == &t_unknown)
2809 // empty list was used
2810 *typep = &t_any;
2811 }
2812 if (generate_instr_drop(cctx,
2813 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
2814 return FAIL;
2815 }
2816 }
2817 else
2818 {
2819 emsg(_(e_string_list_dict_or_blob_required));
2820 return FAIL;
2821 }
2822 return OK;
2823}
2824
2825/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002826 * Generate an instruction to load script-local variable "name", without the
2827 * leading "s:".
2828 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 */
2830 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002831compile_load_scriptvar(
2832 cctx_T *cctx,
2833 char_u *name, // variable NUL terminated
2834 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002835 char_u **end, // end of variable
2836 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002838 scriptitem_T *si;
2839 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 imported_T *import;
2841
Bram Moolenaare3d46852020-08-29 13:39:17 +02002842 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2843 return FAIL;
2844 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002845 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002846 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002848 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002849 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2850 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 }
2852 if (idx >= 0)
2853 {
2854 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2855
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002856 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 current_sctx.sc_sid, idx, sv->sv_type);
2858 return OK;
2859 }
2860
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002861 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 if (import != NULL)
2863 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002864 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002865 {
2866 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002867 char_u *exp_name;
2868 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002869 ufunc_T *ufunc;
2870 type_T *type;
2871
2872 // Used "import * as Name", need to lookup the member.
2873 if (*p != '.')
2874 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002875 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002876 return FAIL;
2877 }
2878 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002879 if (VIM_ISWHITE(*p))
2880 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002881 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002882 return FAIL;
2883 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002884
Bram Moolenaar1c991142020-07-04 13:15:31 +02002885 // isolate one name
2886 exp_name = p;
2887 while (eval_isnamec(*p))
2888 ++p;
2889 cc = *p;
2890 *p = NUL;
2891
Bram Moolenaaredba7072021-03-13 21:14:18 +01002892 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2893 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002894 *p = cc;
2895 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002896 *end = p;
2897
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002898 if (idx < 0)
2899 {
2900 if (*p == '(' && ufunc != NULL)
2901 {
2902 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2903 return OK;
2904 }
2905 return FAIL;
2906 }
2907
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002908 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2909 import->imp_sid,
2910 idx,
2911 type);
2912 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002913 else if (import->imp_funcname != NULL)
2914 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002915 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002916 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2917 import->imp_sid,
2918 import->imp_var_vals_idx,
2919 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 return OK;
2921 }
2922
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002923 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002924 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 return FAIL;
2926}
2927
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002928 static int
2929generate_funcref(cctx_T *cctx, char_u *name)
2930{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002931 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002932
2933 if (ufunc == NULL)
2934 return FAIL;
2935
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002936 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002937 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2938 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002939 == FAIL)
2940 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002941 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002942}
2943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944/*
2945 * Compile a variable name into a load instruction.
2946 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002947 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 * When "error" is FALSE do not give an error when not found.
2949 */
2950 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002951compile_load(
2952 char_u **arg,
2953 char_u *end_arg,
2954 cctx_T *cctx,
2955 int is_expr,
2956 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957{
2958 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002959 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002960 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002962 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963
2964 if (*(*arg + 1) == ':')
2965 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002966 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002967 {
2968 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969
Bram Moolenaarfa596382021-04-07 21:58:16 +02002970 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002971 switch (**arg)
2972 {
2973 case 'g': isn_type = ISN_LOADGDICT; break;
2974 case 'w': isn_type = ISN_LOADWDICT; break;
2975 case 't': isn_type = ISN_LOADTDICT; break;
2976 case 'b': isn_type = ISN_LOADBDICT; break;
2977 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002978 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002979 goto theend;
2980 }
2981 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2982 goto theend;
2983 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002984 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 else
2986 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002987 isntype_T isn_type = ISN_DROP;
2988
Bram Moolenaarfa596382021-04-07 21:58:16 +02002989 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002990 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2991 if (name == NULL)
2992 return FAIL;
2993
2994 switch (**arg)
2995 {
2996 case 'v': res = generate_LOADV(cctx, name, error);
2997 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02002998 case 's': if (is_expr && ASCII_ISUPPER(*name)
2999 && find_func(name, FALSE, cctx) != NULL)
3000 res = generate_funcref(cctx, name);
3001 else
3002 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003003 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003004 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003005 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003006 {
3007 if (is_expr && ASCII_ISUPPER(*name)
3008 && find_func(name, FALSE, cctx) != NULL)
3009 res = generate_funcref(cctx, name);
3010 else
3011 isn_type = ISN_LOADG;
3012 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003013 else
3014 {
3015 isn_type = ISN_LOADAUTO;
3016 vim_free(name);
3017 name = vim_strnsave(*arg, end - *arg);
3018 if (name == NULL)
3019 return FAIL;
3020 }
3021 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003022 case 'w': isn_type = ISN_LOADW; break;
3023 case 't': isn_type = ISN_LOADT; break;
3024 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003025 default: // cannot happen, just in case
3026 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003027 goto theend;
3028 }
3029 if (isn_type != ISN_DROP)
3030 {
3031 // Global, Buffer-local, Window-local and Tabpage-local
3032 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003033 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003034 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 }
3037 }
3038 else
3039 {
3040 size_t len = end - *arg;
3041 int idx;
3042 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003043 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044
3045 name = vim_strnsave(*arg, end - *arg);
3046 if (name == NULL)
3047 return FAIL;
3048
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003049 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003051 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003052 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 }
3054 else
3055 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003056 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003057
Bram Moolenaar709664c2020-12-12 14:33:41 +01003058 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003060 type = lvar.lv_type;
3061 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003062 if (lvar.lv_from_outer != 0)
3063 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003064 else
3065 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 }
3067 else
3068 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003069 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003070 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003071 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003072 || find_imported(name, 0, cctx) != NULL)
3073 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003074
Bram Moolenaar0f769812020-09-12 18:32:34 +02003075 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003076 // uppercase letter it can be a user defined function.
3077 // generate_funcref() will fail if the function can't be found.
3078 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003079 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 }
3081 }
3082 if (gen_load)
3083 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003084 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003085 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003086 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003087 cctx->ctx_outer_used = TRUE;
3088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 }
3090
3091 *arg = end;
3092
3093theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003094 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003095 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 vim_free(name);
3097 return res;
3098}
3099
3100/*
3101 * Compile the argument expressions.
3102 * "arg" points to just after the "(" and is advanced to after the ")"
3103 */
3104 static int
3105compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3106{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003107 char_u *p = *arg;
3108 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003109 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110
Bram Moolenaare6085c52020-04-12 20:19:16 +02003111 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003113 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3114 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003115 if (*p == ')')
3116 {
3117 *arg = p + 1;
3118 return OK;
3119 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003120 if (must_end)
3121 {
3122 semsg(_(e_missing_comma_before_argument_str), p);
3123 return FAIL;
3124 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003125
Bram Moolenaara5565e42020-05-09 15:44:01 +02003126 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 return FAIL;
3128 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003129
3130 if (*p != ',' && *skipwhite(p) == ',')
3131 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003132 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003133 p = skipwhite(p);
3134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003136 {
3137 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003138 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003139 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003140 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003141 else
3142 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003143 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003144 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003146failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003147 emsg(_(e_missing_close));
3148 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149}
3150
3151/*
3152 * Compile a function call: name(arg1, arg2)
3153 * "arg" points to "name", "arg + varlen" to the "(".
3154 * "argcount_init" is 1 for "value->method()"
3155 * Instructions:
3156 * EVAL arg1
3157 * EVAL arg2
3158 * BCALL / DCALL / UCALL
3159 */
3160 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003161compile_call(
3162 char_u **arg,
3163 size_t varlen,
3164 cctx_T *cctx,
3165 ppconst_T *ppconst,
3166 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167{
3168 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003169 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 int argcount = argcount_init;
3171 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003172 char_u fname_buf[FLEN_FIXED + 1];
3173 char_u *tofree = NULL;
3174 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003175 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003176 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003177 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178
Bram Moolenaara5565e42020-05-09 15:44:01 +02003179 // we can evaluate "has('name')" at compile time
3180 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3181 {
3182 char_u *s = skipwhite(*arg + varlen + 1);
3183 typval_T argvars[2];
3184
3185 argvars[0].v_type = VAR_UNKNOWN;
3186 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003187 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003188 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003189 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003190 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003191 if (*s == ')' && argvars[0].v_type == VAR_STRING
3192 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003193 {
3194 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3195
3196 *arg = s + 1;
3197 argvars[1].v_type = VAR_UNKNOWN;
3198 tv->v_type = VAR_NUMBER;
3199 tv->vval.v_number = 0;
3200 f_has(argvars, tv);
3201 clear_tv(&argvars[0]);
3202 ++ppconst->pp_used;
3203 return OK;
3204 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003205 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003206 }
3207
3208 if (generate_ppconst(cctx, ppconst) == FAIL)
3209 return FAIL;
3210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 if (varlen >= sizeof(namebuf))
3212 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003213 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 return FAIL;
3215 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003216 vim_strncpy(namebuf, *arg, varlen);
3217 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218
3219 *arg = skipwhite(*arg + varlen + 1);
3220 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003221 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222
Bram Moolenaar03290b82020-12-19 16:30:44 +01003223 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003224 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 {
3226 int idx;
3227
3228 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003229 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003231 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003232 if (STRCMP(name, "flatten") == 0)
3233 {
3234 emsg(_(e_cannot_use_flatten_in_vim9_script));
3235 goto theend;
3236 }
3237
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003238 if (STRCMP(name, "add") == 0 && argcount == 2)
3239 {
3240 garray_T *stack = &cctx->ctx_type_stack;
3241 type_T *type = ((type_T **)stack->ga_data)[
3242 stack->ga_len - 2];
3243
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003244 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003245 if (type->tt_type == VAR_LIST)
3246 {
3247 // inline "add(list, item)" so that the type can be checked
3248 res = generate_LISTAPPEND(cctx);
3249 idx = -1;
3250 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003251 else if (type->tt_type == VAR_BLOB)
3252 {
3253 // inline "add(blob, nr)" so that the type can be checked
3254 res = generate_BLOBAPPEND(cctx);
3255 idx = -1;
3256 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003257 }
3258
3259 if (idx >= 0)
3260 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3261 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003262 else
3263 semsg(_(e_unknownfunc), namebuf);
3264 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 }
3266
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003267 // An argument or local variable can be a function reference, this
3268 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003269 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003270 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003271 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003272 // If we can find the function by name generate the right call.
3273 // Skip global functions here, a local funcref takes precedence.
3274 ufunc = find_func(name, FALSE, cctx);
3275 if (ufunc != NULL && !func_is_global(ufunc))
3276 {
3277 res = generate_CALL(cctx, ufunc, argcount);
3278 goto theend;
3279 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281
3282 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003283 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003284 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003286 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003287 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003288 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003289 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003290 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003291
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003292 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003293 goto theend;
3294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295
Bram Moolenaar0f769812020-09-12 18:32:34 +02003296 // If we can find a global function by name generate the right call.
3297 if (ufunc != NULL)
3298 {
3299 res = generate_CALL(cctx, ufunc, argcount);
3300 goto theend;
3301 }
3302
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003303 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003304 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003305 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003306 res = generate_UCALL(cctx, name, argcount);
3307 else
3308 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003309
3310theend:
3311 vim_free(tofree);
3312 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313}
3314
3315// like NAMESPACE_CHAR but with 'a' and 'l'.
3316#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3317
3318/*
3319 * Find the end of a variable or function name. Unlike find_name_end() this
3320 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003321 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 * Return a pointer to just after the name. Equal to "arg" if there is no
3323 * valid name.
3324 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003325 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003326to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327{
3328 char_u *p;
3329
3330 // Quick check for valid starting character.
3331 if (!eval_isnamec1(*arg))
3332 return arg;
3333
3334 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3335 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3336 // and can be used in slice "[n:]".
3337 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003338 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3340 break;
3341 return p;
3342}
3343
3344/*
3345 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003346 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 */
3348 char_u *
3349to_name_const_end(char_u *arg)
3350{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003351 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 typval_T rettv;
3353
3354 if (p == arg && *arg == '[')
3355 {
3356
3357 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003358 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 p = arg;
3360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 return p;
3362}
3363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364/*
3365 * parse a list: [expr, expr]
3366 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003367 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 */
3369 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003370compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371{
3372 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003373 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003375 int is_const;
3376 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003378 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003380 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003381 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003382 semsg(_(e_list_end), *arg);
3383 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003384 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003385 if (*p == ',')
3386 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003387 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003388 return FAIL;
3389 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003390 if (*p == ']')
3391 {
3392 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003393 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003394 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003395 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003396 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003397 if (!is_const)
3398 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 ++count;
3400 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003401 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003403 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3404 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003405 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003406 return FAIL;
3407 }
3408 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003409 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 p = skipwhite(p);
3411 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003412 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003414 ppconst->pp_is_const = is_all_const;
3415 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416}
3417
3418/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003419 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003420 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003421 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 */
3423 static int
3424compile_lambda(char_u **arg, cctx_T *cctx)
3425{
Bram Moolenaare462f522020-12-27 14:43:30 +01003426 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 typval_T rettv;
3428 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003429 evalarg_T evalarg;
3430
3431 CLEAR_FIELD(evalarg);
3432 evalarg.eval_flags = EVAL_EVALUATE;
3433 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434
3435 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003436 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3437 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003438 {
3439 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003440 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003441 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003442
Bram Moolenaar65c44152020-12-24 15:14:01 +01003443 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003445 ++ufunc->uf_refcount;
3446 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447
Bram Moolenaar65c44152020-12-24 15:14:01 +01003448 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003449 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003451 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3452 // points into it. Point to the original line to avoid a dangling pointer.
3453 if (evalarg.eval_tofree_cmdline != NULL)
3454 {
3455 size_t off = *arg - evalarg.eval_tofree_cmdline;
3456
3457 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3458 + off;
3459 }
3460
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003461 clear_evalarg(&evalarg, NULL);
3462
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003463 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003464 {
3465 // The return type will now be known.
3466 set_function_type(ufunc);
3467
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003468 // The function reference count will be 1. When the ISN_FUNCREF
3469 // instruction is deleted the reference count is decremented and the
3470 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003471 return generate_FUNCREF(cctx, ufunc);
3472 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003473
3474 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 return FAIL;
3476}
3477
3478/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003479 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003481 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 */
3483 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003484compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485{
3486 garray_T *instr = &cctx->ctx_instr;
3487 int count = 0;
3488 dict_T *d = dict_alloc();
3489 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003490 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003491 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003492 int is_const;
3493 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494
3495 if (d == NULL)
3496 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003497 if (generate_ppconst(cctx, ppconst) == FAIL)
3498 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003499 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003501 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502
Bram Moolenaar23c55272020-06-21 16:58:13 +02003503 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003504 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003505 *arg = NULL;
3506 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003507 }
3508
3509 if (**arg == '}')
3510 break;
3511
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003512 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003514 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003516 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003517 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003518 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003521 if (isn->isn_type == ISN_PUSHNR)
3522 {
3523 char buf[NUMBUFLEN];
3524
3525 // Convert to string at compile time.
3526 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3527 isn->isn_type = ISN_PUSHS;
3528 isn->isn_arg.string = vim_strsave((char_u *)buf);
3529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 if (isn->isn_type == ISN_PUSHS)
3531 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003532 else if (may_generate_2STRING(-1, cctx) == FAIL)
3533 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003534 *arg = skipwhite(*arg);
3535 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003536 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003537 emsg(_(e_missing_matching_bracket_after_dict_key));
3538 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003539 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003540 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003542 else
3543 {
3544 // {"name": value},
3545 // {'name': value},
3546 // {name: value} use "name" as a literal key
3547 key = get_literal_key(arg);
3548 if (key == NULL)
3549 return FAIL;
3550 if (generate_PUSHS(cctx, key) == FAIL)
3551 return FAIL;
3552 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553
3554 // Check for duplicate keys, if using string keys.
3555 if (key != NULL)
3556 {
3557 item = dict_find(d, key, -1);
3558 if (item != NULL)
3559 {
3560 semsg(_(e_duplicate_key), key);
3561 goto failret;
3562 }
3563 item = dictitem_alloc(key);
3564 if (item != NULL)
3565 {
3566 item->di_tv.v_type = VAR_UNKNOWN;
3567 item->di_tv.v_lock = 0;
3568 if (dict_add(d, item) == FAIL)
3569 dictitem_free(item);
3570 }
3571 }
3572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 if (**arg != ':')
3574 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003575 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003576 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003577 else
3578 semsg(_(e_missing_dict_colon), *arg);
3579 return FAIL;
3580 }
3581 whitep = *arg + 1;
3582 if (!IS_WHITE_OR_NUL(*whitep))
3583 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003584 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 return FAIL;
3586 }
3587
Bram Moolenaar23c55272020-06-21 16:58:13 +02003588 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003589 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003590 *arg = NULL;
3591 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003592 }
3593
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003594 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003596 if (!is_const)
3597 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 ++count;
3599
Bram Moolenaar2c330432020-04-13 14:41:35 +02003600 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003601 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003602 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003603 *arg = NULL;
3604 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 if (**arg == '}')
3607 break;
3608 if (**arg != ',')
3609 {
3610 semsg(_(e_missing_dict_comma), *arg);
3611 goto failret;
3612 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003613 if (IS_WHITE_OR_NUL(*whitep))
3614 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003615 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003616 return FAIL;
3617 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003618 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003619 if (!IS_WHITE_OR_NUL(*whitep))
3620 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003621 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003622 return FAIL;
3623 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003624 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 }
3626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 *arg = *arg + 1;
3628
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003629 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003630 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003631 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003632 *arg += STRLEN(*arg);
3633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003635 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 return generate_NEWDICT(cctx, count);
3637
3638failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003639 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003640 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003641 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003642 *arg = (char_u *)"";
3643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 dict_unref(d);
3645 return FAIL;
3646}
3647
3648/*
3649 * Compile "&option".
3650 */
3651 static int
3652compile_get_option(char_u **arg, cctx_T *cctx)
3653{
3654 typval_T rettv;
3655 char_u *start = *arg;
3656 int ret;
3657
3658 // parse the option and get the current value to get the type.
3659 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003660 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 if (ret == OK)
3662 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003663 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003664 char_u *name = vim_strnsave(start, *arg - start);
3665 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3666 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667
3668 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3669 vim_free(name);
3670 }
3671 clear_tv(&rettv);
3672
3673 return ret;
3674}
3675
3676/*
3677 * Compile "$VAR".
3678 */
3679 static int
3680compile_get_env(char_u **arg, cctx_T *cctx)
3681{
3682 char_u *start = *arg;
3683 int len;
3684 int ret;
3685 char_u *name;
3686
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 ++*arg;
3688 len = get_env_len(arg);
3689 if (len == 0)
3690 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003691 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 return FAIL;
3693 }
3694
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003695 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 name = vim_strnsave(start, len + 1);
3697 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3698 vim_free(name);
3699 return ret;
3700}
3701
3702/*
3703 * Compile "@r".
3704 */
3705 static int
3706compile_get_register(char_u **arg, cctx_T *cctx)
3707{
3708 int ret;
3709
3710 ++*arg;
3711 if (**arg == NUL)
3712 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003713 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 return FAIL;
3715 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003716 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 {
3718 emsg_invreg(**arg);
3719 return FAIL;
3720 }
3721 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3722 ++*arg;
3723 return ret;
3724}
3725
3726/*
3727 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003728 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 */
3730 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003731apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003733 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734
3735 // this works from end to start
3736 while (p > start)
3737 {
3738 --p;
3739 if (*p == '-' || *p == '+')
3740 {
3741 // only '-' has an effect, for '+' we only check the type
3742#ifdef FEAT_FLOAT
3743 if (rettv->v_type == VAR_FLOAT)
3744 {
3745 if (*p == '-')
3746 rettv->vval.v_float = -rettv->vval.v_float;
3747 }
3748 else
3749#endif
3750 {
3751 varnumber_T val;
3752 int error = FALSE;
3753
3754 // tv_get_number_chk() accepts a string, but we don't want that
3755 // here
3756 if (check_not_string(rettv) == FAIL)
3757 return FAIL;
3758 val = tv_get_number_chk(rettv, &error);
3759 clear_tv(rettv);
3760 if (error)
3761 return FAIL;
3762 if (*p == '-')
3763 val = -val;
3764 rettv->v_type = VAR_NUMBER;
3765 rettv->vval.v_number = val;
3766 }
3767 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003768 else if (numeric_only)
3769 {
3770 ++p;
3771 break;
3772 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003773 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 {
3775 int v = tv2bool(rettv);
3776
3777 // '!' is permissive in the type.
3778 clear_tv(rettv);
3779 rettv->v_type = VAR_BOOL;
3780 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3781 }
3782 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003783 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 return OK;
3785}
3786
3787/*
3788 * Recognize v: variables that are constants and set "rettv".
3789 */
3790 static void
3791get_vim_constant(char_u **arg, typval_T *rettv)
3792{
3793 if (STRNCMP(*arg, "v:true", 6) == 0)
3794 {
3795 rettv->v_type = VAR_BOOL;
3796 rettv->vval.v_number = VVAL_TRUE;
3797 *arg += 6;
3798 }
3799 else if (STRNCMP(*arg, "v:false", 7) == 0)
3800 {
3801 rettv->v_type = VAR_BOOL;
3802 rettv->vval.v_number = VVAL_FALSE;
3803 *arg += 7;
3804 }
3805 else if (STRNCMP(*arg, "v:null", 6) == 0)
3806 {
3807 rettv->v_type = VAR_SPECIAL;
3808 rettv->vval.v_number = VVAL_NULL;
3809 *arg += 6;
3810 }
3811 else if (STRNCMP(*arg, "v:none", 6) == 0)
3812 {
3813 rettv->v_type = VAR_SPECIAL;
3814 rettv->vval.v_number = VVAL_NONE;
3815 *arg += 6;
3816 }
3817}
3818
Bram Moolenaar657137c2021-01-09 15:45:23 +01003819 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003820get_compare_type(char_u *p, int *len, int *type_is)
3821{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003822 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003823 int i;
3824
3825 switch (p[0])
3826 {
3827 case '=': if (p[1] == '=')
3828 type = EXPR_EQUAL;
3829 else if (p[1] == '~')
3830 type = EXPR_MATCH;
3831 break;
3832 case '!': if (p[1] == '=')
3833 type = EXPR_NEQUAL;
3834 else if (p[1] == '~')
3835 type = EXPR_NOMATCH;
3836 break;
3837 case '>': if (p[1] != '=')
3838 {
3839 type = EXPR_GREATER;
3840 *len = 1;
3841 }
3842 else
3843 type = EXPR_GEQUAL;
3844 break;
3845 case '<': if (p[1] != '=')
3846 {
3847 type = EXPR_SMALLER;
3848 *len = 1;
3849 }
3850 else
3851 type = EXPR_SEQUAL;
3852 break;
3853 case 'i': if (p[1] == 's')
3854 {
3855 // "is" and "isnot"; but not a prefix of a name
3856 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3857 *len = 5;
3858 i = p[*len];
3859 if (!isalnum(i) && i != '_')
3860 {
3861 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3862 *type_is = TRUE;
3863 }
3864 }
3865 break;
3866 }
3867 return type;
3868}
3869
3870/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003871 * Skip over an expression, ignoring most errors.
3872 */
3873 static void
3874skip_expr_cctx(char_u **arg, cctx_T *cctx)
3875{
3876 evalarg_T evalarg;
3877
3878 CLEAR_FIELD(evalarg);
3879 evalarg.eval_cctx = cctx;
3880 skip_expr(arg, &evalarg);
3881}
3882
3883/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003885 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 */
3887 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003888compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003890 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891
3892 // this works from end to start
3893 while (p > start)
3894 {
3895 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003896 while (VIM_ISWHITE(*p))
3897 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 if (*p == '-' || *p == '+')
3899 {
3900 int negate = *p == '-';
3901 isn_T *isn;
3902
3903 // TODO: check type
3904 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3905 {
3906 --p;
3907 if (*p == '-')
3908 negate = !negate;
3909 }
3910 // only '-' has an effect, for '+' we only check the type
3911 if (negate)
3912 isn = generate_instr(cctx, ISN_NEGATENR);
3913 else
3914 isn = generate_instr(cctx, ISN_CHECKNR);
3915 if (isn == NULL)
3916 return FAIL;
3917 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003918 else if (numeric_only)
3919 {
3920 ++p;
3921 break;
3922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 else
3924 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003925 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003927 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003929 if (p[-1] == '!')
3930 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 }
3933 if (generate_2BOOL(cctx, invert) == FAIL)
3934 return FAIL;
3935 }
3936 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003937 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 return OK;
3939}
3940
3941/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003942 * Compile "(expression)": recursive!
3943 * Return FAIL/OK.
3944 */
3945 static int
3946compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3947{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003948 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003949 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003950
Bram Moolenaar24156692021-01-14 20:35:49 +01003951 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3952 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003953 if (ppconst->pp_used <= PPSIZE - 10)
3954 {
3955 ret = compile_expr1(arg, cctx, ppconst);
3956 }
3957 else
3958 {
3959 // Not enough space in ppconst, flush constants.
3960 if (generate_ppconst(cctx, ppconst) == FAIL)
3961 return FAIL;
3962 ret = compile_expr0(arg, cctx);
3963 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003964 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3965 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003966 if (**arg == ')')
3967 ++*arg;
3968 else if (ret == OK)
3969 {
3970 emsg(_(e_missing_close));
3971 ret = FAIL;
3972 }
3973 return ret;
3974}
3975
3976/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003978 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 */
3980 static int
3981compile_subscript(
3982 char_u **arg,
3983 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003984 char_u *start_leader,
3985 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003986 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003988 char_u *name_start = *end_leader;
3989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 for (;;)
3991 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003992 char_u *p = skipwhite(*arg);
3993
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003994 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003995 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003996 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003997
3998 // If a following line starts with "->{" or "->X" advance to that
3999 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004000 // Also if a following line starts with ".x".
4001 if (next != NULL &&
4002 ((next[0] == '-' && next[1] == '>'
4003 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004004 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004005 {
4006 next = next_line_from_context(cctx, TRUE);
4007 if (next == NULL)
4008 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004009 *arg = next;
4010 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004011 }
4012 }
4013
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004014 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004015 // not a function call.
4016 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004018 garray_T *stack = &cctx->ctx_type_stack;
4019 type_T *type;
4020 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004022 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004023 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004024 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004027 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4028
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004029 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4031 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004032 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 return FAIL;
4034 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004035 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004037 char_u *pstart = p;
4038
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004039 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004040 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004041 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 // something->method()
4044 // Apply the '!', '-' and '+' first:
4045 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004046 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004049 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004050 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004051 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004052 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004053 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004054 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004055 garray_T *stack = &cctx->ctx_type_stack;
4056 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004057 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004058 int expr_isn_start = cctx->ctx_instr.ga_len;
4059 int expr_isn_end;
4060 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004061
4062 // Funcref call: list->(Refs[2])(arg)
4063 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004064 //
4065 // Fist compile the function expression.
4066 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004067 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004068
4069 // Remember the next instruction index, where the instructions
4070 // for arguments are being written.
4071 expr_isn_end = cctx->ctx_instr.ga_len;
4072
4073 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004074 if (**arg != '(')
4075 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004076 if (*skipwhite(*arg) == '(')
4077 emsg(_(e_nowhitespace));
4078 else
4079 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004080 return FAIL;
4081 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004082 *arg = skipwhite(*arg + 1);
4083 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4084 return FAIL;
4085
Bram Moolenaar2927c072021-04-05 19:41:21 +02004086 // Move the instructions for the arguments to before the
4087 // instructions of the expression and move the type of the
4088 // expression after the argument types. This is what ISN_PCALL
4089 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004090 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004091 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4092 if (arg_isn_count > 0)
4093 {
4094 int expr_isn_count = expr_isn_end - expr_isn_start;
4095 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4096
4097 if (isn == NULL)
4098 return FAIL;
4099 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4100 + expr_isn_start,
4101 sizeof(isn_T) * expr_isn_count);
4102 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4103 + expr_isn_start,
4104 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4105 sizeof(isn_T) * arg_isn_count);
4106 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4107 + expr_isn_start + arg_isn_count,
4108 isn, sizeof(isn_T) * expr_isn_count);
4109 vim_free(isn);
4110
4111 type = ((type_T **)stack->ga_data)[type_idx_start];
4112 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4113 ((type_T **)stack->ga_data) + type_idx_start + 1,
4114 sizeof(type_T *)
4115 * (stack->ga_len - type_idx_start - 1));
4116 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4117 }
4118
Bram Moolenaar7e368202020-12-25 21:56:57 +01004119 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4120 if (generate_PCALL(cctx, argcount,
4121 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 return FAIL;
4123 }
4124 else
4125 {
4126 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004127 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004128 if (!eval_isnamec1(*p))
4129 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004130 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004131 return FAIL;
4132 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004133 if (ASCII_ISALPHA(*p) && p[1] == ':')
4134 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004135 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 ;
4137 if (*p != '(')
4138 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004139 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 return FAIL;
4141 }
4142 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004143 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 return FAIL;
4145 }
4146 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004147 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004149 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004150
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004151 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004152 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004153 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004154 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004155 // TODO: more arguments
4156 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004157 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004158 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004159 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004160
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004161 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004162 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004163 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004164 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004165 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004166 // missing first index is equal to zero
4167 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004168 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004169 else
4170 {
4171 if (compile_expr0(arg, cctx) == FAIL)
4172 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004173 if (**arg == ':')
4174 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004175 semsg(_(e_white_space_required_before_and_after_str_at_str),
4176 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004177 return FAIL;
4178 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004179 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004180 return FAIL;
4181 *arg = skipwhite(*arg);
4182 }
4183 if (**arg == ':')
4184 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004185 is_slice = TRUE;
4186 ++*arg;
4187 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4188 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004189 semsg(_(e_white_space_required_before_and_after_str_at_str),
4190 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004191 return FAIL;
4192 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004193 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004194 return FAIL;
4195 if (**arg == ']')
4196 // missing second index is equal to end of string
4197 generate_PUSHNR(cctx, -1);
4198 else
4199 {
4200 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004201 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004202 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004203 return FAIL;
4204 *arg = skipwhite(*arg);
4205 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207
4208 if (**arg != ']')
4209 {
4210 emsg(_(e_missbrac));
4211 return FAIL;
4212 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004213 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214
Bram Moolenaare42939a2021-04-05 17:11:17 +02004215 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004216 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004218 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004220 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004221 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004222 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004223 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004224
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004225 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004226 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004227 {
4228 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004229 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004230 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004231 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004232 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 while (eval_isnamec(*p))
4234 MB_PTR_ADV(p);
4235 if (p == *arg)
4236 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004237 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 return FAIL;
4239 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004240 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 return FAIL;
4242 *arg = p;
4243 }
4244 else
4245 break;
4246 }
4247
4248 // TODO - see handle_subscript():
4249 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4250 // Don't do this when "Func" is already a partial that was bound
4251 // explicitly (pt_auto is FALSE).
4252
4253 return OK;
4254}
4255
4256/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004257 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4258 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004260 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004261 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004262 *
4263 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 */
4265
4266/*
4267 * number number constant
4268 * 0zFFFFFFFF Blob constant
4269 * "string" string constant
4270 * 'string' literal string constant
4271 * &option-name option value
4272 * @r register contents
4273 * identifier variable value
4274 * function() function call
4275 * $VAR environment variable
4276 * (expression) nested expression
4277 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004278 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 *
4280 * Also handle:
4281 * ! in front logical NOT
4282 * - in front unary minus
4283 * + in front unary plus (ignored)
4284 * trailing (arg) funcref/partial call
4285 * trailing [] subscript in String or List
4286 * trailing .name entry in Dictionary
4287 * trailing ->name() method call
4288 */
4289 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004290compile_expr7(
4291 char_u **arg,
4292 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004293 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 char_u *start_leader, *end_leader;
4296 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004297 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004298 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004300 ppconst->pp_is_const = FALSE;
4301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 /*
4303 * Skip '!', '-' and '+' characters. They are handled later.
4304 */
4305 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004306 if (eval_leader(arg, TRUE) == FAIL)
4307 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 end_leader = *arg;
4309
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004310 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 switch (**arg)
4312 {
4313 /*
4314 * Number constant.
4315 */
4316 case '0': // also for blob starting with 0z
4317 case '1':
4318 case '2':
4319 case '3':
4320 case '4':
4321 case '5':
4322 case '6':
4323 case '7':
4324 case '8':
4325 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004326 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004328 // Apply "-" and "+" just before the number now, right to
4329 // left. Matters especially when "->" follows. Stops at
4330 // '!'.
4331 if (apply_leader(rettv, TRUE,
4332 start_leader, &end_leader) == FAIL)
4333 {
4334 clear_tv(rettv);
4335 return FAIL;
4336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 break;
4338
4339 /*
4340 * String constant: "string".
4341 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004342 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 return FAIL;
4344 break;
4345
4346 /*
4347 * Literal string constant: 'str''ing'.
4348 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004349 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 return FAIL;
4351 break;
4352
4353 /*
4354 * Constant Vim variable.
4355 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004356 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 ret = NOTDONE;
4358 break;
4359
4360 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004361 * "true" constant
4362 */
4363 case 't': if (STRNCMP(*arg, "true", 4) == 0
4364 && !eval_isnamec((*arg)[4]))
4365 {
4366 *arg += 4;
4367 rettv->v_type = VAR_BOOL;
4368 rettv->vval.v_number = VVAL_TRUE;
4369 }
4370 else
4371 ret = NOTDONE;
4372 break;
4373
4374 /*
4375 * "false" constant
4376 */
4377 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4378 && !eval_isnamec((*arg)[5]))
4379 {
4380 *arg += 5;
4381 rettv->v_type = VAR_BOOL;
4382 rettv->vval.v_number = VVAL_FALSE;
4383 }
4384 else
4385 ret = NOTDONE;
4386 break;
4387
4388 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004389 * "null" constant
4390 */
4391 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004392 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004393 {
4394 *arg += 4;
4395 rettv->v_type = VAR_SPECIAL;
4396 rettv->vval.v_number = VVAL_NULL;
4397 }
4398 else
4399 ret = NOTDONE;
4400 break;
4401
4402 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 * List: [expr, expr]
4404 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004405 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4406 return FAIL;
4407 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 break;
4409
4410 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 * Dictionary: {'key': val, 'key': val}
4412 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004413 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4414 return FAIL;
4415 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 break;
4417
4418 /*
4419 * Option value: &name
4420 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004421 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4422 return FAIL;
4423 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 break;
4425
4426 /*
4427 * Environment variable: $VAR.
4428 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004429 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4430 return FAIL;
4431 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 break;
4433
4434 /*
4435 * Register contents: @r.
4436 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004437 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4438 return FAIL;
4439 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 break;
4441 /*
4442 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004443 * lambda: (arg, arg) => expr
4444 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004446 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4447 ret = compile_lambda(arg, cctx);
4448 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004449 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 break;
4451
4452 default: ret = NOTDONE;
4453 break;
4454 }
4455 if (ret == FAIL)
4456 return FAIL;
4457
Bram Moolenaar1c747212020-05-09 18:28:34 +02004458 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004460 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004461 clear_tv(rettv);
4462 else
4463 // A constant expression can possibly be handled compile time,
4464 // return the value instead of generating code.
4465 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 }
4467 else if (ret == NOTDONE)
4468 {
4469 char_u *p;
4470 int r;
4471
4472 if (!eval_isnamec1(**arg))
4473 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004474 if (!vim9_bad_comment(*arg))
4475 {
4476 if (ends_excmd(*skipwhite(*arg)))
4477 semsg(_(e_empty_expression_str), *arg);
4478 else
4479 semsg(_(e_name_expected_str), *arg);
4480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 return FAIL;
4482 }
4483
4484 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004485 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004486 if (p - *arg == (size_t)1 && **arg == '_')
4487 {
4488 emsg(_(e_cannot_use_underscore_here));
4489 return FAIL;
4490 }
4491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004493 {
4494 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004497 {
4498 if (generate_ppconst(cctx, ppconst) == FAIL)
4499 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004500 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004501 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 if (r == FAIL)
4503 return FAIL;
4504 }
4505
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004506 // Handle following "[]", ".member", etc.
4507 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004508 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004509 ppconst) == FAIL)
4510 return FAIL;
4511 if (ppconst->pp_used > 0)
4512 {
4513 // apply the '!', '-' and '+' before the constant
4514 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004515 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004516 return FAIL;
4517 return OK;
4518 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004519 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004521 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522}
4523
4524/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004525 * Give the "white on both sides" error, taking the operator from "p[len]".
4526 */
4527 void
4528error_white_both(char_u *op, int len)
4529{
4530 char_u buf[10];
4531
4532 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004533 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004534}
4535
4536/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004537 * <type>expr7: runtime type check / conversion
4538 */
4539 static int
4540compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4541{
4542 type_T *want_type = NULL;
4543
4544 // Recognize <type>
4545 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4546 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004547 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004548 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4549 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004550 return FAIL;
4551
4552 if (**arg != '>')
4553 {
4554 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004555 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004556 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004557 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004558 return FAIL;
4559 }
4560 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004561 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004562 return FAIL;
4563 }
4564
4565 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4566 return FAIL;
4567
4568 if (want_type != NULL)
4569 {
4570 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004571 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004572 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004573
Bram Moolenaard1103582020-08-14 22:44:25 +02004574 generate_ppconst(cctx, ppconst);
4575 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004576 where.wt_index = 0;
4577 where.wt_variable = FALSE;
4578 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004579 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004580 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004581 return FAIL;
4582 }
4583 }
4584
4585 return OK;
4586}
4587
4588/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 * * number multiplication
4590 * / number division
4591 * % number modulo
4592 */
4593 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004594compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595{
4596 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004597 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004598 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004600 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004601 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 return FAIL;
4603
4604 /*
4605 * Repeat computing, until no "*", "/" or "%" is following.
4606 */
4607 for (;;)
4608 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004609 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 if (*op != '*' && *op != '/' && *op != '%')
4611 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004612 if (next != NULL)
4613 {
4614 *arg = next_line_from_context(cctx, TRUE);
4615 op = skipwhite(*arg);
4616 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004617
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004618 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004620 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004621 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004623 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004624 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004626 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004627 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004629
4630 if (ppconst->pp_used == ppconst_used + 2
4631 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4632 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004633 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004634 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4635 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4636 varnumber_T res = 0;
4637 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004639 // both are numbers: compute the result
4640 switch (*op)
4641 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004642 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004643 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004644 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004645 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004646 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004647 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004648 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004649 break;
4650 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004651 if (failed)
4652 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004653 tv1->vval.v_number = res;
4654 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004655 }
4656 else
4657 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004658 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004659 generate_two_op(cctx, op);
4660 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 }
4662
4663 return OK;
4664}
4665
4666/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004667 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 * - number subtraction
4669 * .. string concatenation
4670 */
4671 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004672compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673{
4674 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004675 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004677 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678
4679 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004680 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 return FAIL;
4682
4683 /*
4684 * Repeat computing, until no "+", "-" or ".." is following.
4685 */
4686 for (;;)
4687 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004688 op = may_peek_next_line(cctx, *arg, &next);
4689 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004691 if (op[0] == op[1] && *op != '.' && next)
4692 // Finding "++" or "--" on the next line is a separate command.
4693 // But ".." is concatenation.
4694 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004696 if (next != NULL)
4697 {
4698 *arg = next_line_from_context(cctx, TRUE);
4699 op = skipwhite(*arg);
4700 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004702 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004704 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004705 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 }
4707
Bram Moolenaare0de1712020-12-02 17:36:54 +01004708 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004709 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004711 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004712 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 return FAIL;
4714
Bram Moolenaara5565e42020-05-09 15:44:01 +02004715 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004716 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004717 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4718 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4719 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4720 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004722 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4723 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004724
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004725 // concat/subtract/add constant numbers
4726 if (*op == '+')
4727 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4728 else if (*op == '-')
4729 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4730 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004731 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004732 // concatenate constant strings
4733 char_u *s1 = tv1->vval.v_string;
4734 char_u *s2 = tv2->vval.v_string;
4735 size_t len1 = STRLEN(s1);
4736
4737 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4738 if (tv1->vval.v_string == NULL)
4739 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004740 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004741 return FAIL;
4742 }
4743 mch_memmove(tv1->vval.v_string, s1, len1);
4744 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004745 vim_free(s1);
4746 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004747 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004748 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 }
4750 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004751 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004752 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004753 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004754 if (*op == '.')
4755 {
4756 if (may_generate_2STRING(-2, cctx) == FAIL
4757 || may_generate_2STRING(-1, cctx) == FAIL)
4758 return FAIL;
4759 generate_instr_drop(cctx, ISN_CONCAT, 1);
4760 }
4761 else
4762 generate_two_op(cctx, op);
4763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 }
4765
4766 return OK;
4767}
4768
4769/*
4770 * expr5a == expr5b
4771 * expr5a =~ expr5b
4772 * expr5a != expr5b
4773 * expr5a !~ expr5b
4774 * expr5a > expr5b
4775 * expr5a >= expr5b
4776 * expr5a < expr5b
4777 * expr5a <= expr5b
4778 * expr5a is expr5b
4779 * expr5a isnot expr5b
4780 *
4781 * Produces instructions:
4782 * EVAL expr5a Push result of "expr5a"
4783 * EVAL expr5b Push result of "expr5b"
4784 * COMPARE one of the compare instructions
4785 */
4786 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004787compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004789 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004791 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004794 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795
4796 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004797 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 return FAIL;
4799
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004800 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004801 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802
4803 /*
4804 * If there is a comparative operator, use it.
4805 */
4806 if (type != EXPR_UNKNOWN)
4807 {
4808 int ic = FALSE; // Default: do not ignore case
4809
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004810 if (next != NULL)
4811 {
4812 *arg = next_line_from_context(cctx, TRUE);
4813 p = skipwhite(*arg);
4814 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 if (type_is && (p[len] == '?' || p[len] == '#'))
4816 {
4817 semsg(_(e_invexpr2), *arg);
4818 return FAIL;
4819 }
4820 // extra question mark appended: ignore case
4821 if (p[len] == '?')
4822 {
4823 ic = TRUE;
4824 ++len;
4825 }
4826 // extra '#' appended: match case (ignored)
4827 else if (p[len] == '#')
4828 ++len;
4829 // nothing appended: match case
4830
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004831 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004833 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004834 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 }
4836
4837 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004838 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004839 return FAIL;
4840
Bram Moolenaara5565e42020-05-09 15:44:01 +02004841 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 return FAIL;
4843
Bram Moolenaara5565e42020-05-09 15:44:01 +02004844 if (ppconst->pp_used == ppconst_used + 2)
4845 {
4846 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4847 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4848 int ret;
4849
4850 // Both sides are a constant, compute the result now.
4851 // First check for a valid combination of types, this is more
4852 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004853 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004854 ret = FAIL;
4855 else
4856 {
4857 ret = typval_compare(tv1, tv2, type, ic);
4858 tv1->v_type = VAR_BOOL;
4859 tv1->vval.v_number = tv1->vval.v_number
4860 ? VVAL_TRUE : VVAL_FALSE;
4861 clear_tv(tv2);
4862 --ppconst->pp_used;
4863 }
4864 return ret;
4865 }
4866
4867 generate_ppconst(cctx, ppconst);
4868 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869 }
4870
4871 return OK;
4872}
4873
Bram Moolenaar7f141552020-05-09 17:35:53 +02004874static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876/*
4877 * Compile || or &&.
4878 */
4879 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004880compile_and_or(
4881 char_u **arg,
4882 cctx_T *cctx,
4883 char *op,
4884 ppconst_T *ppconst,
4885 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004887 char_u *next;
4888 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 int opchar = *op;
4890
4891 if (p[0] == opchar && p[1] == opchar)
4892 {
4893 garray_T *instr = &cctx->ctx_instr;
4894 garray_T end_ga;
4895
4896 /*
4897 * Repeat until there is no following "||" or "&&"
4898 */
4899 ga_init2(&end_ga, sizeof(int), 10);
4900 while (p[0] == opchar && p[1] == opchar)
4901 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004902 long start_lnum = SOURCING_LNUM;
4903 int start_ctx_lnum = cctx->ctx_lnum;
4904 int save_lnum;
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 }
4911
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004912 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4913 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004914 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004915 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004916 return FAIL;
4917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004919 // TODO: use ppconst if the value is a constant and check
4920 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004921 generate_ppconst(cctx, ppconst);
4922
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004923 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004924 SOURCING_LNUM = start_lnum;
4925 save_lnum = cctx->ctx_lnum;
4926 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004927 if (bool_on_stack(cctx) == FAIL)
4928 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004929 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004930 ga_clear(&end_ga);
4931 return FAIL;
4932 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004933 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 if (ga_grow(&end_ga, 1) == FAIL)
4936 {
4937 ga_clear(&end_ga);
4938 return FAIL;
4939 }
4940 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4941 ++end_ga.ga_len;
4942 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004943 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944
4945 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004946 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004947 {
4948 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004949 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004950 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004951
Bram Moolenaara5565e42020-05-09 15:44:01 +02004952 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4953 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 {
4955 ga_clear(&end_ga);
4956 return FAIL;
4957 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004958
4959 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004961 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004963 // Every part must evaluate to a bool.
4964 if (bool_on_stack(cctx) == FAIL)
4965 {
4966 ga_clear(&end_ga);
4967 return FAIL;
4968 }
4969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 // Fill in the end label in all jumps.
4971 while (end_ga.ga_len > 0)
4972 {
4973 isn_T *isn;
4974
4975 --end_ga.ga_len;
4976 isn = ((isn_T *)instr->ga_data)
4977 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4978 isn->isn_arg.jump.jump_where = instr->ga_len;
4979 }
4980 ga_clear(&end_ga);
4981 }
4982
4983 return OK;
4984}
4985
4986/*
4987 * expr4a && expr4a && expr4a logical AND
4988 *
4989 * Produces instructions:
4990 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004991 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004992 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004994 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 * EVAL expr4c Push result of "expr4c"
4996 * end:
4997 */
4998 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004999compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005001 int ppconst_used = ppconst->pp_used;
5002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005004 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 return FAIL;
5006
5007 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005008 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009}
5010
5011/*
5012 * expr3a || expr3b || expr3c logical OR
5013 *
5014 * Produces instructions:
5015 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005016 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005017 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005019 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 * EVAL expr3c Push result of "expr3c"
5021 * end:
5022 */
5023 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005024compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005026 int ppconst_used = ppconst->pp_used;
5027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005029 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030 return FAIL;
5031
5032 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005033 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034}
5035
5036/*
5037 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005039 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040 * JUMP_IF_FALSE alt jump if false
5041 * EVAL expr1a
5042 * JUMP_ALWAYS end
5043 * alt: EVAL expr1b
5044 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005045 *
5046 * Toplevel expression: expr2 ?? expr1
5047 * Produces instructions:
5048 * EVAL expr2 Push result of "expr2"
5049 * JUMP_AND_KEEP_IF_TRUE end jump if true
5050 * EVAL expr1
5051 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052 */
5053 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005054compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055{
5056 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005057 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005058 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059
Bram Moolenaar3988f642020-08-27 22:43:03 +02005060 // Ignore all kinds of errors when not producing code.
5061 if (cctx->ctx_skip == SKIP_YES)
5062 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005063 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005064 return OK;
5065 }
5066
Bram Moolenaar61a89812020-05-07 16:58:17 +02005067 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005068 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 return FAIL;
5070
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005071 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 if (*p == '?')
5073 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005074 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 garray_T *instr = &cctx->ctx_instr;
5076 garray_T *stack = &cctx->ctx_type_stack;
5077 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005078 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005080 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005081 int has_const_expr = FALSE;
5082 int const_value = FALSE;
5083 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005085 if (next != NULL)
5086 {
5087 *arg = next_line_from_context(cctx, TRUE);
5088 p = skipwhite(*arg);
5089 }
5090
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005091 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005092 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005093 semsg(_(e_white_space_required_before_and_after_str_at_str),
5094 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005095 return FAIL;
5096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097
Bram Moolenaara5565e42020-05-09 15:44:01 +02005098 if (ppconst->pp_used == ppconst_used + 1)
5099 {
5100 // the condition is a constant, we know whether the ? or the :
5101 // expression is to be evaluated.
5102 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005103 if (op_falsy)
5104 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5105 else
5106 {
5107 int error = FALSE;
5108
5109 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5110 &error);
5111 if (error)
5112 return FAIL;
5113 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005114 cctx->ctx_skip = save_skip == SKIP_YES ||
5115 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5116
5117 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5118 // "left ?? right" and "left" is truthy: produce "left"
5119 generate_ppconst(cctx, ppconst);
5120 else
5121 {
5122 clear_tv(&ppconst->pp_tv[ppconst_used]);
5123 --ppconst->pp_used;
5124 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005125 }
5126 else
5127 {
5128 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005129 if (op_falsy)
5130 end_idx = instr->ga_len;
5131 generate_JUMP(cctx, op_falsy
5132 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5133 if (op_falsy)
5134 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005136
5137 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005138 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005139 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005140 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005141 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142
Bram Moolenaara5565e42020-05-09 15:44:01 +02005143 if (!has_const_expr)
5144 {
5145 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005147 if (!op_falsy)
5148 {
5149 // remember the type and drop it
5150 --stack->ga_len;
5151 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005153 end_idx = instr->ga_len;
5154 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005155
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005156 // jump here from JUMP_IF_FALSE
5157 isn = ((isn_T *)instr->ga_data) + alt_idx;
5158 isn->isn_arg.jump.jump_where = instr->ga_len;
5159 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005162 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005164 // Check for the ":".
5165 p = may_peek_next_line(cctx, *arg, &next);
5166 if (*p != ':')
5167 {
5168 emsg(_(e_missing_colon));
5169 return FAIL;
5170 }
5171 if (next != NULL)
5172 {
5173 *arg = next_line_from_context(cctx, TRUE);
5174 p = skipwhite(*arg);
5175 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005176
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005177 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5178 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005179 semsg(_(e_white_space_required_before_and_after_str_at_str),
5180 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005181 return FAIL;
5182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005184 // evaluate the third expression
5185 if (has_const_expr)
5186 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005187 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005188 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005189 return FAIL;
5190 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5191 return FAIL;
5192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193
Bram Moolenaara5565e42020-05-09 15:44:01 +02005194 if (!has_const_expr)
5195 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005196 type_T **typep;
5197
Bram Moolenaara5565e42020-05-09 15:44:01 +02005198 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199
Bram Moolenaara5565e42020-05-09 15:44:01 +02005200 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005201 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5202 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005203
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005204 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005205 isn = ((isn_T *)instr->ga_data) + end_idx;
5206 isn->isn_arg.jump.jump_where = instr->ga_len;
5207 }
5208
5209 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 }
5211 return OK;
5212}
5213
5214/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005215 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005216 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5217 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005218 */
5219 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005220compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005221{
5222 ppconst_T ppconst;
5223
5224 CLEAR_FIELD(ppconst);
5225 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5226 {
5227 clear_ppconst(&ppconst);
5228 return FAIL;
5229 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005230 if (is_const != NULL)
5231 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005232 if (generate_ppconst(cctx, &ppconst) == FAIL)
5233 return FAIL;
5234 return OK;
5235}
5236
5237/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005238 * Toplevel expression.
5239 */
5240 static int
5241compile_expr0(char_u **arg, cctx_T *cctx)
5242{
5243 return compile_expr0_ext(arg, cctx, NULL);
5244}
5245
5246/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 * compile "return [expr]"
5248 */
5249 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005250compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251{
5252 char_u *p = arg;
5253 garray_T *stack = &cctx->ctx_type_stack;
5254 type_T *stack_type;
5255
5256 if (*p != NUL && *p != '|' && *p != '\n')
5257 {
5258 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005259 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005260 return NULL;
5261
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005262 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005263 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005264 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005265 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005266 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5267 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005268 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005269 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005270 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005271 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005272 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005273 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5274 && stack_type->tt_type != VAR_VOID
5275 && stack_type->tt_type != VAR_UNKNOWN)
5276 {
5277 emsg(_(e_returning_value_in_function_without_return_type));
5278 return NULL;
5279 }
5280 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005281 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005282 return NULL;
5283 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005285 }
5286 else
5287 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005288 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005289 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005290 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5291 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005293 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005294 return NULL;
5295 }
5296
5297 // No argument, return zero.
5298 generate_PUSHNR(cctx, 0);
5299 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005300
5301 // Undo any command modifiers.
5302 generate_undo_cmdmods(cctx);
5303
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005304 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005305 return NULL;
5306
5307 // "return val | endif" is possible
5308 return skipwhite(p);
5309}
5310
5311/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005312 * Get a line from the compilation context, compatible with exarg_T getline().
5313 * Return a pointer to the line in allocated memory.
5314 * Return NULL for end-of-file or some error.
5315 */
5316 static char_u *
5317exarg_getline(
5318 int c UNUSED,
5319 void *cookie,
5320 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005321 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005322{
5323 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005324 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005325
Bram Moolenaar66250c92020-08-20 15:02:42 +02005326 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005327 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005328 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005329 return NULL;
5330 ++cctx->ctx_lnum;
5331 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5332 // Comment lines result in NULL pointers, skip them.
5333 if (p != NULL)
5334 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005335 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005336}
5337
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005338 void
5339fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5340{
5341 eap->getline = exarg_getline;
5342 eap->cookie = cctx;
5343}
5344
Bram Moolenaar04b12692020-05-04 23:24:44 +02005345/*
5346 * Compile a nested :def command.
5347 */
5348 static char_u *
5349compile_nested_function(exarg_T *eap, cctx_T *cctx)
5350{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005351 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005352 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005353 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005354 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005355 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005356 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005357
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005358 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005359 {
5360 emsg(_(e_cannot_use_bang_with_nested_def));
5361 return NULL;
5362 }
5363
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005364 if (*name_start == '/')
5365 {
5366 name_end = skip_regexp(name_start + 1, '/', TRUE);
5367 if (*name_end == '/')
5368 ++name_end;
5369 eap->nextcmd = check_nextcmd(name_end);
5370 }
5371 if (name_end == name_start || *skipwhite(name_end) != '(')
5372 {
5373 if (!ends_excmd2(name_start, name_end))
5374 {
5375 semsg(_(e_invalid_command_str), eap->cmd);
5376 return NULL;
5377 }
5378
5379 // "def" or "def Name": list functions
5380 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5381 return NULL;
5382 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5383 }
5384
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005385 // Only g:Func() can use a namespace.
5386 if (name_start[1] == ':' && !is_global)
5387 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005388 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005389 return NULL;
5390 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005391 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005392 return NULL;
5393
Bram Moolenaar04b12692020-05-04 23:24:44 +02005394 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005395 fill_exarg_from_cctx(eap, cctx);
5396
Bram Moolenaar04b12692020-05-04 23:24:44 +02005397 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005398 lambda_name = vim_strsave(get_lambda_name());
5399 if (lambda_name == NULL)
5400 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005401 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005402
Bram Moolenaar822ba242020-05-24 23:00:18 +02005403 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005404 {
5405 r = eap->skip ? OK : FAIL;
5406 goto theend;
5407 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005408
5409 // copy over the block scope IDs before compiling
5410 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5411 {
5412 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5413
5414 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5415 if (ufunc->uf_block_ids != NULL)
5416 {
5417 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5418 sizeof(int) * block_depth);
5419 ufunc->uf_block_depth = block_depth;
5420 }
5421 }
5422
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005423 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5424 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005425 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005426 {
5427 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005428 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005429 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005430
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005431 if (is_global)
5432 {
5433 char_u *func_name = vim_strnsave(name_start + 2,
5434 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005435
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005436 if (func_name == NULL)
5437 r = FAIL;
5438 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005439 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005440 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005441 lambda_name = NULL;
5442 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005443 }
5444 else
5445 {
5446 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005447 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005448 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005449
Bram Moolenaareef21022020-08-01 22:16:43 +02005450 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005451 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005452 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005453 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005454 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5455 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005456 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005457
5458theend:
5459 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005460 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005461}
5462
5463/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005464 * Return the length of an assignment operator, or zero if there isn't one.
5465 */
5466 int
5467assignment_len(char_u *p, int *heredoc)
5468{
5469 if (*p == '=')
5470 {
5471 if (p[1] == '<' && p[2] == '<')
5472 {
5473 *heredoc = TRUE;
5474 return 3;
5475 }
5476 return 1;
5477 }
5478 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5479 return 2;
5480 if (STRNCMP(p, "..=", 3) == 0)
5481 return 3;
5482 return 0;
5483}
5484
5485// words that cannot be used as a variable
5486static char *reserved[] = {
5487 "true",
5488 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005489 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490 NULL
5491};
5492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005494 * Generate the load instruction for "name".
5495 */
5496 static void
5497generate_loadvar(
5498 cctx_T *cctx,
5499 assign_dest_T dest,
5500 char_u *name,
5501 lvar_T *lvar,
5502 type_T *type)
5503{
5504 switch (dest)
5505 {
5506 case dest_option:
5507 // TODO: check the option exists
5508 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5509 break;
5510 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005511 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5512 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5513 else
5514 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005515 break;
5516 case dest_buffer:
5517 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5518 break;
5519 case dest_window:
5520 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5521 break;
5522 case dest_tab:
5523 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5524 break;
5525 case dest_script:
5526 compile_load_scriptvar(cctx,
5527 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5528 break;
5529 case dest_env:
5530 // Include $ in the name here
5531 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5532 break;
5533 case dest_reg:
5534 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5535 break;
5536 case dest_vimvar:
5537 generate_LOADV(cctx, name + 2, TRUE);
5538 break;
5539 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005540 if (lvar->lv_from_outer > 0)
5541 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5542 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005543 else
5544 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5545 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005546 case dest_expr:
5547 // list or dict value should already be on the stack.
5548 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005549 }
5550}
5551
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005552/*
5553 * Skip over "[expr]" or ".member".
5554 * Does not check for any errors.
5555 */
5556 static char_u *
5557skip_index(char_u *start)
5558{
5559 char_u *p = start;
5560
5561 if (*p == '[')
5562 {
5563 p = skipwhite(p + 1);
5564 (void)skip_expr(&p, NULL);
5565 p = skipwhite(p);
5566 if (*p == ']')
5567 return p + 1;
5568 return p;
5569 }
5570 // if (*p == '.')
5571 return to_name_end(p + 1, TRUE);
5572}
5573
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005574 void
5575vim9_declare_error(char_u *name)
5576{
5577 char *scope = "";
5578
5579 switch (*name)
5580 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005581 case 'g': scope = _("global"); break;
5582 case 'b': scope = _("buffer"); break;
5583 case 'w': scope = _("window"); break;
5584 case 't': scope = _("tab"); break;
5585 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005586 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005587 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005588 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005589 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005590 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005591 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005592 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005593 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005594 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005595}
5596
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005597/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005598 * For one assignment figure out the type of destination. Return it in "dest".
5599 * When not recognized "dest" is not set.
5600 * For an option "opt_flags" is set.
5601 * For a v:var "vimvaridx" is set.
5602 * "type" is set to the destination type if known, unchanted otherwise.
5603 * Return FAIL if an error message was given.
5604 */
5605 static int
5606get_var_dest(
5607 char_u *name,
5608 assign_dest_T *dest,
5609 int cmdidx,
5610 int *opt_flags,
5611 int *vimvaridx,
5612 type_T **type,
5613 cctx_T *cctx)
5614{
5615 char_u *p;
5616
5617 if (*name == '&')
5618 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005619 int cc;
5620 long numval;
5621 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005622
5623 *dest = dest_option;
5624 if (cmdidx == CMD_final || cmdidx == CMD_const)
5625 {
5626 emsg(_(e_const_option));
5627 return FAIL;
5628 }
5629 p = name;
5630 p = find_option_end(&p, opt_flags);
5631 if (p == NULL)
5632 {
5633 // cannot happen?
5634 emsg(_(e_letunexp));
5635 return FAIL;
5636 }
5637 cc = *p;
5638 *p = NUL;
5639 opt_type = get_option_value(skip_option_env_lead(name),
5640 &numval, NULL, *opt_flags);
5641 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005642 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005643 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005644 case gov_unknown:
5645 semsg(_(e_unknown_option), name);
5646 return FAIL;
5647 case gov_string:
5648 case gov_hidden_string:
5649 *type = &t_string;
5650 break;
5651 case gov_bool:
5652 case gov_hidden_bool:
5653 *type = &t_bool;
5654 break;
5655 case gov_number:
5656 case gov_hidden_number:
5657 *type = &t_number;
5658 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005659 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005660 }
5661 else if (*name == '$')
5662 {
5663 *dest = dest_env;
5664 *type = &t_string;
5665 }
5666 else if (*name == '@')
5667 {
5668 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5669 {
5670 emsg_invreg(name[1]);
5671 return FAIL;
5672 }
5673 *dest = dest_reg;
5674 *type = &t_string;
5675 }
5676 else if (STRNCMP(name, "g:", 2) == 0)
5677 {
5678 *dest = dest_global;
5679 }
5680 else if (STRNCMP(name, "b:", 2) == 0)
5681 {
5682 *dest = dest_buffer;
5683 }
5684 else if (STRNCMP(name, "w:", 2) == 0)
5685 {
5686 *dest = dest_window;
5687 }
5688 else if (STRNCMP(name, "t:", 2) == 0)
5689 {
5690 *dest = dest_tab;
5691 }
5692 else if (STRNCMP(name, "v:", 2) == 0)
5693 {
5694 typval_T *vtv;
5695 int di_flags;
5696
5697 *vimvaridx = find_vim_var(name + 2, &di_flags);
5698 if (*vimvaridx < 0)
5699 {
5700 semsg(_(e_variable_not_found_str), name);
5701 return FAIL;
5702 }
5703 // We use the current value of "sandbox" here, is that OK?
5704 if (var_check_ro(di_flags, name, FALSE))
5705 return FAIL;
5706 *dest = dest_vimvar;
5707 vtv = get_vim_var_tv(*vimvaridx);
5708 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5709 }
5710 return OK;
5711}
5712
5713/*
5714 * Generate a STORE instruction for "dest", not being "dest_local".
5715 * Return FAIL when out of memory.
5716 */
5717 static int
5718generate_store_var(
5719 cctx_T *cctx,
5720 assign_dest_T dest,
5721 int opt_flags,
5722 int vimvaridx,
5723 int scriptvar_idx,
5724 int scriptvar_sid,
5725 type_T *type,
5726 char_u *name)
5727{
5728 switch (dest)
5729 {
5730 case dest_option:
5731 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5732 opt_flags);
5733 case dest_global:
5734 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005735 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5736 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005737 case dest_buffer:
5738 // include b: with the name, easier to execute that way
5739 return generate_STORE(cctx, ISN_STOREB, 0, name);
5740 case dest_window:
5741 // include w: with the name, easier to execute that way
5742 return generate_STORE(cctx, ISN_STOREW, 0, name);
5743 case dest_tab:
5744 // include t: with the name, easier to execute that way
5745 return generate_STORE(cctx, ISN_STORET, 0, name);
5746 case dest_env:
5747 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5748 case dest_reg:
5749 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5750 case dest_vimvar:
5751 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5752 case dest_script:
5753 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005754 // "s:" may be included in the name.
5755 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005756 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005757 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5758 scriptvar_sid, scriptvar_idx, type);
5759 case dest_local:
5760 case dest_expr:
5761 // cannot happen
5762 break;
5763 }
5764 return FAIL;
5765}
5766
Bram Moolenaar752fc692021-01-04 21:57:11 +01005767 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005768generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5769{
5770 if (lhs->lhs_dest != dest_local)
5771 return generate_store_var(cctx, lhs->lhs_dest,
5772 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5773 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5774 lhs->lhs_type, lhs->lhs_name);
5775
5776 if (lhs->lhs_lvar != NULL)
5777 {
5778 garray_T *instr = &cctx->ctx_instr;
5779 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5780
5781 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5782 // ISN_STORENR
5783 if (lhs->lhs_lvar->lv_from_outer == 0
5784 && instr->ga_len == instr_count + 1
5785 && isn->isn_type == ISN_PUSHNR)
5786 {
5787 varnumber_T val = isn->isn_arg.number;
5788 garray_T *stack = &cctx->ctx_type_stack;
5789
5790 isn->isn_type = ISN_STORENR;
5791 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5792 isn->isn_arg.storenr.stnr_val = val;
5793 if (stack->ga_len > 0)
5794 --stack->ga_len;
5795 }
5796 else if (lhs->lhs_lvar->lv_from_outer > 0)
5797 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5798 lhs->lhs_lvar->lv_from_outer);
5799 else
5800 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5801 }
5802 return OK;
5803}
5804
5805 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005806is_decl_command(int cmdidx)
5807{
5808 return cmdidx == CMD_let || cmdidx == CMD_var
5809 || cmdidx == CMD_final || cmdidx == CMD_const;
5810}
5811
5812/*
5813 * Figure out the LHS type and other properties for an assignment or one item
5814 * of ":unlet" with an index.
5815 * Returns OK or FAIL.
5816 */
5817 static int
5818compile_lhs(
5819 char_u *var_start,
5820 lhs_T *lhs,
5821 int cmdidx,
5822 int heredoc,
5823 int oplen,
5824 cctx_T *cctx)
5825{
5826 char_u *var_end;
5827 int is_decl = is_decl_command(cmdidx);
5828
5829 CLEAR_POINTER(lhs);
5830 lhs->lhs_dest = dest_local;
5831 lhs->lhs_vimvaridx = -1;
5832 lhs->lhs_scriptvar_idx = -1;
5833
5834 // "dest_end" is the end of the destination, including "[expr]" or
5835 // ".name".
5836 // "var_end" is the end of the variable/option/etc. name.
5837 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5838 if (*var_start == '@')
5839 var_end = var_start + 2;
5840 else
5841 {
5842 // skip over the leading "&", "&l:", "&g:" and "$"
5843 var_end = skip_option_env_lead(var_start);
5844 var_end = to_name_end(var_end, TRUE);
5845 }
5846
5847 // "a: type" is declaring variable "a" with a type, not dict "a:".
5848 if (is_decl && lhs->lhs_dest_end == var_start + 2
5849 && lhs->lhs_dest_end[-1] == ':')
5850 --lhs->lhs_dest_end;
5851 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5852 --var_end;
5853
5854 // compute the length of the destination without "[expr]" or ".name"
5855 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02005856 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005857 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5858 if (lhs->lhs_name == NULL)
5859 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005860
5861 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5862 // Something follows after the variable: "var[idx]" or "var.key".
5863 lhs->lhs_has_index = TRUE;
5864
Bram Moolenaar752fc692021-01-04 21:57:11 +01005865 if (heredoc)
5866 lhs->lhs_type = &t_list_string;
5867 else
5868 lhs->lhs_type = &t_any;
5869
5870 if (cctx->ctx_skip != SKIP_YES)
5871 {
5872 int declare_error = FALSE;
5873
5874 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5875 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5876 &lhs->lhs_type, cctx) == FAIL)
5877 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005878 if (lhs->lhs_dest != dest_local
5879 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005880 {
5881 // Specific kind of variable recognized.
5882 declare_error = is_decl;
5883 }
5884 else
5885 {
5886 int idx;
5887
5888 // No specific kind of variable recognized, just a name.
5889 for (idx = 0; reserved[idx] != NULL; ++idx)
5890 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5891 {
5892 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5893 return FAIL;
5894 }
5895
5896
5897 if (lookup_local(var_start, lhs->lhs_varlen,
5898 &lhs->lhs_local_lvar, cctx) == OK)
5899 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5900 else
5901 {
5902 CLEAR_FIELD(lhs->lhs_arg_lvar);
5903 if (arg_exists(var_start, lhs->lhs_varlen,
5904 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5905 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5906 {
5907 if (is_decl)
5908 {
5909 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5910 return FAIL;
5911 }
5912 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5913 }
5914 }
5915 if (lhs->lhs_lvar != NULL)
5916 {
5917 if (is_decl)
5918 {
5919 semsg(_(e_variable_already_declared), lhs->lhs_name);
5920 return FAIL;
5921 }
5922 }
5923 else
5924 {
5925 int script_namespace = lhs->lhs_varlen > 1
5926 && STRNCMP(var_start, "s:", 2) == 0;
5927 int script_var = (script_namespace
5928 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005929 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005930 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005931 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005932 imported_T *import =
5933 find_imported(var_start, lhs->lhs_varlen, cctx);
5934
5935 if (script_namespace || script_var || import != NULL)
5936 {
5937 char_u *rawname = lhs->lhs_name
5938 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5939
5940 if (is_decl)
5941 {
5942 if (script_namespace)
5943 semsg(_(e_cannot_declare_script_variable_in_function),
5944 lhs->lhs_name);
5945 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005946 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005947 lhs->lhs_name);
5948 return FAIL;
5949 }
5950 else if (cctx->ctx_ufunc->uf_script_ctx_version
5951 == SCRIPT_VERSION_VIM9
5952 && script_namespace
5953 && !script_var && import == NULL)
5954 {
5955 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5956 return FAIL;
5957 }
5958
5959 lhs->lhs_dest = dest_script;
5960
5961 // existing script-local variables should have a type
5962 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5963 if (import != NULL)
5964 lhs->lhs_scriptvar_sid = import->imp_sid;
5965 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5966 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005967 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005968 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005969 lhs->lhs_scriptvar_sid, rawname,
5970 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5971 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005972 if (lhs->lhs_scriptvar_idx >= 0)
5973 {
5974 scriptitem_T *si = SCRIPT_ITEM(
5975 lhs->lhs_scriptvar_sid);
5976 svar_T *sv =
5977 ((svar_T *)si->sn_var_vals.ga_data)
5978 + lhs->lhs_scriptvar_idx;
5979 lhs->lhs_type = sv->sv_type;
5980 }
5981 }
5982 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005983 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005984 == FAIL)
5985 return FAIL;
5986 }
5987 }
5988
5989 if (declare_error)
5990 {
5991 vim9_declare_error(lhs->lhs_name);
5992 return FAIL;
5993 }
5994 }
5995
5996 // handle "a:name" as a name, not index "name" on "a"
5997 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5998 var_end = lhs->lhs_dest_end;
5999
6000 if (lhs->lhs_dest != dest_option)
6001 {
6002 if (is_decl && *var_end == ':')
6003 {
6004 char_u *p;
6005
6006 // parse optional type: "let var: type = expr"
6007 if (!VIM_ISWHITE(var_end[1]))
6008 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006009 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006010 return FAIL;
6011 }
6012 p = skipwhite(var_end + 1);
6013 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6014 if (lhs->lhs_type == NULL)
6015 return FAIL;
6016 lhs->lhs_has_type = TRUE;
6017 }
6018 else if (lhs->lhs_lvar != NULL)
6019 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6020 }
6021
Bram Moolenaare42939a2021-04-05 17:11:17 +02006022 if (oplen == 3 && !heredoc
6023 && lhs->lhs_dest != dest_global
6024 && !lhs->lhs_has_index
6025 && lhs->lhs_type->tt_type != VAR_STRING
6026 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006027 {
6028 emsg(_(e_can_only_concatenate_to_string));
6029 return FAIL;
6030 }
6031
6032 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6033 && cctx->ctx_skip != SKIP_YES)
6034 {
6035 if (oplen > 1 && !heredoc)
6036 {
6037 // +=, /=, etc. require an existing variable
6038 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6039 return FAIL;
6040 }
6041 if (!is_decl)
6042 {
6043 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6044 return FAIL;
6045 }
6046
Bram Moolenaar3f327882021-03-17 20:56:38 +01006047 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006048 if ((lhs->lhs_type->tt_type == VAR_FUNC
6049 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006050 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006051 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006052
6053 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006054 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6055 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6056 if (lhs->lhs_lvar == NULL)
6057 return FAIL;
6058 lhs->lhs_new_local = TRUE;
6059 }
6060
6061 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006062 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006063 {
6064 // Something follows after the variable: "var[idx]" or "var.key".
6065 // TODO: should we also handle "->func()" here?
6066 if (is_decl)
6067 {
6068 emsg(_(e_cannot_use_index_when_declaring_variable));
6069 return FAIL;
6070 }
6071
6072 if (var_start[lhs->lhs_varlen] == '['
6073 || var_start[lhs->lhs_varlen] == '.')
6074 {
6075 char_u *after = var_start + lhs->lhs_varlen;
6076 char_u *p;
6077
6078 // Only the last index is used below, if there are others
6079 // before it generate code for the expression. Thus for
6080 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6081 for (;;)
6082 {
6083 p = skip_index(after);
6084 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006085 {
6086 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006087 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006088 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006089 after = p;
6090 }
6091 if (after > var_start + lhs->lhs_varlen)
6092 {
6093 lhs->lhs_varlen = after - var_start;
6094 lhs->lhs_dest = dest_expr;
6095 // We don't know the type before evaluating the expression,
6096 // use "any" until then.
6097 lhs->lhs_type = &t_any;
6098 }
6099
Bram Moolenaar752fc692021-01-04 21:57:11 +01006100 if (lhs->lhs_type->tt_member == NULL)
6101 lhs->lhs_member_type = &t_any;
6102 else
6103 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6104 }
6105 else
6106 {
6107 semsg("Not supported yet: %s", var_start);
6108 return FAIL;
6109 }
6110 }
6111 return OK;
6112}
6113
6114/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006115 * Figure out the LHS and check a few errors.
6116 */
6117 static int
6118compile_assign_lhs(
6119 char_u *var_start,
6120 lhs_T *lhs,
6121 int cmdidx,
6122 int is_decl,
6123 int heredoc,
6124 int oplen,
6125 cctx_T *cctx)
6126{
6127 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6128 return FAIL;
6129
6130 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6131 {
6132 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6133 return FAIL;
6134 }
6135 if (!is_decl && lhs->lhs_lvar != NULL
6136 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6137 {
6138 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6139 return FAIL;
6140 }
6141 return OK;
6142}
6143
6144/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006145 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6146 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006147 */
6148 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006149compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006150 char_u *var_start,
6151 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006152 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006153 cctx_T *cctx)
6154{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006155 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006156 char_u *p;
6157 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006158 int need_white_before = TRUE;
6159 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006160
Bram Moolenaar752fc692021-01-04 21:57:11 +01006161 p = var_start + varlen;
6162 if (*p == '[')
6163 {
6164 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006165 if (*p == ':')
6166 {
6167 // empty first index, push zero
6168 r = generate_PUSHNR(cctx, 0);
6169 need_white_before = FALSE;
6170 }
6171 else
6172 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006173
6174 if (r == OK && *skipwhite(p) == ':')
6175 {
6176 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006177 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006178 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006179 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006180 empty_second = *skipwhite(p + 1) == ']';
6181 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6182 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006183 {
6184 semsg(_(e_white_space_required_before_and_after_str_at_str),
6185 ":", p);
6186 return FAIL;
6187 }
6188 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006189 if (*p == ']')
6190 // empty second index, push "none"
6191 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6192 else
6193 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006194 }
6195
Bram Moolenaar752fc692021-01-04 21:57:11 +01006196 if (r == OK && *skipwhite(p) != ']')
6197 {
6198 // this should not happen
6199 emsg(_(e_missbrac));
6200 r = FAIL;
6201 }
6202 }
6203 else // if (*p == '.')
6204 {
6205 char_u *key_end = to_name_end(p + 1, TRUE);
6206 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6207
6208 r = generate_PUSHS(cctx, key);
6209 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006210 return r;
6211}
6212
6213/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006214 * For a LHS with an index, load the variable to be indexed.
6215 */
6216 static int
6217compile_load_lhs(
6218 lhs_T *lhs,
6219 char_u *var_start,
6220 type_T *rhs_type,
6221 cctx_T *cctx)
6222{
6223 if (lhs->lhs_dest == dest_expr)
6224 {
6225 size_t varlen = lhs->lhs_varlen;
6226 int c = var_start[varlen];
6227 char_u *p = var_start;
6228 garray_T *stack = &cctx->ctx_type_stack;
6229
6230 // Evaluate "ll[expr]" of "ll[expr][idx]"
6231 var_start[varlen] = NUL;
6232 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6233 {
6234 // this should not happen
6235 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006236 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006237 return FAIL;
6238 }
6239 var_start[varlen] = c;
6240
6241 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6242 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6243 // now we can properly check the type
6244 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6245 && rhs_type != &t_void
6246 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6247 FALSE, FALSE) == FAIL)
6248 return FAIL;
6249 }
6250 else
6251 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6252 lhs->lhs_lvar, lhs->lhs_type);
6253 return OK;
6254}
6255
6256/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006257 * Produce code for loading "lhs" and also take care of an index.
6258 * Return OK/FAIL.
6259 */
6260 static int
6261compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6262{
6263 compile_load_lhs(lhs, var_start, NULL, cctx);
6264
6265 if (lhs->lhs_has_index)
6266 {
6267 int range = FALSE;
6268
6269 // Get member from list or dict. First compile the
6270 // index value.
6271 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6272 return FAIL;
6273 if (range)
6274 {
6275 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6276 var_start);
6277 return FAIL;
6278 }
6279
6280 // Get the member.
6281 if (compile_member(FALSE, cctx) == FAIL)
6282 return FAIL;
6283 }
6284 return OK;
6285}
6286
6287/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006288 * Assignment to a list or dict member, or ":unlet" for the item, using the
6289 * information in "lhs".
6290 * Returns OK or FAIL.
6291 */
6292 static int
6293compile_assign_unlet(
6294 char_u *var_start,
6295 lhs_T *lhs,
6296 int is_assign,
6297 type_T *rhs_type,
6298 cctx_T *cctx)
6299{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006300 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006301 garray_T *stack = &cctx->ctx_type_stack;
6302 int range = FALSE;
6303
Bram Moolenaar68452172021-04-12 21:21:02 +02006304 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006305 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006306 if (is_assign && range && lhs->lhs_type != &t_blob
6307 && lhs->lhs_type != &t_any)
6308 {
6309 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6310 return FAIL;
6311 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006312
6313 if (lhs->lhs_type == &t_any)
6314 {
6315 // Index on variable of unknown type: check at runtime.
6316 dest_type = VAR_ANY;
6317 }
6318 else
6319 {
6320 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006321 if (dest_type == VAR_DICT && range)
6322 {
6323 emsg(e_cannot_use_range_with_dictionary);
6324 return FAIL;
6325 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006326 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6327 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006328 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006329 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006330 type_T *type;
6331
6332 if (range)
6333 {
6334 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6335 if (need_type(type, &t_number,
6336 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006337 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006338 }
6339 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6340 if ((dest_type != VAR_BLOB || type != &t_special)
6341 && need_type(type, &t_number,
6342 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006343 return FAIL;
6344 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006345 }
6346
6347 // Load the dict or list. On the stack we then have:
6348 // - value (for assignment, not for :unlet)
6349 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006350 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006351 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006352 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6353 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006354
Bram Moolenaar68452172021-04-12 21:21:02 +02006355 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6356 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006357 {
6358 if (is_assign)
6359 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006360 if (range)
6361 {
6362 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6363 return FAIL;
6364 }
6365 else
6366 {
6367 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006368
Bram Moolenaar68452172021-04-12 21:21:02 +02006369 if (isn == NULL)
6370 return FAIL;
6371 isn->isn_arg.vartype = dest_type;
6372 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006373 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006374 else if (range)
6375 {
6376 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6377 return FAIL;
6378 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006379 else
6380 {
6381 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6382 return FAIL;
6383 }
6384 }
6385 else
6386 {
6387 emsg(_(e_indexable_type_required));
6388 return FAIL;
6389 }
6390
6391 return OK;
6392}
6393
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006394/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006395 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006396 * "let name"
6397 * "var name = expr"
6398 * "final name = expr"
6399 * "const name = expr"
6400 * "name = expr"
6401 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006402 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006403 * Return NULL for an error.
6404 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006405 */
6406 static char_u *
6407compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6408{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006409 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006410 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006411 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006412 char_u *ret = NULL;
6413 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006414 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006416 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006417 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006418 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419 int oplen = 0;
6420 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006421 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006422 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006424 int is_decl = is_decl_command(cmdidx);
6425 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006426 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006428 // Skip over the "var" or "[var, var]" to get to any "=".
6429 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6430 if (p == NULL)
6431 return *arg == '[' ? arg : NULL;
6432
6433 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006435 // TODO: should we allow this, and figure out type inference from list
6436 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006437 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006438 return NULL;
6439 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006440 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442 sp = p;
6443 p = skipwhite(p);
6444 op = p;
6445 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006446
6447 if (var_count > 0 && oplen == 0)
6448 // can be something like "[1, 2]->func()"
6449 return arg;
6450
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006451 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006452 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006453 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006454 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006455 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006456 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6457 {
6458 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6459 oplen = 2;
6460 incdec = TRUE;
6461 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006463 if (heredoc)
6464 {
6465 list_T *l;
6466 listitem_T *li;
6467
6468 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006469 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006470 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006471 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006472 if (l == NULL)
6473 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006474
Bram Moolenaar078269b2020-09-21 20:35:55 +02006475 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006476 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006477 // Push each line and the create the list.
6478 FOR_ALL_LIST_ITEMS(l, li)
6479 {
6480 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6481 li->li_tv.vval.v_string = NULL;
6482 }
6483 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006484 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006485 list_free(l);
6486 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006487 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006489 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006490 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006491 char_u *wp;
6492
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006493 // for "[var, var] = expr" evaluate the expression here, loop over the
6494 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006495 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006496
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006497 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006498 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006499 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006500 if (compile_expr0(&p, cctx) == FAIL)
6501 return NULL;
6502 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006503
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006504 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006505 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006506 type_T *stacktype;
6507
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006508 stacktype = stack->ga_len == 0 ? &t_void
6509 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006510 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006512 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006513 goto theend;
6514 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006515 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006516 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006517 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006518 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006519 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6520 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006521 if (stacktype->tt_member != NULL)
6522 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006523 }
6524 }
6525
6526 /*
6527 * Loop over variables in "[var, var] = expr".
6528 * For "var = expr" and "let var: type" this is done only once.
6529 */
6530 if (var_count > 0)
6531 var_start = skipwhite(arg + 1); // skip over the "["
6532 else
6533 var_start = arg;
6534 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6535 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006536 int instr_count = -1;
6537
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006538 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6539 {
6540 // Ignore underscore in "[a, _, b] = list".
6541 if (var_count > 0)
6542 {
6543 var_start = skipwhite(var_start + 2);
6544 continue;
6545 }
6546 emsg(_(e_cannot_use_underscore_here));
6547 goto theend;
6548 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006549 vim_free(lhs.lhs_name);
6550
6551 /*
6552 * Figure out the LHS type and other properties.
6553 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006554 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6555 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006556 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006557 if (!heredoc)
6558 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006559 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006560 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006561 if (oplen > 0 && var_count == 0)
6562 {
6563 // skip over the "=" and the expression
6564 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006565 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006566 }
6567 }
6568 else if (oplen > 0)
6569 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006570 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006571 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006572
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006573 // For "var = expr" evaluate the expression.
6574 if (var_count == 0)
6575 {
6576 int r;
6577
6578 // for "+=", "*=", "..=" etc. first load the current value
6579 if (*op != '=')
6580 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006581 if (compile_load_lhs_with_index(&lhs, var_start,
6582 cctx) == FAIL)
6583 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006584 }
6585
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006586 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006587 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006588 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006589 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006590 r = generate_PUSHNR(cctx, 1);
6591 }
6592 else
6593 {
6594 // Temporarily hide the new local variable here, it is
6595 // not available to this expression.
6596 if (lhs.lhs_new_local)
6597 --cctx->ctx_locals.ga_len;
6598 wp = op + oplen;
6599 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6600 {
6601 if (lhs.lhs_new_local)
6602 ++cctx->ctx_locals.ga_len;
6603 goto theend;
6604 }
6605 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006606 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006607 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006608 if (r == FAIL)
6609 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006610 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006611 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006612 else if (semicolon && var_idx == var_count - 1)
6613 {
6614 // For "[var; var] = expr" get the rest of the list
6615 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6616 goto theend;
6617 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006618 else
6619 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006620 // For "[var, var] = expr" get the "var_idx" item from the
6621 // list.
6622 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006623 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006624 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006625
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006626 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006627 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006628 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006629 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006630 if ((rhs_type->tt_type == VAR_FUNC
6631 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006632 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006633 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006634 goto theend;
6635
Bram Moolenaar752fc692021-01-04 21:57:11 +01006636 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006637 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006638 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006640 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006641 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006642 }
6643 else
6644 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006645 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006646 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006647 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006648 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006649 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006650 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006651 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006652 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006653 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006654 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006655 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006656 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006657 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006658 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006659 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006660
Bram Moolenaar77709b12021-04-03 21:01:01 +02006661 // Without operator check type here, otherwise below.
6662 // Use the line number of the assignment.
6663 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006664 if (lhs.lhs_has_index)
6665 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006666 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006667 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006668 goto theend;
6669 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006670 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006671 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006672 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006673 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006675 else if (cmdidx == CMD_final)
6676 {
6677 emsg(_(e_final_requires_a_value));
6678 goto theend;
6679 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006680 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006682 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006683 goto theend;
6684 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006685 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006686 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006687 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006688 goto theend;
6689 }
6690 else
6691 {
6692 // variables are always initialized
6693 if (ga_grow(instr, 1) == FAIL)
6694 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006695 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006696 {
6697 case VAR_BOOL:
6698 generate_PUSHBOOL(cctx, VVAL_FALSE);
6699 break;
6700 case VAR_FLOAT:
6701#ifdef FEAT_FLOAT
6702 generate_PUSHF(cctx, 0.0);
6703#endif
6704 break;
6705 case VAR_STRING:
6706 generate_PUSHS(cctx, NULL);
6707 break;
6708 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006709 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006710 break;
6711 case VAR_FUNC:
6712 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6713 break;
6714 case VAR_LIST:
6715 generate_NEWLIST(cctx, 0);
6716 break;
6717 case VAR_DICT:
6718 generate_NEWDICT(cctx, 0);
6719 break;
6720 case VAR_JOB:
6721 generate_PUSHJOB(cctx, NULL);
6722 break;
6723 case VAR_CHANNEL:
6724 generate_PUSHCHANNEL(cctx, NULL);
6725 break;
6726 case VAR_NUMBER:
6727 case VAR_UNKNOWN:
6728 case VAR_ANY:
6729 case VAR_PARTIAL:
6730 case VAR_VOID:
6731 case VAR_SPECIAL: // cannot happen
6732 generate_PUSHNR(cctx, 0);
6733 break;
6734 }
6735 }
6736 if (var_count == 0)
6737 end = p;
6738 }
6739
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006740 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006741 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006742 break;
6743
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006744 if (oplen > 0 && *op != '=')
6745 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006746 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006747 type_T *stacktype;
6748
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006749 if (*op == '.')
6750 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006751 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006752 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006753 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006754 if (
6755#ifdef FEAT_FLOAT
6756 // If variable is float operation with number is OK.
6757 !(expected == &t_float && stacktype == &t_number) &&
6758#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006759 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006760 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006761 goto theend;
6762
6763 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006764 {
6765 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6766 goto theend;
6767 }
6768 else if (*op == '+')
6769 {
6770 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006771 operator_type(lhs.lhs_member_type, stacktype),
6772 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006773 goto theend;
6774 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006775 else if (generate_two_op(cctx, op) == FAIL)
6776 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778
Bram Moolenaar752fc692021-01-04 21:57:11 +01006779 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006780 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006781 // Use the info in "lhs" to store the value at the index in the
6782 // list or dict.
6783 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6784 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006785 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006786 }
6787 else
6788 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006789 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006790 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006791 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006792 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006793 generate_LOCKCONST(cctx);
6794
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006795 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006796 && (lhs.lhs_type->tt_type == VAR_DICT
6797 || lhs.lhs_type->tt_type == VAR_LIST)
6798 && lhs.lhs_type->tt_member != NULL
6799 && lhs.lhs_type->tt_member != &t_any
6800 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006801 // Set the type in the list or dict, so that it can be checked,
6802 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006803 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006804
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006805 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6806 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006807 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006808
6809 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006810 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006811 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006812
6813 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006814 if (var_count > 0 && !semicolon)
6815 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006816 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006817 goto theend;
6818 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006819
Bram Moolenaarb2097502020-07-19 17:17:02 +02006820 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006821
6822theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006823 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824 return ret;
6825}
6826
6827/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006828 * Check for an assignment at "eap->cmd", compile it if found.
6829 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6830 */
6831 static int
6832may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6833{
6834 char_u *pskip;
6835 char_u *p;
6836
6837 // Assuming the command starts with a variable or function name,
6838 // find what follows.
6839 // Skip over "var.member", "var[idx]" and the like.
6840 // Also "&opt = val", "$ENV = val" and "@r = val".
6841 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6842 ? eap->cmd + 1 : eap->cmd;
6843 p = to_name_end(pskip, TRUE);
6844 if (p > eap->cmd && *p != NUL)
6845 {
6846 char_u *var_end;
6847 int oplen;
6848 int heredoc;
6849
6850 if (eap->cmd[0] == '@')
6851 var_end = eap->cmd + 2;
6852 else
6853 var_end = find_name_end(pskip, NULL, NULL,
6854 FNE_CHECK_START | FNE_INCL_BR);
6855 oplen = assignment_len(skipwhite(var_end), &heredoc);
6856 if (oplen > 0)
6857 {
6858 size_t len = p - eap->cmd;
6859
6860 // Recognize an assignment if we recognize the variable
6861 // name:
6862 // "g:var = expr"
6863 // "local = expr" where "local" is a local var.
6864 // "script = expr" where "script" is a script-local var.
6865 // "import = expr" where "import" is an imported var
6866 // "&opt = expr"
6867 // "$ENV = expr"
6868 // "@r = expr"
6869 if (*eap->cmd == '&'
6870 || *eap->cmd == '$'
6871 || *eap->cmd == '@'
6872 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006873 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006874 {
6875 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6876 if (*line == NULL || *line == eap->cmd)
6877 return FAIL;
6878 return OK;
6879 }
6880 }
6881 }
6882
6883 if (*eap->cmd == '[')
6884 {
6885 // [var, var] = expr
6886 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6887 if (*line == NULL)
6888 return FAIL;
6889 if (*line != eap->cmd)
6890 return OK;
6891 }
6892 return NOTDONE;
6893}
6894
6895/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006896 * Check if "name" can be "unlet".
6897 */
6898 int
6899check_vim9_unlet(char_u *name)
6900{
6901 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6902 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006903 // "unlet s:var" is allowed in legacy script.
6904 if (*name == 's' && !script_is_vim9())
6905 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006906 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006907 return FAIL;
6908 }
6909 return OK;
6910}
6911
6912/*
6913 * Callback passed to ex_unletlock().
6914 */
6915 static int
6916compile_unlet(
6917 lval_T *lvp,
6918 char_u *name_end,
6919 exarg_T *eap,
6920 int deep UNUSED,
6921 void *coookie)
6922{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006923 cctx_T *cctx = coookie;
6924 char_u *p = lvp->ll_name;
6925 int cc = *name_end;
6926 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006927
Bram Moolenaar752fc692021-01-04 21:57:11 +01006928 if (cctx->ctx_skip == SKIP_YES)
6929 return OK;
6930
6931 *name_end = NUL;
6932 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006933 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006934 // :unlet $ENV_VAR
6935 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6936 }
6937 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6938 {
6939 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006940
Bram Moolenaar752fc692021-01-04 21:57:11 +01006941 // This is similar to assigning: lookup the list/dict, compile the
6942 // idx/key. Then instead of storing the value unlet the item.
6943 // unlet {list}[idx]
6944 // unlet {dict}[key] dict.key
6945 //
6946 // Figure out the LHS type and other properties.
6947 //
6948 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6949
6950 // : unlet an indexed item
6951 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006952 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006953 iemsg("called compile_lhs() without an index");
6954 ret = FAIL;
6955 }
6956 else
6957 {
6958 // Use the info in "lhs" to unlet the item at the index in the
6959 // list or dict.
6960 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006961 }
6962
Bram Moolenaar752fc692021-01-04 21:57:11 +01006963 vim_free(lhs.lhs_name);
6964 }
6965 else if (check_vim9_unlet(p) == FAIL)
6966 {
6967 ret = FAIL;
6968 }
6969 else
6970 {
6971 // Normal name. Only supports g:, w:, t: and b: namespaces.
6972 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006973 }
6974
Bram Moolenaar752fc692021-01-04 21:57:11 +01006975 *name_end = cc;
6976 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006977}
6978
6979/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006980 * Callback passed to ex_unletlock().
6981 */
6982 static int
6983compile_lock_unlock(
6984 lval_T *lvp,
6985 char_u *name_end,
6986 exarg_T *eap,
6987 int deep UNUSED,
6988 void *coookie)
6989{
6990 cctx_T *cctx = coookie;
6991 int cc = *name_end;
6992 char_u *p = lvp->ll_name;
6993 int ret = OK;
6994 size_t len;
6995 char_u *buf;
6996
6997 if (cctx->ctx_skip == SKIP_YES)
6998 return OK;
6999
7000 // Cannot use :lockvar and :unlockvar on local variables.
7001 if (p[1] != ':')
7002 {
7003 char_u *end = skip_var_one(p, FALSE);
7004
7005 if (lookup_local(p, end - p, NULL, cctx) == OK)
7006 {
7007 emsg(_(e_cannot_lock_unlock_local_variable));
7008 return FAIL;
7009 }
7010 }
7011
7012 // Checking is done at runtime.
7013 *name_end = NUL;
7014 len = name_end - p + 20;
7015 buf = alloc(len);
7016 if (buf == NULL)
7017 ret = FAIL;
7018 else
7019 {
7020 vim_snprintf((char *)buf, len, "%s %s",
7021 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7022 p);
7023 ret = generate_EXEC(cctx, buf);
7024
7025 vim_free(buf);
7026 *name_end = cc;
7027 }
7028 return ret;
7029}
7030
7031/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007032 * compile "unlet var", "lock var" and "unlock var"
7033 * "arg" points to "var".
7034 */
7035 static char_u *
7036compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7037{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007038 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7039 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7040 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007041 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7042}
7043
7044/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045 * Compile an :import command.
7046 */
7047 static char_u *
7048compile_import(char_u *arg, cctx_T *cctx)
7049{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007050 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051}
7052
7053/*
7054 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7055 */
7056 static int
7057compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7058{
7059 garray_T *instr = &cctx->ctx_instr;
7060 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7061
7062 if (endlabel == NULL)
7063 return FAIL;
7064 endlabel->el_next = *el;
7065 *el = endlabel;
7066 endlabel->el_end_label = instr->ga_len;
7067
7068 generate_JUMP(cctx, when, 0);
7069 return OK;
7070}
7071
7072 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007073compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074{
7075 garray_T *instr = &cctx->ctx_instr;
7076
7077 while (*el != NULL)
7078 {
7079 endlabel_T *cur = (*el);
7080 isn_T *isn;
7081
7082 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007083 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 *el = cur->el_next;
7085 vim_free(cur);
7086 }
7087}
7088
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007089 static void
7090compile_free_jump_to_end(endlabel_T **el)
7091{
7092 while (*el != NULL)
7093 {
7094 endlabel_T *cur = (*el);
7095
7096 *el = cur->el_next;
7097 vim_free(cur);
7098 }
7099}
7100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101/*
7102 * Create a new scope and set up the generic items.
7103 */
7104 static scope_T *
7105new_scope(cctx_T *cctx, scopetype_T type)
7106{
7107 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7108
7109 if (scope == NULL)
7110 return NULL;
7111 scope->se_outer = cctx->ctx_scope;
7112 cctx->ctx_scope = scope;
7113 scope->se_type = type;
7114 scope->se_local_count = cctx->ctx_locals.ga_len;
7115 return scope;
7116}
7117
7118/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007119 * Free the current scope and go back to the outer scope.
7120 */
7121 static void
7122drop_scope(cctx_T *cctx)
7123{
7124 scope_T *scope = cctx->ctx_scope;
7125
7126 if (scope == NULL)
7127 {
7128 iemsg("calling drop_scope() without a scope");
7129 return;
7130 }
7131 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007132 switch (scope->se_type)
7133 {
7134 case IF_SCOPE:
7135 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7136 case FOR_SCOPE:
7137 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7138 case WHILE_SCOPE:
7139 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7140 case TRY_SCOPE:
7141 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7142 case NO_SCOPE:
7143 case BLOCK_SCOPE:
7144 break;
7145 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007146 vim_free(scope);
7147}
7148
7149/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007150 * compile "if expr"
7151 *
7152 * "if expr" Produces instructions:
7153 * EVAL expr Push result of "expr"
7154 * JUMP_IF_FALSE end
7155 * ... body ...
7156 * end:
7157 *
7158 * "if expr | else" Produces instructions:
7159 * EVAL expr Push result of "expr"
7160 * JUMP_IF_FALSE else
7161 * ... body ...
7162 * JUMP_ALWAYS end
7163 * else:
7164 * ... body ...
7165 * end:
7166 *
7167 * "if expr1 | elseif expr2 | else" Produces instructions:
7168 * EVAL expr Push result of "expr"
7169 * JUMP_IF_FALSE elseif
7170 * ... body ...
7171 * JUMP_ALWAYS end
7172 * elseif:
7173 * EVAL expr Push result of "expr"
7174 * JUMP_IF_FALSE else
7175 * ... body ...
7176 * JUMP_ALWAYS end
7177 * else:
7178 * ... body ...
7179 * end:
7180 */
7181 static char_u *
7182compile_if(char_u *arg, cctx_T *cctx)
7183{
7184 char_u *p = arg;
7185 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007186 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007187 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007188 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007189 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190
Bram Moolenaara5565e42020-05-09 15:44:01 +02007191 CLEAR_FIELD(ppconst);
7192 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007193 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007194 clear_ppconst(&ppconst);
7195 return NULL;
7196 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007197 if (!ends_excmd2(arg, skipwhite(p)))
7198 {
7199 semsg(_(e_trailing_arg), p);
7200 return NULL;
7201 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007202 if (cctx->ctx_skip == SKIP_YES)
7203 clear_ppconst(&ppconst);
7204 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007205 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007206 int error = FALSE;
7207 int v;
7208
Bram Moolenaara5565e42020-05-09 15:44:01 +02007209 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007210 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007211 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007212 if (error)
7213 return NULL;
7214 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007215 }
7216 else
7217 {
7218 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007219 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007220 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007221 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007222 if (bool_on_stack(cctx) == FAIL)
7223 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007224 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225
Bram Moolenaara91a7132021-03-25 21:12:15 +01007226 // CMDMOD_REV must come before the jump
7227 generate_undo_cmdmods(cctx);
7228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007229 scope = new_scope(cctx, IF_SCOPE);
7230 if (scope == NULL)
7231 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007232 scope->se_skip_save = skip_save;
7233 // "is_had_return" will be reset if any block does not end in :return
7234 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007235
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007236 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007237 {
7238 // "where" is set when ":elseif", "else" or ":endif" is found
7239 scope->se_u.se_if.is_if_label = instr->ga_len;
7240 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7241 }
7242 else
7243 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244
Bram Moolenaarced68a02021-01-24 17:53:47 +01007245#ifdef FEAT_PROFILE
7246 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7247 && skip_save != SKIP_YES)
7248 {
7249 // generated a profile start, need to generate a profile end, since it
7250 // won't be done after returning
7251 cctx->ctx_skip = SKIP_NOT;
7252 generate_instr(cctx, ISN_PROF_END);
7253 cctx->ctx_skip = SKIP_YES;
7254 }
7255#endif
7256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007257 return p;
7258}
7259
7260 static char_u *
7261compile_elseif(char_u *arg, cctx_T *cctx)
7262{
7263 char_u *p = arg;
7264 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007265 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007266 isn_T *isn;
7267 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007268 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007269 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007270
7271 if (scope == NULL || scope->se_type != IF_SCOPE)
7272 {
7273 emsg(_(e_elseif_without_if));
7274 return NULL;
7275 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007276 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007277 if (!cctx->ctx_had_return)
7278 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007279
Bram Moolenaarced68a02021-01-24 17:53:47 +01007280 if (cctx->ctx_skip == SKIP_NOT)
7281 {
7282 // previous block was executed, this one and following will not
7283 cctx->ctx_skip = SKIP_YES;
7284 scope->se_u.se_if.is_seen_skip_not = TRUE;
7285 }
7286 if (scope->se_u.se_if.is_seen_skip_not)
7287 {
7288 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007289 // Do not count the "elseif" for profiling and cmdmod
7290 instr->ga_len = current_instr_idx(cctx);
7291
Bram Moolenaarced68a02021-01-24 17:53:47 +01007292 skip_expr_cctx(&p, cctx);
7293 return p;
7294 }
7295
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007296 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007297 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007298 int moved_cmdmod = FALSE;
7299
7300 // Move any CMDMOD instruction to after the jump
7301 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7302 {
7303 if (ga_grow(instr, 1) == FAIL)
7304 return NULL;
7305 ((isn_T *)instr->ga_data)[instr->ga_len] =
7306 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7307 --instr->ga_len;
7308 moved_cmdmod = TRUE;
7309 }
7310
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007311 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007313 return NULL;
7314 // previous "if" or "elseif" jumps here
7315 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7316 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007317 if (moved_cmdmod)
7318 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007320
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007321 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007322 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007323 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007324 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007325 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007326#ifdef FEAT_PROFILE
7327 if (cctx->ctx_profiling)
7328 {
7329 // the previous block was skipped, need to profile this line
7330 generate_instr(cctx, ISN_PROF_START);
7331 instr_count = instr->ga_len;
7332 }
7333#endif
7334 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007335 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007336 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007337 clear_ppconst(&ppconst);
7338 return NULL;
7339 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007340 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007341 if (!ends_excmd2(arg, skipwhite(p)))
7342 {
7343 semsg(_(e_trailing_arg), p);
7344 return NULL;
7345 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007346 if (scope->se_skip_save == SKIP_YES)
7347 clear_ppconst(&ppconst);
7348 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007349 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007350 int error = FALSE;
7351 int v;
7352
Bram Moolenaar7f141552020-05-09 17:35:53 +02007353 // The expression results in a constant.
7354 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007355 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7356 if (error)
7357 return NULL;
7358 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007359 clear_ppconst(&ppconst);
7360 scope->se_u.se_if.is_if_label = -1;
7361 }
7362 else
7363 {
7364 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007365 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007366 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007367 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007368 if (bool_on_stack(cctx) == FAIL)
7369 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007370
Bram Moolenaara91a7132021-03-25 21:12:15 +01007371 // CMDMOD_REV must come before the jump
7372 generate_undo_cmdmods(cctx);
7373
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007374 // "where" is set when ":elseif", "else" or ":endif" is found
7375 scope->se_u.se_if.is_if_label = instr->ga_len;
7376 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378
7379 return p;
7380}
7381
7382 static char_u *
7383compile_else(char_u *arg, cctx_T *cctx)
7384{
7385 char_u *p = arg;
7386 garray_T *instr = &cctx->ctx_instr;
7387 isn_T *isn;
7388 scope_T *scope = cctx->ctx_scope;
7389
7390 if (scope == NULL || scope->se_type != IF_SCOPE)
7391 {
7392 emsg(_(e_else_without_if));
7393 return NULL;
7394 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007395 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007396 if (!cctx->ctx_had_return)
7397 scope->se_u.se_if.is_had_return = FALSE;
7398 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399
Bram Moolenaarced68a02021-01-24 17:53:47 +01007400#ifdef FEAT_PROFILE
7401 if (cctx->ctx_profiling)
7402 {
7403 if (cctx->ctx_skip == SKIP_NOT
7404 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7405 .isn_type == ISN_PROF_START)
7406 // the previous block was executed, do not count "else" for profiling
7407 --instr->ga_len;
7408 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7409 {
7410 // the previous block was not executed, this one will, do count the
7411 // "else" for profiling
7412 cctx->ctx_skip = SKIP_NOT;
7413 generate_instr(cctx, ISN_PROF_END);
7414 generate_instr(cctx, ISN_PROF_START);
7415 cctx->ctx_skip = SKIP_YES;
7416 }
7417 }
7418#endif
7419
7420 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007421 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007422 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007423 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007424 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007425 if (!cctx->ctx_had_return
7426 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7427 JUMP_ALWAYS, cctx) == FAIL)
7428 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007429 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007430
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007431 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007432 {
7433 if (scope->se_u.se_if.is_if_label >= 0)
7434 {
7435 // previous "if" or "elseif" jumps here
7436 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7437 isn->isn_arg.jump.jump_where = instr->ga_len;
7438 scope->se_u.se_if.is_if_label = -1;
7439 }
7440 }
7441
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007442 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007443 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445
7446 return p;
7447}
7448
7449 static char_u *
7450compile_endif(char_u *arg, cctx_T *cctx)
7451{
7452 scope_T *scope = cctx->ctx_scope;
7453 ifscope_T *ifscope;
7454 garray_T *instr = &cctx->ctx_instr;
7455 isn_T *isn;
7456
Bram Moolenaarfa984412021-03-25 22:15:28 +01007457 if (misplaced_cmdmod(cctx))
7458 return NULL;
7459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 if (scope == NULL || scope->se_type != IF_SCOPE)
7461 {
7462 emsg(_(e_endif_without_if));
7463 return NULL;
7464 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007465 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007466 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007467 if (!cctx->ctx_had_return)
7468 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007470 if (scope->se_u.se_if.is_if_label >= 0)
7471 {
7472 // previous "if" or "elseif" jumps here
7473 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7474 isn->isn_arg.jump.jump_where = instr->ga_len;
7475 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007477 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007478
7479#ifdef FEAT_PROFILE
7480 // even when skipping we count the endif as executed, unless the block it's
7481 // in is skipped
7482 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7483 && scope->se_skip_save != SKIP_YES)
7484 {
7485 cctx->ctx_skip = SKIP_NOT;
7486 generate_instr(cctx, ISN_PROF_START);
7487 }
7488#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007489 cctx->ctx_skip = scope->se_skip_save;
7490
7491 // If all the blocks end in :return and there is an :else then the
7492 // had_return flag is set.
7493 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007495 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 return arg;
7497}
7498
7499/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007500 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007501 *
7502 * Produces instructions:
7503 * PUSHNR -1
7504 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007505 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7507 * - if beyond end, jump to "end"
7508 * - otherwise get item from list and push it
7509 * STORE var Store item in "var"
7510 * ... body ...
7511 * JUMP top Jump back to repeat
7512 * end: DROP Drop the result of "expr"
7513 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007514 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7515 * UNPACK 2 Split item in 2
7516 * STORE var1 Store item in "var1"
7517 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 */
7519 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007520compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007522 char_u *arg;
7523 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007524 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007525 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007526 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007527 int var_count = 0;
7528 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007530 garray_T *stack = &cctx->ctx_type_stack;
7531 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007532 lvar_T *loop_lvar; // loop iteration variable
7533 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007534 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007535 type_T *item_type = &t_any;
7536 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537
Bram Moolenaar792f7862020-11-23 08:31:18 +01007538 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007539 if (p == NULL)
7540 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007541 if (var_count == 0)
7542 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007543
7544 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007545 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007546 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7547 return NULL;
7548 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549 {
7550 emsg(_(e_missing_in));
7551 return NULL;
7552 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007553 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007554 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7555 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007557 scope = new_scope(cctx, FOR_SCOPE);
7558 if (scope == NULL)
7559 return NULL;
7560
Bram Moolenaar792f7862020-11-23 08:31:18 +01007561 // Reserve a variable to store the loop iteration counter and initialize it
7562 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007563 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7564 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007565 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007566 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007567 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007569 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007570 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007571
7572 // compile "expr", it remains on the stack until "endfor"
7573 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007574 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007575 {
7576 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007577 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007578 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007579 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007581 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007582 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007583 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007584 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007585 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007586 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007587 semsg(_(e_for_loop_on_str_not_supported),
7588 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007589 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007590 return NULL;
7591 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007592
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007593 if (vartype->tt_type == VAR_STRING)
7594 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007595 else if (vartype->tt_type == VAR_BLOB)
7596 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007597 else if (vartype->tt_type == VAR_LIST
7598 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007599 {
7600 if (var_count == 1)
7601 item_type = vartype->tt_member;
7602 else if (vartype->tt_member->tt_type == VAR_LIST
7603 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007604 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007605 item_type = vartype->tt_member->tt_member;
7606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607
Bram Moolenaara91a7132021-03-25 21:12:15 +01007608 // CMDMOD_REV must come before the FOR instruction
7609 generate_undo_cmdmods(cctx);
7610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007611 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007612 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007613 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007614
Bram Moolenaar792f7862020-11-23 08:31:18 +01007615 arg = arg_start;
7616 if (var_count > 1)
7617 {
7618 generate_UNPACK(cctx, var_count, semicolon);
7619 arg = skipwhite(arg + 1); // skip white after '['
7620
7621 // the list item is replaced by a number of items
7622 if (ga_grow(stack, var_count - 1) == FAIL)
7623 {
7624 drop_scope(cctx);
7625 return NULL;
7626 }
7627 --stack->ga_len;
7628 for (idx = 0; idx < var_count; ++idx)
7629 {
7630 ((type_T **)stack->ga_data)[stack->ga_len] =
7631 (semicolon && idx == 0) ? vartype : item_type;
7632 ++stack->ga_len;
7633 }
7634 }
7635
7636 for (idx = 0; idx < var_count; ++idx)
7637 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007638 assign_dest_T dest = dest_local;
7639 int opt_flags = 0;
7640 int vimvaridx = -1;
7641 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007642 type_T *lhs_type = &t_any;
7643 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007644
7645 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007646 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007647 name = vim_strnsave(arg, varlen);
7648 if (name == NULL)
7649 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007650 if (*p == ':')
7651 {
7652 p = skipwhite(p + 1);
7653 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7654 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007655
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007656 // TODO: script var not supported?
7657 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7658 &vimvaridx, &type, cctx) == FAIL)
7659 goto failed;
7660 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007661 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007662 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7663 0, 0, type, name) == FAIL)
7664 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007665 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007666 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007667 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007668 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007669 {
7670 semsg(_(e_variable_already_declared), arg);
7671 goto failed;
7672 }
7673
Bram Moolenaarea870692020-12-02 14:24:30 +01007674 if (STRNCMP(name, "s:", 2) == 0)
7675 {
7676 semsg(_(e_cannot_declare_script_variable_in_function), name);
7677 goto failed;
7678 }
7679
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007680 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007681 where.wt_index = var_count > 1 ? idx + 1 : 0;
7682 where.wt_variable = TRUE;
7683 if (lhs_type == &t_any)
7684 lhs_type = item_type;
7685 else if (item_type != &t_unknown
7686 && !(var_count > 1 && item_type == &t_any)
7687 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7688 goto failed;
7689 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007690 if (var_lvar == NULL)
7691 // out of memory or used as an argument
7692 goto failed;
7693
7694 if (semicolon && idx == var_count - 1)
7695 var_lvar->lv_type = vartype;
7696 else
7697 var_lvar->lv_type = item_type;
7698 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7699 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007700
7701 if (*p == ',' || *p == ';')
7702 ++p;
7703 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007704 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007705 }
7706
7707 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007708
7709failed:
7710 vim_free(name);
7711 drop_scope(cctx);
7712 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713}
7714
7715/*
7716 * compile "endfor"
7717 */
7718 static char_u *
7719compile_endfor(char_u *arg, cctx_T *cctx)
7720{
7721 garray_T *instr = &cctx->ctx_instr;
7722 scope_T *scope = cctx->ctx_scope;
7723 forscope_T *forscope;
7724 isn_T *isn;
7725
Bram Moolenaarfa984412021-03-25 22:15:28 +01007726 if (misplaced_cmdmod(cctx))
7727 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007729 if (scope == NULL || scope->se_type != FOR_SCOPE)
7730 {
7731 emsg(_(e_for));
7732 return NULL;
7733 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007734 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007736 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737
7738 // At end of ":for" scope jump back to the FOR instruction.
7739 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7740
7741 // Fill in the "end" label in the FOR statement so it can jump here
7742 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7743 isn->isn_arg.forloop.for_end = instr->ga_len;
7744
7745 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007746 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007747
7748 // Below the ":for" scope drop the "expr" list from the stack.
7749 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7750 return NULL;
7751
7752 vim_free(scope);
7753
7754 return arg;
7755}
7756
7757/*
7758 * compile "while expr"
7759 *
7760 * Produces instructions:
7761 * top: EVAL expr Push result of "expr"
7762 * JUMP_IF_FALSE end jump if false
7763 * ... body ...
7764 * JUMP top Jump back to repeat
7765 * end:
7766 *
7767 */
7768 static char_u *
7769compile_while(char_u *arg, cctx_T *cctx)
7770{
7771 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007772 scope_T *scope;
7773
7774 scope = new_scope(cctx, WHILE_SCOPE);
7775 if (scope == NULL)
7776 return NULL;
7777
Bram Moolenaara91a7132021-03-25 21:12:15 +01007778 // "endwhile" jumps back here, one before when profiling or using cmdmods
7779 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007780
7781 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007782 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007783 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007784 if (!ends_excmd2(arg, skipwhite(p)))
7785 {
7786 semsg(_(e_trailing_arg), p);
7787 return NULL;
7788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007789
Bram Moolenaar13106602020-10-04 16:06:05 +02007790 if (bool_on_stack(cctx) == FAIL)
7791 return FAIL;
7792
Bram Moolenaara91a7132021-03-25 21:12:15 +01007793 // CMDMOD_REV must come before the jump
7794 generate_undo_cmdmods(cctx);
7795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007796 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007797 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798 JUMP_IF_FALSE, cctx) == FAIL)
7799 return FAIL;
7800
7801 return p;
7802}
7803
7804/*
7805 * compile "endwhile"
7806 */
7807 static char_u *
7808compile_endwhile(char_u *arg, cctx_T *cctx)
7809{
7810 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007811 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007812
Bram Moolenaarfa984412021-03-25 22:15:28 +01007813 if (misplaced_cmdmod(cctx))
7814 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7816 {
7817 emsg(_(e_while));
7818 return NULL;
7819 }
7820 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007821 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822
Bram Moolenaarf002a412021-01-24 13:34:18 +01007823#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007824 // count the endwhile before jumping
7825 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007826#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007828 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007829 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830
7831 // Fill in the "end" label in the WHILE statement so it can jump here.
7832 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007833 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7834 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835
7836 vim_free(scope);
7837
7838 return arg;
7839}
7840
7841/*
7842 * compile "continue"
7843 */
7844 static char_u *
7845compile_continue(char_u *arg, cctx_T *cctx)
7846{
7847 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007848 int try_scopes = 0;
7849 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850
7851 for (;;)
7852 {
7853 if (scope == NULL)
7854 {
7855 emsg(_(e_continue));
7856 return NULL;
7857 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007858 if (scope->se_type == FOR_SCOPE)
7859 {
7860 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007861 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007862 }
7863 if (scope->se_type == WHILE_SCOPE)
7864 {
7865 loop_label = scope->se_u.se_while.ws_top_label;
7866 break;
7867 }
7868 if (scope->se_type == TRY_SCOPE)
7869 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870 scope = scope->se_outer;
7871 }
7872
Bram Moolenaarc150c092021-02-13 15:02:46 +01007873 if (try_scopes > 0)
7874 // Inside one or more try/catch blocks we first need to jump to the
7875 // "finally" or "endtry" to cleanup.
7876 generate_TRYCONT(cctx, try_scopes, loop_label);
7877 else
7878 // Jump back to the FOR or WHILE instruction.
7879 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881 return arg;
7882}
7883
7884/*
7885 * compile "break"
7886 */
7887 static char_u *
7888compile_break(char_u *arg, cctx_T *cctx)
7889{
7890 scope_T *scope = cctx->ctx_scope;
7891 endlabel_T **el;
7892
7893 for (;;)
7894 {
7895 if (scope == NULL)
7896 {
7897 emsg(_(e_break));
7898 return NULL;
7899 }
7900 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7901 break;
7902 scope = scope->se_outer;
7903 }
7904
7905 // Jump to the end of the FOR or WHILE loop.
7906 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007907 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007909 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007910 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7911 return FAIL;
7912
7913 return arg;
7914}
7915
7916/*
7917 * compile "{" start of block
7918 */
7919 static char_u *
7920compile_block(char_u *arg, cctx_T *cctx)
7921{
7922 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7923 return NULL;
7924 return skipwhite(arg + 1);
7925}
7926
7927/*
7928 * compile end of block: drop one scope
7929 */
7930 static void
7931compile_endblock(cctx_T *cctx)
7932{
7933 scope_T *scope = cctx->ctx_scope;
7934
7935 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007936 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007937 vim_free(scope);
7938}
7939
7940/*
7941 * compile "try"
7942 * Creates a new scope for the try-endtry, pointing to the first catch and
7943 * finally.
7944 * Creates another scope for the "try" block itself.
7945 * TRY instruction sets up exception handling at runtime.
7946 *
7947 * "try"
7948 * TRY -> catch1, -> finally push trystack entry
7949 * ... try block
7950 * "throw {exception}"
7951 * EVAL {exception}
7952 * THROW create exception
7953 * ... try block
7954 * " catch {expr}"
7955 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007956 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007957 * EVAL {expr}
7958 * MATCH
7959 * JUMP nomatch -> catch2
7960 * CATCH remove exception
7961 * ... catch block
7962 * " catch"
7963 * JUMP -> finally
7964 * catch2: CATCH remove exception
7965 * ... catch block
7966 * " finally"
7967 * finally:
7968 * ... finally block
7969 * " endtry"
7970 * ENDTRY pop trystack entry, may rethrow
7971 */
7972 static char_u *
7973compile_try(char_u *arg, cctx_T *cctx)
7974{
7975 garray_T *instr = &cctx->ctx_instr;
7976 scope_T *try_scope;
7977 scope_T *scope;
7978
Bram Moolenaarfa984412021-03-25 22:15:28 +01007979 if (misplaced_cmdmod(cctx))
7980 return NULL;
7981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007982 // scope that holds the jumps that go to catch/finally/endtry
7983 try_scope = new_scope(cctx, TRY_SCOPE);
7984 if (try_scope == NULL)
7985 return NULL;
7986
Bram Moolenaar69f70502021-01-01 16:10:46 +01007987 if (cctx->ctx_skip != SKIP_YES)
7988 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007989 isn_T *isn;
7990
7991 // "try_catch" is set when the first ":catch" is found or when no catch
7992 // is found and ":finally" is found.
7993 // "try_finally" is set when ":finally" is found
7994 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007995 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007996 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7997 return NULL;
7998 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7999 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008000 return NULL;
8001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008002
8003 // scope for the try block itself
8004 scope = new_scope(cctx, BLOCK_SCOPE);
8005 if (scope == NULL)
8006 return NULL;
8007
8008 return arg;
8009}
8010
8011/*
8012 * compile "catch {expr}"
8013 */
8014 static char_u *
8015compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8016{
8017 scope_T *scope = cctx->ctx_scope;
8018 garray_T *instr = &cctx->ctx_instr;
8019 char_u *p;
8020 isn_T *isn;
8021
Bram Moolenaarfa984412021-03-25 22:15:28 +01008022 if (misplaced_cmdmod(cctx))
8023 return NULL;
8024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008025 // end block scope from :try or :catch
8026 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8027 compile_endblock(cctx);
8028 scope = cctx->ctx_scope;
8029
8030 // Error if not in a :try scope
8031 if (scope == NULL || scope->se_type != TRY_SCOPE)
8032 {
8033 emsg(_(e_catch));
8034 return NULL;
8035 }
8036
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008037 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008038 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008039 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008040 return NULL;
8041 }
8042
Bram Moolenaar69f70502021-01-01 16:10:46 +01008043 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008044 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008045#ifdef FEAT_PROFILE
8046 // the profile-start should be after the jump
8047 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8048 .isn_type == ISN_PROF_START)
8049 --instr->ga_len;
8050#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008051 // Jump from end of previous block to :finally or :endtry
8052 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8053 JUMP_ALWAYS, cctx) == FAIL)
8054 return NULL;
8055
8056 // End :try or :catch scope: set value in ISN_TRY instruction
8057 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008058 if (isn->isn_arg.try.try_ref->try_catch == 0)
8059 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008060 if (scope->se_u.se_try.ts_catch_label != 0)
8061 {
8062 // Previous catch without match jumps here
8063 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8064 isn->isn_arg.jump.jump_where = instr->ga_len;
8065 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008066#ifdef FEAT_PROFILE
8067 if (cctx->ctx_profiling)
8068 {
8069 // a "throw" that jumps here needs to be counted
8070 generate_instr(cctx, ISN_PROF_END);
8071 // the "catch" is also counted
8072 generate_instr(cctx, ISN_PROF_START);
8073 }
8074#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008075 }
8076
8077 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008078 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008080 scope->se_u.se_try.ts_caught_all = TRUE;
8081 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008082 }
8083 else
8084 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008085 char_u *end;
8086 char_u *pat;
8087 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008088 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008089 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008091 // Push v:exception, push {expr} and MATCH
8092 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8093
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008094 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008095 if (*end != *p)
8096 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008097 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008098 vim_free(tofree);
8099 return FAIL;
8100 }
8101 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008102 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008103 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008104 len = (int)(end - tofree);
8105 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008106 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008107 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008108 if (pat == NULL)
8109 return FAIL;
8110 if (generate_PUSHS(cctx, pat) == FAIL)
8111 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008113 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8114 return NULL;
8115
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008116 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8118 return NULL;
8119 }
8120
Bram Moolenaar69f70502021-01-01 16:10:46 +01008121 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008122 return NULL;
8123
8124 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8125 return NULL;
8126 return p;
8127}
8128
8129 static char_u *
8130compile_finally(char_u *arg, cctx_T *cctx)
8131{
8132 scope_T *scope = cctx->ctx_scope;
8133 garray_T *instr = &cctx->ctx_instr;
8134 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008135 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008136
Bram Moolenaarfa984412021-03-25 22:15:28 +01008137 if (misplaced_cmdmod(cctx))
8138 return NULL;
8139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008140 // end block scope from :try or :catch
8141 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8142 compile_endblock(cctx);
8143 scope = cctx->ctx_scope;
8144
8145 // Error if not in a :try scope
8146 if (scope == NULL || scope->se_type != TRY_SCOPE)
8147 {
8148 emsg(_(e_finally));
8149 return NULL;
8150 }
8151
8152 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008153 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008154 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155 {
8156 emsg(_(e_finally_dup));
8157 return NULL;
8158 }
8159
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008160 this_instr = instr->ga_len;
8161#ifdef FEAT_PROFILE
8162 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8163 .isn_type == ISN_PROF_START)
8164 // jump to the profile start of the "finally"
8165 --this_instr;
8166#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008167
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008168 // Fill in the "end" label in jumps at the end of the blocks.
8169 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8170 this_instr, cctx);
8171
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008172 // If there is no :catch then an exception jumps to :finally.
8173 if (isn->isn_arg.try.try_ref->try_catch == 0)
8174 isn->isn_arg.try.try_ref->try_catch = this_instr;
8175 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008176 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177 {
8178 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008179 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008180 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008181 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008182 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008183 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8184 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008185
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008186 // TODO: set index in ts_finally_label jumps
8187
8188 return arg;
8189}
8190
8191 static char_u *
8192compile_endtry(char_u *arg, cctx_T *cctx)
8193{
8194 scope_T *scope = cctx->ctx_scope;
8195 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008196 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008197
Bram Moolenaarfa984412021-03-25 22:15:28 +01008198 if (misplaced_cmdmod(cctx))
8199 return NULL;
8200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008201 // end block scope from :catch or :finally
8202 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8203 compile_endblock(cctx);
8204 scope = cctx->ctx_scope;
8205
8206 // Error if not in a :try scope
8207 if (scope == NULL || scope->se_type != TRY_SCOPE)
8208 {
8209 if (scope == NULL)
8210 emsg(_(e_no_endtry));
8211 else if (scope->se_type == WHILE_SCOPE)
8212 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008213 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008214 emsg(_(e_endfor));
8215 else
8216 emsg(_(e_endif));
8217 return NULL;
8218 }
8219
Bram Moolenaarc150c092021-02-13 15:02:46 +01008220 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008221 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008222 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008223 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8224 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008225 {
8226 emsg(_(e_missing_catch_or_finally));
8227 return NULL;
8228 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008229
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008230#ifdef FEAT_PROFILE
8231 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8232 .isn_type == ISN_PROF_START)
8233 // move the profile start after "endtry" so that it's not counted when
8234 // the exception is rethrown.
8235 --instr->ga_len;
8236#endif
8237
Bram Moolenaar69f70502021-01-01 16:10:46 +01008238 // Fill in the "end" label in jumps at the end of the blocks, if not
8239 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008240 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8241 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008242
Bram Moolenaar69f70502021-01-01 16:10:46 +01008243 if (scope->se_u.se_try.ts_catch_label != 0)
8244 {
8245 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008246 isn_T *isn = ((isn_T *)instr->ga_data)
8247 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008248 isn->isn_arg.jump.jump_where = instr->ga_len;
8249 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008250 }
8251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008252 compile_endblock(cctx);
8253
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008254 if (cctx->ctx_skip != SKIP_YES)
8255 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008256 // End :catch or :finally scope: set instruction index in ISN_TRY
8257 // instruction
8258 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008259 if (cctx->ctx_skip != SKIP_YES
8260 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8261 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008262#ifdef FEAT_PROFILE
8263 if (cctx->ctx_profiling)
8264 generate_instr(cctx, ISN_PROF_START);
8265#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008266 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008267 return arg;
8268}
8269
8270/*
8271 * compile "throw {expr}"
8272 */
8273 static char_u *
8274compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8275{
8276 char_u *p = skipwhite(arg);
8277
Bram Moolenaara5565e42020-05-09 15:44:01 +02008278 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008279 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008280 if (cctx->ctx_skip == SKIP_YES)
8281 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008282 if (may_generate_2STRING(-1, cctx) == FAIL)
8283 return NULL;
8284 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8285 return NULL;
8286
8287 return p;
8288}
8289
8290/*
8291 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008292 * compile "echomsg expr"
8293 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008294 * compile "execute expr"
8295 */
8296 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008297compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008298{
8299 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008300 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008301 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008302 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008303
8304 for (;;)
8305 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008306 if (ends_excmd2(prev, p))
8307 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008308 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008309 return NULL;
8310 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008311 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008312 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008313 }
8314
Bram Moolenaare4984292020-12-13 14:19:25 +01008315 if (count > 0)
8316 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008317 long save_lnum = cctx->ctx_lnum;
8318
8319 // Use the line number where the command started.
8320 cctx->ctx_lnum = start_ctx_lnum;
8321
Bram Moolenaare4984292020-12-13 14:19:25 +01008322 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8323 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8324 else if (cmdidx == CMD_execute)
8325 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8326 else if (cmdidx == CMD_echomsg)
8327 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8328 else
8329 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008330
8331 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008333 return p;
8334}
8335
8336/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008337 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008338 * instruction to compute it and return OK.
8339 * Otherwise return FAIL, the caller must deal with any range.
8340 */
8341 static int
8342compile_variable_range(exarg_T *eap, cctx_T *cctx)
8343{
8344 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8345 char_u *p = skipdigits(eap->cmd);
8346
8347 if (p == range_end)
8348 return FAIL;
8349 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8350}
8351
8352/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008353 * :put r
8354 * :put ={expr}
8355 */
8356 static char_u *
8357compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8358{
8359 char_u *line = arg;
8360 linenr_T lnum;
8361 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008362 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008363
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008364 eap->regname = *line;
8365
8366 if (eap->regname == '=')
8367 {
8368 char_u *p = line + 1;
8369
8370 if (compile_expr0(&p, cctx) == FAIL)
8371 return NULL;
8372 line = p;
8373 }
8374 else if (eap->regname != NUL)
8375 ++line;
8376
Bram Moolenaar08597872020-12-10 19:43:40 +01008377 if (compile_variable_range(eap, cctx) == OK)
8378 {
8379 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8380 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008381 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008382 {
8383 // Either no range or a number.
8384 // "errormsg" will not be set because the range is ADDR_LINES.
8385 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008386 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008387 return NULL;
8388 if (eap->addr_count == 0)
8389 lnum = -1;
8390 else
8391 lnum = eap->line2;
8392 if (above)
8393 --lnum;
8394 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008395
8396 generate_PUT(cctx, eap->regname, lnum);
8397 return line;
8398}
8399
8400/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008401 * A command that is not compiled, execute with legacy code.
8402 */
8403 static char_u *
8404compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8405{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008406 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008407 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008408 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008409
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008410 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008411 goto theend;
8412
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008413 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008414 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008415 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008416 int usefilter = FALSE;
8417
8418 has_expr = argt & (EX_XFILE | EX_EXPAND);
8419
8420 // If the command can be followed by a bar, find the bar and truncate
8421 // it, so that the following command can be compiled.
8422 // The '|' is overwritten with a NUL, it is put back below.
8423 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8424 && *eap->arg == '!')
8425 // :w !filter or :r !filter or :r! filter
8426 usefilter = TRUE;
8427 if ((argt & EX_TRLBAR) && !usefilter)
8428 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008429 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008430 separate_nextcmd(eap);
8431 if (eap->nextcmd != NULL)
8432 nextcmd = eap->nextcmd;
8433 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008434 else if (eap->cmdidx == CMD_wincmd)
8435 {
8436 p = eap->arg;
8437 if (*p != NUL)
8438 ++p;
8439 if (*p == 'g' || *p == Ctrl_G)
8440 ++p;
8441 p = skipwhite(p);
8442 if (*p == '|')
8443 {
8444 *p = NUL;
8445 nextcmd = p + 1;
8446 }
8447 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008448 }
8449
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008450 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8451 {
8452 // expand filename in "syntax include [@group] filename"
8453 has_expr = TRUE;
8454 eap->arg = skipwhite(eap->arg + 7);
8455 if (*eap->arg == '@')
8456 eap->arg = skiptowhite(eap->arg);
8457 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008458
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008459 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8460 && STRLEN(eap->arg) > 4)
8461 {
8462 int delim = *eap->arg;
8463
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008464 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008465 if (*p == delim)
8466 {
8467 eap->arg = p + 1;
8468 has_expr = TRUE;
8469 }
8470 }
8471
Bram Moolenaarecac5912021-01-05 19:23:28 +01008472 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8473 {
8474 // TODO: should only expand when appropriate for the command
8475 eap->arg = skiptowhite(eap->arg);
8476 has_expr = TRUE;
8477 }
8478
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008479 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008480 {
8481 int count = 0;
8482 char_u *start = skipwhite(line);
8483
8484 // :cmd xxx`=expr1`yyy`=expr2`zzz
8485 // PUSHS ":cmd xxx"
8486 // eval expr1
8487 // PUSHS "yyy"
8488 // eval expr2
8489 // PUSHS "zzz"
8490 // EXECCONCAT 5
8491 for (;;)
8492 {
8493 if (p > start)
8494 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008495 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008496 ++count;
8497 }
8498 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008499 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008500 return NULL;
8501 may_generate_2STRING(-1, cctx);
8502 ++count;
8503 p = skipwhite(p);
8504 if (*p != '`')
8505 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008506 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008507 return NULL;
8508 }
8509 start = p + 1;
8510
8511 p = (char_u *)strstr((char *)start, "`=");
8512 if (p == NULL)
8513 {
8514 if (*skipwhite(start) != NUL)
8515 {
8516 generate_PUSHS(cctx, vim_strsave(start));
8517 ++count;
8518 }
8519 break;
8520 }
8521 }
8522 generate_EXECCONCAT(cctx, count);
8523 }
8524 else
8525 generate_EXEC(cctx, line);
8526
8527theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008528 if (*nextcmd != NUL)
8529 {
8530 // the parser expects a pointer to the bar, put it back
8531 --nextcmd;
8532 *nextcmd = '|';
8533 }
8534
8535 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008536}
8537
Bram Moolenaar8238f082021-04-20 21:10:48 +02008538
8539 static void
8540clear_instr_ga(garray_T *gap)
8541{
8542 int idx;
8543
8544 for (idx = 0; idx < gap->ga_len; ++idx)
8545 delete_instr(((isn_T *)gap->ga_data) + idx);
8546 ga_clear(gap);
8547}
8548
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008549/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008550 * :s/pat/repl/
8551 */
8552 static char_u *
8553compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8554{
8555 char_u *cmd = eap->arg;
8556 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8557
8558 if (expr != NULL)
8559 {
8560 int delimiter = *cmd++;
8561
8562 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008563 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008564 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8565 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008566 garray_T save_ga = cctx->ctx_instr;
8567 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008568 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008569 int trailing_error;
8570 int instr_count;
8571 isn_T *instr = NULL;
8572 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008573
8574 cmd += 3;
8575 end = skip_substitute(cmd, delimiter);
8576
Bram Moolenaar8238f082021-04-20 21:10:48 +02008577 // Temporarily reset the list of instructions so that the jumps
8578 // labels are correct.
8579 cctx->ctx_instr.ga_len = 0;
8580 cctx->ctx_instr.ga_maxlen = 0;
8581 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008582 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008583 if (end[-1] == NUL)
8584 end[-1] = delimiter;
8585 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008586 trailing_error = *cmd != delimiter && *cmd != NUL;
8587
Bram Moolenaar169502f2021-04-21 12:19:35 +02008588 if (expr_res == FAIL || trailing_error
8589 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008590 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008591 if (trailing_error)
8592 semsg(_(e_trailing_arg), cmd);
8593 clear_instr_ga(&cctx->ctx_instr);
8594 cctx->ctx_instr = save_ga;
8595 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008596 return NULL;
8597 }
8598
Bram Moolenaar8238f082021-04-20 21:10:48 +02008599 // Move the generated instructions into the ISN_SUBSTITUTE
8600 // instructions, then restore the list of instructions before
8601 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008602 instr_count = cctx->ctx_instr.ga_len;
8603 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008604 instr[instr_count].isn_type = ISN_FINISH;
8605
8606 cctx->ctx_instr = save_ga;
8607 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8608 {
8609 int idx;
8610
8611 for (idx = 0; idx < instr_count; ++idx)
8612 delete_instr(instr + idx);
8613 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008614 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008615 }
8616 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8617 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008618
8619 // skip over flags
8620 if (*end == '&')
8621 ++end;
8622 while (ASCII_ISALPHA(*end) || *end == '#')
8623 ++end;
8624 return end;
8625 }
8626 }
8627
8628 return compile_exec(arg, eap, cctx);
8629}
8630
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008631 static char_u *
8632compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8633{
8634 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008635 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008636
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008637 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008638 {
8639 if (STRNCMP(arg, "END", 3) == 0)
8640 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008641 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008642 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008643 // First load the current variable value.
8644 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8645 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008646 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008647 }
8648
8649 // Gets the redirected text and put it on the stack, then store it
8650 // in the variable.
8651 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8652
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008653 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008654 generate_instr_drop(cctx, ISN_CONCAT, 1);
8655
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008656 if (lhs->lhs_has_index)
8657 {
8658 // Use the info in "lhs" to store the value at the index in the
8659 // list or dict.
8660 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8661 &t_string, cctx) == FAIL)
8662 return NULL;
8663 }
8664 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008665 return NULL;
8666
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008667 VIM_CLEAR(lhs->lhs_name);
8668 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008669 return arg + 3;
8670 }
8671 emsg(_(e_cannot_nest_redir));
8672 return NULL;
8673 }
8674
8675 if (arg[0] == '=' && arg[1] == '>')
8676 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008677 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008678
8679 // redirect to a variable is compiled
8680 arg += 2;
8681 if (*arg == '>')
8682 {
8683 ++arg;
8684 append = TRUE;
8685 }
8686 arg = skipwhite(arg);
8687
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008688 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008689 FALSE, FALSE, 1, cctx) == FAIL)
8690 return NULL;
8691 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008692 lhs->lhs_append = append;
8693 if (lhs->lhs_has_index)
8694 {
8695 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8696 if (lhs->lhs_whole == NULL)
8697 return NULL;
8698 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008699
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008700 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008701 }
8702
8703 // other redirects are handled like at script level
8704 return compile_exec(line, eap, cctx);
8705}
8706
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008707 static char_u *
8708compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8709{
8710 isn_T *isn;
8711 char_u *p;
8712
8713 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8714 if (isn == NULL)
8715 return NULL;
8716 isn->isn_arg.number = eap->cmdidx;
8717
8718 p = eap->arg;
8719 if (compile_expr0(&p, cctx) == FAIL)
8720 return NULL;
8721
8722 isn = generate_instr(cctx, ISN_CEXPR_CORE);
8723 if (isn == NULL)
8724 return NULL;
8725 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
8726 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
8727 return NULL;
8728 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
8729 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
8730 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
8731
8732 return p;
8733}
8734
Bram Moolenaar4c137212021-04-19 16:48:48 +02008735/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008736 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008737 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008738 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008739 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008740add_def_function(ufunc_T *ufunc)
8741{
8742 dfunc_T *dfunc;
8743
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008744 if (def_functions.ga_len == 0)
8745 {
8746 // The first position is not used, so that a zero uf_dfunc_idx means it
8747 // wasn't set.
8748 if (ga_grow(&def_functions, 1) == FAIL)
8749 return FAIL;
8750 ++def_functions.ga_len;
8751 }
8752
Bram Moolenaar09689a02020-05-09 22:50:08 +02008753 // Add the function to "def_functions".
8754 if (ga_grow(&def_functions, 1) == FAIL)
8755 return FAIL;
8756 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8757 CLEAR_POINTER(dfunc);
8758 dfunc->df_idx = def_functions.ga_len;
8759 ufunc->uf_dfunc_idx = dfunc->df_idx;
8760 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008761 dfunc->df_name = vim_strsave(ufunc->uf_name);
8762 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008763 ++def_functions.ga_len;
8764 return OK;
8765}
8766
8767/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008768 * After ex_function() has collected all the function lines: parse and compile
8769 * the lines into instructions.
8770 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008771 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8772 * the return statement (used for lambda). When uf_ret_type is already set
8773 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008774 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008775 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008776 * This can be used recursively through compile_lambda(), which may reallocate
8777 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008778 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008779 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008780 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008781compile_def_function(
8782 ufunc_T *ufunc,
8783 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008784 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008785 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008786{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008787 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008788 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008789 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008790 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008791 cctx_T cctx;
8792 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008793 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008794 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008795 int ret = FAIL;
8796 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008797 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02008798 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008799 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008800 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008801#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008802 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008803#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008804
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008805 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008806 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008807 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008808 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008809 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8810 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008811 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008812 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008813 else
8814 {
8815 if (add_def_function(ufunc) == FAIL)
8816 return FAIL;
8817 new_def_function = TRUE;
8818 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008819
Bram Moolenaar985116a2020-07-12 17:31:09 +02008820 ufunc->uf_def_status = UF_COMPILING;
8821
Bram Moolenaara80faa82020-04-12 19:37:17 +02008822 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008823
Bram Moolenaarf002a412021-01-24 13:34:18 +01008824#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008825 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008826#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008827 cctx.ctx_ufunc = ufunc;
8828 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008829 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008830 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8831 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8832 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8833 cctx.ctx_type_list = &ufunc->uf_type_list;
8834 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8835 instr = &cctx.ctx_instr;
8836
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008837 // Set the context to the function, it may be compiled when called from
8838 // another script. Set the script version to the most modern one.
8839 // The line number will be set in next_line_from_context().
8840 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008841 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8842
Bram Moolenaardc4c2302021-04-25 13:54:42 +02008843 // Don't use the flag from ":legacy" here.
8844 cmdmod.cmod_flags &= ~CMOD_LEGACY;
8845
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008846 // Make sure error messages are OK.
8847 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8848 if (do_estack_push)
8849 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008850 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008851
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008852 if (ufunc->uf_def_args.ga_len > 0)
8853 {
8854 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008855 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008856 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008857 int i;
8858 char_u *arg;
8859 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008860 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008861
8862 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008863 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008864 for (i = 0; i < count; ++i)
8865 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008866 garray_T *stack = &cctx.ctx_type_stack;
8867 type_T *val_type;
8868 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008869 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008870 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008871 int jump_instr_idx = instr->ga_len;
8872 isn_T *isn;
8873
8874 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8875 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8876 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008877
8878 // Make sure later arguments are not found.
8879 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008880
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008881 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008882 r = compile_expr0(&arg, &cctx);
8883
8884 ufunc->uf_args.ga_len = uf_args_len;
8885 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008886 goto erret;
8887
8888 // If no type specified use the type of the default value.
8889 // Otherwise check that the default value type matches the
8890 // specified type.
8891 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008892 where.wt_index = arg_idx + 1;
8893 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008894 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008895 {
8896 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008897 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008898 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008899 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008900 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008901 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008902
8903 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008904 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008905
8906 // set instruction index in JUMP_IF_ARG_SET to here
8907 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8908 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008909 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008910
8911 if (did_set_arg_type)
8912 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008913 }
8914
8915 /*
8916 * Loop over all the lines of the function and generate instructions.
8917 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008918 for (;;)
8919 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008920 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008921 int starts_with_colon = FALSE;
8922 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008923 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008924
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008925 // Bail out on the first error to avoid a flood of errors and report
8926 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008927 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008928 goto erret;
8929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008930 if (line != NULL && *line == '|')
8931 // the line continues after a '|'
8932 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008933 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008934 && !(*line == '#' && (line == cctx.ctx_line_start
8935 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008936 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008937 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008938 goto erret;
8939 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008940 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8941 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008942 else
8943 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008944 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008945 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008946 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008947 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008948#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008949 if (cctx.ctx_skip != SKIP_YES)
8950 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008951#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008952 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008953 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008954 // Make a copy, splitting off nextcmd and removing trailing spaces
8955 // may change it.
8956 if (line != NULL)
8957 {
8958 line = vim_strsave(line);
8959 vim_free(line_to_free);
8960 line_to_free = line;
8961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008962 }
8963
Bram Moolenaara80faa82020-04-12 19:37:17 +02008964 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008965 ea.cmdlinep = &line;
8966 ea.cmd = skipwhite(line);
8967
Bram Moolenaarb2049902021-01-24 12:53:53 +01008968 if (*ea.cmd == '#')
8969 {
8970 // "#" starts a comment
8971 line = (char_u *)"";
8972 continue;
8973 }
8974
Bram Moolenaarf002a412021-01-24 13:34:18 +01008975#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008976 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8977 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008978 {
8979 may_generate_prof_end(&cctx, prof_lnum);
8980
8981 prof_lnum = cctx.ctx_lnum;
8982 generate_instr(&cctx, ISN_PROF_START);
8983 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008984#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008985
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008986 // Some things can be recognized by the first character.
8987 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008988 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008989 case '}':
8990 {
8991 // "}" ends a block scope
8992 scopetype_T stype = cctx.ctx_scope == NULL
8993 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008994
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008995 if (stype == BLOCK_SCOPE)
8996 {
8997 compile_endblock(&cctx);
8998 line = ea.cmd;
8999 }
9000 else
9001 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009002 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009003 goto erret;
9004 }
9005 if (line != NULL)
9006 line = skipwhite(ea.cmd + 1);
9007 continue;
9008 }
9009
9010 case '{':
9011 // "{" starts a block scope
9012 // "{'a': 1}->func() is something else
9013 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9014 {
9015 line = compile_block(ea.cmd, &cctx);
9016 continue;
9017 }
9018 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009019 }
9020
9021 /*
9022 * COMMAND MODIFIERS
9023 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009024 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009025 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9026 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009027 {
9028 if (errormsg != NULL)
9029 goto erret;
9030 // empty line or comment
9031 line = (char_u *)"";
9032 continue;
9033 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009034 generate_cmdmods(&cctx, &local_cmdmod);
9035 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009036
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009037 // Check if there was a colon after the last command modifier or before
9038 // the current position.
9039 for (p = ea.cmd; p >= line; --p)
9040 {
9041 if (*p == ':')
9042 starts_with_colon = TRUE;
9043 if (p < ea.cmd && !VIM_ISWHITE(*p))
9044 break;
9045 }
9046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009047 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009048 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009049 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009050 {
9051 if (*ea.cmd == '(')
9052 // not for "call()"
9053 ea.cmd = p;
9054 else
9055 ea.cmd = skipwhite(ea.cmd);
9056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009057
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009058 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009059 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009060 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009061
Bram Moolenaar17126b12021-01-07 22:03:02 +01009062 // Check for assignment after command modifiers.
9063 assign = may_compile_assignment(&ea, &line, &cctx);
9064 if (assign == OK)
9065 goto nextline;
9066 if (assign == FAIL)
9067 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009068 }
9069
9070 /*
9071 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009072 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009073 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009074 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009075 cmd = ea.cmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009076 if (starts_with_colon || !(*cmd == '\''
9077 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009078 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009079 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009080 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009081 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009082 if (!starts_with_colon)
9083 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009084 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009085 goto erret;
9086 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009087 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009088 if (ends_excmd2(line, ea.cmd))
9089 {
9090 // A range without a command: jump to the line.
9091 // TODO: compile to a more efficient command, possibly
9092 // calling parse_cmd_address().
9093 ea.cmdidx = CMD_SIZE;
9094 line = compile_exec(line, &ea, &cctx);
9095 goto nextline;
9096 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009097 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009098 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009099 p = find_ex_command(&ea, NULL, starts_with_colon
9100 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009101
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009102 if (p == NULL)
9103 {
9104 if (cctx.ctx_skip != SKIP_YES)
9105 emsg(_(e_ambiguous_use_of_user_defined_command));
9106 goto erret;
9107 }
9108
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009109 // When using ":legacy cmd" always use compile_exec().
9110 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
9111 ea.cmdidx = CMD_legacy;
9112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009113 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9114 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009115 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009116 {
9117 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009118 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009119 }
9120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009121 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009122 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009123 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009124 // CMD_var cannot happen, compile_assignment() above would be
9125 // used. Most likely an assignment to a non-existing variable.
9126 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009127 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009129 }
9130
Bram Moolenaar3988f642020-08-27 22:43:03 +02009131 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009132 && ea.cmdidx != CMD_elseif
9133 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009134 && ea.cmdidx != CMD_endif
9135 && ea.cmdidx != CMD_endfor
9136 && ea.cmdidx != CMD_endwhile
9137 && ea.cmdidx != CMD_catch
9138 && ea.cmdidx != CMD_finally
9139 && ea.cmdidx != CMD_endtry)
9140 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009141 emsg(_(e_unreachable_code_after_return));
9142 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009143 }
9144
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009145 p = skipwhite(p);
9146 if (ea.cmdidx != CMD_SIZE
9147 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9148 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009149 if (ea.cmdidx >= 0)
9150 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009151 if ((ea.argt & EX_BANG) && *p == '!')
9152 {
9153 ea.forceit = TRUE;
9154 p = skipwhite(p + 1);
9155 }
9156 }
9157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009158 switch (ea.cmdidx)
9159 {
9160 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009161 ea.arg = p;
9162 line = compile_nested_function(&ea, &cctx);
9163 break;
9164
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009165 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009166 // TODO: should we allow this, e.g. to declare a global
9167 // function?
9168 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009169 goto erret;
9170
9171 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009172 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009173 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009174 break;
9175
9176 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009177 emsg(_(e_cannot_use_let_in_vim9_script));
9178 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009179 case CMD_var:
9180 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009181 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009182 case CMD_increment:
9183 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009184 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009185 if (line == p)
9186 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009187 break;
9188
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009189 case CMD_unlet:
9190 case CMD_unlockvar:
9191 case CMD_lockvar:
9192 line = compile_unletlock(p, &ea, &cctx);
9193 break;
9194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009195 case CMD_import:
9196 line = compile_import(p, &cctx);
9197 break;
9198
9199 case CMD_if:
9200 line = compile_if(p, &cctx);
9201 break;
9202 case CMD_elseif:
9203 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009204 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009205 break;
9206 case CMD_else:
9207 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009208 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009209 break;
9210 case CMD_endif:
9211 line = compile_endif(p, &cctx);
9212 break;
9213
9214 case CMD_while:
9215 line = compile_while(p, &cctx);
9216 break;
9217 case CMD_endwhile:
9218 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009219 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009220 break;
9221
9222 case CMD_for:
9223 line = compile_for(p, &cctx);
9224 break;
9225 case CMD_endfor:
9226 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009227 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009228 break;
9229 case CMD_continue:
9230 line = compile_continue(p, &cctx);
9231 break;
9232 case CMD_break:
9233 line = compile_break(p, &cctx);
9234 break;
9235
9236 case CMD_try:
9237 line = compile_try(p, &cctx);
9238 break;
9239 case CMD_catch:
9240 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009241 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009242 break;
9243 case CMD_finally:
9244 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009245 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009246 break;
9247 case CMD_endtry:
9248 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009249 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009250 break;
9251 case CMD_throw:
9252 line = compile_throw(p, &cctx);
9253 break;
9254
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009255 case CMD_eval:
9256 if (compile_expr0(&p, &cctx) == FAIL)
9257 goto erret;
9258
Bram Moolenaar3988f642020-08-27 22:43:03 +02009259 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009260 generate_instr_drop(&cctx, ISN_DROP, 1);
9261
9262 line = skipwhite(p);
9263 break;
9264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009265 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009266 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009267 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009268 case CMD_echomsg:
9269 case CMD_echoerr:
9270 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009271 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009272
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009273 case CMD_put:
9274 ea.cmd = cmd;
9275 line = compile_put(p, &ea, &cctx);
9276 break;
9277
Bram Moolenaar4c137212021-04-19 16:48:48 +02009278 case CMD_substitute:
9279 if (cctx.ctx_skip == SKIP_YES)
9280 line = (char_u *)"";
9281 else
9282 {
9283 ea.arg = p;
9284 line = compile_substitute(line, &ea, &cctx);
9285 }
9286 break;
9287
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009288 case CMD_redir:
9289 ea.arg = p;
9290 line = compile_redir(line, &ea, &cctx);
9291 break;
9292
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009293 case CMD_cexpr:
9294 case CMD_lexpr:
9295 case CMD_caddexpr:
9296 case CMD_laddexpr:
9297 case CMD_cgetexpr:
9298 case CMD_lgetexpr:
9299 ea.arg = p;
9300 line = compile_cexpr(line, &ea, &cctx);
9301 break;
9302
Bram Moolenaar3988f642020-08-27 22:43:03 +02009303 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009304
Bram Moolenaarae616492020-07-28 20:07:27 +02009305 case CMD_append:
9306 case CMD_change:
9307 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009308 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009309 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009310 case CMD_xit:
9311 not_in_vim9(&ea);
9312 goto erret;
9313
Bram Moolenaar002262f2020-07-08 17:47:57 +02009314 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009315 if (cctx.ctx_skip != SKIP_YES)
9316 {
9317 semsg(_(e_invalid_command_str), ea.cmd);
9318 goto erret;
9319 }
9320 // We don't check for a next command here.
9321 line = (char_u *)"";
9322 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009324 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009325 if (cctx.ctx_skip == SKIP_YES)
9326 {
9327 // We don't check for a next command here.
9328 line = (char_u *)"";
9329 }
9330 else
9331 {
9332 // Not recognized, execute with do_cmdline_cmd().
9333 ea.arg = p;
9334 line = compile_exec(line, &ea, &cctx);
9335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009336 break;
9337 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009338nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009339 if (line == NULL)
9340 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009341 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009342
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009343 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009344 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009346 if (cctx.ctx_type_stack.ga_len < 0)
9347 {
9348 iemsg("Type stack underflow");
9349 goto erret;
9350 }
9351 }
9352
9353 if (cctx.ctx_scope != NULL)
9354 {
9355 if (cctx.ctx_scope->se_type == IF_SCOPE)
9356 emsg(_(e_endif));
9357 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9358 emsg(_(e_endwhile));
9359 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9360 emsg(_(e_endfor));
9361 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009362 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009363 goto erret;
9364 }
9365
Bram Moolenaarefd88552020-06-18 20:50:10 +02009366 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009367 {
9368 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
9369 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009370 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009371 goto erret;
9372 }
9373
9374 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01009375 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009376 }
9377
Bram Moolenaar599410c2021-04-10 14:03:43 +02009378 // When compiled with ":silent!" and there was an error don't consider the
9379 // function compiled.
9380 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009381 {
9382 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9383 + ufunc->uf_dfunc_idx;
9384 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009385 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009386#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009387 if (cctx.ctx_profiling)
9388 {
9389 dfunc->df_instr_prof = instr->ga_data;
9390 dfunc->df_instr_prof_count = instr->ga_len;
9391 }
9392 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009393#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009394 {
9395 dfunc->df_instr = instr->ga_data;
9396 dfunc->df_instr_count = instr->ga_len;
9397 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009398 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009399 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009400 if (cctx.ctx_outer_used)
9401 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009402 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009403 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009404
9405 ret = OK;
9406
9407erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009408 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009409 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009410 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9411 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009412
Bram Moolenaar8238f082021-04-20 21:10:48 +02009413 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009414 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009415
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009416 // If using the last entry in the table and it was added above, we
9417 // might as well remove it.
9418 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009419 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009420 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009421 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009422 ufunc->uf_dfunc_idx = 0;
9423 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009424 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009425
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009426 while (cctx.ctx_scope != NULL)
9427 drop_scope(&cctx);
9428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009429 if (errormsg != NULL)
9430 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009431 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009432 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009433 }
9434
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009435 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9436 {
9437 if (ret == OK)
9438 {
9439 emsg(_(e_missing_redir_end));
9440 ret = FAIL;
9441 }
9442 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009443 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009444 }
9445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009446 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009447 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009448 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009449 if (do_estack_push)
9450 estack_pop();
9451
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009452 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009453 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009454 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009455 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009456 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009457}
9458
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009459 void
9460set_function_type(ufunc_T *ufunc)
9461{
9462 int varargs = ufunc->uf_va_name != NULL;
9463 int argcount = ufunc->uf_args.ga_len;
9464
9465 // Create a type for the function, with the return type and any
9466 // argument types.
9467 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9468 // The type is included in "tt_args".
9469 if (argcount > 0 || varargs)
9470 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009471 if (ufunc->uf_type_list.ga_itemsize == 0)
9472 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009473 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9474 argcount, &ufunc->uf_type_list);
9475 // Add argument types to the function type.
9476 if (func_type_add_arg_types(ufunc->uf_func_type,
9477 argcount + varargs,
9478 &ufunc->uf_type_list) == FAIL)
9479 return;
9480 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9481 ufunc->uf_func_type->tt_min_argcount =
9482 argcount - ufunc->uf_def_args.ga_len;
9483 if (ufunc->uf_arg_types == NULL)
9484 {
9485 int i;
9486
9487 // lambda does not have argument types.
9488 for (i = 0; i < argcount; ++i)
9489 ufunc->uf_func_type->tt_args[i] = &t_any;
9490 }
9491 else
9492 mch_memmove(ufunc->uf_func_type->tt_args,
9493 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9494 if (varargs)
9495 {
9496 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009497 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009498 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9499 }
9500 }
9501 else
9502 // No arguments, can use a predefined type.
9503 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9504 argcount, &ufunc->uf_type_list);
9505}
9506
9507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009508/*
9509 * Delete an instruction, free what it contains.
9510 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009511 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009512delete_instr(isn_T *isn)
9513{
9514 switch (isn->isn_type)
9515 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009516 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009517 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009518 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009519 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009520 case ISN_LOADENV:
9521 case ISN_LOADG:
9522 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009523 case ISN_LOADT:
9524 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009525 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009526 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009527 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009528 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009529 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009530 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009531 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009532 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009533 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009534 case ISN_STOREW:
9535 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009536 vim_free(isn->isn_arg.string);
9537 break;
9538
Bram Moolenaar4c137212021-04-19 16:48:48 +02009539 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009540 {
9541 int idx;
9542 isn_T *list = isn->isn_arg.subs.subs_instr;
9543
9544 vim_free(isn->isn_arg.subs.subs_cmd);
9545 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9546 delete_instr(list + idx);
9547 vim_free(list);
9548 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009549 break;
9550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009551 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009552 case ISN_STORES:
9553 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009554 break;
9555
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009556 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009557 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009558 vim_free(isn->isn_arg.unlet.ul_name);
9559 break;
9560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009561 case ISN_STOREOPT:
9562 vim_free(isn->isn_arg.storeopt.so_name);
9563 break;
9564
9565 case ISN_PUSHBLOB: // push blob isn_arg.blob
9566 blob_unref(isn->isn_arg.blob);
9567 break;
9568
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009569 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009570#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009571 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009572#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009573 break;
9574
9575 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009576#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009577 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009578#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009579 break;
9580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009581 case ISN_UCALL:
9582 vim_free(isn->isn_arg.ufunc.cuf_name);
9583 break;
9584
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009585 case ISN_FUNCREF:
9586 {
9587 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9588 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009589 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009590
Bram Moolenaar077a4232020-12-22 18:33:27 +01009591 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9592 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009593 }
9594 break;
9595
9596 case ISN_DCALL:
9597 {
9598 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9599 + isn->isn_arg.dfunc.cdf_idx;
9600
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009601 if (dfunc->df_ufunc != NULL
9602 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009603 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009604 }
9605 break;
9606
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009607 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009608 {
9609 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9610 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9611
9612 if (ufunc != NULL)
9613 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009614 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009615 func_ptr_unref(ufunc);
9616 }
9617
9618 vim_free(lambda);
9619 vim_free(isn->isn_arg.newfunc.nf_global);
9620 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009621 break;
9622
Bram Moolenaar5e654232020-09-16 15:22:00 +02009623 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009624 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009625 free_type(isn->isn_arg.type.ct_type);
9626 break;
9627
Bram Moolenaar02194d22020-10-24 23:08:38 +02009628 case ISN_CMDMOD:
9629 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9630 ->cmod_filter_regmatch.regprog);
9631 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9632 break;
9633
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009634 case ISN_LOADSCRIPT:
9635 case ISN_STORESCRIPT:
9636 vim_free(isn->isn_arg.script.scriptref);
9637 break;
9638
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009639 case ISN_TRY:
9640 vim_free(isn->isn_arg.try.try_ref);
9641 break;
9642
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009643 case ISN_CEXPR_CORE:
9644 vim_free(isn->isn_arg.cexpr.cexpr_ref);
9645 break;
9646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009647 case ISN_2BOOL:
9648 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009649 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009650 case ISN_ADDBLOB:
9651 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009652 case ISN_ANYINDEX:
9653 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009654 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009655 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009656 case ISN_BLOBINDEX:
9657 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009658 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009659 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009660 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009661 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009662 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009663 case ISN_COMPAREANY:
9664 case ISN_COMPAREBLOB:
9665 case ISN_COMPAREBOOL:
9666 case ISN_COMPAREDICT:
9667 case ISN_COMPAREFLOAT:
9668 case ISN_COMPAREFUNC:
9669 case ISN_COMPARELIST:
9670 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009671 case ISN_COMPARESPECIAL:
9672 case ISN_COMPARESTRING:
9673 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009674 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009675 case ISN_DROP:
9676 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009677 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009678 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009679 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009680 case ISN_EXECCONCAT:
9681 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009682 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009683 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009684 case ISN_GETITEM:
9685 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009686 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009687 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009688 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009689 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009690 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009691 case ISN_LOADBDICT:
9692 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009693 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009694 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009695 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009696 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009697 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009698 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009699 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009700 case ISN_NEGATENR:
9701 case ISN_NEWDICT:
9702 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009703 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009704 case ISN_OPFLOAT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009705 case ISN_FINISH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009706 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009707 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009708 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009709 case ISN_PROF_END:
9710 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009711 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009712 case ISN_PUSHF:
9713 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009714 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009715 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009716 case ISN_REDIREND:
9717 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009718 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009719 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009720 case ISN_SHUFFLE:
9721 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009722 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009723 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009724 case ISN_STORENR:
9725 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009726 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009727 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009728 case ISN_STOREV:
9729 case ISN_STRINDEX:
9730 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009731 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009732 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009733 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009734 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009735 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009736 // nothing allocated
9737 break;
9738 }
9739}
9740
9741/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009742 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009743 */
9744 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009745delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009746{
9747 int idx;
9748
9749 ga_clear(&dfunc->df_def_args_isn);
9750
9751 if (dfunc->df_instr != NULL)
9752 {
9753 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9754 delete_instr(dfunc->df_instr + idx);
9755 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009756 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009757 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009758#ifdef FEAT_PROFILE
9759 if (dfunc->df_instr_prof != NULL)
9760 {
9761 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9762 delete_instr(dfunc->df_instr_prof + idx);
9763 VIM_CLEAR(dfunc->df_instr_prof);
9764 dfunc->df_instr_prof = NULL;
9765 }
9766#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009767
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009768 if (mark_deleted)
9769 dfunc->df_deleted = TRUE;
9770 if (dfunc->df_ufunc != NULL)
9771 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009772}
9773
9774/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009775 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009776 * function, unless another user function still uses it.
9777 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009778 */
9779 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009780unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009781{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009782 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009783 {
9784 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9785 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009786
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009787 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009788 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009789 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009790 ufunc->uf_dfunc_idx = 0;
9791 if (dfunc->df_ufunc == ufunc)
9792 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009793 }
9794}
9795
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009796/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009797 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009798 */
9799 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009800link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009801{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009802 if (ufunc->uf_dfunc_idx > 0)
9803 {
9804 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9805 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009806
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009807 ++dfunc->df_refcount;
9808 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009809}
9810
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009811#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009812/*
9813 * Free all functions defined with ":def".
9814 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009815 void
9816free_def_functions(void)
9817{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009818 int idx;
9819
9820 for (idx = 0; idx < def_functions.ga_len; ++idx)
9821 {
9822 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9823
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009824 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009825 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009826 }
9827
9828 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009829}
9830#endif
9831
9832
9833#endif // FEAT_EVAL