blob: e78a032cb59f7fd1fa4fb3d5b85a7698945701fc [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 Moolenaar8cec9272021-06-23 20:20:53 +0200177 int ctx_prev_lnum; // line number below previous command, for
178 // debugging
179
Bram Moolenaare99d4222021-06-13 14:01:26 +0200180 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200184 int ctx_has_closure; // set to one if a closures was created in
185 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100187 garray_T ctx_imports; // imported items
188
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200189 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200191 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193 cctx_T *ctx_outer; // outer scope for lambda or nested
194 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200198 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200199
Bram Moolenaar02194d22020-10-24 23:08:38 +0200200 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200201
202 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
203 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204};
205
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100206static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207
208/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100209 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100210 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100211 * If "lvar" is NULL only check if the variable can be found.
212 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100214 static int
215lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100218 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100221 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200222
223 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
225 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100226 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
227 if (STRNCMP(name, lvp->lv_name, len) == 0
228 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200229 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100230 if (lvar != NULL)
231 {
232 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100233 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100234 }
235 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200238
239 // Find local in outer function scope.
240 if (cctx->ctx_outer != NULL)
241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100242 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200243 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100244 if (lvar != NULL)
245 {
246 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100247 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100248 }
249 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200250 }
251 }
252
Bram Moolenaar709664c2020-12-12 14:33:41 +0100253 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254}
255
256/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200257 * Lookup an argument in the current function and an enclosing function.
258 * Returns the argument index in "idxp"
259 * Returns the argument type in "type"
260 * Sets "gen_load_outer" to TRUE if found in outer scope.
261 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 */
263 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200265 char_u *name,
266 size_t len,
267 int *idxp,
268 type_T **type,
269 int *gen_load_outer,
270 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271{
272 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200273 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100275 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200276 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
278 {
279 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
280
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200281 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
282 {
283 if (idxp != NULL)
284 {
285 // Arguments are located above the frame pointer. One further
286 // if there is a vararg argument
287 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
288 + STACK_FRAME_SIZE)
289 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
290
291 if (cctx->ctx_ufunc->uf_arg_types != NULL)
292 *type = cctx->ctx_ufunc->uf_arg_types[idx];
293 else
294 *type = &t_any;
295 }
296 return OK;
297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200300 va_name = cctx->ctx_ufunc->uf_va_name;
301 if (va_name != NULL
302 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
303 {
304 if (idxp != NULL)
305 {
306 // varargs is always the last argument
307 *idxp = -STACK_FRAME_SIZE - 1;
308 *type = cctx->ctx_ufunc->uf_va_type;
309 }
310 return OK;
311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200313 if (cctx->ctx_outer != NULL)
314 {
315 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200316 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200317 == OK)
318 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100319 if (gen_load_outer != NULL)
320 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200321 return OK;
322 }
323 }
324
325 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326}
327
328/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329 * Lookup a script-local variable in the current script, possibly defined in a
330 * block that contains the function "cctx->ctx_ufunc".
331 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100332 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * Return NULL when not found.
334 */
335 static sallvar_T *
336find_script_var(char_u *name, size_t len, cctx_T *cctx)
337{
338 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
339 hashitem_T *hi;
340 int cc;
341 sallvar_T *sav;
342 ufunc_T *ufunc;
343
344 // Find the list of all script variables with the right name.
345 if (len > 0)
346 {
347 cc = name[len];
348 name[len] = NUL;
349 }
350 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
351 if (len > 0)
352 name[len] = cc;
353 if (HASHITEM_EMPTY(hi))
354 return NULL;
355
356 sav = HI2SAV(hi);
357 if (sav->sav_block_id == 0 || cctx == NULL)
358 // variable defined in the script scope or not in a function.
359 return sav;
360
361 // Go over the variables with this name and find one that was visible
362 // from the function.
363 ufunc = cctx->ctx_ufunc;
364 while (sav != NULL)
365 {
366 int idx;
367
368 // Go over the blocks that this function was defined in. If the
369 // variable block ID matches it was visible to the function.
370 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
371 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
372 return sav;
373 sav = sav->sav_next;
374 }
375
376 return NULL;
377}
378
379/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100380 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200381 */
382 static int
383script_is_vim9()
384{
385 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
386}
387
388/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200389 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200390 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 * Returns OK or FAIL.
392 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200393 static int
394script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200396 if (current_sctx.sc_sid <= 0)
397 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200398 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200399 {
400 // Check script variables that were visible where the function was
401 // defined.
402 if (find_script_var(name, len, cctx) != NULL)
403 return OK;
404 }
405 else
406 {
407 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
408 dictitem_T *di;
409 int cc;
410
411 // Check script variables that are currently visible
412 cc = name[len];
413 name[len] = NUL;
414 di = find_var_in_ht(ht, 0, name, TRUE);
415 name[len] = cc;
416 if (di != NULL)
417 return OK;
418 }
419
420 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100421}
422
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100423/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100424 * Return TRUE if "name" is a local variable, argument, script variable or
425 * imported.
426 */
427 static int
428variable_exists(char_u *name, size_t len, cctx_T *cctx)
429{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100430 return (cctx != NULL
431 && (lookup_local(name, len, NULL, cctx) == OK
432 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200433 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100434 || find_imported(name, len, cctx) != NULL;
435}
436
437/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100438 * Return TRUE if "name" is a local variable, argument, script variable,
439 * imported or function.
440 */
441 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100442item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100443{
444 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100445 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100446
447 if (variable_exists(name, len, cctx))
448 return TRUE;
449
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100450 // This is similar to what is in lookup_scriptitem():
451 // Find a function, so that a following "->" works.
452 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
453 // a function call.
454 p = skipwhite(name + len);
455
456 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
457 {
458 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200459 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100460 // Skip "g:" before a function name.
461 is_global = (name[0] == 'g' && name[1] == ':');
462 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
463 }
464 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100465}
466
467/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100468 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200469 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200470 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100471 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100472 * Return FAIL and give an error if it defined.
473 */
474 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100475check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100476{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200477 int c = p[len];
478 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200479
Bram Moolenaarda479c72021-04-10 21:01:38 +0200480 // underscore argument is OK
481 if (len == 1 && *p == '_')
482 return OK;
483
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200484 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100485 {
486 if (is_arg)
487 semsg(_(e_argument_already_declared_in_script_str), p);
488 else
489 semsg(_(e_variable_already_declared_in_script_str), p);
490 return FAIL;
491 }
492
Bram Moolenaarad486a02020-08-01 23:22:18 +0200493 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100494 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100495 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200496 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200497 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200498 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100499 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200500 // A local or script-local function can shadow a global function.
501 if (ufunc == NULL || !func_is_global(ufunc)
502 || (p[0] == 'g' && p[1] == ':'))
503 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100504 if (is_arg)
505 semsg(_(e_argument_name_shadows_existing_variable_str), p);
506 else
507 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200508 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200509 return FAIL;
510 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100511 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200512 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100513 return OK;
514}
515
Bram Moolenaar65b95452020-07-19 14:03:09 +0200516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100517/////////////////////////////////////////////////////////////////////
518// Following generate_ functions expect the caller to call ga_grow().
519
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200520#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
521#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523/*
524 * Generate an instruction without arguments.
525 * Returns a pointer to the new instruction, NULL if failed.
526 */
527 static isn_T *
528generate_instr(cctx_T *cctx, isntype_T isn_type)
529{
530 garray_T *instr = &cctx->ctx_instr;
531 isn_T *isn;
532
Bram Moolenaar080457c2020-03-03 21:53:32 +0100533 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534 if (ga_grow(instr, 1) == FAIL)
535 return NULL;
536 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
537 isn->isn_type = isn_type;
538 isn->isn_lnum = cctx->ctx_lnum + 1;
539 ++instr->ga_len;
540
541 return isn;
542}
543
544/*
545 * Generate an instruction without arguments.
546 * "drop" will be removed from the stack.
547 * Returns a pointer to the new instruction, NULL if failed.
548 */
549 static isn_T *
550generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
551{
552 garray_T *stack = &cctx->ctx_type_stack;
553
Bram Moolenaar080457c2020-03-03 21:53:32 +0100554 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 stack->ga_len -= drop;
556 return generate_instr(cctx, isn_type);
557}
558
559/*
560 * Generate instruction "isn_type" and put "type" on the type stack.
561 */
562 static isn_T *
563generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
564{
565 isn_T *isn;
566 garray_T *stack = &cctx->ctx_type_stack;
567
568 if ((isn = generate_instr(cctx, isn_type)) == NULL)
569 return NULL;
570
571 if (ga_grow(stack, 1) == FAIL)
572 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200573 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 ++stack->ga_len;
575
576 return isn;
577}
578
579/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200580 * Generate an ISN_DEBUG instruction.
581 */
582 static isn_T *
583generate_instr_debug(cctx_T *cctx)
584{
585 isn_T *isn;
586 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
587 + cctx->ctx_ufunc->uf_dfunc_idx;
588
589 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
590 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200591 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
592 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200593 return isn;
594}
595
596/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200598 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200599 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 */
601 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200602may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603{
604 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200605 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100607 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100609 RETURN_OK_IF_SKIP(cctx);
610 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200611 switch ((*type)->tt_type)
612 {
613 // nothing to be done
614 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200616 // conversion possible
617 case VAR_SPECIAL:
618 case VAR_BOOL:
619 case VAR_NUMBER:
620 case VAR_FLOAT:
621 break;
622
623 // conversion possible (with runtime check)
624 case VAR_ANY:
625 case VAR_UNKNOWN:
626 isntype = ISN_2STRING_ANY;
627 break;
628
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200629 // conversion possible when tolerant
630 case VAR_LIST:
631 if (tolerant)
632 {
633 isntype = ISN_2STRING_ANY;
634 break;
635 }
636 // FALLTHROUGH
637
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200638 // conversion not possible
639 case VAR_VOID:
640 case VAR_BLOB:
641 case VAR_FUNC:
642 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200643 case VAR_DICT:
644 case VAR_JOB:
645 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200646 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200647 to_string_error((*type)->tt_type);
648 return FAIL;
649 }
650
651 *type = &t_string;
652 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200654 isn->isn_arg.tostring.offset = offset;
655 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656
657 return OK;
658}
659
660 static int
661check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
662{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200663 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200665 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 {
667 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200668 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200670 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671 return FAIL;
672 }
673 return OK;
674}
675
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200676 static int
677generate_add_instr(
678 cctx_T *cctx,
679 vartype_T vartype,
680 type_T *type1,
681 type_T *type2)
682{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100683 garray_T *stack = &cctx->ctx_type_stack;
684 isn_T *isn = generate_instr_drop(cctx,
685 vartype == VAR_NUMBER ? ISN_OPNR
686 : vartype == VAR_LIST ? ISN_ADDLIST
687 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200688#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100689 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200690#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100691 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200692
693 if (vartype != VAR_LIST && vartype != VAR_BLOB
694 && type1->tt_type != VAR_ANY
695 && type2->tt_type != VAR_ANY
696 && check_number_or_float(
697 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
698 return FAIL;
699
700 if (isn != NULL)
701 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100702
703 // When concatenating two lists with different member types the member type
704 // becomes "any".
705 if (vartype == VAR_LIST
706 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
707 && type1->tt_member != type2->tt_member)
708 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
709
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200710 return isn == NULL ? FAIL : OK;
711}
712
713/*
714 * Get the type to use for an instruction for an operation on "type1" and
715 * "type2". If they are matching use a type-specific instruction. Otherwise
716 * fall back to runtime type checking.
717 */
718 static vartype_T
719operator_type(type_T *type1, type_T *type2)
720{
721 if (type1->tt_type == type2->tt_type
722 && (type1->tt_type == VAR_NUMBER
723 || type1->tt_type == VAR_LIST
724#ifdef FEAT_FLOAT
725 || type1->tt_type == VAR_FLOAT
726#endif
727 || type1->tt_type == VAR_BLOB))
728 return type1->tt_type;
729 return VAR_ANY;
730}
731
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732/*
733 * Generate an instruction with two arguments. The instruction depends on the
734 * type of the arguments.
735 */
736 static int
737generate_two_op(cctx_T *cctx, char_u *op)
738{
739 garray_T *stack = &cctx->ctx_type_stack;
740 type_T *type1;
741 type_T *type2;
742 vartype_T vartype;
743 isn_T *isn;
744
Bram Moolenaar080457c2020-03-03 21:53:32 +0100745 RETURN_OK_IF_SKIP(cctx);
746
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200747 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
749 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200750 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751
752 switch (*op)
753 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200754 case '+':
755 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 break;
758
759 case '-':
760 case '*':
761 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
762 op) == FAIL)
763 return FAIL;
764 if (vartype == VAR_NUMBER)
765 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
766#ifdef FEAT_FLOAT
767 else if (vartype == VAR_FLOAT)
768 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
769#endif
770 else
771 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
772 if (isn != NULL)
773 isn->isn_arg.op.op_type = *op == '*'
774 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
775 break;
776
Bram Moolenaar4c683752020-04-05 21:38:23 +0200777 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200779 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100780 && type2->tt_type != VAR_NUMBER))
781 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200782 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 return FAIL;
784 }
785 isn = generate_instr_drop(cctx,
786 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
787 if (isn != NULL)
788 isn->isn_arg.op.op_type = EXPR_REM;
789 break;
790 }
791
792 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200793 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 {
795 type_T *type = &t_any;
796
797#ifdef FEAT_FLOAT
798 // float+number and number+float results in float
799 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
800 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
801 type = &t_float;
802#endif
803 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
804 }
805
806 return OK;
807}
808
809/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200810 * Get the instruction to use for comparing "type1" with "type2"
811 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200813 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100814get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815{
816 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817
Bram Moolenaar4c683752020-04-05 21:38:23 +0200818 if (type1 == VAR_UNKNOWN)
819 type1 = VAR_ANY;
820 if (type2 == VAR_UNKNOWN)
821 type2 = VAR_ANY;
822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if (type1 == type2)
824 {
825 switch (type1)
826 {
827 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
828 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
829 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
830 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
831 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
832 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
833 case VAR_LIST: isntype = ISN_COMPARELIST; break;
834 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
835 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 default: isntype = ISN_COMPAREANY; break;
837 }
838 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200839 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100841 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842 isntype = ISN_COMPAREANY;
843
Bram Moolenaar657137c2021-01-09 15:45:23 +0100844 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 && (isntype == ISN_COMPAREBOOL
846 || isntype == ISN_COMPARESPECIAL
847 || isntype == ISN_COMPARENR
848 || isntype == ISN_COMPAREFLOAT))
849 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200850 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100851 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200852 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 }
854 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100855 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
857 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100858 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
859 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 && (type1 == VAR_BLOB || type2 == VAR_BLOB
861 || type1 == VAR_LIST || type2 == VAR_LIST))))
862 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200863 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200865 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200867 return isntype;
868}
869
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200870 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100871check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200872{
873 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
874 return FAIL;
875 return OK;
876}
877
Bram Moolenaara5565e42020-05-09 15:44:01 +0200878/*
879 * Generate an ISN_COMPARE* instruction with a boolean result.
880 */
881 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100882generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200883{
884 isntype_T isntype;
885 isn_T *isn;
886 garray_T *stack = &cctx->ctx_type_stack;
887 vartype_T type1;
888 vartype_T type2;
889
890 RETURN_OK_IF_SKIP(cctx);
891
892 // Get the known type of the two items on the stack. If they are matching
893 // use a type-specific instruction. Otherwise fall back to runtime type
894 // checking.
895 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
896 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100897 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200898 if (isntype == ISN_DROP)
899 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900
901 if ((isn = generate_instr(cctx, isntype)) == NULL)
902 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100903 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 isn->isn_arg.op.op_ic = ic;
905
906 // takes two arguments, puts one bool back
907 if (stack->ga_len >= 2)
908 {
909 --stack->ga_len;
910 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
911 }
912
913 return OK;
914}
915
916/*
917 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200918 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919 */
920 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200921generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922{
923 isn_T *isn;
924 garray_T *stack = &cctx->ctx_type_stack;
925
Bram Moolenaar080457c2020-03-03 21:53:32 +0100926 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
928 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200929 isn->isn_arg.tobool.invert = invert;
930 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931
932 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200933 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934
935 return OK;
936}
937
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200938/*
939 * Generate an ISN_COND2BOOL instruction.
940 */
941 static int
942generate_COND2BOOL(cctx_T *cctx)
943{
944 isn_T *isn;
945 garray_T *stack = &cctx->ctx_type_stack;
946
947 RETURN_OK_IF_SKIP(cctx);
948 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
949 return FAIL;
950
951 // type becomes bool
952 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
953
954 return OK;
955}
956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200958generate_TYPECHECK(
959 cctx_T *cctx,
960 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100961 int offset,
962 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963{
964 isn_T *isn;
965 garray_T *stack = &cctx->ctx_type_stack;
966
Bram Moolenaar080457c2020-03-03 21:53:32 +0100967 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
969 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200970 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100971 isn->isn_arg.type.ct_off = (int8_T)offset;
972 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973
Bram Moolenaar5e654232020-09-16 15:22:00 +0200974 // type becomes expected
975 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976
977 return OK;
978}
979
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100980 static int
981generate_SETTYPE(
982 cctx_T *cctx,
983 type_T *expected)
984{
985 isn_T *isn;
986
987 RETURN_OK_IF_SKIP(cctx);
988 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
989 return FAIL;
990 isn->isn_arg.type.ct_type = alloc_type(expected);
991 return OK;
992}
993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200995 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
996 * used. Return FALSE if the types will never match.
997 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100998 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200999use_typecheck(type_T *actual, type_T *expected)
1000{
1001 if (actual->tt_type == VAR_ANY
1002 || actual->tt_type == VAR_UNKNOWN
1003 || (actual->tt_type == VAR_FUNC
1004 && (expected->tt_type == VAR_FUNC
1005 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001006 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1007 && ((actual->tt_member == &t_void)
1008 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001009 return TRUE;
1010 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1011 && actual->tt_type == expected->tt_type)
1012 // This takes care of a nested list or dict.
1013 return use_typecheck(actual->tt_member, expected->tt_member);
1014 return FALSE;
1015}
1016
1017/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001018 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001019 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001020 * - "actual" is a type that can be "expected" type: add a runtime check; or
1021 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001022 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1023 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001024 */
Bram Moolenaar351ead02021-01-16 16:07:01 +01001025 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001026need_type(
1027 type_T *actual,
1028 type_T *expected,
1029 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001030 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001031 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001032 int silent,
1033 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001034{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001035 where_T where;
1036
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001037 if (expected == &t_bool && actual != &t_bool
1038 && (actual->tt_flags & TTFLAG_BOOL_OK))
1039 {
1040 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1041 // boolean is OK but requires a conversion.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001042 generate_2BOOL(cctx, FALSE, offset);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001043 return OK;
1044 }
1045
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001046 where.wt_index = arg_idx;
1047 where.wt_variable = FALSE;
1048 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001049 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001050
1051 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001052 // If it's a constant a runtime check makes no sense.
1053 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001054 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001055 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001056 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001057 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001058
1059 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001060 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001061 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001062}
1063
1064/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001065 * Check that the top of the type stack has a type that can be used as a
1066 * condition. Give an error and return FAIL if not.
1067 */
1068 static int
1069bool_on_stack(cctx_T *cctx)
1070{
1071 garray_T *stack = &cctx->ctx_type_stack;
1072 type_T *type;
1073
1074 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1075 if (type == &t_bool)
1076 return OK;
1077
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001078 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001079 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1080 // This requires a runtime type check.
1081 return generate_COND2BOOL(cctx);
1082
Bram Moolenaar351ead02021-01-16 16:07:01 +01001083 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001084}
1085
1086/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 * Generate an ISN_PUSHNR instruction.
1088 */
1089 static int
1090generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1091{
1092 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001093 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094
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_PUSHNR, &t_number)) == NULL)
1097 return FAIL;
1098 isn->isn_arg.number = number;
1099
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001100 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001101 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001102 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103 return OK;
1104}
1105
1106/*
1107 * Generate an ISN_PUSHBOOL instruction.
1108 */
1109 static int
1110generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1111{
1112 isn_T *isn;
1113
Bram Moolenaar080457c2020-03-03 21:53:32 +01001114 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1116 return FAIL;
1117 isn->isn_arg.number = number;
1118
1119 return OK;
1120}
1121
1122/*
1123 * Generate an ISN_PUSHSPEC instruction.
1124 */
1125 static int
1126generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1127{
1128 isn_T *isn;
1129
Bram Moolenaar080457c2020-03-03 21:53:32 +01001130 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1132 return FAIL;
1133 isn->isn_arg.number = number;
1134
1135 return OK;
1136}
1137
1138#ifdef FEAT_FLOAT
1139/*
1140 * Generate an ISN_PUSHF instruction.
1141 */
1142 static int
1143generate_PUSHF(cctx_T *cctx, float_T fnumber)
1144{
1145 isn_T *isn;
1146
Bram Moolenaar080457c2020-03-03 21:53:32 +01001147 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1149 return FAIL;
1150 isn->isn_arg.fnumber = fnumber;
1151
1152 return OK;
1153}
1154#endif
1155
1156/*
1157 * Generate an ISN_PUSHS instruction.
1158 * Consumes "str".
1159 */
1160 static int
1161generate_PUSHS(cctx_T *cctx, char_u *str)
1162{
1163 isn_T *isn;
1164
Bram Moolenaar508b5612021-01-02 18:17:26 +01001165 if (cctx->ctx_skip == SKIP_YES)
1166 {
1167 vim_free(str);
1168 return OK;
1169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1171 return FAIL;
1172 isn->isn_arg.string = str;
1173
1174 return OK;
1175}
1176
1177/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001178 * Generate an ISN_PUSHCHANNEL instruction.
1179 * Consumes "channel".
1180 */
1181 static int
1182generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1183{
1184 isn_T *isn;
1185
Bram Moolenaar080457c2020-03-03 21:53:32 +01001186 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001187 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1188 return FAIL;
1189 isn->isn_arg.channel = channel;
1190
1191 return OK;
1192}
1193
1194/*
1195 * Generate an ISN_PUSHJOB instruction.
1196 * Consumes "job".
1197 */
1198 static int
1199generate_PUSHJOB(cctx_T *cctx, job_T *job)
1200{
1201 isn_T *isn;
1202
Bram Moolenaar080457c2020-03-03 21:53:32 +01001203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001204 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001205 return FAIL;
1206 isn->isn_arg.job = job;
1207
1208 return OK;
1209}
1210
1211/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 * Generate an ISN_PUSHBLOB instruction.
1213 * Consumes "blob".
1214 */
1215 static int
1216generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1217{
1218 isn_T *isn;
1219
Bram Moolenaar080457c2020-03-03 21:53:32 +01001220 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1222 return FAIL;
1223 isn->isn_arg.blob = blob;
1224
1225 return OK;
1226}
1227
1228/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001229 * Generate an ISN_PUSHFUNC instruction with name "name".
1230 * Consumes "name".
1231 */
1232 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001233generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001234{
1235 isn_T *isn;
1236
Bram Moolenaar080457c2020-03-03 21:53:32 +01001237 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001238 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001239 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001240 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001241
1242 return OK;
1243}
1244
1245/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001246 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001247 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1248 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001249 */
1250 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001251generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001252{
1253 isn_T *isn;
1254 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001255 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1256 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001257 type_T *item_type = &t_any;
1258
1259 RETURN_OK_IF_SKIP(cctx);
1260
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001261 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001262 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001263 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001264 emsg(_(e_listreq));
1265 return FAIL;
1266 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001267 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001268 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1269 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001270 isn->isn_arg.getitem.gi_index = index;
1271 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001272
1273 // add the item type to the type stack
1274 if (ga_grow(stack, 1) == FAIL)
1275 return FAIL;
1276 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1277 ++stack->ga_len;
1278 return OK;
1279}
1280
1281/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001282 * Generate an ISN_SLICE instruction with "count".
1283 */
1284 static int
1285generate_SLICE(cctx_T *cctx, int count)
1286{
1287 isn_T *isn;
1288
1289 RETURN_OK_IF_SKIP(cctx);
1290 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1291 return FAIL;
1292 isn->isn_arg.number = count;
1293 return OK;
1294}
1295
1296/*
1297 * Generate an ISN_CHECKLEN instruction with "min_len".
1298 */
1299 static int
1300generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1301{
1302 isn_T *isn;
1303
1304 RETURN_OK_IF_SKIP(cctx);
1305
1306 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1307 return FAIL;
1308 isn->isn_arg.checklen.cl_min_len = min_len;
1309 isn->isn_arg.checklen.cl_more_OK = more_OK;
1310
1311 return OK;
1312}
1313
1314/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 * Generate an ISN_STORE instruction.
1316 */
1317 static int
1318generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1319{
1320 isn_T *isn;
1321
Bram Moolenaar080457c2020-03-03 21:53:32 +01001322 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1324 return FAIL;
1325 if (name != NULL)
1326 isn->isn_arg.string = vim_strsave(name);
1327 else
1328 isn->isn_arg.number = idx;
1329
1330 return OK;
1331}
1332
1333/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001334 * Generate an ISN_STOREOUTER instruction.
1335 */
1336 static int
1337generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1338{
1339 isn_T *isn;
1340
1341 RETURN_OK_IF_SKIP(cctx);
1342 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1343 return FAIL;
1344 isn->isn_arg.outer.outer_idx = idx;
1345 isn->isn_arg.outer.outer_depth = level;
1346
1347 return OK;
1348}
1349
1350/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1352 */
1353 static int
1354generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1355{
1356 isn_T *isn;
1357
Bram Moolenaar080457c2020-03-03 21:53:32 +01001358 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1360 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001361 isn->isn_arg.storenr.stnr_idx = idx;
1362 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363
1364 return OK;
1365}
1366
1367/*
1368 * Generate an ISN_STOREOPT instruction
1369 */
1370 static int
1371generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1372{
1373 isn_T *isn;
1374
Bram Moolenaar080457c2020-03-03 21:53:32 +01001375 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001376 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 return FAIL;
1378 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1379 isn->isn_arg.storeopt.so_flags = opt_flags;
1380
1381 return OK;
1382}
1383
1384/*
1385 * Generate an ISN_LOAD or similar instruction.
1386 */
1387 static int
1388generate_LOAD(
1389 cctx_T *cctx,
1390 isntype_T isn_type,
1391 int idx,
1392 char_u *name,
1393 type_T *type)
1394{
1395 isn_T *isn;
1396
Bram Moolenaar080457c2020-03-03 21:53:32 +01001397 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1399 return FAIL;
1400 if (name != NULL)
1401 isn->isn_arg.string = vim_strsave(name);
1402 else
1403 isn->isn_arg.number = idx;
1404
1405 return OK;
1406}
1407
1408/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001409 * Generate an ISN_LOADOUTER instruction
1410 */
1411 static int
1412generate_LOADOUTER(
1413 cctx_T *cctx,
1414 int idx,
1415 int nesting,
1416 type_T *type)
1417{
1418 isn_T *isn;
1419
1420 RETURN_OK_IF_SKIP(cctx);
1421 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1422 return FAIL;
1423 isn->isn_arg.outer.outer_idx = idx;
1424 isn->isn_arg.outer.outer_depth = nesting;
1425
1426 return OK;
1427}
1428
1429/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001430 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001431 */
1432 static int
1433generate_LOADV(
1434 cctx_T *cctx,
1435 char_u *name,
1436 int error)
1437{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001438 int di_flags;
1439 int vidx = find_vim_var(name, &di_flags);
1440 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001441
Bram Moolenaar080457c2020-03-03 21:53:32 +01001442 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001443 if (vidx < 0)
1444 {
1445 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001446 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001447 return FAIL;
1448 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001449 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001450
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001451 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001452}
1453
1454/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001455 * Generate an ISN_UNLET instruction.
1456 */
1457 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001458generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001459{
1460 isn_T *isn;
1461
1462 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001463 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001464 return FAIL;
1465 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1466 isn->isn_arg.unlet.ul_forceit = forceit;
1467
1468 return OK;
1469}
1470
1471/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001472 * Generate an ISN_LOCKCONST instruction.
1473 */
1474 static int
1475generate_LOCKCONST(cctx_T *cctx)
1476{
1477 isn_T *isn;
1478
1479 RETURN_OK_IF_SKIP(cctx);
1480 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1481 return FAIL;
1482 return OK;
1483}
1484
1485/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 * Generate an ISN_LOADS instruction.
1487 */
1488 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001489generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001491 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001493 int sid,
1494 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495{
1496 isn_T *isn;
1497
Bram Moolenaar080457c2020-03-03 21:53:32 +01001498 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001499 if (isn_type == ISN_LOADS)
1500 isn = generate_instr_type(cctx, isn_type, type);
1501 else
1502 isn = generate_instr_drop(cctx, isn_type, 1);
1503 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001505 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1506 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507
1508 return OK;
1509}
1510
1511/*
1512 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1513 */
1514 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001515generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516 cctx_T *cctx,
1517 isntype_T isn_type,
1518 int sid,
1519 int idx,
1520 type_T *type)
1521{
1522 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001523 scriptref_T *sref;
1524 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525
Bram Moolenaar080457c2020-03-03 21:53:32 +01001526 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 if (isn_type == ISN_LOADSCRIPT)
1528 isn = generate_instr_type(cctx, isn_type, type);
1529 else
1530 isn = generate_instr_drop(cctx, isn_type, 1);
1531 if (isn == NULL)
1532 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001533
1534 // This requires three arguments, which doesn't fit in an instruction, thus
1535 // we need to allocate a struct for this.
1536 sref = ALLOC_ONE(scriptref_T);
1537 if (sref == NULL)
1538 return FAIL;
1539 isn->isn_arg.script.scriptref = sref;
1540 sref->sref_sid = sid;
1541 sref->sref_idx = idx;
1542 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001543 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 return OK;
1545}
1546
1547/*
1548 * Generate an ISN_NEWLIST instruction.
1549 */
1550 static int
1551generate_NEWLIST(cctx_T *cctx, int count)
1552{
1553 isn_T *isn;
1554 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 type_T *type;
1556 type_T *member;
1557
Bram Moolenaar080457c2020-03-03 21:53:32 +01001558 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1560 return FAIL;
1561 isn->isn_arg.number = count;
1562
Bram Moolenaar127542b2020-08-09 17:22:04 +02001563 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001564 if (count == 0)
1565 member = &t_void;
1566 else
1567 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001568 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1569 cctx->ctx_type_list);
1570 type = get_list_type(member, cctx->ctx_type_list);
1571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 // drop the value types
1573 stack->ga_len -= count;
1574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 // add the list type to the type stack
1576 if (ga_grow(stack, 1) == FAIL)
1577 return FAIL;
1578 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1579 ++stack->ga_len;
1580
1581 return OK;
1582}
1583
1584/*
1585 * Generate an ISN_NEWDICT instruction.
1586 */
1587 static int
1588generate_NEWDICT(cctx_T *cctx, int count)
1589{
1590 isn_T *isn;
1591 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 type_T *type;
1593 type_T *member;
1594
Bram Moolenaar080457c2020-03-03 21:53:32 +01001595 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1597 return FAIL;
1598 isn->isn_arg.number = count;
1599
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001600 if (count == 0)
1601 member = &t_void;
1602 else
1603 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001604 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1605 cctx->ctx_type_list);
1606 type = get_dict_type(member, cctx->ctx_type_list);
1607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 // drop the key and value types
1609 stack->ga_len -= 2 * count;
1610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 // add the dict type to the type stack
1612 if (ga_grow(stack, 1) == FAIL)
1613 return FAIL;
1614 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1615 ++stack->ga_len;
1616
1617 return OK;
1618}
1619
1620/*
1621 * Generate an ISN_FUNCREF instruction.
1622 */
1623 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001624generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625{
1626 isn_T *isn;
1627 garray_T *stack = &cctx->ctx_type_stack;
1628
Bram Moolenaar080457c2020-03-03 21:53:32 +01001629 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1631 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001632 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001633 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001635 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001636 // the nested context, including this one.
1637 if (ufunc->uf_flags & FC_CLOSURE)
1638 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 if (ga_grow(stack, 1) == FAIL)
1641 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001642 ((type_T **)stack->ga_data)[stack->ga_len] =
1643 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 ++stack->ga_len;
1645
1646 return OK;
1647}
1648
1649/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001650 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001651 * "lambda_name" and "func_name" must be in allocated memory and will be
1652 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001653 */
1654 static int
1655generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1656{
1657 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001658
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001659 if (cctx->ctx_skip == SKIP_YES)
1660 {
1661 vim_free(lambda_name);
1662 vim_free(func_name);
1663 return OK;
1664 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001665 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001666 {
1667 vim_free(lambda_name);
1668 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001669 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001670 }
1671 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001672 isn->isn_arg.newfunc.nf_global = func_name;
1673
1674 return OK;
1675}
1676
1677/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001678 * Generate an ISN_DEF instruction: list functions
1679 */
1680 static int
1681generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1682{
1683 isn_T *isn;
1684
1685 RETURN_OK_IF_SKIP(cctx);
1686 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1687 return FAIL;
1688 if (len > 0)
1689 {
1690 isn->isn_arg.string = vim_strnsave(name, len);
1691 if (isn->isn_arg.string == NULL)
1692 return FAIL;
1693 }
1694 return OK;
1695}
1696
1697/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 * Generate an ISN_JUMP instruction.
1699 */
1700 static int
1701generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1702{
1703 isn_T *isn;
1704 garray_T *stack = &cctx->ctx_type_stack;
1705
Bram Moolenaar080457c2020-03-03 21:53:32 +01001706 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1708 return FAIL;
1709 isn->isn_arg.jump.jump_when = when;
1710 isn->isn_arg.jump.jump_where = where;
1711
1712 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1713 --stack->ga_len;
1714
1715 return OK;
1716}
1717
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001718/*
1719 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1720 */
1721 static int
1722generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1723{
1724 isn_T *isn;
1725
1726 RETURN_OK_IF_SKIP(cctx);
1727 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1728 return FAIL;
1729 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1730 // jump_where is set later
1731 return OK;
1732}
1733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 static int
1735generate_FOR(cctx_T *cctx, int loop_idx)
1736{
1737 isn_T *isn;
1738 garray_T *stack = &cctx->ctx_type_stack;
1739
Bram Moolenaar080457c2020-03-03 21:53:32 +01001740 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1742 return FAIL;
1743 isn->isn_arg.forloop.for_idx = loop_idx;
1744
1745 if (ga_grow(stack, 1) == FAIL)
1746 return FAIL;
1747 // type doesn't matter, will be stored next
1748 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1749 ++stack->ga_len;
1750
1751 return OK;
1752}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001753/*
1754 * Generate an ISN_TRYCONT instruction.
1755 */
1756 static int
1757generate_TRYCONT(cctx_T *cctx, int levels, int where)
1758{
1759 isn_T *isn;
1760
1761 RETURN_OK_IF_SKIP(cctx);
1762 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1763 return FAIL;
1764 isn->isn_arg.trycont.tct_levels = levels;
1765 isn->isn_arg.trycont.tct_where = where;
1766
1767 return OK;
1768}
1769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770
1771/*
1772 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001773 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 * Return FAIL if the number of arguments is wrong.
1775 */
1776 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001777generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778{
1779 isn_T *isn;
1780 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001781 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001782 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001783 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784
Bram Moolenaar080457c2020-03-03 21:53:32 +01001785 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001786 argoff = check_internal_func(func_idx, argcount);
1787 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 return FAIL;
1789
Bram Moolenaar389df252020-07-09 21:20:47 +02001790 if (method_call && argoff > 1)
1791 {
1792 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1793 return FAIL;
1794 isn->isn_arg.shuffle.shfl_item = argcount;
1795 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1796 }
1797
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001798 if (argcount > 0)
1799 {
1800 // Check the types of the arguments.
1801 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001802 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1803 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001804 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001805 if (internal_func_is_map(func_idx))
1806 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001807 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1810 return FAIL;
1811 isn->isn_arg.bfunc.cbf_idx = func_idx;
1812 isn->isn_arg.bfunc.cbf_argcount = argcount;
1813
Bram Moolenaar94738d82020-10-21 14:25:07 +02001814 // Drop the argument types and push the return type.
1815 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 if (ga_grow(stack, 1) == FAIL)
1817 return FAIL;
1818 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001819 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001820 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001822 if (maptype != NULL && maptype->tt_member != NULL
1823 && maptype->tt_member != &t_any)
1824 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001825 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 return OK;
1828}
1829
1830/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001831 * Generate an ISN_LISTAPPEND instruction. Works like add().
1832 * Argument count is already checked.
1833 */
1834 static int
1835generate_LISTAPPEND(cctx_T *cctx)
1836{
1837 garray_T *stack = &cctx->ctx_type_stack;
1838 type_T *list_type;
1839 type_T *item_type;
1840 type_T *expected;
1841
1842 // Caller already checked that list_type is a list.
1843 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1844 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1845 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001846 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001847 return FAIL;
1848
1849 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1850 return FAIL;
1851
1852 --stack->ga_len; // drop the argument
1853 return OK;
1854}
1855
1856/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001857 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1858 * Argument count is already checked.
1859 */
1860 static int
1861generate_BLOBAPPEND(cctx_T *cctx)
1862{
1863 garray_T *stack = &cctx->ctx_type_stack;
1864 type_T *item_type;
1865
1866 // Caller already checked that blob_type is a blob.
1867 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001868 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001869 return FAIL;
1870
1871 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1872 return FAIL;
1873
1874 --stack->ga_len; // drop the argument
1875 return OK;
1876}
1877
1878/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001879 * Return TRUE if "ufunc" should be compiled, taking into account whether
1880 * "profile" indicates profiling is to be done.
1881 */
1882 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001883func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001884{
1885 switch (ufunc->uf_def_status)
1886 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001887 case UF_TO_BE_COMPILED:
1888 return TRUE;
1889
Bram Moolenaarb2049902021-01-24 12:53:53 +01001890 case UF_COMPILED:
1891 {
1892 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1893 + ufunc->uf_dfunc_idx;
1894
Bram Moolenaare99d4222021-06-13 14:01:26 +02001895 switch (compile_type)
1896 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001897 case CT_PROFILE:
1898#ifdef FEAT_PROFILE
1899 return dfunc->df_instr_prof == NULL;
1900#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001901 case CT_NONE:
1902 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001903 case CT_DEBUG:
1904 return dfunc->df_instr_debug == NULL;
1905 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001906 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001907
1908 case UF_NOT_COMPILED:
1909 case UF_COMPILE_ERROR:
1910 case UF_COMPILING:
1911 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001912 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001913 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001914}
1915
1916/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 * Generate an ISN_DCALL or ISN_UCALL instruction.
1918 * Return FAIL if the number of arguments is wrong.
1919 */
1920 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001921generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922{
1923 isn_T *isn;
1924 garray_T *stack = &cctx->ctx_type_stack;
1925 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001926 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927
Bram Moolenaar080457c2020-03-03 21:53:32 +01001928 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 if (argcount > regular_args && !has_varargs(ufunc))
1930 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001931 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 return FAIL;
1933 }
1934 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1935 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001936 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 return FAIL;
1938 }
1939
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001940 if (ufunc->uf_def_status != UF_NOT_COMPILED
1941 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001942 {
1943 int i;
1944
1945 for (i = 0; i < argcount; ++i)
1946 {
1947 type_T *expected;
1948 type_T *actual;
1949
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001950 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1951 if (actual == &t_special
1952 && i >= regular_args - ufunc->uf_def_args.ga_len)
1953 {
1954 // assume v:none used for default argument value
1955 continue;
1956 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001957 if (i < regular_args)
1958 {
1959 if (ufunc->uf_arg_types == NULL)
1960 continue;
1961 expected = ufunc->uf_arg_types[i];
1962 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001963 else if (ufunc->uf_va_type == NULL
1964 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001965 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001966 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001967 else
1968 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001969 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001970 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001971 {
1972 arg_type_mismatch(expected, actual, i + 1);
1973 return FAIL;
1974 }
1975 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02001976 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001977 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02001978 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001979 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001980 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02001981 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1982 {
1983 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1984 ufunc->uf_name);
1985 return FAIL;
1986 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001989 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001990 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001992 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 {
1994 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1995 isn->isn_arg.dfunc.cdf_argcount = argcount;
1996 }
1997 else
1998 {
1999 // A user function may be deleted and redefined later, can't use the
2000 // ufunc pointer, need to look it up again at runtime.
2001 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2002 isn->isn_arg.ufunc.cuf_argcount = argcount;
2003 }
2004
2005 stack->ga_len -= argcount; // drop the arguments
2006 if (ga_grow(stack, 1) == FAIL)
2007 return FAIL;
2008 // add return value
2009 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2010 ++stack->ga_len;
2011
2012 return OK;
2013}
2014
2015/*
2016 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2017 */
2018 static int
2019generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2020{
2021 isn_T *isn;
2022 garray_T *stack = &cctx->ctx_type_stack;
2023
Bram Moolenaar080457c2020-03-03 21:53:32 +01002024 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2026 return FAIL;
2027 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2028 isn->isn_arg.ufunc.cuf_argcount = argcount;
2029
2030 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002031 if (ga_grow(stack, 1) == FAIL)
2032 return FAIL;
2033 // add return value
2034 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2035 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036
2037 return OK;
2038}
2039
2040/*
2041 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002042 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 */
2044 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002045generate_PCALL(
2046 cctx_T *cctx,
2047 int argcount,
2048 char_u *name,
2049 type_T *type,
2050 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051{
2052 isn_T *isn;
2053 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002054 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055
Bram Moolenaar080457c2020-03-03 21:53:32 +01002056 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002057
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002058 if (type->tt_type == VAR_ANY)
2059 ret_type = &t_any;
2060 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002061 {
2062 if (type->tt_argcount != -1)
2063 {
2064 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2065
2066 if (argcount < type->tt_min_argcount - varargs)
2067 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002068 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002069 return FAIL;
2070 }
2071 if (!varargs && argcount > type->tt_argcount)
2072 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002073 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002074 return FAIL;
2075 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002076 if (type->tt_args != NULL)
2077 {
2078 int i;
2079
2080 for (i = 0; i < argcount; ++i)
2081 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002082 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002083 type_T *actual = ((type_T **)stack->ga_data)[
2084 stack->ga_len + offset];
2085 type_T *expected;
2086
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002087 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002088 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002089 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002090 else if (i >= type->tt_min_argcount
2091 && actual == &t_special)
2092 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002093 else
2094 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002095 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002096 cctx, TRUE, FALSE) == FAIL)
2097 {
2098 arg_type_mismatch(expected, actual, i + 1);
2099 return FAIL;
2100 }
2101 }
2102 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002103 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002104 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002105 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002106 else
2107 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002108 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002109 return FAIL;
2110 }
2111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2113 return FAIL;
2114 isn->isn_arg.pfunc.cpf_top = at_top;
2115 isn->isn_arg.pfunc.cpf_argcount = argcount;
2116
2117 stack->ga_len -= argcount; // drop the arguments
2118
2119 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002120 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002122 // If partial is above the arguments it must be cleared and replaced with
2123 // the return value.
2124 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2125 return FAIL;
2126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 return OK;
2128}
2129
2130/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002131 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 */
2133 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002134generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135{
2136 isn_T *isn;
2137 garray_T *stack = &cctx->ctx_type_stack;
2138 type_T *type;
2139
Bram Moolenaar080457c2020-03-03 21:53:32 +01002140 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002141 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002143 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002145 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002147 if (type->tt_type != VAR_DICT && type != &t_any)
2148 {
2149 emsg(_(e_dictreq));
2150 return FAIL;
2151 }
2152 // change dict type to dict member type
2153 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002154 {
2155 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2156 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158
2159 return OK;
2160}
2161
2162/*
2163 * Generate an ISN_ECHO instruction.
2164 */
2165 static int
2166generate_ECHO(cctx_T *cctx, int with_white, int count)
2167{
2168 isn_T *isn;
2169
Bram Moolenaar080457c2020-03-03 21:53:32 +01002170 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2172 return FAIL;
2173 isn->isn_arg.echo.echo_with_white = with_white;
2174 isn->isn_arg.echo.echo_count = count;
2175
2176 return OK;
2177}
2178
Bram Moolenaarad39c092020-02-26 18:23:43 +01002179/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002180 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002181 */
2182 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002183generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002184{
2185 isn_T *isn;
2186
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002187 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002188 return FAIL;
2189 isn->isn_arg.number = count;
2190
2191 return OK;
2192}
2193
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002194/*
2195 * Generate an ISN_PUT instruction.
2196 */
2197 static int
2198generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2199{
2200 isn_T *isn;
2201
2202 RETURN_OK_IF_SKIP(cctx);
2203 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2204 return FAIL;
2205 isn->isn_arg.put.put_regname = regname;
2206 isn->isn_arg.put.put_lnum = lnum;
2207 return OK;
2208}
2209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 static int
2211generate_EXEC(cctx_T *cctx, char_u *line)
2212{
2213 isn_T *isn;
2214
Bram Moolenaar080457c2020-03-03 21:53:32 +01002215 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2217 return FAIL;
2218 isn->isn_arg.string = vim_strsave(line);
2219 return OK;
2220}
2221
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002222 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002223generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2224{
2225 isn_T *isn;
2226 garray_T *stack = &cctx->ctx_type_stack;
2227
2228 RETURN_OK_IF_SKIP(cctx);
2229 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2230 return FAIL;
2231 isn->isn_arg.string = vim_strsave(line);
2232
2233 if (ga_grow(stack, 1) == FAIL)
2234 return FAIL;
2235 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2236 ++stack->ga_len;
2237
2238 return OK;
2239}
2240
2241 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002242generate_EXECCONCAT(cctx_T *cctx, int count)
2243{
2244 isn_T *isn;
2245
2246 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2247 return FAIL;
2248 isn->isn_arg.number = count;
2249 return OK;
2250}
2251
Bram Moolenaar08597872020-12-10 19:43:40 +01002252/*
2253 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2254 */
2255 static int
2256generate_RANGE(cctx_T *cctx, char_u *range)
2257{
2258 isn_T *isn;
2259 garray_T *stack = &cctx->ctx_type_stack;
2260
2261 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2262 return FAIL;
2263 isn->isn_arg.string = range;
2264
2265 if (ga_grow(stack, 1) == FAIL)
2266 return FAIL;
2267 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2268 ++stack->ga_len;
2269 return OK;
2270}
2271
Bram Moolenaar792f7862020-11-23 08:31:18 +01002272 static int
2273generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2274{
2275 isn_T *isn;
2276
2277 RETURN_OK_IF_SKIP(cctx);
2278 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2279 return FAIL;
2280 isn->isn_arg.unpack.unp_count = var_count;
2281 isn->isn_arg.unpack.unp_semicolon = semicolon;
2282 return OK;
2283}
2284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002286 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002287 */
2288 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002289generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002290{
2291 isn_T *isn;
2292
Bram Moolenaarfa984412021-03-25 22:15:28 +01002293 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002294 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002295 cctx->ctx_has_cmdmod = TRUE;
2296
2297 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002298 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002299 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2300 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2301 return FAIL;
2302 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002303 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002304 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002305 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002306
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002307 return OK;
2308}
2309
2310 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002311generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002312{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002313 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2314 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002315 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002316 return OK;
2317}
2318
Bram Moolenaarfa984412021-03-25 22:15:28 +01002319 static int
2320misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002321{
2322 garray_T *instr = &cctx->ctx_instr;
2323
Bram Moolenaara91a7132021-03-25 21:12:15 +01002324 if (cctx->ctx_has_cmdmod
2325 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2326 == ISN_CMDMOD)
2327 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002328 emsg(_(e_misplaced_command_modifier));
2329 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002330 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002331 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002332}
2333
2334/*
2335 * Get the index of the current instruction.
2336 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2337 */
2338 static int
2339current_instr_idx(cctx_T *cctx)
2340{
2341 garray_T *instr = &cctx->ctx_instr;
2342 int idx = instr->ga_len;
2343
Bram Moolenaare99d4222021-06-13 14:01:26 +02002344 while (idx > 0)
2345 {
2346 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002347 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002348 {
2349 --idx;
2350 continue;
2351 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002352#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002353 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2354 {
2355 --idx;
2356 continue;
2357 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002358#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002359 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2360 {
2361 --idx;
2362 continue;
2363 }
2364 break;
2365 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002366 return idx;
2367}
2368
Bram Moolenaarf002a412021-01-24 13:34:18 +01002369#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002370 static void
2371may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2372{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002373 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002374 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002375}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002376#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002377
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002378/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002380 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002382 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002383reserve_local(
2384 cctx_T *cctx,
2385 char_u *name,
2386 size_t len,
2387 int isConst,
2388 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002391 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002393 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002395 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002396 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 }
2398
2399 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002400 return NULL;
2401 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002402 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002404 // Every local variable uses the next entry on the stack. We could re-use
2405 // the last ones when leaving a scope, but then variables used in a closure
2406 // might get overwritten. To keep things simple do not re-use stack
2407 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002408 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2409 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002410
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002411 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 lvar->lv_const = isConst;
2413 lvar->lv_type = type;
2414
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002415 // Remember the name for debugging.
2416 if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2417 return NULL;
2418 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2419 vim_strsave(lvar->lv_name);
2420 ++dfunc->df_var_names.ga_len;
2421
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002422 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423}
2424
2425/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002426 * Remove local variables above "new_top".
2427 */
2428 static void
2429unwind_locals(cctx_T *cctx, int new_top)
2430{
2431 if (cctx->ctx_locals.ga_len > new_top)
2432 {
2433 int idx;
2434 lvar_T *lvar;
2435
2436 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2437 {
2438 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2439 vim_free(lvar->lv_name);
2440 }
2441 }
2442 cctx->ctx_locals.ga_len = new_top;
2443}
2444
2445/*
2446 * Free all local variables.
2447 */
2448 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002449free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002450{
2451 unwind_locals(cctx, 0);
2452 ga_clear(&cctx->ctx_locals);
2453}
2454
2455/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002456 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2457 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2458 * error if the variable was defined with :const.
2459 */
2460 static int
2461check_item_writable(svar_T *sv, int check_writable, char_u *name)
2462{
2463 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2464 || (check_writable == ASSIGN_FINAL
2465 && sv->sv_const == ASSIGN_CONST))
2466 {
2467 semsg(_(e_readonlyvar), name);
2468 return FAIL;
2469 }
2470 return OK;
2471}
2472
2473/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002475 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 * Returns the index in "sn_var_vals" if found.
2477 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002478 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 */
2480 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002481get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482{
2483 hashtab_T *ht;
2484 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002485 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002486 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 int idx;
2488
Bram Moolenaare3d46852020-08-29 13:39:17 +02002489 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002491 if (sid == current_sctx.sc_sid)
2492 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002493 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002494
2495 if (sav == NULL)
2496 return -2;
2497 idx = sav->sav_var_vals_idx;
2498 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002499 if (check_item_writable(sv, check_writable, name) == FAIL)
2500 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002501 return idx;
2502 }
2503
2504 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 ht = &SCRIPT_VARS(sid);
2506 di = find_var_in_ht(ht, 0, name, TRUE);
2507 if (di == NULL)
2508 return -2;
2509
2510 // Now find the svar_T index in sn_var_vals.
2511 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2512 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002513 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 if (sv->sv_tv == &di->di_tv)
2515 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002516 if (check_item_writable(sv, check_writable, name) == FAIL)
2517 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 return idx;
2519 }
2520 }
2521 return -1;
2522}
2523
2524/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002525 * Find "name" in imported items of the current script or in "cctx" if not
2526 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 */
2528 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002529find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 int idx;
2532
Bram Moolenaare3d46852020-08-29 13:39:17 +02002533 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002534 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 if (cctx != NULL)
2536 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2537 {
2538 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2539 + idx;
2540
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002541 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2542 : STRLEN(import->imp_name) == len
2543 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 return import;
2545 }
2546
Bram Moolenaarefa94442020-08-08 22:16:00 +02002547 return find_imported_in_script(name, len, current_sctx.sc_sid);
2548}
2549
2550 imported_T *
2551find_imported_in_script(char_u *name, size_t len, int sid)
2552{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002553 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002554 int idx;
2555
Bram Moolenaare3d46852020-08-29 13:39:17 +02002556 if (!SCRIPT_ID_VALID(sid))
2557 return NULL;
2558 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2560 {
2561 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2562
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002563 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2564 : STRLEN(import->imp_name) == len
2565 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 return import;
2567 }
2568 return NULL;
2569}
2570
2571/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002572 * Free all imported variables.
2573 */
2574 static void
2575free_imported(cctx_T *cctx)
2576{
2577 int idx;
2578
2579 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2580 {
2581 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2582
2583 vim_free(import->imp_name);
2584 }
2585 ga_clear(&cctx->ctx_imports);
2586}
2587
2588/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002589 * Return a pointer to the next line that isn't empty or only contains a
2590 * comment. Skips over white space.
2591 * Returns NULL if there is none.
2592 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002593 char_u *
2594peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002595{
2596 int lnum = cctx->ctx_lnum;
2597
2598 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2599 {
2600 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002601 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002602
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002603 // ignore NULLs inserted for continuation lines
2604 if (line != NULL)
2605 {
2606 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002607 if (vim9_bad_comment(p))
2608 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002609 if (*p != NUL && !vim9_comment_start(p))
2610 return p;
2611 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002612 }
2613 return NULL;
2614}
2615
2616/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002617 * Called when checking for a following operator at "arg". When the rest of
2618 * the line is empty or only a comment, peek the next line. If there is a next
2619 * line return a pointer to it and set "nextp".
2620 * Otherwise skip over white space.
2621 */
2622 static char_u *
2623may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2624{
2625 char_u *p = skipwhite(arg);
2626
2627 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002628 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002629 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002630 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002631 if (*nextp != NULL)
2632 return *nextp;
2633 }
2634 return p;
2635}
2636
2637/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002638 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002639 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002640 * Returns NULL when at the end.
2641 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002642 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002643next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002644{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002645 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002646
2647 do
2648 {
2649 ++cctx->ctx_lnum;
2650 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002651 {
2652 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002653 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002654 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002655 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002656 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002657 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002658 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002659 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002660 return line;
2661}
2662
2663/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002664 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002665 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002666 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002667 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2668 */
2669 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002670may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002671{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002672 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002673 if (vim9_bad_comment(*arg))
2674 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002675 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002676 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002677 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002678
2679 if (next == NULL)
2680 return FAIL;
2681 *arg = skipwhite(next);
2682 }
2683 return OK;
2684}
2685
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002686/*
2687 * Idem, and give an error when failed.
2688 */
2689 static int
2690may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2691{
2692 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2693 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002694 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002695 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002696 return FAIL;
2697 }
2698 return OK;
2699}
2700
2701
Bram Moolenaara5565e42020-05-09 15:44:01 +02002702// Structure passed between the compile_expr* functions to keep track of
2703// constants that have been parsed but for which no code was produced yet. If
2704// possible expressions on these constants are applied at compile time. If
2705// that is not possible, the code to push the constants needs to be generated
2706// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002707// Using 50 should be more than enough of 5 levels of ().
2708#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002709typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002710 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002711 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002712 int pp_is_const; // all generated code was constants, used for a
2713 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002714} ppconst_T;
2715
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002716static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002717static int compile_expr0(char_u **arg, cctx_T *cctx);
2718static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2719
Bram Moolenaara5565e42020-05-09 15:44:01 +02002720/*
2721 * Generate a PUSH instruction for "tv".
2722 * "tv" will be consumed or cleared.
2723 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2724 */
2725 static int
2726generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2727{
2728 if (tv != NULL)
2729 {
2730 switch (tv->v_type)
2731 {
2732 case VAR_UNKNOWN:
2733 break;
2734 case VAR_BOOL:
2735 generate_PUSHBOOL(cctx, tv->vval.v_number);
2736 break;
2737 case VAR_SPECIAL:
2738 generate_PUSHSPEC(cctx, tv->vval.v_number);
2739 break;
2740 case VAR_NUMBER:
2741 generate_PUSHNR(cctx, tv->vval.v_number);
2742 break;
2743#ifdef FEAT_FLOAT
2744 case VAR_FLOAT:
2745 generate_PUSHF(cctx, tv->vval.v_float);
2746 break;
2747#endif
2748 case VAR_BLOB:
2749 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2750 tv->vval.v_blob = NULL;
2751 break;
2752 case VAR_STRING:
2753 generate_PUSHS(cctx, tv->vval.v_string);
2754 tv->vval.v_string = NULL;
2755 break;
2756 default:
2757 iemsg("constant type not supported");
2758 clear_tv(tv);
2759 return FAIL;
2760 }
2761 tv->v_type = VAR_UNKNOWN;
2762 }
2763 return OK;
2764}
2765
2766/*
2767 * Generate code for any ppconst entries.
2768 */
2769 static int
2770generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2771{
2772 int i;
2773 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002774 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002775
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002776 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002777 for (i = 0; i < ppconst->pp_used; ++i)
2778 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2779 ret = FAIL;
2780 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002781 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002782 return ret;
2783}
2784
2785/*
2786 * Clear ppconst constants. Used when failing.
2787 */
2788 static void
2789clear_ppconst(ppconst_T *ppconst)
2790{
2791 int i;
2792
2793 for (i = 0; i < ppconst->pp_used; ++i)
2794 clear_tv(&ppconst->pp_tv[i]);
2795 ppconst->pp_used = 0;
2796}
2797
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002798/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002799 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002800 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002801 */
2802 static int
2803compile_member(int is_slice, cctx_T *cctx)
2804{
2805 type_T **typep;
2806 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002807 vartype_T vartype;
2808 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002809
Bram Moolenaar261417b2021-05-06 21:04:55 +02002810 // We can index a list, dict and blob. If we don't know the type
2811 // we can use the index value type. If we still don't know use an "ANY"
2812 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002813 typep = ((type_T **)stack->ga_data) + stack->ga_len
2814 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002815 vartype = (*typep)->tt_type;
2816 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002817 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002818 if (*typep == &t_any && idxtype == &t_string)
2819 vartype = VAR_DICT;
2820 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002821 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002822 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002823 return FAIL;
2824 if (is_slice)
2825 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002826 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2827 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002828 FALSE, FALSE) == FAIL)
2829 return FAIL;
2830 }
2831 }
2832
Bram Moolenaar261417b2021-05-06 21:04:55 +02002833 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002834 {
2835 if (is_slice)
2836 {
2837 emsg(_(e_cannot_slice_dictionary));
2838 return FAIL;
2839 }
2840 if ((*typep)->tt_type == VAR_DICT)
2841 {
2842 *typep = (*typep)->tt_member;
2843 if (*typep == &t_unknown)
2844 // empty dict was used
2845 *typep = &t_any;
2846 }
2847 else
2848 {
2849 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2850 FALSE, FALSE) == FAIL)
2851 return FAIL;
2852 *typep = &t_any;
2853 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002854 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002855 return FAIL;
2856 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2857 return FAIL;
2858 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002859 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002860 {
2861 *typep = &t_string;
2862 if ((is_slice
2863 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2864 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2865 return FAIL;
2866 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002867 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002868 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002869 if (is_slice)
2870 {
2871 *typep = &t_blob;
2872 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2873 return FAIL;
2874 }
2875 else
2876 {
2877 *typep = &t_number;
2878 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2879 return FAIL;
2880 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002881 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002882 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002883 {
2884 if (is_slice)
2885 {
2886 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002887 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002888 2) == FAIL)
2889 return FAIL;
2890 }
2891 else
2892 {
2893 if ((*typep)->tt_type == VAR_LIST)
2894 {
2895 *typep = (*typep)->tt_member;
2896 if (*typep == &t_unknown)
2897 // empty list was used
2898 *typep = &t_any;
2899 }
2900 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002901 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2902 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002903 return FAIL;
2904 }
2905 }
2906 else
2907 {
2908 emsg(_(e_string_list_dict_or_blob_required));
2909 return FAIL;
2910 }
2911 return OK;
2912}
2913
2914/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002915 * Generate an instruction to load script-local variable "name", without the
2916 * leading "s:".
2917 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 */
2919 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002920compile_load_scriptvar(
2921 cctx_T *cctx,
2922 char_u *name, // variable NUL terminated
2923 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002924 char_u **end, // end of variable
2925 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002927 scriptitem_T *si;
2928 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 imported_T *import;
2930
Bram Moolenaare3d46852020-08-29 13:39:17 +02002931 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2932 return FAIL;
2933 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002934 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002935 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002937 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002938 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2939 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 }
2941 if (idx >= 0)
2942 {
2943 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2944
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002945 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 current_sctx.sc_sid, idx, sv->sv_type);
2947 return OK;
2948 }
2949
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002950 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 if (import != NULL)
2952 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002953 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002954 {
2955 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002956 char_u *exp_name;
2957 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002958 ufunc_T *ufunc;
2959 type_T *type;
2960
2961 // Used "import * as Name", need to lookup the member.
2962 if (*p != '.')
2963 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002964 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002965 return FAIL;
2966 }
2967 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002968 if (VIM_ISWHITE(*p))
2969 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002970 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002971 return FAIL;
2972 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002973
Bram Moolenaar1c991142020-07-04 13:15:31 +02002974 // isolate one name
2975 exp_name = p;
2976 while (eval_isnamec(*p))
2977 ++p;
2978 cc = *p;
2979 *p = NUL;
2980
Bram Moolenaaredba7072021-03-13 21:14:18 +01002981 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2982 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002983 *p = cc;
2984 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002985 *end = p;
2986
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002987 if (idx < 0)
2988 {
2989 if (*p == '(' && ufunc != NULL)
2990 {
2991 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2992 return OK;
2993 }
2994 return FAIL;
2995 }
2996
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002997 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2998 import->imp_sid,
2999 idx,
3000 type);
3001 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003002 else if (import->imp_funcname != NULL)
3003 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003004 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003005 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3006 import->imp_sid,
3007 import->imp_var_vals_idx,
3008 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 return OK;
3010 }
3011
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003012 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003013 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 return FAIL;
3015}
3016
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003017 static int
3018generate_funcref(cctx_T *cctx, char_u *name)
3019{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003020 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003021
3022 if (ufunc == NULL)
3023 return FAIL;
3024
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003025 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003026 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3027 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003028 == FAIL)
3029 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003030 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003031}
3032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033/*
3034 * Compile a variable name into a load instruction.
3035 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003036 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 * When "error" is FALSE do not give an error when not found.
3038 */
3039 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003040compile_load(
3041 char_u **arg,
3042 char_u *end_arg,
3043 cctx_T *cctx,
3044 int is_expr,
3045 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046{
3047 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003048 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003049 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003051 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052
3053 if (*(*arg + 1) == ':')
3054 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003055 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003056 {
3057 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058
Bram Moolenaarfa596382021-04-07 21:58:16 +02003059 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003060 switch (**arg)
3061 {
3062 case 'g': isn_type = ISN_LOADGDICT; break;
3063 case 'w': isn_type = ISN_LOADWDICT; break;
3064 case 't': isn_type = ISN_LOADTDICT; break;
3065 case 'b': isn_type = ISN_LOADBDICT; break;
3066 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003067 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003068 goto theend;
3069 }
3070 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3071 goto theend;
3072 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 else
3075 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003076 isntype_T isn_type = ISN_DROP;
3077
Bram Moolenaarfa596382021-04-07 21:58:16 +02003078 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003079 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3080 if (name == NULL)
3081 return FAIL;
3082
3083 switch (**arg)
3084 {
3085 case 'v': res = generate_LOADV(cctx, name, error);
3086 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003087 case 's': if (is_expr && ASCII_ISUPPER(*name)
3088 && find_func(name, FALSE, cctx) != NULL)
3089 res = generate_funcref(cctx, name);
3090 else
3091 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003092 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003093 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003094 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003095 {
3096 if (is_expr && ASCII_ISUPPER(*name)
3097 && find_func(name, FALSE, cctx) != NULL)
3098 res = generate_funcref(cctx, name);
3099 else
3100 isn_type = ISN_LOADG;
3101 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003102 else
3103 {
3104 isn_type = ISN_LOADAUTO;
3105 vim_free(name);
3106 name = vim_strnsave(*arg, end - *arg);
3107 if (name == NULL)
3108 return FAIL;
3109 }
3110 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003111 case 'w': isn_type = ISN_LOADW; break;
3112 case 't': isn_type = ISN_LOADT; break;
3113 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003114 default: // cannot happen, just in case
3115 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003116 goto theend;
3117 }
3118 if (isn_type != ISN_DROP)
3119 {
3120 // Global, Buffer-local, Window-local and Tabpage-local
3121 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003122 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003123 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 }
3126 }
3127 else
3128 {
3129 size_t len = end - *arg;
3130 int idx;
3131 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003132 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133
3134 name = vim_strnsave(*arg, end - *arg);
3135 if (name == NULL)
3136 return FAIL;
3137
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003138 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3139 {
3140 script_autoload(name, FALSE);
3141 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3142 }
3143 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3144 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003146 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003147 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 }
3149 else
3150 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003151 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003152
Bram Moolenaar709664c2020-12-12 14:33:41 +01003153 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003155 type = lvar.lv_type;
3156 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003157 if (lvar.lv_from_outer != 0)
3158 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003159 else
3160 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 }
3162 else
3163 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003164 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003165 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003166 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003167 || find_imported(name, 0, cctx) != NULL)
3168 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003169
Bram Moolenaar0f769812020-09-12 18:32:34 +02003170 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003171 // uppercase letter it can be a user defined function.
3172 // generate_funcref() will fail if the function can't be found.
3173 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003174 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 }
3176 }
3177 if (gen_load)
3178 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003179 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003180 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003181 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003182 cctx->ctx_outer_used = TRUE;
3183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 }
3185
3186 *arg = end;
3187
3188theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003189 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003190 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 vim_free(name);
3192 return res;
3193}
3194
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003195 static void
3196clear_instr_ga(garray_T *gap)
3197{
3198 int idx;
3199
3200 for (idx = 0; idx < gap->ga_len; ++idx)
3201 delete_instr(((isn_T *)gap->ga_data) + idx);
3202 ga_clear(gap);
3203}
3204
3205/*
3206 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3207 * Returns FAIL if compilation fails.
3208 */
3209 static int
3210compile_string(isn_T *isn, cctx_T *cctx)
3211{
3212 char_u *s = isn->isn_arg.string;
3213 garray_T save_ga = cctx->ctx_instr;
3214 int expr_res;
3215 int trailing_error;
3216 int instr_count;
3217 isn_T *instr = NULL;
3218
3219 // Temporarily reset the list of instructions so that the jump labels are
3220 // correct.
3221 cctx->ctx_instr.ga_len = 0;
3222 cctx->ctx_instr.ga_maxlen = 0;
3223 cctx->ctx_instr.ga_data = NULL;
3224 expr_res = compile_expr0(&s, cctx);
3225 s = skipwhite(s);
3226 trailing_error = *s != NUL;
3227
Bram Moolenaarff652882021-05-16 15:24:49 +02003228 if (expr_res == FAIL || trailing_error
3229 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003230 {
3231 if (trailing_error)
3232 semsg(_(e_trailing_arg), s);
3233 clear_instr_ga(&cctx->ctx_instr);
3234 cctx->ctx_instr = save_ga;
3235 return FAIL;
3236 }
3237
3238 // Move the generated instructions into the ISN_INSTR instruction, then
3239 // restore the list of instructions.
3240 instr_count = cctx->ctx_instr.ga_len;
3241 instr = cctx->ctx_instr.ga_data;
3242 instr[instr_count].isn_type = ISN_FINISH;
3243
3244 cctx->ctx_instr = save_ga;
3245 vim_free(isn->isn_arg.string);
3246 isn->isn_type = ISN_INSTR;
3247 isn->isn_arg.instr = instr;
3248 return OK;
3249}
3250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251/*
3252 * Compile the argument expressions.
3253 * "arg" points to just after the "(" and is advanced to after the ")"
3254 */
3255 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003256compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003258 char_u *p = *arg;
3259 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003260 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003261 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262
Bram Moolenaare6085c52020-04-12 20:19:16 +02003263 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003265 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3266 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003267 if (*p == ')')
3268 {
3269 *arg = p + 1;
3270 return OK;
3271 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003272 if (must_end)
3273 {
3274 semsg(_(e_missing_comma_before_argument_str), p);
3275 return FAIL;
3276 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003277
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003278 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003279 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 return FAIL;
3281 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003282
Bram Moolenaarff652882021-05-16 15:24:49 +02003283 if (is_searchpair && *argcount >= 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003284 && cctx->ctx_instr.ga_len == instr_count + 1)
3285 {
3286 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3287
3288 // {skip} argument of searchpair() can be compiled if not empty
3289 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3290 compile_string(isn, cctx);
3291 }
3292
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003293 if (*p != ',' && *skipwhite(p) == ',')
3294 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003295 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003296 p = skipwhite(p);
3297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003299 {
3300 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003301 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003302 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003303 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003304 else
3305 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003306 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003307 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003309failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003310 emsg(_(e_missing_close));
3311 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312}
3313
3314/*
3315 * Compile a function call: name(arg1, arg2)
3316 * "arg" points to "name", "arg + varlen" to the "(".
3317 * "argcount_init" is 1 for "value->method()"
3318 * Instructions:
3319 * EVAL arg1
3320 * EVAL arg2
3321 * BCALL / DCALL / UCALL
3322 */
3323 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003324compile_call(
3325 char_u **arg,
3326 size_t varlen,
3327 cctx_T *cctx,
3328 ppconst_T *ppconst,
3329 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330{
3331 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003332 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 int argcount = argcount_init;
3334 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003335 char_u fname_buf[FLEN_FIXED + 1];
3336 char_u *tofree = NULL;
3337 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003338 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003339 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003340 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003341 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342
Bram Moolenaara5565e42020-05-09 15:44:01 +02003343 // we can evaluate "has('name')" at compile time
3344 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3345 {
3346 char_u *s = skipwhite(*arg + varlen + 1);
3347 typval_T argvars[2];
3348
3349 argvars[0].v_type = VAR_UNKNOWN;
3350 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003351 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003352 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003353 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003354 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003355 if (*s == ')' && argvars[0].v_type == VAR_STRING
3356 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003357 {
3358 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3359
3360 *arg = s + 1;
3361 argvars[1].v_type = VAR_UNKNOWN;
3362 tv->v_type = VAR_NUMBER;
3363 tv->vval.v_number = 0;
3364 f_has(argvars, tv);
3365 clear_tv(&argvars[0]);
3366 ++ppconst->pp_used;
3367 return OK;
3368 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003369 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003370 }
3371
3372 if (generate_ppconst(cctx, ppconst) == FAIL)
3373 return FAIL;
3374
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 if (varlen >= sizeof(namebuf))
3376 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003377 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 return FAIL;
3379 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003380 vim_strncpy(namebuf, *arg, varlen);
3381 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003383 // We handle the "skip" argument of searchpair() and searchpairpos()
3384 // differently.
3385 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3386 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3387 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3388 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003391 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003392 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393
Bram Moolenaar03290b82020-12-19 16:30:44 +01003394 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003395 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 {
3397 int idx;
3398
3399 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003400 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003402 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003403 if (STRCMP(name, "flatten") == 0)
3404 {
3405 emsg(_(e_cannot_use_flatten_in_vim9_script));
3406 goto theend;
3407 }
3408
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003409 if (STRCMP(name, "add") == 0 && argcount == 2)
3410 {
3411 garray_T *stack = &cctx->ctx_type_stack;
3412 type_T *type = ((type_T **)stack->ga_data)[
3413 stack->ga_len - 2];
3414
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003415 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003416 if (type->tt_type == VAR_LIST)
3417 {
3418 // inline "add(list, item)" so that the type can be checked
3419 res = generate_LISTAPPEND(cctx);
3420 idx = -1;
3421 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003422 else if (type->tt_type == VAR_BLOB)
3423 {
3424 // inline "add(blob, nr)" so that the type can be checked
3425 res = generate_BLOBAPPEND(cctx);
3426 idx = -1;
3427 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003428 }
3429
3430 if (idx >= 0)
3431 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3432 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003433 else
3434 semsg(_(e_unknownfunc), namebuf);
3435 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 }
3437
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003438 // An argument or local variable can be a function reference, this
3439 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003440 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003441 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003442 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003443 // If we can find the function by name generate the right call.
3444 // Skip global functions here, a local funcref takes precedence.
3445 ufunc = find_func(name, FALSE, cctx);
3446 if (ufunc != NULL && !func_is_global(ufunc))
3447 {
3448 res = generate_CALL(cctx, ufunc, argcount);
3449 goto theend;
3450 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003451 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452
3453 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003454 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003455 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003457 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003458 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003459 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003460 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003461 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003462
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003463 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003464 goto theend;
3465 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466
Bram Moolenaar0f769812020-09-12 18:32:34 +02003467 // If we can find a global function by name generate the right call.
3468 if (ufunc != NULL)
3469 {
3470 res = generate_CALL(cctx, ufunc, argcount);
3471 goto theend;
3472 }
3473
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003474 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003475 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003476 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003477 res = generate_UCALL(cctx, name, argcount);
3478 else
3479 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003480
3481theend:
3482 vim_free(tofree);
3483 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484}
3485
3486// like NAMESPACE_CHAR but with 'a' and 'l'.
3487#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3488
3489/*
3490 * Find the end of a variable or function name. Unlike find_name_end() this
3491 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003492 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 * Return a pointer to just after the name. Equal to "arg" if there is no
3494 * valid name.
3495 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003496 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003497to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498{
3499 char_u *p;
3500
3501 // Quick check for valid starting character.
3502 if (!eval_isnamec1(*arg))
3503 return arg;
3504
3505 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3506 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3507 // and can be used in slice "[n:]".
3508 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003509 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3511 break;
3512 return p;
3513}
3514
3515/*
3516 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003517 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 */
3519 char_u *
3520to_name_const_end(char_u *arg)
3521{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003522 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 typval_T rettv;
3524
3525 if (p == arg && *arg == '[')
3526 {
3527
3528 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003529 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 p = arg;
3531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 return p;
3533}
3534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535/*
3536 * parse a list: [expr, expr]
3537 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003538 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 */
3540 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003541compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542{
3543 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003544 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003546 int is_const;
3547 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003549 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003551 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003552 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003553 semsg(_(e_list_end), *arg);
3554 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003555 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003556 if (*p == ',')
3557 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003558 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003559 return FAIL;
3560 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003561 if (*p == ']')
3562 {
3563 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003564 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003565 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003566 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003567 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003568 if (!is_const)
3569 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 ++count;
3571 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003572 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003574 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3575 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003576 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003577 return FAIL;
3578 }
3579 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003580 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 p = skipwhite(p);
3582 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003583 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003585 ppconst->pp_is_const = is_all_const;
3586 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587}
3588
3589/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003590 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003591 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003592 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 */
3594 static int
3595compile_lambda(char_u **arg, cctx_T *cctx)
3596{
Bram Moolenaare462f522020-12-27 14:43:30 +01003597 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 typval_T rettv;
3599 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003600 evalarg_T evalarg;
3601
3602 CLEAR_FIELD(evalarg);
3603 evalarg.eval_flags = EVAL_EVALUATE;
3604 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605
3606 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003607 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3608 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003609 {
3610 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003611 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003612 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003613
Bram Moolenaar65c44152020-12-24 15:14:01 +01003614 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003616 ++ufunc->uf_refcount;
3617 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618
Bram Moolenaara9931532021-06-12 15:58:16 +02003619 // Compile it here to get the return type. The return type is optional,
3620 // when it's missing use t_unknown. This is recognized in
3621 // compile_return().
3622 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3623 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaare99d4222021-06-13 14:01:26 +02003624 compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003626 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3627 // points into it. Point to the original line to avoid a dangling pointer.
3628 if (evalarg.eval_tofree_cmdline != NULL)
3629 {
3630 size_t off = *arg - evalarg.eval_tofree_cmdline;
3631
3632 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3633 + off;
3634 }
3635
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003636 clear_evalarg(&evalarg, NULL);
3637
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003638 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003639 {
3640 // The return type will now be known.
3641 set_function_type(ufunc);
3642
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003643 // The function reference count will be 1. When the ISN_FUNCREF
3644 // instruction is deleted the reference count is decremented and the
3645 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003646 return generate_FUNCREF(cctx, ufunc);
3647 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003648
3649 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 return FAIL;
3651}
3652
3653/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003654 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003656 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 */
3658 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003659compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660{
3661 garray_T *instr = &cctx->ctx_instr;
3662 int count = 0;
3663 dict_T *d = dict_alloc();
3664 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003665 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003666 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003667 int is_const;
3668 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669
3670 if (d == NULL)
3671 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003672 if (generate_ppconst(cctx, ppconst) == FAIL)
3673 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003674 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003676 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677
Bram Moolenaar23c55272020-06-21 16:58:13 +02003678 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003679 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003680 *arg = NULL;
3681 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003682 }
3683
3684 if (**arg == '}')
3685 break;
3686
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003687 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003689 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003691 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003692 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003693 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003696 if (isn->isn_type == ISN_PUSHNR)
3697 {
3698 char buf[NUMBUFLEN];
3699
3700 // Convert to string at compile time.
3701 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3702 isn->isn_type = ISN_PUSHS;
3703 isn->isn_arg.string = vim_strsave((char_u *)buf);
3704 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 if (isn->isn_type == ISN_PUSHS)
3706 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003707 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003708 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003709 *arg = skipwhite(*arg);
3710 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003711 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003712 emsg(_(e_missing_matching_bracket_after_dict_key));
3713 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003714 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003715 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003717 else
3718 {
3719 // {"name": value},
3720 // {'name': value},
3721 // {name: value} use "name" as a literal key
3722 key = get_literal_key(arg);
3723 if (key == NULL)
3724 return FAIL;
3725 if (generate_PUSHS(cctx, key) == FAIL)
3726 return FAIL;
3727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728
3729 // Check for duplicate keys, if using string keys.
3730 if (key != NULL)
3731 {
3732 item = dict_find(d, key, -1);
3733 if (item != NULL)
3734 {
3735 semsg(_(e_duplicate_key), key);
3736 goto failret;
3737 }
3738 item = dictitem_alloc(key);
3739 if (item != NULL)
3740 {
3741 item->di_tv.v_type = VAR_UNKNOWN;
3742 item->di_tv.v_lock = 0;
3743 if (dict_add(d, item) == FAIL)
3744 dictitem_free(item);
3745 }
3746 }
3747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 if (**arg != ':')
3749 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003750 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003751 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003752 else
3753 semsg(_(e_missing_dict_colon), *arg);
3754 return FAIL;
3755 }
3756 whitep = *arg + 1;
3757 if (!IS_WHITE_OR_NUL(*whitep))
3758 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003759 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 return FAIL;
3761 }
3762
Bram Moolenaar23c55272020-06-21 16:58:13 +02003763 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003764 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003765 *arg = NULL;
3766 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003767 }
3768
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003769 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003771 if (!is_const)
3772 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 ++count;
3774
Bram Moolenaar2c330432020-04-13 14:41:35 +02003775 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003776 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003777 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003778 *arg = NULL;
3779 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 if (**arg == '}')
3782 break;
3783 if (**arg != ',')
3784 {
3785 semsg(_(e_missing_dict_comma), *arg);
3786 goto failret;
3787 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003788 if (IS_WHITE_OR_NUL(*whitep))
3789 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003790 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003791 return FAIL;
3792 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003793 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003794 if (!IS_WHITE_OR_NUL(*whitep))
3795 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003796 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003797 return FAIL;
3798 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003799 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 }
3801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 *arg = *arg + 1;
3803
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003804 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003805 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003806 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003807 *arg += STRLEN(*arg);
3808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003810 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 return generate_NEWDICT(cctx, count);
3812
3813failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003814 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003815 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003816 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003817 *arg = (char_u *)"";
3818 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 dict_unref(d);
3820 return FAIL;
3821}
3822
3823/*
3824 * Compile "&option".
3825 */
3826 static int
3827compile_get_option(char_u **arg, cctx_T *cctx)
3828{
3829 typval_T rettv;
3830 char_u *start = *arg;
3831 int ret;
3832
3833 // parse the option and get the current value to get the type.
3834 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003835 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 if (ret == OK)
3837 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003838 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003839 char_u *name = vim_strnsave(start, *arg - start);
3840 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3841 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842
3843 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3844 vim_free(name);
3845 }
3846 clear_tv(&rettv);
3847
3848 return ret;
3849}
3850
3851/*
3852 * Compile "$VAR".
3853 */
3854 static int
3855compile_get_env(char_u **arg, cctx_T *cctx)
3856{
3857 char_u *start = *arg;
3858 int len;
3859 int ret;
3860 char_u *name;
3861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 ++*arg;
3863 len = get_env_len(arg);
3864 if (len == 0)
3865 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003866 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 return FAIL;
3868 }
3869
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003870 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 name = vim_strnsave(start, len + 1);
3872 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3873 vim_free(name);
3874 return ret;
3875}
3876
3877/*
3878 * Compile "@r".
3879 */
3880 static int
3881compile_get_register(char_u **arg, cctx_T *cctx)
3882{
3883 int ret;
3884
3885 ++*arg;
3886 if (**arg == NUL)
3887 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003888 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 return FAIL;
3890 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003891 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 {
3893 emsg_invreg(**arg);
3894 return FAIL;
3895 }
3896 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3897 ++*arg;
3898 return ret;
3899}
3900
3901/*
3902 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003903 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 */
3905 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003906apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003908 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909
3910 // this works from end to start
3911 while (p > start)
3912 {
3913 --p;
3914 if (*p == '-' || *p == '+')
3915 {
3916 // only '-' has an effect, for '+' we only check the type
3917#ifdef FEAT_FLOAT
3918 if (rettv->v_type == VAR_FLOAT)
3919 {
3920 if (*p == '-')
3921 rettv->vval.v_float = -rettv->vval.v_float;
3922 }
3923 else
3924#endif
3925 {
3926 varnumber_T val;
3927 int error = FALSE;
3928
3929 // tv_get_number_chk() accepts a string, but we don't want that
3930 // here
3931 if (check_not_string(rettv) == FAIL)
3932 return FAIL;
3933 val = tv_get_number_chk(rettv, &error);
3934 clear_tv(rettv);
3935 if (error)
3936 return FAIL;
3937 if (*p == '-')
3938 val = -val;
3939 rettv->v_type = VAR_NUMBER;
3940 rettv->vval.v_number = val;
3941 }
3942 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003943 else if (numeric_only)
3944 {
3945 ++p;
3946 break;
3947 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003948 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 {
3950 int v = tv2bool(rettv);
3951
3952 // '!' is permissive in the type.
3953 clear_tv(rettv);
3954 rettv->v_type = VAR_BOOL;
3955 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3956 }
3957 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003958 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 return OK;
3960}
3961
3962/*
3963 * Recognize v: variables that are constants and set "rettv".
3964 */
3965 static void
3966get_vim_constant(char_u **arg, typval_T *rettv)
3967{
3968 if (STRNCMP(*arg, "v:true", 6) == 0)
3969 {
3970 rettv->v_type = VAR_BOOL;
3971 rettv->vval.v_number = VVAL_TRUE;
3972 *arg += 6;
3973 }
3974 else if (STRNCMP(*arg, "v:false", 7) == 0)
3975 {
3976 rettv->v_type = VAR_BOOL;
3977 rettv->vval.v_number = VVAL_FALSE;
3978 *arg += 7;
3979 }
3980 else if (STRNCMP(*arg, "v:null", 6) == 0)
3981 {
3982 rettv->v_type = VAR_SPECIAL;
3983 rettv->vval.v_number = VVAL_NULL;
3984 *arg += 6;
3985 }
3986 else if (STRNCMP(*arg, "v:none", 6) == 0)
3987 {
3988 rettv->v_type = VAR_SPECIAL;
3989 rettv->vval.v_number = VVAL_NONE;
3990 *arg += 6;
3991 }
3992}
3993
Bram Moolenaar657137c2021-01-09 15:45:23 +01003994 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003995get_compare_type(char_u *p, int *len, int *type_is)
3996{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003997 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003998 int i;
3999
4000 switch (p[0])
4001 {
4002 case '=': if (p[1] == '=')
4003 type = EXPR_EQUAL;
4004 else if (p[1] == '~')
4005 type = EXPR_MATCH;
4006 break;
4007 case '!': if (p[1] == '=')
4008 type = EXPR_NEQUAL;
4009 else if (p[1] == '~')
4010 type = EXPR_NOMATCH;
4011 break;
4012 case '>': if (p[1] != '=')
4013 {
4014 type = EXPR_GREATER;
4015 *len = 1;
4016 }
4017 else
4018 type = EXPR_GEQUAL;
4019 break;
4020 case '<': if (p[1] != '=')
4021 {
4022 type = EXPR_SMALLER;
4023 *len = 1;
4024 }
4025 else
4026 type = EXPR_SEQUAL;
4027 break;
4028 case 'i': if (p[1] == 's')
4029 {
4030 // "is" and "isnot"; but not a prefix of a name
4031 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4032 *len = 5;
4033 i = p[*len];
4034 if (!isalnum(i) && i != '_')
4035 {
4036 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4037 *type_is = TRUE;
4038 }
4039 }
4040 break;
4041 }
4042 return type;
4043}
4044
4045/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004046 * Skip over an expression, ignoring most errors.
4047 */
4048 static void
4049skip_expr_cctx(char_u **arg, cctx_T *cctx)
4050{
4051 evalarg_T evalarg;
4052
4053 CLEAR_FIELD(evalarg);
4054 evalarg.eval_cctx = cctx;
4055 skip_expr(arg, &evalarg);
4056}
4057
4058/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004060 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 */
4062 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004063compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004065 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066
4067 // this works from end to start
4068 while (p > start)
4069 {
4070 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004071 while (VIM_ISWHITE(*p))
4072 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 if (*p == '-' || *p == '+')
4074 {
4075 int negate = *p == '-';
4076 isn_T *isn;
4077
4078 // TODO: check type
4079 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4080 {
4081 --p;
4082 if (*p == '-')
4083 negate = !negate;
4084 }
4085 // only '-' has an effect, for '+' we only check the type
4086 if (negate)
4087 isn = generate_instr(cctx, ISN_NEGATENR);
4088 else
4089 isn = generate_instr(cctx, ISN_CHECKNR);
4090 if (isn == NULL)
4091 return FAIL;
4092 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004093 else if (numeric_only)
4094 {
4095 ++p;
4096 break;
4097 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 else
4099 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004100 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004102 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004104 if (p[-1] == '!')
4105 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004108 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 return FAIL;
4110 }
4111 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004112 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 return OK;
4114}
4115
4116/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004117 * Compile "(expression)": recursive!
4118 * Return FAIL/OK.
4119 */
4120 static int
4121compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4122{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004123 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004124 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004125
Bram Moolenaar24156692021-01-14 20:35:49 +01004126 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4127 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004128 if (ppconst->pp_used <= PPSIZE - 10)
4129 {
4130 ret = compile_expr1(arg, cctx, ppconst);
4131 }
4132 else
4133 {
4134 // Not enough space in ppconst, flush constants.
4135 if (generate_ppconst(cctx, ppconst) == FAIL)
4136 return FAIL;
4137 ret = compile_expr0(arg, cctx);
4138 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004139 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4140 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004141 if (**arg == ')')
4142 ++*arg;
4143 else if (ret == OK)
4144 {
4145 emsg(_(e_missing_close));
4146 ret = FAIL;
4147 }
4148 return ret;
4149}
4150
4151/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004153 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 */
4155 static int
4156compile_subscript(
4157 char_u **arg,
4158 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004159 char_u *start_leader,
4160 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004161 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004163 char_u *name_start = *end_leader;
4164
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 for (;;)
4166 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004167 char_u *p = skipwhite(*arg);
4168
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004169 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004170 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004171 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004172
4173 // If a following line starts with "->{" or "->X" advance to that
4174 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004175 // Also if a following line starts with ".x".
4176 if (next != NULL &&
4177 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004178 && (next[2] == '{'
4179 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004180 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004181 {
4182 next = next_line_from_context(cctx, TRUE);
4183 if (next == NULL)
4184 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004185 *arg = next;
4186 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004187 }
4188 }
4189
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004190 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004191 // not a function call.
4192 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004194 garray_T *stack = &cctx->ctx_type_stack;
4195 type_T *type;
4196 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004198 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004199 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004200 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004201
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004203 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4204
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004205 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004206 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004208 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 return FAIL;
4210 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004211 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004213 char_u *pstart = p;
4214
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004215 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004216 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004217 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 // something->method()
4220 // Apply the '!', '-' and '+' first:
4221 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004222 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004225 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004226 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004227 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004228 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004229 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004230 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004231 garray_T *stack = &cctx->ctx_type_stack;
4232 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004233 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004234 int expr_isn_start = cctx->ctx_instr.ga_len;
4235 int expr_isn_end;
4236 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004237
4238 // Funcref call: list->(Refs[2])(arg)
4239 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004240 //
4241 // Fist compile the function expression.
4242 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004243 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004244
4245 // Remember the next instruction index, where the instructions
4246 // for arguments are being written.
4247 expr_isn_end = cctx->ctx_instr.ga_len;
4248
4249 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004250 if (**arg != '(')
4251 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004252 if (*skipwhite(*arg) == '(')
4253 emsg(_(e_nowhitespace));
4254 else
4255 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004256 return FAIL;
4257 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004258 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004259 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004260 return FAIL;
4261
Bram Moolenaar2927c072021-04-05 19:41:21 +02004262 // Move the instructions for the arguments to before the
4263 // instructions of the expression and move the type of the
4264 // expression after the argument types. This is what ISN_PCALL
4265 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004266 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004267 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4268 if (arg_isn_count > 0)
4269 {
4270 int expr_isn_count = expr_isn_end - expr_isn_start;
4271 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4272
4273 if (isn == NULL)
4274 return FAIL;
4275 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4276 + expr_isn_start,
4277 sizeof(isn_T) * expr_isn_count);
4278 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4279 + expr_isn_start,
4280 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4281 sizeof(isn_T) * arg_isn_count);
4282 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4283 + expr_isn_start + arg_isn_count,
4284 isn, sizeof(isn_T) * expr_isn_count);
4285 vim_free(isn);
4286
4287 type = ((type_T **)stack->ga_data)[type_idx_start];
4288 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4289 ((type_T **)stack->ga_data) + type_idx_start + 1,
4290 sizeof(type_T *)
4291 * (stack->ga_len - type_idx_start - 1));
4292 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4293 }
4294
Bram Moolenaar7e368202020-12-25 21:56:57 +01004295 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4296 if (generate_PCALL(cctx, argcount,
4297 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 return FAIL;
4299 }
4300 else
4301 {
4302 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004303 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004304 if (!eval_isnamec1(*p))
4305 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004306 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004307 return FAIL;
4308 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004309 if (ASCII_ISALPHA(*p) && p[1] == ':')
4310 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004311 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 ;
4313 if (*p != '(')
4314 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004315 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 return FAIL;
4317 }
4318 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004319 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 return FAIL;
4321 }
4322 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004323 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004325 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004326
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004327 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004328 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004329 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004330 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004331 // TODO: more arguments
4332 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004333 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004334 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004335 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004336
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004337 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004338 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004339 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004340 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004341 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004342 // missing first index is equal to zero
4343 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004344 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004345 else
4346 {
4347 if (compile_expr0(arg, cctx) == FAIL)
4348 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004349 if (**arg == ':')
4350 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004351 semsg(_(e_white_space_required_before_and_after_str_at_str),
4352 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004353 return FAIL;
4354 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004355 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004356 return FAIL;
4357 *arg = skipwhite(*arg);
4358 }
4359 if (**arg == ':')
4360 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004361 is_slice = TRUE;
4362 ++*arg;
4363 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4364 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004365 semsg(_(e_white_space_required_before_and_after_str_at_str),
4366 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004367 return FAIL;
4368 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004369 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004370 return FAIL;
4371 if (**arg == ']')
4372 // missing second index is equal to end of string
4373 generate_PUSHNR(cctx, -1);
4374 else
4375 {
4376 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004377 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004378 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004379 return FAIL;
4380 *arg = skipwhite(*arg);
4381 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004382 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383
4384 if (**arg != ']')
4385 {
4386 emsg(_(e_missbrac));
4387 return FAIL;
4388 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004389 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390
Bram Moolenaare42939a2021-04-05 17:11:17 +02004391 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004392 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004394 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004396 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004397 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004398 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004399 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004400
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004401 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004402 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004403 {
4404 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004405 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004406 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004407 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004408 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 while (eval_isnamec(*p))
4410 MB_PTR_ADV(p);
4411 if (p == *arg)
4412 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004413 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 return FAIL;
4415 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004416 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 return FAIL;
4418 *arg = p;
4419 }
4420 else
4421 break;
4422 }
4423
4424 // TODO - see handle_subscript():
4425 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4426 // Don't do this when "Func" is already a partial that was bound
4427 // explicitly (pt_auto is FALSE).
4428
4429 return OK;
4430}
4431
4432/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004433 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4434 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004436 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004437 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004438 *
4439 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 */
4441
4442/*
4443 * number number constant
4444 * 0zFFFFFFFF Blob constant
4445 * "string" string constant
4446 * 'string' literal string constant
4447 * &option-name option value
4448 * @r register contents
4449 * identifier variable value
4450 * function() function call
4451 * $VAR environment variable
4452 * (expression) nested expression
4453 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004454 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 *
4456 * Also handle:
4457 * ! in front logical NOT
4458 * - in front unary minus
4459 * + in front unary plus (ignored)
4460 * trailing (arg) funcref/partial call
4461 * trailing [] subscript in String or List
4462 * trailing .name entry in Dictionary
4463 * trailing ->name() method call
4464 */
4465 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004466compile_expr7(
4467 char_u **arg,
4468 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004469 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471 char_u *start_leader, *end_leader;
4472 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004473 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004474 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004476 ppconst->pp_is_const = FALSE;
4477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 /*
4479 * Skip '!', '-' and '+' characters. They are handled later.
4480 */
4481 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004482 if (eval_leader(arg, TRUE) == FAIL)
4483 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 end_leader = *arg;
4485
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004486 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 switch (**arg)
4488 {
4489 /*
4490 * Number constant.
4491 */
4492 case '0': // also for blob starting with 0z
4493 case '1':
4494 case '2':
4495 case '3':
4496 case '4':
4497 case '5':
4498 case '6':
4499 case '7':
4500 case '8':
4501 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004502 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004504 // Apply "-" and "+" just before the number now, right to
4505 // left. Matters especially when "->" follows. Stops at
4506 // '!'.
4507 if (apply_leader(rettv, TRUE,
4508 start_leader, &end_leader) == FAIL)
4509 {
4510 clear_tv(rettv);
4511 return FAIL;
4512 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 break;
4514
4515 /*
4516 * String constant: "string".
4517 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004518 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 return FAIL;
4520 break;
4521
4522 /*
4523 * Literal string constant: 'str''ing'.
4524 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004525 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526 return FAIL;
4527 break;
4528
4529 /*
4530 * Constant Vim variable.
4531 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004532 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 ret = NOTDONE;
4534 break;
4535
4536 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004537 * "true" constant
4538 */
4539 case 't': if (STRNCMP(*arg, "true", 4) == 0
4540 && !eval_isnamec((*arg)[4]))
4541 {
4542 *arg += 4;
4543 rettv->v_type = VAR_BOOL;
4544 rettv->vval.v_number = VVAL_TRUE;
4545 }
4546 else
4547 ret = NOTDONE;
4548 break;
4549
4550 /*
4551 * "false" constant
4552 */
4553 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4554 && !eval_isnamec((*arg)[5]))
4555 {
4556 *arg += 5;
4557 rettv->v_type = VAR_BOOL;
4558 rettv->vval.v_number = VVAL_FALSE;
4559 }
4560 else
4561 ret = NOTDONE;
4562 break;
4563
4564 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004565 * "null" constant
4566 */
4567 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004568 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004569 {
4570 *arg += 4;
4571 rettv->v_type = VAR_SPECIAL;
4572 rettv->vval.v_number = VVAL_NULL;
4573 }
4574 else
4575 ret = NOTDONE;
4576 break;
4577
4578 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 * List: [expr, expr]
4580 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004581 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4582 return FAIL;
4583 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 break;
4585
4586 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 * Dictionary: {'key': val, 'key': val}
4588 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004589 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4590 return FAIL;
4591 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 break;
4593
4594 /*
4595 * Option value: &name
4596 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004597 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4598 return FAIL;
4599 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 break;
4601
4602 /*
4603 * Environment variable: $VAR.
4604 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004605 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4606 return FAIL;
4607 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 break;
4609
4610 /*
4611 * Register contents: @r.
4612 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004613 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4614 return FAIL;
4615 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 break;
4617 /*
4618 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004619 * lambda: (arg, arg) => expr
4620 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004622 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4623 ret = compile_lambda(arg, cctx);
4624 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004625 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 break;
4627
4628 default: ret = NOTDONE;
4629 break;
4630 }
4631 if (ret == FAIL)
4632 return FAIL;
4633
Bram Moolenaar1c747212020-05-09 18:28:34 +02004634 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004636 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004637 clear_tv(rettv);
4638 else
4639 // A constant expression can possibly be handled compile time,
4640 // return the value instead of generating code.
4641 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 }
4643 else if (ret == NOTDONE)
4644 {
4645 char_u *p;
4646 int r;
4647
4648 if (!eval_isnamec1(**arg))
4649 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004650 if (!vim9_bad_comment(*arg))
4651 {
4652 if (ends_excmd(*skipwhite(*arg)))
4653 semsg(_(e_empty_expression_str), *arg);
4654 else
4655 semsg(_(e_name_expected_str), *arg);
4656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 return FAIL;
4658 }
4659
4660 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004661 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004662 if (p - *arg == (size_t)1 && **arg == '_')
4663 {
4664 emsg(_(e_cannot_use_underscore_here));
4665 return FAIL;
4666 }
4667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 {
4670 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4671 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004673 {
4674 if (generate_ppconst(cctx, ppconst) == FAIL)
4675 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004676 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004677 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 if (r == FAIL)
4679 return FAIL;
4680 }
4681
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004682 // Handle following "[]", ".member", etc.
4683 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004684 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004685 ppconst) == FAIL)
4686 return FAIL;
4687 if (ppconst->pp_used > 0)
4688 {
4689 // apply the '!', '-' and '+' before the constant
4690 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004691 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004692 return FAIL;
4693 return OK;
4694 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004695 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004697 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698}
4699
4700/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004701 * Give the "white on both sides" error, taking the operator from "p[len]".
4702 */
4703 void
4704error_white_both(char_u *op, int len)
4705{
4706 char_u buf[10];
4707
4708 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004709 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004710}
4711
4712/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004713 * <type>expr7: runtime type check / conversion
4714 */
4715 static int
4716compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4717{
4718 type_T *want_type = NULL;
4719
4720 // Recognize <type>
4721 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4722 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004723 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004724 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4725 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004726 return FAIL;
4727
4728 if (**arg != '>')
4729 {
4730 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004731 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004732 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004733 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004734 return FAIL;
4735 }
4736 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004737 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004738 return FAIL;
4739 }
4740
4741 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4742 return FAIL;
4743
4744 if (want_type != NULL)
4745 {
4746 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004747 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004748 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004749
Bram Moolenaard1103582020-08-14 22:44:25 +02004750 generate_ppconst(cctx, ppconst);
4751 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004752 where.wt_index = 0;
4753 where.wt_variable = FALSE;
4754 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004755 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004756 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004757 return FAIL;
4758 }
4759 }
4760
4761 return OK;
4762}
4763
4764/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 * * number multiplication
4766 * / number division
4767 * % number modulo
4768 */
4769 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004770compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771{
4772 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004773 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004774 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004776 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004777 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 return FAIL;
4779
4780 /*
4781 * Repeat computing, until no "*", "/" or "%" is following.
4782 */
4783 for (;;)
4784 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004785 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 if (*op != '*' && *op != '/' && *op != '%')
4787 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004788 if (next != NULL)
4789 {
4790 *arg = next_line_from_context(cctx, TRUE);
4791 op = skipwhite(*arg);
4792 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004793
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004794 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004796 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004797 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004799 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004800 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004802 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004803 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004805
4806 if (ppconst->pp_used == ppconst_used + 2
4807 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4808 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004809 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004810 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4811 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4812 varnumber_T res = 0;
4813 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004815 // both are numbers: compute the result
4816 switch (*op)
4817 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004818 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004819 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004820 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004821 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004822 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004823 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004824 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004825 break;
4826 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004827 if (failed)
4828 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004829 tv1->vval.v_number = res;
4830 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004831 }
4832 else
4833 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004834 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004835 generate_two_op(cctx, op);
4836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 }
4838
4839 return OK;
4840}
4841
4842/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004843 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 * - number subtraction
4845 * .. string concatenation
4846 */
4847 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004848compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849{
4850 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004851 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004853 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854
4855 // get the first variable
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
4859 /*
4860 * Repeat computing, until no "+", "-" or ".." is following.
4861 */
4862 for (;;)
4863 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004864 op = may_peek_next_line(cctx, *arg, &next);
4865 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004867 if (op[0] == op[1] && *op != '.' && next)
4868 // Finding "++" or "--" on the next line is a separate command.
4869 // But ".." is concatenation.
4870 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004872 if (next != NULL)
4873 {
4874 *arg = next_line_from_context(cctx, TRUE);
4875 op = skipwhite(*arg);
4876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004878 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004880 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004881 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882 }
4883
Bram Moolenaare0de1712020-12-02 17:36:54 +01004884 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004885 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004887 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004888 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 return FAIL;
4890
Bram Moolenaara5565e42020-05-09 15:44:01 +02004891 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004892 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004893 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4894 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4895 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4896 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004898 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4899 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004900
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004901 // concat/subtract/add constant numbers
4902 if (*op == '+')
4903 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4904 else if (*op == '-')
4905 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4906 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004907 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004908 // concatenate constant strings
4909 char_u *s1 = tv1->vval.v_string;
4910 char_u *s2 = tv2->vval.v_string;
4911 size_t len1 = STRLEN(s1);
4912
4913 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4914 if (tv1->vval.v_string == NULL)
4915 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004916 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004917 return FAIL;
4918 }
4919 mch_memmove(tv1->vval.v_string, s1, len1);
4920 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004921 vim_free(s1);
4922 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004923 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004924 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 }
4926 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004927 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004928 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004929 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004930 if (*op == '.')
4931 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004932 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4933 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004934 return FAIL;
4935 generate_instr_drop(cctx, ISN_CONCAT, 1);
4936 }
4937 else
4938 generate_two_op(cctx, op);
4939 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 }
4941
4942 return OK;
4943}
4944
4945/*
4946 * expr5a == expr5b
4947 * expr5a =~ expr5b
4948 * expr5a != expr5b
4949 * expr5a !~ expr5b
4950 * expr5a > expr5b
4951 * expr5a >= expr5b
4952 * expr5a < expr5b
4953 * expr5a <= expr5b
4954 * expr5a is expr5b
4955 * expr5a isnot expr5b
4956 *
4957 * Produces instructions:
4958 * EVAL expr5a Push result of "expr5a"
4959 * EVAL expr5b Push result of "expr5b"
4960 * COMPARE one of the compare instructions
4961 */
4962 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004963compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004965 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004967 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004970 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971
4972 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004973 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 return FAIL;
4975
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004976 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004977 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978
4979 /*
4980 * If there is a comparative operator, use it.
4981 */
4982 if (type != EXPR_UNKNOWN)
4983 {
4984 int ic = FALSE; // Default: do not ignore case
4985
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004986 if (next != NULL)
4987 {
4988 *arg = next_line_from_context(cctx, TRUE);
4989 p = skipwhite(*arg);
4990 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 if (type_is && (p[len] == '?' || p[len] == '#'))
4992 {
4993 semsg(_(e_invexpr2), *arg);
4994 return FAIL;
4995 }
4996 // extra question mark appended: ignore case
4997 if (p[len] == '?')
4998 {
4999 ic = TRUE;
5000 ++len;
5001 }
5002 // extra '#' appended: match case (ignored)
5003 else if (p[len] == '#')
5004 ++len;
5005 // nothing appended: match case
5006
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005007 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005009 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005010 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 }
5012
5013 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005014 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005015 return FAIL;
5016
Bram Moolenaara5565e42020-05-09 15:44:01 +02005017 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 return FAIL;
5019
Bram Moolenaara5565e42020-05-09 15:44:01 +02005020 if (ppconst->pp_used == ppconst_used + 2)
5021 {
5022 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5023 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5024 int ret;
5025
5026 // Both sides are a constant, compute the result now.
5027 // First check for a valid combination of types, this is more
5028 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005029 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005030 ret = FAIL;
5031 else
5032 {
5033 ret = typval_compare(tv1, tv2, type, ic);
5034 tv1->v_type = VAR_BOOL;
5035 tv1->vval.v_number = tv1->vval.v_number
5036 ? VVAL_TRUE : VVAL_FALSE;
5037 clear_tv(tv2);
5038 --ppconst->pp_used;
5039 }
5040 return ret;
5041 }
5042
5043 generate_ppconst(cctx, ppconst);
5044 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 }
5046
5047 return OK;
5048}
5049
Bram Moolenaar7f141552020-05-09 17:35:53 +02005050static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052/*
5053 * Compile || or &&.
5054 */
5055 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005056compile_and_or(
5057 char_u **arg,
5058 cctx_T *cctx,
5059 char *op,
5060 ppconst_T *ppconst,
5061 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005063 char_u *next;
5064 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 int opchar = *op;
5066
5067 if (p[0] == opchar && p[1] == opchar)
5068 {
5069 garray_T *instr = &cctx->ctx_instr;
5070 garray_T end_ga;
5071
5072 /*
5073 * Repeat until there is no following "||" or "&&"
5074 */
5075 ga_init2(&end_ga, sizeof(int), 10);
5076 while (p[0] == opchar && p[1] == opchar)
5077 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005078 long start_lnum = SOURCING_LNUM;
5079 int start_ctx_lnum = cctx->ctx_lnum;
5080 int save_lnum;
5081
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005082 if (next != NULL)
5083 {
5084 *arg = next_line_from_context(cctx, TRUE);
5085 p = skipwhite(*arg);
5086 }
5087
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005088 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5089 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005090 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005091 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005092 return FAIL;
5093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005095 // TODO: use ppconst if the value is a constant and check
5096 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02005097 generate_ppconst(cctx, ppconst);
5098
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005099 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02005100 SOURCING_LNUM = start_lnum;
5101 save_lnum = cctx->ctx_lnum;
5102 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005103 if (bool_on_stack(cctx) == FAIL)
5104 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005105 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005106 ga_clear(&end_ga);
5107 return FAIL;
5108 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005109 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 if (ga_grow(&end_ga, 1) == FAIL)
5112 {
5113 ga_clear(&end_ga);
5114 return FAIL;
5115 }
5116 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5117 ++end_ga.ga_len;
5118 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005119 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120
5121 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01005122 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005123 {
5124 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005125 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005126 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005127
Bram Moolenaara5565e42020-05-09 15:44:01 +02005128 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5129 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 {
5131 ga_clear(&end_ga);
5132 return FAIL;
5133 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005134
5135 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005136 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005137 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005139 // Every part must evaluate to a bool.
5140 if (bool_on_stack(cctx) == FAIL)
5141 {
5142 ga_clear(&end_ga);
5143 return FAIL;
5144 }
5145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 // Fill in the end label in all jumps.
5147 while (end_ga.ga_len > 0)
5148 {
5149 isn_T *isn;
5150
5151 --end_ga.ga_len;
5152 isn = ((isn_T *)instr->ga_data)
5153 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5154 isn->isn_arg.jump.jump_where = instr->ga_len;
5155 }
5156 ga_clear(&end_ga);
5157 }
5158
5159 return OK;
5160}
5161
5162/*
5163 * expr4a && expr4a && expr4a logical AND
5164 *
5165 * Produces instructions:
5166 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005167 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005168 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005169 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005170 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171 * EVAL expr4c Push result of "expr4c"
5172 * end:
5173 */
5174 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005175compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005177 int ppconst_used = ppconst->pp_used;
5178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005180 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 return FAIL;
5182
5183 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005184 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005185}
5186
5187/*
5188 * expr3a || expr3b || expr3c logical OR
5189 *
5190 * Produces instructions:
5191 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005192 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005193 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005194 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005195 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196 * EVAL expr3c Push result of "expr3c"
5197 * end:
5198 */
5199 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005200compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005202 int ppconst_used = ppconst->pp_used;
5203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005205 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 return FAIL;
5207
5208 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005209 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210}
5211
5212/*
5213 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005215 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216 * JUMP_IF_FALSE alt jump if false
5217 * EVAL expr1a
5218 * JUMP_ALWAYS end
5219 * alt: EVAL expr1b
5220 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005221 *
5222 * Toplevel expression: expr2 ?? expr1
5223 * Produces instructions:
5224 * EVAL expr2 Push result of "expr2"
5225 * JUMP_AND_KEEP_IF_TRUE end jump if true
5226 * EVAL expr1
5227 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 */
5229 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005230compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231{
5232 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005233 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005234 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235
Bram Moolenaar3988f642020-08-27 22:43:03 +02005236 // Ignore all kinds of errors when not producing code.
5237 if (cctx->ctx_skip == SKIP_YES)
5238 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005239 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005240 return OK;
5241 }
5242
Bram Moolenaar61a89812020-05-07 16:58:17 +02005243 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005244 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 return FAIL;
5246
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005247 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248 if (*p == '?')
5249 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005250 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251 garray_T *instr = &cctx->ctx_instr;
5252 garray_T *stack = &cctx->ctx_type_stack;
5253 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005254 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005256 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005257 int has_const_expr = FALSE;
5258 int const_value = FALSE;
5259 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005260
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005261 if (next != NULL)
5262 {
5263 *arg = next_line_from_context(cctx, TRUE);
5264 p = skipwhite(*arg);
5265 }
5266
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005267 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005268 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005269 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005270 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005271 return FAIL;
5272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005273
Bram Moolenaara5565e42020-05-09 15:44:01 +02005274 if (ppconst->pp_used == ppconst_used + 1)
5275 {
5276 // the condition is a constant, we know whether the ? or the :
5277 // expression is to be evaluated.
5278 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005279 if (op_falsy)
5280 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5281 else
5282 {
5283 int error = FALSE;
5284
5285 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5286 &error);
5287 if (error)
5288 return FAIL;
5289 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005290 cctx->ctx_skip = save_skip == SKIP_YES ||
5291 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5292
5293 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5294 // "left ?? right" and "left" is truthy: produce "left"
5295 generate_ppconst(cctx, ppconst);
5296 else
5297 {
5298 clear_tv(&ppconst->pp_tv[ppconst_used]);
5299 --ppconst->pp_used;
5300 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005301 }
5302 else
5303 {
5304 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005305 if (op_falsy)
5306 end_idx = instr->ga_len;
5307 generate_JUMP(cctx, op_falsy
5308 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5309 if (op_falsy)
5310 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312
5313 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005314 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005315 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005316 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005317 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318
Bram Moolenaara5565e42020-05-09 15:44:01 +02005319 if (!has_const_expr)
5320 {
5321 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005322
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005323 if (!op_falsy)
5324 {
5325 // remember the type and drop it
5326 --stack->ga_len;
5327 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005329 end_idx = instr->ga_len;
5330 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005331
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005332 // jump here from JUMP_IF_FALSE
5333 isn = ((isn_T *)instr->ga_data) + alt_idx;
5334 isn->isn_arg.jump.jump_where = instr->ga_len;
5335 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005338 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005340 // Check for the ":".
5341 p = may_peek_next_line(cctx, *arg, &next);
5342 if (*p != ':')
5343 {
5344 emsg(_(e_missing_colon));
5345 return FAIL;
5346 }
5347 if (next != NULL)
5348 {
5349 *arg = next_line_from_context(cctx, TRUE);
5350 p = skipwhite(*arg);
5351 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005352
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005353 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5354 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005355 semsg(_(e_white_space_required_before_and_after_str_at_str),
5356 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005357 return FAIL;
5358 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005359
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005360 // evaluate the third expression
5361 if (has_const_expr)
5362 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005363 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005364 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005365 return FAIL;
5366 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5367 return FAIL;
5368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369
Bram Moolenaara5565e42020-05-09 15:44:01 +02005370 if (!has_const_expr)
5371 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005372 type_T **typep;
5373
Bram Moolenaara5565e42020-05-09 15:44:01 +02005374 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005375
Bram Moolenaara5565e42020-05-09 15:44:01 +02005376 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005377 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5378 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005379
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005380 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005381 isn = ((isn_T *)instr->ga_data) + end_idx;
5382 isn->isn_arg.jump.jump_where = instr->ga_len;
5383 }
5384
5385 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386 }
5387 return OK;
5388}
5389
5390/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005391 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005392 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5393 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005394 */
5395 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005396compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005397{
5398 ppconst_T ppconst;
5399
5400 CLEAR_FIELD(ppconst);
5401 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5402 {
5403 clear_ppconst(&ppconst);
5404 return FAIL;
5405 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005406 if (is_const != NULL)
5407 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005408 if (generate_ppconst(cctx, &ppconst) == FAIL)
5409 return FAIL;
5410 return OK;
5411}
5412
5413/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005414 * Toplevel expression.
5415 */
5416 static int
5417compile_expr0(char_u **arg, cctx_T *cctx)
5418{
5419 return compile_expr0_ext(arg, cctx, NULL);
5420}
5421
5422/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005423 * Compile "return [expr]".
5424 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005425 */
5426 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005427compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428{
5429 char_u *p = arg;
5430 garray_T *stack = &cctx->ctx_type_stack;
5431 type_T *stack_type;
5432
5433 if (*p != NUL && *p != '|' && *p != '\n')
5434 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005435 if (legacy)
5436 {
5437 int save_flags = cmdmod.cmod_flags;
5438
5439 generate_LEGACY_EVAL(cctx, p);
5440 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5441 0, cctx, FALSE, FALSE) == FAIL)
5442 return NULL;
5443 cmdmod.cmod_flags |= CMOD_LEGACY;
5444 (void)skip_expr(&p, NULL);
5445 cmdmod.cmod_flags = save_flags;
5446 }
5447 else
5448 {
5449 // compile return argument into instructions
5450 if (compile_expr0(&p, cctx) == FAIL)
5451 return NULL;
5452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005453
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005454 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005455 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005456 // "check_return_type" with uf_ret_type set to &t_unknown is used
5457 // for an inline function without a specified return type. Set the
5458 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005459 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005460 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005461 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5462 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005463 || (!check_return_type
5464 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005465 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005466 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005467 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005468 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005469 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005470 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5471 && stack_type->tt_type != VAR_VOID
5472 && stack_type->tt_type != VAR_UNKNOWN)
5473 {
5474 emsg(_(e_returning_value_in_function_without_return_type));
5475 return NULL;
5476 }
5477 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005478 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005479 return NULL;
5480 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482 }
5483 else
5484 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005485 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005486 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005487 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5488 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005490 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491 return NULL;
5492 }
5493
5494 // No argument, return zero.
5495 generate_PUSHNR(cctx, 0);
5496 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005497
5498 // Undo any command modifiers.
5499 generate_undo_cmdmods(cctx);
5500
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005501 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502 return NULL;
5503
5504 // "return val | endif" is possible
5505 return skipwhite(p);
5506}
5507
5508/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005509 * Get a line from the compilation context, compatible with exarg_T getline().
5510 * Return a pointer to the line in allocated memory.
5511 * Return NULL for end-of-file or some error.
5512 */
5513 static char_u *
5514exarg_getline(
5515 int c UNUSED,
5516 void *cookie,
5517 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005518 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005519{
5520 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005521 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005522
Bram Moolenaar66250c92020-08-20 15:02:42 +02005523 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005524 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005525 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005526 return NULL;
5527 ++cctx->ctx_lnum;
5528 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5529 // Comment lines result in NULL pointers, skip them.
5530 if (p != NULL)
5531 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005532 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005533}
5534
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005535 void
5536fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5537{
5538 eap->getline = exarg_getline;
5539 eap->cookie = cctx;
5540}
5541
Bram Moolenaar04b12692020-05-04 23:24:44 +02005542/*
5543 * Compile a nested :def command.
5544 */
5545 static char_u *
5546compile_nested_function(exarg_T *eap, cctx_T *cctx)
5547{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005548 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005549 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005550 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005551 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005552 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005553 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005554
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005555 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005556 {
5557 emsg(_(e_cannot_use_bang_with_nested_def));
5558 return NULL;
5559 }
5560
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005561 if (*name_start == '/')
5562 {
5563 name_end = skip_regexp(name_start + 1, '/', TRUE);
5564 if (*name_end == '/')
5565 ++name_end;
5566 eap->nextcmd = check_nextcmd(name_end);
5567 }
5568 if (name_end == name_start || *skipwhite(name_end) != '(')
5569 {
5570 if (!ends_excmd2(name_start, name_end))
5571 {
5572 semsg(_(e_invalid_command_str), eap->cmd);
5573 return NULL;
5574 }
5575
5576 // "def" or "def Name": list functions
5577 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5578 return NULL;
5579 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5580 }
5581
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005582 // Only g:Func() can use a namespace.
5583 if (name_start[1] == ':' && !is_global)
5584 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005585 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005586 return NULL;
5587 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005588 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005589 return NULL;
5590
Bram Moolenaar04b12692020-05-04 23:24:44 +02005591 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005592 fill_exarg_from_cctx(eap, cctx);
5593
Bram Moolenaar04b12692020-05-04 23:24:44 +02005594 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005595 lambda_name = vim_strsave(get_lambda_name());
5596 if (lambda_name == NULL)
5597 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005598 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005599
Bram Moolenaar822ba242020-05-24 23:00:18 +02005600 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005601 {
5602 r = eap->skip ? OK : FAIL;
5603 goto theend;
5604 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005605
5606 // copy over the block scope IDs before compiling
5607 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5608 {
5609 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5610
5611 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5612 if (ufunc->uf_block_ids != NULL)
5613 {
5614 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5615 sizeof(int) * block_depth);
5616 ufunc->uf_block_depth = block_depth;
5617 }
5618 }
5619
Bram Moolenaare99d4222021-06-13 14:01:26 +02005620 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
5621 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005622 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005623 {
5624 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005625 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005626 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005627
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005628 if (is_global)
5629 {
5630 char_u *func_name = vim_strnsave(name_start + 2,
5631 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005632
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005633 if (func_name == NULL)
5634 r = FAIL;
5635 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005636 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005637 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005638 lambda_name = NULL;
5639 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005640 }
5641 else
5642 {
5643 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005644 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005645 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005646
Bram Moolenaareef21022020-08-01 22:16:43 +02005647 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005648 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005649 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005650 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005651 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5652 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005653 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005654
5655theend:
5656 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005657 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005658}
5659
5660/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005661 * Return the length of an assignment operator, or zero if there isn't one.
5662 */
5663 int
5664assignment_len(char_u *p, int *heredoc)
5665{
5666 if (*p == '=')
5667 {
5668 if (p[1] == '<' && p[2] == '<')
5669 {
5670 *heredoc = TRUE;
5671 return 3;
5672 }
5673 return 1;
5674 }
5675 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5676 return 2;
5677 if (STRNCMP(p, "..=", 3) == 0)
5678 return 3;
5679 return 0;
5680}
5681
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005682/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005683 * Generate the load instruction for "name".
5684 */
5685 static void
5686generate_loadvar(
5687 cctx_T *cctx,
5688 assign_dest_T dest,
5689 char_u *name,
5690 lvar_T *lvar,
5691 type_T *type)
5692{
5693 switch (dest)
5694 {
5695 case dest_option:
5696 // TODO: check the option exists
5697 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5698 break;
5699 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005700 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5701 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5702 else
5703 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005704 break;
5705 case dest_buffer:
5706 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5707 break;
5708 case dest_window:
5709 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5710 break;
5711 case dest_tab:
5712 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5713 break;
5714 case dest_script:
5715 compile_load_scriptvar(cctx,
5716 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5717 break;
5718 case dest_env:
5719 // Include $ in the name here
5720 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5721 break;
5722 case dest_reg:
5723 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5724 break;
5725 case dest_vimvar:
5726 generate_LOADV(cctx, name + 2, TRUE);
5727 break;
5728 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005729 if (lvar->lv_from_outer > 0)
5730 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5731 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005732 else
5733 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5734 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005735 case dest_expr:
5736 // list or dict value should already be on the stack.
5737 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005738 }
5739}
5740
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005741/*
5742 * Skip over "[expr]" or ".member".
5743 * Does not check for any errors.
5744 */
5745 static char_u *
5746skip_index(char_u *start)
5747{
5748 char_u *p = start;
5749
5750 if (*p == '[')
5751 {
5752 p = skipwhite(p + 1);
5753 (void)skip_expr(&p, NULL);
5754 p = skipwhite(p);
5755 if (*p == ']')
5756 return p + 1;
5757 return p;
5758 }
5759 // if (*p == '.')
5760 return to_name_end(p + 1, TRUE);
5761}
5762
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005763 void
5764vim9_declare_error(char_u *name)
5765{
5766 char *scope = "";
5767
5768 switch (*name)
5769 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005770 case 'g': scope = _("global"); break;
5771 case 'b': scope = _("buffer"); break;
5772 case 'w': scope = _("window"); break;
5773 case 't': scope = _("tab"); break;
5774 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005775 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005776 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005777 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005778 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005779 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005780 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005781 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005782 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005783 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005784}
5785
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005786/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005787 * For one assignment figure out the type of destination. Return it in "dest".
5788 * When not recognized "dest" is not set.
5789 * For an option "opt_flags" is set.
5790 * For a v:var "vimvaridx" is set.
5791 * "type" is set to the destination type if known, unchanted otherwise.
5792 * Return FAIL if an error message was given.
5793 */
5794 static int
5795get_var_dest(
5796 char_u *name,
5797 assign_dest_T *dest,
5798 int cmdidx,
5799 int *opt_flags,
5800 int *vimvaridx,
5801 type_T **type,
5802 cctx_T *cctx)
5803{
5804 char_u *p;
5805
5806 if (*name == '&')
5807 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005808 int cc;
5809 long numval;
5810 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005811
5812 *dest = dest_option;
5813 if (cmdidx == CMD_final || cmdidx == CMD_const)
5814 {
5815 emsg(_(e_const_option));
5816 return FAIL;
5817 }
5818 p = name;
5819 p = find_option_end(&p, opt_flags);
5820 if (p == NULL)
5821 {
5822 // cannot happen?
5823 emsg(_(e_letunexp));
5824 return FAIL;
5825 }
5826 cc = *p;
5827 *p = NUL;
5828 opt_type = get_option_value(skip_option_env_lead(name),
5829 &numval, NULL, *opt_flags);
5830 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005831 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005832 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005833 case gov_unknown:
5834 semsg(_(e_unknown_option), name);
5835 return FAIL;
5836 case gov_string:
5837 case gov_hidden_string:
5838 *type = &t_string;
5839 break;
5840 case gov_bool:
5841 case gov_hidden_bool:
5842 *type = &t_bool;
5843 break;
5844 case gov_number:
5845 case gov_hidden_number:
5846 *type = &t_number;
5847 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005848 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005849 }
5850 else if (*name == '$')
5851 {
5852 *dest = dest_env;
5853 *type = &t_string;
5854 }
5855 else if (*name == '@')
5856 {
5857 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5858 {
5859 emsg_invreg(name[1]);
5860 return FAIL;
5861 }
5862 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005863 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005864 }
5865 else if (STRNCMP(name, "g:", 2) == 0)
5866 {
5867 *dest = dest_global;
5868 }
5869 else if (STRNCMP(name, "b:", 2) == 0)
5870 {
5871 *dest = dest_buffer;
5872 }
5873 else if (STRNCMP(name, "w:", 2) == 0)
5874 {
5875 *dest = dest_window;
5876 }
5877 else if (STRNCMP(name, "t:", 2) == 0)
5878 {
5879 *dest = dest_tab;
5880 }
5881 else if (STRNCMP(name, "v:", 2) == 0)
5882 {
5883 typval_T *vtv;
5884 int di_flags;
5885
5886 *vimvaridx = find_vim_var(name + 2, &di_flags);
5887 if (*vimvaridx < 0)
5888 {
5889 semsg(_(e_variable_not_found_str), name);
5890 return FAIL;
5891 }
5892 // We use the current value of "sandbox" here, is that OK?
5893 if (var_check_ro(di_flags, name, FALSE))
5894 return FAIL;
5895 *dest = dest_vimvar;
5896 vtv = get_vim_var_tv(*vimvaridx);
5897 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5898 }
5899 return OK;
5900}
5901
5902/*
5903 * Generate a STORE instruction for "dest", not being "dest_local".
5904 * Return FAIL when out of memory.
5905 */
5906 static int
5907generate_store_var(
5908 cctx_T *cctx,
5909 assign_dest_T dest,
5910 int opt_flags,
5911 int vimvaridx,
5912 int scriptvar_idx,
5913 int scriptvar_sid,
5914 type_T *type,
5915 char_u *name)
5916{
5917 switch (dest)
5918 {
5919 case dest_option:
5920 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5921 opt_flags);
5922 case dest_global:
5923 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005924 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5925 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005926 case dest_buffer:
5927 // include b: with the name, easier to execute that way
5928 return generate_STORE(cctx, ISN_STOREB, 0, name);
5929 case dest_window:
5930 // include w: with the name, easier to execute that way
5931 return generate_STORE(cctx, ISN_STOREW, 0, name);
5932 case dest_tab:
5933 // include t: with the name, easier to execute that way
5934 return generate_STORE(cctx, ISN_STORET, 0, name);
5935 case dest_env:
5936 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5937 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005938 return generate_STORE(cctx, ISN_STOREREG,
5939 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005940 case dest_vimvar:
5941 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5942 case dest_script:
5943 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005944 // "s:" may be included in the name.
5945 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005946 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005947 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5948 scriptvar_sid, scriptvar_idx, type);
5949 case dest_local:
5950 case dest_expr:
5951 // cannot happen
5952 break;
5953 }
5954 return FAIL;
5955}
5956
Bram Moolenaar752fc692021-01-04 21:57:11 +01005957 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005958generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5959{
5960 if (lhs->lhs_dest != dest_local)
5961 return generate_store_var(cctx, lhs->lhs_dest,
5962 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5963 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5964 lhs->lhs_type, lhs->lhs_name);
5965
5966 if (lhs->lhs_lvar != NULL)
5967 {
5968 garray_T *instr = &cctx->ctx_instr;
5969 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5970
5971 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5972 // ISN_STORENR
5973 if (lhs->lhs_lvar->lv_from_outer == 0
5974 && instr->ga_len == instr_count + 1
5975 && isn->isn_type == ISN_PUSHNR)
5976 {
5977 varnumber_T val = isn->isn_arg.number;
5978 garray_T *stack = &cctx->ctx_type_stack;
5979
5980 isn->isn_type = ISN_STORENR;
5981 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5982 isn->isn_arg.storenr.stnr_val = val;
5983 if (stack->ga_len > 0)
5984 --stack->ga_len;
5985 }
5986 else if (lhs->lhs_lvar->lv_from_outer > 0)
5987 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5988 lhs->lhs_lvar->lv_from_outer);
5989 else
5990 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5991 }
5992 return OK;
5993}
5994
5995 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01005996is_decl_command(int cmdidx)
5997{
5998 return cmdidx == CMD_let || cmdidx == CMD_var
5999 || cmdidx == CMD_final || cmdidx == CMD_const;
6000}
6001
6002/*
6003 * Figure out the LHS type and other properties for an assignment or one item
6004 * of ":unlet" with an index.
6005 * Returns OK or FAIL.
6006 */
6007 static int
6008compile_lhs(
6009 char_u *var_start,
6010 lhs_T *lhs,
6011 int cmdidx,
6012 int heredoc,
6013 int oplen,
6014 cctx_T *cctx)
6015{
6016 char_u *var_end;
6017 int is_decl = is_decl_command(cmdidx);
6018
6019 CLEAR_POINTER(lhs);
6020 lhs->lhs_dest = dest_local;
6021 lhs->lhs_vimvaridx = -1;
6022 lhs->lhs_scriptvar_idx = -1;
6023
6024 // "dest_end" is the end of the destination, including "[expr]" or
6025 // ".name".
6026 // "var_end" is the end of the variable/option/etc. name.
6027 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6028 if (*var_start == '@')
6029 var_end = var_start + 2;
6030 else
6031 {
6032 // skip over the leading "&", "&l:", "&g:" and "$"
6033 var_end = skip_option_env_lead(var_start);
6034 var_end = to_name_end(var_end, TRUE);
6035 }
6036
6037 // "a: type" is declaring variable "a" with a type, not dict "a:".
6038 if (is_decl && lhs->lhs_dest_end == var_start + 2
6039 && lhs->lhs_dest_end[-1] == ':')
6040 --lhs->lhs_dest_end;
6041 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6042 --var_end;
6043
6044 // compute the length of the destination without "[expr]" or ".name"
6045 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006046 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006047 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6048 if (lhs->lhs_name == NULL)
6049 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006050
6051 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6052 // Something follows after the variable: "var[idx]" or "var.key".
6053 lhs->lhs_has_index = TRUE;
6054
Bram Moolenaar752fc692021-01-04 21:57:11 +01006055 if (heredoc)
6056 lhs->lhs_type = &t_list_string;
6057 else
6058 lhs->lhs_type = &t_any;
6059
6060 if (cctx->ctx_skip != SKIP_YES)
6061 {
6062 int declare_error = FALSE;
6063
6064 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6065 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6066 &lhs->lhs_type, cctx) == FAIL)
6067 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006068 if (lhs->lhs_dest != dest_local
6069 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006070 {
6071 // Specific kind of variable recognized.
6072 declare_error = is_decl;
6073 }
6074 else
6075 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006076 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006077 if (check_reserved_name(lhs->lhs_name) == FAIL)
6078 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006079
6080 if (lookup_local(var_start, lhs->lhs_varlen,
6081 &lhs->lhs_local_lvar, cctx) == OK)
6082 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6083 else
6084 {
6085 CLEAR_FIELD(lhs->lhs_arg_lvar);
6086 if (arg_exists(var_start, lhs->lhs_varlen,
6087 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6088 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6089 {
6090 if (is_decl)
6091 {
6092 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6093 return FAIL;
6094 }
6095 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6096 }
6097 }
6098 if (lhs->lhs_lvar != NULL)
6099 {
6100 if (is_decl)
6101 {
6102 semsg(_(e_variable_already_declared), lhs->lhs_name);
6103 return FAIL;
6104 }
6105 }
6106 else
6107 {
6108 int script_namespace = lhs->lhs_varlen > 1
6109 && STRNCMP(var_start, "s:", 2) == 0;
6110 int script_var = (script_namespace
6111 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006112 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006113 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006114 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006115 imported_T *import =
6116 find_imported(var_start, lhs->lhs_varlen, cctx);
6117
6118 if (script_namespace || script_var || import != NULL)
6119 {
6120 char_u *rawname = lhs->lhs_name
6121 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6122
6123 if (is_decl)
6124 {
6125 if (script_namespace)
6126 semsg(_(e_cannot_declare_script_variable_in_function),
6127 lhs->lhs_name);
6128 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006129 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006130 lhs->lhs_name);
6131 return FAIL;
6132 }
6133 else if (cctx->ctx_ufunc->uf_script_ctx_version
6134 == SCRIPT_VERSION_VIM9
6135 && script_namespace
6136 && !script_var && import == NULL)
6137 {
6138 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6139 return FAIL;
6140 }
6141
6142 lhs->lhs_dest = dest_script;
6143
6144 // existing script-local variables should have a type
6145 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6146 if (import != NULL)
6147 lhs->lhs_scriptvar_sid = import->imp_sid;
6148 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6149 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006150 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006151 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006152 lhs->lhs_scriptvar_sid, rawname,
6153 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6154 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006155 if (lhs->lhs_scriptvar_idx >= 0)
6156 {
6157 scriptitem_T *si = SCRIPT_ITEM(
6158 lhs->lhs_scriptvar_sid);
6159 svar_T *sv =
6160 ((svar_T *)si->sn_var_vals.ga_data)
6161 + lhs->lhs_scriptvar_idx;
6162 lhs->lhs_type = sv->sv_type;
6163 }
6164 }
6165 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006166 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006167 == FAIL)
6168 return FAIL;
6169 }
6170 }
6171
6172 if (declare_error)
6173 {
6174 vim9_declare_error(lhs->lhs_name);
6175 return FAIL;
6176 }
6177 }
6178
6179 // handle "a:name" as a name, not index "name" on "a"
6180 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6181 var_end = lhs->lhs_dest_end;
6182
6183 if (lhs->lhs_dest != dest_option)
6184 {
6185 if (is_decl && *var_end == ':')
6186 {
6187 char_u *p;
6188
6189 // parse optional type: "let var: type = expr"
6190 if (!VIM_ISWHITE(var_end[1]))
6191 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006192 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006193 return FAIL;
6194 }
6195 p = skipwhite(var_end + 1);
6196 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6197 if (lhs->lhs_type == NULL)
6198 return FAIL;
6199 lhs->lhs_has_type = TRUE;
6200 }
6201 else if (lhs->lhs_lvar != NULL)
6202 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6203 }
6204
Bram Moolenaare42939a2021-04-05 17:11:17 +02006205 if (oplen == 3 && !heredoc
6206 && lhs->lhs_dest != dest_global
6207 && !lhs->lhs_has_index
6208 && lhs->lhs_type->tt_type != VAR_STRING
6209 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006210 {
6211 emsg(_(e_can_only_concatenate_to_string));
6212 return FAIL;
6213 }
6214
6215 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6216 && cctx->ctx_skip != SKIP_YES)
6217 {
6218 if (oplen > 1 && !heredoc)
6219 {
6220 // +=, /=, etc. require an existing variable
6221 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6222 return FAIL;
6223 }
6224 if (!is_decl)
6225 {
6226 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6227 return FAIL;
6228 }
6229
Bram Moolenaar3f327882021-03-17 20:56:38 +01006230 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006231 if ((lhs->lhs_type->tt_type == VAR_FUNC
6232 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006233 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006234 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006235
6236 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006237 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6238 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6239 if (lhs->lhs_lvar == NULL)
6240 return FAIL;
6241 lhs->lhs_new_local = TRUE;
6242 }
6243
6244 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006245 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006246 {
6247 // Something follows after the variable: "var[idx]" or "var.key".
6248 // TODO: should we also handle "->func()" here?
6249 if (is_decl)
6250 {
6251 emsg(_(e_cannot_use_index_when_declaring_variable));
6252 return FAIL;
6253 }
6254
6255 if (var_start[lhs->lhs_varlen] == '['
6256 || var_start[lhs->lhs_varlen] == '.')
6257 {
6258 char_u *after = var_start + lhs->lhs_varlen;
6259 char_u *p;
6260
6261 // Only the last index is used below, if there are others
6262 // before it generate code for the expression. Thus for
6263 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6264 for (;;)
6265 {
6266 p = skip_index(after);
6267 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006268 {
6269 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006270 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006271 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006272 after = p;
6273 }
6274 if (after > var_start + lhs->lhs_varlen)
6275 {
6276 lhs->lhs_varlen = after - var_start;
6277 lhs->lhs_dest = dest_expr;
6278 // We don't know the type before evaluating the expression,
6279 // use "any" until then.
6280 lhs->lhs_type = &t_any;
6281 }
6282
Bram Moolenaar752fc692021-01-04 21:57:11 +01006283 if (lhs->lhs_type->tt_member == NULL)
6284 lhs->lhs_member_type = &t_any;
6285 else
6286 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6287 }
6288 else
6289 {
6290 semsg("Not supported yet: %s", var_start);
6291 return FAIL;
6292 }
6293 }
6294 return OK;
6295}
6296
6297/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006298 * Figure out the LHS and check a few errors.
6299 */
6300 static int
6301compile_assign_lhs(
6302 char_u *var_start,
6303 lhs_T *lhs,
6304 int cmdidx,
6305 int is_decl,
6306 int heredoc,
6307 int oplen,
6308 cctx_T *cctx)
6309{
6310 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6311 return FAIL;
6312
6313 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6314 {
6315 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6316 return FAIL;
6317 }
6318 if (!is_decl && lhs->lhs_lvar != NULL
6319 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6320 {
6321 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6322 return FAIL;
6323 }
6324 return OK;
6325}
6326
6327/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006328 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6329 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006330 */
6331 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006332compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006333 char_u *var_start,
6334 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006335 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006336 cctx_T *cctx)
6337{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006338 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006339 char_u *p;
6340 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006341 int need_white_before = TRUE;
6342 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006343
Bram Moolenaar752fc692021-01-04 21:57:11 +01006344 p = var_start + varlen;
6345 if (*p == '[')
6346 {
6347 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006348 if (*p == ':')
6349 {
6350 // empty first index, push zero
6351 r = generate_PUSHNR(cctx, 0);
6352 need_white_before = FALSE;
6353 }
6354 else
6355 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006356
6357 if (r == OK && *skipwhite(p) == ':')
6358 {
6359 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006360 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006361 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006362 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006363 empty_second = *skipwhite(p + 1) == ']';
6364 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6365 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006366 {
6367 semsg(_(e_white_space_required_before_and_after_str_at_str),
6368 ":", p);
6369 return FAIL;
6370 }
6371 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006372 if (*p == ']')
6373 // empty second index, push "none"
6374 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6375 else
6376 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006377 }
6378
Bram Moolenaar752fc692021-01-04 21:57:11 +01006379 if (r == OK && *skipwhite(p) != ']')
6380 {
6381 // this should not happen
6382 emsg(_(e_missbrac));
6383 r = FAIL;
6384 }
6385 }
6386 else // if (*p == '.')
6387 {
6388 char_u *key_end = to_name_end(p + 1, TRUE);
6389 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6390
6391 r = generate_PUSHS(cctx, key);
6392 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006393 return r;
6394}
6395
6396/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006397 * For a LHS with an index, load the variable to be indexed.
6398 */
6399 static int
6400compile_load_lhs(
6401 lhs_T *lhs,
6402 char_u *var_start,
6403 type_T *rhs_type,
6404 cctx_T *cctx)
6405{
6406 if (lhs->lhs_dest == dest_expr)
6407 {
6408 size_t varlen = lhs->lhs_varlen;
6409 int c = var_start[varlen];
6410 char_u *p = var_start;
6411 garray_T *stack = &cctx->ctx_type_stack;
6412
6413 // Evaluate "ll[expr]" of "ll[expr][idx]"
6414 var_start[varlen] = NUL;
6415 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6416 {
6417 // this should not happen
6418 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006419 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006420 return FAIL;
6421 }
6422 var_start[varlen] = c;
6423
6424 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6425 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6426 // now we can properly check the type
6427 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6428 && rhs_type != &t_void
6429 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6430 FALSE, FALSE) == FAIL)
6431 return FAIL;
6432 }
6433 else
6434 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6435 lhs->lhs_lvar, lhs->lhs_type);
6436 return OK;
6437}
6438
6439/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006440 * Produce code for loading "lhs" and also take care of an index.
6441 * Return OK/FAIL.
6442 */
6443 static int
6444compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6445{
6446 compile_load_lhs(lhs, var_start, NULL, cctx);
6447
6448 if (lhs->lhs_has_index)
6449 {
6450 int range = FALSE;
6451
6452 // Get member from list or dict. First compile the
6453 // index value.
6454 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6455 return FAIL;
6456 if (range)
6457 {
6458 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6459 var_start);
6460 return FAIL;
6461 }
6462
6463 // Get the member.
6464 if (compile_member(FALSE, cctx) == FAIL)
6465 return FAIL;
6466 }
6467 return OK;
6468}
6469
6470/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006471 * Assignment to a list or dict member, or ":unlet" for the item, using the
6472 * information in "lhs".
6473 * Returns OK or FAIL.
6474 */
6475 static int
6476compile_assign_unlet(
6477 char_u *var_start,
6478 lhs_T *lhs,
6479 int is_assign,
6480 type_T *rhs_type,
6481 cctx_T *cctx)
6482{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006483 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006484 garray_T *stack = &cctx->ctx_type_stack;
6485 int range = FALSE;
6486
Bram Moolenaar68452172021-04-12 21:21:02 +02006487 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006488 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006489 if (is_assign && range && lhs->lhs_type != &t_blob
6490 && lhs->lhs_type != &t_any)
6491 {
6492 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6493 return FAIL;
6494 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006495
6496 if (lhs->lhs_type == &t_any)
6497 {
6498 // Index on variable of unknown type: check at runtime.
6499 dest_type = VAR_ANY;
6500 }
6501 else
6502 {
6503 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006504 if (dest_type == VAR_DICT && range)
6505 {
6506 emsg(e_cannot_use_range_with_dictionary);
6507 return FAIL;
6508 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006509 if (dest_type == VAR_DICT
6510 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006511 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006512 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006513 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006514 type_T *type;
6515
6516 if (range)
6517 {
6518 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6519 if (need_type(type, &t_number,
6520 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006521 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006522 }
6523 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6524 if ((dest_type != VAR_BLOB || type != &t_special)
6525 && need_type(type, &t_number,
6526 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006527 return FAIL;
6528 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006529 }
6530
6531 // Load the dict or list. On the stack we then have:
6532 // - value (for assignment, not for :unlet)
6533 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006534 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006535 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006536 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6537 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006538
Bram Moolenaar68452172021-04-12 21:21:02 +02006539 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6540 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006541 {
6542 if (is_assign)
6543 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006544 if (range)
6545 {
6546 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6547 return FAIL;
6548 }
6549 else
6550 {
6551 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006552
Bram Moolenaar68452172021-04-12 21:21:02 +02006553 if (isn == NULL)
6554 return FAIL;
6555 isn->isn_arg.vartype = dest_type;
6556 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006557 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006558 else if (range)
6559 {
6560 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6561 return FAIL;
6562 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006563 else
6564 {
6565 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6566 return FAIL;
6567 }
6568 }
6569 else
6570 {
6571 emsg(_(e_indexable_type_required));
6572 return FAIL;
6573 }
6574
6575 return OK;
6576}
6577
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006578/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006579 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006580 * "let name"
6581 * "var name = expr"
6582 * "final name = expr"
6583 * "const name = expr"
6584 * "name = expr"
6585 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006586 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006587 * Return NULL for an error.
6588 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589 */
6590 static char_u *
6591compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6592{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006593 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006595 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596 char_u *ret = NULL;
6597 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006598 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006601 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 int oplen = 0;
6604 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006605 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006606 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006608 int is_decl = is_decl_command(cmdidx);
6609 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006610 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006612 // Skip over the "var" or "[var, var]" to get to any "=".
6613 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6614 if (p == NULL)
6615 return *arg == '[' ? arg : NULL;
6616
6617 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006618 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006619 // TODO: should we allow this, and figure out type inference from list
6620 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006621 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 return NULL;
6623 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006624 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626 sp = p;
6627 p = skipwhite(p);
6628 op = p;
6629 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006630
6631 if (var_count > 0 && oplen == 0)
6632 // can be something like "[1, 2]->func()"
6633 return arg;
6634
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006635 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006637 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006638 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006639 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006640 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6641 {
6642 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6643 oplen = 2;
6644 incdec = TRUE;
6645 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006647 if (heredoc)
6648 {
6649 list_T *l;
6650 listitem_T *li;
6651
6652 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006653 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006655 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006656 if (l == NULL)
6657 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006658
Bram Moolenaar078269b2020-09-21 20:35:55 +02006659 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006661 // Push each line and the create the list.
6662 FOR_ALL_LIST_ITEMS(l, li)
6663 {
6664 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6665 li->li_tv.vval.v_string = NULL;
6666 }
6667 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006668 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669 list_free(l);
6670 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006671 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006672 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006673 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006675 char_u *wp;
6676
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006677 // for "[var, var] = expr" evaluate the expression here, loop over the
6678 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006679 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006680
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006681 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006682 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006683 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006684 if (compile_expr0(&p, cctx) == FAIL)
6685 return NULL;
6686 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006688 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006689 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006690 type_T *stacktype;
6691
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006692 stacktype = stack->ga_len == 0 ? &t_void
6693 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006694 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006696 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006697 goto theend;
6698 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006699 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006700 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006701 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006702 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006703 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6704 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006705 if (stacktype->tt_member != NULL)
6706 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006707 }
6708 }
6709
6710 /*
6711 * Loop over variables in "[var, var] = expr".
6712 * For "var = expr" and "let var: type" this is done only once.
6713 */
6714 if (var_count > 0)
6715 var_start = skipwhite(arg + 1); // skip over the "["
6716 else
6717 var_start = arg;
6718 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6719 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006720 int instr_count = -1;
6721
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006722 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6723 {
6724 // Ignore underscore in "[a, _, b] = list".
6725 if (var_count > 0)
6726 {
6727 var_start = skipwhite(var_start + 2);
6728 continue;
6729 }
6730 emsg(_(e_cannot_use_underscore_here));
6731 goto theend;
6732 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006733 vim_free(lhs.lhs_name);
6734
6735 /*
6736 * Figure out the LHS type and other properties.
6737 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006738 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6739 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006740 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006741 if (!heredoc)
6742 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006743 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006744 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006745 if (oplen > 0 && var_count == 0)
6746 {
6747 // skip over the "=" and the expression
6748 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006749 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006750 }
6751 }
6752 else if (oplen > 0)
6753 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006754 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006755 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006756
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006757 // for "+=", "*=", "..=" etc. first load the current value
6758 if (*op != '='
6759 && compile_load_lhs_with_index(&lhs, var_start,
6760 cctx) == FAIL)
6761 goto theend;
6762
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006763 // For "var = expr" evaluate the expression.
6764 if (var_count == 0)
6765 {
6766 int r;
6767
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006768 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006769 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006770 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006771 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006772 r = generate_PUSHNR(cctx, 1);
6773 }
6774 else
6775 {
6776 // Temporarily hide the new local variable here, it is
6777 // not available to this expression.
6778 if (lhs.lhs_new_local)
6779 --cctx->ctx_locals.ga_len;
6780 wp = op + oplen;
6781 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6782 {
6783 if (lhs.lhs_new_local)
6784 ++cctx->ctx_locals.ga_len;
6785 goto theend;
6786 }
6787 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006788 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006789 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006790 if (r == FAIL)
6791 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006792 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006793 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006794 else if (semicolon && var_idx == var_count - 1)
6795 {
6796 // For "[var; var] = expr" get the rest of the list
6797 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6798 goto theend;
6799 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006800 else
6801 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006802 // For "[var, var] = expr" get the "var_idx" item from the
6803 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006804 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006805 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006806 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006807
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006808 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006809 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006810 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006811 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006812 if ((rhs_type->tt_type == VAR_FUNC
6813 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006814 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006815 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006816 goto theend;
6817
Bram Moolenaar752fc692021-01-04 21:57:11 +01006818 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006819 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006820 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006821 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006822 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006823 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006824 }
6825 else
6826 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006827 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006828 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006829 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006830 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006831 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006832 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006833 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006834 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006835 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006836 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006837 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006838 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006839 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006840 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006841 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006842
Bram Moolenaar77709b12021-04-03 21:01:01 +02006843 // Without operator check type here, otherwise below.
6844 // Use the line number of the assignment.
6845 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006846 if (lhs.lhs_has_index)
6847 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006848 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006849 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006850 goto theend;
6851 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006852 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006853 else
6854 {
6855 type_T *lhs_type = lhs.lhs_member_type;
6856
6857 // Special case: assigning to @# can use a number or a
6858 // string.
6859 if (lhs_type == &t_number_or_string
6860 && rhs_type->tt_type == VAR_NUMBER)
6861 lhs_type = &t_number;
6862 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006863 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006864 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006866 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006867 else if (cmdidx == CMD_final)
6868 {
6869 emsg(_(e_final_requires_a_value));
6870 goto theend;
6871 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006872 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006873 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006874 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006875 goto theend;
6876 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006877 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006878 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006879 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006880 goto theend;
6881 }
6882 else
6883 {
6884 // variables are always initialized
6885 if (ga_grow(instr, 1) == FAIL)
6886 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006887 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006888 {
6889 case VAR_BOOL:
6890 generate_PUSHBOOL(cctx, VVAL_FALSE);
6891 break;
6892 case VAR_FLOAT:
6893#ifdef FEAT_FLOAT
6894 generate_PUSHF(cctx, 0.0);
6895#endif
6896 break;
6897 case VAR_STRING:
6898 generate_PUSHS(cctx, NULL);
6899 break;
6900 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006901 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006902 break;
6903 case VAR_FUNC:
6904 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6905 break;
6906 case VAR_LIST:
6907 generate_NEWLIST(cctx, 0);
6908 break;
6909 case VAR_DICT:
6910 generate_NEWDICT(cctx, 0);
6911 break;
6912 case VAR_JOB:
6913 generate_PUSHJOB(cctx, NULL);
6914 break;
6915 case VAR_CHANNEL:
6916 generate_PUSHCHANNEL(cctx, NULL);
6917 break;
6918 case VAR_NUMBER:
6919 case VAR_UNKNOWN:
6920 case VAR_ANY:
6921 case VAR_PARTIAL:
6922 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006923 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006924 case VAR_SPECIAL: // cannot happen
6925 generate_PUSHNR(cctx, 0);
6926 break;
6927 }
6928 }
6929 if (var_count == 0)
6930 end = p;
6931 }
6932
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006933 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006934 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006935 break;
6936
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006937 if (oplen > 0 && *op != '=')
6938 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006939 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006940 type_T *stacktype;
6941
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006942 if (*op == '.')
6943 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006944 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006945 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006946 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006947 if (
6948#ifdef FEAT_FLOAT
6949 // If variable is float operation with number is OK.
6950 !(expected == &t_float && stacktype == &t_number) &&
6951#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006952 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006953 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006954 goto theend;
6955
6956 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006957 {
6958 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6959 goto theend;
6960 }
6961 else if (*op == '+')
6962 {
6963 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006964 operator_type(lhs.lhs_member_type, stacktype),
6965 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006966 goto theend;
6967 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006968 else if (generate_two_op(cctx, op) == FAIL)
6969 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971
Bram Moolenaar752fc692021-01-04 21:57:11 +01006972 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006973 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006974 // Use the info in "lhs" to store the value at the index in the
6975 // list or dict.
6976 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6977 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006978 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006979 }
6980 else
6981 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006982 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006983 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006984 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006985 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006986 generate_LOCKCONST(cctx);
6987
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006988 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006989 && (lhs.lhs_type->tt_type == VAR_DICT
6990 || lhs.lhs_type->tt_type == VAR_LIST)
6991 && lhs.lhs_type->tt_member != NULL
6992 && lhs.lhs_type->tt_member != &t_any
6993 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006994 // Set the type in the list or dict, so that it can be checked,
6995 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006996 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006997
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006998 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
6999 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007000 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007001
7002 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007003 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007004 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007005
7006 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02007007 if (var_count > 0 && !semicolon)
7008 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007009 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007010 goto theend;
7011 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007012
Bram Moolenaarb2097502020-07-19 17:17:02 +02007013 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014
7015theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007016 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017 return ret;
7018}
7019
7020/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007021 * Check for an assignment at "eap->cmd", compile it if found.
7022 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7023 */
7024 static int
7025may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7026{
7027 char_u *pskip;
7028 char_u *p;
7029
7030 // Assuming the command starts with a variable or function name,
7031 // find what follows.
7032 // Skip over "var.member", "var[idx]" and the like.
7033 // Also "&opt = val", "$ENV = val" and "@r = val".
7034 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7035 ? eap->cmd + 1 : eap->cmd;
7036 p = to_name_end(pskip, TRUE);
7037 if (p > eap->cmd && *p != NUL)
7038 {
7039 char_u *var_end;
7040 int oplen;
7041 int heredoc;
7042
7043 if (eap->cmd[0] == '@')
7044 var_end = eap->cmd + 2;
7045 else
7046 var_end = find_name_end(pskip, NULL, NULL,
7047 FNE_CHECK_START | FNE_INCL_BR);
7048 oplen = assignment_len(skipwhite(var_end), &heredoc);
7049 if (oplen > 0)
7050 {
7051 size_t len = p - eap->cmd;
7052
7053 // Recognize an assignment if we recognize the variable
7054 // name:
7055 // "g:var = expr"
7056 // "local = expr" where "local" is a local var.
7057 // "script = expr" where "script" is a script-local var.
7058 // "import = expr" where "import" is an imported var
7059 // "&opt = expr"
7060 // "$ENV = expr"
7061 // "@r = expr"
7062 if (*eap->cmd == '&'
7063 || *eap->cmd == '$'
7064 || *eap->cmd == '@'
7065 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007066 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007067 {
7068 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7069 if (*line == NULL || *line == eap->cmd)
7070 return FAIL;
7071 return OK;
7072 }
7073 }
7074 }
7075
7076 if (*eap->cmd == '[')
7077 {
7078 // [var, var] = expr
7079 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7080 if (*line == NULL)
7081 return FAIL;
7082 if (*line != eap->cmd)
7083 return OK;
7084 }
7085 return NOTDONE;
7086}
7087
7088/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007089 * Check if "name" can be "unlet".
7090 */
7091 int
7092check_vim9_unlet(char_u *name)
7093{
7094 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7095 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007096 // "unlet s:var" is allowed in legacy script.
7097 if (*name == 's' && !script_is_vim9())
7098 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007099 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007100 return FAIL;
7101 }
7102 return OK;
7103}
7104
7105/*
7106 * Callback passed to ex_unletlock().
7107 */
7108 static int
7109compile_unlet(
7110 lval_T *lvp,
7111 char_u *name_end,
7112 exarg_T *eap,
7113 int deep UNUSED,
7114 void *coookie)
7115{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007116 cctx_T *cctx = coookie;
7117 char_u *p = lvp->ll_name;
7118 int cc = *name_end;
7119 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007120
Bram Moolenaar752fc692021-01-04 21:57:11 +01007121 if (cctx->ctx_skip == SKIP_YES)
7122 return OK;
7123
7124 *name_end = NUL;
7125 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007126 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007127 // :unlet $ENV_VAR
7128 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7129 }
7130 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7131 {
7132 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007133
Bram Moolenaar752fc692021-01-04 21:57:11 +01007134 // This is similar to assigning: lookup the list/dict, compile the
7135 // idx/key. Then instead of storing the value unlet the item.
7136 // unlet {list}[idx]
7137 // unlet {dict}[key] dict.key
7138 //
7139 // Figure out the LHS type and other properties.
7140 //
7141 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7142
7143 // : unlet an indexed item
7144 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007145 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007146 iemsg("called compile_lhs() without an index");
7147 ret = FAIL;
7148 }
7149 else
7150 {
7151 // Use the info in "lhs" to unlet the item at the index in the
7152 // list or dict.
7153 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007154 }
7155
Bram Moolenaar752fc692021-01-04 21:57:11 +01007156 vim_free(lhs.lhs_name);
7157 }
7158 else if (check_vim9_unlet(p) == FAIL)
7159 {
7160 ret = FAIL;
7161 }
7162 else
7163 {
7164 // Normal name. Only supports g:, w:, t: and b: namespaces.
7165 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007166 }
7167
Bram Moolenaar752fc692021-01-04 21:57:11 +01007168 *name_end = cc;
7169 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007170}
7171
7172/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007173 * Callback passed to ex_unletlock().
7174 */
7175 static int
7176compile_lock_unlock(
7177 lval_T *lvp,
7178 char_u *name_end,
7179 exarg_T *eap,
7180 int deep UNUSED,
7181 void *coookie)
7182{
7183 cctx_T *cctx = coookie;
7184 int cc = *name_end;
7185 char_u *p = lvp->ll_name;
7186 int ret = OK;
7187 size_t len;
7188 char_u *buf;
7189
7190 if (cctx->ctx_skip == SKIP_YES)
7191 return OK;
7192
7193 // Cannot use :lockvar and :unlockvar on local variables.
7194 if (p[1] != ':')
7195 {
7196 char_u *end = skip_var_one(p, FALSE);
7197
7198 if (lookup_local(p, end - p, NULL, cctx) == OK)
7199 {
7200 emsg(_(e_cannot_lock_unlock_local_variable));
7201 return FAIL;
7202 }
7203 }
7204
7205 // Checking is done at runtime.
7206 *name_end = NUL;
7207 len = name_end - p + 20;
7208 buf = alloc(len);
7209 if (buf == NULL)
7210 ret = FAIL;
7211 else
7212 {
7213 vim_snprintf((char *)buf, len, "%s %s",
7214 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7215 p);
7216 ret = generate_EXEC(cctx, buf);
7217
7218 vim_free(buf);
7219 *name_end = cc;
7220 }
7221 return ret;
7222}
7223
7224/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007225 * compile "unlet var", "lock var" and "unlock var"
7226 * "arg" points to "var".
7227 */
7228 static char_u *
7229compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7230{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007231 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7232 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7233 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007234 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7235}
7236
7237/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007238 * Compile an :import command.
7239 */
7240 static char_u *
7241compile_import(char_u *arg, cctx_T *cctx)
7242{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007243 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244}
7245
7246/*
7247 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7248 */
7249 static int
7250compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7251{
7252 garray_T *instr = &cctx->ctx_instr;
7253 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7254
7255 if (endlabel == NULL)
7256 return FAIL;
7257 endlabel->el_next = *el;
7258 *el = endlabel;
7259 endlabel->el_end_label = instr->ga_len;
7260
7261 generate_JUMP(cctx, when, 0);
7262 return OK;
7263}
7264
7265 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007266compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007267{
7268 garray_T *instr = &cctx->ctx_instr;
7269
7270 while (*el != NULL)
7271 {
7272 endlabel_T *cur = (*el);
7273 isn_T *isn;
7274
7275 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007276 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 *el = cur->el_next;
7278 vim_free(cur);
7279 }
7280}
7281
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007282 static void
7283compile_free_jump_to_end(endlabel_T **el)
7284{
7285 while (*el != NULL)
7286 {
7287 endlabel_T *cur = (*el);
7288
7289 *el = cur->el_next;
7290 vim_free(cur);
7291 }
7292}
7293
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007294/*
7295 * Create a new scope and set up the generic items.
7296 */
7297 static scope_T *
7298new_scope(cctx_T *cctx, scopetype_T type)
7299{
7300 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7301
7302 if (scope == NULL)
7303 return NULL;
7304 scope->se_outer = cctx->ctx_scope;
7305 cctx->ctx_scope = scope;
7306 scope->se_type = type;
7307 scope->se_local_count = cctx->ctx_locals.ga_len;
7308 return scope;
7309}
7310
7311/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007312 * Free the current scope and go back to the outer scope.
7313 */
7314 static void
7315drop_scope(cctx_T *cctx)
7316{
7317 scope_T *scope = cctx->ctx_scope;
7318
7319 if (scope == NULL)
7320 {
7321 iemsg("calling drop_scope() without a scope");
7322 return;
7323 }
7324 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007325 switch (scope->se_type)
7326 {
7327 case IF_SCOPE:
7328 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7329 case FOR_SCOPE:
7330 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7331 case WHILE_SCOPE:
7332 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7333 case TRY_SCOPE:
7334 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7335 case NO_SCOPE:
7336 case BLOCK_SCOPE:
7337 break;
7338 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007339 vim_free(scope);
7340}
7341
7342/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007343 * compile "if expr"
7344 *
7345 * "if expr" Produces instructions:
7346 * EVAL expr Push result of "expr"
7347 * JUMP_IF_FALSE end
7348 * ... body ...
7349 * end:
7350 *
7351 * "if expr | else" Produces instructions:
7352 * EVAL expr Push result of "expr"
7353 * JUMP_IF_FALSE else
7354 * ... body ...
7355 * JUMP_ALWAYS end
7356 * else:
7357 * ... body ...
7358 * end:
7359 *
7360 * "if expr1 | elseif expr2 | else" Produces instructions:
7361 * EVAL expr Push result of "expr"
7362 * JUMP_IF_FALSE elseif
7363 * ... body ...
7364 * JUMP_ALWAYS end
7365 * elseif:
7366 * EVAL expr Push result of "expr"
7367 * JUMP_IF_FALSE else
7368 * ... body ...
7369 * JUMP_ALWAYS end
7370 * else:
7371 * ... body ...
7372 * end:
7373 */
7374 static char_u *
7375compile_if(char_u *arg, cctx_T *cctx)
7376{
7377 char_u *p = arg;
7378 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007379 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007381 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007382 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383
Bram Moolenaara5565e42020-05-09 15:44:01 +02007384 CLEAR_FIELD(ppconst);
7385 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007386 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007387 clear_ppconst(&ppconst);
7388 return NULL;
7389 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007390 if (!ends_excmd2(arg, skipwhite(p)))
7391 {
7392 semsg(_(e_trailing_arg), p);
7393 return NULL;
7394 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007395 if (cctx->ctx_skip == SKIP_YES)
7396 clear_ppconst(&ppconst);
7397 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007398 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007399 int error = FALSE;
7400 int v;
7401
Bram Moolenaara5565e42020-05-09 15:44:01 +02007402 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007403 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007404 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007405 if (error)
7406 return NULL;
7407 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007408 }
7409 else
7410 {
7411 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007412 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007413 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007414 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007415 if (bool_on_stack(cctx) == FAIL)
7416 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007417 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007418
Bram Moolenaara91a7132021-03-25 21:12:15 +01007419 // CMDMOD_REV must come before the jump
7420 generate_undo_cmdmods(cctx);
7421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422 scope = new_scope(cctx, IF_SCOPE);
7423 if (scope == NULL)
7424 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007425 scope->se_skip_save = skip_save;
7426 // "is_had_return" will be reset if any block does not end in :return
7427 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007429 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007430 {
7431 // "where" is set when ":elseif", "else" or ":endif" is found
7432 scope->se_u.se_if.is_if_label = instr->ga_len;
7433 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7434 }
7435 else
7436 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007437
Bram Moolenaarced68a02021-01-24 17:53:47 +01007438#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007439 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007440 && skip_save != SKIP_YES)
7441 {
7442 // generated a profile start, need to generate a profile end, since it
7443 // won't be done after returning
7444 cctx->ctx_skip = SKIP_NOT;
7445 generate_instr(cctx, ISN_PROF_END);
7446 cctx->ctx_skip = SKIP_YES;
7447 }
7448#endif
7449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 return p;
7451}
7452
7453 static char_u *
7454compile_elseif(char_u *arg, cctx_T *cctx)
7455{
7456 char_u *p = arg;
7457 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007458 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459 isn_T *isn;
7460 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007461 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007462 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463
7464 if (scope == NULL || scope->se_type != IF_SCOPE)
7465 {
7466 emsg(_(e_elseif_without_if));
7467 return NULL;
7468 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007469 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007470 if (!cctx->ctx_had_return)
7471 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472
Bram Moolenaarced68a02021-01-24 17:53:47 +01007473 if (cctx->ctx_skip == SKIP_NOT)
7474 {
7475 // previous block was executed, this one and following will not
7476 cctx->ctx_skip = SKIP_YES;
7477 scope->se_u.se_if.is_seen_skip_not = TRUE;
7478 }
7479 if (scope->se_u.se_if.is_seen_skip_not)
7480 {
7481 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007482 // Do not count the "elseif" for profiling and cmdmod
7483 instr->ga_len = current_instr_idx(cctx);
7484
Bram Moolenaarced68a02021-01-24 17:53:47 +01007485 skip_expr_cctx(&p, cctx);
7486 return p;
7487 }
7488
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007489 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007490 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007491 int moved_cmdmod = FALSE;
7492
7493 // Move any CMDMOD instruction to after the jump
7494 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7495 {
7496 if (ga_grow(instr, 1) == FAIL)
7497 return NULL;
7498 ((isn_T *)instr->ga_data)[instr->ga_len] =
7499 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7500 --instr->ga_len;
7501 moved_cmdmod = TRUE;
7502 }
7503
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007504 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007505 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007506 return NULL;
7507 // previous "if" or "elseif" jumps here
7508 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7509 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007510 if (moved_cmdmod)
7511 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007512 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007514 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007515 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007516 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007517 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007518 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007519#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007520 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007521 {
7522 // the previous block was skipped, need to profile this line
7523 generate_instr(cctx, ISN_PROF_START);
7524 instr_count = instr->ga_len;
7525 }
7526#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007527 if (cctx->ctx_compile_type == CT_DEBUG)
7528 {
7529 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007530 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007531 instr_count = instr->ga_len;
7532 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007533 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007534 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007535 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007536 clear_ppconst(&ppconst);
7537 return NULL;
7538 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007539 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007540 if (!ends_excmd2(arg, skipwhite(p)))
7541 {
7542 semsg(_(e_trailing_arg), p);
7543 return NULL;
7544 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007545 if (scope->se_skip_save == SKIP_YES)
7546 clear_ppconst(&ppconst);
7547 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007548 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007549 int error = FALSE;
7550 int v;
7551
Bram Moolenaar7f141552020-05-09 17:35:53 +02007552 // The expression results in a constant.
7553 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007554 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7555 if (error)
7556 return NULL;
7557 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007558 clear_ppconst(&ppconst);
7559 scope->se_u.se_if.is_if_label = -1;
7560 }
7561 else
7562 {
7563 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007564 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007565 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007566 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007567 if (bool_on_stack(cctx) == FAIL)
7568 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007569
Bram Moolenaara91a7132021-03-25 21:12:15 +01007570 // CMDMOD_REV must come before the jump
7571 generate_undo_cmdmods(cctx);
7572
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007573 // "where" is set when ":elseif", "else" or ":endif" is found
7574 scope->se_u.se_if.is_if_label = instr->ga_len;
7575 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7576 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007577
7578 return p;
7579}
7580
7581 static char_u *
7582compile_else(char_u *arg, cctx_T *cctx)
7583{
7584 char_u *p = arg;
7585 garray_T *instr = &cctx->ctx_instr;
7586 isn_T *isn;
7587 scope_T *scope = cctx->ctx_scope;
7588
7589 if (scope == NULL || scope->se_type != IF_SCOPE)
7590 {
7591 emsg(_(e_else_without_if));
7592 return NULL;
7593 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007594 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007595 if (!cctx->ctx_had_return)
7596 scope->se_u.se_if.is_had_return = FALSE;
7597 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007598
Bram Moolenaarced68a02021-01-24 17:53:47 +01007599#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007600 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007601 {
7602 if (cctx->ctx_skip == SKIP_NOT
7603 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7604 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007605 // the previous block was executed, do not count "else" for
7606 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007607 --instr->ga_len;
7608 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7609 {
7610 // the previous block was not executed, this one will, do count the
7611 // "else" for profiling
7612 cctx->ctx_skip = SKIP_NOT;
7613 generate_instr(cctx, ISN_PROF_END);
7614 generate_instr(cctx, ISN_PROF_START);
7615 cctx->ctx_skip = SKIP_YES;
7616 }
7617 }
7618#endif
7619
7620 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007621 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007622 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007623 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007624 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007625 if (!cctx->ctx_had_return
7626 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7627 JUMP_ALWAYS, cctx) == FAIL)
7628 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007629 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007630
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007631 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007632 {
7633 if (scope->se_u.se_if.is_if_label >= 0)
7634 {
7635 // previous "if" or "elseif" jumps here
7636 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7637 isn->isn_arg.jump.jump_where = instr->ga_len;
7638 scope->se_u.se_if.is_if_label = -1;
7639 }
7640 }
7641
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007642 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007643 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7644 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645
7646 return p;
7647}
7648
7649 static char_u *
7650compile_endif(char_u *arg, cctx_T *cctx)
7651{
7652 scope_T *scope = cctx->ctx_scope;
7653 ifscope_T *ifscope;
7654 garray_T *instr = &cctx->ctx_instr;
7655 isn_T *isn;
7656
Bram Moolenaarfa984412021-03-25 22:15:28 +01007657 if (misplaced_cmdmod(cctx))
7658 return NULL;
7659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660 if (scope == NULL || scope->se_type != IF_SCOPE)
7661 {
7662 emsg(_(e_endif_without_if));
7663 return NULL;
7664 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007665 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007666 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007667 if (!cctx->ctx_had_return)
7668 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007669
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007670 if (scope->se_u.se_if.is_if_label >= 0)
7671 {
7672 // previous "if" or "elseif" jumps here
7673 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7674 isn->isn_arg.jump.jump_where = instr->ga_len;
7675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007676 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007677 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007678
7679#ifdef FEAT_PROFILE
7680 // even when skipping we count the endif as executed, unless the block it's
7681 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007682 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007683 && scope->se_skip_save != SKIP_YES)
7684 {
7685 cctx->ctx_skip = SKIP_NOT;
7686 generate_instr(cctx, ISN_PROF_START);
7687 }
7688#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007689 cctx->ctx_skip = scope->se_skip_save;
7690
7691 // If all the blocks end in :return and there is an :else then the
7692 // had_return flag is set.
7693 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007695 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007696 return arg;
7697}
7698
7699/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007700 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007701 *
7702 * Produces instructions:
7703 * PUSHNR -1
7704 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007705 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007706 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7707 * - if beyond end, jump to "end"
7708 * - otherwise get item from list and push it
7709 * STORE var Store item in "var"
7710 * ... body ...
7711 * JUMP top Jump back to repeat
7712 * end: DROP Drop the result of "expr"
7713 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007714 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7715 * UNPACK 2 Split item in 2
7716 * STORE var1 Store item in "var1"
7717 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007718 */
7719 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007720compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007721{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007722 char_u *arg;
7723 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007724 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007726 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007727 int var_count = 0;
7728 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007729 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007730 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007731 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007732 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007733 lvar_T *loop_lvar; // loop iteration variable
7734 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007736 type_T *item_type = &t_any;
7737 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007738
Bram Moolenaar792f7862020-11-23 08:31:18 +01007739 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007740 if (p == NULL)
7741 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007742 if (var_count == 0)
7743 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007744
7745 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007746 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007747 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7748 return NULL;
7749 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007750 {
7751 emsg(_(e_missing_in));
7752 return NULL;
7753 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007754 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007755 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7756 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007757
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007758 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7759 // instruction.
7760 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7761 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7762 .isn_type == ISN_DEBUG)
7763 --instr->ga_len;
7764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007765 scope = new_scope(cctx, FOR_SCOPE);
7766 if (scope == NULL)
7767 return NULL;
7768
Bram Moolenaar792f7862020-11-23 08:31:18 +01007769 // Reserve a variable to store the loop iteration counter and initialize it
7770 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007771 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7772 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007773 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007774 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007775 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007776 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007777 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007778 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779
7780 // compile "expr", it remains on the stack until "endfor"
7781 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007782 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007783 {
7784 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007786 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007787 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007788
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007789 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007790 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007791 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007792 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007793 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007794 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007795 semsg(_(e_for_loop_on_str_not_supported),
7796 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007797 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798 return NULL;
7799 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007800
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007801 if (vartype->tt_type == VAR_STRING)
7802 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007803 else if (vartype->tt_type == VAR_BLOB)
7804 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007805 else if (vartype->tt_type == VAR_LIST
7806 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007807 {
7808 if (var_count == 1)
7809 item_type = vartype->tt_member;
7810 else if (vartype->tt_member->tt_type == VAR_LIST
7811 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007812 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007813 item_type = vartype->tt_member->tt_member;
7814 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007816 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007817 generate_undo_cmdmods(cctx);
7818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007819 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007820 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007821
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007822 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007823
Bram Moolenaar792f7862020-11-23 08:31:18 +01007824 arg = arg_start;
7825 if (var_count > 1)
7826 {
7827 generate_UNPACK(cctx, var_count, semicolon);
7828 arg = skipwhite(arg + 1); // skip white after '['
7829
7830 // the list item is replaced by a number of items
7831 if (ga_grow(stack, var_count - 1) == FAIL)
7832 {
7833 drop_scope(cctx);
7834 return NULL;
7835 }
7836 --stack->ga_len;
7837 for (idx = 0; idx < var_count; ++idx)
7838 {
7839 ((type_T **)stack->ga_data)[stack->ga_len] =
7840 (semicolon && idx == 0) ? vartype : item_type;
7841 ++stack->ga_len;
7842 }
7843 }
7844
7845 for (idx = 0; idx < var_count; ++idx)
7846 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007847 assign_dest_T dest = dest_local;
7848 int opt_flags = 0;
7849 int vimvaridx = -1;
7850 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007851 type_T *lhs_type = &t_any;
7852 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007853
7854 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007855 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007856 name = vim_strnsave(arg, varlen);
7857 if (name == NULL)
7858 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007859 if (*p == ':')
7860 {
7861 p = skipwhite(p + 1);
7862 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7863 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007864
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007865 // TODO: script var not supported?
7866 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7867 &vimvaridx, &type, cctx) == FAIL)
7868 goto failed;
7869 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007870 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007871 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7872 0, 0, type, name) == FAIL)
7873 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007874 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02007875 else if (varlen == 1 && *arg == '_')
7876 {
7877 // Assigning to "_": drop the value.
7878 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7879 goto failed;
7880 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007881 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007882 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007883 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007884 {
7885 semsg(_(e_variable_already_declared), arg);
7886 goto failed;
7887 }
7888
Bram Moolenaarea870692020-12-02 14:24:30 +01007889 if (STRNCMP(name, "s:", 2) == 0)
7890 {
7891 semsg(_(e_cannot_declare_script_variable_in_function), name);
7892 goto failed;
7893 }
7894
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007895 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007896 where.wt_index = var_count > 1 ? idx + 1 : 0;
7897 where.wt_variable = TRUE;
7898 if (lhs_type == &t_any)
7899 lhs_type = item_type;
7900 else if (item_type != &t_unknown
7901 && !(var_count > 1 && item_type == &t_any)
7902 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7903 goto failed;
7904 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007905 if (var_lvar == NULL)
7906 // out of memory or used as an argument
7907 goto failed;
7908
7909 if (semicolon && idx == var_count - 1)
7910 var_lvar->lv_type = vartype;
7911 else
7912 var_lvar->lv_type = item_type;
7913 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7914 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007915
7916 if (*p == ',' || *p == ';')
7917 ++p;
7918 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007919 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007920 }
7921
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007922 if (cctx->ctx_compile_type == CT_DEBUG)
7923 // Add ISN_DEBUG here, so that the loop variables can be inspected.
7924 generate_instr_debug(cctx);
7925
Bram Moolenaar792f7862020-11-23 08:31:18 +01007926 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007927
7928failed:
7929 vim_free(name);
7930 drop_scope(cctx);
7931 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932}
7933
7934/*
7935 * compile "endfor"
7936 */
7937 static char_u *
7938compile_endfor(char_u *arg, cctx_T *cctx)
7939{
7940 garray_T *instr = &cctx->ctx_instr;
7941 scope_T *scope = cctx->ctx_scope;
7942 forscope_T *forscope;
7943 isn_T *isn;
7944
Bram Moolenaarfa984412021-03-25 22:15:28 +01007945 if (misplaced_cmdmod(cctx))
7946 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007948 if (scope == NULL || scope->se_type != FOR_SCOPE)
7949 {
7950 emsg(_(e_for));
7951 return NULL;
7952 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007953 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007955 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956
7957 // At end of ":for" scope jump back to the FOR instruction.
7958 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7959
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007960 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007961 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7962 isn->isn_arg.forloop.for_end = instr->ga_len;
7963
7964 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007965 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007966
7967 // Below the ":for" scope drop the "expr" list from the stack.
7968 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7969 return NULL;
7970
7971 vim_free(scope);
7972
7973 return arg;
7974}
7975
7976/*
7977 * compile "while expr"
7978 *
7979 * Produces instructions:
7980 * top: EVAL expr Push result of "expr"
7981 * JUMP_IF_FALSE end jump if false
7982 * ... body ...
7983 * JUMP top Jump back to repeat
7984 * end:
7985 *
7986 */
7987 static char_u *
7988compile_while(char_u *arg, cctx_T *cctx)
7989{
7990 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007991 scope_T *scope;
7992
7993 scope = new_scope(cctx, WHILE_SCOPE);
7994 if (scope == NULL)
7995 return NULL;
7996
Bram Moolenaara91a7132021-03-25 21:12:15 +01007997 // "endwhile" jumps back here, one before when profiling or using cmdmods
7998 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007999
8000 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008001 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008002 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008003 if (!ends_excmd2(arg, skipwhite(p)))
8004 {
8005 semsg(_(e_trailing_arg), p);
8006 return NULL;
8007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008008
Bram Moolenaar13106602020-10-04 16:06:05 +02008009 if (bool_on_stack(cctx) == FAIL)
8010 return FAIL;
8011
Bram Moolenaara91a7132021-03-25 21:12:15 +01008012 // CMDMOD_REV must come before the jump
8013 generate_undo_cmdmods(cctx);
8014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008015 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008016 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008017 JUMP_IF_FALSE, cctx) == FAIL)
8018 return FAIL;
8019
8020 return p;
8021}
8022
8023/*
8024 * compile "endwhile"
8025 */
8026 static char_u *
8027compile_endwhile(char_u *arg, cctx_T *cctx)
8028{
8029 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008030 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008031
Bram Moolenaarfa984412021-03-25 22:15:28 +01008032 if (misplaced_cmdmod(cctx))
8033 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008034 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8035 {
8036 emsg(_(e_while));
8037 return NULL;
8038 }
8039 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008040 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008041
Bram Moolenaarf002a412021-01-24 13:34:18 +01008042#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008043 // count the endwhile before jumping
8044 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008045#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008047 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008048 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008049
8050 // Fill in the "end" label in the WHILE statement so it can jump here.
8051 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008052 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8053 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008054
8055 vim_free(scope);
8056
8057 return arg;
8058}
8059
8060/*
8061 * compile "continue"
8062 */
8063 static char_u *
8064compile_continue(char_u *arg, cctx_T *cctx)
8065{
8066 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008067 int try_scopes = 0;
8068 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008069
8070 for (;;)
8071 {
8072 if (scope == NULL)
8073 {
8074 emsg(_(e_continue));
8075 return NULL;
8076 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008077 if (scope->se_type == FOR_SCOPE)
8078 {
8079 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008080 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008081 }
8082 if (scope->se_type == WHILE_SCOPE)
8083 {
8084 loop_label = scope->se_u.se_while.ws_top_label;
8085 break;
8086 }
8087 if (scope->se_type == TRY_SCOPE)
8088 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008089 scope = scope->se_outer;
8090 }
8091
Bram Moolenaarc150c092021-02-13 15:02:46 +01008092 if (try_scopes > 0)
8093 // Inside one or more try/catch blocks we first need to jump to the
8094 // "finally" or "endtry" to cleanup.
8095 generate_TRYCONT(cctx, try_scopes, loop_label);
8096 else
8097 // Jump back to the FOR or WHILE instruction.
8098 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008100 return arg;
8101}
8102
8103/*
8104 * compile "break"
8105 */
8106 static char_u *
8107compile_break(char_u *arg, cctx_T *cctx)
8108{
8109 scope_T *scope = cctx->ctx_scope;
8110 endlabel_T **el;
8111
8112 for (;;)
8113 {
8114 if (scope == NULL)
8115 {
8116 emsg(_(e_break));
8117 return NULL;
8118 }
8119 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8120 break;
8121 scope = scope->se_outer;
8122 }
8123
8124 // Jump to the end of the FOR or WHILE loop.
8125 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008126 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008127 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008128 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8130 return FAIL;
8131
8132 return arg;
8133}
8134
8135/*
8136 * compile "{" start of block
8137 */
8138 static char_u *
8139compile_block(char_u *arg, cctx_T *cctx)
8140{
8141 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8142 return NULL;
8143 return skipwhite(arg + 1);
8144}
8145
8146/*
8147 * compile end of block: drop one scope
8148 */
8149 static void
8150compile_endblock(cctx_T *cctx)
8151{
8152 scope_T *scope = cctx->ctx_scope;
8153
8154 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008155 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008156 vim_free(scope);
8157}
8158
8159/*
8160 * compile "try"
8161 * Creates a new scope for the try-endtry, pointing to the first catch and
8162 * finally.
8163 * Creates another scope for the "try" block itself.
8164 * TRY instruction sets up exception handling at runtime.
8165 *
8166 * "try"
8167 * TRY -> catch1, -> finally push trystack entry
8168 * ... try block
8169 * "throw {exception}"
8170 * EVAL {exception}
8171 * THROW create exception
8172 * ... try block
8173 * " catch {expr}"
8174 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008175 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008176 * EVAL {expr}
8177 * MATCH
8178 * JUMP nomatch -> catch2
8179 * CATCH remove exception
8180 * ... catch block
8181 * " catch"
8182 * JUMP -> finally
8183 * catch2: CATCH remove exception
8184 * ... catch block
8185 * " finally"
8186 * finally:
8187 * ... finally block
8188 * " endtry"
8189 * ENDTRY pop trystack entry, may rethrow
8190 */
8191 static char_u *
8192compile_try(char_u *arg, cctx_T *cctx)
8193{
8194 garray_T *instr = &cctx->ctx_instr;
8195 scope_T *try_scope;
8196 scope_T *scope;
8197
Bram Moolenaarfa984412021-03-25 22:15:28 +01008198 if (misplaced_cmdmod(cctx))
8199 return NULL;
8200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008201 // scope that holds the jumps that go to catch/finally/endtry
8202 try_scope = new_scope(cctx, TRY_SCOPE);
8203 if (try_scope == NULL)
8204 return NULL;
8205
Bram Moolenaar69f70502021-01-01 16:10:46 +01008206 if (cctx->ctx_skip != SKIP_YES)
8207 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008208 isn_T *isn;
8209
8210 // "try_catch" is set when the first ":catch" is found or when no catch
8211 // is found and ":finally" is found.
8212 // "try_finally" is set when ":finally" is found
8213 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008214 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008215 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8216 return NULL;
8217 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8218 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008219 return NULL;
8220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008221
8222 // scope for the try block itself
8223 scope = new_scope(cctx, BLOCK_SCOPE);
8224 if (scope == NULL)
8225 return NULL;
8226
8227 return arg;
8228}
8229
8230/*
8231 * compile "catch {expr}"
8232 */
8233 static char_u *
8234compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8235{
8236 scope_T *scope = cctx->ctx_scope;
8237 garray_T *instr = &cctx->ctx_instr;
8238 char_u *p;
8239 isn_T *isn;
8240
Bram Moolenaarfa984412021-03-25 22:15:28 +01008241 if (misplaced_cmdmod(cctx))
8242 return NULL;
8243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008244 // end block scope from :try or :catch
8245 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8246 compile_endblock(cctx);
8247 scope = cctx->ctx_scope;
8248
8249 // Error if not in a :try scope
8250 if (scope == NULL || scope->se_type != TRY_SCOPE)
8251 {
8252 emsg(_(e_catch));
8253 return NULL;
8254 }
8255
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008256 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008257 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008258 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008259 return NULL;
8260 }
8261
Bram Moolenaar69f70502021-01-01 16:10:46 +01008262 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008263 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008264#ifdef FEAT_PROFILE
8265 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008266 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008267 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008268 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008269 .isn_type == ISN_PROF_START)
8270 --instr->ga_len;
8271#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008272 // Jump from end of previous block to :finally or :endtry
8273 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8274 JUMP_ALWAYS, cctx) == FAIL)
8275 return NULL;
8276
8277 // End :try or :catch scope: set value in ISN_TRY instruction
8278 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008279 if (isn->isn_arg.try.try_ref->try_catch == 0)
8280 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008281 if (scope->se_u.se_try.ts_catch_label != 0)
8282 {
8283 // Previous catch without match jumps here
8284 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8285 isn->isn_arg.jump.jump_where = instr->ga_len;
8286 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008287#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008288 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008289 {
8290 // a "throw" that jumps here needs to be counted
8291 generate_instr(cctx, ISN_PROF_END);
8292 // the "catch" is also counted
8293 generate_instr(cctx, ISN_PROF_START);
8294 }
8295#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008296 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008297 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008298 }
8299
8300 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008301 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008302 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008303 scope->se_u.se_try.ts_caught_all = TRUE;
8304 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008305 }
8306 else
8307 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008308 char_u *end;
8309 char_u *pat;
8310 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008311 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008312 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008314 // Push v:exception, push {expr} and MATCH
8315 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8316
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008317 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008318 if (*end != *p)
8319 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008320 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008321 vim_free(tofree);
8322 return FAIL;
8323 }
8324 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008325 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008326 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008327 len = (int)(end - tofree);
8328 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008329 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008330 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008331 if (pat == NULL)
8332 return FAIL;
8333 if (generate_PUSHS(cctx, pat) == FAIL)
8334 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008336 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8337 return NULL;
8338
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008339 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008340 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8341 return NULL;
8342 }
8343
Bram Moolenaar69f70502021-01-01 16:10:46 +01008344 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008345 return NULL;
8346
8347 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8348 return NULL;
8349 return p;
8350}
8351
8352 static char_u *
8353compile_finally(char_u *arg, cctx_T *cctx)
8354{
8355 scope_T *scope = cctx->ctx_scope;
8356 garray_T *instr = &cctx->ctx_instr;
8357 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008358 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008359
Bram Moolenaarfa984412021-03-25 22:15:28 +01008360 if (misplaced_cmdmod(cctx))
8361 return NULL;
8362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008363 // end block scope from :try or :catch
8364 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8365 compile_endblock(cctx);
8366 scope = cctx->ctx_scope;
8367
8368 // Error if not in a :try scope
8369 if (scope == NULL || scope->se_type != TRY_SCOPE)
8370 {
8371 emsg(_(e_finally));
8372 return NULL;
8373 }
8374
8375 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008376 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008377 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008378 {
8379 emsg(_(e_finally_dup));
8380 return NULL;
8381 }
8382
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008383 this_instr = instr->ga_len;
8384#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008385 if (cctx->ctx_compile_type == CT_PROFILE
8386 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008387 .isn_type == ISN_PROF_START)
8388 // jump to the profile start of the "finally"
8389 --this_instr;
8390#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008391
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008392 // Fill in the "end" label in jumps at the end of the blocks.
8393 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8394 this_instr, cctx);
8395
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008396 // If there is no :catch then an exception jumps to :finally.
8397 if (isn->isn_arg.try.try_ref->try_catch == 0)
8398 isn->isn_arg.try.try_ref->try_catch = this_instr;
8399 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008400 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008401 {
8402 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008403 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008404 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008405 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008406 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008407 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8408 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008410 // TODO: set index in ts_finally_label jumps
8411
8412 return arg;
8413}
8414
8415 static char_u *
8416compile_endtry(char_u *arg, cctx_T *cctx)
8417{
8418 scope_T *scope = cctx->ctx_scope;
8419 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008420 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008421
Bram Moolenaarfa984412021-03-25 22:15:28 +01008422 if (misplaced_cmdmod(cctx))
8423 return NULL;
8424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008425 // end block scope from :catch or :finally
8426 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8427 compile_endblock(cctx);
8428 scope = cctx->ctx_scope;
8429
8430 // Error if not in a :try scope
8431 if (scope == NULL || scope->se_type != TRY_SCOPE)
8432 {
8433 if (scope == NULL)
8434 emsg(_(e_no_endtry));
8435 else if (scope->se_type == WHILE_SCOPE)
8436 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008437 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008438 emsg(_(e_endfor));
8439 else
8440 emsg(_(e_endif));
8441 return NULL;
8442 }
8443
Bram Moolenaarc150c092021-02-13 15:02:46 +01008444 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008445 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008446 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008447 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8448 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008449 {
8450 emsg(_(e_missing_catch_or_finally));
8451 return NULL;
8452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008453
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008454#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008455 if (cctx->ctx_compile_type == CT_PROFILE
8456 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008457 .isn_type == ISN_PROF_START)
8458 // move the profile start after "endtry" so that it's not counted when
8459 // the exception is rethrown.
8460 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008461#endif
8462
Bram Moolenaar69f70502021-01-01 16:10:46 +01008463 // Fill in the "end" label in jumps at the end of the blocks, if not
8464 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008465 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8466 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008467
Bram Moolenaar69f70502021-01-01 16:10:46 +01008468 if (scope->se_u.se_try.ts_catch_label != 0)
8469 {
8470 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008471 isn_T *isn = ((isn_T *)instr->ga_data)
8472 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008473 isn->isn_arg.jump.jump_where = instr->ga_len;
8474 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008475 }
8476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008477 compile_endblock(cctx);
8478
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008479 if (cctx->ctx_skip != SKIP_YES)
8480 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008481 // End :catch or :finally scope: set instruction index in ISN_TRY
8482 // instruction
8483 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008484 if (cctx->ctx_skip != SKIP_YES
8485 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8486 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008487#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008488 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008489 generate_instr(cctx, ISN_PROF_START);
8490#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008491 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008492 return arg;
8493}
8494
8495/*
8496 * compile "throw {expr}"
8497 */
8498 static char_u *
8499compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8500{
8501 char_u *p = skipwhite(arg);
8502
Bram Moolenaara5565e42020-05-09 15:44:01 +02008503 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008504 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008505 if (cctx->ctx_skip == SKIP_YES)
8506 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008507 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008508 return NULL;
8509 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8510 return NULL;
8511
8512 return p;
8513}
8514
8515/*
8516 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008517 * compile "echomsg expr"
8518 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008519 * compile "execute expr"
8520 */
8521 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008522compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008523{
8524 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008525 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008526 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008527 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008528 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008529 garray_T *stack = &cctx->ctx_type_stack;
8530 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008531
8532 for (;;)
8533 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008534 if (ends_excmd2(prev, p))
8535 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008536 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008537 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008538 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008539
8540 if (cctx->ctx_skip != SKIP_YES)
8541 {
8542 // check for non-void type
8543 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8544 if (type->tt_type == VAR_VOID)
8545 {
8546 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8547 return NULL;
8548 }
8549 }
8550
Bram Moolenaarad39c092020-02-26 18:23:43 +01008551 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008552 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008553 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008554 }
8555
Bram Moolenaare4984292020-12-13 14:19:25 +01008556 if (count > 0)
8557 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008558 long save_lnum = cctx->ctx_lnum;
8559
8560 // Use the line number where the command started.
8561 cctx->ctx_lnum = start_ctx_lnum;
8562
Bram Moolenaare4984292020-12-13 14:19:25 +01008563 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8564 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8565 else if (cmdidx == CMD_execute)
8566 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8567 else if (cmdidx == CMD_echomsg)
8568 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8569 else
8570 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008571
8572 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008574 return p;
8575}
8576
8577/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008578 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008579 * instruction to compute it and return OK.
8580 * Otherwise return FAIL, the caller must deal with any range.
8581 */
8582 static int
8583compile_variable_range(exarg_T *eap, cctx_T *cctx)
8584{
8585 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8586 char_u *p = skipdigits(eap->cmd);
8587
8588 if (p == range_end)
8589 return FAIL;
8590 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8591}
8592
8593/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008594 * :put r
8595 * :put ={expr}
8596 */
8597 static char_u *
8598compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8599{
8600 char_u *line = arg;
8601 linenr_T lnum;
8602 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008603 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008604
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008605 eap->regname = *line;
8606
8607 if (eap->regname == '=')
8608 {
8609 char_u *p = line + 1;
8610
8611 if (compile_expr0(&p, cctx) == FAIL)
8612 return NULL;
8613 line = p;
8614 }
8615 else if (eap->regname != NUL)
8616 ++line;
8617
Bram Moolenaar08597872020-12-10 19:43:40 +01008618 if (compile_variable_range(eap, cctx) == OK)
8619 {
8620 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8621 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008622 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008623 {
8624 // Either no range or a number.
8625 // "errormsg" will not be set because the range is ADDR_LINES.
8626 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008627 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008628 return NULL;
8629 if (eap->addr_count == 0)
8630 lnum = -1;
8631 else
8632 lnum = eap->line2;
8633 if (above)
8634 --lnum;
8635 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008636
8637 generate_PUT(cctx, eap->regname, lnum);
8638 return line;
8639}
8640
8641/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008642 * A command that is not compiled, execute with legacy code.
8643 */
8644 static char_u *
8645compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8646{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008647 char_u *p;
8648 int has_expr = FALSE;
8649 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008650
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008651 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008652 goto theend;
8653
Bram Moolenaare729ce22021-06-06 21:38:09 +02008654 // If there was a prececing command modifier, drop it and include it in the
8655 // EXEC command.
8656 if (cctx->ctx_has_cmdmod)
8657 {
8658 garray_T *instr = &cctx->ctx_instr;
8659 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8660
8661 if (isn->isn_type == ISN_CMDMOD)
8662 {
8663 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8664 ->cmod_filter_regmatch.regprog);
8665 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8666 --instr->ga_len;
8667 cctx->ctx_has_cmdmod = FALSE;
8668 }
8669 }
8670
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008671 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008672 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008673 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008674 int usefilter = FALSE;
8675
8676 has_expr = argt & (EX_XFILE | EX_EXPAND);
8677
8678 // If the command can be followed by a bar, find the bar and truncate
8679 // it, so that the following command can be compiled.
8680 // The '|' is overwritten with a NUL, it is put back below.
8681 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8682 && *eap->arg == '!')
8683 // :w !filter or :r !filter or :r! filter
8684 usefilter = TRUE;
8685 if ((argt & EX_TRLBAR) && !usefilter)
8686 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008687 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008688 separate_nextcmd(eap);
8689 if (eap->nextcmd != NULL)
8690 nextcmd = eap->nextcmd;
8691 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008692 else if (eap->cmdidx == CMD_wincmd)
8693 {
8694 p = eap->arg;
8695 if (*p != NUL)
8696 ++p;
8697 if (*p == 'g' || *p == Ctrl_G)
8698 ++p;
8699 p = skipwhite(p);
8700 if (*p == '|')
8701 {
8702 *p = NUL;
8703 nextcmd = p + 1;
8704 }
8705 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008706 }
8707
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008708 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8709 {
8710 // expand filename in "syntax include [@group] filename"
8711 has_expr = TRUE;
8712 eap->arg = skipwhite(eap->arg + 7);
8713 if (*eap->arg == '@')
8714 eap->arg = skiptowhite(eap->arg);
8715 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008716
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008717 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8718 && STRLEN(eap->arg) > 4)
8719 {
8720 int delim = *eap->arg;
8721
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008722 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008723 if (*p == delim)
8724 {
8725 eap->arg = p + 1;
8726 has_expr = TRUE;
8727 }
8728 }
8729
Bram Moolenaarecac5912021-01-05 19:23:28 +01008730 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8731 {
8732 // TODO: should only expand when appropriate for the command
8733 eap->arg = skiptowhite(eap->arg);
8734 has_expr = TRUE;
8735 }
8736
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008737 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008738 {
8739 int count = 0;
8740 char_u *start = skipwhite(line);
8741
8742 // :cmd xxx`=expr1`yyy`=expr2`zzz
8743 // PUSHS ":cmd xxx"
8744 // eval expr1
8745 // PUSHS "yyy"
8746 // eval expr2
8747 // PUSHS "zzz"
8748 // EXECCONCAT 5
8749 for (;;)
8750 {
8751 if (p > start)
8752 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008753 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008754 ++count;
8755 }
8756 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008757 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008758 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008759 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008760 ++count;
8761 p = skipwhite(p);
8762 if (*p != '`')
8763 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008764 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008765 return NULL;
8766 }
8767 start = p + 1;
8768
8769 p = (char_u *)strstr((char *)start, "`=");
8770 if (p == NULL)
8771 {
8772 if (*skipwhite(start) != NUL)
8773 {
8774 generate_PUSHS(cctx, vim_strsave(start));
8775 ++count;
8776 }
8777 break;
8778 }
8779 }
8780 generate_EXECCONCAT(cctx, count);
8781 }
8782 else
8783 generate_EXEC(cctx, line);
8784
8785theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008786 if (*nextcmd != NUL)
8787 {
8788 // the parser expects a pointer to the bar, put it back
8789 --nextcmd;
8790 *nextcmd = '|';
8791 }
8792
8793 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008794}
8795
Bram Moolenaar20677332021-06-06 17:02:53 +02008796/*
8797 * A script command with heredoc, e.g.
8798 * ruby << EOF
8799 * command
8800 * EOF
8801 * Has been turned into one long line with NL characters by
8802 * get_function_body():
8803 * ruby << EOF<NL> command<NL>EOF
8804 */
8805 static char_u *
8806compile_script(char_u *line, cctx_T *cctx)
8807{
8808 if (cctx->ctx_skip != SKIP_YES)
8809 {
8810 isn_T *isn;
8811
8812 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8813 return NULL;
8814 isn->isn_arg.string = vim_strsave(line);
8815 }
8816 return (char_u *)"";
8817}
8818
Bram Moolenaar8238f082021-04-20 21:10:48 +02008819
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008820/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008821 * :s/pat/repl/
8822 */
8823 static char_u *
8824compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8825{
8826 char_u *cmd = eap->arg;
8827 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8828
8829 if (expr != NULL)
8830 {
8831 int delimiter = *cmd++;
8832
8833 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02008834 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008835 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8836 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008837 garray_T save_ga = cctx->ctx_instr;
8838 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008839 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008840 int trailing_error;
8841 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008842 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008843 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008844
8845 cmd += 3;
8846 end = skip_substitute(cmd, delimiter);
8847
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008848 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02008849 // labels are correct.
8850 cctx->ctx_instr.ga_len = 0;
8851 cctx->ctx_instr.ga_maxlen = 0;
8852 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02008853 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008854 if (end[-1] == NUL)
8855 end[-1] = delimiter;
8856 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02008857 trailing_error = *cmd != delimiter && *cmd != NUL;
8858
Bram Moolenaar169502f2021-04-21 12:19:35 +02008859 if (expr_res == FAIL || trailing_error
8860 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02008861 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02008862 if (trailing_error)
8863 semsg(_(e_trailing_arg), cmd);
8864 clear_instr_ga(&cctx->ctx_instr);
8865 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008866 return NULL;
8867 }
8868
Bram Moolenaar8238f082021-04-20 21:10:48 +02008869 // Move the generated instructions into the ISN_SUBSTITUTE
8870 // instructions, then restore the list of instructions before
8871 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02008872 instr_count = cctx->ctx_instr.ga_len;
8873 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008874 instr[instr_count].isn_type = ISN_FINISH;
8875
8876 cctx->ctx_instr = save_ga;
8877 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8878 {
8879 int idx;
8880
8881 for (idx = 0; idx < instr_count; ++idx)
8882 delete_instr(instr + idx);
8883 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02008884 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02008885 }
8886 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8887 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02008888
8889 // skip over flags
8890 if (*end == '&')
8891 ++end;
8892 while (ASCII_ISALPHA(*end) || *end == '#')
8893 ++end;
8894 return end;
8895 }
8896 }
8897
8898 return compile_exec(arg, eap, cctx);
8899}
8900
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008901 static char_u *
8902compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8903{
8904 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008905 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008906
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008907 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008908 {
8909 if (STRNCMP(arg, "END", 3) == 0)
8910 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008911 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008912 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02008913 // First load the current variable value.
8914 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8915 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008916 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008917 }
8918
8919 // Gets the redirected text and put it on the stack, then store it
8920 // in the variable.
8921 generate_instr_type(cctx, ISN_REDIREND, &t_string);
8922
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008923 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008924 generate_instr_drop(cctx, ISN_CONCAT, 1);
8925
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008926 if (lhs->lhs_has_index)
8927 {
8928 // Use the info in "lhs" to store the value at the index in the
8929 // list or dict.
8930 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8931 &t_string, cctx) == FAIL)
8932 return NULL;
8933 }
8934 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008935 return NULL;
8936
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008937 VIM_CLEAR(lhs->lhs_name);
8938 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008939 return arg + 3;
8940 }
8941 emsg(_(e_cannot_nest_redir));
8942 return NULL;
8943 }
8944
8945 if (arg[0] == '=' && arg[1] == '>')
8946 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008947 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008948
8949 // redirect to a variable is compiled
8950 arg += 2;
8951 if (*arg == '>')
8952 {
8953 ++arg;
8954 append = TRUE;
8955 }
8956 arg = skipwhite(arg);
8957
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008958 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008959 FALSE, FALSE, 1, cctx) == FAIL)
8960 return NULL;
8961 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008962 lhs->lhs_append = append;
8963 if (lhs->lhs_has_index)
8964 {
8965 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8966 if (lhs->lhs_whole == NULL)
8967 return NULL;
8968 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008969
Bram Moolenaar753bcf82021-04-21 14:24:24 +02008970 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02008971 }
8972
8973 // other redirects are handled like at script level
8974 return compile_exec(line, eap, cctx);
8975}
8976
Bram Moolenaarb7c97812021-05-05 22:51:39 +02008977#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008978 static char_u *
8979compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8980{
8981 isn_T *isn;
8982 char_u *p;
8983
8984 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8985 if (isn == NULL)
8986 return NULL;
8987 isn->isn_arg.number = eap->cmdidx;
8988
8989 p = eap->arg;
8990 if (compile_expr0(&p, cctx) == FAIL)
8991 return NULL;
8992
8993 isn = generate_instr(cctx, ISN_CEXPR_CORE);
8994 if (isn == NULL)
8995 return NULL;
8996 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
8997 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
8998 return NULL;
8999 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9000 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9001 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9002
9003 return p;
9004}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009005#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009006
Bram Moolenaar4c137212021-04-19 16:48:48 +02009007/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009008 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009009 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009010 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009011 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009012add_def_function(ufunc_T *ufunc)
9013{
9014 dfunc_T *dfunc;
9015
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009016 if (def_functions.ga_len == 0)
9017 {
9018 // The first position is not used, so that a zero uf_dfunc_idx means it
9019 // wasn't set.
9020 if (ga_grow(&def_functions, 1) == FAIL)
9021 return FAIL;
9022 ++def_functions.ga_len;
9023 }
9024
Bram Moolenaar09689a02020-05-09 22:50:08 +02009025 // Add the function to "def_functions".
9026 if (ga_grow(&def_functions, 1) == FAIL)
9027 return FAIL;
9028 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9029 CLEAR_POINTER(dfunc);
9030 dfunc->df_idx = def_functions.ga_len;
9031 ufunc->uf_dfunc_idx = dfunc->df_idx;
9032 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009033 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009034 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009035 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009036 ++def_functions.ga_len;
9037 return OK;
9038}
9039
9040/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009041 * After ex_function() has collected all the function lines: parse and compile
9042 * the lines into instructions.
9043 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009044 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9045 * the return statement (used for lambda). When uf_ret_type is already set
9046 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009047 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009048 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009049 * This can be used recursively through compile_lambda(), which may reallocate
9050 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009051 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009052 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009053 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009054compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009055 ufunc_T *ufunc,
9056 int check_return_type,
9057 compiletype_T compile_type,
9058 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009059{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009060 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009061 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009062 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009063 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009064 cctx_T cctx;
9065 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009066 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009067 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009068 int ret = FAIL;
9069 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009070 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009071 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009072 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009073 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009074#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009075 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009076#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009077 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009078
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009079 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009080 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009081 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009082 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009083 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9084 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009085 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009086
9087 switch (compile_type)
9088 {
9089 case CT_PROFILE:
9090#ifdef FEAT_PROFILE
9091 instr_dest = dfunc->df_instr_prof; break;
9092#endif
9093 case CT_NONE: instr_dest = dfunc->df_instr; break;
9094 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9095 }
9096 if (instr_dest != NULL)
9097 // Was compiled in this mode before: Free old instructions.
9098 delete_def_function_contents(dfunc, FALSE);
9099 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009100 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009101 else
9102 {
9103 if (add_def_function(ufunc) == FAIL)
9104 return FAIL;
9105 new_def_function = TRUE;
9106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009107
Bram Moolenaar985116a2020-07-12 17:31:09 +02009108 ufunc->uf_def_status = UF_COMPILING;
9109
Bram Moolenaara80faa82020-04-12 19:37:17 +02009110 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009111
Bram Moolenaare99d4222021-06-13 14:01:26 +02009112 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009113 cctx.ctx_ufunc = ufunc;
9114 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009115 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009116 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9117 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9118 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9119 cctx.ctx_type_list = &ufunc->uf_type_list;
9120 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9121 instr = &cctx.ctx_instr;
9122
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009123 // Set the context to the function, it may be compiled when called from
9124 // another script. Set the script version to the most modern one.
9125 // The line number will be set in next_line_from_context().
9126 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009127 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9128
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009129 // Don't use the flag from ":legacy" here.
9130 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9131
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009132 // Make sure error messages are OK.
9133 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9134 if (do_estack_push)
9135 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009136 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009137
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009138 if (ufunc->uf_def_args.ga_len > 0)
9139 {
9140 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009141 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009142 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009143 int i;
9144 char_u *arg;
9145 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009146 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009147
9148 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009149 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009150 for (i = 0; i < count; ++i)
9151 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009152 garray_T *stack = &cctx.ctx_type_stack;
9153 type_T *val_type;
9154 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009155 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009156 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009157 int jump_instr_idx = instr->ga_len;
9158 isn_T *isn;
9159
9160 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9161 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9162 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009163
9164 // Make sure later arguments are not found.
9165 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009166
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009167 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009168 r = compile_expr0(&arg, &cctx);
9169
9170 ufunc->uf_args.ga_len = uf_args_len;
9171 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009172 goto erret;
9173
9174 // If no type specified use the type of the default value.
9175 // Otherwise check that the default value type matches the
9176 // specified type.
9177 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009178 where.wt_index = arg_idx + 1;
9179 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009180 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009181 {
9182 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009183 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009184 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009185 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009186 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009187 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009188
9189 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009190 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009191
9192 // set instruction index in JUMP_IF_ARG_SET to here
9193 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9194 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009195 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009196
9197 if (did_set_arg_type)
9198 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009199 }
9200
9201 /*
9202 * Loop over all the lines of the function and generate instructions.
9203 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009204 for (;;)
9205 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009206 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009207 int starts_with_colon = FALSE;
9208 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009209 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009210
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009211 // Bail out on the first error to avoid a flood of errors and report
9212 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009213 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009214 goto erret;
9215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009216 if (line != NULL && *line == '|')
9217 // the line continues after a '|'
9218 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009219 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009220 && !(*line == '#' && (line == cctx.ctx_line_start
9221 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009222 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009223 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009224 goto erret;
9225 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009226 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9227 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009228 else
9229 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009230 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009231 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009232 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009233 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009234#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009235 if (cctx.ctx_skip != SKIP_YES)
9236 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009237#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009238 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009239 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009240 // Make a copy, splitting off nextcmd and removing trailing spaces
9241 // may change it.
9242 if (line != NULL)
9243 {
9244 line = vim_strsave(line);
9245 vim_free(line_to_free);
9246 line_to_free = line;
9247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009248 }
9249
Bram Moolenaara80faa82020-04-12 19:37:17 +02009250 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009251 ea.cmdlinep = &line;
9252 ea.cmd = skipwhite(line);
9253
Bram Moolenaarb2049902021-01-24 12:53:53 +01009254 if (*ea.cmd == '#')
9255 {
9256 // "#" starts a comment
9257 line = (char_u *)"";
9258 continue;
9259 }
9260
Bram Moolenaarf002a412021-01-24 13:34:18 +01009261#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009262 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9263 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009264 {
9265 may_generate_prof_end(&cctx, prof_lnum);
9266
9267 prof_lnum = cctx.ctx_lnum;
9268 generate_instr(&cctx, ISN_PROF_START);
9269 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009270#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009271 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9272 && cctx.ctx_skip != SKIP_YES)
9273 {
9274 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009275 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009276 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009277 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009278
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009279 // Some things can be recognized by the first character.
9280 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009281 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009282 case '}':
9283 {
9284 // "}" ends a block scope
9285 scopetype_T stype = cctx.ctx_scope == NULL
9286 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009287
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009288 if (stype == BLOCK_SCOPE)
9289 {
9290 compile_endblock(&cctx);
9291 line = ea.cmd;
9292 }
9293 else
9294 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009295 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009296 goto erret;
9297 }
9298 if (line != NULL)
9299 line = skipwhite(ea.cmd + 1);
9300 continue;
9301 }
9302
9303 case '{':
9304 // "{" starts a block scope
9305 // "{'a': 1}->func() is something else
9306 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9307 {
9308 line = compile_block(ea.cmd, &cctx);
9309 continue;
9310 }
9311 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009312 }
9313
9314 /*
9315 * COMMAND MODIFIERS
9316 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009317 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009318 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9319 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009320 {
9321 if (errormsg != NULL)
9322 goto erret;
9323 // empty line or comment
9324 line = (char_u *)"";
9325 continue;
9326 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009327 generate_cmdmods(&cctx, &local_cmdmod);
9328 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009329
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009330 // Check if there was a colon after the last command modifier or before
9331 // the current position.
9332 for (p = ea.cmd; p >= line; --p)
9333 {
9334 if (*p == ':')
9335 starts_with_colon = TRUE;
9336 if (p < ea.cmd && !VIM_ISWHITE(*p))
9337 break;
9338 }
9339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009340 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009341 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009342 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009343 {
9344 if (*ea.cmd == '(')
9345 // not for "call()"
9346 ea.cmd = p;
9347 else
9348 ea.cmd = skipwhite(ea.cmd);
9349 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009350
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009351 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009352 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01009353 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009354
Bram Moolenaar17126b12021-01-07 22:03:02 +01009355 // Check for assignment after command modifiers.
9356 assign = may_compile_assignment(&ea, &line, &cctx);
9357 if (assign == OK)
9358 goto nextline;
9359 if (assign == FAIL)
9360 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009361 }
9362
9363 /*
9364 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009365 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009366 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009367 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009368 cmd = ea.cmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009369 if (starts_with_colon || !(*cmd == '\''
9370 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009371 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009372 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009373 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009374 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009375 if (!starts_with_colon)
9376 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009377 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009378 goto erret;
9379 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009380 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009381 if (ends_excmd2(line, ea.cmd))
9382 {
9383 // A range without a command: jump to the line.
9384 // TODO: compile to a more efficient command, possibly
9385 // calling parse_cmd_address().
9386 ea.cmdidx = CMD_SIZE;
9387 line = compile_exec(line, &ea, &cctx);
9388 goto nextline;
9389 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009390 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009391 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009392 p = find_ex_command(&ea, NULL, starts_with_colon
9393 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009394
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009395 if (p == NULL)
9396 {
9397 if (cctx.ctx_skip != SKIP_YES)
9398 emsg(_(e_ambiguous_use_of_user_defined_command));
9399 goto erret;
9400 }
9401
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009402 // When using ":legacy cmd" always use compile_exec().
9403 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009404 {
9405 char_u *start = ea.cmd;
9406
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009407 switch (ea.cmdidx)
9408 {
9409 case CMD_if:
9410 case CMD_elseif:
9411 case CMD_else:
9412 case CMD_endif:
9413 case CMD_for:
9414 case CMD_endfor:
9415 case CMD_continue:
9416 case CMD_break:
9417 case CMD_while:
9418 case CMD_endwhile:
9419 case CMD_try:
9420 case CMD_catch:
9421 case CMD_finally:
9422 case CMD_endtry:
9423 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9424 goto erret;
9425 default: break;
9426 }
9427
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009428 // ":legacy return expr" needs to be handled differently.
9429 if (checkforcmd(&start, "return", 4))
9430 ea.cmdidx = CMD_return;
9431 else
9432 ea.cmdidx = CMD_legacy;
9433 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009435 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9436 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009437 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009438 {
9439 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009440 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009441 }
9442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009443 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009444 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009445 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009446 // CMD_var cannot happen, compile_assignment() above would be
9447 // used. Most likely an assignment to a non-existing variable.
9448 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009449 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009450 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009451 }
9452
Bram Moolenaar3988f642020-08-27 22:43:03 +02009453 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009454 && ea.cmdidx != CMD_elseif
9455 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009456 && ea.cmdidx != CMD_endif
9457 && ea.cmdidx != CMD_endfor
9458 && ea.cmdidx != CMD_endwhile
9459 && ea.cmdidx != CMD_catch
9460 && ea.cmdidx != CMD_finally
9461 && ea.cmdidx != CMD_endtry)
9462 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009463 emsg(_(e_unreachable_code_after_return));
9464 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009465 }
9466
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009467 p = skipwhite(p);
9468 if (ea.cmdidx != CMD_SIZE
9469 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9470 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009471 if (ea.cmdidx >= 0)
9472 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009473 if ((ea.argt & EX_BANG) && *p == '!')
9474 {
9475 ea.forceit = TRUE;
9476 p = skipwhite(p + 1);
9477 }
9478 }
9479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009480 switch (ea.cmdidx)
9481 {
9482 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009483 ea.arg = p;
9484 line = compile_nested_function(&ea, &cctx);
9485 break;
9486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009487 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009488 // TODO: should we allow this, e.g. to declare a global
9489 // function?
9490 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009491 goto erret;
9492
9493 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009494 line = compile_return(p, check_return_type,
9495 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009496 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009497 break;
9498
9499 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009500 emsg(_(e_cannot_use_let_in_vim9_script));
9501 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009502 case CMD_var:
9503 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009504 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009505 case CMD_increment:
9506 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009507 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009508 if (line == p)
9509 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009510 break;
9511
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009512 case CMD_unlet:
9513 case CMD_unlockvar:
9514 case CMD_lockvar:
9515 line = compile_unletlock(p, &ea, &cctx);
9516 break;
9517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009518 case CMD_import:
9519 line = compile_import(p, &cctx);
9520 break;
9521
9522 case CMD_if:
9523 line = compile_if(p, &cctx);
9524 break;
9525 case CMD_elseif:
9526 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009527 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009528 break;
9529 case CMD_else:
9530 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009531 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009532 break;
9533 case CMD_endif:
9534 line = compile_endif(p, &cctx);
9535 break;
9536
9537 case CMD_while:
9538 line = compile_while(p, &cctx);
9539 break;
9540 case CMD_endwhile:
9541 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009542 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009543 break;
9544
9545 case CMD_for:
9546 line = compile_for(p, &cctx);
9547 break;
9548 case CMD_endfor:
9549 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009550 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009551 break;
9552 case CMD_continue:
9553 line = compile_continue(p, &cctx);
9554 break;
9555 case CMD_break:
9556 line = compile_break(p, &cctx);
9557 break;
9558
9559 case CMD_try:
9560 line = compile_try(p, &cctx);
9561 break;
9562 case CMD_catch:
9563 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009564 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009565 break;
9566 case CMD_finally:
9567 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009568 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009569 break;
9570 case CMD_endtry:
9571 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009572 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009573 break;
9574 case CMD_throw:
9575 line = compile_throw(p, &cctx);
9576 break;
9577
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009578 case CMD_eval:
9579 if (compile_expr0(&p, &cctx) == FAIL)
9580 goto erret;
9581
Bram Moolenaar3988f642020-08-27 22:43:03 +02009582 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009583 generate_instr_drop(&cctx, ISN_DROP, 1);
9584
9585 line = skipwhite(p);
9586 break;
9587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009588 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009589 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009590 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009591 case CMD_echomsg:
9592 case CMD_echoerr:
9593 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009594 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009595
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009596 case CMD_put:
9597 ea.cmd = cmd;
9598 line = compile_put(p, &ea, &cctx);
9599 break;
9600
Bram Moolenaar4c137212021-04-19 16:48:48 +02009601 case CMD_substitute:
9602 if (cctx.ctx_skip == SKIP_YES)
9603 line = (char_u *)"";
9604 else
9605 {
9606 ea.arg = p;
9607 line = compile_substitute(line, &ea, &cctx);
9608 }
9609 break;
9610
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009611 case CMD_redir:
9612 ea.arg = p;
9613 line = compile_redir(line, &ea, &cctx);
9614 break;
9615
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009616 case CMD_cexpr:
9617 case CMD_lexpr:
9618 case CMD_caddexpr:
9619 case CMD_laddexpr:
9620 case CMD_cgetexpr:
9621 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009622#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009623 ea.arg = p;
9624 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009625#else
9626 ex_ni(&ea);
9627 line = NULL;
9628#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009629 break;
9630
Bram Moolenaar3988f642020-08-27 22:43:03 +02009631 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009632
Bram Moolenaarae616492020-07-28 20:07:27 +02009633 case CMD_append:
9634 case CMD_change:
9635 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009636 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009637 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009638 case CMD_xit:
9639 not_in_vim9(&ea);
9640 goto erret;
9641
Bram Moolenaar002262f2020-07-08 17:47:57 +02009642 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009643 if (cctx.ctx_skip != SKIP_YES)
9644 {
9645 semsg(_(e_invalid_command_str), ea.cmd);
9646 goto erret;
9647 }
9648 // We don't check for a next command here.
9649 line = (char_u *)"";
9650 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009651
Bram Moolenaar20677332021-06-06 17:02:53 +02009652 case CMD_lua:
9653 case CMD_mzscheme:
9654 case CMD_perl:
9655 case CMD_py3:
9656 case CMD_python3:
9657 case CMD_python:
9658 case CMD_pythonx:
9659 case CMD_ruby:
9660 case CMD_tcl:
9661 ea.arg = p;
9662 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009663 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009664 else
9665 // heredoc lines have been concatenated with NL
9666 // characters in get_function_body()
9667 line = compile_script(line, &cctx);
9668 break;
9669
9670 default:
9671 // Not recognized, execute with do_cmdline_cmd().
9672 ea.arg = p;
9673 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009674 break;
9675 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009676nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009677 if (line == NULL)
9678 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009679 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009680
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009681 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009682 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009684 if (cctx.ctx_type_stack.ga_len < 0)
9685 {
9686 iemsg("Type stack underflow");
9687 goto erret;
9688 }
9689 }
9690
9691 if (cctx.ctx_scope != NULL)
9692 {
9693 if (cctx.ctx_scope->se_type == IF_SCOPE)
9694 emsg(_(e_endif));
9695 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9696 emsg(_(e_endwhile));
9697 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9698 emsg(_(e_endfor));
9699 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009700 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009701 goto erret;
9702 }
9703
Bram Moolenaarefd88552020-06-18 20:50:10 +02009704 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009705 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009706 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9707 ufunc->uf_ret_type = &t_void;
9708 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009709 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009710 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009711 goto erret;
9712 }
9713
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009714 // Return void if there is no return at the end.
9715 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009716 }
9717
Bram Moolenaar599410c2021-04-10 14:03:43 +02009718 // When compiled with ":silent!" and there was an error don't consider the
9719 // function compiled.
9720 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009721 {
9722 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9723 + ufunc->uf_dfunc_idx;
9724 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009725 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009726#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009727 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009728 {
9729 dfunc->df_instr_prof = instr->ga_data;
9730 dfunc->df_instr_prof_count = instr->ga_len;
9731 }
9732 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009733#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009734 if (cctx.ctx_compile_type == CT_DEBUG)
9735 {
9736 dfunc->df_instr_debug = instr->ga_data;
9737 dfunc->df_instr_debug_count = instr->ga_len;
9738 }
9739 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009740 {
9741 dfunc->df_instr = instr->ga_data;
9742 dfunc->df_instr_count = instr->ga_len;
9743 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009744 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009745 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009746 if (cctx.ctx_outer_used)
9747 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009748 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009749 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009750
9751 ret = OK;
9752
9753erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009754 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009755 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009756 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9757 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009758
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009759 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009760 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009761 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009762 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009763
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009764 // If using the last entry in the table and it was added above, we
9765 // might as well remove it.
9766 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009767 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009768 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009769 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009770 ufunc->uf_dfunc_idx = 0;
9771 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009772 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009773
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009774 while (cctx.ctx_scope != NULL)
9775 drop_scope(&cctx);
9776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009777 if (errormsg != NULL)
9778 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009779 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009780 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009781 }
9782
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009783 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9784 {
9785 if (ret == OK)
9786 {
9787 emsg(_(e_missing_redir_end));
9788 ret = FAIL;
9789 }
9790 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009791 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009792 }
9793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009794 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009795 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009796 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009797 if (do_estack_push)
9798 estack_pop();
9799
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009800 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009801 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009802 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009803 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009804 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009805}
9806
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009807 void
9808set_function_type(ufunc_T *ufunc)
9809{
9810 int varargs = ufunc->uf_va_name != NULL;
9811 int argcount = ufunc->uf_args.ga_len;
9812
9813 // Create a type for the function, with the return type and any
9814 // argument types.
9815 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9816 // The type is included in "tt_args".
9817 if (argcount > 0 || varargs)
9818 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009819 if (ufunc->uf_type_list.ga_itemsize == 0)
9820 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009821 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9822 argcount, &ufunc->uf_type_list);
9823 // Add argument types to the function type.
9824 if (func_type_add_arg_types(ufunc->uf_func_type,
9825 argcount + varargs,
9826 &ufunc->uf_type_list) == FAIL)
9827 return;
9828 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9829 ufunc->uf_func_type->tt_min_argcount =
9830 argcount - ufunc->uf_def_args.ga_len;
9831 if (ufunc->uf_arg_types == NULL)
9832 {
9833 int i;
9834
9835 // lambda does not have argument types.
9836 for (i = 0; i < argcount; ++i)
9837 ufunc->uf_func_type->tt_args[i] = &t_any;
9838 }
9839 else
9840 mch_memmove(ufunc->uf_func_type->tt_args,
9841 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9842 if (varargs)
9843 {
9844 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009845 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009846 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9847 }
9848 }
9849 else
9850 // No arguments, can use a predefined type.
9851 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9852 argcount, &ufunc->uf_type_list);
9853}
9854
9855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009856/*
9857 * Delete an instruction, free what it contains.
9858 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009859 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009860delete_instr(isn_T *isn)
9861{
9862 switch (isn->isn_type)
9863 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009864 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009865 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +02009866 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009867 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009868 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009869 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009870 case ISN_LOADENV:
9871 case ISN_LOADG:
9872 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009873 case ISN_LOADT:
9874 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009875 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009876 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009877 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009878 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009879 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009880 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009881 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009882 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009883 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009884 case ISN_STOREW:
9885 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009886 vim_free(isn->isn_arg.string);
9887 break;
9888
Bram Moolenaar4c137212021-04-19 16:48:48 +02009889 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +02009890 {
9891 int idx;
9892 isn_T *list = isn->isn_arg.subs.subs_instr;
9893
9894 vim_free(isn->isn_arg.subs.subs_cmd);
9895 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9896 delete_instr(list + idx);
9897 vim_free(list);
9898 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02009899 break;
9900
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009901 case ISN_INSTR:
9902 {
9903 int idx;
9904 isn_T *list = isn->isn_arg.instr;
9905
9906 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9907 delete_instr(list + idx);
9908 vim_free(list);
9909 }
9910 break;
9911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009912 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009913 case ISN_STORES:
9914 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009915 break;
9916
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009917 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009918 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009919 vim_free(isn->isn_arg.unlet.ul_name);
9920 break;
9921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009922 case ISN_STOREOPT:
9923 vim_free(isn->isn_arg.storeopt.so_name);
9924 break;
9925
9926 case ISN_PUSHBLOB: // push blob isn_arg.blob
9927 blob_unref(isn->isn_arg.blob);
9928 break;
9929
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009930 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009931#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009932 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009933#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009934 break;
9935
9936 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009937#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009938 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009939#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009940 break;
9941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009942 case ISN_UCALL:
9943 vim_free(isn->isn_arg.ufunc.cuf_name);
9944 break;
9945
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009946 case ISN_FUNCREF:
9947 {
9948 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9949 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009950 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009951
Bram Moolenaar077a4232020-12-22 18:33:27 +01009952 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9953 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009954 }
9955 break;
9956
9957 case ISN_DCALL:
9958 {
9959 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9960 + isn->isn_arg.dfunc.cdf_idx;
9961
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009962 if (dfunc->df_ufunc != NULL
9963 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009964 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009965 }
9966 break;
9967
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009968 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009969 {
9970 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9971 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9972
9973 if (ufunc != NULL)
9974 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009975 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009976 func_ptr_unref(ufunc);
9977 }
9978
9979 vim_free(lambda);
9980 vim_free(isn->isn_arg.newfunc.nf_global);
9981 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009982 break;
9983
Bram Moolenaar5e654232020-09-16 15:22:00 +02009984 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009985 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009986 free_type(isn->isn_arg.type.ct_type);
9987 break;
9988
Bram Moolenaar02194d22020-10-24 23:08:38 +02009989 case ISN_CMDMOD:
9990 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9991 ->cmod_filter_regmatch.regprog);
9992 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9993 break;
9994
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009995 case ISN_LOADSCRIPT:
9996 case ISN_STORESCRIPT:
9997 vim_free(isn->isn_arg.script.scriptref);
9998 break;
9999
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010000 case ISN_TRY:
10001 vim_free(isn->isn_arg.try.try_ref);
10002 break;
10003
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010004 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010005 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010006 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10007 break;
10008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010009 case ISN_2BOOL:
10010 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010011 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010012 case ISN_ADDBLOB:
10013 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010014 case ISN_ANYINDEX:
10015 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010016 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010017 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010018 case ISN_BLOBINDEX:
10019 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010020 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010021 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010022 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010023 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010024 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010025 case ISN_COMPAREANY:
10026 case ISN_COMPAREBLOB:
10027 case ISN_COMPAREBOOL:
10028 case ISN_COMPAREDICT:
10029 case ISN_COMPAREFLOAT:
10030 case ISN_COMPAREFUNC:
10031 case ISN_COMPARELIST:
10032 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010033 case ISN_COMPARESPECIAL:
10034 case ISN_COMPARESTRING:
10035 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010036 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010037 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010038 case ISN_DROP:
10039 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010040 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010041 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010042 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010043 case ISN_EXECCONCAT:
10044 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010045 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010046 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010047 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010048 case ISN_GETITEM:
10049 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010050 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010051 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010052 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010053 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010054 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010055 case ISN_LOADBDICT:
10056 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010057 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010058 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010059 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010060 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010061 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010062 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010063 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010064 case ISN_NEGATENR:
10065 case ISN_NEWDICT:
10066 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010067 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010068 case ISN_OPFLOAT:
10069 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010070 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010071 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010072 case ISN_PROF_END:
10073 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010074 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010075 case ISN_PUSHF:
10076 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010077 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010078 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010079 case ISN_REDIREND:
10080 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010081 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010082 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010083 case ISN_SHUFFLE:
10084 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010085 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010086 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010087 case ISN_STORENR:
10088 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010089 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010090 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010091 case ISN_STOREV:
10092 case ISN_STRINDEX:
10093 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010094 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010095 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010096 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010097 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010098 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010099 // nothing allocated
10100 break;
10101 }
10102}
10103
10104/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010105 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010106 */
10107 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010108delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010109{
10110 int idx;
10111
10112 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010113 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010114
10115 if (dfunc->df_instr != NULL)
10116 {
10117 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10118 delete_instr(dfunc->df_instr + idx);
10119 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010120 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010121 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010122 if (dfunc->df_instr_debug != NULL)
10123 {
10124 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10125 delete_instr(dfunc->df_instr_debug + idx);
10126 VIM_CLEAR(dfunc->df_instr_debug);
10127 dfunc->df_instr_debug = NULL;
10128 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010129#ifdef FEAT_PROFILE
10130 if (dfunc->df_instr_prof != NULL)
10131 {
10132 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10133 delete_instr(dfunc->df_instr_prof + idx);
10134 VIM_CLEAR(dfunc->df_instr_prof);
10135 dfunc->df_instr_prof = NULL;
10136 }
10137#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010138
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010139 if (mark_deleted)
10140 dfunc->df_deleted = TRUE;
10141 if (dfunc->df_ufunc != NULL)
10142 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010143}
10144
10145/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010146 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010147 * function, unless another user function still uses it.
10148 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010149 */
10150 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010151unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010152{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010153 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010154 {
10155 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10156 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010157
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010158 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010159 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010160 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010161 ufunc->uf_dfunc_idx = 0;
10162 if (dfunc->df_ufunc == ufunc)
10163 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010164 }
10165}
10166
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010167/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010168 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010169 */
10170 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010171link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010172{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010173 if (ufunc->uf_dfunc_idx > 0)
10174 {
10175 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10176 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010177
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010178 ++dfunc->df_refcount;
10179 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010180}
10181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010182#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010183/*
10184 * Free all functions defined with ":def".
10185 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010186 void
10187free_def_functions(void)
10188{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010189 int idx;
10190
10191 for (idx = 0; idx < def_functions.ga_len; ++idx)
10192 {
10193 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10194
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010195 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010196 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010197 }
10198
10199 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010200}
10201#endif
10202
10203
10204#endif // FEAT_EVAL