blob: 7c0190677f2a1c85e540c1391d99ba18e2dd7d2e [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 Moolenaare99d4222021-06-13 14:01:26 +0200177 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200180 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200182 int ctx_has_closure; // set to one if a closures was created in
183 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100185 garray_T ctx_imports; // imported items
186
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200187 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200189 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200191 cctx_T *ctx_outer; // outer scope for lambda or nested
192 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200193 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200196 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200197
Bram Moolenaar02194d22020-10-24 23:08:38 +0200198 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200199
200 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
201 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202};
203
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100204static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205
206/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100207 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100208 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100209 * If "lvar" is NULL only check if the variable can be found.
210 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100212 static int
213lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214{
215 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100216 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100218 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100219 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200220
221 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
223 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100224 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
225 if (STRNCMP(name, lvp->lv_name, len) == 0
226 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200227 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100228 if (lvar != NULL)
229 {
230 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100231 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100232 }
233 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200236
237 // Find local in outer function scope.
238 if (cctx->ctx_outer != NULL)
239 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100240 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100242 if (lvar != NULL)
243 {
244 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100245 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100246 }
247 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200248 }
249 }
250
Bram Moolenaar709664c2020-12-12 14:33:41 +0100251 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252}
253
254/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200255 * Lookup an argument in the current function and an enclosing function.
256 * Returns the argument index in "idxp"
257 * Returns the argument type in "type"
258 * Sets "gen_load_outer" to TRUE if found in outer scope.
259 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260 */
261 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200262arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200263 char_u *name,
264 size_t len,
265 int *idxp,
266 type_T **type,
267 int *gen_load_outer,
268 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269{
270 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200271 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100273 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200274 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
276 {
277 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
278
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200279 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
280 {
281 if (idxp != NULL)
282 {
283 // Arguments are located above the frame pointer. One further
284 // if there is a vararg argument
285 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
286 + STACK_FRAME_SIZE)
287 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
288
289 if (cctx->ctx_ufunc->uf_arg_types != NULL)
290 *type = cctx->ctx_ufunc->uf_arg_types[idx];
291 else
292 *type = &t_any;
293 }
294 return OK;
295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200298 va_name = cctx->ctx_ufunc->uf_va_name;
299 if (va_name != NULL
300 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
301 {
302 if (idxp != NULL)
303 {
304 // varargs is always the last argument
305 *idxp = -STACK_FRAME_SIZE - 1;
306 *type = cctx->ctx_ufunc->uf_va_type;
307 }
308 return OK;
309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100310
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200311 if (cctx->ctx_outer != NULL)
312 {
313 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200314 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200315 == OK)
316 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100317 if (gen_load_outer != NULL)
318 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200319 return OK;
320 }
321 }
322
323 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100324}
325
326/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200327 * Lookup a script-local variable in the current script, possibly defined in a
328 * block that contains the function "cctx->ctx_ufunc".
329 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100330 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331 * Return NULL when not found.
332 */
333 static sallvar_T *
334find_script_var(char_u *name, size_t len, cctx_T *cctx)
335{
336 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
337 hashitem_T *hi;
338 int cc;
339 sallvar_T *sav;
340 ufunc_T *ufunc;
341
342 // Find the list of all script variables with the right name.
343 if (len > 0)
344 {
345 cc = name[len];
346 name[len] = NUL;
347 }
348 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
349 if (len > 0)
350 name[len] = cc;
351 if (HASHITEM_EMPTY(hi))
352 return NULL;
353
354 sav = HI2SAV(hi);
355 if (sav->sav_block_id == 0 || cctx == NULL)
356 // variable defined in the script scope or not in a function.
357 return sav;
358
359 // Go over the variables with this name and find one that was visible
360 // from the function.
361 ufunc = cctx->ctx_ufunc;
362 while (sav != NULL)
363 {
364 int idx;
365
366 // Go over the blocks that this function was defined in. If the
367 // variable block ID matches it was visible to the function.
368 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
369 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
370 return sav;
371 sav = sav->sav_next;
372 }
373
374 return NULL;
375}
376
377/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100378 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200379 */
380 static int
381script_is_vim9()
382{
383 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
384}
385
386/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200388 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389 * Returns OK or FAIL.
390 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200391 static int
392script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200394 if (current_sctx.sc_sid <= 0)
395 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200396 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200397 {
398 // Check script variables that were visible where the function was
399 // defined.
400 if (find_script_var(name, len, cctx) != NULL)
401 return OK;
402 }
403 else
404 {
405 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
406 dictitem_T *di;
407 int cc;
408
409 // Check script variables that are currently visible
410 cc = name[len];
411 name[len] = NUL;
412 di = find_var_in_ht(ht, 0, name, TRUE);
413 name[len] = cc;
414 if (di != NULL)
415 return OK;
416 }
417
418 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419}
420
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100421/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100422 * Return TRUE if "name" is a local variable, argument, script variable or
423 * imported.
424 */
425 static int
426variable_exists(char_u *name, size_t len, cctx_T *cctx)
427{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100428 return (cctx != NULL
429 && (lookup_local(name, len, NULL, cctx) == OK
430 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200431 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100432 || find_imported(name, len, cctx) != NULL;
433}
434
435/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100436 * Return TRUE if "name" is a local variable, argument, script variable,
437 * imported or function.
438 */
439 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100440item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100441{
442 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100443 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100444
445 if (variable_exists(name, len, cctx))
446 return TRUE;
447
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100448 // This is similar to what is in lookup_scriptitem():
449 // Find a function, so that a following "->" works.
450 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
451 // a function call.
452 p = skipwhite(name + len);
453
454 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
455 {
456 // Do not check for an internal function, since it might also be a
457 // valid command, such as ":split" versuse "split()".
458 // Skip "g:" before a function name.
459 is_global = (name[0] == 'g' && name[1] == ':');
460 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
461 }
462 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100463}
464
465/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100466 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200467 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200468 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100469 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100470 * Return FAIL and give an error if it defined.
471 */
472 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100473check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100474{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200475 int c = p[len];
476 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200477
Bram Moolenaarda479c72021-04-10 21:01:38 +0200478 // underscore argument is OK
479 if (len == 1 && *p == '_')
480 return OK;
481
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200482 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100483 {
484 if (is_arg)
485 semsg(_(e_argument_already_declared_in_script_str), p);
486 else
487 semsg(_(e_variable_already_declared_in_script_str), p);
488 return FAIL;
489 }
490
Bram Moolenaarad486a02020-08-01 23:22:18 +0200491 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100492 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100493 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200494 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200495 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200496 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100497 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200498 // A local or script-local function can shadow a global function.
499 if (ufunc == NULL || !func_is_global(ufunc)
500 || (p[0] == 'g' && p[1] == ':'))
501 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100502 if (is_arg)
503 semsg(_(e_argument_name_shadows_existing_variable_str), p);
504 else
505 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200506 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200507 return FAIL;
508 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100509 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200510 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100511 return OK;
512}
513
Bram Moolenaar65b95452020-07-19 14:03:09 +0200514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515/////////////////////////////////////////////////////////////////////
516// Following generate_ functions expect the caller to call ga_grow().
517
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200518#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
519#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521/*
522 * Generate an instruction without arguments.
523 * Returns a pointer to the new instruction, NULL if failed.
524 */
525 static isn_T *
526generate_instr(cctx_T *cctx, isntype_T isn_type)
527{
528 garray_T *instr = &cctx->ctx_instr;
529 isn_T *isn;
530
Bram Moolenaar080457c2020-03-03 21:53:32 +0100531 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 if (ga_grow(instr, 1) == FAIL)
533 return NULL;
534 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
535 isn->isn_type = isn_type;
536 isn->isn_lnum = cctx->ctx_lnum + 1;
537 ++instr->ga_len;
538
539 return isn;
540}
541
542/*
543 * Generate an instruction without arguments.
544 * "drop" will be removed from the stack.
545 * Returns a pointer to the new instruction, NULL if failed.
546 */
547 static isn_T *
548generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
549{
550 garray_T *stack = &cctx->ctx_type_stack;
551
Bram Moolenaar080457c2020-03-03 21:53:32 +0100552 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 stack->ga_len -= drop;
554 return generate_instr(cctx, isn_type);
555}
556
557/*
558 * Generate instruction "isn_type" and put "type" on the type stack.
559 */
560 static isn_T *
561generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
562{
563 isn_T *isn;
564 garray_T *stack = &cctx->ctx_type_stack;
565
566 if ((isn = generate_instr(cctx, isn_type)) == NULL)
567 return NULL;
568
569 if (ga_grow(stack, 1) == FAIL)
570 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200571 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 ++stack->ga_len;
573
574 return isn;
575}
576
577/*
578 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200579 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200580 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 */
582 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200583may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584{
585 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200586 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100588 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100590 RETURN_OK_IF_SKIP(cctx);
591 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200592 switch ((*type)->tt_type)
593 {
594 // nothing to be done
595 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200597 // conversion possible
598 case VAR_SPECIAL:
599 case VAR_BOOL:
600 case VAR_NUMBER:
601 case VAR_FLOAT:
602 break;
603
604 // conversion possible (with runtime check)
605 case VAR_ANY:
606 case VAR_UNKNOWN:
607 isntype = ISN_2STRING_ANY;
608 break;
609
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200610 // conversion possible when tolerant
611 case VAR_LIST:
612 if (tolerant)
613 {
614 isntype = ISN_2STRING_ANY;
615 break;
616 }
617 // FALLTHROUGH
618
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200619 // conversion not possible
620 case VAR_VOID:
621 case VAR_BLOB:
622 case VAR_FUNC:
623 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200624 case VAR_DICT:
625 case VAR_JOB:
626 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200627 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200628 to_string_error((*type)->tt_type);
629 return FAIL;
630 }
631
632 *type = &t_string;
633 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200635 isn->isn_arg.tostring.offset = offset;
636 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637
638 return OK;
639}
640
641 static int
642check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
643{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200644 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200646 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647 {
648 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200649 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200651 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 return FAIL;
653 }
654 return OK;
655}
656
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200657 static int
658generate_add_instr(
659 cctx_T *cctx,
660 vartype_T vartype,
661 type_T *type1,
662 type_T *type2)
663{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100664 garray_T *stack = &cctx->ctx_type_stack;
665 isn_T *isn = generate_instr_drop(cctx,
666 vartype == VAR_NUMBER ? ISN_OPNR
667 : vartype == VAR_LIST ? ISN_ADDLIST
668 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200669#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100670 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200671#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100672 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200673
674 if (vartype != VAR_LIST && vartype != VAR_BLOB
675 && type1->tt_type != VAR_ANY
676 && type2->tt_type != VAR_ANY
677 && check_number_or_float(
678 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
679 return FAIL;
680
681 if (isn != NULL)
682 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100683
684 // When concatenating two lists with different member types the member type
685 // becomes "any".
686 if (vartype == VAR_LIST
687 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
688 && type1->tt_member != type2->tt_member)
689 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
690
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200691 return isn == NULL ? FAIL : OK;
692}
693
694/*
695 * Get the type to use for an instruction for an operation on "type1" and
696 * "type2". If they are matching use a type-specific instruction. Otherwise
697 * fall back to runtime type checking.
698 */
699 static vartype_T
700operator_type(type_T *type1, type_T *type2)
701{
702 if (type1->tt_type == type2->tt_type
703 && (type1->tt_type == VAR_NUMBER
704 || type1->tt_type == VAR_LIST
705#ifdef FEAT_FLOAT
706 || type1->tt_type == VAR_FLOAT
707#endif
708 || type1->tt_type == VAR_BLOB))
709 return type1->tt_type;
710 return VAR_ANY;
711}
712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713/*
714 * Generate an instruction with two arguments. The instruction depends on the
715 * type of the arguments.
716 */
717 static int
718generate_two_op(cctx_T *cctx, char_u *op)
719{
720 garray_T *stack = &cctx->ctx_type_stack;
721 type_T *type1;
722 type_T *type2;
723 vartype_T vartype;
724 isn_T *isn;
725
Bram Moolenaar080457c2020-03-03 21:53:32 +0100726 RETURN_OK_IF_SKIP(cctx);
727
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200728 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
730 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200731 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732
733 switch (*op)
734 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200735 case '+':
736 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 break;
739
740 case '-':
741 case '*':
742 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
743 op) == FAIL)
744 return FAIL;
745 if (vartype == VAR_NUMBER)
746 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
747#ifdef FEAT_FLOAT
748 else if (vartype == VAR_FLOAT)
749 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
750#endif
751 else
752 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
753 if (isn != NULL)
754 isn->isn_arg.op.op_type = *op == '*'
755 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
756 break;
757
Bram Moolenaar4c683752020-04-05 21:38:23 +0200758 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200760 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 && type2->tt_type != VAR_NUMBER))
762 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200763 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 return FAIL;
765 }
766 isn = generate_instr_drop(cctx,
767 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
768 if (isn != NULL)
769 isn->isn_arg.op.op_type = EXPR_REM;
770 break;
771 }
772
773 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200774 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 {
776 type_T *type = &t_any;
777
778#ifdef FEAT_FLOAT
779 // float+number and number+float results in float
780 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
781 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
782 type = &t_float;
783#endif
784 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
785 }
786
787 return OK;
788}
789
790/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200791 * Get the instruction to use for comparing "type1" with "type2"
792 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200794 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100795get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796{
797 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798
Bram Moolenaar4c683752020-04-05 21:38:23 +0200799 if (type1 == VAR_UNKNOWN)
800 type1 = VAR_ANY;
801 if (type2 == VAR_UNKNOWN)
802 type2 = VAR_ANY;
803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 if (type1 == type2)
805 {
806 switch (type1)
807 {
808 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
809 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
810 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
811 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
812 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
813 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
814 case VAR_LIST: isntype = ISN_COMPARELIST; break;
815 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
816 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 default: isntype = ISN_COMPAREANY; break;
818 }
819 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200820 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100822 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 isntype = ISN_COMPAREANY;
824
Bram Moolenaar657137c2021-01-09 15:45:23 +0100825 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 && (isntype == ISN_COMPAREBOOL
827 || isntype == ISN_COMPARESPECIAL
828 || isntype == ISN_COMPARENR
829 || isntype == ISN_COMPAREFLOAT))
830 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200831 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100832 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200833 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834 }
835 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100836 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
838 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100839 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
840 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 && (type1 == VAR_BLOB || type2 == VAR_BLOB
842 || type1 == VAR_LIST || type2 == VAR_LIST))))
843 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200844 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200846 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200848 return isntype;
849}
850
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200851 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100852check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200853{
854 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
855 return FAIL;
856 return OK;
857}
858
Bram Moolenaara5565e42020-05-09 15:44:01 +0200859/*
860 * Generate an ISN_COMPARE* instruction with a boolean result.
861 */
862 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100863generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200864{
865 isntype_T isntype;
866 isn_T *isn;
867 garray_T *stack = &cctx->ctx_type_stack;
868 vartype_T type1;
869 vartype_T type2;
870
871 RETURN_OK_IF_SKIP(cctx);
872
873 // Get the known type of the two items on the stack. If they are matching
874 // use a type-specific instruction. Otherwise fall back to runtime type
875 // checking.
876 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
877 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100878 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200879 if (isntype == ISN_DROP)
880 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881
882 if ((isn = generate_instr(cctx, isntype)) == NULL)
883 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100884 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 isn->isn_arg.op.op_ic = ic;
886
887 // takes two arguments, puts one bool back
888 if (stack->ga_len >= 2)
889 {
890 --stack->ga_len;
891 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
892 }
893
894 return OK;
895}
896
897/*
898 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200899 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 */
901 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200902generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903{
904 isn_T *isn;
905 garray_T *stack = &cctx->ctx_type_stack;
906
Bram Moolenaar080457c2020-03-03 21:53:32 +0100907 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
909 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200910 isn->isn_arg.tobool.invert = invert;
911 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912
913 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200914 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915
916 return OK;
917}
918
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200919/*
920 * Generate an ISN_COND2BOOL instruction.
921 */
922 static int
923generate_COND2BOOL(cctx_T *cctx)
924{
925 isn_T *isn;
926 garray_T *stack = &cctx->ctx_type_stack;
927
928 RETURN_OK_IF_SKIP(cctx);
929 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
930 return FAIL;
931
932 // type becomes bool
933 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
934
935 return OK;
936}
937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200939generate_TYPECHECK(
940 cctx_T *cctx,
941 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100942 int offset,
943 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944{
945 isn_T *isn;
946 garray_T *stack = &cctx->ctx_type_stack;
947
Bram Moolenaar080457c2020-03-03 21:53:32 +0100948 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
950 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200951 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100952 isn->isn_arg.type.ct_off = (int8_T)offset;
953 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954
Bram Moolenaar5e654232020-09-16 15:22:00 +0200955 // type becomes expected
956 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957
958 return OK;
959}
960
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100961 static int
962generate_SETTYPE(
963 cctx_T *cctx,
964 type_T *expected)
965{
966 isn_T *isn;
967
968 RETURN_OK_IF_SKIP(cctx);
969 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
970 return FAIL;
971 isn->isn_arg.type.ct_type = alloc_type(expected);
972 return OK;
973}
974
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200976 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
977 * used. Return FALSE if the types will never match.
978 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100979 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200980use_typecheck(type_T *actual, type_T *expected)
981{
982 if (actual->tt_type == VAR_ANY
983 || actual->tt_type == VAR_UNKNOWN
984 || (actual->tt_type == VAR_FUNC
985 && (expected->tt_type == VAR_FUNC
986 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100987 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
988 && ((actual->tt_member == &t_void)
989 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200990 return TRUE;
991 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
992 && actual->tt_type == expected->tt_type)
993 // This takes care of a nested list or dict.
994 return use_typecheck(actual->tt_member, expected->tt_member);
995 return FALSE;
996}
997
998/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200999 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001000 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001001 * - "actual" is a type that can be "expected" type: add a runtime check; or
1002 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001003 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1004 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001005 */
Bram Moolenaar351ead02021-01-16 16:07:01 +01001006 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001007need_type(
1008 type_T *actual,
1009 type_T *expected,
1010 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001011 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001012 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001013 int silent,
1014 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001015{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001016 where_T where;
1017
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001018 if (expected == &t_bool && actual != &t_bool
1019 && (actual->tt_flags & TTFLAG_BOOL_OK))
1020 {
1021 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1022 // boolean is OK but requires a conversion.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001023 generate_2BOOL(cctx, FALSE, offset);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001024 return OK;
1025 }
1026
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001027 where.wt_index = arg_idx;
1028 where.wt_variable = FALSE;
1029 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001030 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001031
1032 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001033 // If it's a constant a runtime check makes no sense.
1034 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001035 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001036 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001037 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001038 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001039
1040 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001041 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001042 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001043}
1044
1045/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001046 * Check that the top of the type stack has a type that can be used as a
1047 * condition. Give an error and return FAIL if not.
1048 */
1049 static int
1050bool_on_stack(cctx_T *cctx)
1051{
1052 garray_T *stack = &cctx->ctx_type_stack;
1053 type_T *type;
1054
1055 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1056 if (type == &t_bool)
1057 return OK;
1058
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001059 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001060 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1061 // This requires a runtime type check.
1062 return generate_COND2BOOL(cctx);
1063
Bram Moolenaar351ead02021-01-16 16:07:01 +01001064 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001065}
1066
1067/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 * Generate an ISN_PUSHNR instruction.
1069 */
1070 static int
1071generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1072{
1073 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001074 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075
Bram Moolenaar080457c2020-03-03 21:53:32 +01001076 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1078 return FAIL;
1079 isn->isn_arg.number = number;
1080
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001081 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001082 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001083 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 return OK;
1085}
1086
1087/*
1088 * Generate an ISN_PUSHBOOL instruction.
1089 */
1090 static int
1091generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1092{
1093 isn_T *isn;
1094
Bram Moolenaar080457c2020-03-03 21:53:32 +01001095 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1097 return FAIL;
1098 isn->isn_arg.number = number;
1099
1100 return OK;
1101}
1102
1103/*
1104 * Generate an ISN_PUSHSPEC instruction.
1105 */
1106 static int
1107generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1108{
1109 isn_T *isn;
1110
Bram Moolenaar080457c2020-03-03 21:53:32 +01001111 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1113 return FAIL;
1114 isn->isn_arg.number = number;
1115
1116 return OK;
1117}
1118
1119#ifdef FEAT_FLOAT
1120/*
1121 * Generate an ISN_PUSHF instruction.
1122 */
1123 static int
1124generate_PUSHF(cctx_T *cctx, float_T fnumber)
1125{
1126 isn_T *isn;
1127
Bram Moolenaar080457c2020-03-03 21:53:32 +01001128 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1130 return FAIL;
1131 isn->isn_arg.fnumber = fnumber;
1132
1133 return OK;
1134}
1135#endif
1136
1137/*
1138 * Generate an ISN_PUSHS instruction.
1139 * Consumes "str".
1140 */
1141 static int
1142generate_PUSHS(cctx_T *cctx, char_u *str)
1143{
1144 isn_T *isn;
1145
Bram Moolenaar508b5612021-01-02 18:17:26 +01001146 if (cctx->ctx_skip == SKIP_YES)
1147 {
1148 vim_free(str);
1149 return OK;
1150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1152 return FAIL;
1153 isn->isn_arg.string = str;
1154
1155 return OK;
1156}
1157
1158/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001159 * Generate an ISN_PUSHCHANNEL instruction.
1160 * Consumes "channel".
1161 */
1162 static int
1163generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1164{
1165 isn_T *isn;
1166
Bram Moolenaar080457c2020-03-03 21:53:32 +01001167 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001168 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1169 return FAIL;
1170 isn->isn_arg.channel = channel;
1171
1172 return OK;
1173}
1174
1175/*
1176 * Generate an ISN_PUSHJOB instruction.
1177 * Consumes "job".
1178 */
1179 static int
1180generate_PUSHJOB(cctx_T *cctx, job_T *job)
1181{
1182 isn_T *isn;
1183
Bram Moolenaar080457c2020-03-03 21:53:32 +01001184 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001185 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001186 return FAIL;
1187 isn->isn_arg.job = job;
1188
1189 return OK;
1190}
1191
1192/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 * Generate an ISN_PUSHBLOB instruction.
1194 * Consumes "blob".
1195 */
1196 static int
1197generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1198{
1199 isn_T *isn;
1200
Bram Moolenaar080457c2020-03-03 21:53:32 +01001201 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1203 return FAIL;
1204 isn->isn_arg.blob = blob;
1205
1206 return OK;
1207}
1208
1209/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001210 * Generate an ISN_PUSHFUNC instruction with name "name".
1211 * Consumes "name".
1212 */
1213 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001214generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001215{
1216 isn_T *isn;
1217
Bram Moolenaar080457c2020-03-03 21:53:32 +01001218 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001219 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001220 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001221 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001222
1223 return OK;
1224}
1225
1226/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001227 * Generate an ISN_GETITEM instruction with "index".
1228 */
1229 static int
1230generate_GETITEM(cctx_T *cctx, int index)
1231{
1232 isn_T *isn;
1233 garray_T *stack = &cctx->ctx_type_stack;
1234 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1235 type_T *item_type = &t_any;
1236
1237 RETURN_OK_IF_SKIP(cctx);
1238
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001239 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001240 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001241 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001242 emsg(_(e_listreq));
1243 return FAIL;
1244 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001245 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001246 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1247 return FAIL;
1248 isn->isn_arg.number = index;
1249
1250 // add the item type to the type stack
1251 if (ga_grow(stack, 1) == FAIL)
1252 return FAIL;
1253 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1254 ++stack->ga_len;
1255 return OK;
1256}
1257
1258/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001259 * Generate an ISN_SLICE instruction with "count".
1260 */
1261 static int
1262generate_SLICE(cctx_T *cctx, int count)
1263{
1264 isn_T *isn;
1265
1266 RETURN_OK_IF_SKIP(cctx);
1267 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1268 return FAIL;
1269 isn->isn_arg.number = count;
1270 return OK;
1271}
1272
1273/*
1274 * Generate an ISN_CHECKLEN instruction with "min_len".
1275 */
1276 static int
1277generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1278{
1279 isn_T *isn;
1280
1281 RETURN_OK_IF_SKIP(cctx);
1282
1283 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1284 return FAIL;
1285 isn->isn_arg.checklen.cl_min_len = min_len;
1286 isn->isn_arg.checklen.cl_more_OK = more_OK;
1287
1288 return OK;
1289}
1290
1291/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 * Generate an ISN_STORE instruction.
1293 */
1294 static int
1295generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1296{
1297 isn_T *isn;
1298
Bram Moolenaar080457c2020-03-03 21:53:32 +01001299 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1301 return FAIL;
1302 if (name != NULL)
1303 isn->isn_arg.string = vim_strsave(name);
1304 else
1305 isn->isn_arg.number = idx;
1306
1307 return OK;
1308}
1309
1310/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001311 * Generate an ISN_STOREOUTER instruction.
1312 */
1313 static int
1314generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1315{
1316 isn_T *isn;
1317
1318 RETURN_OK_IF_SKIP(cctx);
1319 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1320 return FAIL;
1321 isn->isn_arg.outer.outer_idx = idx;
1322 isn->isn_arg.outer.outer_depth = level;
1323
1324 return OK;
1325}
1326
1327/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1329 */
1330 static int
1331generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1332{
1333 isn_T *isn;
1334
Bram Moolenaar080457c2020-03-03 21:53:32 +01001335 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1337 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001338 isn->isn_arg.storenr.stnr_idx = idx;
1339 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340
1341 return OK;
1342}
1343
1344/*
1345 * Generate an ISN_STOREOPT instruction
1346 */
1347 static int
1348generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1349{
1350 isn_T *isn;
1351
Bram Moolenaar080457c2020-03-03 21:53:32 +01001352 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001353 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 return FAIL;
1355 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1356 isn->isn_arg.storeopt.so_flags = opt_flags;
1357
1358 return OK;
1359}
1360
1361/*
1362 * Generate an ISN_LOAD or similar instruction.
1363 */
1364 static int
1365generate_LOAD(
1366 cctx_T *cctx,
1367 isntype_T isn_type,
1368 int idx,
1369 char_u *name,
1370 type_T *type)
1371{
1372 isn_T *isn;
1373
Bram Moolenaar080457c2020-03-03 21:53:32 +01001374 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1376 return FAIL;
1377 if (name != NULL)
1378 isn->isn_arg.string = vim_strsave(name);
1379 else
1380 isn->isn_arg.number = idx;
1381
1382 return OK;
1383}
1384
1385/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001386 * Generate an ISN_LOADOUTER instruction
1387 */
1388 static int
1389generate_LOADOUTER(
1390 cctx_T *cctx,
1391 int idx,
1392 int nesting,
1393 type_T *type)
1394{
1395 isn_T *isn;
1396
1397 RETURN_OK_IF_SKIP(cctx);
1398 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1399 return FAIL;
1400 isn->isn_arg.outer.outer_idx = idx;
1401 isn->isn_arg.outer.outer_depth = nesting;
1402
1403 return OK;
1404}
1405
1406/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001407 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001408 */
1409 static int
1410generate_LOADV(
1411 cctx_T *cctx,
1412 char_u *name,
1413 int error)
1414{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001415 int di_flags;
1416 int vidx = find_vim_var(name, &di_flags);
1417 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001418
Bram Moolenaar080457c2020-03-03 21:53:32 +01001419 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001420 if (vidx < 0)
1421 {
1422 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001423 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001424 return FAIL;
1425 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001426 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001427
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001428 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001429}
1430
1431/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001432 * Generate an ISN_UNLET instruction.
1433 */
1434 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001435generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001436{
1437 isn_T *isn;
1438
1439 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001440 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001441 return FAIL;
1442 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1443 isn->isn_arg.unlet.ul_forceit = forceit;
1444
1445 return OK;
1446}
1447
1448/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001449 * Generate an ISN_LOCKCONST instruction.
1450 */
1451 static int
1452generate_LOCKCONST(cctx_T *cctx)
1453{
1454 isn_T *isn;
1455
1456 RETURN_OK_IF_SKIP(cctx);
1457 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1458 return FAIL;
1459 return OK;
1460}
1461
1462/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 * Generate an ISN_LOADS instruction.
1464 */
1465 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001466generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001468 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001470 int sid,
1471 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472{
1473 isn_T *isn;
1474
Bram Moolenaar080457c2020-03-03 21:53:32 +01001475 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001476 if (isn_type == ISN_LOADS)
1477 isn = generate_instr_type(cctx, isn_type, type);
1478 else
1479 isn = generate_instr_drop(cctx, isn_type, 1);
1480 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001482 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1483 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484
1485 return OK;
1486}
1487
1488/*
1489 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1490 */
1491 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001492generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 cctx_T *cctx,
1494 isntype_T isn_type,
1495 int sid,
1496 int idx,
1497 type_T *type)
1498{
1499 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001500 scriptref_T *sref;
1501 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502
Bram Moolenaar080457c2020-03-03 21:53:32 +01001503 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 if (isn_type == ISN_LOADSCRIPT)
1505 isn = generate_instr_type(cctx, isn_type, type);
1506 else
1507 isn = generate_instr_drop(cctx, isn_type, 1);
1508 if (isn == NULL)
1509 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001510
1511 // This requires three arguments, which doesn't fit in an instruction, thus
1512 // we need to allocate a struct for this.
1513 sref = ALLOC_ONE(scriptref_T);
1514 if (sref == NULL)
1515 return FAIL;
1516 isn->isn_arg.script.scriptref = sref;
1517 sref->sref_sid = sid;
1518 sref->sref_idx = idx;
1519 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001520 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 return OK;
1522}
1523
1524/*
1525 * Generate an ISN_NEWLIST instruction.
1526 */
1527 static int
1528generate_NEWLIST(cctx_T *cctx, int count)
1529{
1530 isn_T *isn;
1531 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532 type_T *type;
1533 type_T *member;
1534
Bram Moolenaar080457c2020-03-03 21:53:32 +01001535 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1537 return FAIL;
1538 isn->isn_arg.number = count;
1539
Bram Moolenaar127542b2020-08-09 17:22:04 +02001540 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001541 if (count == 0)
1542 member = &t_void;
1543 else
1544 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001545 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1546 cctx->ctx_type_list);
1547 type = get_list_type(member, cctx->ctx_type_list);
1548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 // drop the value types
1550 stack->ga_len -= count;
1551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 // add the list type to the type stack
1553 if (ga_grow(stack, 1) == FAIL)
1554 return FAIL;
1555 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1556 ++stack->ga_len;
1557
1558 return OK;
1559}
1560
1561/*
1562 * Generate an ISN_NEWDICT instruction.
1563 */
1564 static int
1565generate_NEWDICT(cctx_T *cctx, int count)
1566{
1567 isn_T *isn;
1568 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 type_T *type;
1570 type_T *member;
1571
Bram Moolenaar080457c2020-03-03 21:53:32 +01001572 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1574 return FAIL;
1575 isn->isn_arg.number = count;
1576
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001577 if (count == 0)
1578 member = &t_void;
1579 else
1580 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001581 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1582 cctx->ctx_type_list);
1583 type = get_dict_type(member, cctx->ctx_type_list);
1584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 // drop the key and value types
1586 stack->ga_len -= 2 * count;
1587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 // add the dict type to the type stack
1589 if (ga_grow(stack, 1) == FAIL)
1590 return FAIL;
1591 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1592 ++stack->ga_len;
1593
1594 return OK;
1595}
1596
1597/*
1598 * Generate an ISN_FUNCREF instruction.
1599 */
1600 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001601generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602{
1603 isn_T *isn;
1604 garray_T *stack = &cctx->ctx_type_stack;
1605
Bram Moolenaar080457c2020-03-03 21:53:32 +01001606 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1608 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001609 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001610 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001612 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001613 // the nested context, including this one.
1614 if (ufunc->uf_flags & FC_CLOSURE)
1615 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 if (ga_grow(stack, 1) == FAIL)
1618 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001619 ((type_T **)stack->ga_data)[stack->ga_len] =
1620 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 ++stack->ga_len;
1622
1623 return OK;
1624}
1625
1626/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001627 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001628 * "lambda_name" and "func_name" must be in allocated memory and will be
1629 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001630 */
1631 static int
1632generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1633{
1634 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001635
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001636 if (cctx->ctx_skip == SKIP_YES)
1637 {
1638 vim_free(lambda_name);
1639 vim_free(func_name);
1640 return OK;
1641 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001642 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001643 {
1644 vim_free(lambda_name);
1645 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001646 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001647 }
1648 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001649 isn->isn_arg.newfunc.nf_global = func_name;
1650
1651 return OK;
1652}
1653
1654/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001655 * Generate an ISN_DEF instruction: list functions
1656 */
1657 static int
1658generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1659{
1660 isn_T *isn;
1661
1662 RETURN_OK_IF_SKIP(cctx);
1663 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1664 return FAIL;
1665 if (len > 0)
1666 {
1667 isn->isn_arg.string = vim_strnsave(name, len);
1668 if (isn->isn_arg.string == NULL)
1669 return FAIL;
1670 }
1671 return OK;
1672}
1673
1674/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 * Generate an ISN_JUMP instruction.
1676 */
1677 static int
1678generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1679{
1680 isn_T *isn;
1681 garray_T *stack = &cctx->ctx_type_stack;
1682
Bram Moolenaar080457c2020-03-03 21:53:32 +01001683 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1685 return FAIL;
1686 isn->isn_arg.jump.jump_when = when;
1687 isn->isn_arg.jump.jump_where = where;
1688
1689 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1690 --stack->ga_len;
1691
1692 return OK;
1693}
1694
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001695/*
1696 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1697 */
1698 static int
1699generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1700{
1701 isn_T *isn;
1702
1703 RETURN_OK_IF_SKIP(cctx);
1704 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1705 return FAIL;
1706 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1707 // jump_where is set later
1708 return OK;
1709}
1710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 static int
1712generate_FOR(cctx_T *cctx, int loop_idx)
1713{
1714 isn_T *isn;
1715 garray_T *stack = &cctx->ctx_type_stack;
1716
Bram Moolenaar080457c2020-03-03 21:53:32 +01001717 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1719 return FAIL;
1720 isn->isn_arg.forloop.for_idx = loop_idx;
1721
1722 if (ga_grow(stack, 1) == FAIL)
1723 return FAIL;
1724 // type doesn't matter, will be stored next
1725 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1726 ++stack->ga_len;
1727
1728 return OK;
1729}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001730/*
1731 * Generate an ISN_TRYCONT instruction.
1732 */
1733 static int
1734generate_TRYCONT(cctx_T *cctx, int levels, int where)
1735{
1736 isn_T *isn;
1737
1738 RETURN_OK_IF_SKIP(cctx);
1739 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1740 return FAIL;
1741 isn->isn_arg.trycont.tct_levels = levels;
1742 isn->isn_arg.trycont.tct_where = where;
1743
1744 return OK;
1745}
1746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747
1748/*
1749 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001750 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 * Return FAIL if the number of arguments is wrong.
1752 */
1753 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001754generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755{
1756 isn_T *isn;
1757 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001758 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001759 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001760 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761
Bram Moolenaar080457c2020-03-03 21:53:32 +01001762 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001763 argoff = check_internal_func(func_idx, argcount);
1764 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 return FAIL;
1766
Bram Moolenaar389df252020-07-09 21:20:47 +02001767 if (method_call && argoff > 1)
1768 {
1769 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1770 return FAIL;
1771 isn->isn_arg.shuffle.shfl_item = argcount;
1772 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1773 }
1774
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001775 if (argcount > 0)
1776 {
1777 // Check the types of the arguments.
1778 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001779 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1780 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001781 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001782 if (internal_func_is_map(func_idx))
1783 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001784 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1787 return FAIL;
1788 isn->isn_arg.bfunc.cbf_idx = func_idx;
1789 isn->isn_arg.bfunc.cbf_argcount = argcount;
1790
Bram Moolenaar94738d82020-10-21 14:25:07 +02001791 // Drop the argument types and push the return type.
1792 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 if (ga_grow(stack, 1) == FAIL)
1794 return FAIL;
1795 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001796 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001797 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001799 if (maptype != NULL && maptype->tt_member != NULL
1800 && maptype->tt_member != &t_any)
1801 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001802 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 return OK;
1805}
1806
1807/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001808 * Generate an ISN_LISTAPPEND instruction. Works like add().
1809 * Argument count is already checked.
1810 */
1811 static int
1812generate_LISTAPPEND(cctx_T *cctx)
1813{
1814 garray_T *stack = &cctx->ctx_type_stack;
1815 type_T *list_type;
1816 type_T *item_type;
1817 type_T *expected;
1818
1819 // Caller already checked that list_type is a list.
1820 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1821 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1822 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001823 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001824 return FAIL;
1825
1826 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1827 return FAIL;
1828
1829 --stack->ga_len; // drop the argument
1830 return OK;
1831}
1832
1833/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001834 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1835 * Argument count is already checked.
1836 */
1837 static int
1838generate_BLOBAPPEND(cctx_T *cctx)
1839{
1840 garray_T *stack = &cctx->ctx_type_stack;
1841 type_T *item_type;
1842
1843 // Caller already checked that blob_type is a blob.
1844 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001845 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001846 return FAIL;
1847
1848 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1849 return FAIL;
1850
1851 --stack->ga_len; // drop the argument
1852 return OK;
1853}
1854
1855/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001856 * Return TRUE if "ufunc" should be compiled, taking into account whether
1857 * "profile" indicates profiling is to be done.
1858 */
1859 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001860func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001861{
1862 switch (ufunc->uf_def_status)
1863 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001864 case UF_TO_BE_COMPILED:
1865 return TRUE;
1866
Bram Moolenaarb2049902021-01-24 12:53:53 +01001867 case UF_COMPILED:
1868 {
1869 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1870 + ufunc->uf_dfunc_idx;
1871
Bram Moolenaare99d4222021-06-13 14:01:26 +02001872 switch (compile_type)
1873 {
1874 case CT_NONE:
1875 return dfunc->df_instr == NULL;
1876 case CT_PROFILE:
1877 return dfunc->df_instr_prof == NULL;
1878 case CT_DEBUG:
1879 return dfunc->df_instr_debug == NULL;
1880 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001881 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001882
1883 case UF_NOT_COMPILED:
1884 case UF_COMPILE_ERROR:
1885 case UF_COMPILING:
1886 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001887 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001888 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001889}
1890
1891/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 * Generate an ISN_DCALL or ISN_UCALL instruction.
1893 * Return FAIL if the number of arguments is wrong.
1894 */
1895 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001896generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897{
1898 isn_T *isn;
1899 garray_T *stack = &cctx->ctx_type_stack;
1900 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001901 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902
Bram Moolenaar080457c2020-03-03 21:53:32 +01001903 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 if (argcount > regular_args && !has_varargs(ufunc))
1905 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001906 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 return FAIL;
1908 }
1909 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1910 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001911 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 return FAIL;
1913 }
1914
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001915 if (ufunc->uf_def_status != UF_NOT_COMPILED
1916 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001917 {
1918 int i;
1919
1920 for (i = 0; i < argcount; ++i)
1921 {
1922 type_T *expected;
1923 type_T *actual;
1924
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001925 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1926 if (actual == &t_special
1927 && i >= regular_args - ufunc->uf_def_args.ga_len)
1928 {
1929 // assume v:none used for default argument value
1930 continue;
1931 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001932 if (i < regular_args)
1933 {
1934 if (ufunc->uf_arg_types == NULL)
1935 continue;
1936 expected = ufunc->uf_arg_types[i];
1937 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001938 else if (ufunc->uf_va_type == NULL
1939 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001940 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001941 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001942 else
1943 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001944 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001945 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001946 {
1947 arg_type_mismatch(expected, actual, i + 1);
1948 return FAIL;
1949 }
1950 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02001951 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001952 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02001953 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001954 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001955 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02001956 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1957 {
1958 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1959 ufunc->uf_name);
1960 return FAIL;
1961 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001964 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001965 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001967 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 {
1969 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1970 isn->isn_arg.dfunc.cdf_argcount = argcount;
1971 }
1972 else
1973 {
1974 // A user function may be deleted and redefined later, can't use the
1975 // ufunc pointer, need to look it up again at runtime.
1976 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1977 isn->isn_arg.ufunc.cuf_argcount = argcount;
1978 }
1979
1980 stack->ga_len -= argcount; // drop the arguments
1981 if (ga_grow(stack, 1) == FAIL)
1982 return FAIL;
1983 // add return value
1984 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1985 ++stack->ga_len;
1986
1987 return OK;
1988}
1989
1990/*
1991 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1992 */
1993 static int
1994generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1995{
1996 isn_T *isn;
1997 garray_T *stack = &cctx->ctx_type_stack;
1998
Bram Moolenaar080457c2020-03-03 21:53:32 +01001999 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2001 return FAIL;
2002 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2003 isn->isn_arg.ufunc.cuf_argcount = argcount;
2004
2005 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002006 if (ga_grow(stack, 1) == FAIL)
2007 return FAIL;
2008 // add return value
2009 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2010 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011
2012 return OK;
2013}
2014
2015/*
2016 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002017 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 */
2019 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002020generate_PCALL(
2021 cctx_T *cctx,
2022 int argcount,
2023 char_u *name,
2024 type_T *type,
2025 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026{
2027 isn_T *isn;
2028 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002029 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030
Bram Moolenaar080457c2020-03-03 21:53:32 +01002031 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002032
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002033 if (type->tt_type == VAR_ANY)
2034 ret_type = &t_any;
2035 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002036 {
2037 if (type->tt_argcount != -1)
2038 {
2039 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2040
2041 if (argcount < type->tt_min_argcount - varargs)
2042 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002043 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002044 return FAIL;
2045 }
2046 if (!varargs && argcount > type->tt_argcount)
2047 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002048 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002049 return FAIL;
2050 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002051 if (type->tt_args != NULL)
2052 {
2053 int i;
2054
2055 for (i = 0; i < argcount; ++i)
2056 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002057 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002058 type_T *actual = ((type_T **)stack->ga_data)[
2059 stack->ga_len + offset];
2060 type_T *expected;
2061
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002062 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002063 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002064 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002065 else if (i >= type->tt_min_argcount
2066 && actual == &t_special)
2067 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002068 else
2069 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002070 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002071 cctx, TRUE, FALSE) == FAIL)
2072 {
2073 arg_type_mismatch(expected, actual, i + 1);
2074 return FAIL;
2075 }
2076 }
2077 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002078 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002079 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002080 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002081 else
2082 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002083 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002084 return FAIL;
2085 }
2086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2088 return FAIL;
2089 isn->isn_arg.pfunc.cpf_top = at_top;
2090 isn->isn_arg.pfunc.cpf_argcount = argcount;
2091
2092 stack->ga_len -= argcount; // drop the arguments
2093
2094 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002095 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002097 // If partial is above the arguments it must be cleared and replaced with
2098 // the return value.
2099 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2100 return FAIL;
2101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 return OK;
2103}
2104
2105/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002106 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 */
2108 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002109generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110{
2111 isn_T *isn;
2112 garray_T *stack = &cctx->ctx_type_stack;
2113 type_T *type;
2114
Bram Moolenaar080457c2020-03-03 21:53:32 +01002115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002116 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002118 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002120 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002122 if (type->tt_type != VAR_DICT && type != &t_any)
2123 {
2124 emsg(_(e_dictreq));
2125 return FAIL;
2126 }
2127 // change dict type to dict member type
2128 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002129 {
2130 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2131 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133
2134 return OK;
2135}
2136
2137/*
2138 * Generate an ISN_ECHO instruction.
2139 */
2140 static int
2141generate_ECHO(cctx_T *cctx, int with_white, int count)
2142{
2143 isn_T *isn;
2144
Bram Moolenaar080457c2020-03-03 21:53:32 +01002145 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2147 return FAIL;
2148 isn->isn_arg.echo.echo_with_white = with_white;
2149 isn->isn_arg.echo.echo_count = count;
2150
2151 return OK;
2152}
2153
Bram Moolenaarad39c092020-02-26 18:23:43 +01002154/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002155 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002156 */
2157 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002158generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002159{
2160 isn_T *isn;
2161
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002162 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002163 return FAIL;
2164 isn->isn_arg.number = count;
2165
2166 return OK;
2167}
2168
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002169/*
2170 * Generate an ISN_PUT instruction.
2171 */
2172 static int
2173generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2174{
2175 isn_T *isn;
2176
2177 RETURN_OK_IF_SKIP(cctx);
2178 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2179 return FAIL;
2180 isn->isn_arg.put.put_regname = regname;
2181 isn->isn_arg.put.put_lnum = lnum;
2182 return OK;
2183}
2184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 static int
2186generate_EXEC(cctx_T *cctx, char_u *line)
2187{
2188 isn_T *isn;
2189
Bram Moolenaar080457c2020-03-03 21:53:32 +01002190 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2192 return FAIL;
2193 isn->isn_arg.string = vim_strsave(line);
2194 return OK;
2195}
2196
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002197 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002198generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2199{
2200 isn_T *isn;
2201 garray_T *stack = &cctx->ctx_type_stack;
2202
2203 RETURN_OK_IF_SKIP(cctx);
2204 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2205 return FAIL;
2206 isn->isn_arg.string = vim_strsave(line);
2207
2208 if (ga_grow(stack, 1) == FAIL)
2209 return FAIL;
2210 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2211 ++stack->ga_len;
2212
2213 return OK;
2214}
2215
2216 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002217generate_EXECCONCAT(cctx_T *cctx, int count)
2218{
2219 isn_T *isn;
2220
2221 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2222 return FAIL;
2223 isn->isn_arg.number = count;
2224 return OK;
2225}
2226
Bram Moolenaar08597872020-12-10 19:43:40 +01002227/*
2228 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2229 */
2230 static int
2231generate_RANGE(cctx_T *cctx, char_u *range)
2232{
2233 isn_T *isn;
2234 garray_T *stack = &cctx->ctx_type_stack;
2235
2236 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2237 return FAIL;
2238 isn->isn_arg.string = range;
2239
2240 if (ga_grow(stack, 1) == FAIL)
2241 return FAIL;
2242 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2243 ++stack->ga_len;
2244 return OK;
2245}
2246
Bram Moolenaar792f7862020-11-23 08:31:18 +01002247 static int
2248generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2249{
2250 isn_T *isn;
2251
2252 RETURN_OK_IF_SKIP(cctx);
2253 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2254 return FAIL;
2255 isn->isn_arg.unpack.unp_count = var_count;
2256 isn->isn_arg.unpack.unp_semicolon = semicolon;
2257 return OK;
2258}
2259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002261 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002262 */
2263 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002264generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002265{
2266 isn_T *isn;
2267
Bram Moolenaarfa984412021-03-25 22:15:28 +01002268 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002269 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002270 cctx->ctx_has_cmdmod = TRUE;
2271
2272 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002273 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002274 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2275 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2276 return FAIL;
2277 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002278 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002279 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002280 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002281
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002282 return OK;
2283}
2284
2285 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002286generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002287{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002288 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2289 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002290 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002291 return OK;
2292}
2293
Bram Moolenaarfa984412021-03-25 22:15:28 +01002294 static int
2295misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002296{
2297 garray_T *instr = &cctx->ctx_instr;
2298
Bram Moolenaara91a7132021-03-25 21:12:15 +01002299 if (cctx->ctx_has_cmdmod
2300 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2301 == ISN_CMDMOD)
2302 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002303 emsg(_(e_misplaced_command_modifier));
2304 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002305 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002306 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002307}
2308
2309/*
2310 * Get the index of the current instruction.
2311 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2312 */
2313 static int
2314current_instr_idx(cctx_T *cctx)
2315{
2316 garray_T *instr = &cctx->ctx_instr;
2317 int idx = instr->ga_len;
2318
Bram Moolenaare99d4222021-06-13 14:01:26 +02002319 while (idx > 0)
2320 {
2321 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002322 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002323 {
2324 --idx;
2325 continue;
2326 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002327#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002328 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2329 {
2330 --idx;
2331 continue;
2332 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002333#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002334 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2335 {
2336 --idx;
2337 continue;
2338 }
2339 break;
2340 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002341 return idx;
2342}
2343
Bram Moolenaarf002a412021-01-24 13:34:18 +01002344#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002345 static void
2346may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2347{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002348 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002349 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002350}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002351#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002352
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002353/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002355 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002357 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002358reserve_local(
2359 cctx_T *cctx,
2360 char_u *name,
2361 size_t len,
2362 int isConst,
2363 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 lvar_T *lvar;
2366
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002367 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002369 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002370 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 }
2372
2373 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002374 return NULL;
2375 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002376 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002378 // Every local variable uses the next entry on the stack. We could re-use
2379 // the last ones when leaving a scope, but then variables used in a closure
2380 // might get overwritten. To keep things simple do not re-use stack
2381 // entries. This is less efficient, but memory is cheap these days.
2382 lvar->lv_idx = cctx->ctx_locals_count++;
2383
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002384 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 lvar->lv_const = isConst;
2386 lvar->lv_type = type;
2387
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002388 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389}
2390
2391/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002392 * Remove local variables above "new_top".
2393 */
2394 static void
2395unwind_locals(cctx_T *cctx, int new_top)
2396{
2397 if (cctx->ctx_locals.ga_len > new_top)
2398 {
2399 int idx;
2400 lvar_T *lvar;
2401
2402 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2403 {
2404 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2405 vim_free(lvar->lv_name);
2406 }
2407 }
2408 cctx->ctx_locals.ga_len = new_top;
2409}
2410
2411/*
2412 * Free all local variables.
2413 */
2414 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002415free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002416{
2417 unwind_locals(cctx, 0);
2418 ga_clear(&cctx->ctx_locals);
2419}
2420
2421/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002422 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2423 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2424 * error if the variable was defined with :const.
2425 */
2426 static int
2427check_item_writable(svar_T *sv, int check_writable, char_u *name)
2428{
2429 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2430 || (check_writable == ASSIGN_FINAL
2431 && sv->sv_const == ASSIGN_CONST))
2432 {
2433 semsg(_(e_readonlyvar), name);
2434 return FAIL;
2435 }
2436 return OK;
2437}
2438
2439/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002441 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 * Returns the index in "sn_var_vals" if found.
2443 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002444 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 */
2446 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002447get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448{
2449 hashtab_T *ht;
2450 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002451 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002452 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453 int idx;
2454
Bram Moolenaare3d46852020-08-29 13:39:17 +02002455 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002457 if (sid == current_sctx.sc_sid)
2458 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002459 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002460
2461 if (sav == NULL)
2462 return -2;
2463 idx = sav->sav_var_vals_idx;
2464 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002465 if (check_item_writable(sv, check_writable, name) == FAIL)
2466 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002467 return idx;
2468 }
2469
2470 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 ht = &SCRIPT_VARS(sid);
2472 di = find_var_in_ht(ht, 0, name, TRUE);
2473 if (di == NULL)
2474 return -2;
2475
2476 // Now find the svar_T index in sn_var_vals.
2477 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2478 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002479 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 if (sv->sv_tv == &di->di_tv)
2481 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002482 if (check_item_writable(sv, check_writable, name) == FAIL)
2483 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 return idx;
2485 }
2486 }
2487 return -1;
2488}
2489
2490/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002491 * Find "name" in imported items of the current script or in "cctx" if not
2492 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 */
2494 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002495find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 int idx;
2498
Bram Moolenaare3d46852020-08-29 13:39:17 +02002499 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002500 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501 if (cctx != NULL)
2502 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2503 {
2504 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2505 + idx;
2506
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002507 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2508 : STRLEN(import->imp_name) == len
2509 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 return import;
2511 }
2512
Bram Moolenaarefa94442020-08-08 22:16:00 +02002513 return find_imported_in_script(name, len, current_sctx.sc_sid);
2514}
2515
2516 imported_T *
2517find_imported_in_script(char_u *name, size_t len, int sid)
2518{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002519 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002520 int idx;
2521
Bram Moolenaare3d46852020-08-29 13:39:17 +02002522 if (!SCRIPT_ID_VALID(sid))
2523 return NULL;
2524 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2526 {
2527 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2528
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002529 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2530 : STRLEN(import->imp_name) == len
2531 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 return import;
2533 }
2534 return NULL;
2535}
2536
2537/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002538 * Free all imported variables.
2539 */
2540 static void
2541free_imported(cctx_T *cctx)
2542{
2543 int idx;
2544
2545 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2546 {
2547 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2548
2549 vim_free(import->imp_name);
2550 }
2551 ga_clear(&cctx->ctx_imports);
2552}
2553
2554/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002555 * Return a pointer to the next line that isn't empty or only contains a
2556 * comment. Skips over white space.
2557 * Returns NULL if there is none.
2558 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002559 char_u *
2560peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002561{
2562 int lnum = cctx->ctx_lnum;
2563
2564 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2565 {
2566 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002567 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002568
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002569 // ignore NULLs inserted for continuation lines
2570 if (line != NULL)
2571 {
2572 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002573 if (vim9_bad_comment(p))
2574 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002575 if (*p != NUL && !vim9_comment_start(p))
2576 return p;
2577 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002578 }
2579 return NULL;
2580}
2581
2582/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002583 * Called when checking for a following operator at "arg". When the rest of
2584 * the line is empty or only a comment, peek the next line. If there is a next
2585 * line return a pointer to it and set "nextp".
2586 * Otherwise skip over white space.
2587 */
2588 static char_u *
2589may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2590{
2591 char_u *p = skipwhite(arg);
2592
2593 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002594 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002595 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002596 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002597 if (*nextp != NULL)
2598 return *nextp;
2599 }
2600 return p;
2601}
2602
2603/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002604 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002605 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002606 * Returns NULL when at the end.
2607 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002608 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002609next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002610{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002611 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002612
2613 do
2614 {
2615 ++cctx->ctx_lnum;
2616 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002617 {
2618 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002619 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002620 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002621 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002622 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002623 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002624 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002625 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002626 return line;
2627}
2628
2629/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002630 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002631 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002632 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002633 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2634 */
2635 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002636may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002637{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002638 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002639 if (vim9_bad_comment(*arg))
2640 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002641 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002642 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002643 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002644
2645 if (next == NULL)
2646 return FAIL;
2647 *arg = skipwhite(next);
2648 }
2649 return OK;
2650}
2651
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002652/*
2653 * Idem, and give an error when failed.
2654 */
2655 static int
2656may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2657{
2658 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2659 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002660 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002661 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002662 return FAIL;
2663 }
2664 return OK;
2665}
2666
2667
Bram Moolenaara5565e42020-05-09 15:44:01 +02002668// Structure passed between the compile_expr* functions to keep track of
2669// constants that have been parsed but for which no code was produced yet. If
2670// possible expressions on these constants are applied at compile time. If
2671// that is not possible, the code to push the constants needs to be generated
2672// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002673// Using 50 should be more than enough of 5 levels of ().
2674#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002675typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002676 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002677 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002678 int pp_is_const; // all generated code was constants, used for a
2679 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002680} ppconst_T;
2681
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002682static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002683static int compile_expr0(char_u **arg, cctx_T *cctx);
2684static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2685
Bram Moolenaara5565e42020-05-09 15:44:01 +02002686/*
2687 * Generate a PUSH instruction for "tv".
2688 * "tv" will be consumed or cleared.
2689 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2690 */
2691 static int
2692generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2693{
2694 if (tv != NULL)
2695 {
2696 switch (tv->v_type)
2697 {
2698 case VAR_UNKNOWN:
2699 break;
2700 case VAR_BOOL:
2701 generate_PUSHBOOL(cctx, tv->vval.v_number);
2702 break;
2703 case VAR_SPECIAL:
2704 generate_PUSHSPEC(cctx, tv->vval.v_number);
2705 break;
2706 case VAR_NUMBER:
2707 generate_PUSHNR(cctx, tv->vval.v_number);
2708 break;
2709#ifdef FEAT_FLOAT
2710 case VAR_FLOAT:
2711 generate_PUSHF(cctx, tv->vval.v_float);
2712 break;
2713#endif
2714 case VAR_BLOB:
2715 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2716 tv->vval.v_blob = NULL;
2717 break;
2718 case VAR_STRING:
2719 generate_PUSHS(cctx, tv->vval.v_string);
2720 tv->vval.v_string = NULL;
2721 break;
2722 default:
2723 iemsg("constant type not supported");
2724 clear_tv(tv);
2725 return FAIL;
2726 }
2727 tv->v_type = VAR_UNKNOWN;
2728 }
2729 return OK;
2730}
2731
2732/*
2733 * Generate code for any ppconst entries.
2734 */
2735 static int
2736generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2737{
2738 int i;
2739 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002740 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002741
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002742 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002743 for (i = 0; i < ppconst->pp_used; ++i)
2744 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2745 ret = FAIL;
2746 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002747 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002748 return ret;
2749}
2750
2751/*
2752 * Clear ppconst constants. Used when failing.
2753 */
2754 static void
2755clear_ppconst(ppconst_T *ppconst)
2756{
2757 int i;
2758
2759 for (i = 0; i < ppconst->pp_used; ++i)
2760 clear_tv(&ppconst->pp_tv[i]);
2761 ppconst->pp_used = 0;
2762}
2763
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002764/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002765 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002766 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002767 */
2768 static int
2769compile_member(int is_slice, cctx_T *cctx)
2770{
2771 type_T **typep;
2772 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002773 vartype_T vartype;
2774 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002775
Bram Moolenaar261417b2021-05-06 21:04:55 +02002776 // We can index a list, dict and blob. If we don't know the type
2777 // we can use the index value type. If we still don't know use an "ANY"
2778 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002779 typep = ((type_T **)stack->ga_data) + stack->ga_len
2780 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002781 vartype = (*typep)->tt_type;
2782 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002783 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002784 if (*typep == &t_any && idxtype == &t_string)
2785 vartype = VAR_DICT;
2786 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002787 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002788 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002789 return FAIL;
2790 if (is_slice)
2791 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002792 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2793 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002794 FALSE, FALSE) == FAIL)
2795 return FAIL;
2796 }
2797 }
2798
Bram Moolenaar261417b2021-05-06 21:04:55 +02002799 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002800 {
2801 if (is_slice)
2802 {
2803 emsg(_(e_cannot_slice_dictionary));
2804 return FAIL;
2805 }
2806 if ((*typep)->tt_type == VAR_DICT)
2807 {
2808 *typep = (*typep)->tt_member;
2809 if (*typep == &t_unknown)
2810 // empty dict was used
2811 *typep = &t_any;
2812 }
2813 else
2814 {
2815 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2816 FALSE, FALSE) == FAIL)
2817 return FAIL;
2818 *typep = &t_any;
2819 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002820 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002821 return FAIL;
2822 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2823 return FAIL;
2824 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002825 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002826 {
2827 *typep = &t_string;
2828 if ((is_slice
2829 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2830 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2831 return FAIL;
2832 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002833 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002834 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002835 if (is_slice)
2836 {
2837 *typep = &t_blob;
2838 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2839 return FAIL;
2840 }
2841 else
2842 {
2843 *typep = &t_number;
2844 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2845 return FAIL;
2846 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002847 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002848 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002849 {
2850 if (is_slice)
2851 {
2852 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002853 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002854 2) == FAIL)
2855 return FAIL;
2856 }
2857 else
2858 {
2859 if ((*typep)->tt_type == VAR_LIST)
2860 {
2861 *typep = (*typep)->tt_member;
2862 if (*typep == &t_unknown)
2863 // empty list was used
2864 *typep = &t_any;
2865 }
2866 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002867 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2868 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002869 return FAIL;
2870 }
2871 }
2872 else
2873 {
2874 emsg(_(e_string_list_dict_or_blob_required));
2875 return FAIL;
2876 }
2877 return OK;
2878}
2879
2880/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002881 * Generate an instruction to load script-local variable "name", without the
2882 * leading "s:".
2883 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 */
2885 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002886compile_load_scriptvar(
2887 cctx_T *cctx,
2888 char_u *name, // variable NUL terminated
2889 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002890 char_u **end, // end of variable
2891 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002893 scriptitem_T *si;
2894 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 imported_T *import;
2896
Bram Moolenaare3d46852020-08-29 13:39:17 +02002897 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2898 return FAIL;
2899 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002900 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002901 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002903 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002904 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2905 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 }
2907 if (idx >= 0)
2908 {
2909 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2910
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002911 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 current_sctx.sc_sid, idx, sv->sv_type);
2913 return OK;
2914 }
2915
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002916 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 if (import != NULL)
2918 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002919 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002920 {
2921 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002922 char_u *exp_name;
2923 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002924 ufunc_T *ufunc;
2925 type_T *type;
2926
2927 // Used "import * as Name", need to lookup the member.
2928 if (*p != '.')
2929 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002930 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002931 return FAIL;
2932 }
2933 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002934 if (VIM_ISWHITE(*p))
2935 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002936 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002937 return FAIL;
2938 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002939
Bram Moolenaar1c991142020-07-04 13:15:31 +02002940 // isolate one name
2941 exp_name = p;
2942 while (eval_isnamec(*p))
2943 ++p;
2944 cc = *p;
2945 *p = NUL;
2946
Bram Moolenaaredba7072021-03-13 21:14:18 +01002947 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2948 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002949 *p = cc;
2950 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002951 *end = p;
2952
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002953 if (idx < 0)
2954 {
2955 if (*p == '(' && ufunc != NULL)
2956 {
2957 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2958 return OK;
2959 }
2960 return FAIL;
2961 }
2962
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002963 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2964 import->imp_sid,
2965 idx,
2966 type);
2967 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002968 else if (import->imp_funcname != NULL)
2969 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002970 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002971 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2972 import->imp_sid,
2973 import->imp_var_vals_idx,
2974 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 return OK;
2976 }
2977
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002978 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002979 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 return FAIL;
2981}
2982
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002983 static int
2984generate_funcref(cctx_T *cctx, char_u *name)
2985{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002986 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002987
2988 if (ufunc == NULL)
2989 return FAIL;
2990
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002991 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02002992 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
2993 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002994 == FAIL)
2995 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002996 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002997}
2998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999/*
3000 * Compile a variable name into a load instruction.
3001 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003002 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 * When "error" is FALSE do not give an error when not found.
3004 */
3005 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003006compile_load(
3007 char_u **arg,
3008 char_u *end_arg,
3009 cctx_T *cctx,
3010 int is_expr,
3011 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012{
3013 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003014 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003015 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003017 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018
3019 if (*(*arg + 1) == ':')
3020 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003021 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003022 {
3023 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024
Bram Moolenaarfa596382021-04-07 21:58:16 +02003025 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003026 switch (**arg)
3027 {
3028 case 'g': isn_type = ISN_LOADGDICT; break;
3029 case 'w': isn_type = ISN_LOADWDICT; break;
3030 case 't': isn_type = ISN_LOADTDICT; break;
3031 case 'b': isn_type = ISN_LOADBDICT; break;
3032 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003033 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003034 goto theend;
3035 }
3036 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3037 goto theend;
3038 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003039 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 else
3041 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003042 isntype_T isn_type = ISN_DROP;
3043
Bram Moolenaarfa596382021-04-07 21:58:16 +02003044 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003045 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3046 if (name == NULL)
3047 return FAIL;
3048
3049 switch (**arg)
3050 {
3051 case 'v': res = generate_LOADV(cctx, name, error);
3052 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003053 case 's': if (is_expr && ASCII_ISUPPER(*name)
3054 && find_func(name, FALSE, cctx) != NULL)
3055 res = generate_funcref(cctx, name);
3056 else
3057 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003058 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003059 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003060 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003061 {
3062 if (is_expr && ASCII_ISUPPER(*name)
3063 && find_func(name, FALSE, cctx) != NULL)
3064 res = generate_funcref(cctx, name);
3065 else
3066 isn_type = ISN_LOADG;
3067 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003068 else
3069 {
3070 isn_type = ISN_LOADAUTO;
3071 vim_free(name);
3072 name = vim_strnsave(*arg, end - *arg);
3073 if (name == NULL)
3074 return FAIL;
3075 }
3076 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003077 case 'w': isn_type = ISN_LOADW; break;
3078 case 't': isn_type = ISN_LOADT; break;
3079 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003080 default: // cannot happen, just in case
3081 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003082 goto theend;
3083 }
3084 if (isn_type != ISN_DROP)
3085 {
3086 // Global, Buffer-local, Window-local and Tabpage-local
3087 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003088 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003089 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3090 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 }
3092 }
3093 else
3094 {
3095 size_t len = end - *arg;
3096 int idx;
3097 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003098 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099
3100 name = vim_strnsave(*arg, end - *arg);
3101 if (name == NULL)
3102 return FAIL;
3103
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003104 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3105 {
3106 script_autoload(name, FALSE);
3107 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3108 }
3109 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3110 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003112 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003113 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 }
3115 else
3116 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003117 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003118
Bram Moolenaar709664c2020-12-12 14:33:41 +01003119 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003121 type = lvar.lv_type;
3122 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003123 if (lvar.lv_from_outer != 0)
3124 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003125 else
3126 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 }
3128 else
3129 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003130 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003131 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003132 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003133 || find_imported(name, 0, cctx) != NULL)
3134 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003135
Bram Moolenaar0f769812020-09-12 18:32:34 +02003136 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003137 // uppercase letter it can be a user defined function.
3138 // generate_funcref() will fail if the function can't be found.
3139 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003140 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 }
3142 }
3143 if (gen_load)
3144 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003145 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003146 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003147 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003148 cctx->ctx_outer_used = TRUE;
3149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 }
3151
3152 *arg = end;
3153
3154theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003155 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003156 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 vim_free(name);
3158 return res;
3159}
3160
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003161 static void
3162clear_instr_ga(garray_T *gap)
3163{
3164 int idx;
3165
3166 for (idx = 0; idx < gap->ga_len; ++idx)
3167 delete_instr(((isn_T *)gap->ga_data) + idx);
3168 ga_clear(gap);
3169}
3170
3171/*
3172 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3173 * Returns FAIL if compilation fails.
3174 */
3175 static int
3176compile_string(isn_T *isn, cctx_T *cctx)
3177{
3178 char_u *s = isn->isn_arg.string;
3179 garray_T save_ga = cctx->ctx_instr;
3180 int expr_res;
3181 int trailing_error;
3182 int instr_count;
3183 isn_T *instr = NULL;
3184
3185 // Temporarily reset the list of instructions so that the jump labels are
3186 // correct.
3187 cctx->ctx_instr.ga_len = 0;
3188 cctx->ctx_instr.ga_maxlen = 0;
3189 cctx->ctx_instr.ga_data = NULL;
3190 expr_res = compile_expr0(&s, cctx);
3191 s = skipwhite(s);
3192 trailing_error = *s != NUL;
3193
Bram Moolenaarff652882021-05-16 15:24:49 +02003194 if (expr_res == FAIL || trailing_error
3195 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003196 {
3197 if (trailing_error)
3198 semsg(_(e_trailing_arg), s);
3199 clear_instr_ga(&cctx->ctx_instr);
3200 cctx->ctx_instr = save_ga;
3201 return FAIL;
3202 }
3203
3204 // Move the generated instructions into the ISN_INSTR instruction, then
3205 // restore the list of instructions.
3206 instr_count = cctx->ctx_instr.ga_len;
3207 instr = cctx->ctx_instr.ga_data;
3208 instr[instr_count].isn_type = ISN_FINISH;
3209
3210 cctx->ctx_instr = save_ga;
3211 vim_free(isn->isn_arg.string);
3212 isn->isn_type = ISN_INSTR;
3213 isn->isn_arg.instr = instr;
3214 return OK;
3215}
3216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217/*
3218 * Compile the argument expressions.
3219 * "arg" points to just after the "(" and is advanced to after the ")"
3220 */
3221 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003222compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003224 char_u *p = *arg;
3225 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003226 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003227 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228
Bram Moolenaare6085c52020-04-12 20:19:16 +02003229 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003231 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3232 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003233 if (*p == ')')
3234 {
3235 *arg = p + 1;
3236 return OK;
3237 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003238 if (must_end)
3239 {
3240 semsg(_(e_missing_comma_before_argument_str), p);
3241 return FAIL;
3242 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003243
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003244 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003245 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 return FAIL;
3247 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003248
Bram Moolenaarff652882021-05-16 15:24:49 +02003249 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003250 && cctx->ctx_instr.ga_len == instr_count + 1)
3251 {
3252 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3253
3254 // {skip} argument of searchpair() can be compiled if not empty
3255 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3256 compile_string(isn, cctx);
3257 }
3258
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003259 if (*p != ',' && *skipwhite(p) == ',')
3260 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003261 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003262 p = skipwhite(p);
3263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003265 {
3266 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003267 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003268 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003269 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003270 else
3271 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003272 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003273 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003275failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003276 emsg(_(e_missing_close));
3277 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278}
3279
3280/*
3281 * Compile a function call: name(arg1, arg2)
3282 * "arg" points to "name", "arg + varlen" to the "(".
3283 * "argcount_init" is 1 for "value->method()"
3284 * Instructions:
3285 * EVAL arg1
3286 * EVAL arg2
3287 * BCALL / DCALL / UCALL
3288 */
3289 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003290compile_call(
3291 char_u **arg,
3292 size_t varlen,
3293 cctx_T *cctx,
3294 ppconst_T *ppconst,
3295 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296{
3297 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003298 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 int argcount = argcount_init;
3300 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003301 char_u fname_buf[FLEN_FIXED + 1];
3302 char_u *tofree = NULL;
3303 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003304 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003305 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003306 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003307 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308
Bram Moolenaara5565e42020-05-09 15:44:01 +02003309 // we can evaluate "has('name')" at compile time
3310 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3311 {
3312 char_u *s = skipwhite(*arg + varlen + 1);
3313 typval_T argvars[2];
3314
3315 argvars[0].v_type = VAR_UNKNOWN;
3316 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003317 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003318 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003319 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003320 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003321 if (*s == ')' && argvars[0].v_type == VAR_STRING
3322 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003323 {
3324 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3325
3326 *arg = s + 1;
3327 argvars[1].v_type = VAR_UNKNOWN;
3328 tv->v_type = VAR_NUMBER;
3329 tv->vval.v_number = 0;
3330 f_has(argvars, tv);
3331 clear_tv(&argvars[0]);
3332 ++ppconst->pp_used;
3333 return OK;
3334 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003335 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003336 }
3337
3338 if (generate_ppconst(cctx, ppconst) == FAIL)
3339 return FAIL;
3340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 if (varlen >= sizeof(namebuf))
3342 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003343 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 return FAIL;
3345 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003346 vim_strncpy(namebuf, *arg, varlen);
3347 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003349 // We handle the "skip" argument of searchpair() and searchpairpos()
3350 // differently.
3351 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3352 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3353 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3354 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003357 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003358 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359
Bram Moolenaar03290b82020-12-19 16:30:44 +01003360 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003361 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 {
3363 int idx;
3364
3365 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003366 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003368 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003369 if (STRCMP(name, "flatten") == 0)
3370 {
3371 emsg(_(e_cannot_use_flatten_in_vim9_script));
3372 goto theend;
3373 }
3374
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003375 if (STRCMP(name, "add") == 0 && argcount == 2)
3376 {
3377 garray_T *stack = &cctx->ctx_type_stack;
3378 type_T *type = ((type_T **)stack->ga_data)[
3379 stack->ga_len - 2];
3380
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003381 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003382 if (type->tt_type == VAR_LIST)
3383 {
3384 // inline "add(list, item)" so that the type can be checked
3385 res = generate_LISTAPPEND(cctx);
3386 idx = -1;
3387 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003388 else if (type->tt_type == VAR_BLOB)
3389 {
3390 // inline "add(blob, nr)" so that the type can be checked
3391 res = generate_BLOBAPPEND(cctx);
3392 idx = -1;
3393 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003394 }
3395
3396 if (idx >= 0)
3397 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3398 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003399 else
3400 semsg(_(e_unknownfunc), namebuf);
3401 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 }
3403
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003404 // An argument or local variable can be a function reference, this
3405 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003406 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003407 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003408 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003409 // If we can find the function by name generate the right call.
3410 // Skip global functions here, a local funcref takes precedence.
3411 ufunc = find_func(name, FALSE, cctx);
3412 if (ufunc != NULL && !func_is_global(ufunc))
3413 {
3414 res = generate_CALL(cctx, ufunc, argcount);
3415 goto theend;
3416 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003417 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418
3419 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003420 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003421 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003423 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003424 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003425 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003426 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003427 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003428
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003429 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003430 goto theend;
3431 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432
Bram Moolenaar0f769812020-09-12 18:32:34 +02003433 // If we can find a global function by name generate the right call.
3434 if (ufunc != NULL)
3435 {
3436 res = generate_CALL(cctx, ufunc, argcount);
3437 goto theend;
3438 }
3439
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003440 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003441 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003442 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003443 res = generate_UCALL(cctx, name, argcount);
3444 else
3445 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003446
3447theend:
3448 vim_free(tofree);
3449 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450}
3451
3452// like NAMESPACE_CHAR but with 'a' and 'l'.
3453#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3454
3455/*
3456 * Find the end of a variable or function name. Unlike find_name_end() this
3457 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003458 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 * Return a pointer to just after the name. Equal to "arg" if there is no
3460 * valid name.
3461 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003462 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003463to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464{
3465 char_u *p;
3466
3467 // Quick check for valid starting character.
3468 if (!eval_isnamec1(*arg))
3469 return arg;
3470
3471 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3472 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3473 // and can be used in slice "[n:]".
3474 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003475 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3477 break;
3478 return p;
3479}
3480
3481/*
3482 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003483 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 */
3485 char_u *
3486to_name_const_end(char_u *arg)
3487{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003488 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 typval_T rettv;
3490
3491 if (p == arg && *arg == '[')
3492 {
3493
3494 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003495 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 p = arg;
3497 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 return p;
3499}
3500
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501/*
3502 * parse a list: [expr, expr]
3503 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003504 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 */
3506 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003507compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508{
3509 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003510 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003512 int is_const;
3513 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003515 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003517 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003518 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003519 semsg(_(e_list_end), *arg);
3520 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003521 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003522 if (*p == ',')
3523 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003524 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003525 return FAIL;
3526 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003527 if (*p == ']')
3528 {
3529 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003530 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003531 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003532 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003533 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003534 if (!is_const)
3535 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 ++count;
3537 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003538 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003540 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3541 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003542 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003543 return FAIL;
3544 }
3545 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003546 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 p = skipwhite(p);
3548 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003549 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003551 ppconst->pp_is_const = is_all_const;
3552 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553}
3554
3555/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003556 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003557 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003558 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 */
3560 static int
3561compile_lambda(char_u **arg, cctx_T *cctx)
3562{
Bram Moolenaare462f522020-12-27 14:43:30 +01003563 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 typval_T rettv;
3565 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003566 evalarg_T evalarg;
3567
3568 CLEAR_FIELD(evalarg);
3569 evalarg.eval_flags = EVAL_EVALUATE;
3570 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571
3572 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003573 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3574 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003575 {
3576 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003577 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003578 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003579
Bram Moolenaar65c44152020-12-24 15:14:01 +01003580 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003582 ++ufunc->uf_refcount;
3583 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584
Bram Moolenaara9931532021-06-12 15:58:16 +02003585 // Compile it here to get the return type. The return type is optional,
3586 // when it's missing use t_unknown. This is recognized in
3587 // compile_return().
3588 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3589 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaare99d4222021-06-13 14:01:26 +02003590 compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003592 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3593 // points into it. Point to the original line to avoid a dangling pointer.
3594 if (evalarg.eval_tofree_cmdline != NULL)
3595 {
3596 size_t off = *arg - evalarg.eval_tofree_cmdline;
3597
3598 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3599 + off;
3600 }
3601
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003602 clear_evalarg(&evalarg, NULL);
3603
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003604 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003605 {
3606 // The return type will now be known.
3607 set_function_type(ufunc);
3608
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003609 // The function reference count will be 1. When the ISN_FUNCREF
3610 // instruction is deleted the reference count is decremented and the
3611 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003612 return generate_FUNCREF(cctx, ufunc);
3613 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003614
3615 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 return FAIL;
3617}
3618
3619/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003620 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003622 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 */
3624 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003625compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626{
3627 garray_T *instr = &cctx->ctx_instr;
3628 int count = 0;
3629 dict_T *d = dict_alloc();
3630 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003631 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003632 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003633 int is_const;
3634 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635
3636 if (d == NULL)
3637 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003638 if (generate_ppconst(cctx, ppconst) == FAIL)
3639 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003640 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003642 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643
Bram Moolenaar23c55272020-06-21 16:58:13 +02003644 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003645 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003646 *arg = NULL;
3647 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003648 }
3649
3650 if (**arg == '}')
3651 break;
3652
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003653 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003655 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003657 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003658 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003659 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003662 if (isn->isn_type == ISN_PUSHNR)
3663 {
3664 char buf[NUMBUFLEN];
3665
3666 // Convert to string at compile time.
3667 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3668 isn->isn_type = ISN_PUSHS;
3669 isn->isn_arg.string = vim_strsave((char_u *)buf);
3670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 if (isn->isn_type == ISN_PUSHS)
3672 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003673 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003674 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003675 *arg = skipwhite(*arg);
3676 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003677 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003678 emsg(_(e_missing_matching_bracket_after_dict_key));
3679 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003680 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003681 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003683 else
3684 {
3685 // {"name": value},
3686 // {'name': value},
3687 // {name: value} use "name" as a literal key
3688 key = get_literal_key(arg);
3689 if (key == NULL)
3690 return FAIL;
3691 if (generate_PUSHS(cctx, key) == FAIL)
3692 return FAIL;
3693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694
3695 // Check for duplicate keys, if using string keys.
3696 if (key != NULL)
3697 {
3698 item = dict_find(d, key, -1);
3699 if (item != NULL)
3700 {
3701 semsg(_(e_duplicate_key), key);
3702 goto failret;
3703 }
3704 item = dictitem_alloc(key);
3705 if (item != NULL)
3706 {
3707 item->di_tv.v_type = VAR_UNKNOWN;
3708 item->di_tv.v_lock = 0;
3709 if (dict_add(d, item) == FAIL)
3710 dictitem_free(item);
3711 }
3712 }
3713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 if (**arg != ':')
3715 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003716 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003717 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003718 else
3719 semsg(_(e_missing_dict_colon), *arg);
3720 return FAIL;
3721 }
3722 whitep = *arg + 1;
3723 if (!IS_WHITE_OR_NUL(*whitep))
3724 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003725 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 return FAIL;
3727 }
3728
Bram Moolenaar23c55272020-06-21 16:58:13 +02003729 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003730 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003731 *arg = NULL;
3732 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003733 }
3734
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003735 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003737 if (!is_const)
3738 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 ++count;
3740
Bram Moolenaar2c330432020-04-13 14:41:35 +02003741 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003742 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003743 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003744 *arg = NULL;
3745 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 if (**arg == '}')
3748 break;
3749 if (**arg != ',')
3750 {
3751 semsg(_(e_missing_dict_comma), *arg);
3752 goto failret;
3753 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003754 if (IS_WHITE_OR_NUL(*whitep))
3755 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003756 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003757 return FAIL;
3758 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003759 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003760 if (!IS_WHITE_OR_NUL(*whitep))
3761 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003762 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003763 return FAIL;
3764 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003765 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 }
3767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 *arg = *arg + 1;
3769
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003770 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003771 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003772 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003773 *arg += STRLEN(*arg);
3774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003776 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 return generate_NEWDICT(cctx, count);
3778
3779failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003780 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003781 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003782 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003783 *arg = (char_u *)"";
3784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 dict_unref(d);
3786 return FAIL;
3787}
3788
3789/*
3790 * Compile "&option".
3791 */
3792 static int
3793compile_get_option(char_u **arg, cctx_T *cctx)
3794{
3795 typval_T rettv;
3796 char_u *start = *arg;
3797 int ret;
3798
3799 // parse the option and get the current value to get the type.
3800 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003801 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 if (ret == OK)
3803 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003804 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003805 char_u *name = vim_strnsave(start, *arg - start);
3806 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3807 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808
3809 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3810 vim_free(name);
3811 }
3812 clear_tv(&rettv);
3813
3814 return ret;
3815}
3816
3817/*
3818 * Compile "$VAR".
3819 */
3820 static int
3821compile_get_env(char_u **arg, cctx_T *cctx)
3822{
3823 char_u *start = *arg;
3824 int len;
3825 int ret;
3826 char_u *name;
3827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 ++*arg;
3829 len = get_env_len(arg);
3830 if (len == 0)
3831 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003832 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 return FAIL;
3834 }
3835
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003836 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 name = vim_strnsave(start, len + 1);
3838 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3839 vim_free(name);
3840 return ret;
3841}
3842
3843/*
3844 * Compile "@r".
3845 */
3846 static int
3847compile_get_register(char_u **arg, cctx_T *cctx)
3848{
3849 int ret;
3850
3851 ++*arg;
3852 if (**arg == NUL)
3853 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003854 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 return FAIL;
3856 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003857 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 {
3859 emsg_invreg(**arg);
3860 return FAIL;
3861 }
3862 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3863 ++*arg;
3864 return ret;
3865}
3866
3867/*
3868 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003869 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 */
3871 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003872apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003874 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875
3876 // this works from end to start
3877 while (p > start)
3878 {
3879 --p;
3880 if (*p == '-' || *p == '+')
3881 {
3882 // only '-' has an effect, for '+' we only check the type
3883#ifdef FEAT_FLOAT
3884 if (rettv->v_type == VAR_FLOAT)
3885 {
3886 if (*p == '-')
3887 rettv->vval.v_float = -rettv->vval.v_float;
3888 }
3889 else
3890#endif
3891 {
3892 varnumber_T val;
3893 int error = FALSE;
3894
3895 // tv_get_number_chk() accepts a string, but we don't want that
3896 // here
3897 if (check_not_string(rettv) == FAIL)
3898 return FAIL;
3899 val = tv_get_number_chk(rettv, &error);
3900 clear_tv(rettv);
3901 if (error)
3902 return FAIL;
3903 if (*p == '-')
3904 val = -val;
3905 rettv->v_type = VAR_NUMBER;
3906 rettv->vval.v_number = val;
3907 }
3908 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003909 else if (numeric_only)
3910 {
3911 ++p;
3912 break;
3913 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003914 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 {
3916 int v = tv2bool(rettv);
3917
3918 // '!' is permissive in the type.
3919 clear_tv(rettv);
3920 rettv->v_type = VAR_BOOL;
3921 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3922 }
3923 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003924 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 return OK;
3926}
3927
3928/*
3929 * Recognize v: variables that are constants and set "rettv".
3930 */
3931 static void
3932get_vim_constant(char_u **arg, typval_T *rettv)
3933{
3934 if (STRNCMP(*arg, "v:true", 6) == 0)
3935 {
3936 rettv->v_type = VAR_BOOL;
3937 rettv->vval.v_number = VVAL_TRUE;
3938 *arg += 6;
3939 }
3940 else if (STRNCMP(*arg, "v:false", 7) == 0)
3941 {
3942 rettv->v_type = VAR_BOOL;
3943 rettv->vval.v_number = VVAL_FALSE;
3944 *arg += 7;
3945 }
3946 else if (STRNCMP(*arg, "v:null", 6) == 0)
3947 {
3948 rettv->v_type = VAR_SPECIAL;
3949 rettv->vval.v_number = VVAL_NULL;
3950 *arg += 6;
3951 }
3952 else if (STRNCMP(*arg, "v:none", 6) == 0)
3953 {
3954 rettv->v_type = VAR_SPECIAL;
3955 rettv->vval.v_number = VVAL_NONE;
3956 *arg += 6;
3957 }
3958}
3959
Bram Moolenaar657137c2021-01-09 15:45:23 +01003960 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003961get_compare_type(char_u *p, int *len, int *type_is)
3962{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003963 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003964 int i;
3965
3966 switch (p[0])
3967 {
3968 case '=': if (p[1] == '=')
3969 type = EXPR_EQUAL;
3970 else if (p[1] == '~')
3971 type = EXPR_MATCH;
3972 break;
3973 case '!': if (p[1] == '=')
3974 type = EXPR_NEQUAL;
3975 else if (p[1] == '~')
3976 type = EXPR_NOMATCH;
3977 break;
3978 case '>': if (p[1] != '=')
3979 {
3980 type = EXPR_GREATER;
3981 *len = 1;
3982 }
3983 else
3984 type = EXPR_GEQUAL;
3985 break;
3986 case '<': if (p[1] != '=')
3987 {
3988 type = EXPR_SMALLER;
3989 *len = 1;
3990 }
3991 else
3992 type = EXPR_SEQUAL;
3993 break;
3994 case 'i': if (p[1] == 's')
3995 {
3996 // "is" and "isnot"; but not a prefix of a name
3997 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3998 *len = 5;
3999 i = p[*len];
4000 if (!isalnum(i) && i != '_')
4001 {
4002 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4003 *type_is = TRUE;
4004 }
4005 }
4006 break;
4007 }
4008 return type;
4009}
4010
4011/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004012 * Skip over an expression, ignoring most errors.
4013 */
4014 static void
4015skip_expr_cctx(char_u **arg, cctx_T *cctx)
4016{
4017 evalarg_T evalarg;
4018
4019 CLEAR_FIELD(evalarg);
4020 evalarg.eval_cctx = cctx;
4021 skip_expr(arg, &evalarg);
4022}
4023
4024/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004026 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 */
4028 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004029compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004031 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032
4033 // this works from end to start
4034 while (p > start)
4035 {
4036 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004037 while (VIM_ISWHITE(*p))
4038 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 if (*p == '-' || *p == '+')
4040 {
4041 int negate = *p == '-';
4042 isn_T *isn;
4043
4044 // TODO: check type
4045 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4046 {
4047 --p;
4048 if (*p == '-')
4049 negate = !negate;
4050 }
4051 // only '-' has an effect, for '+' we only check the type
4052 if (negate)
4053 isn = generate_instr(cctx, ISN_NEGATENR);
4054 else
4055 isn = generate_instr(cctx, ISN_CHECKNR);
4056 if (isn == NULL)
4057 return FAIL;
4058 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004059 else if (numeric_only)
4060 {
4061 ++p;
4062 break;
4063 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 else
4065 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004066 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004068 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004070 if (p[-1] == '!')
4071 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004074 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 return FAIL;
4076 }
4077 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004078 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 return OK;
4080}
4081
4082/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004083 * Compile "(expression)": recursive!
4084 * Return FAIL/OK.
4085 */
4086 static int
4087compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4088{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004089 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004090 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004091
Bram Moolenaar24156692021-01-14 20:35:49 +01004092 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4093 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004094 if (ppconst->pp_used <= PPSIZE - 10)
4095 {
4096 ret = compile_expr1(arg, cctx, ppconst);
4097 }
4098 else
4099 {
4100 // Not enough space in ppconst, flush constants.
4101 if (generate_ppconst(cctx, ppconst) == FAIL)
4102 return FAIL;
4103 ret = compile_expr0(arg, cctx);
4104 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004105 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4106 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004107 if (**arg == ')')
4108 ++*arg;
4109 else if (ret == OK)
4110 {
4111 emsg(_(e_missing_close));
4112 ret = FAIL;
4113 }
4114 return ret;
4115}
4116
4117/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004119 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 */
4121 static int
4122compile_subscript(
4123 char_u **arg,
4124 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004125 char_u *start_leader,
4126 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004127 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004129 char_u *name_start = *end_leader;
4130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 for (;;)
4132 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004133 char_u *p = skipwhite(*arg);
4134
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004135 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004136 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004137 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004138
4139 // If a following line starts with "->{" or "->X" advance to that
4140 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004141 // Also if a following line starts with ".x".
4142 if (next != NULL &&
4143 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004144 && (next[2] == '{'
4145 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004146 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004147 {
4148 next = next_line_from_context(cctx, TRUE);
4149 if (next == NULL)
4150 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004151 *arg = next;
4152 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004153 }
4154 }
4155
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004156 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004157 // not a function call.
4158 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004160 garray_T *stack = &cctx->ctx_type_stack;
4161 type_T *type;
4162 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004164 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004165 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004166 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004169 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4170
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004171 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004172 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004174 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 return FAIL;
4176 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004177 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004179 char_u *pstart = p;
4180
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004181 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004182 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004183 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 // something->method()
4186 // Apply the '!', '-' and '+' first:
4187 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004188 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004191 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004192 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004193 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004194 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004195 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004196 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004197 garray_T *stack = &cctx->ctx_type_stack;
4198 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004199 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004200 int expr_isn_start = cctx->ctx_instr.ga_len;
4201 int expr_isn_end;
4202 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004203
4204 // Funcref call: list->(Refs[2])(arg)
4205 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004206 //
4207 // Fist compile the function expression.
4208 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004209 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004210
4211 // Remember the next instruction index, where the instructions
4212 // for arguments are being written.
4213 expr_isn_end = cctx->ctx_instr.ga_len;
4214
4215 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004216 if (**arg != '(')
4217 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004218 if (*skipwhite(*arg) == '(')
4219 emsg(_(e_nowhitespace));
4220 else
4221 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004222 return FAIL;
4223 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004224 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004225 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004226 return FAIL;
4227
Bram Moolenaar2927c072021-04-05 19:41:21 +02004228 // Move the instructions for the arguments to before the
4229 // instructions of the expression and move the type of the
4230 // expression after the argument types. This is what ISN_PCALL
4231 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004232 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004233 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4234 if (arg_isn_count > 0)
4235 {
4236 int expr_isn_count = expr_isn_end - expr_isn_start;
4237 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4238
4239 if (isn == NULL)
4240 return FAIL;
4241 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4242 + expr_isn_start,
4243 sizeof(isn_T) * expr_isn_count);
4244 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4245 + expr_isn_start,
4246 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4247 sizeof(isn_T) * arg_isn_count);
4248 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4249 + expr_isn_start + arg_isn_count,
4250 isn, sizeof(isn_T) * expr_isn_count);
4251 vim_free(isn);
4252
4253 type = ((type_T **)stack->ga_data)[type_idx_start];
4254 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4255 ((type_T **)stack->ga_data) + type_idx_start + 1,
4256 sizeof(type_T *)
4257 * (stack->ga_len - type_idx_start - 1));
4258 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4259 }
4260
Bram Moolenaar7e368202020-12-25 21:56:57 +01004261 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4262 if (generate_PCALL(cctx, argcount,
4263 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 return FAIL;
4265 }
4266 else
4267 {
4268 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004269 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004270 if (!eval_isnamec1(*p))
4271 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004272 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004273 return FAIL;
4274 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004275 if (ASCII_ISALPHA(*p) && p[1] == ':')
4276 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004277 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 ;
4279 if (*p != '(')
4280 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004281 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 return FAIL;
4283 }
4284 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004285 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 return FAIL;
4287 }
4288 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004289 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004291 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004292
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004293 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004294 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004295 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004296 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004297 // TODO: more arguments
4298 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004299 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004300 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004301 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004302
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004303 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004304 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004305 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004306 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004307 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004308 // missing first index is equal to zero
4309 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004310 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004311 else
4312 {
4313 if (compile_expr0(arg, cctx) == FAIL)
4314 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004315 if (**arg == ':')
4316 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004317 semsg(_(e_white_space_required_before_and_after_str_at_str),
4318 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004319 return FAIL;
4320 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004321 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004322 return FAIL;
4323 *arg = skipwhite(*arg);
4324 }
4325 if (**arg == ':')
4326 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004327 is_slice = TRUE;
4328 ++*arg;
4329 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4330 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004331 semsg(_(e_white_space_required_before_and_after_str_at_str),
4332 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004333 return FAIL;
4334 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004335 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004336 return FAIL;
4337 if (**arg == ']')
4338 // missing second index is equal to end of string
4339 generate_PUSHNR(cctx, -1);
4340 else
4341 {
4342 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004343 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004344 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004345 return FAIL;
4346 *arg = skipwhite(*arg);
4347 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349
4350 if (**arg != ']')
4351 {
4352 emsg(_(e_missbrac));
4353 return FAIL;
4354 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004355 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356
Bram Moolenaare42939a2021-04-05 17:11:17 +02004357 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004358 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004360 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004362 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004363 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004364 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004365 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004366
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004367 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004368 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004369 {
4370 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004371 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004372 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004373 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004374 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 while (eval_isnamec(*p))
4376 MB_PTR_ADV(p);
4377 if (p == *arg)
4378 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004379 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 return FAIL;
4381 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004382 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 return FAIL;
4384 *arg = p;
4385 }
4386 else
4387 break;
4388 }
4389
4390 // TODO - see handle_subscript():
4391 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4392 // Don't do this when "Func" is already a partial that was bound
4393 // explicitly (pt_auto is FALSE).
4394
4395 return OK;
4396}
4397
4398/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004399 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4400 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004402 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004403 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004404 *
4405 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 */
4407
4408/*
4409 * number number constant
4410 * 0zFFFFFFFF Blob constant
4411 * "string" string constant
4412 * 'string' literal string constant
4413 * &option-name option value
4414 * @r register contents
4415 * identifier variable value
4416 * function() function call
4417 * $VAR environment variable
4418 * (expression) nested expression
4419 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004420 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 *
4422 * Also handle:
4423 * ! in front logical NOT
4424 * - in front unary minus
4425 * + in front unary plus (ignored)
4426 * trailing (arg) funcref/partial call
4427 * trailing [] subscript in String or List
4428 * trailing .name entry in Dictionary
4429 * trailing ->name() method call
4430 */
4431 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004432compile_expr7(
4433 char_u **arg,
4434 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004435 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 char_u *start_leader, *end_leader;
4438 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004439 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004440 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004442 ppconst->pp_is_const = FALSE;
4443
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 /*
4445 * Skip '!', '-' and '+' characters. They are handled later.
4446 */
4447 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004448 if (eval_leader(arg, TRUE) == FAIL)
4449 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 end_leader = *arg;
4451
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004452 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 switch (**arg)
4454 {
4455 /*
4456 * Number constant.
4457 */
4458 case '0': // also for blob starting with 0z
4459 case '1':
4460 case '2':
4461 case '3':
4462 case '4':
4463 case '5':
4464 case '6':
4465 case '7':
4466 case '8':
4467 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004468 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004470 // Apply "-" and "+" just before the number now, right to
4471 // left. Matters especially when "->" follows. Stops at
4472 // '!'.
4473 if (apply_leader(rettv, TRUE,
4474 start_leader, &end_leader) == FAIL)
4475 {
4476 clear_tv(rettv);
4477 return FAIL;
4478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 break;
4480
4481 /*
4482 * String constant: "string".
4483 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004484 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 return FAIL;
4486 break;
4487
4488 /*
4489 * Literal string constant: 'str''ing'.
4490 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004491 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 return FAIL;
4493 break;
4494
4495 /*
4496 * Constant Vim variable.
4497 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004498 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 ret = NOTDONE;
4500 break;
4501
4502 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004503 * "true" constant
4504 */
4505 case 't': if (STRNCMP(*arg, "true", 4) == 0
4506 && !eval_isnamec((*arg)[4]))
4507 {
4508 *arg += 4;
4509 rettv->v_type = VAR_BOOL;
4510 rettv->vval.v_number = VVAL_TRUE;
4511 }
4512 else
4513 ret = NOTDONE;
4514 break;
4515
4516 /*
4517 * "false" constant
4518 */
4519 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4520 && !eval_isnamec((*arg)[5]))
4521 {
4522 *arg += 5;
4523 rettv->v_type = VAR_BOOL;
4524 rettv->vval.v_number = VVAL_FALSE;
4525 }
4526 else
4527 ret = NOTDONE;
4528 break;
4529
4530 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004531 * "null" constant
4532 */
4533 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004534 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004535 {
4536 *arg += 4;
4537 rettv->v_type = VAR_SPECIAL;
4538 rettv->vval.v_number = VVAL_NULL;
4539 }
4540 else
4541 ret = NOTDONE;
4542 break;
4543
4544 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 * List: [expr, expr]
4546 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004547 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4548 return FAIL;
4549 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 break;
4551
4552 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 * Dictionary: {'key': val, 'key': val}
4554 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004555 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4556 return FAIL;
4557 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 break;
4559
4560 /*
4561 * Option value: &name
4562 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004563 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4564 return FAIL;
4565 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 break;
4567
4568 /*
4569 * Environment variable: $VAR.
4570 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004571 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4572 return FAIL;
4573 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 break;
4575
4576 /*
4577 * Register contents: @r.
4578 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004579 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4580 return FAIL;
4581 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 break;
4583 /*
4584 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004585 * lambda: (arg, arg) => expr
4586 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004588 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4589 ret = compile_lambda(arg, cctx);
4590 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004591 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 break;
4593
4594 default: ret = NOTDONE;
4595 break;
4596 }
4597 if (ret == FAIL)
4598 return FAIL;
4599
Bram Moolenaar1c747212020-05-09 18:28:34 +02004600 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004602 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004603 clear_tv(rettv);
4604 else
4605 // A constant expression can possibly be handled compile time,
4606 // return the value instead of generating code.
4607 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 }
4609 else if (ret == NOTDONE)
4610 {
4611 char_u *p;
4612 int r;
4613
4614 if (!eval_isnamec1(**arg))
4615 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004616 if (!vim9_bad_comment(*arg))
4617 {
4618 if (ends_excmd(*skipwhite(*arg)))
4619 semsg(_(e_empty_expression_str), *arg);
4620 else
4621 semsg(_(e_name_expected_str), *arg);
4622 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 return FAIL;
4624 }
4625
4626 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004627 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004628 if (p - *arg == (size_t)1 && **arg == '_')
4629 {
4630 emsg(_(e_cannot_use_underscore_here));
4631 return FAIL;
4632 }
4633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004635 {
4636 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004639 {
4640 if (generate_ppconst(cctx, ppconst) == FAIL)
4641 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004642 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 if (r == FAIL)
4645 return FAIL;
4646 }
4647
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004648 // Handle following "[]", ".member", etc.
4649 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004650 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004651 ppconst) == FAIL)
4652 return FAIL;
4653 if (ppconst->pp_used > 0)
4654 {
4655 // apply the '!', '-' and '+' before the constant
4656 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004657 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004658 return FAIL;
4659 return OK;
4660 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004661 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004663 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664}
4665
4666/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004667 * Give the "white on both sides" error, taking the operator from "p[len]".
4668 */
4669 void
4670error_white_both(char_u *op, int len)
4671{
4672 char_u buf[10];
4673
4674 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004675 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004676}
4677
4678/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004679 * <type>expr7: runtime type check / conversion
4680 */
4681 static int
4682compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4683{
4684 type_T *want_type = NULL;
4685
4686 // Recognize <type>
4687 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4688 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004689 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004690 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4691 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004692 return FAIL;
4693
4694 if (**arg != '>')
4695 {
4696 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004697 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004698 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004699 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004700 return FAIL;
4701 }
4702 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004703 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004704 return FAIL;
4705 }
4706
4707 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4708 return FAIL;
4709
4710 if (want_type != NULL)
4711 {
4712 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004713 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004714 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004715
Bram Moolenaard1103582020-08-14 22:44:25 +02004716 generate_ppconst(cctx, ppconst);
4717 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004718 where.wt_index = 0;
4719 where.wt_variable = FALSE;
4720 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004721 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004722 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004723 return FAIL;
4724 }
4725 }
4726
4727 return OK;
4728}
4729
4730/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 * * number multiplication
4732 * / number division
4733 * % number modulo
4734 */
4735 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004736compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737{
4738 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004739 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004740 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004742 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004743 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 return FAIL;
4745
4746 /*
4747 * Repeat computing, until no "*", "/" or "%" is following.
4748 */
4749 for (;;)
4750 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004751 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 if (*op != '*' && *op != '/' && *op != '%')
4753 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004754 if (next != NULL)
4755 {
4756 *arg = next_line_from_context(cctx, TRUE);
4757 op = skipwhite(*arg);
4758 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004759
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004760 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004762 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004763 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004765 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004766 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004768 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004769 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004771
4772 if (ppconst->pp_used == ppconst_used + 2
4773 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4774 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004775 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004776 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4777 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4778 varnumber_T res = 0;
4779 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004781 // both are numbers: compute the result
4782 switch (*op)
4783 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004784 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004785 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004786 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004787 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004788 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004789 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004790 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004791 break;
4792 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004793 if (failed)
4794 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004795 tv1->vval.v_number = res;
4796 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004797 }
4798 else
4799 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004800 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004801 generate_two_op(cctx, op);
4802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803 }
4804
4805 return OK;
4806}
4807
4808/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004809 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810 * - number subtraction
4811 * .. string concatenation
4812 */
4813 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004814compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815{
4816 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004817 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004818 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004819 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820
4821 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004822 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 return FAIL;
4824
4825 /*
4826 * Repeat computing, until no "+", "-" or ".." is following.
4827 */
4828 for (;;)
4829 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004830 op = may_peek_next_line(cctx, *arg, &next);
4831 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004833 if (op[0] == op[1] && *op != '.' && next)
4834 // Finding "++" or "--" on the next line is a separate command.
4835 // But ".." is concatenation.
4836 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004838 if (next != NULL)
4839 {
4840 *arg = next_line_from_context(cctx, TRUE);
4841 op = skipwhite(*arg);
4842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004844 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004846 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004847 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 }
4849
Bram Moolenaare0de1712020-12-02 17:36:54 +01004850 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004851 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004853 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004854 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855 return FAIL;
4856
Bram Moolenaara5565e42020-05-09 15:44:01 +02004857 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004858 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004859 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4860 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4861 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4862 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004864 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4865 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004866
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004867 // concat/subtract/add constant numbers
4868 if (*op == '+')
4869 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4870 else if (*op == '-')
4871 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4872 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004873 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004874 // concatenate constant strings
4875 char_u *s1 = tv1->vval.v_string;
4876 char_u *s2 = tv2->vval.v_string;
4877 size_t len1 = STRLEN(s1);
4878
4879 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4880 if (tv1->vval.v_string == NULL)
4881 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004882 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004883 return FAIL;
4884 }
4885 mch_memmove(tv1->vval.v_string, s1, len1);
4886 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004887 vim_free(s1);
4888 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004889 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004890 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891 }
4892 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004893 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004894 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004895 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004896 if (*op == '.')
4897 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004898 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4899 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004900 return FAIL;
4901 generate_instr_drop(cctx, ISN_CONCAT, 1);
4902 }
4903 else
4904 generate_two_op(cctx, op);
4905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906 }
4907
4908 return OK;
4909}
4910
4911/*
4912 * expr5a == expr5b
4913 * expr5a =~ expr5b
4914 * expr5a != expr5b
4915 * expr5a !~ expr5b
4916 * expr5a > expr5b
4917 * expr5a >= expr5b
4918 * expr5a < expr5b
4919 * expr5a <= expr5b
4920 * expr5a is expr5b
4921 * expr5a isnot expr5b
4922 *
4923 * Produces instructions:
4924 * EVAL expr5a Push result of "expr5a"
4925 * EVAL expr5b Push result of "expr5b"
4926 * COMPARE one of the compare instructions
4927 */
4928 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004929compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004931 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004933 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004936 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937
4938 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004939 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 return FAIL;
4941
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004942 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004943 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944
4945 /*
4946 * If there is a comparative operator, use it.
4947 */
4948 if (type != EXPR_UNKNOWN)
4949 {
4950 int ic = FALSE; // Default: do not ignore case
4951
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004952 if (next != NULL)
4953 {
4954 *arg = next_line_from_context(cctx, TRUE);
4955 p = skipwhite(*arg);
4956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957 if (type_is && (p[len] == '?' || p[len] == '#'))
4958 {
4959 semsg(_(e_invexpr2), *arg);
4960 return FAIL;
4961 }
4962 // extra question mark appended: ignore case
4963 if (p[len] == '?')
4964 {
4965 ic = TRUE;
4966 ++len;
4967 }
4968 // extra '#' appended: match case (ignored)
4969 else if (p[len] == '#')
4970 ++len;
4971 // nothing appended: match case
4972
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004973 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004975 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004976 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 }
4978
4979 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004980 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004981 return FAIL;
4982
Bram Moolenaara5565e42020-05-09 15:44:01 +02004983 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 return FAIL;
4985
Bram Moolenaara5565e42020-05-09 15:44:01 +02004986 if (ppconst->pp_used == ppconst_used + 2)
4987 {
4988 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4989 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4990 int ret;
4991
4992 // Both sides are a constant, compute the result now.
4993 // First check for a valid combination of types, this is more
4994 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004995 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004996 ret = FAIL;
4997 else
4998 {
4999 ret = typval_compare(tv1, tv2, type, ic);
5000 tv1->v_type = VAR_BOOL;
5001 tv1->vval.v_number = tv1->vval.v_number
5002 ? VVAL_TRUE : VVAL_FALSE;
5003 clear_tv(tv2);
5004 --ppconst->pp_used;
5005 }
5006 return ret;
5007 }
5008
5009 generate_ppconst(cctx, ppconst);
5010 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 }
5012
5013 return OK;
5014}
5015
Bram Moolenaar7f141552020-05-09 17:35:53 +02005016static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018/*
5019 * Compile || or &&.
5020 */
5021 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005022compile_and_or(
5023 char_u **arg,
5024 cctx_T *cctx,
5025 char *op,
5026 ppconst_T *ppconst,
5027 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005029 char_u *next;
5030 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 int opchar = *op;
5032
5033 if (p[0] == opchar && p[1] == opchar)
5034 {
5035 garray_T *instr = &cctx->ctx_instr;
5036 garray_T end_ga;
5037
5038 /*
5039 * Repeat until there is no following "||" or "&&"
5040 */
5041 ga_init2(&end_ga, sizeof(int), 10);
5042 while (p[0] == opchar && p[1] == opchar)
5043 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005044 long start_lnum = SOURCING_LNUM;
5045 int start_ctx_lnum = cctx->ctx_lnum;
5046 int save_lnum;
5047
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005048 if (next != NULL)
5049 {
5050 *arg = next_line_from_context(cctx, TRUE);
5051 p = skipwhite(*arg);
5052 }
5053
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005054 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5055 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005056 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005057 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005058 return FAIL;
5059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005061 // TODO: use ppconst if the value is a constant and check
5062 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005063 generate_ppconst(cctx, ppconst);
5064
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005065 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005066 SOURCING_LNUM = start_lnum;
5067 save_lnum = cctx->ctx_lnum;
5068 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005069 if (bool_on_stack(cctx) == FAIL)
5070 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005071 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005072 ga_clear(&end_ga);
5073 return FAIL;
5074 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005075 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 if (ga_grow(&end_ga, 1) == FAIL)
5078 {
5079 ga_clear(&end_ga);
5080 return FAIL;
5081 }
5082 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5083 ++end_ga.ga_len;
5084 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005085 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086
5087 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005088 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005089 {
5090 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005091 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005092 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005093
Bram Moolenaara5565e42020-05-09 15:44:01 +02005094 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5095 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 {
5097 ga_clear(&end_ga);
5098 return FAIL;
5099 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005100
5101 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005103 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005105 // Every part must evaluate to a bool.
5106 if (bool_on_stack(cctx) == FAIL)
5107 {
5108 ga_clear(&end_ga);
5109 return FAIL;
5110 }
5111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 // Fill in the end label in all jumps.
5113 while (end_ga.ga_len > 0)
5114 {
5115 isn_T *isn;
5116
5117 --end_ga.ga_len;
5118 isn = ((isn_T *)instr->ga_data)
5119 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5120 isn->isn_arg.jump.jump_where = instr->ga_len;
5121 }
5122 ga_clear(&end_ga);
5123 }
5124
5125 return OK;
5126}
5127
5128/*
5129 * expr4a && expr4a && expr4a logical AND
5130 *
5131 * Produces instructions:
5132 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005133 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005134 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005135 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005136 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 * EVAL expr4c Push result of "expr4c"
5138 * end:
5139 */
5140 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005141compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005143 int ppconst_used = ppconst->pp_used;
5144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005146 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 return FAIL;
5148
5149 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005150 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151}
5152
5153/*
5154 * expr3a || expr3b || expr3c logical OR
5155 *
5156 * Produces instructions:
5157 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005158 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005159 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005160 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005161 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162 * EVAL expr3c Push result of "expr3c"
5163 * end:
5164 */
5165 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005166compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005168 int ppconst_used = ppconst->pp_used;
5169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005171 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172 return FAIL;
5173
5174 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005175 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176}
5177
5178/*
5179 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005181 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 * JUMP_IF_FALSE alt jump if false
5183 * EVAL expr1a
5184 * JUMP_ALWAYS end
5185 * alt: EVAL expr1b
5186 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005187 *
5188 * Toplevel expression: expr2 ?? expr1
5189 * Produces instructions:
5190 * EVAL expr2 Push result of "expr2"
5191 * JUMP_AND_KEEP_IF_TRUE end jump if true
5192 * EVAL expr1
5193 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005194 */
5195 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005196compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197{
5198 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005199 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005200 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201
Bram Moolenaar3988f642020-08-27 22:43:03 +02005202 // Ignore all kinds of errors when not producing code.
5203 if (cctx->ctx_skip == SKIP_YES)
5204 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005205 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005206 return OK;
5207 }
5208
Bram Moolenaar61a89812020-05-07 16:58:17 +02005209 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005210 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 return FAIL;
5212
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005213 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 if (*p == '?')
5215 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005216 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217 garray_T *instr = &cctx->ctx_instr;
5218 garray_T *stack = &cctx->ctx_type_stack;
5219 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005220 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005222 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005223 int has_const_expr = FALSE;
5224 int const_value = FALSE;
5225 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005226
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005227 if (next != NULL)
5228 {
5229 *arg = next_line_from_context(cctx, TRUE);
5230 p = skipwhite(*arg);
5231 }
5232
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005233 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005234 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005235 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005236 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005237 return FAIL;
5238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239
Bram Moolenaara5565e42020-05-09 15:44:01 +02005240 if (ppconst->pp_used == ppconst_used + 1)
5241 {
5242 // the condition is a constant, we know whether the ? or the :
5243 // expression is to be evaluated.
5244 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005245 if (op_falsy)
5246 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5247 else
5248 {
5249 int error = FALSE;
5250
5251 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5252 &error);
5253 if (error)
5254 return FAIL;
5255 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005256 cctx->ctx_skip = save_skip == SKIP_YES ||
5257 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5258
5259 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5260 // "left ?? right" and "left" is truthy: produce "left"
5261 generate_ppconst(cctx, ppconst);
5262 else
5263 {
5264 clear_tv(&ppconst->pp_tv[ppconst_used]);
5265 --ppconst->pp_used;
5266 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005267 }
5268 else
5269 {
5270 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005271 if (op_falsy)
5272 end_idx = instr->ga_len;
5273 generate_JUMP(cctx, op_falsy
5274 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5275 if (op_falsy)
5276 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005277 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005278
5279 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005280 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005281 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005282 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005283 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284
Bram Moolenaara5565e42020-05-09 15:44:01 +02005285 if (!has_const_expr)
5286 {
5287 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005289 if (!op_falsy)
5290 {
5291 // remember the type and drop it
5292 --stack->ga_len;
5293 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005294
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005295 end_idx = instr->ga_len;
5296 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005297
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005298 // jump here from JUMP_IF_FALSE
5299 isn = ((isn_T *)instr->ga_data) + alt_idx;
5300 isn->isn_arg.jump.jump_where = instr->ga_len;
5301 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005304 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005305 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005306 // Check for the ":".
5307 p = may_peek_next_line(cctx, *arg, &next);
5308 if (*p != ':')
5309 {
5310 emsg(_(e_missing_colon));
5311 return FAIL;
5312 }
5313 if (next != NULL)
5314 {
5315 *arg = next_line_from_context(cctx, TRUE);
5316 p = skipwhite(*arg);
5317 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005318
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005319 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5320 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005321 semsg(_(e_white_space_required_before_and_after_str_at_str),
5322 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005323 return FAIL;
5324 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005325
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005326 // evaluate the third expression
5327 if (has_const_expr)
5328 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005329 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005330 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005331 return FAIL;
5332 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5333 return FAIL;
5334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335
Bram Moolenaara5565e42020-05-09 15:44:01 +02005336 if (!has_const_expr)
5337 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005338 type_T **typep;
5339
Bram Moolenaara5565e42020-05-09 15:44:01 +02005340 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341
Bram Moolenaara5565e42020-05-09 15:44:01 +02005342 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005343 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5344 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005345
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005346 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005347 isn = ((isn_T *)instr->ga_data) + end_idx;
5348 isn->isn_arg.jump.jump_where = instr->ga_len;
5349 }
5350
5351 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005352 }
5353 return OK;
5354}
5355
5356/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005357 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005358 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5359 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005360 */
5361 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005362compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005363{
5364 ppconst_T ppconst;
5365
5366 CLEAR_FIELD(ppconst);
5367 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5368 {
5369 clear_ppconst(&ppconst);
5370 return FAIL;
5371 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005372 if (is_const != NULL)
5373 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005374 if (generate_ppconst(cctx, &ppconst) == FAIL)
5375 return FAIL;
5376 return OK;
5377}
5378
5379/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005380 * Toplevel expression.
5381 */
5382 static int
5383compile_expr0(char_u **arg, cctx_T *cctx)
5384{
5385 return compile_expr0_ext(arg, cctx, NULL);
5386}
5387
5388/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005389 * Compile "return [expr]".
5390 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005391 */
5392 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005393compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394{
5395 char_u *p = arg;
5396 garray_T *stack = &cctx->ctx_type_stack;
5397 type_T *stack_type;
5398
5399 if (*p != NUL && *p != '|' && *p != '\n')
5400 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005401 if (legacy)
5402 {
5403 int save_flags = cmdmod.cmod_flags;
5404
5405 generate_LEGACY_EVAL(cctx, p);
5406 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5407 0, cctx, FALSE, FALSE) == FAIL)
5408 return NULL;
5409 cmdmod.cmod_flags |= CMOD_LEGACY;
5410 (void)skip_expr(&p, NULL);
5411 cmdmod.cmod_flags = save_flags;
5412 }
5413 else
5414 {
5415 // compile return argument into instructions
5416 if (compile_expr0(&p, cctx) == FAIL)
5417 return NULL;
5418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005419
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005420 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005421 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005422 // "check_return_type" with uf_ret_type set to &t_unknown is used
5423 // for an inline function without a specified return type. Set the
5424 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005425 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005426 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005427 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5428 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005429 || (!check_return_type
5430 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005431 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005432 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005433 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005434 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005435 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005436 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5437 && stack_type->tt_type != VAR_VOID
5438 && stack_type->tt_type != VAR_UNKNOWN)
5439 {
5440 emsg(_(e_returning_value_in_function_without_return_type));
5441 return NULL;
5442 }
5443 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005444 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005445 return NULL;
5446 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448 }
5449 else
5450 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005451 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005452 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005453 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5454 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005456 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457 return NULL;
5458 }
5459
5460 // No argument, return zero.
5461 generate_PUSHNR(cctx, 0);
5462 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005463
5464 // Undo any command modifiers.
5465 generate_undo_cmdmods(cctx);
5466
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005467 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468 return NULL;
5469
5470 // "return val | endif" is possible
5471 return skipwhite(p);
5472}
5473
5474/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005475 * Get a line from the compilation context, compatible with exarg_T getline().
5476 * Return a pointer to the line in allocated memory.
5477 * Return NULL for end-of-file or some error.
5478 */
5479 static char_u *
5480exarg_getline(
5481 int c UNUSED,
5482 void *cookie,
5483 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005484 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005485{
5486 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005487 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005488
Bram Moolenaar66250c92020-08-20 15:02:42 +02005489 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005490 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005491 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005492 return NULL;
5493 ++cctx->ctx_lnum;
5494 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5495 // Comment lines result in NULL pointers, skip them.
5496 if (p != NULL)
5497 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005498 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005499}
5500
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005501 void
5502fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5503{
5504 eap->getline = exarg_getline;
5505 eap->cookie = cctx;
5506}
5507
Bram Moolenaar04b12692020-05-04 23:24:44 +02005508/*
5509 * Compile a nested :def command.
5510 */
5511 static char_u *
5512compile_nested_function(exarg_T *eap, cctx_T *cctx)
5513{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005514 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005515 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005516 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005517 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005518 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005519 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005520
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005521 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005522 {
5523 emsg(_(e_cannot_use_bang_with_nested_def));
5524 return NULL;
5525 }
5526
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005527 if (*name_start == '/')
5528 {
5529 name_end = skip_regexp(name_start + 1, '/', TRUE);
5530 if (*name_end == '/')
5531 ++name_end;
5532 eap->nextcmd = check_nextcmd(name_end);
5533 }
5534 if (name_end == name_start || *skipwhite(name_end) != '(')
5535 {
5536 if (!ends_excmd2(name_start, name_end))
5537 {
5538 semsg(_(e_invalid_command_str), eap->cmd);
5539 return NULL;
5540 }
5541
5542 // "def" or "def Name": list functions
5543 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5544 return NULL;
5545 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5546 }
5547
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005548 // Only g:Func() can use a namespace.
5549 if (name_start[1] == ':' && !is_global)
5550 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005551 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005552 return NULL;
5553 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005554 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005555 return NULL;
5556
Bram Moolenaar04b12692020-05-04 23:24:44 +02005557 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005558 fill_exarg_from_cctx(eap, cctx);
5559
Bram Moolenaar04b12692020-05-04 23:24:44 +02005560 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005561 lambda_name = vim_strsave(get_lambda_name());
5562 if (lambda_name == NULL)
5563 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005564 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005565
Bram Moolenaar822ba242020-05-24 23:00:18 +02005566 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005567 {
5568 r = eap->skip ? OK : FAIL;
5569 goto theend;
5570 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005571
5572 // copy over the block scope IDs before compiling
5573 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5574 {
5575 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5576
5577 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5578 if (ufunc->uf_block_ids != NULL)
5579 {
5580 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5581 sizeof(int) * block_depth);
5582 ufunc->uf_block_depth = block_depth;
5583 }
5584 }
5585
Bram Moolenaare99d4222021-06-13 14:01:26 +02005586 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
5587 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005588 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005589 {
5590 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005591 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005592 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005593
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005594 if (is_global)
5595 {
5596 char_u *func_name = vim_strnsave(name_start + 2,
5597 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005598
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005599 if (func_name == NULL)
5600 r = FAIL;
5601 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005602 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005603 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005604 lambda_name = NULL;
5605 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005606 }
5607 else
5608 {
5609 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005610 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005611 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005612
Bram Moolenaareef21022020-08-01 22:16:43 +02005613 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005614 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005615 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005616 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005617 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5618 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005619 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005620
5621theend:
5622 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005623 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005624}
5625
5626/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005627 * Return the length of an assignment operator, or zero if there isn't one.
5628 */
5629 int
5630assignment_len(char_u *p, int *heredoc)
5631{
5632 if (*p == '=')
5633 {
5634 if (p[1] == '<' && p[2] == '<')
5635 {
5636 *heredoc = TRUE;
5637 return 3;
5638 }
5639 return 1;
5640 }
5641 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5642 return 2;
5643 if (STRNCMP(p, "..=", 3) == 0)
5644 return 3;
5645 return 0;
5646}
5647
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005648/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005649 * Generate the load instruction for "name".
5650 */
5651 static void
5652generate_loadvar(
5653 cctx_T *cctx,
5654 assign_dest_T dest,
5655 char_u *name,
5656 lvar_T *lvar,
5657 type_T *type)
5658{
5659 switch (dest)
5660 {
5661 case dest_option:
5662 // TODO: check the option exists
5663 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5664 break;
5665 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005666 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5667 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5668 else
5669 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005670 break;
5671 case dest_buffer:
5672 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5673 break;
5674 case dest_window:
5675 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5676 break;
5677 case dest_tab:
5678 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5679 break;
5680 case dest_script:
5681 compile_load_scriptvar(cctx,
5682 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5683 break;
5684 case dest_env:
5685 // Include $ in the name here
5686 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5687 break;
5688 case dest_reg:
5689 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5690 break;
5691 case dest_vimvar:
5692 generate_LOADV(cctx, name + 2, TRUE);
5693 break;
5694 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005695 if (lvar->lv_from_outer > 0)
5696 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5697 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005698 else
5699 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5700 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005701 case dest_expr:
5702 // list or dict value should already be on the stack.
5703 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005704 }
5705}
5706
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005707/*
5708 * Skip over "[expr]" or ".member".
5709 * Does not check for any errors.
5710 */
5711 static char_u *
5712skip_index(char_u *start)
5713{
5714 char_u *p = start;
5715
5716 if (*p == '[')
5717 {
5718 p = skipwhite(p + 1);
5719 (void)skip_expr(&p, NULL);
5720 p = skipwhite(p);
5721 if (*p == ']')
5722 return p + 1;
5723 return p;
5724 }
5725 // if (*p == '.')
5726 return to_name_end(p + 1, TRUE);
5727}
5728
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005729 void
5730vim9_declare_error(char_u *name)
5731{
5732 char *scope = "";
5733
5734 switch (*name)
5735 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005736 case 'g': scope = _("global"); break;
5737 case 'b': scope = _("buffer"); break;
5738 case 'w': scope = _("window"); break;
5739 case 't': scope = _("tab"); break;
5740 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005741 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005742 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005743 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005744 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005745 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005746 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005747 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005748 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005749 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005750}
5751
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005752/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005753 * For one assignment figure out the type of destination. Return it in "dest".
5754 * When not recognized "dest" is not set.
5755 * For an option "opt_flags" is set.
5756 * For a v:var "vimvaridx" is set.
5757 * "type" is set to the destination type if known, unchanted otherwise.
5758 * Return FAIL if an error message was given.
5759 */
5760 static int
5761get_var_dest(
5762 char_u *name,
5763 assign_dest_T *dest,
5764 int cmdidx,
5765 int *opt_flags,
5766 int *vimvaridx,
5767 type_T **type,
5768 cctx_T *cctx)
5769{
5770 char_u *p;
5771
5772 if (*name == '&')
5773 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005774 int cc;
5775 long numval;
5776 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005777
5778 *dest = dest_option;
5779 if (cmdidx == CMD_final || cmdidx == CMD_const)
5780 {
5781 emsg(_(e_const_option));
5782 return FAIL;
5783 }
5784 p = name;
5785 p = find_option_end(&p, opt_flags);
5786 if (p == NULL)
5787 {
5788 // cannot happen?
5789 emsg(_(e_letunexp));
5790 return FAIL;
5791 }
5792 cc = *p;
5793 *p = NUL;
5794 opt_type = get_option_value(skip_option_env_lead(name),
5795 &numval, NULL, *opt_flags);
5796 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005797 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005798 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005799 case gov_unknown:
5800 semsg(_(e_unknown_option), name);
5801 return FAIL;
5802 case gov_string:
5803 case gov_hidden_string:
5804 *type = &t_string;
5805 break;
5806 case gov_bool:
5807 case gov_hidden_bool:
5808 *type = &t_bool;
5809 break;
5810 case gov_number:
5811 case gov_hidden_number:
5812 *type = &t_number;
5813 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005814 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005815 }
5816 else if (*name == '$')
5817 {
5818 *dest = dest_env;
5819 *type = &t_string;
5820 }
5821 else if (*name == '@')
5822 {
5823 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5824 {
5825 emsg_invreg(name[1]);
5826 return FAIL;
5827 }
5828 *dest = dest_reg;
5829 *type = &t_string;
5830 }
5831 else if (STRNCMP(name, "g:", 2) == 0)
5832 {
5833 *dest = dest_global;
5834 }
5835 else if (STRNCMP(name, "b:", 2) == 0)
5836 {
5837 *dest = dest_buffer;
5838 }
5839 else if (STRNCMP(name, "w:", 2) == 0)
5840 {
5841 *dest = dest_window;
5842 }
5843 else if (STRNCMP(name, "t:", 2) == 0)
5844 {
5845 *dest = dest_tab;
5846 }
5847 else if (STRNCMP(name, "v:", 2) == 0)
5848 {
5849 typval_T *vtv;
5850 int di_flags;
5851
5852 *vimvaridx = find_vim_var(name + 2, &di_flags);
5853 if (*vimvaridx < 0)
5854 {
5855 semsg(_(e_variable_not_found_str), name);
5856 return FAIL;
5857 }
5858 // We use the current value of "sandbox" here, is that OK?
5859 if (var_check_ro(di_flags, name, FALSE))
5860 return FAIL;
5861 *dest = dest_vimvar;
5862 vtv = get_vim_var_tv(*vimvaridx);
5863 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5864 }
5865 return OK;
5866}
5867
5868/*
5869 * Generate a STORE instruction for "dest", not being "dest_local".
5870 * Return FAIL when out of memory.
5871 */
5872 static int
5873generate_store_var(
5874 cctx_T *cctx,
5875 assign_dest_T dest,
5876 int opt_flags,
5877 int vimvaridx,
5878 int scriptvar_idx,
5879 int scriptvar_sid,
5880 type_T *type,
5881 char_u *name)
5882{
5883 switch (dest)
5884 {
5885 case dest_option:
5886 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5887 opt_flags);
5888 case dest_global:
5889 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005890 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5891 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005892 case dest_buffer:
5893 // include b: with the name, easier to execute that way
5894 return generate_STORE(cctx, ISN_STOREB, 0, name);
5895 case dest_window:
5896 // include w: with the name, easier to execute that way
5897 return generate_STORE(cctx, ISN_STOREW, 0, name);
5898 case dest_tab:
5899 // include t: with the name, easier to execute that way
5900 return generate_STORE(cctx, ISN_STORET, 0, name);
5901 case dest_env:
5902 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5903 case dest_reg:
5904 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5905 case dest_vimvar:
5906 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5907 case dest_script:
5908 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005909 // "s:" may be included in the name.
5910 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005911 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005912 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5913 scriptvar_sid, scriptvar_idx, type);
5914 case dest_local:
5915 case dest_expr:
5916 // cannot happen
5917 break;
5918 }
5919 return FAIL;
5920}
5921
Bram Moolenaar752fc692021-01-04 21:57:11 +01005922 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005923generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5924{
5925 if (lhs->lhs_dest != dest_local)
5926 return generate_store_var(cctx, lhs->lhs_dest,
5927 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5928 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5929 lhs->lhs_type, lhs->lhs_name);
5930
5931 if (lhs->lhs_lvar != NULL)
5932 {
5933 garray_T *instr = &cctx->ctx_instr;
5934 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5935
5936 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5937 // ISN_STORENR
5938 if (lhs->lhs_lvar->lv_from_outer == 0
5939 && instr->ga_len == instr_count + 1
5940 && isn->isn_type == ISN_PUSHNR)
5941 {
5942 varnumber_T val = isn->isn_arg.number;
5943 garray_T *stack = &cctx->ctx_type_stack;
5944
5945 isn->isn_type = ISN_STORENR;
5946 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5947 isn->isn_arg.storenr.stnr_val = val;
5948 if (stack->ga_len > 0)
5949 --stack->ga_len;
5950 }
5951 else if (lhs->lhs_lvar->lv_from_outer > 0)
5952 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5953 lhs->lhs_lvar->lv_from_outer);
5954 else
5955 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5956 }
5957 return OK;
5958}
5959
5960 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005961is_decl_command(int cmdidx)
5962{
5963 return cmdidx == CMD_let || cmdidx == CMD_var
5964 || cmdidx == CMD_final || cmdidx == CMD_const;
5965}
5966
5967/*
5968 * Figure out the LHS type and other properties for an assignment or one item
5969 * of ":unlet" with an index.
5970 * Returns OK or FAIL.
5971 */
5972 static int
5973compile_lhs(
5974 char_u *var_start,
5975 lhs_T *lhs,
5976 int cmdidx,
5977 int heredoc,
5978 int oplen,
5979 cctx_T *cctx)
5980{
5981 char_u *var_end;
5982 int is_decl = is_decl_command(cmdidx);
5983
5984 CLEAR_POINTER(lhs);
5985 lhs->lhs_dest = dest_local;
5986 lhs->lhs_vimvaridx = -1;
5987 lhs->lhs_scriptvar_idx = -1;
5988
5989 // "dest_end" is the end of the destination, including "[expr]" or
5990 // ".name".
5991 // "var_end" is the end of the variable/option/etc. name.
5992 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5993 if (*var_start == '@')
5994 var_end = var_start + 2;
5995 else
5996 {
5997 // skip over the leading "&", "&l:", "&g:" and "$"
5998 var_end = skip_option_env_lead(var_start);
5999 var_end = to_name_end(var_end, TRUE);
6000 }
6001
6002 // "a: type" is declaring variable "a" with a type, not dict "a:".
6003 if (is_decl && lhs->lhs_dest_end == var_start + 2
6004 && lhs->lhs_dest_end[-1] == ':')
6005 --lhs->lhs_dest_end;
6006 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6007 --var_end;
6008
6009 // compute the length of the destination without "[expr]" or ".name"
6010 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006011 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006012 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6013 if (lhs->lhs_name == NULL)
6014 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006015
6016 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6017 // Something follows after the variable: "var[idx]" or "var.key".
6018 lhs->lhs_has_index = TRUE;
6019
Bram Moolenaar752fc692021-01-04 21:57:11 +01006020 if (heredoc)
6021 lhs->lhs_type = &t_list_string;
6022 else
6023 lhs->lhs_type = &t_any;
6024
6025 if (cctx->ctx_skip != SKIP_YES)
6026 {
6027 int declare_error = FALSE;
6028
6029 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6030 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6031 &lhs->lhs_type, cctx) == FAIL)
6032 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006033 if (lhs->lhs_dest != dest_local
6034 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006035 {
6036 // Specific kind of variable recognized.
6037 declare_error = is_decl;
6038 }
6039 else
6040 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006041 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006042 if (check_reserved_name(lhs->lhs_name) == FAIL)
6043 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006044
6045 if (lookup_local(var_start, lhs->lhs_varlen,
6046 &lhs->lhs_local_lvar, cctx) == OK)
6047 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6048 else
6049 {
6050 CLEAR_FIELD(lhs->lhs_arg_lvar);
6051 if (arg_exists(var_start, lhs->lhs_varlen,
6052 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6053 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6054 {
6055 if (is_decl)
6056 {
6057 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6058 return FAIL;
6059 }
6060 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6061 }
6062 }
6063 if (lhs->lhs_lvar != NULL)
6064 {
6065 if (is_decl)
6066 {
6067 semsg(_(e_variable_already_declared), lhs->lhs_name);
6068 return FAIL;
6069 }
6070 }
6071 else
6072 {
6073 int script_namespace = lhs->lhs_varlen > 1
6074 && STRNCMP(var_start, "s:", 2) == 0;
6075 int script_var = (script_namespace
6076 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006077 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006078 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006079 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006080 imported_T *import =
6081 find_imported(var_start, lhs->lhs_varlen, cctx);
6082
6083 if (script_namespace || script_var || import != NULL)
6084 {
6085 char_u *rawname = lhs->lhs_name
6086 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6087
6088 if (is_decl)
6089 {
6090 if (script_namespace)
6091 semsg(_(e_cannot_declare_script_variable_in_function),
6092 lhs->lhs_name);
6093 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006094 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006095 lhs->lhs_name);
6096 return FAIL;
6097 }
6098 else if (cctx->ctx_ufunc->uf_script_ctx_version
6099 == SCRIPT_VERSION_VIM9
6100 && script_namespace
6101 && !script_var && import == NULL)
6102 {
6103 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6104 return FAIL;
6105 }
6106
6107 lhs->lhs_dest = dest_script;
6108
6109 // existing script-local variables should have a type
6110 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6111 if (import != NULL)
6112 lhs->lhs_scriptvar_sid = import->imp_sid;
6113 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6114 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006115 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006116 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006117 lhs->lhs_scriptvar_sid, rawname,
6118 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6119 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006120 if (lhs->lhs_scriptvar_idx >= 0)
6121 {
6122 scriptitem_T *si = SCRIPT_ITEM(
6123 lhs->lhs_scriptvar_sid);
6124 svar_T *sv =
6125 ((svar_T *)si->sn_var_vals.ga_data)
6126 + lhs->lhs_scriptvar_idx;
6127 lhs->lhs_type = sv->sv_type;
6128 }
6129 }
6130 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006131 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006132 == FAIL)
6133 return FAIL;
6134 }
6135 }
6136
6137 if (declare_error)
6138 {
6139 vim9_declare_error(lhs->lhs_name);
6140 return FAIL;
6141 }
6142 }
6143
6144 // handle "a:name" as a name, not index "name" on "a"
6145 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6146 var_end = lhs->lhs_dest_end;
6147
6148 if (lhs->lhs_dest != dest_option)
6149 {
6150 if (is_decl && *var_end == ':')
6151 {
6152 char_u *p;
6153
6154 // parse optional type: "let var: type = expr"
6155 if (!VIM_ISWHITE(var_end[1]))
6156 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006157 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006158 return FAIL;
6159 }
6160 p = skipwhite(var_end + 1);
6161 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6162 if (lhs->lhs_type == NULL)
6163 return FAIL;
6164 lhs->lhs_has_type = TRUE;
6165 }
6166 else if (lhs->lhs_lvar != NULL)
6167 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6168 }
6169
Bram Moolenaare42939a2021-04-05 17:11:17 +02006170 if (oplen == 3 && !heredoc
6171 && lhs->lhs_dest != dest_global
6172 && !lhs->lhs_has_index
6173 && lhs->lhs_type->tt_type != VAR_STRING
6174 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006175 {
6176 emsg(_(e_can_only_concatenate_to_string));
6177 return FAIL;
6178 }
6179
6180 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6181 && cctx->ctx_skip != SKIP_YES)
6182 {
6183 if (oplen > 1 && !heredoc)
6184 {
6185 // +=, /=, etc. require an existing variable
6186 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6187 return FAIL;
6188 }
6189 if (!is_decl)
6190 {
6191 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6192 return FAIL;
6193 }
6194
Bram Moolenaar3f327882021-03-17 20:56:38 +01006195 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006196 if ((lhs->lhs_type->tt_type == VAR_FUNC
6197 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006198 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006199 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006200
6201 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006202 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6203 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6204 if (lhs->lhs_lvar == NULL)
6205 return FAIL;
6206 lhs->lhs_new_local = TRUE;
6207 }
6208
6209 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006210 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006211 {
6212 // Something follows after the variable: "var[idx]" or "var.key".
6213 // TODO: should we also handle "->func()" here?
6214 if (is_decl)
6215 {
6216 emsg(_(e_cannot_use_index_when_declaring_variable));
6217 return FAIL;
6218 }
6219
6220 if (var_start[lhs->lhs_varlen] == '['
6221 || var_start[lhs->lhs_varlen] == '.')
6222 {
6223 char_u *after = var_start + lhs->lhs_varlen;
6224 char_u *p;
6225
6226 // Only the last index is used below, if there are others
6227 // before it generate code for the expression. Thus for
6228 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6229 for (;;)
6230 {
6231 p = skip_index(after);
6232 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006233 {
6234 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006235 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006236 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006237 after = p;
6238 }
6239 if (after > var_start + lhs->lhs_varlen)
6240 {
6241 lhs->lhs_varlen = after - var_start;
6242 lhs->lhs_dest = dest_expr;
6243 // We don't know the type before evaluating the expression,
6244 // use "any" until then.
6245 lhs->lhs_type = &t_any;
6246 }
6247
Bram Moolenaar752fc692021-01-04 21:57:11 +01006248 if (lhs->lhs_type->tt_member == NULL)
6249 lhs->lhs_member_type = &t_any;
6250 else
6251 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6252 }
6253 else
6254 {
6255 semsg("Not supported yet: %s", var_start);
6256 return FAIL;
6257 }
6258 }
6259 return OK;
6260}
6261
6262/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006263 * Figure out the LHS and check a few errors.
6264 */
6265 static int
6266compile_assign_lhs(
6267 char_u *var_start,
6268 lhs_T *lhs,
6269 int cmdidx,
6270 int is_decl,
6271 int heredoc,
6272 int oplen,
6273 cctx_T *cctx)
6274{
6275 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6276 return FAIL;
6277
6278 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6279 {
6280 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6281 return FAIL;
6282 }
6283 if (!is_decl && lhs->lhs_lvar != NULL
6284 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6285 {
6286 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6287 return FAIL;
6288 }
6289 return OK;
6290}
6291
6292/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006293 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6294 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006295 */
6296 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006297compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006298 char_u *var_start,
6299 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006300 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006301 cctx_T *cctx)
6302{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006303 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006304 char_u *p;
6305 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006306 int need_white_before = TRUE;
6307 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006308
Bram Moolenaar752fc692021-01-04 21:57:11 +01006309 p = var_start + varlen;
6310 if (*p == '[')
6311 {
6312 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006313 if (*p == ':')
6314 {
6315 // empty first index, push zero
6316 r = generate_PUSHNR(cctx, 0);
6317 need_white_before = FALSE;
6318 }
6319 else
6320 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006321
6322 if (r == OK && *skipwhite(p) == ':')
6323 {
6324 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006325 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006326 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006327 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006328 empty_second = *skipwhite(p + 1) == ']';
6329 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6330 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006331 {
6332 semsg(_(e_white_space_required_before_and_after_str_at_str),
6333 ":", p);
6334 return FAIL;
6335 }
6336 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006337 if (*p == ']')
6338 // empty second index, push "none"
6339 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6340 else
6341 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006342 }
6343
Bram Moolenaar752fc692021-01-04 21:57:11 +01006344 if (r == OK && *skipwhite(p) != ']')
6345 {
6346 // this should not happen
6347 emsg(_(e_missbrac));
6348 r = FAIL;
6349 }
6350 }
6351 else // if (*p == '.')
6352 {
6353 char_u *key_end = to_name_end(p + 1, TRUE);
6354 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6355
6356 r = generate_PUSHS(cctx, key);
6357 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006358 return r;
6359}
6360
6361/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006362 * For a LHS with an index, load the variable to be indexed.
6363 */
6364 static int
6365compile_load_lhs(
6366 lhs_T *lhs,
6367 char_u *var_start,
6368 type_T *rhs_type,
6369 cctx_T *cctx)
6370{
6371 if (lhs->lhs_dest == dest_expr)
6372 {
6373 size_t varlen = lhs->lhs_varlen;
6374 int c = var_start[varlen];
6375 char_u *p = var_start;
6376 garray_T *stack = &cctx->ctx_type_stack;
6377
6378 // Evaluate "ll[expr]" of "ll[expr][idx]"
6379 var_start[varlen] = NUL;
6380 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6381 {
6382 // this should not happen
6383 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006384 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006385 return FAIL;
6386 }
6387 var_start[varlen] = c;
6388
6389 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6390 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6391 // now we can properly check the type
6392 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6393 && rhs_type != &t_void
6394 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6395 FALSE, FALSE) == FAIL)
6396 return FAIL;
6397 }
6398 else
6399 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6400 lhs->lhs_lvar, lhs->lhs_type);
6401 return OK;
6402}
6403
6404/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006405 * Produce code for loading "lhs" and also take care of an index.
6406 * Return OK/FAIL.
6407 */
6408 static int
6409compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6410{
6411 compile_load_lhs(lhs, var_start, NULL, cctx);
6412
6413 if (lhs->lhs_has_index)
6414 {
6415 int range = FALSE;
6416
6417 // Get member from list or dict. First compile the
6418 // index value.
6419 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6420 return FAIL;
6421 if (range)
6422 {
6423 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6424 var_start);
6425 return FAIL;
6426 }
6427
6428 // Get the member.
6429 if (compile_member(FALSE, cctx) == FAIL)
6430 return FAIL;
6431 }
6432 return OK;
6433}
6434
6435/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006436 * Assignment to a list or dict member, or ":unlet" for the item, using the
6437 * information in "lhs".
6438 * Returns OK or FAIL.
6439 */
6440 static int
6441compile_assign_unlet(
6442 char_u *var_start,
6443 lhs_T *lhs,
6444 int is_assign,
6445 type_T *rhs_type,
6446 cctx_T *cctx)
6447{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006448 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006449 garray_T *stack = &cctx->ctx_type_stack;
6450 int range = FALSE;
6451
Bram Moolenaar68452172021-04-12 21:21:02 +02006452 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006453 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006454 if (is_assign && range && lhs->lhs_type != &t_blob
6455 && lhs->lhs_type != &t_any)
6456 {
6457 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6458 return FAIL;
6459 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006460
6461 if (lhs->lhs_type == &t_any)
6462 {
6463 // Index on variable of unknown type: check at runtime.
6464 dest_type = VAR_ANY;
6465 }
6466 else
6467 {
6468 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006469 if (dest_type == VAR_DICT && range)
6470 {
6471 emsg(e_cannot_use_range_with_dictionary);
6472 return FAIL;
6473 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006474 if (dest_type == VAR_DICT
6475 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006476 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006477 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006478 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006479 type_T *type;
6480
6481 if (range)
6482 {
6483 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6484 if (need_type(type, &t_number,
6485 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006486 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006487 }
6488 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6489 if ((dest_type != VAR_BLOB || type != &t_special)
6490 && need_type(type, &t_number,
6491 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006492 return FAIL;
6493 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006494 }
6495
6496 // Load the dict or list. On the stack we then have:
6497 // - value (for assignment, not for :unlet)
6498 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006499 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006500 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006501 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6502 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006503
Bram Moolenaar68452172021-04-12 21:21:02 +02006504 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6505 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006506 {
6507 if (is_assign)
6508 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006509 if (range)
6510 {
6511 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6512 return FAIL;
6513 }
6514 else
6515 {
6516 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006517
Bram Moolenaar68452172021-04-12 21:21:02 +02006518 if (isn == NULL)
6519 return FAIL;
6520 isn->isn_arg.vartype = dest_type;
6521 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006522 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006523 else if (range)
6524 {
6525 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6526 return FAIL;
6527 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006528 else
6529 {
6530 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6531 return FAIL;
6532 }
6533 }
6534 else
6535 {
6536 emsg(_(e_indexable_type_required));
6537 return FAIL;
6538 }
6539
6540 return OK;
6541}
6542
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006543/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006544 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006545 * "let name"
6546 * "var name = expr"
6547 * "final name = expr"
6548 * "const name = expr"
6549 * "name = expr"
6550 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006551 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006552 * Return NULL for an error.
6553 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006554 */
6555 static char_u *
6556compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6557{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006558 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006559 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006560 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006561 char_u *ret = NULL;
6562 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006563 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006565 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006566 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006567 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006568 int oplen = 0;
6569 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006570 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006571 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006573 int is_decl = is_decl_command(cmdidx);
6574 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006575 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006576
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006577 // Skip over the "var" or "[var, var]" to get to any "=".
6578 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6579 if (p == NULL)
6580 return *arg == '[' ? arg : NULL;
6581
6582 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006584 // TODO: should we allow this, and figure out type inference from list
6585 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006586 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 return NULL;
6588 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006589 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591 sp = p;
6592 p = skipwhite(p);
6593 op = p;
6594 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006595
6596 if (var_count > 0 && oplen == 0)
6597 // can be something like "[1, 2]->func()"
6598 return arg;
6599
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006600 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006602 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006603 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006604 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006605 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6606 {
6607 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6608 oplen = 2;
6609 incdec = TRUE;
6610 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006612 if (heredoc)
6613 {
6614 list_T *l;
6615 listitem_T *li;
6616
6617 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006618 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006620 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006621 if (l == NULL)
6622 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006623
Bram Moolenaar078269b2020-09-21 20:35:55 +02006624 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006626 // Push each line and the create the list.
6627 FOR_ALL_LIST_ITEMS(l, li)
6628 {
6629 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6630 li->li_tv.vval.v_string = NULL;
6631 }
6632 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006634 list_free(l);
6635 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006636 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006638 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006640 char_u *wp;
6641
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006642 // for "[var, var] = expr" evaluate the expression here, loop over the
6643 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006644 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006645
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006646 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006647 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006648 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006649 if (compile_expr0(&p, cctx) == FAIL)
6650 return NULL;
6651 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006652
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006653 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006655 type_T *stacktype;
6656
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006657 stacktype = stack->ga_len == 0 ? &t_void
6658 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006659 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006661 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006662 goto theend;
6663 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006664 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006665 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006666 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006667 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006668 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6669 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006670 if (stacktype->tt_member != NULL)
6671 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006672 }
6673 }
6674
6675 /*
6676 * Loop over variables in "[var, var] = expr".
6677 * For "var = expr" and "let var: type" this is done only once.
6678 */
6679 if (var_count > 0)
6680 var_start = skipwhite(arg + 1); // skip over the "["
6681 else
6682 var_start = arg;
6683 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6684 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006685 int instr_count = -1;
6686
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006687 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6688 {
6689 // Ignore underscore in "[a, _, b] = list".
6690 if (var_count > 0)
6691 {
6692 var_start = skipwhite(var_start + 2);
6693 continue;
6694 }
6695 emsg(_(e_cannot_use_underscore_here));
6696 goto theend;
6697 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006698 vim_free(lhs.lhs_name);
6699
6700 /*
6701 * Figure out the LHS type and other properties.
6702 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006703 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6704 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006705 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006706 if (!heredoc)
6707 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006708 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006709 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006710 if (oplen > 0 && var_count == 0)
6711 {
6712 // skip over the "=" and the expression
6713 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006714 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006715 }
6716 }
6717 else if (oplen > 0)
6718 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006719 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006720 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006721
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006722 // For "var = expr" evaluate the expression.
6723 if (var_count == 0)
6724 {
6725 int r;
6726
6727 // for "+=", "*=", "..=" etc. first load the current value
6728 if (*op != '=')
6729 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006730 if (compile_load_lhs_with_index(&lhs, var_start,
6731 cctx) == FAIL)
6732 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006733 }
6734
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006735 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006736 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006737 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006738 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006739 r = generate_PUSHNR(cctx, 1);
6740 }
6741 else
6742 {
6743 // Temporarily hide the new local variable here, it is
6744 // not available to this expression.
6745 if (lhs.lhs_new_local)
6746 --cctx->ctx_locals.ga_len;
6747 wp = op + oplen;
6748 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6749 {
6750 if (lhs.lhs_new_local)
6751 ++cctx->ctx_locals.ga_len;
6752 goto theend;
6753 }
6754 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006755 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006756 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006757 if (r == FAIL)
6758 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006759 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006760 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006761 else if (semicolon && var_idx == var_count - 1)
6762 {
6763 // For "[var; var] = expr" get the rest of the list
6764 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6765 goto theend;
6766 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006767 else
6768 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006769 // For "[var, var] = expr" get the "var_idx" item from the
6770 // list.
6771 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006772 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006773 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006774
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006775 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006776 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006777 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006778 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006779 if ((rhs_type->tt_type == VAR_FUNC
6780 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006781 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006782 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006783 goto theend;
6784
Bram Moolenaar752fc692021-01-04 21:57:11 +01006785 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006786 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006787 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006788 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006789 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006790 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006791 }
6792 else
6793 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006794 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006795 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006796 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006797 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006798 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006799 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006800 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006801 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006802 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006803 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006804 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006805 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006806 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006807 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006808 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006809
Bram Moolenaar77709b12021-04-03 21:01:01 +02006810 // Without operator check type here, otherwise below.
6811 // Use the line number of the assignment.
6812 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006813 if (lhs.lhs_has_index)
6814 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006815 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006816 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006817 goto theend;
6818 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006819 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006820 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006821 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006822 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006823 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006824 else if (cmdidx == CMD_final)
6825 {
6826 emsg(_(e_final_requires_a_value));
6827 goto theend;
6828 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006829 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006830 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006831 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006832 goto theend;
6833 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006834 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006835 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006836 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006837 goto theend;
6838 }
6839 else
6840 {
6841 // variables are always initialized
6842 if (ga_grow(instr, 1) == FAIL)
6843 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006844 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006845 {
6846 case VAR_BOOL:
6847 generate_PUSHBOOL(cctx, VVAL_FALSE);
6848 break;
6849 case VAR_FLOAT:
6850#ifdef FEAT_FLOAT
6851 generate_PUSHF(cctx, 0.0);
6852#endif
6853 break;
6854 case VAR_STRING:
6855 generate_PUSHS(cctx, NULL);
6856 break;
6857 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006858 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006859 break;
6860 case VAR_FUNC:
6861 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6862 break;
6863 case VAR_LIST:
6864 generate_NEWLIST(cctx, 0);
6865 break;
6866 case VAR_DICT:
6867 generate_NEWDICT(cctx, 0);
6868 break;
6869 case VAR_JOB:
6870 generate_PUSHJOB(cctx, NULL);
6871 break;
6872 case VAR_CHANNEL:
6873 generate_PUSHCHANNEL(cctx, NULL);
6874 break;
6875 case VAR_NUMBER:
6876 case VAR_UNKNOWN:
6877 case VAR_ANY:
6878 case VAR_PARTIAL:
6879 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006880 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006881 case VAR_SPECIAL: // cannot happen
6882 generate_PUSHNR(cctx, 0);
6883 break;
6884 }
6885 }
6886 if (var_count == 0)
6887 end = p;
6888 }
6889
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006890 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006891 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006892 break;
6893
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006894 if (oplen > 0 && *op != '=')
6895 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006896 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006897 type_T *stacktype;
6898
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006899 if (*op == '.')
6900 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006901 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006902 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006903 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006904 if (
6905#ifdef FEAT_FLOAT
6906 // If variable is float operation with number is OK.
6907 !(expected == &t_float && stacktype == &t_number) &&
6908#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006909 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006910 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006911 goto theend;
6912
6913 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006914 {
6915 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6916 goto theend;
6917 }
6918 else if (*op == '+')
6919 {
6920 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006921 operator_type(lhs.lhs_member_type, stacktype),
6922 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006923 goto theend;
6924 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006925 else if (generate_two_op(cctx, op) == FAIL)
6926 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006928
Bram Moolenaar752fc692021-01-04 21:57:11 +01006929 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006930 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006931 // Use the info in "lhs" to store the value at the index in the
6932 // list or dict.
6933 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6934 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006935 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006936 }
6937 else
6938 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006939 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006940 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006941 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006942 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006943 generate_LOCKCONST(cctx);
6944
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006945 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006946 && (lhs.lhs_type->tt_type == VAR_DICT
6947 || lhs.lhs_type->tt_type == VAR_LIST)
6948 && lhs.lhs_type->tt_member != NULL
6949 && lhs.lhs_type->tt_member != &t_any
6950 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006951 // Set the type in the list or dict, so that it can be checked,
6952 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006953 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006954
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006955 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6956 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006957 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006958
6959 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006960 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006961 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006962
6963 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006964 if (var_count > 0 && !semicolon)
6965 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006966 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006967 goto theend;
6968 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006969
Bram Moolenaarb2097502020-07-19 17:17:02 +02006970 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971
6972theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006973 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974 return ret;
6975}
6976
6977/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006978 * Check for an assignment at "eap->cmd", compile it if found.
6979 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6980 */
6981 static int
6982may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6983{
6984 char_u *pskip;
6985 char_u *p;
6986
6987 // Assuming the command starts with a variable or function name,
6988 // find what follows.
6989 // Skip over "var.member", "var[idx]" and the like.
6990 // Also "&opt = val", "$ENV = val" and "@r = val".
6991 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6992 ? eap->cmd + 1 : eap->cmd;
6993 p = to_name_end(pskip, TRUE);
6994 if (p > eap->cmd && *p != NUL)
6995 {
6996 char_u *var_end;
6997 int oplen;
6998 int heredoc;
6999
7000 if (eap->cmd[0] == '@')
7001 var_end = eap->cmd + 2;
7002 else
7003 var_end = find_name_end(pskip, NULL, NULL,
7004 FNE_CHECK_START | FNE_INCL_BR);
7005 oplen = assignment_len(skipwhite(var_end), &heredoc);
7006 if (oplen > 0)
7007 {
7008 size_t len = p - eap->cmd;
7009
7010 // Recognize an assignment if we recognize the variable
7011 // name:
7012 // "g:var = expr"
7013 // "local = expr" where "local" is a local var.
7014 // "script = expr" where "script" is a script-local var.
7015 // "import = expr" where "import" is an imported var
7016 // "&opt = expr"
7017 // "$ENV = expr"
7018 // "@r = expr"
7019 if (*eap->cmd == '&'
7020 || *eap->cmd == '$'
7021 || *eap->cmd == '@'
7022 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007023 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007024 {
7025 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7026 if (*line == NULL || *line == eap->cmd)
7027 return FAIL;
7028 return OK;
7029 }
7030 }
7031 }
7032
7033 if (*eap->cmd == '[')
7034 {
7035 // [var, var] = expr
7036 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7037 if (*line == NULL)
7038 return FAIL;
7039 if (*line != eap->cmd)
7040 return OK;
7041 }
7042 return NOTDONE;
7043}
7044
7045/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007046 * Check if "name" can be "unlet".
7047 */
7048 int
7049check_vim9_unlet(char_u *name)
7050{
7051 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7052 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007053 // "unlet s:var" is allowed in legacy script.
7054 if (*name == 's' && !script_is_vim9())
7055 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007056 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007057 return FAIL;
7058 }
7059 return OK;
7060}
7061
7062/*
7063 * Callback passed to ex_unletlock().
7064 */
7065 static int
7066compile_unlet(
7067 lval_T *lvp,
7068 char_u *name_end,
7069 exarg_T *eap,
7070 int deep UNUSED,
7071 void *coookie)
7072{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007073 cctx_T *cctx = coookie;
7074 char_u *p = lvp->ll_name;
7075 int cc = *name_end;
7076 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007077
Bram Moolenaar752fc692021-01-04 21:57:11 +01007078 if (cctx->ctx_skip == SKIP_YES)
7079 return OK;
7080
7081 *name_end = NUL;
7082 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007083 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007084 // :unlet $ENV_VAR
7085 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7086 }
7087 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7088 {
7089 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007090
Bram Moolenaar752fc692021-01-04 21:57:11 +01007091 // This is similar to assigning: lookup the list/dict, compile the
7092 // idx/key. Then instead of storing the value unlet the item.
7093 // unlet {list}[idx]
7094 // unlet {dict}[key] dict.key
7095 //
7096 // Figure out the LHS type and other properties.
7097 //
7098 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7099
7100 // : unlet an indexed item
7101 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007102 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007103 iemsg("called compile_lhs() without an index");
7104 ret = FAIL;
7105 }
7106 else
7107 {
7108 // Use the info in "lhs" to unlet the item at the index in the
7109 // list or dict.
7110 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007111 }
7112
Bram Moolenaar752fc692021-01-04 21:57:11 +01007113 vim_free(lhs.lhs_name);
7114 }
7115 else if (check_vim9_unlet(p) == FAIL)
7116 {
7117 ret = FAIL;
7118 }
7119 else
7120 {
7121 // Normal name. Only supports g:, w:, t: and b: namespaces.
7122 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007123 }
7124
Bram Moolenaar752fc692021-01-04 21:57:11 +01007125 *name_end = cc;
7126 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007127}
7128
7129/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007130 * Callback passed to ex_unletlock().
7131 */
7132 static int
7133compile_lock_unlock(
7134 lval_T *lvp,
7135 char_u *name_end,
7136 exarg_T *eap,
7137 int deep UNUSED,
7138 void *coookie)
7139{
7140 cctx_T *cctx = coookie;
7141 int cc = *name_end;
7142 char_u *p = lvp->ll_name;
7143 int ret = OK;
7144 size_t len;
7145 char_u *buf;
7146
7147 if (cctx->ctx_skip == SKIP_YES)
7148 return OK;
7149
7150 // Cannot use :lockvar and :unlockvar on local variables.
7151 if (p[1] != ':')
7152 {
7153 char_u *end = skip_var_one(p, FALSE);
7154
7155 if (lookup_local(p, end - p, NULL, cctx) == OK)
7156 {
7157 emsg(_(e_cannot_lock_unlock_local_variable));
7158 return FAIL;
7159 }
7160 }
7161
7162 // Checking is done at runtime.
7163 *name_end = NUL;
7164 len = name_end - p + 20;
7165 buf = alloc(len);
7166 if (buf == NULL)
7167 ret = FAIL;
7168 else
7169 {
7170 vim_snprintf((char *)buf, len, "%s %s",
7171 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7172 p);
7173 ret = generate_EXEC(cctx, buf);
7174
7175 vim_free(buf);
7176 *name_end = cc;
7177 }
7178 return ret;
7179}
7180
7181/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007182 * compile "unlet var", "lock var" and "unlock var"
7183 * "arg" points to "var".
7184 */
7185 static char_u *
7186compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7187{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007188 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7189 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7190 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007191 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7192}
7193
7194/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 * Compile an :import command.
7196 */
7197 static char_u *
7198compile_import(char_u *arg, cctx_T *cctx)
7199{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007200 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007201}
7202
7203/*
7204 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7205 */
7206 static int
7207compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7208{
7209 garray_T *instr = &cctx->ctx_instr;
7210 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7211
7212 if (endlabel == NULL)
7213 return FAIL;
7214 endlabel->el_next = *el;
7215 *el = endlabel;
7216 endlabel->el_end_label = instr->ga_len;
7217
7218 generate_JUMP(cctx, when, 0);
7219 return OK;
7220}
7221
7222 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007223compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007224{
7225 garray_T *instr = &cctx->ctx_instr;
7226
7227 while (*el != NULL)
7228 {
7229 endlabel_T *cur = (*el);
7230 isn_T *isn;
7231
7232 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007233 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234 *el = cur->el_next;
7235 vim_free(cur);
7236 }
7237}
7238
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007239 static void
7240compile_free_jump_to_end(endlabel_T **el)
7241{
7242 while (*el != NULL)
7243 {
7244 endlabel_T *cur = (*el);
7245
7246 *el = cur->el_next;
7247 vim_free(cur);
7248 }
7249}
7250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007251/*
7252 * Create a new scope and set up the generic items.
7253 */
7254 static scope_T *
7255new_scope(cctx_T *cctx, scopetype_T type)
7256{
7257 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7258
7259 if (scope == NULL)
7260 return NULL;
7261 scope->se_outer = cctx->ctx_scope;
7262 cctx->ctx_scope = scope;
7263 scope->se_type = type;
7264 scope->se_local_count = cctx->ctx_locals.ga_len;
7265 return scope;
7266}
7267
7268/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007269 * Free the current scope and go back to the outer scope.
7270 */
7271 static void
7272drop_scope(cctx_T *cctx)
7273{
7274 scope_T *scope = cctx->ctx_scope;
7275
7276 if (scope == NULL)
7277 {
7278 iemsg("calling drop_scope() without a scope");
7279 return;
7280 }
7281 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007282 switch (scope->se_type)
7283 {
7284 case IF_SCOPE:
7285 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7286 case FOR_SCOPE:
7287 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7288 case WHILE_SCOPE:
7289 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7290 case TRY_SCOPE:
7291 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7292 case NO_SCOPE:
7293 case BLOCK_SCOPE:
7294 break;
7295 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007296 vim_free(scope);
7297}
7298
7299/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007300 * compile "if expr"
7301 *
7302 * "if expr" Produces instructions:
7303 * EVAL expr Push result of "expr"
7304 * JUMP_IF_FALSE end
7305 * ... body ...
7306 * end:
7307 *
7308 * "if expr | else" Produces instructions:
7309 * EVAL expr Push result of "expr"
7310 * JUMP_IF_FALSE else
7311 * ... body ...
7312 * JUMP_ALWAYS end
7313 * else:
7314 * ... body ...
7315 * end:
7316 *
7317 * "if expr1 | elseif expr2 | else" Produces instructions:
7318 * EVAL expr Push result of "expr"
7319 * JUMP_IF_FALSE elseif
7320 * ... body ...
7321 * JUMP_ALWAYS end
7322 * elseif:
7323 * EVAL expr Push result of "expr"
7324 * JUMP_IF_FALSE else
7325 * ... body ...
7326 * JUMP_ALWAYS end
7327 * else:
7328 * ... body ...
7329 * end:
7330 */
7331 static char_u *
7332compile_if(char_u *arg, cctx_T *cctx)
7333{
7334 char_u *p = arg;
7335 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007336 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007338 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007339 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340
Bram Moolenaara5565e42020-05-09 15:44:01 +02007341 CLEAR_FIELD(ppconst);
7342 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007343 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007344 clear_ppconst(&ppconst);
7345 return NULL;
7346 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007347 if (!ends_excmd2(arg, skipwhite(p)))
7348 {
7349 semsg(_(e_trailing_arg), p);
7350 return NULL;
7351 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007352 if (cctx->ctx_skip == SKIP_YES)
7353 clear_ppconst(&ppconst);
7354 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007355 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007356 int error = FALSE;
7357 int v;
7358
Bram Moolenaara5565e42020-05-09 15:44:01 +02007359 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007360 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007361 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007362 if (error)
7363 return NULL;
7364 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007365 }
7366 else
7367 {
7368 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007369 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007370 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007371 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007372 if (bool_on_stack(cctx) == FAIL)
7373 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007374 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375
Bram Moolenaara91a7132021-03-25 21:12:15 +01007376 // CMDMOD_REV must come before the jump
7377 generate_undo_cmdmods(cctx);
7378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 scope = new_scope(cctx, IF_SCOPE);
7380 if (scope == NULL)
7381 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007382 scope->se_skip_save = skip_save;
7383 // "is_had_return" will be reset if any block does not end in :return
7384 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007386 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007387 {
7388 // "where" is set when ":elseif", "else" or ":endif" is found
7389 scope->se_u.se_if.is_if_label = instr->ga_len;
7390 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7391 }
7392 else
7393 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394
Bram Moolenaarced68a02021-01-24 17:53:47 +01007395#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007396 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007397 && skip_save != SKIP_YES)
7398 {
7399 // generated a profile start, need to generate a profile end, since it
7400 // won't be done after returning
7401 cctx->ctx_skip = SKIP_NOT;
7402 generate_instr(cctx, ISN_PROF_END);
7403 cctx->ctx_skip = SKIP_YES;
7404 }
7405#endif
7406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 return p;
7408}
7409
7410 static char_u *
7411compile_elseif(char_u *arg, cctx_T *cctx)
7412{
7413 char_u *p = arg;
7414 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007415 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416 isn_T *isn;
7417 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007418 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007419 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420
7421 if (scope == NULL || scope->se_type != IF_SCOPE)
7422 {
7423 emsg(_(e_elseif_without_if));
7424 return NULL;
7425 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007426 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007427 if (!cctx->ctx_had_return)
7428 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429
Bram Moolenaarced68a02021-01-24 17:53:47 +01007430 if (cctx->ctx_skip == SKIP_NOT)
7431 {
7432 // previous block was executed, this one and following will not
7433 cctx->ctx_skip = SKIP_YES;
7434 scope->se_u.se_if.is_seen_skip_not = TRUE;
7435 }
7436 if (scope->se_u.se_if.is_seen_skip_not)
7437 {
7438 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007439 // Do not count the "elseif" for profiling and cmdmod
7440 instr->ga_len = current_instr_idx(cctx);
7441
Bram Moolenaarced68a02021-01-24 17:53:47 +01007442 skip_expr_cctx(&p, cctx);
7443 return p;
7444 }
7445
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007446 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007447 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007448 int moved_cmdmod = FALSE;
7449
7450 // Move any CMDMOD instruction to after the jump
7451 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7452 {
7453 if (ga_grow(instr, 1) == FAIL)
7454 return NULL;
7455 ((isn_T *)instr->ga_data)[instr->ga_len] =
7456 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7457 --instr->ga_len;
7458 moved_cmdmod = TRUE;
7459 }
7460
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007461 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007463 return NULL;
7464 // previous "if" or "elseif" jumps here
7465 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7466 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007467 if (moved_cmdmod)
7468 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007469 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007470
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007471 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007472 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007473 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007474 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007475 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007476#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007477 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007478 {
7479 // the previous block was skipped, need to profile this line
7480 generate_instr(cctx, ISN_PROF_START);
7481 instr_count = instr->ga_len;
7482 }
7483#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007484 if (cctx->ctx_compile_type == CT_DEBUG)
7485 {
7486 // the previous block was skipped, may want to debug this line
7487 generate_instr(cctx, ISN_DEBUG);
7488 instr_count = instr->ga_len;
7489 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007490 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007491 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007492 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007493 clear_ppconst(&ppconst);
7494 return NULL;
7495 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007496 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007497 if (!ends_excmd2(arg, skipwhite(p)))
7498 {
7499 semsg(_(e_trailing_arg), p);
7500 return NULL;
7501 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007502 if (scope->se_skip_save == SKIP_YES)
7503 clear_ppconst(&ppconst);
7504 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007505 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007506 int error = FALSE;
7507 int v;
7508
Bram Moolenaar7f141552020-05-09 17:35:53 +02007509 // The expression results in a constant.
7510 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007511 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7512 if (error)
7513 return NULL;
7514 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007515 clear_ppconst(&ppconst);
7516 scope->se_u.se_if.is_if_label = -1;
7517 }
7518 else
7519 {
7520 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007521 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007522 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007523 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007524 if (bool_on_stack(cctx) == FAIL)
7525 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007526
Bram Moolenaara91a7132021-03-25 21:12:15 +01007527 // CMDMOD_REV must come before the jump
7528 generate_undo_cmdmods(cctx);
7529
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007530 // "where" is set when ":elseif", "else" or ":endif" is found
7531 scope->se_u.se_if.is_if_label = instr->ga_len;
7532 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7533 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007534
7535 return p;
7536}
7537
7538 static char_u *
7539compile_else(char_u *arg, cctx_T *cctx)
7540{
7541 char_u *p = arg;
7542 garray_T *instr = &cctx->ctx_instr;
7543 isn_T *isn;
7544 scope_T *scope = cctx->ctx_scope;
7545
7546 if (scope == NULL || scope->se_type != IF_SCOPE)
7547 {
7548 emsg(_(e_else_without_if));
7549 return NULL;
7550 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007551 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007552 if (!cctx->ctx_had_return)
7553 scope->se_u.se_if.is_had_return = FALSE;
7554 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007555
Bram Moolenaarced68a02021-01-24 17:53:47 +01007556#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007557 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007558 {
7559 if (cctx->ctx_skip == SKIP_NOT
7560 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7561 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007562 // the previous block was executed, do not count "else" for
7563 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007564 --instr->ga_len;
7565 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7566 {
7567 // the previous block was not executed, this one will, do count the
7568 // "else" for profiling
7569 cctx->ctx_skip = SKIP_NOT;
7570 generate_instr(cctx, ISN_PROF_END);
7571 generate_instr(cctx, ISN_PROF_START);
7572 cctx->ctx_skip = SKIP_YES;
7573 }
7574 }
7575#endif
7576
7577 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007578 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007579 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007580 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007581 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007582 if (!cctx->ctx_had_return
7583 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7584 JUMP_ALWAYS, cctx) == FAIL)
7585 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007586 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007587
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007588 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007589 {
7590 if (scope->se_u.se_if.is_if_label >= 0)
7591 {
7592 // previous "if" or "elseif" jumps here
7593 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7594 isn->isn_arg.jump.jump_where = instr->ga_len;
7595 scope->se_u.se_if.is_if_label = -1;
7596 }
7597 }
7598
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007599 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007600 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602
7603 return p;
7604}
7605
7606 static char_u *
7607compile_endif(char_u *arg, cctx_T *cctx)
7608{
7609 scope_T *scope = cctx->ctx_scope;
7610 ifscope_T *ifscope;
7611 garray_T *instr = &cctx->ctx_instr;
7612 isn_T *isn;
7613
Bram Moolenaarfa984412021-03-25 22:15:28 +01007614 if (misplaced_cmdmod(cctx))
7615 return NULL;
7616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617 if (scope == NULL || scope->se_type != IF_SCOPE)
7618 {
7619 emsg(_(e_endif_without_if));
7620 return NULL;
7621 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007622 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007623 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007624 if (!cctx->ctx_had_return)
7625 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007627 if (scope->se_u.se_if.is_if_label >= 0)
7628 {
7629 // previous "if" or "elseif" jumps here
7630 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7631 isn->isn_arg.jump.jump_where = instr->ga_len;
7632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007633 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007634 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007635
7636#ifdef FEAT_PROFILE
7637 // even when skipping we count the endif as executed, unless the block it's
7638 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007639 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007640 && scope->se_skip_save != SKIP_YES)
7641 {
7642 cctx->ctx_skip = SKIP_NOT;
7643 generate_instr(cctx, ISN_PROF_START);
7644 }
7645#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007646 cctx->ctx_skip = scope->se_skip_save;
7647
7648 // If all the blocks end in :return and there is an :else then the
7649 // had_return flag is set.
7650 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007652 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007653 return arg;
7654}
7655
7656/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007657 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007658 *
7659 * Produces instructions:
7660 * PUSHNR -1
7661 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007662 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007663 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7664 * - if beyond end, jump to "end"
7665 * - otherwise get item from list and push it
7666 * STORE var Store item in "var"
7667 * ... body ...
7668 * JUMP top Jump back to repeat
7669 * end: DROP Drop the result of "expr"
7670 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007671 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7672 * UNPACK 2 Split item in 2
7673 * STORE var1 Store item in "var1"
7674 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007675 */
7676 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007677compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007678{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007679 char_u *arg;
7680 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007681 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007682 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007683 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007684 int var_count = 0;
7685 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007686 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007687 garray_T *stack = &cctx->ctx_type_stack;
7688 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007689 lvar_T *loop_lvar; // loop iteration variable
7690 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007691 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007692 type_T *item_type = &t_any;
7693 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694
Bram Moolenaar792f7862020-11-23 08:31:18 +01007695 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007696 if (p == NULL)
7697 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007698 if (var_count == 0)
7699 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700
7701 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007702 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007703 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7704 return NULL;
7705 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007706 {
7707 emsg(_(e_missing_in));
7708 return NULL;
7709 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007710 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007711 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7712 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 scope = new_scope(cctx, FOR_SCOPE);
7715 if (scope == NULL)
7716 return NULL;
7717
Bram Moolenaar792f7862020-11-23 08:31:18 +01007718 // Reserve a variable to store the loop iteration counter and initialize it
7719 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007720 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7721 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007722 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007723 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007724 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007726 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007727 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007728
7729 // compile "expr", it remains on the stack until "endfor"
7730 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007731 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007732 {
7733 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007734 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007735 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007736 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007738 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007739 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007741 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007742 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007743 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007744 semsg(_(e_for_loop_on_str_not_supported),
7745 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007746 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007747 return NULL;
7748 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007749
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007750 if (vartype->tt_type == VAR_STRING)
7751 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007752 else if (vartype->tt_type == VAR_BLOB)
7753 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007754 else if (vartype->tt_type == VAR_LIST
7755 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007756 {
7757 if (var_count == 1)
7758 item_type = vartype->tt_member;
7759 else if (vartype->tt_member->tt_type == VAR_LIST
7760 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007761 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007762 item_type = vartype->tt_member->tt_member;
7763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007764
Bram Moolenaara91a7132021-03-25 21:12:15 +01007765 // CMDMOD_REV must come before the FOR instruction
7766 generate_undo_cmdmods(cctx);
7767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007768 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007769 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007770 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771
Bram Moolenaar792f7862020-11-23 08:31:18 +01007772 arg = arg_start;
7773 if (var_count > 1)
7774 {
7775 generate_UNPACK(cctx, var_count, semicolon);
7776 arg = skipwhite(arg + 1); // skip white after '['
7777
7778 // the list item is replaced by a number of items
7779 if (ga_grow(stack, var_count - 1) == FAIL)
7780 {
7781 drop_scope(cctx);
7782 return NULL;
7783 }
7784 --stack->ga_len;
7785 for (idx = 0; idx < var_count; ++idx)
7786 {
7787 ((type_T **)stack->ga_data)[stack->ga_len] =
7788 (semicolon && idx == 0) ? vartype : item_type;
7789 ++stack->ga_len;
7790 }
7791 }
7792
7793 for (idx = 0; idx < var_count; ++idx)
7794 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007795 assign_dest_T dest = dest_local;
7796 int opt_flags = 0;
7797 int vimvaridx = -1;
7798 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007799 type_T *lhs_type = &t_any;
7800 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007801
7802 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007803 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007804 name = vim_strnsave(arg, varlen);
7805 if (name == NULL)
7806 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007807 if (*p == ':')
7808 {
7809 p = skipwhite(p + 1);
7810 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7811 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007812
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007813 // TODO: script var not supported?
7814 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7815 &vimvaridx, &type, cctx) == FAIL)
7816 goto failed;
7817 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007818 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007819 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7820 0, 0, type, name) == FAIL)
7821 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007822 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007823 else if (varlen == 1 && *arg == '_')
7824 {
7825 // Assigning to "_": drop the value.
7826 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7827 goto failed;
7828 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007829 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007830 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007831 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007832 {
7833 semsg(_(e_variable_already_declared), arg);
7834 goto failed;
7835 }
7836
Bram Moolenaarea870692020-12-02 14:24:30 +01007837 if (STRNCMP(name, "s:", 2) == 0)
7838 {
7839 semsg(_(e_cannot_declare_script_variable_in_function), name);
7840 goto failed;
7841 }
7842
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007843 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007844 where.wt_index = var_count > 1 ? idx + 1 : 0;
7845 where.wt_variable = TRUE;
7846 if (lhs_type == &t_any)
7847 lhs_type = item_type;
7848 else if (item_type != &t_unknown
7849 && !(var_count > 1 && item_type == &t_any)
7850 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7851 goto failed;
7852 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007853 if (var_lvar == NULL)
7854 // out of memory or used as an argument
7855 goto failed;
7856
7857 if (semicolon && idx == var_count - 1)
7858 var_lvar->lv_type = vartype;
7859 else
7860 var_lvar->lv_type = item_type;
7861 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7862 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007863
7864 if (*p == ',' || *p == ';')
7865 ++p;
7866 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007867 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007868 }
7869
7870 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007871
7872failed:
7873 vim_free(name);
7874 drop_scope(cctx);
7875 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007876}
7877
7878/*
7879 * compile "endfor"
7880 */
7881 static char_u *
7882compile_endfor(char_u *arg, cctx_T *cctx)
7883{
7884 garray_T *instr = &cctx->ctx_instr;
7885 scope_T *scope = cctx->ctx_scope;
7886 forscope_T *forscope;
7887 isn_T *isn;
7888
Bram Moolenaarfa984412021-03-25 22:15:28 +01007889 if (misplaced_cmdmod(cctx))
7890 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007892 if (scope == NULL || scope->se_type != FOR_SCOPE)
7893 {
7894 emsg(_(e_for));
7895 return NULL;
7896 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007897 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007898 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007899 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007900
7901 // At end of ":for" scope jump back to the FOR instruction.
7902 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7903
7904 // Fill in the "end" label in the FOR statement so it can jump here
7905 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7906 isn->isn_arg.forloop.for_end = instr->ga_len;
7907
7908 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007909 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007910
7911 // Below the ":for" scope drop the "expr" list from the stack.
7912 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7913 return NULL;
7914
7915 vim_free(scope);
7916
7917 return arg;
7918}
7919
7920/*
7921 * compile "while expr"
7922 *
7923 * Produces instructions:
7924 * top: EVAL expr Push result of "expr"
7925 * JUMP_IF_FALSE end jump if false
7926 * ... body ...
7927 * JUMP top Jump back to repeat
7928 * end:
7929 *
7930 */
7931 static char_u *
7932compile_while(char_u *arg, cctx_T *cctx)
7933{
7934 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 scope_T *scope;
7936
7937 scope = new_scope(cctx, WHILE_SCOPE);
7938 if (scope == NULL)
7939 return NULL;
7940
Bram Moolenaara91a7132021-03-25 21:12:15 +01007941 // "endwhile" jumps back here, one before when profiling or using cmdmods
7942 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007943
7944 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007945 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007946 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007947 if (!ends_excmd2(arg, skipwhite(p)))
7948 {
7949 semsg(_(e_trailing_arg), p);
7950 return NULL;
7951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007952
Bram Moolenaar13106602020-10-04 16:06:05 +02007953 if (bool_on_stack(cctx) == FAIL)
7954 return FAIL;
7955
Bram Moolenaara91a7132021-03-25 21:12:15 +01007956 // CMDMOD_REV must come before the jump
7957 generate_undo_cmdmods(cctx);
7958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007959 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007960 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007961 JUMP_IF_FALSE, cctx) == FAIL)
7962 return FAIL;
7963
7964 return p;
7965}
7966
7967/*
7968 * compile "endwhile"
7969 */
7970 static char_u *
7971compile_endwhile(char_u *arg, cctx_T *cctx)
7972{
7973 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007974 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007975
Bram Moolenaarfa984412021-03-25 22:15:28 +01007976 if (misplaced_cmdmod(cctx))
7977 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007978 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7979 {
7980 emsg(_(e_while));
7981 return NULL;
7982 }
7983 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007984 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007985
Bram Moolenaarf002a412021-01-24 13:34:18 +01007986#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007987 // count the endwhile before jumping
7988 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007989#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007991 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007992 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007993
7994 // Fill in the "end" label in the WHILE statement so it can jump here.
7995 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007996 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7997 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007998
7999 vim_free(scope);
8000
8001 return arg;
8002}
8003
8004/*
8005 * compile "continue"
8006 */
8007 static char_u *
8008compile_continue(char_u *arg, cctx_T *cctx)
8009{
8010 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008011 int try_scopes = 0;
8012 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008013
8014 for (;;)
8015 {
8016 if (scope == NULL)
8017 {
8018 emsg(_(e_continue));
8019 return NULL;
8020 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008021 if (scope->se_type == FOR_SCOPE)
8022 {
8023 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008024 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008025 }
8026 if (scope->se_type == WHILE_SCOPE)
8027 {
8028 loop_label = scope->se_u.se_while.ws_top_label;
8029 break;
8030 }
8031 if (scope->se_type == TRY_SCOPE)
8032 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008033 scope = scope->se_outer;
8034 }
8035
Bram Moolenaarc150c092021-02-13 15:02:46 +01008036 if (try_scopes > 0)
8037 // Inside one or more try/catch blocks we first need to jump to the
8038 // "finally" or "endtry" to cleanup.
8039 generate_TRYCONT(cctx, try_scopes, loop_label);
8040 else
8041 // Jump back to the FOR or WHILE instruction.
8042 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008044 return arg;
8045}
8046
8047/*
8048 * compile "break"
8049 */
8050 static char_u *
8051compile_break(char_u *arg, cctx_T *cctx)
8052{
8053 scope_T *scope = cctx->ctx_scope;
8054 endlabel_T **el;
8055
8056 for (;;)
8057 {
8058 if (scope == NULL)
8059 {
8060 emsg(_(e_break));
8061 return NULL;
8062 }
8063 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8064 break;
8065 scope = scope->se_outer;
8066 }
8067
8068 // Jump to the end of the FOR or WHILE loop.
8069 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008070 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008071 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008072 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008073 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8074 return FAIL;
8075
8076 return arg;
8077}
8078
8079/*
8080 * compile "{" start of block
8081 */
8082 static char_u *
8083compile_block(char_u *arg, cctx_T *cctx)
8084{
8085 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8086 return NULL;
8087 return skipwhite(arg + 1);
8088}
8089
8090/*
8091 * compile end of block: drop one scope
8092 */
8093 static void
8094compile_endblock(cctx_T *cctx)
8095{
8096 scope_T *scope = cctx->ctx_scope;
8097
8098 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008099 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008100 vim_free(scope);
8101}
8102
8103/*
8104 * compile "try"
8105 * Creates a new scope for the try-endtry, pointing to the first catch and
8106 * finally.
8107 * Creates another scope for the "try" block itself.
8108 * TRY instruction sets up exception handling at runtime.
8109 *
8110 * "try"
8111 * TRY -> catch1, -> finally push trystack entry
8112 * ... try block
8113 * "throw {exception}"
8114 * EVAL {exception}
8115 * THROW create exception
8116 * ... try block
8117 * " catch {expr}"
8118 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008119 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008120 * EVAL {expr}
8121 * MATCH
8122 * JUMP nomatch -> catch2
8123 * CATCH remove exception
8124 * ... catch block
8125 * " catch"
8126 * JUMP -> finally
8127 * catch2: CATCH remove exception
8128 * ... catch block
8129 * " finally"
8130 * finally:
8131 * ... finally block
8132 * " endtry"
8133 * ENDTRY pop trystack entry, may rethrow
8134 */
8135 static char_u *
8136compile_try(char_u *arg, cctx_T *cctx)
8137{
8138 garray_T *instr = &cctx->ctx_instr;
8139 scope_T *try_scope;
8140 scope_T *scope;
8141
Bram Moolenaarfa984412021-03-25 22:15:28 +01008142 if (misplaced_cmdmod(cctx))
8143 return NULL;
8144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008145 // scope that holds the jumps that go to catch/finally/endtry
8146 try_scope = new_scope(cctx, TRY_SCOPE);
8147 if (try_scope == NULL)
8148 return NULL;
8149
Bram Moolenaar69f70502021-01-01 16:10:46 +01008150 if (cctx->ctx_skip != SKIP_YES)
8151 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008152 isn_T *isn;
8153
8154 // "try_catch" is set when the first ":catch" is found or when no catch
8155 // is found and ":finally" is found.
8156 // "try_finally" is set when ":finally" is found
8157 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008158 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008159 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8160 return NULL;
8161 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8162 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008163 return NULL;
8164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008165
8166 // scope for the try block itself
8167 scope = new_scope(cctx, BLOCK_SCOPE);
8168 if (scope == NULL)
8169 return NULL;
8170
8171 return arg;
8172}
8173
8174/*
8175 * compile "catch {expr}"
8176 */
8177 static char_u *
8178compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8179{
8180 scope_T *scope = cctx->ctx_scope;
8181 garray_T *instr = &cctx->ctx_instr;
8182 char_u *p;
8183 isn_T *isn;
8184
Bram Moolenaarfa984412021-03-25 22:15:28 +01008185 if (misplaced_cmdmod(cctx))
8186 return NULL;
8187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188 // end block scope from :try or :catch
8189 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8190 compile_endblock(cctx);
8191 scope = cctx->ctx_scope;
8192
8193 // Error if not in a :try scope
8194 if (scope == NULL || scope->se_type != TRY_SCOPE)
8195 {
8196 emsg(_(e_catch));
8197 return NULL;
8198 }
8199
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008200 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008201 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008202 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008203 return NULL;
8204 }
8205
Bram Moolenaar69f70502021-01-01 16:10:46 +01008206 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008207 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008208#ifdef FEAT_PROFILE
8209 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008210 if (cctx->ctx_compile_type == CT_PROFILE
8211 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008212 .isn_type == ISN_PROF_START)
8213 --instr->ga_len;
8214#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008215 // Jump from end of previous block to :finally or :endtry
8216 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8217 JUMP_ALWAYS, cctx) == FAIL)
8218 return NULL;
8219
8220 // End :try or :catch scope: set value in ISN_TRY instruction
8221 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008222 if (isn->isn_arg.try.try_ref->try_catch == 0)
8223 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008224 if (scope->se_u.se_try.ts_catch_label != 0)
8225 {
8226 // Previous catch without match jumps here
8227 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8228 isn->isn_arg.jump.jump_where = instr->ga_len;
8229 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008230#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008231 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008232 {
8233 // a "throw" that jumps here needs to be counted
8234 generate_instr(cctx, ISN_PROF_END);
8235 // the "catch" is also counted
8236 generate_instr(cctx, ISN_PROF_START);
8237 }
8238#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008239 if (cctx->ctx_compile_type == CT_DEBUG)
8240 generate_instr(cctx, ISN_DEBUG);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008241 }
8242
8243 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008244 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008245 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008246 scope->se_u.se_try.ts_caught_all = TRUE;
8247 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008248 }
8249 else
8250 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008251 char_u *end;
8252 char_u *pat;
8253 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008254 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008255 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008257 // Push v:exception, push {expr} and MATCH
8258 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8259
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008260 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008261 if (*end != *p)
8262 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008263 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008264 vim_free(tofree);
8265 return FAIL;
8266 }
8267 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008268 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008269 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008270 len = (int)(end - tofree);
8271 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008272 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008273 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008274 if (pat == NULL)
8275 return FAIL;
8276 if (generate_PUSHS(cctx, pat) == FAIL)
8277 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008278
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008279 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8280 return NULL;
8281
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008282 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008283 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8284 return NULL;
8285 }
8286
Bram Moolenaar69f70502021-01-01 16:10:46 +01008287 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008288 return NULL;
8289
8290 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8291 return NULL;
8292 return p;
8293}
8294
8295 static char_u *
8296compile_finally(char_u *arg, cctx_T *cctx)
8297{
8298 scope_T *scope = cctx->ctx_scope;
8299 garray_T *instr = &cctx->ctx_instr;
8300 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008301 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008302
Bram Moolenaarfa984412021-03-25 22:15:28 +01008303 if (misplaced_cmdmod(cctx))
8304 return NULL;
8305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008306 // end block scope from :try or :catch
8307 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8308 compile_endblock(cctx);
8309 scope = cctx->ctx_scope;
8310
8311 // Error if not in a :try scope
8312 if (scope == NULL || scope->se_type != TRY_SCOPE)
8313 {
8314 emsg(_(e_finally));
8315 return NULL;
8316 }
8317
8318 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008319 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008320 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008321 {
8322 emsg(_(e_finally_dup));
8323 return NULL;
8324 }
8325
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008326 this_instr = instr->ga_len;
8327#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008328 if (cctx->ctx_compile_type == CT_PROFILE
8329 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008330 .isn_type == ISN_PROF_START)
8331 // jump to the profile start of the "finally"
8332 --this_instr;
8333#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008334
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008335 // Fill in the "end" label in jumps at the end of the blocks.
8336 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8337 this_instr, cctx);
8338
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008339 // If there is no :catch then an exception jumps to :finally.
8340 if (isn->isn_arg.try.try_ref->try_catch == 0)
8341 isn->isn_arg.try.try_ref->try_catch = this_instr;
8342 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008343 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008344 {
8345 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008346 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008347 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008348 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008349 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008350 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8351 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008353 // TODO: set index in ts_finally_label jumps
8354
8355 return arg;
8356}
8357
8358 static char_u *
8359compile_endtry(char_u *arg, cctx_T *cctx)
8360{
8361 scope_T *scope = cctx->ctx_scope;
8362 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008363 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008364
Bram Moolenaarfa984412021-03-25 22:15:28 +01008365 if (misplaced_cmdmod(cctx))
8366 return NULL;
8367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008368 // end block scope from :catch or :finally
8369 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8370 compile_endblock(cctx);
8371 scope = cctx->ctx_scope;
8372
8373 // Error if not in a :try scope
8374 if (scope == NULL || scope->se_type != TRY_SCOPE)
8375 {
8376 if (scope == NULL)
8377 emsg(_(e_no_endtry));
8378 else if (scope->se_type == WHILE_SCOPE)
8379 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008380 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008381 emsg(_(e_endfor));
8382 else
8383 emsg(_(e_endif));
8384 return NULL;
8385 }
8386
Bram Moolenaarc150c092021-02-13 15:02:46 +01008387 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008388 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008389 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008390 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8391 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008392 {
8393 emsg(_(e_missing_catch_or_finally));
8394 return NULL;
8395 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008396
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008397#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008398 if (cctx->ctx_compile_type == CT_PROFILE
8399 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008400 .isn_type == ISN_PROF_START)
8401 // move the profile start after "endtry" so that it's not counted when
8402 // the exception is rethrown.
8403 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008404#endif
8405
Bram Moolenaar69f70502021-01-01 16:10:46 +01008406 // Fill in the "end" label in jumps at the end of the blocks, if not
8407 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008408 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8409 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008410
Bram Moolenaar69f70502021-01-01 16:10:46 +01008411 if (scope->se_u.se_try.ts_catch_label != 0)
8412 {
8413 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008414 isn_T *isn = ((isn_T *)instr->ga_data)
8415 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008416 isn->isn_arg.jump.jump_where = instr->ga_len;
8417 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008418 }
8419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008420 compile_endblock(cctx);
8421
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008422 if (cctx->ctx_skip != SKIP_YES)
8423 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008424 // End :catch or :finally scope: set instruction index in ISN_TRY
8425 // instruction
8426 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008427 if (cctx->ctx_skip != SKIP_YES
8428 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8429 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008430#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008431 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008432 generate_instr(cctx, ISN_PROF_START);
8433#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008435 return arg;
8436}
8437
8438/*
8439 * compile "throw {expr}"
8440 */
8441 static char_u *
8442compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8443{
8444 char_u *p = skipwhite(arg);
8445
Bram Moolenaara5565e42020-05-09 15:44:01 +02008446 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008447 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008448 if (cctx->ctx_skip == SKIP_YES)
8449 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008450 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008451 return NULL;
8452 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8453 return NULL;
8454
8455 return p;
8456}
8457
8458/*
8459 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008460 * compile "echomsg expr"
8461 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008462 * compile "execute expr"
8463 */
8464 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008465compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008466{
8467 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008468 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008469 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008470 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008471 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008472 garray_T *stack = &cctx->ctx_type_stack;
8473 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008474
8475 for (;;)
8476 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008477 if (ends_excmd2(prev, p))
8478 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008479 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008480 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008481 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008482
8483 if (cctx->ctx_skip != SKIP_YES)
8484 {
8485 // check for non-void type
8486 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8487 if (type->tt_type == VAR_VOID)
8488 {
8489 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8490 return NULL;
8491 }
8492 }
8493
Bram Moolenaarad39c092020-02-26 18:23:43 +01008494 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008495 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008496 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008497 }
8498
Bram Moolenaare4984292020-12-13 14:19:25 +01008499 if (count > 0)
8500 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008501 long save_lnum = cctx->ctx_lnum;
8502
8503 // Use the line number where the command started.
8504 cctx->ctx_lnum = start_ctx_lnum;
8505
Bram Moolenaare4984292020-12-13 14:19:25 +01008506 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8507 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8508 else if (cmdidx == CMD_execute)
8509 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8510 else if (cmdidx == CMD_echomsg)
8511 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8512 else
8513 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008514
8515 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008517 return p;
8518}
8519
8520/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008521 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008522 * instruction to compute it and return OK.
8523 * Otherwise return FAIL, the caller must deal with any range.
8524 */
8525 static int
8526compile_variable_range(exarg_T *eap, cctx_T *cctx)
8527{
8528 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8529 char_u *p = skipdigits(eap->cmd);
8530
8531 if (p == range_end)
8532 return FAIL;
8533 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8534}
8535
8536/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008537 * :put r
8538 * :put ={expr}
8539 */
8540 static char_u *
8541compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8542{
8543 char_u *line = arg;
8544 linenr_T lnum;
8545 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008546 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008547
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008548 eap->regname = *line;
8549
8550 if (eap->regname == '=')
8551 {
8552 char_u *p = line + 1;
8553
8554 if (compile_expr0(&p, cctx) == FAIL)
8555 return NULL;
8556 line = p;
8557 }
8558 else if (eap->regname != NUL)
8559 ++line;
8560
Bram Moolenaar08597872020-12-10 19:43:40 +01008561 if (compile_variable_range(eap, cctx) == OK)
8562 {
8563 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8564 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008565 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008566 {
8567 // Either no range or a number.
8568 // "errormsg" will not be set because the range is ADDR_LINES.
8569 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008570 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008571 return NULL;
8572 if (eap->addr_count == 0)
8573 lnum = -1;
8574 else
8575 lnum = eap->line2;
8576 if (above)
8577 --lnum;
8578 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008579
8580 generate_PUT(cctx, eap->regname, lnum);
8581 return line;
8582}
8583
8584/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008585 * A command that is not compiled, execute with legacy code.
8586 */
8587 static char_u *
8588compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8589{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008590 char_u *p;
8591 int has_expr = FALSE;
8592 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008593
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008594 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008595 goto theend;
8596
Bram Moolenaare729ce22021-06-06 21:38:09 +02008597 // If there was a prececing command modifier, drop it and include it in the
8598 // EXEC command.
8599 if (cctx->ctx_has_cmdmod)
8600 {
8601 garray_T *instr = &cctx->ctx_instr;
8602 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8603
8604 if (isn->isn_type == ISN_CMDMOD)
8605 {
8606 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8607 ->cmod_filter_regmatch.regprog);
8608 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8609 --instr->ga_len;
8610 cctx->ctx_has_cmdmod = FALSE;
8611 }
8612 }
8613
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008614 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008615 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008616 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008617 int usefilter = FALSE;
8618
8619 has_expr = argt & (EX_XFILE | EX_EXPAND);
8620
8621 // If the command can be followed by a bar, find the bar and truncate
8622 // it, so that the following command can be compiled.
8623 // The '|' is overwritten with a NUL, it is put back below.
8624 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8625 && *eap->arg == '!')
8626 // :w !filter or :r !filter or :r! filter
8627 usefilter = TRUE;
8628 if ((argt & EX_TRLBAR) && !usefilter)
8629 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008630 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008631 separate_nextcmd(eap);
8632 if (eap->nextcmd != NULL)
8633 nextcmd = eap->nextcmd;
8634 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008635 else if (eap->cmdidx == CMD_wincmd)
8636 {
8637 p = eap->arg;
8638 if (*p != NUL)
8639 ++p;
8640 if (*p == 'g' || *p == Ctrl_G)
8641 ++p;
8642 p = skipwhite(p);
8643 if (*p == '|')
8644 {
8645 *p = NUL;
8646 nextcmd = p + 1;
8647 }
8648 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008649 }
8650
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008651 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8652 {
8653 // expand filename in "syntax include [@group] filename"
8654 has_expr = TRUE;
8655 eap->arg = skipwhite(eap->arg + 7);
8656 if (*eap->arg == '@')
8657 eap->arg = skiptowhite(eap->arg);
8658 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008659
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008660 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8661 && STRLEN(eap->arg) > 4)
8662 {
8663 int delim = *eap->arg;
8664
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008665 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008666 if (*p == delim)
8667 {
8668 eap->arg = p + 1;
8669 has_expr = TRUE;
8670 }
8671 }
8672
Bram Moolenaarecac5912021-01-05 19:23:28 +01008673 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8674 {
8675 // TODO: should only expand when appropriate for the command
8676 eap->arg = skiptowhite(eap->arg);
8677 has_expr = TRUE;
8678 }
8679
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008680 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008681 {
8682 int count = 0;
8683 char_u *start = skipwhite(line);
8684
8685 // :cmd xxx`=expr1`yyy`=expr2`zzz
8686 // PUSHS ":cmd xxx"
8687 // eval expr1
8688 // PUSHS "yyy"
8689 // eval expr2
8690 // PUSHS "zzz"
8691 // EXECCONCAT 5
8692 for (;;)
8693 {
8694 if (p > start)
8695 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008696 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008697 ++count;
8698 }
8699 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008700 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008701 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008702 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008703 ++count;
8704 p = skipwhite(p);
8705 if (*p != '`')
8706 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008707 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008708 return NULL;
8709 }
8710 start = p + 1;
8711
8712 p = (char_u *)strstr((char *)start, "`=");
8713 if (p == NULL)
8714 {
8715 if (*skipwhite(start) != NUL)
8716 {
8717 generate_PUSHS(cctx, vim_strsave(start));
8718 ++count;
8719 }
8720 break;
8721 }
8722 }
8723 generate_EXECCONCAT(cctx, count);
8724 }
8725 else
8726 generate_EXEC(cctx, line);
8727
8728theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008729 if (*nextcmd != NUL)
8730 {
8731 // the parser expects a pointer to the bar, put it back
8732 --nextcmd;
8733 *nextcmd = '|';
8734 }
8735
8736 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008737}
8738
Bram Moolenaar20677332021-06-06 17:02:53 +02008739/*
8740 * A script command with heredoc, e.g.
8741 * ruby << EOF
8742 * command
8743 * EOF
8744 * Has been turned into one long line with NL characters by
8745 * get_function_body():
8746 * ruby << EOF<NL> command<NL>EOF
8747 */
8748 static char_u *
8749compile_script(char_u *line, cctx_T *cctx)
8750{
8751 if (cctx->ctx_skip != SKIP_YES)
8752 {
8753 isn_T *isn;
8754
8755 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8756 return NULL;
8757 isn->isn_arg.string = vim_strsave(line);
8758 }
8759 return (char_u *)"";
8760}
8761
Bram Moolenaar8238f082021-04-20 21:10:48 +02008762
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008763/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008764 * :s/pat/repl/
8765 */
8766 static char_u *
8767compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8768{
8769 char_u *cmd = eap->arg;
8770 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8771
8772 if (expr != NULL)
8773 {
8774 int delimiter = *cmd++;
8775
8776 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008777 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008778 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8779 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008780 garray_T save_ga = cctx->ctx_instr;
8781 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008782 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008783 int trailing_error;
8784 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008785 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008786 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008787
8788 cmd += 3;
8789 end = skip_substitute(cmd, delimiter);
8790
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008791 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008792 // labels are correct.
8793 cctx->ctx_instr.ga_len = 0;
8794 cctx->ctx_instr.ga_maxlen = 0;
8795 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008796 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008797 if (end[-1] == NUL)
8798 end[-1] = delimiter;
8799 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008800 trailing_error = *cmd != delimiter && *cmd != NUL;
8801
Bram Moolenaar169502f2021-04-21 12:19:35 +02008802 if (expr_res == FAIL || trailing_error
8803 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008804 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008805 if (trailing_error)
8806 semsg(_(e_trailing_arg), cmd);
8807 clear_instr_ga(&cctx->ctx_instr);
8808 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008809 return NULL;
8810 }
8811
Bram Moolenaar8238f082021-04-20 21:10:48 +02008812 // Move the generated instructions into the ISN_SUBSTITUTE
8813 // instructions, then restore the list of instructions before
8814 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008815 instr_count = cctx->ctx_instr.ga_len;
8816 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008817 instr[instr_count].isn_type = ISN_FINISH;
8818
8819 cctx->ctx_instr = save_ga;
8820 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8821 {
8822 int idx;
8823
8824 for (idx = 0; idx < instr_count; ++idx)
8825 delete_instr(instr + idx);
8826 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008827 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008828 }
8829 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8830 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008831
8832 // skip over flags
8833 if (*end == '&')
8834 ++end;
8835 while (ASCII_ISALPHA(*end) || *end == '#')
8836 ++end;
8837 return end;
8838 }
8839 }
8840
8841 return compile_exec(arg, eap, cctx);
8842}
8843
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008844 static char_u *
8845compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8846{
8847 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008848 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008849
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008850 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008851 {
8852 if (STRNCMP(arg, "END", 3) == 0)
8853 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008854 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008855 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008856 // First load the current variable value.
8857 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8858 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008859 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008860 }
8861
8862 // Gets the redirected text and put it on the stack, then store it
8863 // in the variable.
8864 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8865
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008866 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008867 generate_instr_drop(cctx, ISN_CONCAT, 1);
8868
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008869 if (lhs->lhs_has_index)
8870 {
8871 // Use the info in "lhs" to store the value at the index in the
8872 // list or dict.
8873 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8874 &t_string, cctx) == FAIL)
8875 return NULL;
8876 }
8877 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008878 return NULL;
8879
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008880 VIM_CLEAR(lhs->lhs_name);
8881 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008882 return arg + 3;
8883 }
8884 emsg(_(e_cannot_nest_redir));
8885 return NULL;
8886 }
8887
8888 if (arg[0] == '=' && arg[1] == '>')
8889 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008890 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008891
8892 // redirect to a variable is compiled
8893 arg += 2;
8894 if (*arg == '>')
8895 {
8896 ++arg;
8897 append = TRUE;
8898 }
8899 arg = skipwhite(arg);
8900
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008901 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008902 FALSE, FALSE, 1, cctx) == FAIL)
8903 return NULL;
8904 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008905 lhs->lhs_append = append;
8906 if (lhs->lhs_has_index)
8907 {
8908 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8909 if (lhs->lhs_whole == NULL)
8910 return NULL;
8911 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008912
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008913 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008914 }
8915
8916 // other redirects are handled like at script level
8917 return compile_exec(line, eap, cctx);
8918}
8919
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008920#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008921 static char_u *
8922compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8923{
8924 isn_T *isn;
8925 char_u *p;
8926
8927 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8928 if (isn == NULL)
8929 return NULL;
8930 isn->isn_arg.number = eap->cmdidx;
8931
8932 p = eap->arg;
8933 if (compile_expr0(&p, cctx) == FAIL)
8934 return NULL;
8935
8936 isn = generate_instr(cctx, ISN_CEXPR_CORE);
8937 if (isn == NULL)
8938 return NULL;
8939 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
8940 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
8941 return NULL;
8942 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
8943 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
8944 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
8945
8946 return p;
8947}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008948#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008949
Bram Moolenaar4c137212021-04-19 16:48:48 +02008950/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008951 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008952 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008953 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008954 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008955add_def_function(ufunc_T *ufunc)
8956{
8957 dfunc_T *dfunc;
8958
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008959 if (def_functions.ga_len == 0)
8960 {
8961 // The first position is not used, so that a zero uf_dfunc_idx means it
8962 // wasn't set.
8963 if (ga_grow(&def_functions, 1) == FAIL)
8964 return FAIL;
8965 ++def_functions.ga_len;
8966 }
8967
Bram Moolenaar09689a02020-05-09 22:50:08 +02008968 // Add the function to "def_functions".
8969 if (ga_grow(&def_functions, 1) == FAIL)
8970 return FAIL;
8971 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8972 CLEAR_POINTER(dfunc);
8973 dfunc->df_idx = def_functions.ga_len;
8974 ufunc->uf_dfunc_idx = dfunc->df_idx;
8975 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008976 dfunc->df_name = vim_strsave(ufunc->uf_name);
8977 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008978 ++def_functions.ga_len;
8979 return OK;
8980}
8981
8982/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008983 * After ex_function() has collected all the function lines: parse and compile
8984 * the lines into instructions.
8985 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008986 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8987 * the return statement (used for lambda). When uf_ret_type is already set
8988 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008989 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008990 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008991 * This can be used recursively through compile_lambda(), which may reallocate
8992 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008993 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008994 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008995 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008996compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02008997 ufunc_T *ufunc,
8998 int check_return_type,
8999 compiletype_T compile_type,
9000 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009001{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009002 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009003 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009004 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009005 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009006 cctx_T cctx;
9007 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009008 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009009 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009010 int ret = FAIL;
9011 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009012 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009013 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009014 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009015 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009016#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009017 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009018#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009019 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009020
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009021 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009022 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009023 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009024 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009025 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9026 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009027 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009028 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009029 else
9030 {
9031 if (add_def_function(ufunc) == FAIL)
9032 return FAIL;
9033 new_def_function = TRUE;
9034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009035
Bram Moolenaar985116a2020-07-12 17:31:09 +02009036 ufunc->uf_def_status = UF_COMPILING;
9037
Bram Moolenaara80faa82020-04-12 19:37:17 +02009038 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009039
Bram Moolenaare99d4222021-06-13 14:01:26 +02009040 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009041 cctx.ctx_ufunc = ufunc;
9042 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009043 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009044 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9045 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9046 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9047 cctx.ctx_type_list = &ufunc->uf_type_list;
9048 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9049 instr = &cctx.ctx_instr;
9050
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009051 // Set the context to the function, it may be compiled when called from
9052 // another script. Set the script version to the most modern one.
9053 // The line number will be set in next_line_from_context().
9054 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009055 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9056
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009057 // Don't use the flag from ":legacy" here.
9058 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9059
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009060 // Make sure error messages are OK.
9061 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9062 if (do_estack_push)
9063 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009064 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009065
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009066 if (ufunc->uf_def_args.ga_len > 0)
9067 {
9068 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009069 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009070 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009071 int i;
9072 char_u *arg;
9073 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009074 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009075
9076 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009077 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009078 for (i = 0; i < count; ++i)
9079 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009080 garray_T *stack = &cctx.ctx_type_stack;
9081 type_T *val_type;
9082 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009083 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009084 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009085 int jump_instr_idx = instr->ga_len;
9086 isn_T *isn;
9087
9088 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9089 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9090 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009091
9092 // Make sure later arguments are not found.
9093 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009094
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009095 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009096 r = compile_expr0(&arg, &cctx);
9097
9098 ufunc->uf_args.ga_len = uf_args_len;
9099 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009100 goto erret;
9101
9102 // If no type specified use the type of the default value.
9103 // Otherwise check that the default value type matches the
9104 // specified type.
9105 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009106 where.wt_index = arg_idx + 1;
9107 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009108 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009109 {
9110 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009111 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009112 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009113 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009114 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009115 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009116
9117 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009118 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009119
9120 // set instruction index in JUMP_IF_ARG_SET to here
9121 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9122 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009123 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009124
9125 if (did_set_arg_type)
9126 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009127 }
9128
9129 /*
9130 * Loop over all the lines of the function and generate instructions.
9131 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009132 for (;;)
9133 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009134 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009135 int starts_with_colon = FALSE;
9136 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009137 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009138
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009139 // Bail out on the first error to avoid a flood of errors and report
9140 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009141 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009142 goto erret;
9143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009144 if (line != NULL && *line == '|')
9145 // the line continues after a '|'
9146 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009147 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009148 && !(*line == '#' && (line == cctx.ctx_line_start
9149 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009150 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009151 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009152 goto erret;
9153 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009154 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9155 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009156 else
9157 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009158 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009159 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009160 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009161 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009162#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009163 if (cctx.ctx_skip != SKIP_YES)
9164 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009165#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009166 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009167 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009168 // Make a copy, splitting off nextcmd and removing trailing spaces
9169 // may change it.
9170 if (line != NULL)
9171 {
9172 line = vim_strsave(line);
9173 vim_free(line_to_free);
9174 line_to_free = line;
9175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009176 }
9177
Bram Moolenaara80faa82020-04-12 19:37:17 +02009178 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009179 ea.cmdlinep = &line;
9180 ea.cmd = skipwhite(line);
9181
Bram Moolenaarb2049902021-01-24 12:53:53 +01009182 if (*ea.cmd == '#')
9183 {
9184 // "#" starts a comment
9185 line = (char_u *)"";
9186 continue;
9187 }
9188
Bram Moolenaarf002a412021-01-24 13:34:18 +01009189#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009190 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9191 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009192 {
9193 may_generate_prof_end(&cctx, prof_lnum);
9194
9195 prof_lnum = cctx.ctx_lnum;
9196 generate_instr(&cctx, ISN_PROF_START);
9197 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009198#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009199 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9200 && cctx.ctx_skip != SKIP_YES)
9201 {
9202 debug_lnum = cctx.ctx_lnum;
9203 generate_instr(&cctx, ISN_DEBUG);
9204 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01009205
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009206 // Some things can be recognized by the first character.
9207 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009208 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009209 case '}':
9210 {
9211 // "}" ends a block scope
9212 scopetype_T stype = cctx.ctx_scope == NULL
9213 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009214
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009215 if (stype == BLOCK_SCOPE)
9216 {
9217 compile_endblock(&cctx);
9218 line = ea.cmd;
9219 }
9220 else
9221 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009222 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009223 goto erret;
9224 }
9225 if (line != NULL)
9226 line = skipwhite(ea.cmd + 1);
9227 continue;
9228 }
9229
9230 case '{':
9231 // "{" starts a block scope
9232 // "{'a': 1}->func() is something else
9233 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9234 {
9235 line = compile_block(ea.cmd, &cctx);
9236 continue;
9237 }
9238 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009239 }
9240
9241 /*
9242 * COMMAND MODIFIERS
9243 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009244 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009245 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9246 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009247 {
9248 if (errormsg != NULL)
9249 goto erret;
9250 // empty line or comment
9251 line = (char_u *)"";
9252 continue;
9253 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009254 generate_cmdmods(&cctx, &local_cmdmod);
9255 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009256
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009257 // Check if there was a colon after the last command modifier or before
9258 // the current position.
9259 for (p = ea.cmd; p >= line; --p)
9260 {
9261 if (*p == ':')
9262 starts_with_colon = TRUE;
9263 if (p < ea.cmd && !VIM_ISWHITE(*p))
9264 break;
9265 }
9266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009267 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009268 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009269 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009270 {
9271 if (*ea.cmd == '(')
9272 // not for "call()"
9273 ea.cmd = p;
9274 else
9275 ea.cmd = skipwhite(ea.cmd);
9276 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009277
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009278 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009279 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009280 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009281
Bram Moolenaar17126b12021-01-07 22:03:02 +01009282 // Check for assignment after command modifiers.
9283 assign = may_compile_assignment(&ea, &line, &cctx);
9284 if (assign == OK)
9285 goto nextline;
9286 if (assign == FAIL)
9287 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009288 }
9289
9290 /*
9291 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009292 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009293 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009294 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009295 cmd = ea.cmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009296 if (starts_with_colon || !(*cmd == '\''
9297 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009298 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009299 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009300 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009301 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009302 if (!starts_with_colon)
9303 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009304 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009305 goto erret;
9306 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009307 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009308 if (ends_excmd2(line, ea.cmd))
9309 {
9310 // A range without a command: jump to the line.
9311 // TODO: compile to a more efficient command, possibly
9312 // calling parse_cmd_address().
9313 ea.cmdidx = CMD_SIZE;
9314 line = compile_exec(line, &ea, &cctx);
9315 goto nextline;
9316 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009317 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009318 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009319 p = find_ex_command(&ea, NULL, starts_with_colon
9320 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009321
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009322 if (p == NULL)
9323 {
9324 if (cctx.ctx_skip != SKIP_YES)
9325 emsg(_(e_ambiguous_use_of_user_defined_command));
9326 goto erret;
9327 }
9328
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009329 // When using ":legacy cmd" always use compile_exec().
9330 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009331 {
9332 char_u *start = ea.cmd;
9333
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009334 switch (ea.cmdidx)
9335 {
9336 case CMD_if:
9337 case CMD_elseif:
9338 case CMD_else:
9339 case CMD_endif:
9340 case CMD_for:
9341 case CMD_endfor:
9342 case CMD_continue:
9343 case CMD_break:
9344 case CMD_while:
9345 case CMD_endwhile:
9346 case CMD_try:
9347 case CMD_catch:
9348 case CMD_finally:
9349 case CMD_endtry:
9350 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9351 goto erret;
9352 default: break;
9353 }
9354
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009355 // ":legacy return expr" needs to be handled differently.
9356 if (checkforcmd(&start, "return", 4))
9357 ea.cmdidx = CMD_return;
9358 else
9359 ea.cmdidx = CMD_legacy;
9360 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009362 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9363 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009364 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009365 {
9366 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009367 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009368 }
9369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009370 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009371 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009372 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009373 // CMD_var cannot happen, compile_assignment() above would be
9374 // used. Most likely an assignment to a non-existing variable.
9375 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009376 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009378 }
9379
Bram Moolenaar3988f642020-08-27 22:43:03 +02009380 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009381 && ea.cmdidx != CMD_elseif
9382 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009383 && ea.cmdidx != CMD_endif
9384 && ea.cmdidx != CMD_endfor
9385 && ea.cmdidx != CMD_endwhile
9386 && ea.cmdidx != CMD_catch
9387 && ea.cmdidx != CMD_finally
9388 && ea.cmdidx != CMD_endtry)
9389 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009390 emsg(_(e_unreachable_code_after_return));
9391 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009392 }
9393
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009394 p = skipwhite(p);
9395 if (ea.cmdidx != CMD_SIZE
9396 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9397 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009398 if (ea.cmdidx >= 0)
9399 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009400 if ((ea.argt & EX_BANG) && *p == '!')
9401 {
9402 ea.forceit = TRUE;
9403 p = skipwhite(p + 1);
9404 }
9405 }
9406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009407 switch (ea.cmdidx)
9408 {
9409 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009410 ea.arg = p;
9411 line = compile_nested_function(&ea, &cctx);
9412 break;
9413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009414 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009415 // TODO: should we allow this, e.g. to declare a global
9416 // function?
9417 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009418 goto erret;
9419
9420 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009421 line = compile_return(p, check_return_type,
9422 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009423 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009424 break;
9425
9426 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009427 emsg(_(e_cannot_use_let_in_vim9_script));
9428 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009429 case CMD_var:
9430 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009431 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009432 case CMD_increment:
9433 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009434 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009435 if (line == p)
9436 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009437 break;
9438
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009439 case CMD_unlet:
9440 case CMD_unlockvar:
9441 case CMD_lockvar:
9442 line = compile_unletlock(p, &ea, &cctx);
9443 break;
9444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009445 case CMD_import:
9446 line = compile_import(p, &cctx);
9447 break;
9448
9449 case CMD_if:
9450 line = compile_if(p, &cctx);
9451 break;
9452 case CMD_elseif:
9453 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009454 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009455 break;
9456 case CMD_else:
9457 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009458 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009459 break;
9460 case CMD_endif:
9461 line = compile_endif(p, &cctx);
9462 break;
9463
9464 case CMD_while:
9465 line = compile_while(p, &cctx);
9466 break;
9467 case CMD_endwhile:
9468 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009469 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009470 break;
9471
9472 case CMD_for:
9473 line = compile_for(p, &cctx);
9474 break;
9475 case CMD_endfor:
9476 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009477 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009478 break;
9479 case CMD_continue:
9480 line = compile_continue(p, &cctx);
9481 break;
9482 case CMD_break:
9483 line = compile_break(p, &cctx);
9484 break;
9485
9486 case CMD_try:
9487 line = compile_try(p, &cctx);
9488 break;
9489 case CMD_catch:
9490 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009491 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009492 break;
9493 case CMD_finally:
9494 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009495 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009496 break;
9497 case CMD_endtry:
9498 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009499 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009500 break;
9501 case CMD_throw:
9502 line = compile_throw(p, &cctx);
9503 break;
9504
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009505 case CMD_eval:
9506 if (compile_expr0(&p, &cctx) == FAIL)
9507 goto erret;
9508
Bram Moolenaar3988f642020-08-27 22:43:03 +02009509 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009510 generate_instr_drop(&cctx, ISN_DROP, 1);
9511
9512 line = skipwhite(p);
9513 break;
9514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009515 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009516 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009517 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009518 case CMD_echomsg:
9519 case CMD_echoerr:
9520 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009521 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009522
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009523 case CMD_put:
9524 ea.cmd = cmd;
9525 line = compile_put(p, &ea, &cctx);
9526 break;
9527
Bram Moolenaar4c137212021-04-19 16:48:48 +02009528 case CMD_substitute:
9529 if (cctx.ctx_skip == SKIP_YES)
9530 line = (char_u *)"";
9531 else
9532 {
9533 ea.arg = p;
9534 line = compile_substitute(line, &ea, &cctx);
9535 }
9536 break;
9537
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009538 case CMD_redir:
9539 ea.arg = p;
9540 line = compile_redir(line, &ea, &cctx);
9541 break;
9542
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009543 case CMD_cexpr:
9544 case CMD_lexpr:
9545 case CMD_caddexpr:
9546 case CMD_laddexpr:
9547 case CMD_cgetexpr:
9548 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009549#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009550 ea.arg = p;
9551 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009552#else
9553 ex_ni(&ea);
9554 line = NULL;
9555#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009556 break;
9557
Bram Moolenaar3988f642020-08-27 22:43:03 +02009558 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009559
Bram Moolenaarae616492020-07-28 20:07:27 +02009560 case CMD_append:
9561 case CMD_change:
9562 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009563 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009564 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009565 case CMD_xit:
9566 not_in_vim9(&ea);
9567 goto erret;
9568
Bram Moolenaar002262f2020-07-08 17:47:57 +02009569 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009570 if (cctx.ctx_skip != SKIP_YES)
9571 {
9572 semsg(_(e_invalid_command_str), ea.cmd);
9573 goto erret;
9574 }
9575 // We don't check for a next command here.
9576 line = (char_u *)"";
9577 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009578
Bram Moolenaar20677332021-06-06 17:02:53 +02009579 case CMD_lua:
9580 case CMD_mzscheme:
9581 case CMD_perl:
9582 case CMD_py3:
9583 case CMD_python3:
9584 case CMD_python:
9585 case CMD_pythonx:
9586 case CMD_ruby:
9587 case CMD_tcl:
9588 ea.arg = p;
9589 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009590 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009591 else
9592 // heredoc lines have been concatenated with NL
9593 // characters in get_function_body()
9594 line = compile_script(line, &cctx);
9595 break;
9596
9597 default:
9598 // Not recognized, execute with do_cmdline_cmd().
9599 ea.arg = p;
9600 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009601 break;
9602 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009603nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009604 if (line == NULL)
9605 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009606 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009607
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009608 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009609 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009611 if (cctx.ctx_type_stack.ga_len < 0)
9612 {
9613 iemsg("Type stack underflow");
9614 goto erret;
9615 }
9616 }
9617
9618 if (cctx.ctx_scope != NULL)
9619 {
9620 if (cctx.ctx_scope->se_type == IF_SCOPE)
9621 emsg(_(e_endif));
9622 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9623 emsg(_(e_endwhile));
9624 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9625 emsg(_(e_endfor));
9626 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009627 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009628 goto erret;
9629 }
9630
Bram Moolenaarefd88552020-06-18 20:50:10 +02009631 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009632 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009633 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9634 ufunc->uf_ret_type = &t_void;
9635 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009636 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009637 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009638 goto erret;
9639 }
9640
9641 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01009642 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009643 }
9644
Bram Moolenaar599410c2021-04-10 14:03:43 +02009645 // When compiled with ":silent!" and there was an error don't consider the
9646 // function compiled.
9647 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009648 {
9649 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9650 + ufunc->uf_dfunc_idx;
9651 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009652 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009653#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009654 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009655 {
9656 dfunc->df_instr_prof = instr->ga_data;
9657 dfunc->df_instr_prof_count = instr->ga_len;
9658 }
9659 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009660#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009661 if (cctx.ctx_compile_type == CT_DEBUG)
9662 {
9663 dfunc->df_instr_debug = instr->ga_data;
9664 dfunc->df_instr_debug_count = instr->ga_len;
9665 }
9666 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009667 {
9668 dfunc->df_instr = instr->ga_data;
9669 dfunc->df_instr_count = instr->ga_len;
9670 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009671 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009672 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009673 if (cctx.ctx_outer_used)
9674 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009675 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009677
9678 ret = OK;
9679
9680erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009681 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009682 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009683 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9684 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009685
Bram Moolenaar8238f082021-04-20 21:10:48 +02009686 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009687 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009688
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009689 // If using the last entry in the table and it was added above, we
9690 // might as well remove it.
9691 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009692 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009693 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009694 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009695 ufunc->uf_dfunc_idx = 0;
9696 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009697 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009698
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009699 while (cctx.ctx_scope != NULL)
9700 drop_scope(&cctx);
9701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009702 if (errormsg != NULL)
9703 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009704 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009705 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009706 }
9707
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009708 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9709 {
9710 if (ret == OK)
9711 {
9712 emsg(_(e_missing_redir_end));
9713 ret = FAIL;
9714 }
9715 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009716 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009717 }
9718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009719 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009720 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009721 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009722 if (do_estack_push)
9723 estack_pop();
9724
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009725 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009726 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009727 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009728 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009729 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009730}
9731
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009732 void
9733set_function_type(ufunc_T *ufunc)
9734{
9735 int varargs = ufunc->uf_va_name != NULL;
9736 int argcount = ufunc->uf_args.ga_len;
9737
9738 // Create a type for the function, with the return type and any
9739 // argument types.
9740 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9741 // The type is included in "tt_args".
9742 if (argcount > 0 || varargs)
9743 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009744 if (ufunc->uf_type_list.ga_itemsize == 0)
9745 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009746 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9747 argcount, &ufunc->uf_type_list);
9748 // Add argument types to the function type.
9749 if (func_type_add_arg_types(ufunc->uf_func_type,
9750 argcount + varargs,
9751 &ufunc->uf_type_list) == FAIL)
9752 return;
9753 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9754 ufunc->uf_func_type->tt_min_argcount =
9755 argcount - ufunc->uf_def_args.ga_len;
9756 if (ufunc->uf_arg_types == NULL)
9757 {
9758 int i;
9759
9760 // lambda does not have argument types.
9761 for (i = 0; i < argcount; ++i)
9762 ufunc->uf_func_type->tt_args[i] = &t_any;
9763 }
9764 else
9765 mch_memmove(ufunc->uf_func_type->tt_args,
9766 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9767 if (varargs)
9768 {
9769 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009770 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009771 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9772 }
9773 }
9774 else
9775 // No arguments, can use a predefined type.
9776 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9777 argcount, &ufunc->uf_type_list);
9778}
9779
9780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009781/*
9782 * Delete an instruction, free what it contains.
9783 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009784 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009785delete_instr(isn_T *isn)
9786{
9787 switch (isn->isn_type)
9788 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009789 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009790 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009791 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009792 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009793 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009794 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009795 case ISN_LOADENV:
9796 case ISN_LOADG:
9797 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009798 case ISN_LOADT:
9799 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009800 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009801 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009802 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009803 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009804 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009805 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009806 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009807 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009808 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009809 case ISN_STOREW:
9810 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009811 vim_free(isn->isn_arg.string);
9812 break;
9813
Bram Moolenaar4c137212021-04-19 16:48:48 +02009814 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009815 {
9816 int idx;
9817 isn_T *list = isn->isn_arg.subs.subs_instr;
9818
9819 vim_free(isn->isn_arg.subs.subs_cmd);
9820 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9821 delete_instr(list + idx);
9822 vim_free(list);
9823 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009824 break;
9825
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009826 case ISN_INSTR:
9827 {
9828 int idx;
9829 isn_T *list = isn->isn_arg.instr;
9830
9831 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9832 delete_instr(list + idx);
9833 vim_free(list);
9834 }
9835 break;
9836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009837 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009838 case ISN_STORES:
9839 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009840 break;
9841
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009842 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009843 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009844 vim_free(isn->isn_arg.unlet.ul_name);
9845 break;
9846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009847 case ISN_STOREOPT:
9848 vim_free(isn->isn_arg.storeopt.so_name);
9849 break;
9850
9851 case ISN_PUSHBLOB: // push blob isn_arg.blob
9852 blob_unref(isn->isn_arg.blob);
9853 break;
9854
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009855 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009856#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009857 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009858#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009859 break;
9860
9861 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009862#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009863 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009864#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009865 break;
9866
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009867 case ISN_UCALL:
9868 vim_free(isn->isn_arg.ufunc.cuf_name);
9869 break;
9870
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009871 case ISN_FUNCREF:
9872 {
9873 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9874 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009875 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009876
Bram Moolenaar077a4232020-12-22 18:33:27 +01009877 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9878 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009879 }
9880 break;
9881
9882 case ISN_DCALL:
9883 {
9884 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9885 + isn->isn_arg.dfunc.cdf_idx;
9886
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009887 if (dfunc->df_ufunc != NULL
9888 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009889 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009890 }
9891 break;
9892
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009893 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009894 {
9895 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9896 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9897
9898 if (ufunc != NULL)
9899 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009900 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009901 func_ptr_unref(ufunc);
9902 }
9903
9904 vim_free(lambda);
9905 vim_free(isn->isn_arg.newfunc.nf_global);
9906 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009907 break;
9908
Bram Moolenaar5e654232020-09-16 15:22:00 +02009909 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009910 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009911 free_type(isn->isn_arg.type.ct_type);
9912 break;
9913
Bram Moolenaar02194d22020-10-24 23:08:38 +02009914 case ISN_CMDMOD:
9915 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9916 ->cmod_filter_regmatch.regprog);
9917 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9918 break;
9919
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009920 case ISN_LOADSCRIPT:
9921 case ISN_STORESCRIPT:
9922 vim_free(isn->isn_arg.script.scriptref);
9923 break;
9924
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009925 case ISN_TRY:
9926 vim_free(isn->isn_arg.try.try_ref);
9927 break;
9928
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009929 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +02009930 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009931 vim_free(isn->isn_arg.cexpr.cexpr_ref);
9932 break;
9933
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009934 case ISN_2BOOL:
9935 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009936 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009937 case ISN_ADDBLOB:
9938 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009939 case ISN_ANYINDEX:
9940 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009941 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009942 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009943 case ISN_BLOBINDEX:
9944 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009945 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009946 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009947 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009948 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009949 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009950 case ISN_COMPAREANY:
9951 case ISN_COMPAREBLOB:
9952 case ISN_COMPAREBOOL:
9953 case ISN_COMPAREDICT:
9954 case ISN_COMPAREFLOAT:
9955 case ISN_COMPAREFUNC:
9956 case ISN_COMPARELIST:
9957 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009958 case ISN_COMPARESPECIAL:
9959 case ISN_COMPARESTRING:
9960 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009961 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +02009962 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009963 case ISN_DROP:
9964 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009965 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009966 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009967 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009968 case ISN_EXECCONCAT:
9969 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009970 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +02009971 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009972 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009973 case ISN_GETITEM:
9974 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009975 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009976 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009977 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009978 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009979 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009980 case ISN_LOADBDICT:
9981 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009982 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009983 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009984 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009985 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009986 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009987 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009988 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009989 case ISN_NEGATENR:
9990 case ISN_NEWDICT:
9991 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009992 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009993 case ISN_OPFLOAT:
9994 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009995 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009996 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009997 case ISN_PROF_END:
9998 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009999 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010000 case ISN_PUSHF:
10001 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010002 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010003 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010004 case ISN_REDIREND:
10005 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010006 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +010010007 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010008 case ISN_SHUFFLE:
10009 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010010 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010011 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010012 case ISN_STORENR:
10013 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010014 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010015 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010016 case ISN_STOREV:
10017 case ISN_STRINDEX:
10018 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010019 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010020 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010021 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010022 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010023 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010024 // nothing allocated
10025 break;
10026 }
10027}
10028
10029/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010030 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010031 */
10032 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010033delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010034{
10035 int idx;
10036
10037 ga_clear(&dfunc->df_def_args_isn);
10038
10039 if (dfunc->df_instr != NULL)
10040 {
10041 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10042 delete_instr(dfunc->df_instr + idx);
10043 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010044 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010045 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010046#ifdef FEAT_PROFILE
10047 if (dfunc->df_instr_prof != NULL)
10048 {
10049 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10050 delete_instr(dfunc->df_instr_prof + idx);
10051 VIM_CLEAR(dfunc->df_instr_prof);
10052 dfunc->df_instr_prof = NULL;
10053 }
10054#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010055
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010056 if (mark_deleted)
10057 dfunc->df_deleted = TRUE;
10058 if (dfunc->df_ufunc != NULL)
10059 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010060}
10061
10062/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010063 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010064 * function, unless another user function still uses it.
10065 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010066 */
10067 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010068unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010069{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010070 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010071 {
10072 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10073 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010074
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010075 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010076 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010077 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010078 ufunc->uf_dfunc_idx = 0;
10079 if (dfunc->df_ufunc == ufunc)
10080 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010081 }
10082}
10083
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010084/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010085 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010086 */
10087 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010088link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010089{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010090 if (ufunc->uf_dfunc_idx > 0)
10091 {
10092 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10093 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010094
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010095 ++dfunc->df_refcount;
10096 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010097}
10098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010099#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010100/*
10101 * Free all functions defined with ":def".
10102 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010103 void
10104free_def_functions(void)
10105{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010106 int idx;
10107
10108 for (idx = 0; idx < def_functions.ga_len; ++idx)
10109 {
10110 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10111
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010112 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010113 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010114 }
10115
10116 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010117}
10118#endif
10119
10120
10121#endif // FEAT_EVAL