blob: 12f41f1259c8ae717f2bd7c11b21557f1f55875e [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
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200457 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100458 // 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 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001874 case CT_PROFILE:
1875#ifdef FEAT_PROFILE
1876 return dfunc->df_instr_prof == NULL;
1877#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001878 case CT_NONE:
1879 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001880 case CT_DEBUG:
1881 return dfunc->df_instr_debug == NULL;
1882 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001883 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001884
1885 case UF_NOT_COMPILED:
1886 case UF_COMPILE_ERROR:
1887 case UF_COMPILING:
1888 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001889 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001890 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001891}
1892
1893/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 * Generate an ISN_DCALL or ISN_UCALL instruction.
1895 * Return FAIL if the number of arguments is wrong.
1896 */
1897 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001898generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899{
1900 isn_T *isn;
1901 garray_T *stack = &cctx->ctx_type_stack;
1902 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001903 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904
Bram Moolenaar080457c2020-03-03 21:53:32 +01001905 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 if (argcount > regular_args && !has_varargs(ufunc))
1907 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001908 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 return FAIL;
1910 }
1911 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1912 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001913 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 return FAIL;
1915 }
1916
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001917 if (ufunc->uf_def_status != UF_NOT_COMPILED
1918 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001919 {
1920 int i;
1921
1922 for (i = 0; i < argcount; ++i)
1923 {
1924 type_T *expected;
1925 type_T *actual;
1926
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001927 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1928 if (actual == &t_special
1929 && i >= regular_args - ufunc->uf_def_args.ga_len)
1930 {
1931 // assume v:none used for default argument value
1932 continue;
1933 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001934 if (i < regular_args)
1935 {
1936 if (ufunc->uf_arg_types == NULL)
1937 continue;
1938 expected = ufunc->uf_arg_types[i];
1939 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001940 else if (ufunc->uf_va_type == NULL
1941 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001942 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001943 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001944 else
1945 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001946 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001947 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001948 {
1949 arg_type_mismatch(expected, actual, i + 1);
1950 return FAIL;
1951 }
1952 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02001953 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001954 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02001955 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001956 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001957 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02001958 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1959 {
1960 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1961 ufunc->uf_name);
1962 return FAIL;
1963 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001966 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001967 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001969 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 {
1971 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1972 isn->isn_arg.dfunc.cdf_argcount = argcount;
1973 }
1974 else
1975 {
1976 // A user function may be deleted and redefined later, can't use the
1977 // ufunc pointer, need to look it up again at runtime.
1978 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1979 isn->isn_arg.ufunc.cuf_argcount = argcount;
1980 }
1981
1982 stack->ga_len -= argcount; // drop the arguments
1983 if (ga_grow(stack, 1) == FAIL)
1984 return FAIL;
1985 // add return value
1986 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1987 ++stack->ga_len;
1988
1989 return OK;
1990}
1991
1992/*
1993 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1994 */
1995 static int
1996generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1997{
1998 isn_T *isn;
1999 garray_T *stack = &cctx->ctx_type_stack;
2000
Bram Moolenaar080457c2020-03-03 21:53:32 +01002001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2003 return FAIL;
2004 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2005 isn->isn_arg.ufunc.cuf_argcount = argcount;
2006
2007 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002008 if (ga_grow(stack, 1) == FAIL)
2009 return FAIL;
2010 // add return value
2011 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2012 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013
2014 return OK;
2015}
2016
2017/*
2018 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002019 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 */
2021 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002022generate_PCALL(
2023 cctx_T *cctx,
2024 int argcount,
2025 char_u *name,
2026 type_T *type,
2027 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028{
2029 isn_T *isn;
2030 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002031 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032
Bram Moolenaar080457c2020-03-03 21:53:32 +01002033 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002034
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002035 if (type->tt_type == VAR_ANY)
2036 ret_type = &t_any;
2037 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002038 {
2039 if (type->tt_argcount != -1)
2040 {
2041 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2042
2043 if (argcount < type->tt_min_argcount - varargs)
2044 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002045 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002046 return FAIL;
2047 }
2048 if (!varargs && argcount > type->tt_argcount)
2049 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002050 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002051 return FAIL;
2052 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002053 if (type->tt_args != NULL)
2054 {
2055 int i;
2056
2057 for (i = 0; i < argcount; ++i)
2058 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002059 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002060 type_T *actual = ((type_T **)stack->ga_data)[
2061 stack->ga_len + offset];
2062 type_T *expected;
2063
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002064 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002065 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002066 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002067 else if (i >= type->tt_min_argcount
2068 && actual == &t_special)
2069 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002070 else
2071 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002072 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002073 cctx, TRUE, FALSE) == FAIL)
2074 {
2075 arg_type_mismatch(expected, actual, i + 1);
2076 return FAIL;
2077 }
2078 }
2079 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002080 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002081 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002082 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002083 else
2084 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002085 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002086 return FAIL;
2087 }
2088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2090 return FAIL;
2091 isn->isn_arg.pfunc.cpf_top = at_top;
2092 isn->isn_arg.pfunc.cpf_argcount = argcount;
2093
2094 stack->ga_len -= argcount; // drop the arguments
2095
2096 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002097 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002099 // If partial is above the arguments it must be cleared and replaced with
2100 // the return value.
2101 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2102 return FAIL;
2103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 return OK;
2105}
2106
2107/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002108 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 */
2110 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002111generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112{
2113 isn_T *isn;
2114 garray_T *stack = &cctx->ctx_type_stack;
2115 type_T *type;
2116
Bram Moolenaar080457c2020-03-03 21:53:32 +01002117 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002118 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002120 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002122 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002124 if (type->tt_type != VAR_DICT && type != &t_any)
2125 {
2126 emsg(_(e_dictreq));
2127 return FAIL;
2128 }
2129 // change dict type to dict member type
2130 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002131 {
2132 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2133 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135
2136 return OK;
2137}
2138
2139/*
2140 * Generate an ISN_ECHO instruction.
2141 */
2142 static int
2143generate_ECHO(cctx_T *cctx, int with_white, int count)
2144{
2145 isn_T *isn;
2146
Bram Moolenaar080457c2020-03-03 21:53:32 +01002147 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2149 return FAIL;
2150 isn->isn_arg.echo.echo_with_white = with_white;
2151 isn->isn_arg.echo.echo_count = count;
2152
2153 return OK;
2154}
2155
Bram Moolenaarad39c092020-02-26 18:23:43 +01002156/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002157 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002158 */
2159 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002160generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002161{
2162 isn_T *isn;
2163
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002164 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002165 return FAIL;
2166 isn->isn_arg.number = count;
2167
2168 return OK;
2169}
2170
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002171/*
2172 * Generate an ISN_PUT instruction.
2173 */
2174 static int
2175generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2176{
2177 isn_T *isn;
2178
2179 RETURN_OK_IF_SKIP(cctx);
2180 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2181 return FAIL;
2182 isn->isn_arg.put.put_regname = regname;
2183 isn->isn_arg.put.put_lnum = lnum;
2184 return OK;
2185}
2186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 static int
2188generate_EXEC(cctx_T *cctx, char_u *line)
2189{
2190 isn_T *isn;
2191
Bram Moolenaar080457c2020-03-03 21:53:32 +01002192 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2194 return FAIL;
2195 isn->isn_arg.string = vim_strsave(line);
2196 return OK;
2197}
2198
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002199 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002200generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2201{
2202 isn_T *isn;
2203 garray_T *stack = &cctx->ctx_type_stack;
2204
2205 RETURN_OK_IF_SKIP(cctx);
2206 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2207 return FAIL;
2208 isn->isn_arg.string = vim_strsave(line);
2209
2210 if (ga_grow(stack, 1) == FAIL)
2211 return FAIL;
2212 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2213 ++stack->ga_len;
2214
2215 return OK;
2216}
2217
2218 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002219generate_EXECCONCAT(cctx_T *cctx, int count)
2220{
2221 isn_T *isn;
2222
2223 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2224 return FAIL;
2225 isn->isn_arg.number = count;
2226 return OK;
2227}
2228
Bram Moolenaar08597872020-12-10 19:43:40 +01002229/*
2230 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2231 */
2232 static int
2233generate_RANGE(cctx_T *cctx, char_u *range)
2234{
2235 isn_T *isn;
2236 garray_T *stack = &cctx->ctx_type_stack;
2237
2238 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2239 return FAIL;
2240 isn->isn_arg.string = range;
2241
2242 if (ga_grow(stack, 1) == FAIL)
2243 return FAIL;
2244 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2245 ++stack->ga_len;
2246 return OK;
2247}
2248
Bram Moolenaar792f7862020-11-23 08:31:18 +01002249 static int
2250generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2251{
2252 isn_T *isn;
2253
2254 RETURN_OK_IF_SKIP(cctx);
2255 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2256 return FAIL;
2257 isn->isn_arg.unpack.unp_count = var_count;
2258 isn->isn_arg.unpack.unp_semicolon = semicolon;
2259 return OK;
2260}
2261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002263 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002264 */
2265 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002266generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002267{
2268 isn_T *isn;
2269
Bram Moolenaarfa984412021-03-25 22:15:28 +01002270 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002271 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002272 cctx->ctx_has_cmdmod = TRUE;
2273
2274 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002275 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002276 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2277 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2278 return FAIL;
2279 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002280 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002281 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002282 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002283
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002284 return OK;
2285}
2286
2287 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002288generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002289{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002290 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2291 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002292 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002293 return OK;
2294}
2295
Bram Moolenaarfa984412021-03-25 22:15:28 +01002296 static int
2297misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002298{
2299 garray_T *instr = &cctx->ctx_instr;
2300
Bram Moolenaara91a7132021-03-25 21:12:15 +01002301 if (cctx->ctx_has_cmdmod
2302 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2303 == ISN_CMDMOD)
2304 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002305 emsg(_(e_misplaced_command_modifier));
2306 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002307 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002308 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002309}
2310
2311/*
2312 * Get the index of the current instruction.
2313 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2314 */
2315 static int
2316current_instr_idx(cctx_T *cctx)
2317{
2318 garray_T *instr = &cctx->ctx_instr;
2319 int idx = instr->ga_len;
2320
Bram Moolenaare99d4222021-06-13 14:01:26 +02002321 while (idx > 0)
2322 {
2323 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002324 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002325 {
2326 --idx;
2327 continue;
2328 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002329#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002330 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2331 {
2332 --idx;
2333 continue;
2334 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002335#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002336 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2337 {
2338 --idx;
2339 continue;
2340 }
2341 break;
2342 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002343 return idx;
2344}
2345
Bram Moolenaarf002a412021-01-24 13:34:18 +01002346#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002347 static void
2348may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2349{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002350 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002351 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002352}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002353#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002354
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002355/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002357 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002359 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002360reserve_local(
2361 cctx_T *cctx,
2362 char_u *name,
2363 size_t len,
2364 int isConst,
2365 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367 lvar_T *lvar;
2368
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002369 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002371 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002372 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 }
2374
2375 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002376 return NULL;
2377 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002378 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002380 // Every local variable uses the next entry on the stack. We could re-use
2381 // the last ones when leaving a scope, but then variables used in a closure
2382 // might get overwritten. To keep things simple do not re-use stack
2383 // entries. This is less efficient, but memory is cheap these days.
2384 lvar->lv_idx = cctx->ctx_locals_count++;
2385
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002386 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 lvar->lv_const = isConst;
2388 lvar->lv_type = type;
2389
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002390 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391}
2392
2393/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002394 * Remove local variables above "new_top".
2395 */
2396 static void
2397unwind_locals(cctx_T *cctx, int new_top)
2398{
2399 if (cctx->ctx_locals.ga_len > new_top)
2400 {
2401 int idx;
2402 lvar_T *lvar;
2403
2404 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2405 {
2406 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2407 vim_free(lvar->lv_name);
2408 }
2409 }
2410 cctx->ctx_locals.ga_len = new_top;
2411}
2412
2413/*
2414 * Free all local variables.
2415 */
2416 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002417free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002418{
2419 unwind_locals(cctx, 0);
2420 ga_clear(&cctx->ctx_locals);
2421}
2422
2423/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002424 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2425 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2426 * error if the variable was defined with :const.
2427 */
2428 static int
2429check_item_writable(svar_T *sv, int check_writable, char_u *name)
2430{
2431 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2432 || (check_writable == ASSIGN_FINAL
2433 && sv->sv_const == ASSIGN_CONST))
2434 {
2435 semsg(_(e_readonlyvar), name);
2436 return FAIL;
2437 }
2438 return OK;
2439}
2440
2441/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002443 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 * Returns the index in "sn_var_vals" if found.
2445 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002446 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 */
2448 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002449get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450{
2451 hashtab_T *ht;
2452 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002453 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002454 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455 int idx;
2456
Bram Moolenaare3d46852020-08-29 13:39:17 +02002457 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002459 if (sid == current_sctx.sc_sid)
2460 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002461 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002462
2463 if (sav == NULL)
2464 return -2;
2465 idx = sav->sav_var_vals_idx;
2466 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002467 if (check_item_writable(sv, check_writable, name) == FAIL)
2468 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002469 return idx;
2470 }
2471
2472 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 ht = &SCRIPT_VARS(sid);
2474 di = find_var_in_ht(ht, 0, name, TRUE);
2475 if (di == NULL)
2476 return -2;
2477
2478 // Now find the svar_T index in sn_var_vals.
2479 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2480 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002481 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 if (sv->sv_tv == &di->di_tv)
2483 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002484 if (check_item_writable(sv, check_writable, name) == FAIL)
2485 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 return idx;
2487 }
2488 }
2489 return -1;
2490}
2491
2492/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002493 * Find "name" in imported items of the current script or in "cctx" if not
2494 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 */
2496 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002497find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 int idx;
2500
Bram Moolenaare3d46852020-08-29 13:39:17 +02002501 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002502 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 if (cctx != NULL)
2504 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2505 {
2506 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2507 + idx;
2508
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002509 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2510 : STRLEN(import->imp_name) == len
2511 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 return import;
2513 }
2514
Bram Moolenaarefa94442020-08-08 22:16:00 +02002515 return find_imported_in_script(name, len, current_sctx.sc_sid);
2516}
2517
2518 imported_T *
2519find_imported_in_script(char_u *name, size_t len, int sid)
2520{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002521 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002522 int idx;
2523
Bram Moolenaare3d46852020-08-29 13:39:17 +02002524 if (!SCRIPT_ID_VALID(sid))
2525 return NULL;
2526 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2528 {
2529 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2530
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002531 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2532 : STRLEN(import->imp_name) == len
2533 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 return import;
2535 }
2536 return NULL;
2537}
2538
2539/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002540 * Free all imported variables.
2541 */
2542 static void
2543free_imported(cctx_T *cctx)
2544{
2545 int idx;
2546
2547 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2548 {
2549 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2550
2551 vim_free(import->imp_name);
2552 }
2553 ga_clear(&cctx->ctx_imports);
2554}
2555
2556/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002557 * Return a pointer to the next line that isn't empty or only contains a
2558 * comment. Skips over white space.
2559 * Returns NULL if there is none.
2560 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002561 char_u *
2562peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002563{
2564 int lnum = cctx->ctx_lnum;
2565
2566 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2567 {
2568 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002569 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002570
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002571 // ignore NULLs inserted for continuation lines
2572 if (line != NULL)
2573 {
2574 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002575 if (vim9_bad_comment(p))
2576 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002577 if (*p != NUL && !vim9_comment_start(p))
2578 return p;
2579 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002580 }
2581 return NULL;
2582}
2583
2584/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002585 * Called when checking for a following operator at "arg". When the rest of
2586 * the line is empty or only a comment, peek the next line. If there is a next
2587 * line return a pointer to it and set "nextp".
2588 * Otherwise skip over white space.
2589 */
2590 static char_u *
2591may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2592{
2593 char_u *p = skipwhite(arg);
2594
2595 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002596 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002597 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002598 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002599 if (*nextp != NULL)
2600 return *nextp;
2601 }
2602 return p;
2603}
2604
2605/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002606 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002607 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002608 * Returns NULL when at the end.
2609 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002610 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002611next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002612{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002613 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002614
2615 do
2616 {
2617 ++cctx->ctx_lnum;
2618 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002619 {
2620 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002621 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002622 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002623 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002624 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002625 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002626 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002627 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002628 return line;
2629}
2630
2631/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002632 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002633 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002634 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002635 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2636 */
2637 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002638may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002639{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002640 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002641 if (vim9_bad_comment(*arg))
2642 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002643 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002644 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002645 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002646
2647 if (next == NULL)
2648 return FAIL;
2649 *arg = skipwhite(next);
2650 }
2651 return OK;
2652}
2653
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002654/*
2655 * Idem, and give an error when failed.
2656 */
2657 static int
2658may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2659{
2660 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2661 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002662 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002663 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002664 return FAIL;
2665 }
2666 return OK;
2667}
2668
2669
Bram Moolenaara5565e42020-05-09 15:44:01 +02002670// Structure passed between the compile_expr* functions to keep track of
2671// constants that have been parsed but for which no code was produced yet. If
2672// possible expressions on these constants are applied at compile time. If
2673// that is not possible, the code to push the constants needs to be generated
2674// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002675// Using 50 should be more than enough of 5 levels of ().
2676#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002677typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002678 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002679 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002680 int pp_is_const; // all generated code was constants, used for a
2681 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002682} ppconst_T;
2683
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002684static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002685static int compile_expr0(char_u **arg, cctx_T *cctx);
2686static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2687
Bram Moolenaara5565e42020-05-09 15:44:01 +02002688/*
2689 * Generate a PUSH instruction for "tv".
2690 * "tv" will be consumed or cleared.
2691 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2692 */
2693 static int
2694generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2695{
2696 if (tv != NULL)
2697 {
2698 switch (tv->v_type)
2699 {
2700 case VAR_UNKNOWN:
2701 break;
2702 case VAR_BOOL:
2703 generate_PUSHBOOL(cctx, tv->vval.v_number);
2704 break;
2705 case VAR_SPECIAL:
2706 generate_PUSHSPEC(cctx, tv->vval.v_number);
2707 break;
2708 case VAR_NUMBER:
2709 generate_PUSHNR(cctx, tv->vval.v_number);
2710 break;
2711#ifdef FEAT_FLOAT
2712 case VAR_FLOAT:
2713 generate_PUSHF(cctx, tv->vval.v_float);
2714 break;
2715#endif
2716 case VAR_BLOB:
2717 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2718 tv->vval.v_blob = NULL;
2719 break;
2720 case VAR_STRING:
2721 generate_PUSHS(cctx, tv->vval.v_string);
2722 tv->vval.v_string = NULL;
2723 break;
2724 default:
2725 iemsg("constant type not supported");
2726 clear_tv(tv);
2727 return FAIL;
2728 }
2729 tv->v_type = VAR_UNKNOWN;
2730 }
2731 return OK;
2732}
2733
2734/*
2735 * Generate code for any ppconst entries.
2736 */
2737 static int
2738generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2739{
2740 int i;
2741 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002742 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002743
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002744 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002745 for (i = 0; i < ppconst->pp_used; ++i)
2746 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2747 ret = FAIL;
2748 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002749 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002750 return ret;
2751}
2752
2753/*
2754 * Clear ppconst constants. Used when failing.
2755 */
2756 static void
2757clear_ppconst(ppconst_T *ppconst)
2758{
2759 int i;
2760
2761 for (i = 0; i < ppconst->pp_used; ++i)
2762 clear_tv(&ppconst->pp_tv[i]);
2763 ppconst->pp_used = 0;
2764}
2765
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002766/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002767 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002768 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002769 */
2770 static int
2771compile_member(int is_slice, cctx_T *cctx)
2772{
2773 type_T **typep;
2774 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002775 vartype_T vartype;
2776 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002777
Bram Moolenaar261417b2021-05-06 21:04:55 +02002778 // We can index a list, dict and blob. If we don't know the type
2779 // we can use the index value type. If we still don't know use an "ANY"
2780 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002781 typep = ((type_T **)stack->ga_data) + stack->ga_len
2782 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002783 vartype = (*typep)->tt_type;
2784 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002785 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002786 if (*typep == &t_any && idxtype == &t_string)
2787 vartype = VAR_DICT;
2788 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002789 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002790 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002791 return FAIL;
2792 if (is_slice)
2793 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002794 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2795 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002796 FALSE, FALSE) == FAIL)
2797 return FAIL;
2798 }
2799 }
2800
Bram Moolenaar261417b2021-05-06 21:04:55 +02002801 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002802 {
2803 if (is_slice)
2804 {
2805 emsg(_(e_cannot_slice_dictionary));
2806 return FAIL;
2807 }
2808 if ((*typep)->tt_type == VAR_DICT)
2809 {
2810 *typep = (*typep)->tt_member;
2811 if (*typep == &t_unknown)
2812 // empty dict was used
2813 *typep = &t_any;
2814 }
2815 else
2816 {
2817 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2818 FALSE, FALSE) == FAIL)
2819 return FAIL;
2820 *typep = &t_any;
2821 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002822 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002823 return FAIL;
2824 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2825 return FAIL;
2826 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002827 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002828 {
2829 *typep = &t_string;
2830 if ((is_slice
2831 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2832 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2833 return FAIL;
2834 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002835 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002836 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002837 if (is_slice)
2838 {
2839 *typep = &t_blob;
2840 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2841 return FAIL;
2842 }
2843 else
2844 {
2845 *typep = &t_number;
2846 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2847 return FAIL;
2848 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002849 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002850 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002851 {
2852 if (is_slice)
2853 {
2854 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002855 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002856 2) == FAIL)
2857 return FAIL;
2858 }
2859 else
2860 {
2861 if ((*typep)->tt_type == VAR_LIST)
2862 {
2863 *typep = (*typep)->tt_member;
2864 if (*typep == &t_unknown)
2865 // empty list was used
2866 *typep = &t_any;
2867 }
2868 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002869 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2870 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002871 return FAIL;
2872 }
2873 }
2874 else
2875 {
2876 emsg(_(e_string_list_dict_or_blob_required));
2877 return FAIL;
2878 }
2879 return OK;
2880}
2881
2882/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002883 * Generate an instruction to load script-local variable "name", without the
2884 * leading "s:".
2885 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 */
2887 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002888compile_load_scriptvar(
2889 cctx_T *cctx,
2890 char_u *name, // variable NUL terminated
2891 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002892 char_u **end, // end of variable
2893 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002895 scriptitem_T *si;
2896 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 imported_T *import;
2898
Bram Moolenaare3d46852020-08-29 13:39:17 +02002899 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2900 return FAIL;
2901 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002902 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002903 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002905 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002906 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2907 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 }
2909 if (idx >= 0)
2910 {
2911 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2912
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002913 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 current_sctx.sc_sid, idx, sv->sv_type);
2915 return OK;
2916 }
2917
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002918 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 if (import != NULL)
2920 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002921 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002922 {
2923 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002924 char_u *exp_name;
2925 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002926 ufunc_T *ufunc;
2927 type_T *type;
2928
2929 // Used "import * as Name", need to lookup the member.
2930 if (*p != '.')
2931 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002932 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002933 return FAIL;
2934 }
2935 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002936 if (VIM_ISWHITE(*p))
2937 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002938 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002939 return FAIL;
2940 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002941
Bram Moolenaar1c991142020-07-04 13:15:31 +02002942 // isolate one name
2943 exp_name = p;
2944 while (eval_isnamec(*p))
2945 ++p;
2946 cc = *p;
2947 *p = NUL;
2948
Bram Moolenaaredba7072021-03-13 21:14:18 +01002949 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2950 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002951 *p = cc;
2952 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002953 *end = p;
2954
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002955 if (idx < 0)
2956 {
2957 if (*p == '(' && ufunc != NULL)
2958 {
2959 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2960 return OK;
2961 }
2962 return FAIL;
2963 }
2964
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002965 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2966 import->imp_sid,
2967 idx,
2968 type);
2969 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002970 else if (import->imp_funcname != NULL)
2971 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002972 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002973 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2974 import->imp_sid,
2975 import->imp_var_vals_idx,
2976 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 return OK;
2978 }
2979
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002980 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002981 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 return FAIL;
2983}
2984
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002985 static int
2986generate_funcref(cctx_T *cctx, char_u *name)
2987{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002988 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002989
2990 if (ufunc == NULL)
2991 return FAIL;
2992
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002993 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02002994 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
2995 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002996 == FAIL)
2997 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002998 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002999}
3000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001/*
3002 * Compile a variable name into a load instruction.
3003 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003004 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 * When "error" is FALSE do not give an error when not found.
3006 */
3007 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003008compile_load(
3009 char_u **arg,
3010 char_u *end_arg,
3011 cctx_T *cctx,
3012 int is_expr,
3013 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014{
3015 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003016 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003017 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003019 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020
3021 if (*(*arg + 1) == ':')
3022 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003023 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003024 {
3025 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026
Bram Moolenaarfa596382021-04-07 21:58:16 +02003027 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003028 switch (**arg)
3029 {
3030 case 'g': isn_type = ISN_LOADGDICT; break;
3031 case 'w': isn_type = ISN_LOADWDICT; break;
3032 case 't': isn_type = ISN_LOADTDICT; break;
3033 case 'b': isn_type = ISN_LOADBDICT; break;
3034 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003035 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003036 goto theend;
3037 }
3038 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3039 goto theend;
3040 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 else
3043 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003044 isntype_T isn_type = ISN_DROP;
3045
Bram Moolenaarfa596382021-04-07 21:58:16 +02003046 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003047 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3048 if (name == NULL)
3049 return FAIL;
3050
3051 switch (**arg)
3052 {
3053 case 'v': res = generate_LOADV(cctx, name, error);
3054 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003055 case 's': if (is_expr && ASCII_ISUPPER(*name)
3056 && find_func(name, FALSE, cctx) != NULL)
3057 res = generate_funcref(cctx, name);
3058 else
3059 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003060 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003061 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003062 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003063 {
3064 if (is_expr && ASCII_ISUPPER(*name)
3065 && find_func(name, FALSE, cctx) != NULL)
3066 res = generate_funcref(cctx, name);
3067 else
3068 isn_type = ISN_LOADG;
3069 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003070 else
3071 {
3072 isn_type = ISN_LOADAUTO;
3073 vim_free(name);
3074 name = vim_strnsave(*arg, end - *arg);
3075 if (name == NULL)
3076 return FAIL;
3077 }
3078 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003079 case 'w': isn_type = ISN_LOADW; break;
3080 case 't': isn_type = ISN_LOADT; break;
3081 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003082 default: // cannot happen, just in case
3083 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003084 goto theend;
3085 }
3086 if (isn_type != ISN_DROP)
3087 {
3088 // Global, Buffer-local, Window-local and Tabpage-local
3089 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003090 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003091 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3092 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 }
3094 }
3095 else
3096 {
3097 size_t len = end - *arg;
3098 int idx;
3099 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003100 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101
3102 name = vim_strnsave(*arg, end - *arg);
3103 if (name == NULL)
3104 return FAIL;
3105
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003106 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3107 {
3108 script_autoload(name, FALSE);
3109 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3110 }
3111 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3112 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003114 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003115 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 }
3117 else
3118 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003119 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003120
Bram Moolenaar709664c2020-12-12 14:33:41 +01003121 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003123 type = lvar.lv_type;
3124 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003125 if (lvar.lv_from_outer != 0)
3126 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003127 else
3128 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 }
3130 else
3131 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003132 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003133 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003134 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003135 || find_imported(name, 0, cctx) != NULL)
3136 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003137
Bram Moolenaar0f769812020-09-12 18:32:34 +02003138 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003139 // uppercase letter it can be a user defined function.
3140 // generate_funcref() will fail if the function can't be found.
3141 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003142 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 }
3144 }
3145 if (gen_load)
3146 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003147 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003148 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003149 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003150 cctx->ctx_outer_used = TRUE;
3151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 }
3153
3154 *arg = end;
3155
3156theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003157 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003158 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 vim_free(name);
3160 return res;
3161}
3162
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003163 static void
3164clear_instr_ga(garray_T *gap)
3165{
3166 int idx;
3167
3168 for (idx = 0; idx < gap->ga_len; ++idx)
3169 delete_instr(((isn_T *)gap->ga_data) + idx);
3170 ga_clear(gap);
3171}
3172
3173/*
3174 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3175 * Returns FAIL if compilation fails.
3176 */
3177 static int
3178compile_string(isn_T *isn, cctx_T *cctx)
3179{
3180 char_u *s = isn->isn_arg.string;
3181 garray_T save_ga = cctx->ctx_instr;
3182 int expr_res;
3183 int trailing_error;
3184 int instr_count;
3185 isn_T *instr = NULL;
3186
3187 // Temporarily reset the list of instructions so that the jump labels are
3188 // correct.
3189 cctx->ctx_instr.ga_len = 0;
3190 cctx->ctx_instr.ga_maxlen = 0;
3191 cctx->ctx_instr.ga_data = NULL;
3192 expr_res = compile_expr0(&s, cctx);
3193 s = skipwhite(s);
3194 trailing_error = *s != NUL;
3195
Bram Moolenaarff652882021-05-16 15:24:49 +02003196 if (expr_res == FAIL || trailing_error
3197 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003198 {
3199 if (trailing_error)
3200 semsg(_(e_trailing_arg), s);
3201 clear_instr_ga(&cctx->ctx_instr);
3202 cctx->ctx_instr = save_ga;
3203 return FAIL;
3204 }
3205
3206 // Move the generated instructions into the ISN_INSTR instruction, then
3207 // restore the list of instructions.
3208 instr_count = cctx->ctx_instr.ga_len;
3209 instr = cctx->ctx_instr.ga_data;
3210 instr[instr_count].isn_type = ISN_FINISH;
3211
3212 cctx->ctx_instr = save_ga;
3213 vim_free(isn->isn_arg.string);
3214 isn->isn_type = ISN_INSTR;
3215 isn->isn_arg.instr = instr;
3216 return OK;
3217}
3218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219/*
3220 * Compile the argument expressions.
3221 * "arg" points to just after the "(" and is advanced to after the ")"
3222 */
3223 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003224compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003226 char_u *p = *arg;
3227 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003228 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003229 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230
Bram Moolenaare6085c52020-04-12 20:19:16 +02003231 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003233 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3234 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003235 if (*p == ')')
3236 {
3237 *arg = p + 1;
3238 return OK;
3239 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003240 if (must_end)
3241 {
3242 semsg(_(e_missing_comma_before_argument_str), p);
3243 return FAIL;
3244 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003245
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003246 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003247 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 return FAIL;
3249 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003250
Bram Moolenaarff652882021-05-16 15:24:49 +02003251 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003252 && cctx->ctx_instr.ga_len == instr_count + 1)
3253 {
3254 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3255
3256 // {skip} argument of searchpair() can be compiled if not empty
3257 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3258 compile_string(isn, cctx);
3259 }
3260
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003261 if (*p != ',' && *skipwhite(p) == ',')
3262 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003263 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003264 p = skipwhite(p);
3265 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003267 {
3268 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003269 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003270 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003271 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003272 else
3273 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003274 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003275 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003277failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003278 emsg(_(e_missing_close));
3279 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280}
3281
3282/*
3283 * Compile a function call: name(arg1, arg2)
3284 * "arg" points to "name", "arg + varlen" to the "(".
3285 * "argcount_init" is 1 for "value->method()"
3286 * Instructions:
3287 * EVAL arg1
3288 * EVAL arg2
3289 * BCALL / DCALL / UCALL
3290 */
3291 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003292compile_call(
3293 char_u **arg,
3294 size_t varlen,
3295 cctx_T *cctx,
3296 ppconst_T *ppconst,
3297 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298{
3299 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003300 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 int argcount = argcount_init;
3302 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003303 char_u fname_buf[FLEN_FIXED + 1];
3304 char_u *tofree = NULL;
3305 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003306 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003307 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003308 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003309 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310
Bram Moolenaara5565e42020-05-09 15:44:01 +02003311 // we can evaluate "has('name')" at compile time
3312 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3313 {
3314 char_u *s = skipwhite(*arg + varlen + 1);
3315 typval_T argvars[2];
3316
3317 argvars[0].v_type = VAR_UNKNOWN;
3318 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003319 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003320 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003321 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003322 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003323 if (*s == ')' && argvars[0].v_type == VAR_STRING
3324 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003325 {
3326 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3327
3328 *arg = s + 1;
3329 argvars[1].v_type = VAR_UNKNOWN;
3330 tv->v_type = VAR_NUMBER;
3331 tv->vval.v_number = 0;
3332 f_has(argvars, tv);
3333 clear_tv(&argvars[0]);
3334 ++ppconst->pp_used;
3335 return OK;
3336 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003337 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003338 }
3339
3340 if (generate_ppconst(cctx, ppconst) == FAIL)
3341 return FAIL;
3342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 if (varlen >= sizeof(namebuf))
3344 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003345 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 return FAIL;
3347 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003348 vim_strncpy(namebuf, *arg, varlen);
3349 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003351 // We handle the "skip" argument of searchpair() and searchpairpos()
3352 // differently.
3353 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3354 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3355 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3356 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003359 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003360 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361
Bram Moolenaar03290b82020-12-19 16:30:44 +01003362 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003363 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 {
3365 int idx;
3366
3367 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003368 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003370 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003371 if (STRCMP(name, "flatten") == 0)
3372 {
3373 emsg(_(e_cannot_use_flatten_in_vim9_script));
3374 goto theend;
3375 }
3376
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003377 if (STRCMP(name, "add") == 0 && argcount == 2)
3378 {
3379 garray_T *stack = &cctx->ctx_type_stack;
3380 type_T *type = ((type_T **)stack->ga_data)[
3381 stack->ga_len - 2];
3382
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003383 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003384 if (type->tt_type == VAR_LIST)
3385 {
3386 // inline "add(list, item)" so that the type can be checked
3387 res = generate_LISTAPPEND(cctx);
3388 idx = -1;
3389 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003390 else if (type->tt_type == VAR_BLOB)
3391 {
3392 // inline "add(blob, nr)" so that the type can be checked
3393 res = generate_BLOBAPPEND(cctx);
3394 idx = -1;
3395 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003396 }
3397
3398 if (idx >= 0)
3399 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3400 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003401 else
3402 semsg(_(e_unknownfunc), namebuf);
3403 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 }
3405
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003406 // An argument or local variable can be a function reference, this
3407 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003408 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003409 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003410 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003411 // If we can find the function by name generate the right call.
3412 // Skip global functions here, a local funcref takes precedence.
3413 ufunc = find_func(name, FALSE, cctx);
3414 if (ufunc != NULL && !func_is_global(ufunc))
3415 {
3416 res = generate_CALL(cctx, ufunc, argcount);
3417 goto theend;
3418 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420
3421 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003422 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003423 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003425 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003426 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003427 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003428 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003429 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003430
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003431 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003432 goto theend;
3433 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434
Bram Moolenaar0f769812020-09-12 18:32:34 +02003435 // If we can find a global function by name generate the right call.
3436 if (ufunc != NULL)
3437 {
3438 res = generate_CALL(cctx, ufunc, argcount);
3439 goto theend;
3440 }
3441
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003442 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003443 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003444 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003445 res = generate_UCALL(cctx, name, argcount);
3446 else
3447 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003448
3449theend:
3450 vim_free(tofree);
3451 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452}
3453
3454// like NAMESPACE_CHAR but with 'a' and 'l'.
3455#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3456
3457/*
3458 * Find the end of a variable or function name. Unlike find_name_end() this
3459 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003460 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 * Return a pointer to just after the name. Equal to "arg" if there is no
3462 * valid name.
3463 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003464 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003465to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466{
3467 char_u *p;
3468
3469 // Quick check for valid starting character.
3470 if (!eval_isnamec1(*arg))
3471 return arg;
3472
3473 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3474 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3475 // and can be used in slice "[n:]".
3476 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003477 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3479 break;
3480 return p;
3481}
3482
3483/*
3484 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003485 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 */
3487 char_u *
3488to_name_const_end(char_u *arg)
3489{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003490 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 typval_T rettv;
3492
3493 if (p == arg && *arg == '[')
3494 {
3495
3496 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003497 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 p = arg;
3499 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 return p;
3501}
3502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503/*
3504 * parse a list: [expr, expr]
3505 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003506 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 */
3508 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003509compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510{
3511 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003512 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003514 int is_const;
3515 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003517 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003519 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003520 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003521 semsg(_(e_list_end), *arg);
3522 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003523 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003524 if (*p == ',')
3525 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003526 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003527 return FAIL;
3528 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003529 if (*p == ']')
3530 {
3531 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003532 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003533 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003534 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003535 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003536 if (!is_const)
3537 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 ++count;
3539 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003540 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003542 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3543 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003544 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003545 return FAIL;
3546 }
3547 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003548 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 p = skipwhite(p);
3550 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003551 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003553 ppconst->pp_is_const = is_all_const;
3554 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555}
3556
3557/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003558 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003559 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003560 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 */
3562 static int
3563compile_lambda(char_u **arg, cctx_T *cctx)
3564{
Bram Moolenaare462f522020-12-27 14:43:30 +01003565 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 typval_T rettv;
3567 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003568 evalarg_T evalarg;
3569
3570 CLEAR_FIELD(evalarg);
3571 evalarg.eval_flags = EVAL_EVALUATE;
3572 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573
3574 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003575 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3576 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003577 {
3578 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003579 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003580 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003581
Bram Moolenaar65c44152020-12-24 15:14:01 +01003582 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003584 ++ufunc->uf_refcount;
3585 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586
Bram Moolenaara9931532021-06-12 15:58:16 +02003587 // Compile it here to get the return type. The return type is optional,
3588 // when it's missing use t_unknown. This is recognized in
3589 // compile_return().
3590 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3591 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaare99d4222021-06-13 14:01:26 +02003592 compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003594 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3595 // points into it. Point to the original line to avoid a dangling pointer.
3596 if (evalarg.eval_tofree_cmdline != NULL)
3597 {
3598 size_t off = *arg - evalarg.eval_tofree_cmdline;
3599
3600 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3601 + off;
3602 }
3603
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003604 clear_evalarg(&evalarg, NULL);
3605
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003606 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003607 {
3608 // The return type will now be known.
3609 set_function_type(ufunc);
3610
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003611 // The function reference count will be 1. When the ISN_FUNCREF
3612 // instruction is deleted the reference count is decremented and the
3613 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003614 return generate_FUNCREF(cctx, ufunc);
3615 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003616
3617 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 return FAIL;
3619}
3620
3621/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003622 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003624 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 */
3626 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003627compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628{
3629 garray_T *instr = &cctx->ctx_instr;
3630 int count = 0;
3631 dict_T *d = dict_alloc();
3632 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003633 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003634 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003635 int is_const;
3636 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637
3638 if (d == NULL)
3639 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003640 if (generate_ppconst(cctx, ppconst) == FAIL)
3641 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003642 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003644 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645
Bram Moolenaar23c55272020-06-21 16:58:13 +02003646 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003647 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003648 *arg = NULL;
3649 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003650 }
3651
3652 if (**arg == '}')
3653 break;
3654
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003655 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003657 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003659 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003660 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003661 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003664 if (isn->isn_type == ISN_PUSHNR)
3665 {
3666 char buf[NUMBUFLEN];
3667
3668 // Convert to string at compile time.
3669 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3670 isn->isn_type = ISN_PUSHS;
3671 isn->isn_arg.string = vim_strsave((char_u *)buf);
3672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 if (isn->isn_type == ISN_PUSHS)
3674 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003675 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003676 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003677 *arg = skipwhite(*arg);
3678 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003679 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003680 emsg(_(e_missing_matching_bracket_after_dict_key));
3681 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003682 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003683 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003685 else
3686 {
3687 // {"name": value},
3688 // {'name': value},
3689 // {name: value} use "name" as a literal key
3690 key = get_literal_key(arg);
3691 if (key == NULL)
3692 return FAIL;
3693 if (generate_PUSHS(cctx, key) == FAIL)
3694 return FAIL;
3695 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696
3697 // Check for duplicate keys, if using string keys.
3698 if (key != NULL)
3699 {
3700 item = dict_find(d, key, -1);
3701 if (item != NULL)
3702 {
3703 semsg(_(e_duplicate_key), key);
3704 goto failret;
3705 }
3706 item = dictitem_alloc(key);
3707 if (item != NULL)
3708 {
3709 item->di_tv.v_type = VAR_UNKNOWN;
3710 item->di_tv.v_lock = 0;
3711 if (dict_add(d, item) == FAIL)
3712 dictitem_free(item);
3713 }
3714 }
3715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 if (**arg != ':')
3717 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003718 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003719 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003720 else
3721 semsg(_(e_missing_dict_colon), *arg);
3722 return FAIL;
3723 }
3724 whitep = *arg + 1;
3725 if (!IS_WHITE_OR_NUL(*whitep))
3726 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003727 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 return FAIL;
3729 }
3730
Bram Moolenaar23c55272020-06-21 16:58:13 +02003731 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003732 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003733 *arg = NULL;
3734 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003735 }
3736
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003737 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003739 if (!is_const)
3740 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 ++count;
3742
Bram Moolenaar2c330432020-04-13 14:41:35 +02003743 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003744 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003745 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003746 *arg = NULL;
3747 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 if (**arg == '}')
3750 break;
3751 if (**arg != ',')
3752 {
3753 semsg(_(e_missing_dict_comma), *arg);
3754 goto failret;
3755 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003756 if (IS_WHITE_OR_NUL(*whitep))
3757 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003758 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003759 return FAIL;
3760 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003761 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003762 if (!IS_WHITE_OR_NUL(*whitep))
3763 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003764 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003765 return FAIL;
3766 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003767 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 }
3769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 *arg = *arg + 1;
3771
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003772 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003773 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003774 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003775 *arg += STRLEN(*arg);
3776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003778 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 return generate_NEWDICT(cctx, count);
3780
3781failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003782 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003783 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003784 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003785 *arg = (char_u *)"";
3786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 dict_unref(d);
3788 return FAIL;
3789}
3790
3791/*
3792 * Compile "&option".
3793 */
3794 static int
3795compile_get_option(char_u **arg, cctx_T *cctx)
3796{
3797 typval_T rettv;
3798 char_u *start = *arg;
3799 int ret;
3800
3801 // parse the option and get the current value to get the type.
3802 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003803 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 if (ret == OK)
3805 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003806 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003807 char_u *name = vim_strnsave(start, *arg - start);
3808 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3809 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810
3811 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3812 vim_free(name);
3813 }
3814 clear_tv(&rettv);
3815
3816 return ret;
3817}
3818
3819/*
3820 * Compile "$VAR".
3821 */
3822 static int
3823compile_get_env(char_u **arg, cctx_T *cctx)
3824{
3825 char_u *start = *arg;
3826 int len;
3827 int ret;
3828 char_u *name;
3829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 ++*arg;
3831 len = get_env_len(arg);
3832 if (len == 0)
3833 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003834 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 return FAIL;
3836 }
3837
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003838 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 name = vim_strnsave(start, len + 1);
3840 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3841 vim_free(name);
3842 return ret;
3843}
3844
3845/*
3846 * Compile "@r".
3847 */
3848 static int
3849compile_get_register(char_u **arg, cctx_T *cctx)
3850{
3851 int ret;
3852
3853 ++*arg;
3854 if (**arg == NUL)
3855 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003856 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 return FAIL;
3858 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003859 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 {
3861 emsg_invreg(**arg);
3862 return FAIL;
3863 }
3864 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3865 ++*arg;
3866 return ret;
3867}
3868
3869/*
3870 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003871 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 */
3873 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003874apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003876 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877
3878 // this works from end to start
3879 while (p > start)
3880 {
3881 --p;
3882 if (*p == '-' || *p == '+')
3883 {
3884 // only '-' has an effect, for '+' we only check the type
3885#ifdef FEAT_FLOAT
3886 if (rettv->v_type == VAR_FLOAT)
3887 {
3888 if (*p == '-')
3889 rettv->vval.v_float = -rettv->vval.v_float;
3890 }
3891 else
3892#endif
3893 {
3894 varnumber_T val;
3895 int error = FALSE;
3896
3897 // tv_get_number_chk() accepts a string, but we don't want that
3898 // here
3899 if (check_not_string(rettv) == FAIL)
3900 return FAIL;
3901 val = tv_get_number_chk(rettv, &error);
3902 clear_tv(rettv);
3903 if (error)
3904 return FAIL;
3905 if (*p == '-')
3906 val = -val;
3907 rettv->v_type = VAR_NUMBER;
3908 rettv->vval.v_number = val;
3909 }
3910 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003911 else if (numeric_only)
3912 {
3913 ++p;
3914 break;
3915 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003916 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 {
3918 int v = tv2bool(rettv);
3919
3920 // '!' is permissive in the type.
3921 clear_tv(rettv);
3922 rettv->v_type = VAR_BOOL;
3923 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3924 }
3925 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003926 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 return OK;
3928}
3929
3930/*
3931 * Recognize v: variables that are constants and set "rettv".
3932 */
3933 static void
3934get_vim_constant(char_u **arg, typval_T *rettv)
3935{
3936 if (STRNCMP(*arg, "v:true", 6) == 0)
3937 {
3938 rettv->v_type = VAR_BOOL;
3939 rettv->vval.v_number = VVAL_TRUE;
3940 *arg += 6;
3941 }
3942 else if (STRNCMP(*arg, "v:false", 7) == 0)
3943 {
3944 rettv->v_type = VAR_BOOL;
3945 rettv->vval.v_number = VVAL_FALSE;
3946 *arg += 7;
3947 }
3948 else if (STRNCMP(*arg, "v:null", 6) == 0)
3949 {
3950 rettv->v_type = VAR_SPECIAL;
3951 rettv->vval.v_number = VVAL_NULL;
3952 *arg += 6;
3953 }
3954 else if (STRNCMP(*arg, "v:none", 6) == 0)
3955 {
3956 rettv->v_type = VAR_SPECIAL;
3957 rettv->vval.v_number = VVAL_NONE;
3958 *arg += 6;
3959 }
3960}
3961
Bram Moolenaar657137c2021-01-09 15:45:23 +01003962 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003963get_compare_type(char_u *p, int *len, int *type_is)
3964{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003965 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003966 int i;
3967
3968 switch (p[0])
3969 {
3970 case '=': if (p[1] == '=')
3971 type = EXPR_EQUAL;
3972 else if (p[1] == '~')
3973 type = EXPR_MATCH;
3974 break;
3975 case '!': if (p[1] == '=')
3976 type = EXPR_NEQUAL;
3977 else if (p[1] == '~')
3978 type = EXPR_NOMATCH;
3979 break;
3980 case '>': if (p[1] != '=')
3981 {
3982 type = EXPR_GREATER;
3983 *len = 1;
3984 }
3985 else
3986 type = EXPR_GEQUAL;
3987 break;
3988 case '<': if (p[1] != '=')
3989 {
3990 type = EXPR_SMALLER;
3991 *len = 1;
3992 }
3993 else
3994 type = EXPR_SEQUAL;
3995 break;
3996 case 'i': if (p[1] == 's')
3997 {
3998 // "is" and "isnot"; but not a prefix of a name
3999 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4000 *len = 5;
4001 i = p[*len];
4002 if (!isalnum(i) && i != '_')
4003 {
4004 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4005 *type_is = TRUE;
4006 }
4007 }
4008 break;
4009 }
4010 return type;
4011}
4012
4013/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004014 * Skip over an expression, ignoring most errors.
4015 */
4016 static void
4017skip_expr_cctx(char_u **arg, cctx_T *cctx)
4018{
4019 evalarg_T evalarg;
4020
4021 CLEAR_FIELD(evalarg);
4022 evalarg.eval_cctx = cctx;
4023 skip_expr(arg, &evalarg);
4024}
4025
4026/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004028 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 */
4030 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004031compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004033 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034
4035 // this works from end to start
4036 while (p > start)
4037 {
4038 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004039 while (VIM_ISWHITE(*p))
4040 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 if (*p == '-' || *p == '+')
4042 {
4043 int negate = *p == '-';
4044 isn_T *isn;
4045
4046 // TODO: check type
4047 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4048 {
4049 --p;
4050 if (*p == '-')
4051 negate = !negate;
4052 }
4053 // only '-' has an effect, for '+' we only check the type
4054 if (negate)
4055 isn = generate_instr(cctx, ISN_NEGATENR);
4056 else
4057 isn = generate_instr(cctx, ISN_CHECKNR);
4058 if (isn == NULL)
4059 return FAIL;
4060 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004061 else if (numeric_only)
4062 {
4063 ++p;
4064 break;
4065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 else
4067 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004068 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004070 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004072 if (p[-1] == '!')
4073 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004076 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 return FAIL;
4078 }
4079 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004080 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 return OK;
4082}
4083
4084/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004085 * Compile "(expression)": recursive!
4086 * Return FAIL/OK.
4087 */
4088 static int
4089compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4090{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004091 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004092 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004093
Bram Moolenaar24156692021-01-14 20:35:49 +01004094 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4095 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004096 if (ppconst->pp_used <= PPSIZE - 10)
4097 {
4098 ret = compile_expr1(arg, cctx, ppconst);
4099 }
4100 else
4101 {
4102 // Not enough space in ppconst, flush constants.
4103 if (generate_ppconst(cctx, ppconst) == FAIL)
4104 return FAIL;
4105 ret = compile_expr0(arg, cctx);
4106 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004107 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4108 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004109 if (**arg == ')')
4110 ++*arg;
4111 else if (ret == OK)
4112 {
4113 emsg(_(e_missing_close));
4114 ret = FAIL;
4115 }
4116 return ret;
4117}
4118
4119/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004121 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 */
4123 static int
4124compile_subscript(
4125 char_u **arg,
4126 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004127 char_u *start_leader,
4128 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004129 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004131 char_u *name_start = *end_leader;
4132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 for (;;)
4134 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004135 char_u *p = skipwhite(*arg);
4136
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004137 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004138 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004139 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004140
4141 // If a following line starts with "->{" or "->X" advance to that
4142 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004143 // Also if a following line starts with ".x".
4144 if (next != NULL &&
4145 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004146 && (next[2] == '{'
4147 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004148 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004149 {
4150 next = next_line_from_context(cctx, TRUE);
4151 if (next == NULL)
4152 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004153 *arg = next;
4154 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004155 }
4156 }
4157
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004158 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004159 // not a function call.
4160 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004162 garray_T *stack = &cctx->ctx_type_stack;
4163 type_T *type;
4164 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004166 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004167 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004168 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004171 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4172
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004173 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004174 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004176 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 return FAIL;
4178 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004179 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004181 char_u *pstart = p;
4182
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004183 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004184 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004185 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 // something->method()
4188 // Apply the '!', '-' and '+' first:
4189 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004190 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004193 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004194 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004195 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004196 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004197 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004198 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004199 garray_T *stack = &cctx->ctx_type_stack;
4200 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004201 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004202 int expr_isn_start = cctx->ctx_instr.ga_len;
4203 int expr_isn_end;
4204 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004205
4206 // Funcref call: list->(Refs[2])(arg)
4207 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004208 //
4209 // Fist compile the function expression.
4210 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004211 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004212
4213 // Remember the next instruction index, where the instructions
4214 // for arguments are being written.
4215 expr_isn_end = cctx->ctx_instr.ga_len;
4216
4217 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004218 if (**arg != '(')
4219 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004220 if (*skipwhite(*arg) == '(')
4221 emsg(_(e_nowhitespace));
4222 else
4223 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004224 return FAIL;
4225 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004226 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004227 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004228 return FAIL;
4229
Bram Moolenaar2927c072021-04-05 19:41:21 +02004230 // Move the instructions for the arguments to before the
4231 // instructions of the expression and move the type of the
4232 // expression after the argument types. This is what ISN_PCALL
4233 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004234 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004235 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4236 if (arg_isn_count > 0)
4237 {
4238 int expr_isn_count = expr_isn_end - expr_isn_start;
4239 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4240
4241 if (isn == NULL)
4242 return FAIL;
4243 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4244 + expr_isn_start,
4245 sizeof(isn_T) * expr_isn_count);
4246 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4247 + expr_isn_start,
4248 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4249 sizeof(isn_T) * arg_isn_count);
4250 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4251 + expr_isn_start + arg_isn_count,
4252 isn, sizeof(isn_T) * expr_isn_count);
4253 vim_free(isn);
4254
4255 type = ((type_T **)stack->ga_data)[type_idx_start];
4256 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4257 ((type_T **)stack->ga_data) + type_idx_start + 1,
4258 sizeof(type_T *)
4259 * (stack->ga_len - type_idx_start - 1));
4260 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4261 }
4262
Bram Moolenaar7e368202020-12-25 21:56:57 +01004263 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4264 if (generate_PCALL(cctx, argcount,
4265 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 return FAIL;
4267 }
4268 else
4269 {
4270 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004271 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004272 if (!eval_isnamec1(*p))
4273 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004274 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004275 return FAIL;
4276 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004277 if (ASCII_ISALPHA(*p) && p[1] == ':')
4278 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004279 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 ;
4281 if (*p != '(')
4282 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004283 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 return FAIL;
4285 }
4286 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004287 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 return FAIL;
4289 }
4290 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004291 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004293 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004294
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004295 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004296 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004297 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004298 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004299 // TODO: more arguments
4300 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004301 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004302 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004303 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004304
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004305 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004306 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004307 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004308 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004309 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004310 // missing first index is equal to zero
4311 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004312 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004313 else
4314 {
4315 if (compile_expr0(arg, cctx) == FAIL)
4316 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004317 if (**arg == ':')
4318 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004319 semsg(_(e_white_space_required_before_and_after_str_at_str),
4320 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004321 return FAIL;
4322 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004323 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004324 return FAIL;
4325 *arg = skipwhite(*arg);
4326 }
4327 if (**arg == ':')
4328 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004329 is_slice = TRUE;
4330 ++*arg;
4331 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4332 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004333 semsg(_(e_white_space_required_before_and_after_str_at_str),
4334 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004335 return FAIL;
4336 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004337 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004338 return FAIL;
4339 if (**arg == ']')
4340 // missing second index is equal to end of string
4341 generate_PUSHNR(cctx, -1);
4342 else
4343 {
4344 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004345 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004346 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004347 return FAIL;
4348 *arg = skipwhite(*arg);
4349 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004350 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351
4352 if (**arg != ']')
4353 {
4354 emsg(_(e_missbrac));
4355 return FAIL;
4356 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004357 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358
Bram Moolenaare42939a2021-04-05 17:11:17 +02004359 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004360 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004362 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004364 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004365 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004366 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004367 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004368
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004369 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004370 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004371 {
4372 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004373 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004374 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004375 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004376 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 while (eval_isnamec(*p))
4378 MB_PTR_ADV(p);
4379 if (p == *arg)
4380 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004381 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 return FAIL;
4383 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004384 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 return FAIL;
4386 *arg = p;
4387 }
4388 else
4389 break;
4390 }
4391
4392 // TODO - see handle_subscript():
4393 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4394 // Don't do this when "Func" is already a partial that was bound
4395 // explicitly (pt_auto is FALSE).
4396
4397 return OK;
4398}
4399
4400/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004401 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4402 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004404 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004405 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004406 *
4407 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 */
4409
4410/*
4411 * number number constant
4412 * 0zFFFFFFFF Blob constant
4413 * "string" string constant
4414 * 'string' literal string constant
4415 * &option-name option value
4416 * @r register contents
4417 * identifier variable value
4418 * function() function call
4419 * $VAR environment variable
4420 * (expression) nested expression
4421 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004422 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 *
4424 * Also handle:
4425 * ! in front logical NOT
4426 * - in front unary minus
4427 * + in front unary plus (ignored)
4428 * trailing (arg) funcref/partial call
4429 * trailing [] subscript in String or List
4430 * trailing .name entry in Dictionary
4431 * trailing ->name() method call
4432 */
4433 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004434compile_expr7(
4435 char_u **arg,
4436 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004437 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 char_u *start_leader, *end_leader;
4440 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004441 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004442 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004444 ppconst->pp_is_const = FALSE;
4445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 /*
4447 * Skip '!', '-' and '+' characters. They are handled later.
4448 */
4449 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004450 if (eval_leader(arg, TRUE) == FAIL)
4451 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 end_leader = *arg;
4453
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004454 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 switch (**arg)
4456 {
4457 /*
4458 * Number constant.
4459 */
4460 case '0': // also for blob starting with 0z
4461 case '1':
4462 case '2':
4463 case '3':
4464 case '4':
4465 case '5':
4466 case '6':
4467 case '7':
4468 case '8':
4469 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004470 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004472 // Apply "-" and "+" just before the number now, right to
4473 // left. Matters especially when "->" follows. Stops at
4474 // '!'.
4475 if (apply_leader(rettv, TRUE,
4476 start_leader, &end_leader) == FAIL)
4477 {
4478 clear_tv(rettv);
4479 return FAIL;
4480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 break;
4482
4483 /*
4484 * String constant: "string".
4485 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004486 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 return FAIL;
4488 break;
4489
4490 /*
4491 * Literal string constant: 'str''ing'.
4492 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004493 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 return FAIL;
4495 break;
4496
4497 /*
4498 * Constant Vim variable.
4499 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004500 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 ret = NOTDONE;
4502 break;
4503
4504 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004505 * "true" constant
4506 */
4507 case 't': if (STRNCMP(*arg, "true", 4) == 0
4508 && !eval_isnamec((*arg)[4]))
4509 {
4510 *arg += 4;
4511 rettv->v_type = VAR_BOOL;
4512 rettv->vval.v_number = VVAL_TRUE;
4513 }
4514 else
4515 ret = NOTDONE;
4516 break;
4517
4518 /*
4519 * "false" constant
4520 */
4521 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4522 && !eval_isnamec((*arg)[5]))
4523 {
4524 *arg += 5;
4525 rettv->v_type = VAR_BOOL;
4526 rettv->vval.v_number = VVAL_FALSE;
4527 }
4528 else
4529 ret = NOTDONE;
4530 break;
4531
4532 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004533 * "null" constant
4534 */
4535 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004536 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004537 {
4538 *arg += 4;
4539 rettv->v_type = VAR_SPECIAL;
4540 rettv->vval.v_number = VVAL_NULL;
4541 }
4542 else
4543 ret = NOTDONE;
4544 break;
4545
4546 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 * List: [expr, expr]
4548 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004549 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4550 return FAIL;
4551 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 break;
4553
4554 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 * Dictionary: {'key': val, 'key': val}
4556 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004557 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4558 return FAIL;
4559 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 break;
4561
4562 /*
4563 * Option value: &name
4564 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004565 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4566 return FAIL;
4567 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 break;
4569
4570 /*
4571 * Environment variable: $VAR.
4572 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004573 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4574 return FAIL;
4575 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 break;
4577
4578 /*
4579 * Register contents: @r.
4580 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004581 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4582 return FAIL;
4583 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 break;
4585 /*
4586 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004587 * lambda: (arg, arg) => expr
4588 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004590 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4591 ret = compile_lambda(arg, cctx);
4592 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004593 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 break;
4595
4596 default: ret = NOTDONE;
4597 break;
4598 }
4599 if (ret == FAIL)
4600 return FAIL;
4601
Bram Moolenaar1c747212020-05-09 18:28:34 +02004602 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004604 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004605 clear_tv(rettv);
4606 else
4607 // A constant expression can possibly be handled compile time,
4608 // return the value instead of generating code.
4609 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 }
4611 else if (ret == NOTDONE)
4612 {
4613 char_u *p;
4614 int r;
4615
4616 if (!eval_isnamec1(**arg))
4617 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004618 if (!vim9_bad_comment(*arg))
4619 {
4620 if (ends_excmd(*skipwhite(*arg)))
4621 semsg(_(e_empty_expression_str), *arg);
4622 else
4623 semsg(_(e_name_expected_str), *arg);
4624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 return FAIL;
4626 }
4627
4628 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004629 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004630 if (p - *arg == (size_t)1 && **arg == '_')
4631 {
4632 emsg(_(e_cannot_use_underscore_here));
4633 return FAIL;
4634 }
4635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004637 {
4638 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004641 {
4642 if (generate_ppconst(cctx, ppconst) == FAIL)
4643 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004644 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004645 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 if (r == FAIL)
4647 return FAIL;
4648 }
4649
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004650 // Handle following "[]", ".member", etc.
4651 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004652 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004653 ppconst) == FAIL)
4654 return FAIL;
4655 if (ppconst->pp_used > 0)
4656 {
4657 // apply the '!', '-' and '+' before the constant
4658 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004659 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004660 return FAIL;
4661 return OK;
4662 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004663 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004665 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666}
4667
4668/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004669 * Give the "white on both sides" error, taking the operator from "p[len]".
4670 */
4671 void
4672error_white_both(char_u *op, int len)
4673{
4674 char_u buf[10];
4675
4676 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004677 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004678}
4679
4680/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004681 * <type>expr7: runtime type check / conversion
4682 */
4683 static int
4684compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4685{
4686 type_T *want_type = NULL;
4687
4688 // Recognize <type>
4689 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4690 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004691 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004692 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4693 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004694 return FAIL;
4695
4696 if (**arg != '>')
4697 {
4698 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004699 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004700 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004701 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004702 return FAIL;
4703 }
4704 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004705 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004706 return FAIL;
4707 }
4708
4709 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4710 return FAIL;
4711
4712 if (want_type != NULL)
4713 {
4714 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004715 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004716 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004717
Bram Moolenaard1103582020-08-14 22:44:25 +02004718 generate_ppconst(cctx, ppconst);
4719 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004720 where.wt_index = 0;
4721 where.wt_variable = FALSE;
4722 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004723 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004724 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004725 return FAIL;
4726 }
4727 }
4728
4729 return OK;
4730}
4731
4732/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733 * * number multiplication
4734 * / number division
4735 * % number modulo
4736 */
4737 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004738compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739{
4740 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004741 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004742 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004744 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004745 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 return FAIL;
4747
4748 /*
4749 * Repeat computing, until no "*", "/" or "%" is following.
4750 */
4751 for (;;)
4752 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004753 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754 if (*op != '*' && *op != '/' && *op != '%')
4755 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004756 if (next != NULL)
4757 {
4758 *arg = next_line_from_context(cctx, TRUE);
4759 op = skipwhite(*arg);
4760 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004761
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004762 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004764 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004765 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004767 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004768 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004770 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004771 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004773
4774 if (ppconst->pp_used == ppconst_used + 2
4775 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4776 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004777 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004778 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4779 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4780 varnumber_T res = 0;
4781 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004783 // both are numbers: compute the result
4784 switch (*op)
4785 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004786 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004787 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004788 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004789 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004790 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004791 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004792 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004793 break;
4794 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004795 if (failed)
4796 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004797 tv1->vval.v_number = res;
4798 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004799 }
4800 else
4801 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004802 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004803 generate_two_op(cctx, op);
4804 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 }
4806
4807 return OK;
4808}
4809
4810/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004811 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 * - number subtraction
4813 * .. string concatenation
4814 */
4815 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004816compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817{
4818 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004819 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004821 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822
4823 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004824 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 return FAIL;
4826
4827 /*
4828 * Repeat computing, until no "+", "-" or ".." is following.
4829 */
4830 for (;;)
4831 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004832 op = may_peek_next_line(cctx, *arg, &next);
4833 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004835 if (op[0] == op[1] && *op != '.' && next)
4836 // Finding "++" or "--" on the next line is a separate command.
4837 // But ".." is concatenation.
4838 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004840 if (next != NULL)
4841 {
4842 *arg = next_line_from_context(cctx, TRUE);
4843 op = skipwhite(*arg);
4844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004846 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004848 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004849 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 }
4851
Bram Moolenaare0de1712020-12-02 17:36:54 +01004852 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004853 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004855 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004856 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 return FAIL;
4858
Bram Moolenaara5565e42020-05-09 15:44:01 +02004859 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004860 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004861 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4862 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4863 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4864 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004866 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4867 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004868
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004869 // concat/subtract/add constant numbers
4870 if (*op == '+')
4871 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4872 else if (*op == '-')
4873 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4874 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004875 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004876 // concatenate constant strings
4877 char_u *s1 = tv1->vval.v_string;
4878 char_u *s2 = tv2->vval.v_string;
4879 size_t len1 = STRLEN(s1);
4880
4881 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4882 if (tv1->vval.v_string == NULL)
4883 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004884 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004885 return FAIL;
4886 }
4887 mch_memmove(tv1->vval.v_string, s1, len1);
4888 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004889 vim_free(s1);
4890 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004891 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004892 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 }
4894 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004895 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004896 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004897 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004898 if (*op == '.')
4899 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004900 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4901 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004902 return FAIL;
4903 generate_instr_drop(cctx, ISN_CONCAT, 1);
4904 }
4905 else
4906 generate_two_op(cctx, op);
4907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 }
4909
4910 return OK;
4911}
4912
4913/*
4914 * expr5a == expr5b
4915 * expr5a =~ expr5b
4916 * expr5a != expr5b
4917 * expr5a !~ expr5b
4918 * expr5a > expr5b
4919 * expr5a >= expr5b
4920 * expr5a < expr5b
4921 * expr5a <= expr5b
4922 * expr5a is expr5b
4923 * expr5a isnot expr5b
4924 *
4925 * Produces instructions:
4926 * EVAL expr5a Push result of "expr5a"
4927 * EVAL expr5b Push result of "expr5b"
4928 * COMPARE one of the compare instructions
4929 */
4930 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004931compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004933 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004935 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004938 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939
4940 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004941 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 return FAIL;
4943
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004944 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004945 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946
4947 /*
4948 * If there is a comparative operator, use it.
4949 */
4950 if (type != EXPR_UNKNOWN)
4951 {
4952 int ic = FALSE; // Default: do not ignore case
4953
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004954 if (next != NULL)
4955 {
4956 *arg = next_line_from_context(cctx, TRUE);
4957 p = skipwhite(*arg);
4958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 if (type_is && (p[len] == '?' || p[len] == '#'))
4960 {
4961 semsg(_(e_invexpr2), *arg);
4962 return FAIL;
4963 }
4964 // extra question mark appended: ignore case
4965 if (p[len] == '?')
4966 {
4967 ic = TRUE;
4968 ++len;
4969 }
4970 // extra '#' appended: match case (ignored)
4971 else if (p[len] == '#')
4972 ++len;
4973 // nothing appended: match case
4974
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004975 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004977 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004978 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 }
4980
4981 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004982 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004983 return FAIL;
4984
Bram Moolenaara5565e42020-05-09 15:44:01 +02004985 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 return FAIL;
4987
Bram Moolenaara5565e42020-05-09 15:44:01 +02004988 if (ppconst->pp_used == ppconst_used + 2)
4989 {
4990 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4991 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4992 int ret;
4993
4994 // Both sides are a constant, compute the result now.
4995 // First check for a valid combination of types, this is more
4996 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004997 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004998 ret = FAIL;
4999 else
5000 {
5001 ret = typval_compare(tv1, tv2, type, ic);
5002 tv1->v_type = VAR_BOOL;
5003 tv1->vval.v_number = tv1->vval.v_number
5004 ? VVAL_TRUE : VVAL_FALSE;
5005 clear_tv(tv2);
5006 --ppconst->pp_used;
5007 }
5008 return ret;
5009 }
5010
5011 generate_ppconst(cctx, ppconst);
5012 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 }
5014
5015 return OK;
5016}
5017
Bram Moolenaar7f141552020-05-09 17:35:53 +02005018static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020/*
5021 * Compile || or &&.
5022 */
5023 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005024compile_and_or(
5025 char_u **arg,
5026 cctx_T *cctx,
5027 char *op,
5028 ppconst_T *ppconst,
5029 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005031 char_u *next;
5032 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 int opchar = *op;
5034
5035 if (p[0] == opchar && p[1] == opchar)
5036 {
5037 garray_T *instr = &cctx->ctx_instr;
5038 garray_T end_ga;
5039
5040 /*
5041 * Repeat until there is no following "||" or "&&"
5042 */
5043 ga_init2(&end_ga, sizeof(int), 10);
5044 while (p[0] == opchar && p[1] == opchar)
5045 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005046 long start_lnum = SOURCING_LNUM;
5047 int start_ctx_lnum = cctx->ctx_lnum;
5048 int save_lnum;
5049
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005050 if (next != NULL)
5051 {
5052 *arg = next_line_from_context(cctx, TRUE);
5053 p = skipwhite(*arg);
5054 }
5055
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005056 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5057 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005058 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005059 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005060 return FAIL;
5061 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005063 // TODO: use ppconst if the value is a constant and check
5064 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005065 generate_ppconst(cctx, ppconst);
5066
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005067 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005068 SOURCING_LNUM = start_lnum;
5069 save_lnum = cctx->ctx_lnum;
5070 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005071 if (bool_on_stack(cctx) == FAIL)
5072 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005073 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005074 ga_clear(&end_ga);
5075 return FAIL;
5076 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005077 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 if (ga_grow(&end_ga, 1) == FAIL)
5080 {
5081 ga_clear(&end_ga);
5082 return FAIL;
5083 }
5084 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5085 ++end_ga.ga_len;
5086 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005087 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088
5089 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005090 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005091 {
5092 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005093 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005094 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005095
Bram Moolenaara5565e42020-05-09 15:44:01 +02005096 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5097 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098 {
5099 ga_clear(&end_ga);
5100 return FAIL;
5101 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005102
5103 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005105 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005107 // Every part must evaluate to a bool.
5108 if (bool_on_stack(cctx) == FAIL)
5109 {
5110 ga_clear(&end_ga);
5111 return FAIL;
5112 }
5113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 // Fill in the end label in all jumps.
5115 while (end_ga.ga_len > 0)
5116 {
5117 isn_T *isn;
5118
5119 --end_ga.ga_len;
5120 isn = ((isn_T *)instr->ga_data)
5121 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5122 isn->isn_arg.jump.jump_where = instr->ga_len;
5123 }
5124 ga_clear(&end_ga);
5125 }
5126
5127 return OK;
5128}
5129
5130/*
5131 * expr4a && expr4a && expr4a logical AND
5132 *
5133 * Produces instructions:
5134 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005135 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005136 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005138 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 * EVAL expr4c Push result of "expr4c"
5140 * end:
5141 */
5142 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005143compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005145 int ppconst_used = ppconst->pp_used;
5146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005148 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149 return FAIL;
5150
5151 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005152 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153}
5154
5155/*
5156 * expr3a || expr3b || expr3c logical OR
5157 *
5158 * Produces instructions:
5159 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005160 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005161 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005163 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164 * EVAL expr3c Push result of "expr3c"
5165 * end:
5166 */
5167 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005168compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005169{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005170 int ppconst_used = ppconst->pp_used;
5171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005173 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005174 return FAIL;
5175
5176 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005177 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178}
5179
5180/*
5181 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005183 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184 * JUMP_IF_FALSE alt jump if false
5185 * EVAL expr1a
5186 * JUMP_ALWAYS end
5187 * alt: EVAL expr1b
5188 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005189 *
5190 * Toplevel expression: expr2 ?? expr1
5191 * Produces instructions:
5192 * EVAL expr2 Push result of "expr2"
5193 * JUMP_AND_KEEP_IF_TRUE end jump if true
5194 * EVAL expr1
5195 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196 */
5197 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005198compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199{
5200 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005201 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005202 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203
Bram Moolenaar3988f642020-08-27 22:43:03 +02005204 // Ignore all kinds of errors when not producing code.
5205 if (cctx->ctx_skip == SKIP_YES)
5206 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005207 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005208 return OK;
5209 }
5210
Bram Moolenaar61a89812020-05-07 16:58:17 +02005211 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005212 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213 return FAIL;
5214
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005215 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216 if (*p == '?')
5217 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005218 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 garray_T *instr = &cctx->ctx_instr;
5220 garray_T *stack = &cctx->ctx_type_stack;
5221 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005222 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005224 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005225 int has_const_expr = FALSE;
5226 int const_value = FALSE;
5227 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005229 if (next != NULL)
5230 {
5231 *arg = next_line_from_context(cctx, TRUE);
5232 p = skipwhite(*arg);
5233 }
5234
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005235 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005236 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005237 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005238 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005239 return FAIL;
5240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241
Bram Moolenaara5565e42020-05-09 15:44:01 +02005242 if (ppconst->pp_used == ppconst_used + 1)
5243 {
5244 // the condition is a constant, we know whether the ? or the :
5245 // expression is to be evaluated.
5246 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005247 if (op_falsy)
5248 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5249 else
5250 {
5251 int error = FALSE;
5252
5253 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5254 &error);
5255 if (error)
5256 return FAIL;
5257 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005258 cctx->ctx_skip = save_skip == SKIP_YES ||
5259 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5260
5261 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5262 // "left ?? right" and "left" is truthy: produce "left"
5263 generate_ppconst(cctx, ppconst);
5264 else
5265 {
5266 clear_tv(&ppconst->pp_tv[ppconst_used]);
5267 --ppconst->pp_used;
5268 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005269 }
5270 else
5271 {
5272 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005273 if (op_falsy)
5274 end_idx = instr->ga_len;
5275 generate_JUMP(cctx, op_falsy
5276 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5277 if (op_falsy)
5278 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005279 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005280
5281 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005282 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005283 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005284 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005285 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286
Bram Moolenaara5565e42020-05-09 15:44:01 +02005287 if (!has_const_expr)
5288 {
5289 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005290
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005291 if (!op_falsy)
5292 {
5293 // remember the type and drop it
5294 --stack->ga_len;
5295 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005297 end_idx = instr->ga_len;
5298 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005299
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005300 // jump here from JUMP_IF_FALSE
5301 isn = ((isn_T *)instr->ga_data) + alt_idx;
5302 isn->isn_arg.jump.jump_where = instr->ga_len;
5303 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005305
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005306 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005307 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005308 // Check for the ":".
5309 p = may_peek_next_line(cctx, *arg, &next);
5310 if (*p != ':')
5311 {
5312 emsg(_(e_missing_colon));
5313 return FAIL;
5314 }
5315 if (next != NULL)
5316 {
5317 *arg = next_line_from_context(cctx, TRUE);
5318 p = skipwhite(*arg);
5319 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005320
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005321 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5322 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005323 semsg(_(e_white_space_required_before_and_after_str_at_str),
5324 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005325 return FAIL;
5326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005327
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005328 // evaluate the third expression
5329 if (has_const_expr)
5330 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005331 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005332 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005333 return FAIL;
5334 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5335 return FAIL;
5336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337
Bram Moolenaara5565e42020-05-09 15:44:01 +02005338 if (!has_const_expr)
5339 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005340 type_T **typep;
5341
Bram Moolenaara5565e42020-05-09 15:44:01 +02005342 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005343
Bram Moolenaara5565e42020-05-09 15:44:01 +02005344 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005345 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5346 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005347
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005348 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005349 isn = ((isn_T *)instr->ga_data) + end_idx;
5350 isn->isn_arg.jump.jump_where = instr->ga_len;
5351 }
5352
5353 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 }
5355 return OK;
5356}
5357
5358/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005359 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005360 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5361 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005362 */
5363 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005364compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005365{
5366 ppconst_T ppconst;
5367
5368 CLEAR_FIELD(ppconst);
5369 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5370 {
5371 clear_ppconst(&ppconst);
5372 return FAIL;
5373 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005374 if (is_const != NULL)
5375 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005376 if (generate_ppconst(cctx, &ppconst) == FAIL)
5377 return FAIL;
5378 return OK;
5379}
5380
5381/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005382 * Toplevel expression.
5383 */
5384 static int
5385compile_expr0(char_u **arg, cctx_T *cctx)
5386{
5387 return compile_expr0_ext(arg, cctx, NULL);
5388}
5389
5390/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005391 * Compile "return [expr]".
5392 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393 */
5394 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005395compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396{
5397 char_u *p = arg;
5398 garray_T *stack = &cctx->ctx_type_stack;
5399 type_T *stack_type;
5400
5401 if (*p != NUL && *p != '|' && *p != '\n')
5402 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005403 if (legacy)
5404 {
5405 int save_flags = cmdmod.cmod_flags;
5406
5407 generate_LEGACY_EVAL(cctx, p);
5408 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5409 0, cctx, FALSE, FALSE) == FAIL)
5410 return NULL;
5411 cmdmod.cmod_flags |= CMOD_LEGACY;
5412 (void)skip_expr(&p, NULL);
5413 cmdmod.cmod_flags = save_flags;
5414 }
5415 else
5416 {
5417 // compile return argument into instructions
5418 if (compile_expr0(&p, cctx) == FAIL)
5419 return NULL;
5420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005422 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005423 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005424 // "check_return_type" with uf_ret_type set to &t_unknown is used
5425 // for an inline function without a specified return type. Set the
5426 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005427 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005428 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005429 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5430 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005431 || (!check_return_type
5432 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005433 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005434 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005435 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005436 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005437 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005438 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5439 && stack_type->tt_type != VAR_VOID
5440 && stack_type->tt_type != VAR_UNKNOWN)
5441 {
5442 emsg(_(e_returning_value_in_function_without_return_type));
5443 return NULL;
5444 }
5445 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005446 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005447 return NULL;
5448 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450 }
5451 else
5452 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005453 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005454 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005455 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5456 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005458 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459 return NULL;
5460 }
5461
5462 // No argument, return zero.
5463 generate_PUSHNR(cctx, 0);
5464 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005465
5466 // Undo any command modifiers.
5467 generate_undo_cmdmods(cctx);
5468
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005469 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470 return NULL;
5471
5472 // "return val | endif" is possible
5473 return skipwhite(p);
5474}
5475
5476/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005477 * Get a line from the compilation context, compatible with exarg_T getline().
5478 * Return a pointer to the line in allocated memory.
5479 * Return NULL for end-of-file or some error.
5480 */
5481 static char_u *
5482exarg_getline(
5483 int c UNUSED,
5484 void *cookie,
5485 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005486 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005487{
5488 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005489 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005490
Bram Moolenaar66250c92020-08-20 15:02:42 +02005491 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005492 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005493 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005494 return NULL;
5495 ++cctx->ctx_lnum;
5496 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5497 // Comment lines result in NULL pointers, skip them.
5498 if (p != NULL)
5499 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005500 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005501}
5502
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005503 void
5504fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5505{
5506 eap->getline = exarg_getline;
5507 eap->cookie = cctx;
5508}
5509
Bram Moolenaar04b12692020-05-04 23:24:44 +02005510/*
5511 * Compile a nested :def command.
5512 */
5513 static char_u *
5514compile_nested_function(exarg_T *eap, cctx_T *cctx)
5515{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005516 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005517 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005518 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005519 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005520 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005521 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005522
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005523 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005524 {
5525 emsg(_(e_cannot_use_bang_with_nested_def));
5526 return NULL;
5527 }
5528
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005529 if (*name_start == '/')
5530 {
5531 name_end = skip_regexp(name_start + 1, '/', TRUE);
5532 if (*name_end == '/')
5533 ++name_end;
5534 eap->nextcmd = check_nextcmd(name_end);
5535 }
5536 if (name_end == name_start || *skipwhite(name_end) != '(')
5537 {
5538 if (!ends_excmd2(name_start, name_end))
5539 {
5540 semsg(_(e_invalid_command_str), eap->cmd);
5541 return NULL;
5542 }
5543
5544 // "def" or "def Name": list functions
5545 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5546 return NULL;
5547 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5548 }
5549
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005550 // Only g:Func() can use a namespace.
5551 if (name_start[1] == ':' && !is_global)
5552 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005553 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005554 return NULL;
5555 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005556 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005557 return NULL;
5558
Bram Moolenaar04b12692020-05-04 23:24:44 +02005559 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005560 fill_exarg_from_cctx(eap, cctx);
5561
Bram Moolenaar04b12692020-05-04 23:24:44 +02005562 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005563 lambda_name = vim_strsave(get_lambda_name());
5564 if (lambda_name == NULL)
5565 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005566 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005567
Bram Moolenaar822ba242020-05-24 23:00:18 +02005568 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005569 {
5570 r = eap->skip ? OK : FAIL;
5571 goto theend;
5572 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005573
5574 // copy over the block scope IDs before compiling
5575 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5576 {
5577 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5578
5579 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5580 if (ufunc->uf_block_ids != NULL)
5581 {
5582 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5583 sizeof(int) * block_depth);
5584 ufunc->uf_block_depth = block_depth;
5585 }
5586 }
5587
Bram Moolenaare99d4222021-06-13 14:01:26 +02005588 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
5589 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005590 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005591 {
5592 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005593 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005594 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005595
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005596 if (is_global)
5597 {
5598 char_u *func_name = vim_strnsave(name_start + 2,
5599 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005600
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005601 if (func_name == NULL)
5602 r = FAIL;
5603 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005604 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005605 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005606 lambda_name = NULL;
5607 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005608 }
5609 else
5610 {
5611 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005612 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005613 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005614
Bram Moolenaareef21022020-08-01 22:16:43 +02005615 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005616 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005617 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005618 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005619 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5620 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005621 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005622
5623theend:
5624 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005625 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005626}
5627
5628/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005629 * Return the length of an assignment operator, or zero if there isn't one.
5630 */
5631 int
5632assignment_len(char_u *p, int *heredoc)
5633{
5634 if (*p == '=')
5635 {
5636 if (p[1] == '<' && p[2] == '<')
5637 {
5638 *heredoc = TRUE;
5639 return 3;
5640 }
5641 return 1;
5642 }
5643 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5644 return 2;
5645 if (STRNCMP(p, "..=", 3) == 0)
5646 return 3;
5647 return 0;
5648}
5649
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005650/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005651 * Generate the load instruction for "name".
5652 */
5653 static void
5654generate_loadvar(
5655 cctx_T *cctx,
5656 assign_dest_T dest,
5657 char_u *name,
5658 lvar_T *lvar,
5659 type_T *type)
5660{
5661 switch (dest)
5662 {
5663 case dest_option:
5664 // TODO: check the option exists
5665 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5666 break;
5667 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005668 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5669 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5670 else
5671 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005672 break;
5673 case dest_buffer:
5674 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5675 break;
5676 case dest_window:
5677 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5678 break;
5679 case dest_tab:
5680 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5681 break;
5682 case dest_script:
5683 compile_load_scriptvar(cctx,
5684 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5685 break;
5686 case dest_env:
5687 // Include $ in the name here
5688 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5689 break;
5690 case dest_reg:
5691 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5692 break;
5693 case dest_vimvar:
5694 generate_LOADV(cctx, name + 2, TRUE);
5695 break;
5696 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005697 if (lvar->lv_from_outer > 0)
5698 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5699 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005700 else
5701 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5702 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005703 case dest_expr:
5704 // list or dict value should already be on the stack.
5705 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005706 }
5707}
5708
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005709/*
5710 * Skip over "[expr]" or ".member".
5711 * Does not check for any errors.
5712 */
5713 static char_u *
5714skip_index(char_u *start)
5715{
5716 char_u *p = start;
5717
5718 if (*p == '[')
5719 {
5720 p = skipwhite(p + 1);
5721 (void)skip_expr(&p, NULL);
5722 p = skipwhite(p);
5723 if (*p == ']')
5724 return p + 1;
5725 return p;
5726 }
5727 // if (*p == '.')
5728 return to_name_end(p + 1, TRUE);
5729}
5730
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005731 void
5732vim9_declare_error(char_u *name)
5733{
5734 char *scope = "";
5735
5736 switch (*name)
5737 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005738 case 'g': scope = _("global"); break;
5739 case 'b': scope = _("buffer"); break;
5740 case 'w': scope = _("window"); break;
5741 case 't': scope = _("tab"); break;
5742 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005743 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005744 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005745 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005746 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005747 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005748 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005749 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005750 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005751 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005752}
5753
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005754/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005755 * For one assignment figure out the type of destination. Return it in "dest".
5756 * When not recognized "dest" is not set.
5757 * For an option "opt_flags" is set.
5758 * For a v:var "vimvaridx" is set.
5759 * "type" is set to the destination type if known, unchanted otherwise.
5760 * Return FAIL if an error message was given.
5761 */
5762 static int
5763get_var_dest(
5764 char_u *name,
5765 assign_dest_T *dest,
5766 int cmdidx,
5767 int *opt_flags,
5768 int *vimvaridx,
5769 type_T **type,
5770 cctx_T *cctx)
5771{
5772 char_u *p;
5773
5774 if (*name == '&')
5775 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005776 int cc;
5777 long numval;
5778 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005779
5780 *dest = dest_option;
5781 if (cmdidx == CMD_final || cmdidx == CMD_const)
5782 {
5783 emsg(_(e_const_option));
5784 return FAIL;
5785 }
5786 p = name;
5787 p = find_option_end(&p, opt_flags);
5788 if (p == NULL)
5789 {
5790 // cannot happen?
5791 emsg(_(e_letunexp));
5792 return FAIL;
5793 }
5794 cc = *p;
5795 *p = NUL;
5796 opt_type = get_option_value(skip_option_env_lead(name),
5797 &numval, NULL, *opt_flags);
5798 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005799 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005800 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005801 case gov_unknown:
5802 semsg(_(e_unknown_option), name);
5803 return FAIL;
5804 case gov_string:
5805 case gov_hidden_string:
5806 *type = &t_string;
5807 break;
5808 case gov_bool:
5809 case gov_hidden_bool:
5810 *type = &t_bool;
5811 break;
5812 case gov_number:
5813 case gov_hidden_number:
5814 *type = &t_number;
5815 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005816 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005817 }
5818 else if (*name == '$')
5819 {
5820 *dest = dest_env;
5821 *type = &t_string;
5822 }
5823 else if (*name == '@')
5824 {
5825 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5826 {
5827 emsg_invreg(name[1]);
5828 return FAIL;
5829 }
5830 *dest = dest_reg;
5831 *type = &t_string;
5832 }
5833 else if (STRNCMP(name, "g:", 2) == 0)
5834 {
5835 *dest = dest_global;
5836 }
5837 else if (STRNCMP(name, "b:", 2) == 0)
5838 {
5839 *dest = dest_buffer;
5840 }
5841 else if (STRNCMP(name, "w:", 2) == 0)
5842 {
5843 *dest = dest_window;
5844 }
5845 else if (STRNCMP(name, "t:", 2) == 0)
5846 {
5847 *dest = dest_tab;
5848 }
5849 else if (STRNCMP(name, "v:", 2) == 0)
5850 {
5851 typval_T *vtv;
5852 int di_flags;
5853
5854 *vimvaridx = find_vim_var(name + 2, &di_flags);
5855 if (*vimvaridx < 0)
5856 {
5857 semsg(_(e_variable_not_found_str), name);
5858 return FAIL;
5859 }
5860 // We use the current value of "sandbox" here, is that OK?
5861 if (var_check_ro(di_flags, name, FALSE))
5862 return FAIL;
5863 *dest = dest_vimvar;
5864 vtv = get_vim_var_tv(*vimvaridx);
5865 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5866 }
5867 return OK;
5868}
5869
5870/*
5871 * Generate a STORE instruction for "dest", not being "dest_local".
5872 * Return FAIL when out of memory.
5873 */
5874 static int
5875generate_store_var(
5876 cctx_T *cctx,
5877 assign_dest_T dest,
5878 int opt_flags,
5879 int vimvaridx,
5880 int scriptvar_idx,
5881 int scriptvar_sid,
5882 type_T *type,
5883 char_u *name)
5884{
5885 switch (dest)
5886 {
5887 case dest_option:
5888 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5889 opt_flags);
5890 case dest_global:
5891 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005892 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5893 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005894 case dest_buffer:
5895 // include b: with the name, easier to execute that way
5896 return generate_STORE(cctx, ISN_STOREB, 0, name);
5897 case dest_window:
5898 // include w: with the name, easier to execute that way
5899 return generate_STORE(cctx, ISN_STOREW, 0, name);
5900 case dest_tab:
5901 // include t: with the name, easier to execute that way
5902 return generate_STORE(cctx, ISN_STORET, 0, name);
5903 case dest_env:
5904 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5905 case dest_reg:
5906 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5907 case dest_vimvar:
5908 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5909 case dest_script:
5910 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005911 // "s:" may be included in the name.
5912 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005913 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005914 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5915 scriptvar_sid, scriptvar_idx, type);
5916 case dest_local:
5917 case dest_expr:
5918 // cannot happen
5919 break;
5920 }
5921 return FAIL;
5922}
5923
Bram Moolenaar752fc692021-01-04 21:57:11 +01005924 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005925generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5926{
5927 if (lhs->lhs_dest != dest_local)
5928 return generate_store_var(cctx, lhs->lhs_dest,
5929 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5930 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5931 lhs->lhs_type, lhs->lhs_name);
5932
5933 if (lhs->lhs_lvar != NULL)
5934 {
5935 garray_T *instr = &cctx->ctx_instr;
5936 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5937
5938 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5939 // ISN_STORENR
5940 if (lhs->lhs_lvar->lv_from_outer == 0
5941 && instr->ga_len == instr_count + 1
5942 && isn->isn_type == ISN_PUSHNR)
5943 {
5944 varnumber_T val = isn->isn_arg.number;
5945 garray_T *stack = &cctx->ctx_type_stack;
5946
5947 isn->isn_type = ISN_STORENR;
5948 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5949 isn->isn_arg.storenr.stnr_val = val;
5950 if (stack->ga_len > 0)
5951 --stack->ga_len;
5952 }
5953 else if (lhs->lhs_lvar->lv_from_outer > 0)
5954 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5955 lhs->lhs_lvar->lv_from_outer);
5956 else
5957 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5958 }
5959 return OK;
5960}
5961
5962 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005963is_decl_command(int cmdidx)
5964{
5965 return cmdidx == CMD_let || cmdidx == CMD_var
5966 || cmdidx == CMD_final || cmdidx == CMD_const;
5967}
5968
5969/*
5970 * Figure out the LHS type and other properties for an assignment or one item
5971 * of ":unlet" with an index.
5972 * Returns OK or FAIL.
5973 */
5974 static int
5975compile_lhs(
5976 char_u *var_start,
5977 lhs_T *lhs,
5978 int cmdidx,
5979 int heredoc,
5980 int oplen,
5981 cctx_T *cctx)
5982{
5983 char_u *var_end;
5984 int is_decl = is_decl_command(cmdidx);
5985
5986 CLEAR_POINTER(lhs);
5987 lhs->lhs_dest = dest_local;
5988 lhs->lhs_vimvaridx = -1;
5989 lhs->lhs_scriptvar_idx = -1;
5990
5991 // "dest_end" is the end of the destination, including "[expr]" or
5992 // ".name".
5993 // "var_end" is the end of the variable/option/etc. name.
5994 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5995 if (*var_start == '@')
5996 var_end = var_start + 2;
5997 else
5998 {
5999 // skip over the leading "&", "&l:", "&g:" and "$"
6000 var_end = skip_option_env_lead(var_start);
6001 var_end = to_name_end(var_end, TRUE);
6002 }
6003
6004 // "a: type" is declaring variable "a" with a type, not dict "a:".
6005 if (is_decl && lhs->lhs_dest_end == var_start + 2
6006 && lhs->lhs_dest_end[-1] == ':')
6007 --lhs->lhs_dest_end;
6008 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6009 --var_end;
6010
6011 // compute the length of the destination without "[expr]" or ".name"
6012 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006013 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006014 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6015 if (lhs->lhs_name == NULL)
6016 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006017
6018 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6019 // Something follows after the variable: "var[idx]" or "var.key".
6020 lhs->lhs_has_index = TRUE;
6021
Bram Moolenaar752fc692021-01-04 21:57:11 +01006022 if (heredoc)
6023 lhs->lhs_type = &t_list_string;
6024 else
6025 lhs->lhs_type = &t_any;
6026
6027 if (cctx->ctx_skip != SKIP_YES)
6028 {
6029 int declare_error = FALSE;
6030
6031 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6032 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6033 &lhs->lhs_type, cctx) == FAIL)
6034 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006035 if (lhs->lhs_dest != dest_local
6036 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006037 {
6038 // Specific kind of variable recognized.
6039 declare_error = is_decl;
6040 }
6041 else
6042 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006043 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006044 if (check_reserved_name(lhs->lhs_name) == FAIL)
6045 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006046
6047 if (lookup_local(var_start, lhs->lhs_varlen,
6048 &lhs->lhs_local_lvar, cctx) == OK)
6049 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6050 else
6051 {
6052 CLEAR_FIELD(lhs->lhs_arg_lvar);
6053 if (arg_exists(var_start, lhs->lhs_varlen,
6054 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6055 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6056 {
6057 if (is_decl)
6058 {
6059 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6060 return FAIL;
6061 }
6062 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6063 }
6064 }
6065 if (lhs->lhs_lvar != NULL)
6066 {
6067 if (is_decl)
6068 {
6069 semsg(_(e_variable_already_declared), lhs->lhs_name);
6070 return FAIL;
6071 }
6072 }
6073 else
6074 {
6075 int script_namespace = lhs->lhs_varlen > 1
6076 && STRNCMP(var_start, "s:", 2) == 0;
6077 int script_var = (script_namespace
6078 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006079 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006080 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006081 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006082 imported_T *import =
6083 find_imported(var_start, lhs->lhs_varlen, cctx);
6084
6085 if (script_namespace || script_var || import != NULL)
6086 {
6087 char_u *rawname = lhs->lhs_name
6088 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6089
6090 if (is_decl)
6091 {
6092 if (script_namespace)
6093 semsg(_(e_cannot_declare_script_variable_in_function),
6094 lhs->lhs_name);
6095 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006096 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006097 lhs->lhs_name);
6098 return FAIL;
6099 }
6100 else if (cctx->ctx_ufunc->uf_script_ctx_version
6101 == SCRIPT_VERSION_VIM9
6102 && script_namespace
6103 && !script_var && import == NULL)
6104 {
6105 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6106 return FAIL;
6107 }
6108
6109 lhs->lhs_dest = dest_script;
6110
6111 // existing script-local variables should have a type
6112 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6113 if (import != NULL)
6114 lhs->lhs_scriptvar_sid = import->imp_sid;
6115 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6116 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006117 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006118 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006119 lhs->lhs_scriptvar_sid, rawname,
6120 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6121 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006122 if (lhs->lhs_scriptvar_idx >= 0)
6123 {
6124 scriptitem_T *si = SCRIPT_ITEM(
6125 lhs->lhs_scriptvar_sid);
6126 svar_T *sv =
6127 ((svar_T *)si->sn_var_vals.ga_data)
6128 + lhs->lhs_scriptvar_idx;
6129 lhs->lhs_type = sv->sv_type;
6130 }
6131 }
6132 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006133 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006134 == FAIL)
6135 return FAIL;
6136 }
6137 }
6138
6139 if (declare_error)
6140 {
6141 vim9_declare_error(lhs->lhs_name);
6142 return FAIL;
6143 }
6144 }
6145
6146 // handle "a:name" as a name, not index "name" on "a"
6147 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6148 var_end = lhs->lhs_dest_end;
6149
6150 if (lhs->lhs_dest != dest_option)
6151 {
6152 if (is_decl && *var_end == ':')
6153 {
6154 char_u *p;
6155
6156 // parse optional type: "let var: type = expr"
6157 if (!VIM_ISWHITE(var_end[1]))
6158 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006159 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006160 return FAIL;
6161 }
6162 p = skipwhite(var_end + 1);
6163 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6164 if (lhs->lhs_type == NULL)
6165 return FAIL;
6166 lhs->lhs_has_type = TRUE;
6167 }
6168 else if (lhs->lhs_lvar != NULL)
6169 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6170 }
6171
Bram Moolenaare42939a2021-04-05 17:11:17 +02006172 if (oplen == 3 && !heredoc
6173 && lhs->lhs_dest != dest_global
6174 && !lhs->lhs_has_index
6175 && lhs->lhs_type->tt_type != VAR_STRING
6176 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006177 {
6178 emsg(_(e_can_only_concatenate_to_string));
6179 return FAIL;
6180 }
6181
6182 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6183 && cctx->ctx_skip != SKIP_YES)
6184 {
6185 if (oplen > 1 && !heredoc)
6186 {
6187 // +=, /=, etc. require an existing variable
6188 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6189 return FAIL;
6190 }
6191 if (!is_decl)
6192 {
6193 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6194 return FAIL;
6195 }
6196
Bram Moolenaar3f327882021-03-17 20:56:38 +01006197 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006198 if ((lhs->lhs_type->tt_type == VAR_FUNC
6199 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006200 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006201 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006202
6203 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006204 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6205 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6206 if (lhs->lhs_lvar == NULL)
6207 return FAIL;
6208 lhs->lhs_new_local = TRUE;
6209 }
6210
6211 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006212 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006213 {
6214 // Something follows after the variable: "var[idx]" or "var.key".
6215 // TODO: should we also handle "->func()" here?
6216 if (is_decl)
6217 {
6218 emsg(_(e_cannot_use_index_when_declaring_variable));
6219 return FAIL;
6220 }
6221
6222 if (var_start[lhs->lhs_varlen] == '['
6223 || var_start[lhs->lhs_varlen] == '.')
6224 {
6225 char_u *after = var_start + lhs->lhs_varlen;
6226 char_u *p;
6227
6228 // Only the last index is used below, if there are others
6229 // before it generate code for the expression. Thus for
6230 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6231 for (;;)
6232 {
6233 p = skip_index(after);
6234 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006235 {
6236 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006237 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006238 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006239 after = p;
6240 }
6241 if (after > var_start + lhs->lhs_varlen)
6242 {
6243 lhs->lhs_varlen = after - var_start;
6244 lhs->lhs_dest = dest_expr;
6245 // We don't know the type before evaluating the expression,
6246 // use "any" until then.
6247 lhs->lhs_type = &t_any;
6248 }
6249
Bram Moolenaar752fc692021-01-04 21:57:11 +01006250 if (lhs->lhs_type->tt_member == NULL)
6251 lhs->lhs_member_type = &t_any;
6252 else
6253 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6254 }
6255 else
6256 {
6257 semsg("Not supported yet: %s", var_start);
6258 return FAIL;
6259 }
6260 }
6261 return OK;
6262}
6263
6264/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006265 * Figure out the LHS and check a few errors.
6266 */
6267 static int
6268compile_assign_lhs(
6269 char_u *var_start,
6270 lhs_T *lhs,
6271 int cmdidx,
6272 int is_decl,
6273 int heredoc,
6274 int oplen,
6275 cctx_T *cctx)
6276{
6277 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6278 return FAIL;
6279
6280 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6281 {
6282 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6283 return FAIL;
6284 }
6285 if (!is_decl && lhs->lhs_lvar != NULL
6286 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6287 {
6288 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6289 return FAIL;
6290 }
6291 return OK;
6292}
6293
6294/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006295 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6296 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006297 */
6298 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006299compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006300 char_u *var_start,
6301 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006302 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006303 cctx_T *cctx)
6304{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006305 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006306 char_u *p;
6307 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006308 int need_white_before = TRUE;
6309 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006310
Bram Moolenaar752fc692021-01-04 21:57:11 +01006311 p = var_start + varlen;
6312 if (*p == '[')
6313 {
6314 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006315 if (*p == ':')
6316 {
6317 // empty first index, push zero
6318 r = generate_PUSHNR(cctx, 0);
6319 need_white_before = FALSE;
6320 }
6321 else
6322 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006323
6324 if (r == OK && *skipwhite(p) == ':')
6325 {
6326 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006327 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006328 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006329 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006330 empty_second = *skipwhite(p + 1) == ']';
6331 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6332 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006333 {
6334 semsg(_(e_white_space_required_before_and_after_str_at_str),
6335 ":", p);
6336 return FAIL;
6337 }
6338 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006339 if (*p == ']')
6340 // empty second index, push "none"
6341 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6342 else
6343 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006344 }
6345
Bram Moolenaar752fc692021-01-04 21:57:11 +01006346 if (r == OK && *skipwhite(p) != ']')
6347 {
6348 // this should not happen
6349 emsg(_(e_missbrac));
6350 r = FAIL;
6351 }
6352 }
6353 else // if (*p == '.')
6354 {
6355 char_u *key_end = to_name_end(p + 1, TRUE);
6356 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6357
6358 r = generate_PUSHS(cctx, key);
6359 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006360 return r;
6361}
6362
6363/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006364 * For a LHS with an index, load the variable to be indexed.
6365 */
6366 static int
6367compile_load_lhs(
6368 lhs_T *lhs,
6369 char_u *var_start,
6370 type_T *rhs_type,
6371 cctx_T *cctx)
6372{
6373 if (lhs->lhs_dest == dest_expr)
6374 {
6375 size_t varlen = lhs->lhs_varlen;
6376 int c = var_start[varlen];
6377 char_u *p = var_start;
6378 garray_T *stack = &cctx->ctx_type_stack;
6379
6380 // Evaluate "ll[expr]" of "ll[expr][idx]"
6381 var_start[varlen] = NUL;
6382 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6383 {
6384 // this should not happen
6385 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006386 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006387 return FAIL;
6388 }
6389 var_start[varlen] = c;
6390
6391 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6392 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6393 // now we can properly check the type
6394 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6395 && rhs_type != &t_void
6396 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6397 FALSE, FALSE) == FAIL)
6398 return FAIL;
6399 }
6400 else
6401 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6402 lhs->lhs_lvar, lhs->lhs_type);
6403 return OK;
6404}
6405
6406/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006407 * Produce code for loading "lhs" and also take care of an index.
6408 * Return OK/FAIL.
6409 */
6410 static int
6411compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6412{
6413 compile_load_lhs(lhs, var_start, NULL, cctx);
6414
6415 if (lhs->lhs_has_index)
6416 {
6417 int range = FALSE;
6418
6419 // Get member from list or dict. First compile the
6420 // index value.
6421 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6422 return FAIL;
6423 if (range)
6424 {
6425 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6426 var_start);
6427 return FAIL;
6428 }
6429
6430 // Get the member.
6431 if (compile_member(FALSE, cctx) == FAIL)
6432 return FAIL;
6433 }
6434 return OK;
6435}
6436
6437/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006438 * Assignment to a list or dict member, or ":unlet" for the item, using the
6439 * information in "lhs".
6440 * Returns OK or FAIL.
6441 */
6442 static int
6443compile_assign_unlet(
6444 char_u *var_start,
6445 lhs_T *lhs,
6446 int is_assign,
6447 type_T *rhs_type,
6448 cctx_T *cctx)
6449{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006450 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006451 garray_T *stack = &cctx->ctx_type_stack;
6452 int range = FALSE;
6453
Bram Moolenaar68452172021-04-12 21:21:02 +02006454 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006455 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006456 if (is_assign && range && lhs->lhs_type != &t_blob
6457 && lhs->lhs_type != &t_any)
6458 {
6459 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6460 return FAIL;
6461 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006462
6463 if (lhs->lhs_type == &t_any)
6464 {
6465 // Index on variable of unknown type: check at runtime.
6466 dest_type = VAR_ANY;
6467 }
6468 else
6469 {
6470 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006471 if (dest_type == VAR_DICT && range)
6472 {
6473 emsg(e_cannot_use_range_with_dictionary);
6474 return FAIL;
6475 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006476 if (dest_type == VAR_DICT
6477 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006478 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006479 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006480 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006481 type_T *type;
6482
6483 if (range)
6484 {
6485 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6486 if (need_type(type, &t_number,
6487 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006488 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006489 }
6490 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6491 if ((dest_type != VAR_BLOB || type != &t_special)
6492 && need_type(type, &t_number,
6493 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006494 return FAIL;
6495 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006496 }
6497
6498 // Load the dict or list. On the stack we then have:
6499 // - value (for assignment, not for :unlet)
6500 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006501 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006502 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006503 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6504 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006505
Bram Moolenaar68452172021-04-12 21:21:02 +02006506 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6507 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006508 {
6509 if (is_assign)
6510 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006511 if (range)
6512 {
6513 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6514 return FAIL;
6515 }
6516 else
6517 {
6518 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006519
Bram Moolenaar68452172021-04-12 21:21:02 +02006520 if (isn == NULL)
6521 return FAIL;
6522 isn->isn_arg.vartype = dest_type;
6523 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006524 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006525 else if (range)
6526 {
6527 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6528 return FAIL;
6529 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006530 else
6531 {
6532 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6533 return FAIL;
6534 }
6535 }
6536 else
6537 {
6538 emsg(_(e_indexable_type_required));
6539 return FAIL;
6540 }
6541
6542 return OK;
6543}
6544
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006545/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006546 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006547 * "let name"
6548 * "var name = expr"
6549 * "final name = expr"
6550 * "const name = expr"
6551 * "name = expr"
6552 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006553 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006554 * Return NULL for an error.
6555 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556 */
6557 static char_u *
6558compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6559{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006560 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006561 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006562 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006563 char_u *ret = NULL;
6564 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006565 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006567 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006568 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006569 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006570 int oplen = 0;
6571 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006572 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006573 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006574 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006575 int is_decl = is_decl_command(cmdidx);
6576 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006577 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006579 // Skip over the "var" or "[var, var]" to get to any "=".
6580 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6581 if (p == NULL)
6582 return *arg == '[' ? arg : NULL;
6583
6584 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006585 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006586 // TODO: should we allow this, and figure out type inference from list
6587 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006588 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589 return NULL;
6590 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006591 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006593 sp = p;
6594 p = skipwhite(p);
6595 op = p;
6596 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006597
6598 if (var_count > 0 && oplen == 0)
6599 // can be something like "[1, 2]->func()"
6600 return arg;
6601
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006602 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006604 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006605 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006606 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006607 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6608 {
6609 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6610 oplen = 2;
6611 incdec = TRUE;
6612 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006614 if (heredoc)
6615 {
6616 list_T *l;
6617 listitem_T *li;
6618
6619 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006620 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006622 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006623 if (l == NULL)
6624 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625
Bram Moolenaar078269b2020-09-21 20:35:55 +02006626 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006627 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006628 // Push each line and the create the list.
6629 FOR_ALL_LIST_ITEMS(l, li)
6630 {
6631 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6632 li->li_tv.vval.v_string = NULL;
6633 }
6634 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636 list_free(l);
6637 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006638 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006640 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006642 char_u *wp;
6643
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006644 // for "[var, var] = expr" evaluate the expression here, loop over the
6645 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006646 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006647
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006648 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006649 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006650 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006651 if (compile_expr0(&p, cctx) == FAIL)
6652 return NULL;
6653 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006655 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006657 type_T *stacktype;
6658
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006659 stacktype = stack->ga_len == 0 ? &t_void
6660 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006661 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006662 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006663 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006664 goto theend;
6665 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006666 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006667 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006668 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006669 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006670 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6671 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006672 if (stacktype->tt_member != NULL)
6673 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006674 }
6675 }
6676
6677 /*
6678 * Loop over variables in "[var, var] = expr".
6679 * For "var = expr" and "let var: type" this is done only once.
6680 */
6681 if (var_count > 0)
6682 var_start = skipwhite(arg + 1); // skip over the "["
6683 else
6684 var_start = arg;
6685 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6686 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006687 int instr_count = -1;
6688
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006689 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6690 {
6691 // Ignore underscore in "[a, _, b] = list".
6692 if (var_count > 0)
6693 {
6694 var_start = skipwhite(var_start + 2);
6695 continue;
6696 }
6697 emsg(_(e_cannot_use_underscore_here));
6698 goto theend;
6699 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006700 vim_free(lhs.lhs_name);
6701
6702 /*
6703 * Figure out the LHS type and other properties.
6704 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006705 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6706 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006707 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006708 if (!heredoc)
6709 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006710 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006711 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006712 if (oplen > 0 && var_count == 0)
6713 {
6714 // skip over the "=" and the expression
6715 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006716 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006717 }
6718 }
6719 else if (oplen > 0)
6720 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006721 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006722 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006723
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006724 // For "var = expr" evaluate the expression.
6725 if (var_count == 0)
6726 {
6727 int r;
6728
6729 // for "+=", "*=", "..=" etc. first load the current value
6730 if (*op != '=')
6731 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006732 if (compile_load_lhs_with_index(&lhs, var_start,
6733 cctx) == FAIL)
6734 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006735 }
6736
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006737 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006738 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006739 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006740 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006741 r = generate_PUSHNR(cctx, 1);
6742 }
6743 else
6744 {
6745 // Temporarily hide the new local variable here, it is
6746 // not available to this expression.
6747 if (lhs.lhs_new_local)
6748 --cctx->ctx_locals.ga_len;
6749 wp = op + oplen;
6750 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6751 {
6752 if (lhs.lhs_new_local)
6753 ++cctx->ctx_locals.ga_len;
6754 goto theend;
6755 }
6756 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006757 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006758 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006759 if (r == FAIL)
6760 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006761 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006762 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006763 else if (semicolon && var_idx == var_count - 1)
6764 {
6765 // For "[var; var] = expr" get the rest of the list
6766 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6767 goto theend;
6768 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006769 else
6770 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006771 // For "[var, var] = expr" get the "var_idx" item from the
6772 // list.
6773 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006774 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006775 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006776
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006777 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006778 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006779 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006780 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006781 if ((rhs_type->tt_type == VAR_FUNC
6782 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006783 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006784 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006785 goto theend;
6786
Bram Moolenaar752fc692021-01-04 21:57:11 +01006787 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006788 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006789 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006790 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006791 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006792 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006793 }
6794 else
6795 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006796 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006797 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006798 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006799 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006800 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006801 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006802 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006803 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006804 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006805 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006806 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006807 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006808 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006809 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006810 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006811
Bram Moolenaar77709b12021-04-03 21:01:01 +02006812 // Without operator check type here, otherwise below.
6813 // Use the line number of the assignment.
6814 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006815 if (lhs.lhs_has_index)
6816 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006817 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006818 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006819 goto theend;
6820 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006821 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006822 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006823 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006824 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006826 else if (cmdidx == CMD_final)
6827 {
6828 emsg(_(e_final_requires_a_value));
6829 goto theend;
6830 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006831 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006832 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006833 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006834 goto theend;
6835 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006836 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006837 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006838 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006839 goto theend;
6840 }
6841 else
6842 {
6843 // variables are always initialized
6844 if (ga_grow(instr, 1) == FAIL)
6845 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006846 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006847 {
6848 case VAR_BOOL:
6849 generate_PUSHBOOL(cctx, VVAL_FALSE);
6850 break;
6851 case VAR_FLOAT:
6852#ifdef FEAT_FLOAT
6853 generate_PUSHF(cctx, 0.0);
6854#endif
6855 break;
6856 case VAR_STRING:
6857 generate_PUSHS(cctx, NULL);
6858 break;
6859 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006860 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006861 break;
6862 case VAR_FUNC:
6863 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6864 break;
6865 case VAR_LIST:
6866 generate_NEWLIST(cctx, 0);
6867 break;
6868 case VAR_DICT:
6869 generate_NEWDICT(cctx, 0);
6870 break;
6871 case VAR_JOB:
6872 generate_PUSHJOB(cctx, NULL);
6873 break;
6874 case VAR_CHANNEL:
6875 generate_PUSHCHANNEL(cctx, NULL);
6876 break;
6877 case VAR_NUMBER:
6878 case VAR_UNKNOWN:
6879 case VAR_ANY:
6880 case VAR_PARTIAL:
6881 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006882 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006883 case VAR_SPECIAL: // cannot happen
6884 generate_PUSHNR(cctx, 0);
6885 break;
6886 }
6887 }
6888 if (var_count == 0)
6889 end = p;
6890 }
6891
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006892 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006893 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006894 break;
6895
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006896 if (oplen > 0 && *op != '=')
6897 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006898 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006899 type_T *stacktype;
6900
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006901 if (*op == '.')
6902 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006903 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006904 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006905 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006906 if (
6907#ifdef FEAT_FLOAT
6908 // If variable is float operation with number is OK.
6909 !(expected == &t_float && stacktype == &t_number) &&
6910#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006911 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006912 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006913 goto theend;
6914
6915 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006916 {
6917 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6918 goto theend;
6919 }
6920 else if (*op == '+')
6921 {
6922 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006923 operator_type(lhs.lhs_member_type, stacktype),
6924 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006925 goto theend;
6926 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006927 else if (generate_two_op(cctx, op) == FAIL)
6928 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930
Bram Moolenaar752fc692021-01-04 21:57:11 +01006931 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006932 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006933 // Use the info in "lhs" to store the value at the index in the
6934 // list or dict.
6935 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6936 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006937 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006938 }
6939 else
6940 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006941 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006942 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006943 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006944 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006945 generate_LOCKCONST(cctx);
6946
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006947 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006948 && (lhs.lhs_type->tt_type == VAR_DICT
6949 || lhs.lhs_type->tt_type == VAR_LIST)
6950 && lhs.lhs_type->tt_member != NULL
6951 && lhs.lhs_type->tt_member != &t_any
6952 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006953 // Set the type in the list or dict, so that it can be checked,
6954 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006955 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006956
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006957 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6958 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006959 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006960
6961 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006962 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006964
6965 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006966 if (var_count > 0 && !semicolon)
6967 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006968 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006969 goto theend;
6970 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006971
Bram Moolenaarb2097502020-07-19 17:17:02 +02006972 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973
6974theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006975 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976 return ret;
6977}
6978
6979/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006980 * Check for an assignment at "eap->cmd", compile it if found.
6981 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6982 */
6983 static int
6984may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6985{
6986 char_u *pskip;
6987 char_u *p;
6988
6989 // Assuming the command starts with a variable or function name,
6990 // find what follows.
6991 // Skip over "var.member", "var[idx]" and the like.
6992 // Also "&opt = val", "$ENV = val" and "@r = val".
6993 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6994 ? eap->cmd + 1 : eap->cmd;
6995 p = to_name_end(pskip, TRUE);
6996 if (p > eap->cmd && *p != NUL)
6997 {
6998 char_u *var_end;
6999 int oplen;
7000 int heredoc;
7001
7002 if (eap->cmd[0] == '@')
7003 var_end = eap->cmd + 2;
7004 else
7005 var_end = find_name_end(pskip, NULL, NULL,
7006 FNE_CHECK_START | FNE_INCL_BR);
7007 oplen = assignment_len(skipwhite(var_end), &heredoc);
7008 if (oplen > 0)
7009 {
7010 size_t len = p - eap->cmd;
7011
7012 // Recognize an assignment if we recognize the variable
7013 // name:
7014 // "g:var = expr"
7015 // "local = expr" where "local" is a local var.
7016 // "script = expr" where "script" is a script-local var.
7017 // "import = expr" where "import" is an imported var
7018 // "&opt = expr"
7019 // "$ENV = expr"
7020 // "@r = expr"
7021 if (*eap->cmd == '&'
7022 || *eap->cmd == '$'
7023 || *eap->cmd == '@'
7024 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007025 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007026 {
7027 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7028 if (*line == NULL || *line == eap->cmd)
7029 return FAIL;
7030 return OK;
7031 }
7032 }
7033 }
7034
7035 if (*eap->cmd == '[')
7036 {
7037 // [var, var] = expr
7038 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7039 if (*line == NULL)
7040 return FAIL;
7041 if (*line != eap->cmd)
7042 return OK;
7043 }
7044 return NOTDONE;
7045}
7046
7047/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007048 * Check if "name" can be "unlet".
7049 */
7050 int
7051check_vim9_unlet(char_u *name)
7052{
7053 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7054 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007055 // "unlet s:var" is allowed in legacy script.
7056 if (*name == 's' && !script_is_vim9())
7057 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007058 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007059 return FAIL;
7060 }
7061 return OK;
7062}
7063
7064/*
7065 * Callback passed to ex_unletlock().
7066 */
7067 static int
7068compile_unlet(
7069 lval_T *lvp,
7070 char_u *name_end,
7071 exarg_T *eap,
7072 int deep UNUSED,
7073 void *coookie)
7074{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007075 cctx_T *cctx = coookie;
7076 char_u *p = lvp->ll_name;
7077 int cc = *name_end;
7078 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007079
Bram Moolenaar752fc692021-01-04 21:57:11 +01007080 if (cctx->ctx_skip == SKIP_YES)
7081 return OK;
7082
7083 *name_end = NUL;
7084 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007085 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007086 // :unlet $ENV_VAR
7087 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7088 }
7089 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7090 {
7091 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007092
Bram Moolenaar752fc692021-01-04 21:57:11 +01007093 // This is similar to assigning: lookup the list/dict, compile the
7094 // idx/key. Then instead of storing the value unlet the item.
7095 // unlet {list}[idx]
7096 // unlet {dict}[key] dict.key
7097 //
7098 // Figure out the LHS type and other properties.
7099 //
7100 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7101
7102 // : unlet an indexed item
7103 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007104 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007105 iemsg("called compile_lhs() without an index");
7106 ret = FAIL;
7107 }
7108 else
7109 {
7110 // Use the info in "lhs" to unlet the item at the index in the
7111 // list or dict.
7112 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007113 }
7114
Bram Moolenaar752fc692021-01-04 21:57:11 +01007115 vim_free(lhs.lhs_name);
7116 }
7117 else if (check_vim9_unlet(p) == FAIL)
7118 {
7119 ret = FAIL;
7120 }
7121 else
7122 {
7123 // Normal name. Only supports g:, w:, t: and b: namespaces.
7124 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007125 }
7126
Bram Moolenaar752fc692021-01-04 21:57:11 +01007127 *name_end = cc;
7128 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007129}
7130
7131/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007132 * Callback passed to ex_unletlock().
7133 */
7134 static int
7135compile_lock_unlock(
7136 lval_T *lvp,
7137 char_u *name_end,
7138 exarg_T *eap,
7139 int deep UNUSED,
7140 void *coookie)
7141{
7142 cctx_T *cctx = coookie;
7143 int cc = *name_end;
7144 char_u *p = lvp->ll_name;
7145 int ret = OK;
7146 size_t len;
7147 char_u *buf;
7148
7149 if (cctx->ctx_skip == SKIP_YES)
7150 return OK;
7151
7152 // Cannot use :lockvar and :unlockvar on local variables.
7153 if (p[1] != ':')
7154 {
7155 char_u *end = skip_var_one(p, FALSE);
7156
7157 if (lookup_local(p, end - p, NULL, cctx) == OK)
7158 {
7159 emsg(_(e_cannot_lock_unlock_local_variable));
7160 return FAIL;
7161 }
7162 }
7163
7164 // Checking is done at runtime.
7165 *name_end = NUL;
7166 len = name_end - p + 20;
7167 buf = alloc(len);
7168 if (buf == NULL)
7169 ret = FAIL;
7170 else
7171 {
7172 vim_snprintf((char *)buf, len, "%s %s",
7173 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7174 p);
7175 ret = generate_EXEC(cctx, buf);
7176
7177 vim_free(buf);
7178 *name_end = cc;
7179 }
7180 return ret;
7181}
7182
7183/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007184 * compile "unlet var", "lock var" and "unlock var"
7185 * "arg" points to "var".
7186 */
7187 static char_u *
7188compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7189{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007190 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7191 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7192 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007193 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7194}
7195
7196/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007197 * Compile an :import command.
7198 */
7199 static char_u *
7200compile_import(char_u *arg, cctx_T *cctx)
7201{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007202 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007203}
7204
7205/*
7206 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7207 */
7208 static int
7209compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7210{
7211 garray_T *instr = &cctx->ctx_instr;
7212 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7213
7214 if (endlabel == NULL)
7215 return FAIL;
7216 endlabel->el_next = *el;
7217 *el = endlabel;
7218 endlabel->el_end_label = instr->ga_len;
7219
7220 generate_JUMP(cctx, when, 0);
7221 return OK;
7222}
7223
7224 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007225compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007226{
7227 garray_T *instr = &cctx->ctx_instr;
7228
7229 while (*el != NULL)
7230 {
7231 endlabel_T *cur = (*el);
7232 isn_T *isn;
7233
7234 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007235 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236 *el = cur->el_next;
7237 vim_free(cur);
7238 }
7239}
7240
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007241 static void
7242compile_free_jump_to_end(endlabel_T **el)
7243{
7244 while (*el != NULL)
7245 {
7246 endlabel_T *cur = (*el);
7247
7248 *el = cur->el_next;
7249 vim_free(cur);
7250 }
7251}
7252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253/*
7254 * Create a new scope and set up the generic items.
7255 */
7256 static scope_T *
7257new_scope(cctx_T *cctx, scopetype_T type)
7258{
7259 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7260
7261 if (scope == NULL)
7262 return NULL;
7263 scope->se_outer = cctx->ctx_scope;
7264 cctx->ctx_scope = scope;
7265 scope->se_type = type;
7266 scope->se_local_count = cctx->ctx_locals.ga_len;
7267 return scope;
7268}
7269
7270/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007271 * Free the current scope and go back to the outer scope.
7272 */
7273 static void
7274drop_scope(cctx_T *cctx)
7275{
7276 scope_T *scope = cctx->ctx_scope;
7277
7278 if (scope == NULL)
7279 {
7280 iemsg("calling drop_scope() without a scope");
7281 return;
7282 }
7283 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007284 switch (scope->se_type)
7285 {
7286 case IF_SCOPE:
7287 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7288 case FOR_SCOPE:
7289 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7290 case WHILE_SCOPE:
7291 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7292 case TRY_SCOPE:
7293 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7294 case NO_SCOPE:
7295 case BLOCK_SCOPE:
7296 break;
7297 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007298 vim_free(scope);
7299}
7300
7301/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007302 * compile "if expr"
7303 *
7304 * "if expr" Produces instructions:
7305 * EVAL expr Push result of "expr"
7306 * JUMP_IF_FALSE end
7307 * ... body ...
7308 * end:
7309 *
7310 * "if expr | else" Produces instructions:
7311 * EVAL expr Push result of "expr"
7312 * JUMP_IF_FALSE else
7313 * ... body ...
7314 * JUMP_ALWAYS end
7315 * else:
7316 * ... body ...
7317 * end:
7318 *
7319 * "if expr1 | elseif expr2 | else" Produces instructions:
7320 * EVAL expr Push result of "expr"
7321 * JUMP_IF_FALSE elseif
7322 * ... body ...
7323 * JUMP_ALWAYS end
7324 * elseif:
7325 * EVAL expr Push result of "expr"
7326 * JUMP_IF_FALSE else
7327 * ... body ...
7328 * JUMP_ALWAYS end
7329 * else:
7330 * ... body ...
7331 * end:
7332 */
7333 static char_u *
7334compile_if(char_u *arg, cctx_T *cctx)
7335{
7336 char_u *p = arg;
7337 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007338 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007339 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007340 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007341 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007342
Bram Moolenaara5565e42020-05-09 15:44:01 +02007343 CLEAR_FIELD(ppconst);
7344 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007345 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007346 clear_ppconst(&ppconst);
7347 return NULL;
7348 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007349 if (!ends_excmd2(arg, skipwhite(p)))
7350 {
7351 semsg(_(e_trailing_arg), p);
7352 return NULL;
7353 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007354 if (cctx->ctx_skip == SKIP_YES)
7355 clear_ppconst(&ppconst);
7356 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007357 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007358 int error = FALSE;
7359 int v;
7360
Bram Moolenaara5565e42020-05-09 15:44:01 +02007361 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007362 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007363 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007364 if (error)
7365 return NULL;
7366 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007367 }
7368 else
7369 {
7370 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007371 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007372 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007373 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007374 if (bool_on_stack(cctx) == FAIL)
7375 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377
Bram Moolenaara91a7132021-03-25 21:12:15 +01007378 // CMDMOD_REV must come before the jump
7379 generate_undo_cmdmods(cctx);
7380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 scope = new_scope(cctx, IF_SCOPE);
7382 if (scope == NULL)
7383 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007384 scope->se_skip_save = skip_save;
7385 // "is_had_return" will be reset if any block does not end in :return
7386 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007388 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007389 {
7390 // "where" is set when ":elseif", "else" or ":endif" is found
7391 scope->se_u.se_if.is_if_label = instr->ga_len;
7392 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7393 }
7394 else
7395 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007396
Bram Moolenaarced68a02021-01-24 17:53:47 +01007397#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007398 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007399 && skip_save != SKIP_YES)
7400 {
7401 // generated a profile start, need to generate a profile end, since it
7402 // won't be done after returning
7403 cctx->ctx_skip = SKIP_NOT;
7404 generate_instr(cctx, ISN_PROF_END);
7405 cctx->ctx_skip = SKIP_YES;
7406 }
7407#endif
7408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409 return p;
7410}
7411
7412 static char_u *
7413compile_elseif(char_u *arg, cctx_T *cctx)
7414{
7415 char_u *p = arg;
7416 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007417 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007418 isn_T *isn;
7419 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007420 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007421 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422
7423 if (scope == NULL || scope->se_type != IF_SCOPE)
7424 {
7425 emsg(_(e_elseif_without_if));
7426 return NULL;
7427 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007428 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007429 if (!cctx->ctx_had_return)
7430 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431
Bram Moolenaarced68a02021-01-24 17:53:47 +01007432 if (cctx->ctx_skip == SKIP_NOT)
7433 {
7434 // previous block was executed, this one and following will not
7435 cctx->ctx_skip = SKIP_YES;
7436 scope->se_u.se_if.is_seen_skip_not = TRUE;
7437 }
7438 if (scope->se_u.se_if.is_seen_skip_not)
7439 {
7440 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007441 // Do not count the "elseif" for profiling and cmdmod
7442 instr->ga_len = current_instr_idx(cctx);
7443
Bram Moolenaarced68a02021-01-24 17:53:47 +01007444 skip_expr_cctx(&p, cctx);
7445 return p;
7446 }
7447
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007448 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007449 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007450 int moved_cmdmod = FALSE;
7451
7452 // Move any CMDMOD instruction to after the jump
7453 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7454 {
7455 if (ga_grow(instr, 1) == FAIL)
7456 return NULL;
7457 ((isn_T *)instr->ga_data)[instr->ga_len] =
7458 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7459 --instr->ga_len;
7460 moved_cmdmod = TRUE;
7461 }
7462
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007463 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007465 return NULL;
7466 // previous "if" or "elseif" jumps here
7467 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7468 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007469 if (moved_cmdmod)
7470 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007473 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007474 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007475 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007476 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007477 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007478#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007479 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007480 {
7481 // the previous block was skipped, need to profile this line
7482 generate_instr(cctx, ISN_PROF_START);
7483 instr_count = instr->ga_len;
7484 }
7485#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007486 if (cctx->ctx_compile_type == CT_DEBUG)
7487 {
7488 // the previous block was skipped, may want to debug this line
7489 generate_instr(cctx, ISN_DEBUG);
7490 instr_count = instr->ga_len;
7491 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007492 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007493 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007494 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007495 clear_ppconst(&ppconst);
7496 return NULL;
7497 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007498 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007499 if (!ends_excmd2(arg, skipwhite(p)))
7500 {
7501 semsg(_(e_trailing_arg), p);
7502 return NULL;
7503 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007504 if (scope->se_skip_save == SKIP_YES)
7505 clear_ppconst(&ppconst);
7506 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007507 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007508 int error = FALSE;
7509 int v;
7510
Bram Moolenaar7f141552020-05-09 17:35:53 +02007511 // The expression results in a constant.
7512 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007513 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7514 if (error)
7515 return NULL;
7516 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007517 clear_ppconst(&ppconst);
7518 scope->se_u.se_if.is_if_label = -1;
7519 }
7520 else
7521 {
7522 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007523 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007524 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007525 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007526 if (bool_on_stack(cctx) == FAIL)
7527 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007528
Bram Moolenaara91a7132021-03-25 21:12:15 +01007529 // CMDMOD_REV must come before the jump
7530 generate_undo_cmdmods(cctx);
7531
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007532 // "where" is set when ":elseif", "else" or ":endif" is found
7533 scope->se_u.se_if.is_if_label = instr->ga_len;
7534 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7535 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007536
7537 return p;
7538}
7539
7540 static char_u *
7541compile_else(char_u *arg, cctx_T *cctx)
7542{
7543 char_u *p = arg;
7544 garray_T *instr = &cctx->ctx_instr;
7545 isn_T *isn;
7546 scope_T *scope = cctx->ctx_scope;
7547
7548 if (scope == NULL || scope->se_type != IF_SCOPE)
7549 {
7550 emsg(_(e_else_without_if));
7551 return NULL;
7552 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007553 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007554 if (!cctx->ctx_had_return)
7555 scope->se_u.se_if.is_had_return = FALSE;
7556 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007557
Bram Moolenaarced68a02021-01-24 17:53:47 +01007558#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007559 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007560 {
7561 if (cctx->ctx_skip == SKIP_NOT
7562 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7563 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007564 // the previous block was executed, do not count "else" for
7565 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007566 --instr->ga_len;
7567 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7568 {
7569 // the previous block was not executed, this one will, do count the
7570 // "else" for profiling
7571 cctx->ctx_skip = SKIP_NOT;
7572 generate_instr(cctx, ISN_PROF_END);
7573 generate_instr(cctx, ISN_PROF_START);
7574 cctx->ctx_skip = SKIP_YES;
7575 }
7576 }
7577#endif
7578
7579 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007580 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007581 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007582 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007583 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007584 if (!cctx->ctx_had_return
7585 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7586 JUMP_ALWAYS, cctx) == FAIL)
7587 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007588 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007589
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007590 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007591 {
7592 if (scope->se_u.se_if.is_if_label >= 0)
7593 {
7594 // previous "if" or "elseif" jumps here
7595 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7596 isn->isn_arg.jump.jump_where = instr->ga_len;
7597 scope->se_u.se_if.is_if_label = -1;
7598 }
7599 }
7600
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007601 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007602 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604
7605 return p;
7606}
7607
7608 static char_u *
7609compile_endif(char_u *arg, cctx_T *cctx)
7610{
7611 scope_T *scope = cctx->ctx_scope;
7612 ifscope_T *ifscope;
7613 garray_T *instr = &cctx->ctx_instr;
7614 isn_T *isn;
7615
Bram Moolenaarfa984412021-03-25 22:15:28 +01007616 if (misplaced_cmdmod(cctx))
7617 return NULL;
7618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007619 if (scope == NULL || scope->se_type != IF_SCOPE)
7620 {
7621 emsg(_(e_endif_without_if));
7622 return NULL;
7623 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007624 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007625 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007626 if (!cctx->ctx_had_return)
7627 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007628
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007629 if (scope->se_u.se_if.is_if_label >= 0)
7630 {
7631 // previous "if" or "elseif" jumps here
7632 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7633 isn->isn_arg.jump.jump_where = instr->ga_len;
7634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007636 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007637
7638#ifdef FEAT_PROFILE
7639 // even when skipping we count the endif as executed, unless the block it's
7640 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007641 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007642 && scope->se_skip_save != SKIP_YES)
7643 {
7644 cctx->ctx_skip = SKIP_NOT;
7645 generate_instr(cctx, ISN_PROF_START);
7646 }
7647#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007648 cctx->ctx_skip = scope->se_skip_save;
7649
7650 // If all the blocks end in :return and there is an :else then the
7651 // had_return flag is set.
7652 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007653
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007654 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007655 return arg;
7656}
7657
7658/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007659 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660 *
7661 * Produces instructions:
7662 * PUSHNR -1
7663 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007664 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007665 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7666 * - if beyond end, jump to "end"
7667 * - otherwise get item from list and push it
7668 * STORE var Store item in "var"
7669 * ... body ...
7670 * JUMP top Jump back to repeat
7671 * end: DROP Drop the result of "expr"
7672 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007673 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7674 * UNPACK 2 Split item in 2
7675 * STORE var1 Store item in "var1"
7676 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007677 */
7678 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007679compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007680{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007681 char_u *arg;
7682 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007683 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007685 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007686 int var_count = 0;
7687 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007688 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007689 garray_T *stack = &cctx->ctx_type_stack;
7690 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007691 lvar_T *loop_lvar; // loop iteration variable
7692 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007693 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007694 type_T *item_type = &t_any;
7695 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007696
Bram Moolenaar792f7862020-11-23 08:31:18 +01007697 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007698 if (p == NULL)
7699 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007700 if (var_count == 0)
7701 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007702
7703 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007704 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007705 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7706 return NULL;
7707 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007708 {
7709 emsg(_(e_missing_in));
7710 return NULL;
7711 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007712 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007713 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7714 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007716 scope = new_scope(cctx, FOR_SCOPE);
7717 if (scope == NULL)
7718 return NULL;
7719
Bram Moolenaar792f7862020-11-23 08:31:18 +01007720 // Reserve a variable to store the loop iteration counter and initialize it
7721 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007722 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7723 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007724 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007725 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007726 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007728 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007729 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007730
7731 // compile "expr", it remains on the stack until "endfor"
7732 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007733 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007734 {
7735 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007736 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007737 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007738 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007739
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007740 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007741 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007742 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007743 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007744 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007746 semsg(_(e_for_loop_on_str_not_supported),
7747 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007748 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007749 return NULL;
7750 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007751
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007752 if (vartype->tt_type == VAR_STRING)
7753 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007754 else if (vartype->tt_type == VAR_BLOB)
7755 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007756 else if (vartype->tt_type == VAR_LIST
7757 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007758 {
7759 if (var_count == 1)
7760 item_type = vartype->tt_member;
7761 else if (vartype->tt_member->tt_type == VAR_LIST
7762 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007763 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007764 item_type = vartype->tt_member->tt_member;
7765 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007766
Bram Moolenaara91a7132021-03-25 21:12:15 +01007767 // CMDMOD_REV must come before the FOR instruction
7768 generate_undo_cmdmods(cctx);
7769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007770 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007771 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007772 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007773
Bram Moolenaar792f7862020-11-23 08:31:18 +01007774 arg = arg_start;
7775 if (var_count > 1)
7776 {
7777 generate_UNPACK(cctx, var_count, semicolon);
7778 arg = skipwhite(arg + 1); // skip white after '['
7779
7780 // the list item is replaced by a number of items
7781 if (ga_grow(stack, var_count - 1) == FAIL)
7782 {
7783 drop_scope(cctx);
7784 return NULL;
7785 }
7786 --stack->ga_len;
7787 for (idx = 0; idx < var_count; ++idx)
7788 {
7789 ((type_T **)stack->ga_data)[stack->ga_len] =
7790 (semicolon && idx == 0) ? vartype : item_type;
7791 ++stack->ga_len;
7792 }
7793 }
7794
7795 for (idx = 0; idx < var_count; ++idx)
7796 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007797 assign_dest_T dest = dest_local;
7798 int opt_flags = 0;
7799 int vimvaridx = -1;
7800 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007801 type_T *lhs_type = &t_any;
7802 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007803
7804 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007805 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007806 name = vim_strnsave(arg, varlen);
7807 if (name == NULL)
7808 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007809 if (*p == ':')
7810 {
7811 p = skipwhite(p + 1);
7812 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7813 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007814
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007815 // TODO: script var not supported?
7816 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7817 &vimvaridx, &type, cctx) == FAIL)
7818 goto failed;
7819 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007820 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007821 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7822 0, 0, type, name) == FAIL)
7823 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007824 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007825 else if (varlen == 1 && *arg == '_')
7826 {
7827 // Assigning to "_": drop the value.
7828 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7829 goto failed;
7830 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007831 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007832 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007833 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007834 {
7835 semsg(_(e_variable_already_declared), arg);
7836 goto failed;
7837 }
7838
Bram Moolenaarea870692020-12-02 14:24:30 +01007839 if (STRNCMP(name, "s:", 2) == 0)
7840 {
7841 semsg(_(e_cannot_declare_script_variable_in_function), name);
7842 goto failed;
7843 }
7844
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007845 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007846 where.wt_index = var_count > 1 ? idx + 1 : 0;
7847 where.wt_variable = TRUE;
7848 if (lhs_type == &t_any)
7849 lhs_type = item_type;
7850 else if (item_type != &t_unknown
7851 && !(var_count > 1 && item_type == &t_any)
7852 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7853 goto failed;
7854 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007855 if (var_lvar == NULL)
7856 // out of memory or used as an argument
7857 goto failed;
7858
7859 if (semicolon && idx == var_count - 1)
7860 var_lvar->lv_type = vartype;
7861 else
7862 var_lvar->lv_type = item_type;
7863 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7864 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007865
7866 if (*p == ',' || *p == ';')
7867 ++p;
7868 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007869 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007870 }
7871
7872 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007873
7874failed:
7875 vim_free(name);
7876 drop_scope(cctx);
7877 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878}
7879
7880/*
7881 * compile "endfor"
7882 */
7883 static char_u *
7884compile_endfor(char_u *arg, cctx_T *cctx)
7885{
7886 garray_T *instr = &cctx->ctx_instr;
7887 scope_T *scope = cctx->ctx_scope;
7888 forscope_T *forscope;
7889 isn_T *isn;
7890
Bram Moolenaarfa984412021-03-25 22:15:28 +01007891 if (misplaced_cmdmod(cctx))
7892 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 if (scope == NULL || scope->se_type != FOR_SCOPE)
7895 {
7896 emsg(_(e_for));
7897 return NULL;
7898 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007899 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007900 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007901 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007902
7903 // At end of ":for" scope jump back to the FOR instruction.
7904 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7905
7906 // Fill in the "end" label in the FOR statement so it can jump here
7907 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7908 isn->isn_arg.forloop.for_end = instr->ga_len;
7909
7910 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007911 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007912
7913 // Below the ":for" scope drop the "expr" list from the stack.
7914 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7915 return NULL;
7916
7917 vim_free(scope);
7918
7919 return arg;
7920}
7921
7922/*
7923 * compile "while expr"
7924 *
7925 * Produces instructions:
7926 * top: EVAL expr Push result of "expr"
7927 * JUMP_IF_FALSE end jump if false
7928 * ... body ...
7929 * JUMP top Jump back to repeat
7930 * end:
7931 *
7932 */
7933 static char_u *
7934compile_while(char_u *arg, cctx_T *cctx)
7935{
7936 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007937 scope_T *scope;
7938
7939 scope = new_scope(cctx, WHILE_SCOPE);
7940 if (scope == NULL)
7941 return NULL;
7942
Bram Moolenaara91a7132021-03-25 21:12:15 +01007943 // "endwhile" jumps back here, one before when profiling or using cmdmods
7944 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007945
7946 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007947 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007948 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007949 if (!ends_excmd2(arg, skipwhite(p)))
7950 {
7951 semsg(_(e_trailing_arg), p);
7952 return NULL;
7953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954
Bram Moolenaar13106602020-10-04 16:06:05 +02007955 if (bool_on_stack(cctx) == FAIL)
7956 return FAIL;
7957
Bram Moolenaara91a7132021-03-25 21:12:15 +01007958 // CMDMOD_REV must come before the jump
7959 generate_undo_cmdmods(cctx);
7960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007961 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007962 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007963 JUMP_IF_FALSE, cctx) == FAIL)
7964 return FAIL;
7965
7966 return p;
7967}
7968
7969/*
7970 * compile "endwhile"
7971 */
7972 static char_u *
7973compile_endwhile(char_u *arg, cctx_T *cctx)
7974{
7975 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007976 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007977
Bram Moolenaarfa984412021-03-25 22:15:28 +01007978 if (misplaced_cmdmod(cctx))
7979 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007980 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7981 {
7982 emsg(_(e_while));
7983 return NULL;
7984 }
7985 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007986 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007987
Bram Moolenaarf002a412021-01-24 13:34:18 +01007988#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007989 // count the endwhile before jumping
7990 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007991#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007993 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007994 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007995
7996 // Fill in the "end" label in the WHILE statement so it can jump here.
7997 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007998 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7999 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008000
8001 vim_free(scope);
8002
8003 return arg;
8004}
8005
8006/*
8007 * compile "continue"
8008 */
8009 static char_u *
8010compile_continue(char_u *arg, cctx_T *cctx)
8011{
8012 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008013 int try_scopes = 0;
8014 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008015
8016 for (;;)
8017 {
8018 if (scope == NULL)
8019 {
8020 emsg(_(e_continue));
8021 return NULL;
8022 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008023 if (scope->se_type == FOR_SCOPE)
8024 {
8025 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008026 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008027 }
8028 if (scope->se_type == WHILE_SCOPE)
8029 {
8030 loop_label = scope->se_u.se_while.ws_top_label;
8031 break;
8032 }
8033 if (scope->se_type == TRY_SCOPE)
8034 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008035 scope = scope->se_outer;
8036 }
8037
Bram Moolenaarc150c092021-02-13 15:02:46 +01008038 if (try_scopes > 0)
8039 // Inside one or more try/catch blocks we first need to jump to the
8040 // "finally" or "endtry" to cleanup.
8041 generate_TRYCONT(cctx, try_scopes, loop_label);
8042 else
8043 // Jump back to the FOR or WHILE instruction.
8044 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008046 return arg;
8047}
8048
8049/*
8050 * compile "break"
8051 */
8052 static char_u *
8053compile_break(char_u *arg, cctx_T *cctx)
8054{
8055 scope_T *scope = cctx->ctx_scope;
8056 endlabel_T **el;
8057
8058 for (;;)
8059 {
8060 if (scope == NULL)
8061 {
8062 emsg(_(e_break));
8063 return NULL;
8064 }
8065 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8066 break;
8067 scope = scope->se_outer;
8068 }
8069
8070 // Jump to the end of the FOR or WHILE loop.
8071 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008072 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008073 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008074 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008075 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8076 return FAIL;
8077
8078 return arg;
8079}
8080
8081/*
8082 * compile "{" start of block
8083 */
8084 static char_u *
8085compile_block(char_u *arg, cctx_T *cctx)
8086{
8087 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8088 return NULL;
8089 return skipwhite(arg + 1);
8090}
8091
8092/*
8093 * compile end of block: drop one scope
8094 */
8095 static void
8096compile_endblock(cctx_T *cctx)
8097{
8098 scope_T *scope = cctx->ctx_scope;
8099
8100 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008101 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008102 vim_free(scope);
8103}
8104
8105/*
8106 * compile "try"
8107 * Creates a new scope for the try-endtry, pointing to the first catch and
8108 * finally.
8109 * Creates another scope for the "try" block itself.
8110 * TRY instruction sets up exception handling at runtime.
8111 *
8112 * "try"
8113 * TRY -> catch1, -> finally push trystack entry
8114 * ... try block
8115 * "throw {exception}"
8116 * EVAL {exception}
8117 * THROW create exception
8118 * ... try block
8119 * " catch {expr}"
8120 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008121 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008122 * EVAL {expr}
8123 * MATCH
8124 * JUMP nomatch -> catch2
8125 * CATCH remove exception
8126 * ... catch block
8127 * " catch"
8128 * JUMP -> finally
8129 * catch2: CATCH remove exception
8130 * ... catch block
8131 * " finally"
8132 * finally:
8133 * ... finally block
8134 * " endtry"
8135 * ENDTRY pop trystack entry, may rethrow
8136 */
8137 static char_u *
8138compile_try(char_u *arg, cctx_T *cctx)
8139{
8140 garray_T *instr = &cctx->ctx_instr;
8141 scope_T *try_scope;
8142 scope_T *scope;
8143
Bram Moolenaarfa984412021-03-25 22:15:28 +01008144 if (misplaced_cmdmod(cctx))
8145 return NULL;
8146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008147 // scope that holds the jumps that go to catch/finally/endtry
8148 try_scope = new_scope(cctx, TRY_SCOPE);
8149 if (try_scope == NULL)
8150 return NULL;
8151
Bram Moolenaar69f70502021-01-01 16:10:46 +01008152 if (cctx->ctx_skip != SKIP_YES)
8153 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008154 isn_T *isn;
8155
8156 // "try_catch" is set when the first ":catch" is found or when no catch
8157 // is found and ":finally" is found.
8158 // "try_finally" is set when ":finally" is found
8159 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008160 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008161 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8162 return NULL;
8163 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8164 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008165 return NULL;
8166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008167
8168 // scope for the try block itself
8169 scope = new_scope(cctx, BLOCK_SCOPE);
8170 if (scope == NULL)
8171 return NULL;
8172
8173 return arg;
8174}
8175
8176/*
8177 * compile "catch {expr}"
8178 */
8179 static char_u *
8180compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8181{
8182 scope_T *scope = cctx->ctx_scope;
8183 garray_T *instr = &cctx->ctx_instr;
8184 char_u *p;
8185 isn_T *isn;
8186
Bram Moolenaarfa984412021-03-25 22:15:28 +01008187 if (misplaced_cmdmod(cctx))
8188 return NULL;
8189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008190 // end block scope from :try or :catch
8191 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8192 compile_endblock(cctx);
8193 scope = cctx->ctx_scope;
8194
8195 // Error if not in a :try scope
8196 if (scope == NULL || scope->se_type != TRY_SCOPE)
8197 {
8198 emsg(_(e_catch));
8199 return NULL;
8200 }
8201
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008202 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008203 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008204 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008205 return NULL;
8206 }
8207
Bram Moolenaar69f70502021-01-01 16:10:46 +01008208 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008209 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008210#ifdef FEAT_PROFILE
8211 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008212 if (cctx->ctx_compile_type == CT_PROFILE
8213 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008214 .isn_type == ISN_PROF_START)
8215 --instr->ga_len;
8216#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008217 // Jump from end of previous block to :finally or :endtry
8218 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8219 JUMP_ALWAYS, cctx) == FAIL)
8220 return NULL;
8221
8222 // End :try or :catch scope: set value in ISN_TRY instruction
8223 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008224 if (isn->isn_arg.try.try_ref->try_catch == 0)
8225 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008226 if (scope->se_u.se_try.ts_catch_label != 0)
8227 {
8228 // Previous catch without match jumps here
8229 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8230 isn->isn_arg.jump.jump_where = instr->ga_len;
8231 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008232#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008233 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008234 {
8235 // a "throw" that jumps here needs to be counted
8236 generate_instr(cctx, ISN_PROF_END);
8237 // the "catch" is also counted
8238 generate_instr(cctx, ISN_PROF_START);
8239 }
8240#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008241 if (cctx->ctx_compile_type == CT_DEBUG)
8242 generate_instr(cctx, ISN_DEBUG);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008243 }
8244
8245 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008246 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008247 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008248 scope->se_u.se_try.ts_caught_all = TRUE;
8249 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008250 }
8251 else
8252 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008253 char_u *end;
8254 char_u *pat;
8255 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008256 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008257 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008259 // Push v:exception, push {expr} and MATCH
8260 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8261
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008262 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008263 if (*end != *p)
8264 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008265 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008266 vim_free(tofree);
8267 return FAIL;
8268 }
8269 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008270 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008271 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008272 len = (int)(end - tofree);
8273 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008274 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008275 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008276 if (pat == NULL)
8277 return FAIL;
8278 if (generate_PUSHS(cctx, pat) == FAIL)
8279 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008281 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8282 return NULL;
8283
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008284 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008285 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8286 return NULL;
8287 }
8288
Bram Moolenaar69f70502021-01-01 16:10:46 +01008289 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008290 return NULL;
8291
8292 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8293 return NULL;
8294 return p;
8295}
8296
8297 static char_u *
8298compile_finally(char_u *arg, cctx_T *cctx)
8299{
8300 scope_T *scope = cctx->ctx_scope;
8301 garray_T *instr = &cctx->ctx_instr;
8302 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008303 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008304
Bram Moolenaarfa984412021-03-25 22:15:28 +01008305 if (misplaced_cmdmod(cctx))
8306 return NULL;
8307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008308 // end block scope from :try or :catch
8309 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8310 compile_endblock(cctx);
8311 scope = cctx->ctx_scope;
8312
8313 // Error if not in a :try scope
8314 if (scope == NULL || scope->se_type != TRY_SCOPE)
8315 {
8316 emsg(_(e_finally));
8317 return NULL;
8318 }
8319
8320 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008321 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008322 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008323 {
8324 emsg(_(e_finally_dup));
8325 return NULL;
8326 }
8327
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008328 this_instr = instr->ga_len;
8329#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008330 if (cctx->ctx_compile_type == CT_PROFILE
8331 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008332 .isn_type == ISN_PROF_START)
8333 // jump to the profile start of the "finally"
8334 --this_instr;
8335#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008336
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008337 // Fill in the "end" label in jumps at the end of the blocks.
8338 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8339 this_instr, cctx);
8340
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008341 // If there is no :catch then an exception jumps to :finally.
8342 if (isn->isn_arg.try.try_ref->try_catch == 0)
8343 isn->isn_arg.try.try_ref->try_catch = this_instr;
8344 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008345 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008346 {
8347 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008348 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008349 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008350 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008351 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008352 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8353 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008355 // TODO: set index in ts_finally_label jumps
8356
8357 return arg;
8358}
8359
8360 static char_u *
8361compile_endtry(char_u *arg, cctx_T *cctx)
8362{
8363 scope_T *scope = cctx->ctx_scope;
8364 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008365 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008366
Bram Moolenaarfa984412021-03-25 22:15:28 +01008367 if (misplaced_cmdmod(cctx))
8368 return NULL;
8369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008370 // end block scope from :catch or :finally
8371 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8372 compile_endblock(cctx);
8373 scope = cctx->ctx_scope;
8374
8375 // Error if not in a :try scope
8376 if (scope == NULL || scope->se_type != TRY_SCOPE)
8377 {
8378 if (scope == NULL)
8379 emsg(_(e_no_endtry));
8380 else if (scope->se_type == WHILE_SCOPE)
8381 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008382 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008383 emsg(_(e_endfor));
8384 else
8385 emsg(_(e_endif));
8386 return NULL;
8387 }
8388
Bram Moolenaarc150c092021-02-13 15:02:46 +01008389 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008390 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008391 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008392 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8393 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008394 {
8395 emsg(_(e_missing_catch_or_finally));
8396 return NULL;
8397 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008398
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008399#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008400 if (cctx->ctx_compile_type == CT_PROFILE
8401 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008402 .isn_type == ISN_PROF_START)
8403 // move the profile start after "endtry" so that it's not counted when
8404 // the exception is rethrown.
8405 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008406#endif
8407
Bram Moolenaar69f70502021-01-01 16:10:46 +01008408 // Fill in the "end" label in jumps at the end of the blocks, if not
8409 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008410 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8411 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008412
Bram Moolenaar69f70502021-01-01 16:10:46 +01008413 if (scope->se_u.se_try.ts_catch_label != 0)
8414 {
8415 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008416 isn_T *isn = ((isn_T *)instr->ga_data)
8417 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008418 isn->isn_arg.jump.jump_where = instr->ga_len;
8419 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008420 }
8421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008422 compile_endblock(cctx);
8423
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008424 if (cctx->ctx_skip != SKIP_YES)
8425 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008426 // End :catch or :finally scope: set instruction index in ISN_TRY
8427 // instruction
8428 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008429 if (cctx->ctx_skip != SKIP_YES
8430 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8431 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008432#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008433 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008434 generate_instr(cctx, ISN_PROF_START);
8435#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008436 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008437 return arg;
8438}
8439
8440/*
8441 * compile "throw {expr}"
8442 */
8443 static char_u *
8444compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8445{
8446 char_u *p = skipwhite(arg);
8447
Bram Moolenaara5565e42020-05-09 15:44:01 +02008448 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008449 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008450 if (cctx->ctx_skip == SKIP_YES)
8451 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008452 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008453 return NULL;
8454 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8455 return NULL;
8456
8457 return p;
8458}
8459
8460/*
8461 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008462 * compile "echomsg expr"
8463 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008464 * compile "execute expr"
8465 */
8466 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008467compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008468{
8469 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008470 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008471 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008472 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008473 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008474 garray_T *stack = &cctx->ctx_type_stack;
8475 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008476
8477 for (;;)
8478 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008479 if (ends_excmd2(prev, p))
8480 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008481 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008482 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008483 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008484
8485 if (cctx->ctx_skip != SKIP_YES)
8486 {
8487 // check for non-void type
8488 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8489 if (type->tt_type == VAR_VOID)
8490 {
8491 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8492 return NULL;
8493 }
8494 }
8495
Bram Moolenaarad39c092020-02-26 18:23:43 +01008496 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008497 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008498 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008499 }
8500
Bram Moolenaare4984292020-12-13 14:19:25 +01008501 if (count > 0)
8502 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008503 long save_lnum = cctx->ctx_lnum;
8504
8505 // Use the line number where the command started.
8506 cctx->ctx_lnum = start_ctx_lnum;
8507
Bram Moolenaare4984292020-12-13 14:19:25 +01008508 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8509 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8510 else if (cmdidx == CMD_execute)
8511 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8512 else if (cmdidx == CMD_echomsg)
8513 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8514 else
8515 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008516
8517 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008519 return p;
8520}
8521
8522/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008523 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008524 * instruction to compute it and return OK.
8525 * Otherwise return FAIL, the caller must deal with any range.
8526 */
8527 static int
8528compile_variable_range(exarg_T *eap, cctx_T *cctx)
8529{
8530 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8531 char_u *p = skipdigits(eap->cmd);
8532
8533 if (p == range_end)
8534 return FAIL;
8535 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8536}
8537
8538/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008539 * :put r
8540 * :put ={expr}
8541 */
8542 static char_u *
8543compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8544{
8545 char_u *line = arg;
8546 linenr_T lnum;
8547 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008548 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008549
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008550 eap->regname = *line;
8551
8552 if (eap->regname == '=')
8553 {
8554 char_u *p = line + 1;
8555
8556 if (compile_expr0(&p, cctx) == FAIL)
8557 return NULL;
8558 line = p;
8559 }
8560 else if (eap->regname != NUL)
8561 ++line;
8562
Bram Moolenaar08597872020-12-10 19:43:40 +01008563 if (compile_variable_range(eap, cctx) == OK)
8564 {
8565 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8566 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008567 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008568 {
8569 // Either no range or a number.
8570 // "errormsg" will not be set because the range is ADDR_LINES.
8571 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008572 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008573 return NULL;
8574 if (eap->addr_count == 0)
8575 lnum = -1;
8576 else
8577 lnum = eap->line2;
8578 if (above)
8579 --lnum;
8580 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008581
8582 generate_PUT(cctx, eap->regname, lnum);
8583 return line;
8584}
8585
8586/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008587 * A command that is not compiled, execute with legacy code.
8588 */
8589 static char_u *
8590compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8591{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008592 char_u *p;
8593 int has_expr = FALSE;
8594 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008595
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008596 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008597 goto theend;
8598
Bram Moolenaare729ce22021-06-06 21:38:09 +02008599 // If there was a prececing command modifier, drop it and include it in the
8600 // EXEC command.
8601 if (cctx->ctx_has_cmdmod)
8602 {
8603 garray_T *instr = &cctx->ctx_instr;
8604 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8605
8606 if (isn->isn_type == ISN_CMDMOD)
8607 {
8608 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8609 ->cmod_filter_regmatch.regprog);
8610 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8611 --instr->ga_len;
8612 cctx->ctx_has_cmdmod = FALSE;
8613 }
8614 }
8615
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008616 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008617 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008618 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008619 int usefilter = FALSE;
8620
8621 has_expr = argt & (EX_XFILE | EX_EXPAND);
8622
8623 // If the command can be followed by a bar, find the bar and truncate
8624 // it, so that the following command can be compiled.
8625 // The '|' is overwritten with a NUL, it is put back below.
8626 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8627 && *eap->arg == '!')
8628 // :w !filter or :r !filter or :r! filter
8629 usefilter = TRUE;
8630 if ((argt & EX_TRLBAR) && !usefilter)
8631 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008632 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008633 separate_nextcmd(eap);
8634 if (eap->nextcmd != NULL)
8635 nextcmd = eap->nextcmd;
8636 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008637 else if (eap->cmdidx == CMD_wincmd)
8638 {
8639 p = eap->arg;
8640 if (*p != NUL)
8641 ++p;
8642 if (*p == 'g' || *p == Ctrl_G)
8643 ++p;
8644 p = skipwhite(p);
8645 if (*p == '|')
8646 {
8647 *p = NUL;
8648 nextcmd = p + 1;
8649 }
8650 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008651 }
8652
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008653 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8654 {
8655 // expand filename in "syntax include [@group] filename"
8656 has_expr = TRUE;
8657 eap->arg = skipwhite(eap->arg + 7);
8658 if (*eap->arg == '@')
8659 eap->arg = skiptowhite(eap->arg);
8660 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008661
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008662 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8663 && STRLEN(eap->arg) > 4)
8664 {
8665 int delim = *eap->arg;
8666
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008667 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008668 if (*p == delim)
8669 {
8670 eap->arg = p + 1;
8671 has_expr = TRUE;
8672 }
8673 }
8674
Bram Moolenaarecac5912021-01-05 19:23:28 +01008675 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8676 {
8677 // TODO: should only expand when appropriate for the command
8678 eap->arg = skiptowhite(eap->arg);
8679 has_expr = TRUE;
8680 }
8681
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008682 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008683 {
8684 int count = 0;
8685 char_u *start = skipwhite(line);
8686
8687 // :cmd xxx`=expr1`yyy`=expr2`zzz
8688 // PUSHS ":cmd xxx"
8689 // eval expr1
8690 // PUSHS "yyy"
8691 // eval expr2
8692 // PUSHS "zzz"
8693 // EXECCONCAT 5
8694 for (;;)
8695 {
8696 if (p > start)
8697 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008698 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008699 ++count;
8700 }
8701 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008702 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008703 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008704 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008705 ++count;
8706 p = skipwhite(p);
8707 if (*p != '`')
8708 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008709 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008710 return NULL;
8711 }
8712 start = p + 1;
8713
8714 p = (char_u *)strstr((char *)start, "`=");
8715 if (p == NULL)
8716 {
8717 if (*skipwhite(start) != NUL)
8718 {
8719 generate_PUSHS(cctx, vim_strsave(start));
8720 ++count;
8721 }
8722 break;
8723 }
8724 }
8725 generate_EXECCONCAT(cctx, count);
8726 }
8727 else
8728 generate_EXEC(cctx, line);
8729
8730theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008731 if (*nextcmd != NUL)
8732 {
8733 // the parser expects a pointer to the bar, put it back
8734 --nextcmd;
8735 *nextcmd = '|';
8736 }
8737
8738 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008739}
8740
Bram Moolenaar20677332021-06-06 17:02:53 +02008741/*
8742 * A script command with heredoc, e.g.
8743 * ruby << EOF
8744 * command
8745 * EOF
8746 * Has been turned into one long line with NL characters by
8747 * get_function_body():
8748 * ruby << EOF<NL> command<NL>EOF
8749 */
8750 static char_u *
8751compile_script(char_u *line, cctx_T *cctx)
8752{
8753 if (cctx->ctx_skip != SKIP_YES)
8754 {
8755 isn_T *isn;
8756
8757 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8758 return NULL;
8759 isn->isn_arg.string = vim_strsave(line);
8760 }
8761 return (char_u *)"";
8762}
8763
Bram Moolenaar8238f082021-04-20 21:10:48 +02008764
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008765/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008766 * :s/pat/repl/
8767 */
8768 static char_u *
8769compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8770{
8771 char_u *cmd = eap->arg;
8772 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8773
8774 if (expr != NULL)
8775 {
8776 int delimiter = *cmd++;
8777
8778 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008779 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008780 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8781 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008782 garray_T save_ga = cctx->ctx_instr;
8783 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008784 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008785 int trailing_error;
8786 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008787 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008788 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008789
8790 cmd += 3;
8791 end = skip_substitute(cmd, delimiter);
8792
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008793 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008794 // labels are correct.
8795 cctx->ctx_instr.ga_len = 0;
8796 cctx->ctx_instr.ga_maxlen = 0;
8797 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008798 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008799 if (end[-1] == NUL)
8800 end[-1] = delimiter;
8801 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008802 trailing_error = *cmd != delimiter && *cmd != NUL;
8803
Bram Moolenaar169502f2021-04-21 12:19:35 +02008804 if (expr_res == FAIL || trailing_error
8805 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008806 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008807 if (trailing_error)
8808 semsg(_(e_trailing_arg), cmd);
8809 clear_instr_ga(&cctx->ctx_instr);
8810 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008811 return NULL;
8812 }
8813
Bram Moolenaar8238f082021-04-20 21:10:48 +02008814 // Move the generated instructions into the ISN_SUBSTITUTE
8815 // instructions, then restore the list of instructions before
8816 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008817 instr_count = cctx->ctx_instr.ga_len;
8818 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008819 instr[instr_count].isn_type = ISN_FINISH;
8820
8821 cctx->ctx_instr = save_ga;
8822 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8823 {
8824 int idx;
8825
8826 for (idx = 0; idx < instr_count; ++idx)
8827 delete_instr(instr + idx);
8828 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008829 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008830 }
8831 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8832 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008833
8834 // skip over flags
8835 if (*end == '&')
8836 ++end;
8837 while (ASCII_ISALPHA(*end) || *end == '#')
8838 ++end;
8839 return end;
8840 }
8841 }
8842
8843 return compile_exec(arg, eap, cctx);
8844}
8845
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008846 static char_u *
8847compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8848{
8849 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008850 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008851
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008852 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008853 {
8854 if (STRNCMP(arg, "END", 3) == 0)
8855 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008856 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008857 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008858 // First load the current variable value.
8859 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8860 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008861 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008862 }
8863
8864 // Gets the redirected text and put it on the stack, then store it
8865 // in the variable.
8866 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8867
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008868 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008869 generate_instr_drop(cctx, ISN_CONCAT, 1);
8870
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008871 if (lhs->lhs_has_index)
8872 {
8873 // Use the info in "lhs" to store the value at the index in the
8874 // list or dict.
8875 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8876 &t_string, cctx) == FAIL)
8877 return NULL;
8878 }
8879 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008880 return NULL;
8881
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008882 VIM_CLEAR(lhs->lhs_name);
8883 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008884 return arg + 3;
8885 }
8886 emsg(_(e_cannot_nest_redir));
8887 return NULL;
8888 }
8889
8890 if (arg[0] == '=' && arg[1] == '>')
8891 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008892 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008893
8894 // redirect to a variable is compiled
8895 arg += 2;
8896 if (*arg == '>')
8897 {
8898 ++arg;
8899 append = TRUE;
8900 }
8901 arg = skipwhite(arg);
8902
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008903 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008904 FALSE, FALSE, 1, cctx) == FAIL)
8905 return NULL;
8906 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008907 lhs->lhs_append = append;
8908 if (lhs->lhs_has_index)
8909 {
8910 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8911 if (lhs->lhs_whole == NULL)
8912 return NULL;
8913 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008914
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008915 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008916 }
8917
8918 // other redirects are handled like at script level
8919 return compile_exec(line, eap, cctx);
8920}
8921
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008922#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008923 static char_u *
8924compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8925{
8926 isn_T *isn;
8927 char_u *p;
8928
8929 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8930 if (isn == NULL)
8931 return NULL;
8932 isn->isn_arg.number = eap->cmdidx;
8933
8934 p = eap->arg;
8935 if (compile_expr0(&p, cctx) == FAIL)
8936 return NULL;
8937
8938 isn = generate_instr(cctx, ISN_CEXPR_CORE);
8939 if (isn == NULL)
8940 return NULL;
8941 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
8942 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
8943 return NULL;
8944 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
8945 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
8946 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
8947
8948 return p;
8949}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008950#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008951
Bram Moolenaar4c137212021-04-19 16:48:48 +02008952/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008953 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008954 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008955 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008956 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008957add_def_function(ufunc_T *ufunc)
8958{
8959 dfunc_T *dfunc;
8960
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008961 if (def_functions.ga_len == 0)
8962 {
8963 // The first position is not used, so that a zero uf_dfunc_idx means it
8964 // wasn't set.
8965 if (ga_grow(&def_functions, 1) == FAIL)
8966 return FAIL;
8967 ++def_functions.ga_len;
8968 }
8969
Bram Moolenaar09689a02020-05-09 22:50:08 +02008970 // Add the function to "def_functions".
8971 if (ga_grow(&def_functions, 1) == FAIL)
8972 return FAIL;
8973 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8974 CLEAR_POINTER(dfunc);
8975 dfunc->df_idx = def_functions.ga_len;
8976 ufunc->uf_dfunc_idx = dfunc->df_idx;
8977 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008978 dfunc->df_name = vim_strsave(ufunc->uf_name);
8979 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008980 ++def_functions.ga_len;
8981 return OK;
8982}
8983
8984/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008985 * After ex_function() has collected all the function lines: parse and compile
8986 * the lines into instructions.
8987 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008988 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8989 * the return statement (used for lambda). When uf_ret_type is already set
8990 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008991 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008992 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008993 * This can be used recursively through compile_lambda(), which may reallocate
8994 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008995 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008996 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008997 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008998compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02008999 ufunc_T *ufunc,
9000 int check_return_type,
9001 compiletype_T compile_type,
9002 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009003{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009004 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009005 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009006 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009007 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009008 cctx_T cctx;
9009 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009010 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009011 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009012 int ret = FAIL;
9013 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009014 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009015 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009016 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009017 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009018#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009019 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009020#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009021 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009022
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009023 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009024 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009025 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009026 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009027 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9028 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009029 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009030 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009031 else
9032 {
9033 if (add_def_function(ufunc) == FAIL)
9034 return FAIL;
9035 new_def_function = TRUE;
9036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009037
Bram Moolenaar985116a2020-07-12 17:31:09 +02009038 ufunc->uf_def_status = UF_COMPILING;
9039
Bram Moolenaara80faa82020-04-12 19:37:17 +02009040 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009041
Bram Moolenaare99d4222021-06-13 14:01:26 +02009042 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009043 cctx.ctx_ufunc = ufunc;
9044 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009045 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009046 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9047 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9048 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9049 cctx.ctx_type_list = &ufunc->uf_type_list;
9050 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9051 instr = &cctx.ctx_instr;
9052
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009053 // Set the context to the function, it may be compiled when called from
9054 // another script. Set the script version to the most modern one.
9055 // The line number will be set in next_line_from_context().
9056 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009057 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9058
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009059 // Don't use the flag from ":legacy" here.
9060 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9061
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009062 // Make sure error messages are OK.
9063 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9064 if (do_estack_push)
9065 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009066 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009067
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009068 if (ufunc->uf_def_args.ga_len > 0)
9069 {
9070 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009071 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009072 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009073 int i;
9074 char_u *arg;
9075 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009076 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009077
9078 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009079 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009080 for (i = 0; i < count; ++i)
9081 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009082 garray_T *stack = &cctx.ctx_type_stack;
9083 type_T *val_type;
9084 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009085 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009086 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009087 int jump_instr_idx = instr->ga_len;
9088 isn_T *isn;
9089
9090 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9091 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9092 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009093
9094 // Make sure later arguments are not found.
9095 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009096
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009097 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009098 r = compile_expr0(&arg, &cctx);
9099
9100 ufunc->uf_args.ga_len = uf_args_len;
9101 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009102 goto erret;
9103
9104 // If no type specified use the type of the default value.
9105 // Otherwise check that the default value type matches the
9106 // specified type.
9107 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009108 where.wt_index = arg_idx + 1;
9109 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009110 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009111 {
9112 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009113 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009114 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009115 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009116 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009117 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009118
9119 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009120 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009121
9122 // set instruction index in JUMP_IF_ARG_SET to here
9123 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9124 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009125 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009126
9127 if (did_set_arg_type)
9128 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009129 }
9130
9131 /*
9132 * Loop over all the lines of the function and generate instructions.
9133 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009134 for (;;)
9135 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009136 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009137 int starts_with_colon = FALSE;
9138 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009139 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009140
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009141 // Bail out on the first error to avoid a flood of errors and report
9142 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009143 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009144 goto erret;
9145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009146 if (line != NULL && *line == '|')
9147 // the line continues after a '|'
9148 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009149 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009150 && !(*line == '#' && (line == cctx.ctx_line_start
9151 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009152 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009153 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009154 goto erret;
9155 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009156 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9157 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009158 else
9159 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009160 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009161 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009162 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009163 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009164#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009165 if (cctx.ctx_skip != SKIP_YES)
9166 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009167#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009168 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009169 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009170 // Make a copy, splitting off nextcmd and removing trailing spaces
9171 // may change it.
9172 if (line != NULL)
9173 {
9174 line = vim_strsave(line);
9175 vim_free(line_to_free);
9176 line_to_free = line;
9177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009178 }
9179
Bram Moolenaara80faa82020-04-12 19:37:17 +02009180 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009181 ea.cmdlinep = &line;
9182 ea.cmd = skipwhite(line);
9183
Bram Moolenaarb2049902021-01-24 12:53:53 +01009184 if (*ea.cmd == '#')
9185 {
9186 // "#" starts a comment
9187 line = (char_u *)"";
9188 continue;
9189 }
9190
Bram Moolenaarf002a412021-01-24 13:34:18 +01009191#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009192 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9193 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009194 {
9195 may_generate_prof_end(&cctx, prof_lnum);
9196
9197 prof_lnum = cctx.ctx_lnum;
9198 generate_instr(&cctx, ISN_PROF_START);
9199 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009200#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009201 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9202 && cctx.ctx_skip != SKIP_YES)
9203 {
9204 debug_lnum = cctx.ctx_lnum;
9205 generate_instr(&cctx, ISN_DEBUG);
9206 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01009207
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009208 // Some things can be recognized by the first character.
9209 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009210 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009211 case '}':
9212 {
9213 // "}" ends a block scope
9214 scopetype_T stype = cctx.ctx_scope == NULL
9215 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009216
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009217 if (stype == BLOCK_SCOPE)
9218 {
9219 compile_endblock(&cctx);
9220 line = ea.cmd;
9221 }
9222 else
9223 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009224 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009225 goto erret;
9226 }
9227 if (line != NULL)
9228 line = skipwhite(ea.cmd + 1);
9229 continue;
9230 }
9231
9232 case '{':
9233 // "{" starts a block scope
9234 // "{'a': 1}->func() is something else
9235 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9236 {
9237 line = compile_block(ea.cmd, &cctx);
9238 continue;
9239 }
9240 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009241 }
9242
9243 /*
9244 * COMMAND MODIFIERS
9245 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009246 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009247 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9248 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009249 {
9250 if (errormsg != NULL)
9251 goto erret;
9252 // empty line or comment
9253 line = (char_u *)"";
9254 continue;
9255 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009256 generate_cmdmods(&cctx, &local_cmdmod);
9257 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009258
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009259 // Check if there was a colon after the last command modifier or before
9260 // the current position.
9261 for (p = ea.cmd; p >= line; --p)
9262 {
9263 if (*p == ':')
9264 starts_with_colon = TRUE;
9265 if (p < ea.cmd && !VIM_ISWHITE(*p))
9266 break;
9267 }
9268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009269 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009270 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009271 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009272 {
9273 if (*ea.cmd == '(')
9274 // not for "call()"
9275 ea.cmd = p;
9276 else
9277 ea.cmd = skipwhite(ea.cmd);
9278 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009279
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009280 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009281 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009282 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009283
Bram Moolenaar17126b12021-01-07 22:03:02 +01009284 // Check for assignment after command modifiers.
9285 assign = may_compile_assignment(&ea, &line, &cctx);
9286 if (assign == OK)
9287 goto nextline;
9288 if (assign == FAIL)
9289 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009290 }
9291
9292 /*
9293 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009294 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009295 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009296 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009297 cmd = ea.cmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009298 if (starts_with_colon || !(*cmd == '\''
9299 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009300 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009301 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009302 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009303 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009304 if (!starts_with_colon)
9305 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009306 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009307 goto erret;
9308 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009309 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009310 if (ends_excmd2(line, ea.cmd))
9311 {
9312 // A range without a command: jump to the line.
9313 // TODO: compile to a more efficient command, possibly
9314 // calling parse_cmd_address().
9315 ea.cmdidx = CMD_SIZE;
9316 line = compile_exec(line, &ea, &cctx);
9317 goto nextline;
9318 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009319 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009320 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009321 p = find_ex_command(&ea, NULL, starts_with_colon
9322 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009323
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009324 if (p == NULL)
9325 {
9326 if (cctx.ctx_skip != SKIP_YES)
9327 emsg(_(e_ambiguous_use_of_user_defined_command));
9328 goto erret;
9329 }
9330
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009331 // When using ":legacy cmd" always use compile_exec().
9332 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009333 {
9334 char_u *start = ea.cmd;
9335
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009336 switch (ea.cmdidx)
9337 {
9338 case CMD_if:
9339 case CMD_elseif:
9340 case CMD_else:
9341 case CMD_endif:
9342 case CMD_for:
9343 case CMD_endfor:
9344 case CMD_continue:
9345 case CMD_break:
9346 case CMD_while:
9347 case CMD_endwhile:
9348 case CMD_try:
9349 case CMD_catch:
9350 case CMD_finally:
9351 case CMD_endtry:
9352 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9353 goto erret;
9354 default: break;
9355 }
9356
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009357 // ":legacy return expr" needs to be handled differently.
9358 if (checkforcmd(&start, "return", 4))
9359 ea.cmdidx = CMD_return;
9360 else
9361 ea.cmdidx = CMD_legacy;
9362 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009364 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9365 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009366 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009367 {
9368 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009369 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009370 }
9371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009372 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009373 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009374 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009375 // CMD_var cannot happen, compile_assignment() above would be
9376 // used. Most likely an assignment to a non-existing variable.
9377 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009378 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009379 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009380 }
9381
Bram Moolenaar3988f642020-08-27 22:43:03 +02009382 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009383 && ea.cmdidx != CMD_elseif
9384 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009385 && ea.cmdidx != CMD_endif
9386 && ea.cmdidx != CMD_endfor
9387 && ea.cmdidx != CMD_endwhile
9388 && ea.cmdidx != CMD_catch
9389 && ea.cmdidx != CMD_finally
9390 && ea.cmdidx != CMD_endtry)
9391 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009392 emsg(_(e_unreachable_code_after_return));
9393 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009394 }
9395
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009396 p = skipwhite(p);
9397 if (ea.cmdidx != CMD_SIZE
9398 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9399 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009400 if (ea.cmdidx >= 0)
9401 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009402 if ((ea.argt & EX_BANG) && *p == '!')
9403 {
9404 ea.forceit = TRUE;
9405 p = skipwhite(p + 1);
9406 }
9407 }
9408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009409 switch (ea.cmdidx)
9410 {
9411 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009412 ea.arg = p;
9413 line = compile_nested_function(&ea, &cctx);
9414 break;
9415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009416 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009417 // TODO: should we allow this, e.g. to declare a global
9418 // function?
9419 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009420 goto erret;
9421
9422 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009423 line = compile_return(p, check_return_type,
9424 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009425 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009426 break;
9427
9428 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009429 emsg(_(e_cannot_use_let_in_vim9_script));
9430 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009431 case CMD_var:
9432 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009433 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009434 case CMD_increment:
9435 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009436 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009437 if (line == p)
9438 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009439 break;
9440
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009441 case CMD_unlet:
9442 case CMD_unlockvar:
9443 case CMD_lockvar:
9444 line = compile_unletlock(p, &ea, &cctx);
9445 break;
9446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009447 case CMD_import:
9448 line = compile_import(p, &cctx);
9449 break;
9450
9451 case CMD_if:
9452 line = compile_if(p, &cctx);
9453 break;
9454 case CMD_elseif:
9455 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009456 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009457 break;
9458 case CMD_else:
9459 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009460 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009461 break;
9462 case CMD_endif:
9463 line = compile_endif(p, &cctx);
9464 break;
9465
9466 case CMD_while:
9467 line = compile_while(p, &cctx);
9468 break;
9469 case CMD_endwhile:
9470 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009471 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009472 break;
9473
9474 case CMD_for:
9475 line = compile_for(p, &cctx);
9476 break;
9477 case CMD_endfor:
9478 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009479 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009480 break;
9481 case CMD_continue:
9482 line = compile_continue(p, &cctx);
9483 break;
9484 case CMD_break:
9485 line = compile_break(p, &cctx);
9486 break;
9487
9488 case CMD_try:
9489 line = compile_try(p, &cctx);
9490 break;
9491 case CMD_catch:
9492 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009493 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009494 break;
9495 case CMD_finally:
9496 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009497 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009498 break;
9499 case CMD_endtry:
9500 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009501 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009502 break;
9503 case CMD_throw:
9504 line = compile_throw(p, &cctx);
9505 break;
9506
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009507 case CMD_eval:
9508 if (compile_expr0(&p, &cctx) == FAIL)
9509 goto erret;
9510
Bram Moolenaar3988f642020-08-27 22:43:03 +02009511 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009512 generate_instr_drop(&cctx, ISN_DROP, 1);
9513
9514 line = skipwhite(p);
9515 break;
9516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009517 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009518 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009519 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009520 case CMD_echomsg:
9521 case CMD_echoerr:
9522 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009523 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009524
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009525 case CMD_put:
9526 ea.cmd = cmd;
9527 line = compile_put(p, &ea, &cctx);
9528 break;
9529
Bram Moolenaar4c137212021-04-19 16:48:48 +02009530 case CMD_substitute:
9531 if (cctx.ctx_skip == SKIP_YES)
9532 line = (char_u *)"";
9533 else
9534 {
9535 ea.arg = p;
9536 line = compile_substitute(line, &ea, &cctx);
9537 }
9538 break;
9539
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009540 case CMD_redir:
9541 ea.arg = p;
9542 line = compile_redir(line, &ea, &cctx);
9543 break;
9544
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009545 case CMD_cexpr:
9546 case CMD_lexpr:
9547 case CMD_caddexpr:
9548 case CMD_laddexpr:
9549 case CMD_cgetexpr:
9550 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009551#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009552 ea.arg = p;
9553 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009554#else
9555 ex_ni(&ea);
9556 line = NULL;
9557#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009558 break;
9559
Bram Moolenaar3988f642020-08-27 22:43:03 +02009560 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009561
Bram Moolenaarae616492020-07-28 20:07:27 +02009562 case CMD_append:
9563 case CMD_change:
9564 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009565 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009566 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009567 case CMD_xit:
9568 not_in_vim9(&ea);
9569 goto erret;
9570
Bram Moolenaar002262f2020-07-08 17:47:57 +02009571 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009572 if (cctx.ctx_skip != SKIP_YES)
9573 {
9574 semsg(_(e_invalid_command_str), ea.cmd);
9575 goto erret;
9576 }
9577 // We don't check for a next command here.
9578 line = (char_u *)"";
9579 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009580
Bram Moolenaar20677332021-06-06 17:02:53 +02009581 case CMD_lua:
9582 case CMD_mzscheme:
9583 case CMD_perl:
9584 case CMD_py3:
9585 case CMD_python3:
9586 case CMD_python:
9587 case CMD_pythonx:
9588 case CMD_ruby:
9589 case CMD_tcl:
9590 ea.arg = p;
9591 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009592 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009593 else
9594 // heredoc lines have been concatenated with NL
9595 // characters in get_function_body()
9596 line = compile_script(line, &cctx);
9597 break;
9598
9599 default:
9600 // Not recognized, execute with do_cmdline_cmd().
9601 ea.arg = p;
9602 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009603 break;
9604 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009605nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009606 if (line == NULL)
9607 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009608 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009609
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009610 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009611 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009613 if (cctx.ctx_type_stack.ga_len < 0)
9614 {
9615 iemsg("Type stack underflow");
9616 goto erret;
9617 }
9618 }
9619
9620 if (cctx.ctx_scope != NULL)
9621 {
9622 if (cctx.ctx_scope->se_type == IF_SCOPE)
9623 emsg(_(e_endif));
9624 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9625 emsg(_(e_endwhile));
9626 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9627 emsg(_(e_endfor));
9628 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009629 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009630 goto erret;
9631 }
9632
Bram Moolenaarefd88552020-06-18 20:50:10 +02009633 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009634 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009635 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9636 ufunc->uf_ret_type = &t_void;
9637 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009638 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009639 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009640 goto erret;
9641 }
9642
9643 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01009644 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009645 }
9646
Bram Moolenaar599410c2021-04-10 14:03:43 +02009647 // When compiled with ":silent!" and there was an error don't consider the
9648 // function compiled.
9649 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009650 {
9651 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9652 + ufunc->uf_dfunc_idx;
9653 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009654 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009655#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009656 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009657 {
9658 dfunc->df_instr_prof = instr->ga_data;
9659 dfunc->df_instr_prof_count = instr->ga_len;
9660 }
9661 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009662#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009663 if (cctx.ctx_compile_type == CT_DEBUG)
9664 {
9665 dfunc->df_instr_debug = instr->ga_data;
9666 dfunc->df_instr_debug_count = instr->ga_len;
9667 }
9668 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009669 {
9670 dfunc->df_instr = instr->ga_data;
9671 dfunc->df_instr_count = instr->ga_len;
9672 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009673 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009674 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009675 if (cctx.ctx_outer_used)
9676 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009677 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009678 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009679
9680 ret = OK;
9681
9682erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009683 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009684 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009685 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9686 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009687
Bram Moolenaar8238f082021-04-20 21:10:48 +02009688 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009689 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009690
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009691 // If using the last entry in the table and it was added above, we
9692 // might as well remove it.
9693 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009694 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009695 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009696 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009697 ufunc->uf_dfunc_idx = 0;
9698 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009699 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009700
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009701 while (cctx.ctx_scope != NULL)
9702 drop_scope(&cctx);
9703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009704 if (errormsg != NULL)
9705 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009706 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009707 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009708 }
9709
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009710 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9711 {
9712 if (ret == OK)
9713 {
9714 emsg(_(e_missing_redir_end));
9715 ret = FAIL;
9716 }
9717 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009718 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009719 }
9720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009721 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009722 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009723 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009724 if (do_estack_push)
9725 estack_pop();
9726
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009727 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009728 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009729 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009730 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009731 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009732}
9733
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009734 void
9735set_function_type(ufunc_T *ufunc)
9736{
9737 int varargs = ufunc->uf_va_name != NULL;
9738 int argcount = ufunc->uf_args.ga_len;
9739
9740 // Create a type for the function, with the return type and any
9741 // argument types.
9742 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9743 // The type is included in "tt_args".
9744 if (argcount > 0 || varargs)
9745 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009746 if (ufunc->uf_type_list.ga_itemsize == 0)
9747 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009748 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9749 argcount, &ufunc->uf_type_list);
9750 // Add argument types to the function type.
9751 if (func_type_add_arg_types(ufunc->uf_func_type,
9752 argcount + varargs,
9753 &ufunc->uf_type_list) == FAIL)
9754 return;
9755 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9756 ufunc->uf_func_type->tt_min_argcount =
9757 argcount - ufunc->uf_def_args.ga_len;
9758 if (ufunc->uf_arg_types == NULL)
9759 {
9760 int i;
9761
9762 // lambda does not have argument types.
9763 for (i = 0; i < argcount; ++i)
9764 ufunc->uf_func_type->tt_args[i] = &t_any;
9765 }
9766 else
9767 mch_memmove(ufunc->uf_func_type->tt_args,
9768 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9769 if (varargs)
9770 {
9771 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009772 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009773 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9774 }
9775 }
9776 else
9777 // No arguments, can use a predefined type.
9778 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9779 argcount, &ufunc->uf_type_list);
9780}
9781
9782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009783/*
9784 * Delete an instruction, free what it contains.
9785 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009786 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009787delete_instr(isn_T *isn)
9788{
9789 switch (isn->isn_type)
9790 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009791 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009792 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009793 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009794 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009795 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009796 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009797 case ISN_LOADENV:
9798 case ISN_LOADG:
9799 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009800 case ISN_LOADT:
9801 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009802 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009803 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009804 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009805 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009806 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009807 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009808 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009809 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009810 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009811 case ISN_STOREW:
9812 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009813 vim_free(isn->isn_arg.string);
9814 break;
9815
Bram Moolenaar4c137212021-04-19 16:48:48 +02009816 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009817 {
9818 int idx;
9819 isn_T *list = isn->isn_arg.subs.subs_instr;
9820
9821 vim_free(isn->isn_arg.subs.subs_cmd);
9822 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9823 delete_instr(list + idx);
9824 vim_free(list);
9825 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009826 break;
9827
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009828 case ISN_INSTR:
9829 {
9830 int idx;
9831 isn_T *list = isn->isn_arg.instr;
9832
9833 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9834 delete_instr(list + idx);
9835 vim_free(list);
9836 }
9837 break;
9838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009839 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009840 case ISN_STORES:
9841 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009842 break;
9843
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009844 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009845 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009846 vim_free(isn->isn_arg.unlet.ul_name);
9847 break;
9848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009849 case ISN_STOREOPT:
9850 vim_free(isn->isn_arg.storeopt.so_name);
9851 break;
9852
9853 case ISN_PUSHBLOB: // push blob isn_arg.blob
9854 blob_unref(isn->isn_arg.blob);
9855 break;
9856
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009857 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009858#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009859 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009860#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009861 break;
9862
9863 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009864#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009865 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009866#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009867 break;
9868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009869 case ISN_UCALL:
9870 vim_free(isn->isn_arg.ufunc.cuf_name);
9871 break;
9872
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009873 case ISN_FUNCREF:
9874 {
9875 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9876 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009877 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009878
Bram Moolenaar077a4232020-12-22 18:33:27 +01009879 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9880 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009881 }
9882 break;
9883
9884 case ISN_DCALL:
9885 {
9886 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9887 + isn->isn_arg.dfunc.cdf_idx;
9888
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009889 if (dfunc->df_ufunc != NULL
9890 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009891 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009892 }
9893 break;
9894
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009895 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009896 {
9897 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9898 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9899
9900 if (ufunc != NULL)
9901 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009902 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009903 func_ptr_unref(ufunc);
9904 }
9905
9906 vim_free(lambda);
9907 vim_free(isn->isn_arg.newfunc.nf_global);
9908 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009909 break;
9910
Bram Moolenaar5e654232020-09-16 15:22:00 +02009911 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009912 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009913 free_type(isn->isn_arg.type.ct_type);
9914 break;
9915
Bram Moolenaar02194d22020-10-24 23:08:38 +02009916 case ISN_CMDMOD:
9917 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9918 ->cmod_filter_regmatch.regprog);
9919 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9920 break;
9921
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009922 case ISN_LOADSCRIPT:
9923 case ISN_STORESCRIPT:
9924 vim_free(isn->isn_arg.script.scriptref);
9925 break;
9926
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009927 case ISN_TRY:
9928 vim_free(isn->isn_arg.try.try_ref);
9929 break;
9930
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009931 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +02009932 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009933 vim_free(isn->isn_arg.cexpr.cexpr_ref);
9934 break;
9935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009936 case ISN_2BOOL:
9937 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009938 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009939 case ISN_ADDBLOB:
9940 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009941 case ISN_ANYINDEX:
9942 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009943 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009944 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009945 case ISN_BLOBINDEX:
9946 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009947 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009948 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009949 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009950 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009951 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009952 case ISN_COMPAREANY:
9953 case ISN_COMPAREBLOB:
9954 case ISN_COMPAREBOOL:
9955 case ISN_COMPAREDICT:
9956 case ISN_COMPAREFLOAT:
9957 case ISN_COMPAREFUNC:
9958 case ISN_COMPARELIST:
9959 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009960 case ISN_COMPARESPECIAL:
9961 case ISN_COMPARESTRING:
9962 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009963 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +02009964 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009965 case ISN_DROP:
9966 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009967 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009968 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009969 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009970 case ISN_EXECCONCAT:
9971 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009972 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +02009973 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009974 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009975 case ISN_GETITEM:
9976 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009977 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009978 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009979 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009980 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009981 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009982 case ISN_LOADBDICT:
9983 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009984 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009985 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009986 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009987 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009988 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009989 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009990 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009991 case ISN_NEGATENR:
9992 case ISN_NEWDICT:
9993 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009994 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009995 case ISN_OPFLOAT:
9996 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009997 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009998 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009999 case ISN_PROF_END:
10000 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010001 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010002 case ISN_PUSHF:
10003 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010004 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010005 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010006 case ISN_REDIREND:
10007 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010008 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +010010009 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010010 case ISN_SHUFFLE:
10011 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010012 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010013 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010014 case ISN_STORENR:
10015 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010016 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010017 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010018 case ISN_STOREV:
10019 case ISN_STRINDEX:
10020 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010021 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010022 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010023 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010024 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010025 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010026 // nothing allocated
10027 break;
10028 }
10029}
10030
10031/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010032 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010033 */
10034 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010035delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010036{
10037 int idx;
10038
10039 ga_clear(&dfunc->df_def_args_isn);
10040
10041 if (dfunc->df_instr != NULL)
10042 {
10043 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10044 delete_instr(dfunc->df_instr + idx);
10045 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010046 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010047 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010048 if (dfunc->df_instr_debug != NULL)
10049 {
10050 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10051 delete_instr(dfunc->df_instr_debug + idx);
10052 VIM_CLEAR(dfunc->df_instr_debug);
10053 dfunc->df_instr_debug = NULL;
10054 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010055#ifdef FEAT_PROFILE
10056 if (dfunc->df_instr_prof != NULL)
10057 {
10058 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10059 delete_instr(dfunc->df_instr_prof + idx);
10060 VIM_CLEAR(dfunc->df_instr_prof);
10061 dfunc->df_instr_prof = NULL;
10062 }
10063#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010064
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010065 if (mark_deleted)
10066 dfunc->df_deleted = TRUE;
10067 if (dfunc->df_ufunc != NULL)
10068 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010069}
10070
10071/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010072 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010073 * function, unless another user function still uses it.
10074 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010075 */
10076 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010077unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010078{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010079 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010080 {
10081 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10082 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010083
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010084 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010085 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010086 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010087 ufunc->uf_dfunc_idx = 0;
10088 if (dfunc->df_ufunc == ufunc)
10089 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010090 }
10091}
10092
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010093/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010094 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010095 */
10096 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010097link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010098{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010099 if (ufunc->uf_dfunc_idx > 0)
10100 {
10101 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10102 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010103
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010104 ++dfunc->df_refcount;
10105 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010106}
10107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010108#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010109/*
10110 * Free all functions defined with ":def".
10111 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010112 void
10113free_def_functions(void)
10114{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010115 int idx;
10116
10117 for (idx = 0; idx < def_functions.ga_len; ++idx)
10118 {
10119 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10120
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010121 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010122 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010123 }
10124
10125 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010126}
10127#endif
10128
10129
10130#endif // FEAT_EVAL