blob: 388308abfacaedb2408a7c294dba3ec980d8357d [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,
Bram Moolenaardcb53be2021-12-09 14:23:43 +0000121 dest_func_option,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200122 dest_env,
123 dest_global,
124 dest_buffer,
125 dest_window,
126 dest_tab,
127 dest_vimvar,
128 dest_script,
129 dest_reg,
130 dest_expr,
131} assign_dest_T;
132
133// Used by compile_lhs() to store information about the LHS of an assignment
134// and one argument of ":unlet" with an index.
135typedef struct {
136 assign_dest_T lhs_dest; // type of destination
137
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200138 char_u *lhs_name; // allocated name excluding the last
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200139 // "[expr]" or ".name".
140 size_t lhs_varlen; // length of the variable without
141 // "[expr]" or ".name"
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200142 char_u *lhs_whole; // allocated name including the last
143 // "[expr]" or ".name" for :redir
144 size_t lhs_varlen_total; // length of the variable including
145 // any "[expr]" or ".name"
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200146 char_u *lhs_dest_end; // end of the destination, including
147 // "[expr]" or ".name".
Bram Moolenaarab36e6a2021-11-30 16:14:49 +0000148 char_u *lhs_end; // end including any type
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200149
150 int lhs_has_index; // has "[expr]" or ".name"
151
152 int lhs_new_local; // create new local variable
153 int lhs_opt_flags; // for when destination is an option
154 int lhs_vimvaridx; // for when destination is a v:var
155
156 lvar_T lhs_local_lvar; // used for existing local destination
157 lvar_T lhs_arg_lvar; // used for argument destination
158 lvar_T *lhs_lvar; // points to destination lvar
159 int lhs_scriptvar_sid;
160 int lhs_scriptvar_idx;
161
162 int lhs_has_type; // type was specified
163 type_T *lhs_type;
164 type_T *lhs_member_type;
165
166 int lhs_append; // used by ISN_REDIREND
167} lhs_T;
168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169/*
170 * Context for compiling lines of Vim script.
171 * Stores info about the local variables and condition stack.
172 */
173struct cctx_S {
174 ufunc_T *ctx_ufunc; // current function
175 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200176 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177 garray_T ctx_instr; // generated instructions
178
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200179 int ctx_prev_lnum; // line number below previous command, for
180 // debugging
181
Bram Moolenaare99d4222021-06-13 14:01:26 +0200182 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100184 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100185
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200186 int ctx_has_closure; // set to one if a closures was created in
187 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100189 garray_T ctx_imports; // imported items
190
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200191 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200193 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100194
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200195 cctx_T *ctx_outer; // outer scope for lambda or nested
196 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200197 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200200 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200201
Bram Moolenaar02194d22020-10-24 23:08:38 +0200202 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200203
204 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
205 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206};
207
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100208static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100209
210/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100211 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100212 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100213 * If "lvar" is NULL only check if the variable can be found.
214 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100216 static int
217lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218{
219 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100220 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100222 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100223 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200224
225 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
227 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100228 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
229 if (STRNCMP(name, lvp->lv_name, len) == 0
230 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200231 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100232 if (lvar != NULL)
233 {
234 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100235 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100236 }
237 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200240
241 // Find local in outer function scope.
242 if (cctx->ctx_outer != NULL)
243 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100244 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200245 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100246 if (lvar != NULL)
247 {
248 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100249 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100250 }
251 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200252 }
253 }
254
Bram Moolenaar709664c2020-12-12 14:33:41 +0100255 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256}
257
258/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200259 * Lookup an argument in the current function and an enclosing function.
260 * Returns the argument index in "idxp"
261 * Returns the argument type in "type"
262 * Sets "gen_load_outer" to TRUE if found in outer scope.
263 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 */
265 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200266arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200267 char_u *name,
268 size_t len,
269 int *idxp,
270 type_T **type,
271 int *gen_load_outer,
272 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273{
274 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200275 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100277 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200278 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200279 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280 {
281 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
282
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200283 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
284 {
285 if (idxp != NULL)
286 {
287 // Arguments are located above the frame pointer. One further
288 // if there is a vararg argument
289 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
290 + STACK_FRAME_SIZE)
291 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
292
293 if (cctx->ctx_ufunc->uf_arg_types != NULL)
294 *type = cctx->ctx_ufunc->uf_arg_types[idx];
295 else
296 *type = &t_any;
297 }
298 return OK;
299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100300 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200302 va_name = cctx->ctx_ufunc->uf_va_name;
303 if (va_name != NULL
304 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
305 {
306 if (idxp != NULL)
307 {
308 // varargs is always the last argument
309 *idxp = -STACK_FRAME_SIZE - 1;
310 *type = cctx->ctx_ufunc->uf_va_type;
311 }
312 return OK;
313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200315 if (cctx->ctx_outer != NULL)
316 {
317 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200318 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200319 == OK)
320 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100321 if (gen_load_outer != NULL)
322 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200323 return OK;
324 }
325 }
326
327 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328}
329
330/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331 * Lookup a script-local variable in the current script, possibly defined in a
332 * block that contains the function "cctx->ctx_ufunc".
333 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100334 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200335 * Return NULL when not found.
336 */
337 static sallvar_T *
338find_script_var(char_u *name, size_t len, cctx_T *cctx)
339{
340 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
341 hashitem_T *hi;
342 int cc;
343 sallvar_T *sav;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200344 sallvar_T *found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200345 ufunc_T *ufunc;
346
347 // Find the list of all script variables with the right name.
348 if (len > 0)
349 {
350 cc = name[len];
351 name[len] = NUL;
352 }
353 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
354 if (len > 0)
355 name[len] = cc;
356 if (HASHITEM_EMPTY(hi))
357 return NULL;
358
359 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200360 if (sav->sav_block_id == 0)
361 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200362 return sav;
363
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200364 if (cctx == NULL)
365 {
366 // Not in a function scope, find variable with block id equal to or
367 // smaller than the current block id.
368 while (sav != NULL)
369 {
370 if (sav->sav_block_id <= si->sn_current_block_id)
371 break;
372 sav = sav->sav_next;
373 }
374 return sav;
375 }
376
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200377 // Go over the variables with this name and find one that was visible
378 // from the function.
379 ufunc = cctx->ctx_ufunc;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200380 found_sav = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200381 while (sav != NULL)
382 {
383 int idx;
384
385 // Go over the blocks that this function was defined in. If the
386 // variable block ID matches it was visible to the function.
387 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
388 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
389 return sav;
390 sav = sav->sav_next;
391 }
392
Bram Moolenaar88421d62021-07-24 14:14:52 +0200393 // Not found, assume variable at script level was visible.
394 return found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200395}
396
397/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100398 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200399 */
400 static int
401script_is_vim9()
402{
403 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
404}
405
406/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200407 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200408 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409 * Returns OK or FAIL.
410 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200411 static int
412script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200414 if (current_sctx.sc_sid <= 0)
415 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200416 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200417 {
418 // Check script variables that were visible where the function was
419 // defined.
420 if (find_script_var(name, len, cctx) != NULL)
421 return OK;
422 }
423 else
424 {
425 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
426 dictitem_T *di;
427 int cc;
428
429 // Check script variables that are currently visible
430 cc = name[len];
431 name[len] = NUL;
432 di = find_var_in_ht(ht, 0, name, TRUE);
433 name[len] = cc;
434 if (di != NULL)
435 return OK;
436 }
437
438 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439}
440
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100441/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100442 * Return TRUE if "name" is a local variable, argument, script variable or
443 * imported.
444 */
445 static int
446variable_exists(char_u *name, size_t len, cctx_T *cctx)
447{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100448 return (cctx != NULL
449 && (lookup_local(name, len, NULL, cctx) == OK
450 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200451 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100452 || find_imported(name, len, cctx) != NULL;
453}
454
455/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100456 * Return TRUE if "name" is a local variable, argument, script variable,
457 * imported or function.
458 */
459 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100460item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100461{
462 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100463 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100464
465 if (variable_exists(name, len, cctx))
466 return TRUE;
467
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100468 // This is similar to what is in lookup_scriptitem():
469 // Find a function, so that a following "->" works.
470 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
471 // a function call.
472 p = skipwhite(name + len);
473
474 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
475 {
476 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200477 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100478 // Skip "g:" before a function name.
479 is_global = (name[0] == 'g' && name[1] == ':');
480 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
481 }
482 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100483}
484
485/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100486 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200487 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200488 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100489 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100490 * Return FAIL and give an error if it defined.
491 */
492 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100493check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100494{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200495 int c = p[len];
496 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200497
Bram Moolenaarda479c72021-04-10 21:01:38 +0200498 // underscore argument is OK
499 if (len == 1 && *p == '_')
500 return OK;
501
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200502 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100503 {
504 if (is_arg)
505 semsg(_(e_argument_already_declared_in_script_str), p);
506 else
507 semsg(_(e_variable_already_declared_in_script_str), p);
508 return FAIL;
509 }
510
Bram Moolenaarad486a02020-08-01 23:22:18 +0200511 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100512 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100513 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200514 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200515 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200516 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100517 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200518 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200519 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
520 && (!func_is_global(ufunc)
521 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200522 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100523 if (is_arg)
524 semsg(_(e_argument_name_shadows_existing_variable_str), p);
525 else
526 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200527 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200528 return FAIL;
529 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100530 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200531 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100532 return OK;
533}
534
Bram Moolenaar65b95452020-07-19 14:03:09 +0200535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536/////////////////////////////////////////////////////////////////////
537// Following generate_ functions expect the caller to call ga_grow().
538
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200539#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
540#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100542/*
543 * Generate an instruction without arguments.
544 * Returns a pointer to the new instruction, NULL if failed.
545 */
546 static isn_T *
547generate_instr(cctx_T *cctx, isntype_T isn_type)
548{
549 garray_T *instr = &cctx->ctx_instr;
550 isn_T *isn;
551
Bram Moolenaar080457c2020-03-03 21:53:32 +0100552 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar35578162021-08-02 19:10:38 +0200553 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554 return NULL;
555 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
556 isn->isn_type = isn_type;
557 isn->isn_lnum = cctx->ctx_lnum + 1;
558 ++instr->ga_len;
559
560 return isn;
561}
562
563/*
564 * Generate an instruction without arguments.
565 * "drop" will be removed from the stack.
566 * Returns a pointer to the new instruction, NULL if failed.
567 */
568 static isn_T *
569generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
570{
571 garray_T *stack = &cctx->ctx_type_stack;
572
Bram Moolenaar080457c2020-03-03 21:53:32 +0100573 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 stack->ga_len -= drop;
575 return generate_instr(cctx, isn_type);
576}
577
578/*
579 * Generate instruction "isn_type" and put "type" on the type stack.
580 */
581 static isn_T *
582generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
583{
584 isn_T *isn;
585 garray_T *stack = &cctx->ctx_type_stack;
586
587 if ((isn = generate_instr(cctx, isn_type)) == NULL)
588 return NULL;
589
Bram Moolenaar35578162021-08-02 19:10:38 +0200590 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200592 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 ++stack->ga_len;
594
595 return isn;
596}
597
598/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200599 * Generate an ISN_DEBUG instruction.
600 */
601 static isn_T *
602generate_instr_debug(cctx_T *cctx)
603{
604 isn_T *isn;
605 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
606 + cctx->ctx_ufunc->uf_dfunc_idx;
607
608 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
609 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200610 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
611 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200612 return isn;
613}
614
615/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200617 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200618 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619 */
620 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200621may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622{
623 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200624 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100626 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100628 RETURN_OK_IF_SKIP(cctx);
629 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200630 switch ((*type)->tt_type)
631 {
632 // nothing to be done
633 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200635 // conversion possible
636 case VAR_SPECIAL:
637 case VAR_BOOL:
638 case VAR_NUMBER:
639 case VAR_FLOAT:
640 break;
641
642 // conversion possible (with runtime check)
643 case VAR_ANY:
644 case VAR_UNKNOWN:
645 isntype = ISN_2STRING_ANY;
646 break;
647
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200648 // conversion possible when tolerant
649 case VAR_LIST:
650 if (tolerant)
651 {
652 isntype = ISN_2STRING_ANY;
653 break;
654 }
655 // FALLTHROUGH
656
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200657 // conversion not possible
658 case VAR_VOID:
659 case VAR_BLOB:
660 case VAR_FUNC:
661 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200662 case VAR_DICT:
663 case VAR_JOB:
664 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200665 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200666 to_string_error((*type)->tt_type);
667 return FAIL;
668 }
669
670 *type = &t_string;
671 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200673 isn->isn_arg.tostring.offset = offset;
674 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
676 return OK;
677}
678
679 static int
680check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
681{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200684 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 {
686 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200687 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200689 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690 return FAIL;
691 }
692 return OK;
693}
694
Bram Moolenaar07802042021-09-09 23:01:14 +0200695/*
696 * Generate instruction for "+". For a list this creates a new list.
697 */
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200698 static int
699generate_add_instr(
700 cctx_T *cctx,
701 vartype_T vartype,
702 type_T *type1,
Bram Moolenaar07802042021-09-09 23:01:14 +0200703 type_T *type2,
704 exprtype_T expr_type)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200705{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100706 garray_T *stack = &cctx->ctx_type_stack;
707 isn_T *isn = generate_instr_drop(cctx,
708 vartype == VAR_NUMBER ? ISN_OPNR
709 : vartype == VAR_LIST ? ISN_ADDLIST
710 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200711#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100712 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200713#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100714 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200715
716 if (vartype != VAR_LIST && vartype != VAR_BLOB
717 && type1->tt_type != VAR_ANY
718 && type2->tt_type != VAR_ANY
719 && check_number_or_float(
720 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
721 return FAIL;
722
723 if (isn != NULL)
Bram Moolenaar07802042021-09-09 23:01:14 +0200724 {
725 if (isn->isn_type == ISN_ADDLIST)
726 isn->isn_arg.op.op_type = expr_type;
727 else
728 isn->isn_arg.op.op_type = EXPR_ADD;
729 }
Bram Moolenaar399ea812020-12-15 21:28:57 +0100730
731 // When concatenating two lists with different member types the member type
732 // becomes "any".
733 if (vartype == VAR_LIST
734 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
735 && type1->tt_member != type2->tt_member)
736 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
737
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200738 return isn == NULL ? FAIL : OK;
739}
740
741/*
742 * Get the type to use for an instruction for an operation on "type1" and
743 * "type2". If they are matching use a type-specific instruction. Otherwise
744 * fall back to runtime type checking.
745 */
746 static vartype_T
747operator_type(type_T *type1, type_T *type2)
748{
749 if (type1->tt_type == type2->tt_type
750 && (type1->tt_type == VAR_NUMBER
751 || type1->tt_type == VAR_LIST
752#ifdef FEAT_FLOAT
753 || type1->tt_type == VAR_FLOAT
754#endif
755 || type1->tt_type == VAR_BLOB))
756 return type1->tt_type;
757 return VAR_ANY;
758}
759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760/*
761 * Generate an instruction with two arguments. The instruction depends on the
762 * type of the arguments.
763 */
764 static int
765generate_two_op(cctx_T *cctx, char_u *op)
766{
767 garray_T *stack = &cctx->ctx_type_stack;
768 type_T *type1;
769 type_T *type2;
770 vartype_T vartype;
771 isn_T *isn;
772
Bram Moolenaar080457c2020-03-03 21:53:32 +0100773 RETURN_OK_IF_SKIP(cctx);
774
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200775 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
777 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200778 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779
780 switch (*op)
781 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200782 case '+':
Bram Moolenaar07802042021-09-09 23:01:14 +0200783 if (generate_add_instr(cctx, vartype, type1, type2,
784 EXPR_COPY) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 break;
787
788 case '-':
789 case '*':
790 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
791 op) == FAIL)
792 return FAIL;
793 if (vartype == VAR_NUMBER)
794 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
795#ifdef FEAT_FLOAT
796 else if (vartype == VAR_FLOAT)
797 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
798#endif
799 else
800 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
801 if (isn != NULL)
802 isn->isn_arg.op.op_type = *op == '*'
803 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
804 break;
805
Bram Moolenaar4c683752020-04-05 21:38:23 +0200806 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200808 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 && type2->tt_type != VAR_NUMBER))
810 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200811 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 return FAIL;
813 }
814 isn = generate_instr_drop(cctx,
815 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
816 if (isn != NULL)
817 isn->isn_arg.op.op_type = EXPR_REM;
818 break;
819 }
820
821 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200822 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 {
824 type_T *type = &t_any;
825
826#ifdef FEAT_FLOAT
827 // float+number and number+float results in float
828 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
829 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
830 type = &t_float;
831#endif
832 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
833 }
834
835 return OK;
836}
837
838/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200839 * Get the instruction to use for comparing "type1" with "type2"
840 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200842 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100843get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844{
845 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846
Bram Moolenaar4c683752020-04-05 21:38:23 +0200847 if (type1 == VAR_UNKNOWN)
848 type1 = VAR_ANY;
849 if (type2 == VAR_UNKNOWN)
850 type2 = VAR_ANY;
851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100852 if (type1 == type2)
853 {
854 switch (type1)
855 {
856 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
857 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
858 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
859 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
860 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
861 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
862 case VAR_LIST: isntype = ISN_COMPARELIST; break;
863 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
864 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 default: isntype = ISN_COMPAREANY; break;
866 }
867 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200868 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100870 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871 isntype = ISN_COMPAREANY;
872
Bram Moolenaar657137c2021-01-09 15:45:23 +0100873 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 && (isntype == ISN_COMPAREBOOL
875 || isntype == ISN_COMPARESPECIAL
876 || isntype == ISN_COMPARENR
877 || isntype == ISN_COMPAREFLOAT))
878 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200879 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100880 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200881 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 }
883 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100884 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
886 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100887 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
888 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889 && (type1 == VAR_BLOB || type2 == VAR_BLOB
890 || type1 == VAR_LIST || type2 == VAR_LIST))))
891 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200892 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200894 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200896 return isntype;
897}
898
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200899 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100900check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200901{
902 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
903 return FAIL;
904 return OK;
905}
906
Bram Moolenaara5565e42020-05-09 15:44:01 +0200907/*
908 * Generate an ISN_COMPARE* instruction with a boolean result.
909 */
910 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100911generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200912{
913 isntype_T isntype;
914 isn_T *isn;
915 garray_T *stack = &cctx->ctx_type_stack;
916 vartype_T type1;
917 vartype_T type2;
918
919 RETURN_OK_IF_SKIP(cctx);
920
921 // Get the known type of the two items on the stack. If they are matching
922 // use a type-specific instruction. Otherwise fall back to runtime type
923 // checking.
924 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
925 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100926 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200927 if (isntype == ISN_DROP)
928 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929
930 if ((isn = generate_instr(cctx, isntype)) == NULL)
931 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100932 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 isn->isn_arg.op.op_ic = ic;
934
935 // takes two arguments, puts one bool back
936 if (stack->ga_len >= 2)
937 {
938 --stack->ga_len;
939 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
940 }
941
942 return OK;
943}
944
945/*
946 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200947 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 */
949 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200950generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951{
952 isn_T *isn;
953 garray_T *stack = &cctx->ctx_type_stack;
954
Bram Moolenaar080457c2020-03-03 21:53:32 +0100955 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
957 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200958 isn->isn_arg.tobool.invert = invert;
959 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960
961 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200962 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963
964 return OK;
965}
966
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200967/*
968 * Generate an ISN_COND2BOOL instruction.
969 */
970 static int
971generate_COND2BOOL(cctx_T *cctx)
972{
973 isn_T *isn;
974 garray_T *stack = &cctx->ctx_type_stack;
975
976 RETURN_OK_IF_SKIP(cctx);
977 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
978 return FAIL;
979
980 // type becomes bool
981 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
982
983 return OK;
984}
985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200987generate_TYPECHECK(
988 cctx_T *cctx,
989 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100990 int offset,
991 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992{
993 isn_T *isn;
994 garray_T *stack = &cctx->ctx_type_stack;
995
Bram Moolenaar080457c2020-03-03 21:53:32 +0100996 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
998 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200999 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01001000 isn->isn_arg.type.ct_off = (int8_T)offset;
1001 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002
Bram Moolenaar5e654232020-09-16 15:22:00 +02001003 // type becomes expected
1004 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005
1006 return OK;
1007}
1008
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001009 static int
1010generate_SETTYPE(
1011 cctx_T *cctx,
1012 type_T *expected)
1013{
1014 isn_T *isn;
1015
1016 RETURN_OK_IF_SKIP(cctx);
1017 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
1018 return FAIL;
1019 isn->isn_arg.type.ct_type = alloc_type(expected);
1020 return OK;
1021}
1022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001024 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
1025 * used. Return FALSE if the types will never match.
1026 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02001027 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001028use_typecheck(type_T *actual, type_T *expected)
1029{
1030 if (actual->tt_type == VAR_ANY
1031 || actual->tt_type == VAR_UNKNOWN
1032 || (actual->tt_type == VAR_FUNC
1033 && (expected->tt_type == VAR_FUNC
1034 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001035 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1036 && ((actual->tt_member == &t_void)
1037 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001038 return TRUE;
1039 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1040 && actual->tt_type == expected->tt_type)
1041 // This takes care of a nested list or dict.
1042 return use_typecheck(actual->tt_member, expected->tt_member);
1043 return FALSE;
1044}
1045
1046/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001047 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001048 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001049 * - "actual" is a type that can be "expected" type: add a runtime check; or
1050 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001051 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1052 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001053 */
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001054 static int
1055need_type_where(
1056 type_T *actual,
1057 type_T *expected,
1058 int offset,
1059 where_T where,
1060 cctx_T *cctx,
1061 int silent,
1062 int actual_is_const)
1063{
1064 if (expected == &t_bool && actual != &t_bool
1065 && (actual->tt_flags & TTFLAG_BOOL_OK))
1066 {
1067 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1068 // boolean is OK but requires a conversion.
1069 generate_2BOOL(cctx, FALSE, offset);
1070 return OK;
1071 }
1072
1073 if (check_type(expected, actual, FALSE, where) == OK)
1074 return OK;
1075
1076 // If the actual type can be the expected type add a runtime check.
1077 // If it's a constant a runtime check makes no sense.
1078 if ((!actual_is_const || actual == &t_any)
1079 && use_typecheck(actual, expected))
1080 {
1081 generate_TYPECHECK(cctx, expected, offset, where.wt_index);
1082 return OK;
1083 }
1084
1085 if (!silent)
1086 type_mismatch_where(expected, actual, where);
1087 return FAIL;
1088}
1089
Bram Moolenaar351ead02021-01-16 16:07:01 +01001090 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001091need_type(
1092 type_T *actual,
1093 type_T *expected,
1094 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001095 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001096 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001097 int silent,
1098 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001099{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001100 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001101
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001102 where.wt_index = arg_idx;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001103 return need_type_where(actual, expected, offset, where,
1104 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001105}
1106
1107/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001108 * Check that the top of the type stack has a type that can be used as a
1109 * condition. Give an error and return FAIL if not.
1110 */
1111 static int
1112bool_on_stack(cctx_T *cctx)
1113{
1114 garray_T *stack = &cctx->ctx_type_stack;
1115 type_T *type;
1116
1117 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1118 if (type == &t_bool)
1119 return OK;
1120
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001121 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001122 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1123 // This requires a runtime type check.
1124 return generate_COND2BOOL(cctx);
1125
Bram Moolenaar351ead02021-01-16 16:07:01 +01001126 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001127}
1128
1129/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 * Generate an ISN_PUSHNR instruction.
1131 */
1132 static int
1133generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1134{
1135 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001136 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.number = number;
1142
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001143 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001144 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001145 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 return OK;
1147}
1148
1149/*
1150 * Generate an ISN_PUSHBOOL instruction.
1151 */
1152 static int
1153generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1154{
1155 isn_T *isn;
1156
Bram Moolenaar080457c2020-03-03 21:53:32 +01001157 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1159 return FAIL;
1160 isn->isn_arg.number = number;
1161
1162 return OK;
1163}
1164
1165/*
1166 * Generate an ISN_PUSHSPEC instruction.
1167 */
1168 static int
1169generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1170{
1171 isn_T *isn;
1172
Bram Moolenaar080457c2020-03-03 21:53:32 +01001173 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1175 return FAIL;
1176 isn->isn_arg.number = number;
1177
1178 return OK;
1179}
1180
1181#ifdef FEAT_FLOAT
1182/*
1183 * Generate an ISN_PUSHF instruction.
1184 */
1185 static int
1186generate_PUSHF(cctx_T *cctx, float_T fnumber)
1187{
1188 isn_T *isn;
1189
Bram Moolenaar080457c2020-03-03 21:53:32 +01001190 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1192 return FAIL;
1193 isn->isn_arg.fnumber = fnumber;
1194
1195 return OK;
1196}
1197#endif
1198
1199/*
1200 * Generate an ISN_PUSHS instruction.
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001201 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 */
1203 static int
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001204generate_PUSHS(cctx_T *cctx, char_u **str)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205{
1206 isn_T *isn;
1207
Bram Moolenaar508b5612021-01-02 18:17:26 +01001208 if (cctx->ctx_skip == SKIP_YES)
1209 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001210 if (str != NULL)
1211 VIM_CLEAR(*str);
Bram Moolenaar508b5612021-01-02 18:17:26 +01001212 return OK;
1213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001215 {
1216 if (str != NULL)
1217 VIM_CLEAR(*str);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001219 }
1220 isn->isn_arg.string = str == NULL ? NULL : *str;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221
1222 return OK;
1223}
1224
1225/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001226 * Generate an ISN_PUSHCHANNEL instruction.
1227 * Consumes "channel".
1228 */
1229 static int
1230generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1231{
1232 isn_T *isn;
1233
Bram Moolenaar080457c2020-03-03 21:53:32 +01001234 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001235 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1236 return FAIL;
1237 isn->isn_arg.channel = channel;
1238
1239 return OK;
1240}
1241
1242/*
1243 * Generate an ISN_PUSHJOB instruction.
1244 * Consumes "job".
1245 */
1246 static int
1247generate_PUSHJOB(cctx_T *cctx, job_T *job)
1248{
1249 isn_T *isn;
1250
Bram Moolenaar080457c2020-03-03 21:53:32 +01001251 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001252 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001253 return FAIL;
1254 isn->isn_arg.job = job;
1255
1256 return OK;
1257}
1258
1259/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 * Generate an ISN_PUSHBLOB instruction.
1261 * Consumes "blob".
1262 */
1263 static int
1264generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1265{
1266 isn_T *isn;
1267
Bram Moolenaar080457c2020-03-03 21:53:32 +01001268 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1270 return FAIL;
1271 isn->isn_arg.blob = blob;
1272
1273 return OK;
1274}
1275
1276/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001277 * Generate an ISN_PUSHFUNC instruction with name "name".
1278 * Consumes "name".
1279 */
1280 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001281generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001282{
1283 isn_T *isn;
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001284 char_u *funcname;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001285
Bram Moolenaar080457c2020-03-03 21:53:32 +01001286 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001287 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001288 return FAIL;
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001289 if (name == NULL)
1290 funcname = NULL;
1291 else if (*name == K_SPECIAL) // script-local
1292 funcname = vim_strsave(name);
1293 else
1294 {
1295 funcname = alloc(STRLEN(name) + 3);
1296 if (funcname != NULL)
1297 {
1298 STRCPY(funcname, "g:");
1299 STRCPY(funcname + 2, name);
1300 }
1301 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001302
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001303 isn->isn_arg.string = funcname;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001304 return OK;
1305}
1306
1307/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001308 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001309 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1310 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001311 */
1312 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001313generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001314{
1315 isn_T *isn;
1316 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001317 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1318 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001319 type_T *item_type = &t_any;
1320
1321 RETURN_OK_IF_SKIP(cctx);
1322
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001323 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001324 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001325 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001326 emsg(_(e_listreq));
1327 return FAIL;
1328 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001329 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001330 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1331 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001332 isn->isn_arg.getitem.gi_index = index;
1333 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001334
1335 // add the item type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001336 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001337 return FAIL;
1338 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1339 ++stack->ga_len;
1340 return OK;
1341}
1342
1343/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001344 * Generate an ISN_SLICE instruction with "count".
1345 */
1346 static int
1347generate_SLICE(cctx_T *cctx, int count)
1348{
1349 isn_T *isn;
1350
1351 RETURN_OK_IF_SKIP(cctx);
1352 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1353 return FAIL;
1354 isn->isn_arg.number = count;
1355 return OK;
1356}
1357
1358/*
1359 * Generate an ISN_CHECKLEN instruction with "min_len".
1360 */
1361 static int
1362generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1363{
1364 isn_T *isn;
1365
1366 RETURN_OK_IF_SKIP(cctx);
1367
1368 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1369 return FAIL;
1370 isn->isn_arg.checklen.cl_min_len = min_len;
1371 isn->isn_arg.checklen.cl_more_OK = more_OK;
1372
1373 return OK;
1374}
1375
1376/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 * Generate an ISN_STORE instruction.
1378 */
1379 static int
1380generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1381{
1382 isn_T *isn;
1383
Bram Moolenaar080457c2020-03-03 21:53:32 +01001384 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1386 return FAIL;
1387 if (name != NULL)
1388 isn->isn_arg.string = vim_strsave(name);
1389 else
1390 isn->isn_arg.number = idx;
1391
1392 return OK;
1393}
1394
1395/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001396 * Generate an ISN_STOREOUTER instruction.
1397 */
1398 static int
1399generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1400{
1401 isn_T *isn;
1402
1403 RETURN_OK_IF_SKIP(cctx);
1404 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1405 return FAIL;
1406 isn->isn_arg.outer.outer_idx = idx;
1407 isn->isn_arg.outer.outer_depth = level;
1408
1409 return OK;
1410}
1411
1412/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1414 */
1415 static int
1416generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1417{
1418 isn_T *isn;
1419
Bram Moolenaar080457c2020-03-03 21:53:32 +01001420 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1422 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001423 isn->isn_arg.storenr.stnr_idx = idx;
1424 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425
1426 return OK;
1427}
1428
1429/*
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001430 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 */
1432 static int
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001433generate_STOREOPT(
1434 cctx_T *cctx,
1435 isntype_T isn_type,
1436 char_u *name,
1437 int opt_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438{
1439 isn_T *isn;
1440
Bram Moolenaar080457c2020-03-03 21:53:32 +01001441 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001442 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 return FAIL;
1444 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1445 isn->isn_arg.storeopt.so_flags = opt_flags;
1446
1447 return OK;
1448}
1449
1450/*
1451 * Generate an ISN_LOAD or similar instruction.
1452 */
1453 static int
1454generate_LOAD(
1455 cctx_T *cctx,
1456 isntype_T isn_type,
1457 int idx,
1458 char_u *name,
1459 type_T *type)
1460{
1461 isn_T *isn;
1462
Bram Moolenaar080457c2020-03-03 21:53:32 +01001463 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1465 return FAIL;
1466 if (name != NULL)
1467 isn->isn_arg.string = vim_strsave(name);
1468 else
1469 isn->isn_arg.number = idx;
1470
1471 return OK;
1472}
1473
1474/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001475 * Generate an ISN_LOADOUTER instruction
1476 */
1477 static int
1478generate_LOADOUTER(
1479 cctx_T *cctx,
1480 int idx,
1481 int nesting,
1482 type_T *type)
1483{
1484 isn_T *isn;
1485
1486 RETURN_OK_IF_SKIP(cctx);
1487 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1488 return FAIL;
1489 isn->isn_arg.outer.outer_idx = idx;
1490 isn->isn_arg.outer.outer_depth = nesting;
1491
1492 return OK;
1493}
1494
1495/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001496 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001497 */
1498 static int
1499generate_LOADV(
1500 cctx_T *cctx,
1501 char_u *name,
1502 int error)
1503{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001504 int di_flags;
1505 int vidx = find_vim_var(name, &di_flags);
1506 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001507
Bram Moolenaar080457c2020-03-03 21:53:32 +01001508 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001509 if (vidx < 0)
1510 {
1511 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001512 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001513 return FAIL;
1514 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001515 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001516
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001517 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001518}
1519
1520/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001521 * Generate an ISN_UNLET instruction.
1522 */
1523 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001524generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001525{
1526 isn_T *isn;
1527
1528 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001529 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001530 return FAIL;
1531 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1532 isn->isn_arg.unlet.ul_forceit = forceit;
1533
1534 return OK;
1535}
1536
1537/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001538 * Generate an ISN_LOCKCONST instruction.
1539 */
1540 static int
1541generate_LOCKCONST(cctx_T *cctx)
1542{
1543 isn_T *isn;
1544
1545 RETURN_OK_IF_SKIP(cctx);
1546 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1547 return FAIL;
1548 return OK;
1549}
1550
1551/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 * Generate an ISN_LOADS instruction.
1553 */
1554 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001555generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001557 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001559 int sid,
1560 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561{
1562 isn_T *isn;
1563
Bram Moolenaar080457c2020-03-03 21:53:32 +01001564 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001565 if (isn_type == ISN_LOADS)
1566 isn = generate_instr_type(cctx, isn_type, type);
1567 else
1568 isn = generate_instr_drop(cctx, isn_type, 1);
1569 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001571 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1572 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573
1574 return OK;
1575}
1576
1577/*
1578 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1579 */
1580 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001581generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 cctx_T *cctx,
1583 isntype_T isn_type,
1584 int sid,
1585 int idx,
1586 type_T *type)
1587{
1588 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001589 scriptref_T *sref;
1590 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591
Bram Moolenaar080457c2020-03-03 21:53:32 +01001592 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 if (isn_type == ISN_LOADSCRIPT)
1594 isn = generate_instr_type(cctx, isn_type, type);
1595 else
1596 isn = generate_instr_drop(cctx, isn_type, 1);
1597 if (isn == NULL)
1598 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001599
1600 // This requires three arguments, which doesn't fit in an instruction, thus
1601 // we need to allocate a struct for this.
1602 sref = ALLOC_ONE(scriptref_T);
1603 if (sref == NULL)
1604 return FAIL;
1605 isn->isn_arg.script.scriptref = sref;
1606 sref->sref_sid = sid;
1607 sref->sref_idx = idx;
1608 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001609 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 return OK;
1611}
1612
1613/*
1614 * Generate an ISN_NEWLIST instruction.
1615 */
1616 static int
1617generate_NEWLIST(cctx_T *cctx, int count)
1618{
1619 isn_T *isn;
1620 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 type_T *type;
1622 type_T *member;
1623
Bram Moolenaar080457c2020-03-03 21:53:32 +01001624 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1626 return FAIL;
1627 isn->isn_arg.number = count;
1628
Bram Moolenaar127542b2020-08-09 17:22:04 +02001629 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001630 if (count == 0)
Bram Moolenaar6e48b842021-08-10 22:52:02 +02001631 member = &t_unknown;
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001632 else
1633 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001634 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1635 cctx->ctx_type_list);
1636 type = get_list_type(member, cctx->ctx_type_list);
1637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 // drop the value types
1639 stack->ga_len -= count;
1640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 // add the list type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001642 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643 return FAIL;
1644 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1645 ++stack->ga_len;
1646
1647 return OK;
1648}
1649
1650/*
1651 * Generate an ISN_NEWDICT instruction.
1652 */
1653 static int
1654generate_NEWDICT(cctx_T *cctx, int count)
1655{
1656 isn_T *isn;
1657 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 type_T *type;
1659 type_T *member;
1660
Bram Moolenaar080457c2020-03-03 21:53:32 +01001661 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1663 return FAIL;
1664 isn->isn_arg.number = count;
1665
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001666 if (count == 0)
1667 member = &t_void;
1668 else
1669 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001670 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1671 cctx->ctx_type_list);
1672 type = get_dict_type(member, cctx->ctx_type_list);
1673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 // drop the key and value types
1675 stack->ga_len -= 2 * count;
1676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 // add the dict type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001678 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 return FAIL;
1680 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1681 ++stack->ga_len;
1682
1683 return OK;
1684}
1685
1686/*
1687 * Generate an ISN_FUNCREF instruction.
1688 */
1689 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001690generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691{
1692 isn_T *isn;
1693 garray_T *stack = &cctx->ctx_type_stack;
1694
Bram Moolenaar080457c2020-03-03 21:53:32 +01001695 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1697 return FAIL;
Bram Moolenaar38453522021-11-28 22:00:12 +00001698 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1699 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1700 else
1701 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001702 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001704 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001705 // the nested context, including this one.
1706 if (ufunc->uf_flags & FC_CLOSURE)
1707 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1708
Bram Moolenaar35578162021-08-02 19:10:38 +02001709 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001711 ((type_T **)stack->ga_data)[stack->ga_len] =
1712 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 ++stack->ga_len;
1714
1715 return OK;
1716}
1717
1718/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001719 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001720 * "lambda_name" and "func_name" must be in allocated memory and will be
1721 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001722 */
1723 static int
1724generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1725{
1726 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001727
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001728 if (cctx->ctx_skip == SKIP_YES)
1729 {
1730 vim_free(lambda_name);
1731 vim_free(func_name);
1732 return OK;
1733 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001734 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001735 {
1736 vim_free(lambda_name);
1737 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001738 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001739 }
1740 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001741 isn->isn_arg.newfunc.nf_global = func_name;
1742
1743 return OK;
1744}
1745
1746/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001747 * Generate an ISN_DEF instruction: list functions
1748 */
1749 static int
1750generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1751{
1752 isn_T *isn;
1753
1754 RETURN_OK_IF_SKIP(cctx);
1755 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1756 return FAIL;
1757 if (len > 0)
1758 {
1759 isn->isn_arg.string = vim_strnsave(name, len);
1760 if (isn->isn_arg.string == NULL)
1761 return FAIL;
1762 }
1763 return OK;
1764}
1765
1766/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 * Generate an ISN_JUMP instruction.
1768 */
1769 static int
1770generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1771{
1772 isn_T *isn;
1773 garray_T *stack = &cctx->ctx_type_stack;
1774
Bram Moolenaar080457c2020-03-03 21:53:32 +01001775 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1777 return FAIL;
1778 isn->isn_arg.jump.jump_when = when;
1779 isn->isn_arg.jump.jump_where = where;
1780
1781 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1782 --stack->ga_len;
1783
1784 return OK;
1785}
1786
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001787/*
1788 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1789 */
1790 static int
1791generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1792{
1793 isn_T *isn;
1794
1795 RETURN_OK_IF_SKIP(cctx);
1796 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1797 return FAIL;
1798 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1799 // jump_where is set later
1800 return OK;
1801}
1802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 static int
1804generate_FOR(cctx_T *cctx, int loop_idx)
1805{
1806 isn_T *isn;
1807 garray_T *stack = &cctx->ctx_type_stack;
1808
Bram Moolenaar080457c2020-03-03 21:53:32 +01001809 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1811 return FAIL;
1812 isn->isn_arg.forloop.for_idx = loop_idx;
1813
Bram Moolenaar35578162021-08-02 19:10:38 +02001814 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 return FAIL;
1816 // type doesn't matter, will be stored next
1817 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1818 ++stack->ga_len;
1819
1820 return OK;
1821}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001822/*
1823 * Generate an ISN_TRYCONT instruction.
1824 */
1825 static int
1826generate_TRYCONT(cctx_T *cctx, int levels, int where)
1827{
1828 isn_T *isn;
1829
1830 RETURN_OK_IF_SKIP(cctx);
1831 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1832 return FAIL;
1833 isn->isn_arg.trycont.tct_levels = levels;
1834 isn->isn_arg.trycont.tct_where = where;
1835
1836 return OK;
1837}
1838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839
1840/*
1841 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001842 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 * Return FAIL if the number of arguments is wrong.
1844 */
1845 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001846generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847{
1848 isn_T *isn;
1849 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001850 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001851 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001852 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001853 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854
Bram Moolenaar080457c2020-03-03 21:53:32 +01001855 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001856 argoff = check_internal_func(func_idx, argcount);
1857 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 return FAIL;
1859
Bram Moolenaar389df252020-07-09 21:20:47 +02001860 if (method_call && argoff > 1)
1861 {
1862 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1863 return FAIL;
1864 isn->isn_arg.shuffle.shfl_item = argcount;
1865 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1866 }
1867
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001868 if (argcount > 0)
1869 {
1870 // Check the types of the arguments.
1871 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001872 if (method_call && argoff > 1)
1873 {
1874 int i;
1875
1876 for (i = 0; i < argcount; ++i)
1877 shuffled_argtypes[i] = (i < argoff - 1)
1878 ? argtypes[i + 1]
1879 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1880 argtypes = shuffled_argtypes;
1881 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001882 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1883 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001884 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001885 if (internal_func_is_map(func_idx))
1886 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001887 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1890 return FAIL;
1891 isn->isn_arg.bfunc.cbf_idx = func_idx;
1892 isn->isn_arg.bfunc.cbf_argcount = argcount;
1893
Bram Moolenaar94738d82020-10-21 14:25:07 +02001894 // Drop the argument types and push the return type.
1895 stack->ga_len -= argcount;
Bram Moolenaar35578162021-08-02 19:10:38 +02001896 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897 return FAIL;
1898 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001899 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001900 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001902 if (maptype != NULL && maptype->tt_member != NULL
1903 && maptype->tt_member != &t_any)
1904 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001905 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 return OK;
1908}
1909
1910/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001911 * Generate an ISN_LISTAPPEND instruction. Works like add().
1912 * Argument count is already checked.
1913 */
1914 static int
1915generate_LISTAPPEND(cctx_T *cctx)
1916{
1917 garray_T *stack = &cctx->ctx_type_stack;
1918 type_T *list_type;
1919 type_T *item_type;
1920 type_T *expected;
1921
1922 // Caller already checked that list_type is a list.
1923 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1924 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1925 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001926 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001927 return FAIL;
1928
1929 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1930 return FAIL;
1931
1932 --stack->ga_len; // drop the argument
1933 return OK;
1934}
1935
1936/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001937 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1938 * Argument count is already checked.
1939 */
1940 static int
1941generate_BLOBAPPEND(cctx_T *cctx)
1942{
1943 garray_T *stack = &cctx->ctx_type_stack;
1944 type_T *item_type;
1945
1946 // Caller already checked that blob_type is a blob.
1947 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001948 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001949 return FAIL;
1950
1951 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1952 return FAIL;
1953
1954 --stack->ga_len; // drop the argument
1955 return OK;
1956}
1957
1958/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001959 * Return TRUE if "ufunc" should be compiled, taking into account whether
1960 * "profile" indicates profiling is to be done.
1961 */
1962 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001963func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001964{
1965 switch (ufunc->uf_def_status)
1966 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001967 case UF_TO_BE_COMPILED:
1968 return TRUE;
1969
Bram Moolenaarb2049902021-01-24 12:53:53 +01001970 case UF_COMPILED:
1971 {
1972 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1973 + ufunc->uf_dfunc_idx;
1974
Bram Moolenaare99d4222021-06-13 14:01:26 +02001975 switch (compile_type)
1976 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001977 case CT_PROFILE:
1978#ifdef FEAT_PROFILE
1979 return dfunc->df_instr_prof == NULL;
1980#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001981 case CT_NONE:
1982 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001983 case CT_DEBUG:
1984 return dfunc->df_instr_debug == NULL;
1985 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001986 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001987
1988 case UF_NOT_COMPILED:
1989 case UF_COMPILE_ERROR:
1990 case UF_COMPILING:
1991 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001992 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001993 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001994}
1995
1996/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 * Generate an ISN_DCALL or ISN_UCALL instruction.
1998 * Return FAIL if the number of arguments is wrong.
1999 */
2000 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002001generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002{
2003 isn_T *isn;
2004 garray_T *stack = &cctx->ctx_type_stack;
2005 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002006 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007
Bram Moolenaar080457c2020-03-03 21:53:32 +01002008 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 if (argcount > regular_args && !has_varargs(ufunc))
2010 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002011 semsg(_(e_too_many_arguments_for_function_str),
2012 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 return FAIL;
2014 }
2015 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
2016 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002017 semsg(_(e_not_enough_arguments_for_function_str),
2018 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 return FAIL;
2020 }
2021
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002022 if (ufunc->uf_def_status != UF_NOT_COMPILED
2023 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002024 {
2025 int i;
2026
2027 for (i = 0; i < argcount; ++i)
2028 {
2029 type_T *expected;
2030 type_T *actual;
2031
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002032 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
2033 if (actual == &t_special
2034 && i >= regular_args - ufunc->uf_def_args.ga_len)
2035 {
2036 // assume v:none used for default argument value
2037 continue;
2038 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002039 if (i < regular_args)
2040 {
2041 if (ufunc->uf_arg_types == NULL)
2042 continue;
2043 expected = ufunc->uf_arg_types[i];
2044 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02002045 else if (ufunc->uf_va_type == NULL
2046 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02002047 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02002048 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002049 else
2050 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01002051 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002052 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002053 {
2054 arg_type_mismatch(expected, actual, i + 1);
2055 return FAIL;
2056 }
2057 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002058 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002059 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002060 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002061 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002062 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002063 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2064 {
2065 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2066 ufunc->uf_name);
2067 return FAIL;
2068 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002071 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002072 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002074 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 {
2076 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2077 isn->isn_arg.dfunc.cdf_argcount = argcount;
2078 }
2079 else
2080 {
2081 // A user function may be deleted and redefined later, can't use the
2082 // ufunc pointer, need to look it up again at runtime.
2083 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2084 isn->isn_arg.ufunc.cuf_argcount = argcount;
2085 }
2086
2087 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002088 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 return FAIL;
2090 // add return value
2091 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2092 ++stack->ga_len;
2093
2094 return OK;
2095}
2096
2097/*
2098 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2099 */
2100 static int
2101generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2102{
2103 isn_T *isn;
2104 garray_T *stack = &cctx->ctx_type_stack;
2105
Bram Moolenaar080457c2020-03-03 21:53:32 +01002106 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2108 return FAIL;
2109 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2110 isn->isn_arg.ufunc.cuf_argcount = argcount;
2111
2112 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002113 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002114 return FAIL;
2115 // add return value
2116 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2117 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118
2119 return OK;
2120}
2121
2122/*
2123 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002124 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 */
2126 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002127generate_PCALL(
2128 cctx_T *cctx,
2129 int argcount,
2130 char_u *name,
2131 type_T *type,
2132 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133{
2134 isn_T *isn;
2135 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002136 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137
Bram Moolenaar080457c2020-03-03 21:53:32 +01002138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002139
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002140 if (type->tt_type == VAR_ANY)
2141 ret_type = &t_any;
2142 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002143 {
2144 if (type->tt_argcount != -1)
2145 {
2146 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2147
2148 if (argcount < type->tt_min_argcount - varargs)
2149 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002150 semsg(_(e_not_enough_arguments_for_function_str), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002151 return FAIL;
2152 }
2153 if (!varargs && argcount > type->tt_argcount)
2154 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002155 semsg(_(e_too_many_arguments_for_function_str), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002156 return FAIL;
2157 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002158 if (type->tt_args != NULL)
2159 {
2160 int i;
2161
2162 for (i = 0; i < argcount; ++i)
2163 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002164 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002165 type_T *actual = ((type_T **)stack->ga_data)[
2166 stack->ga_len + offset];
2167 type_T *expected;
2168
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002169 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002170 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002171 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002172 else if (i >= type->tt_min_argcount
2173 && actual == &t_special)
2174 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002175 else
2176 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002177 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002178 cctx, TRUE, FALSE) == FAIL)
2179 {
2180 arg_type_mismatch(expected, actual, i + 1);
2181 return FAIL;
2182 }
2183 }
2184 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002185 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002186 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002187 if (ret_type == &t_unknown)
2188 // return type not known yet, use a runtime check
2189 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002190 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002191 else
2192 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002193 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002194 return FAIL;
2195 }
2196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2198 return FAIL;
2199 isn->isn_arg.pfunc.cpf_top = at_top;
2200 isn->isn_arg.pfunc.cpf_argcount = argcount;
2201
2202 stack->ga_len -= argcount; // drop the arguments
2203
2204 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002205 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002207 // If partial is above the arguments it must be cleared and replaced with
2208 // the return value.
2209 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2210 return FAIL;
2211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 return OK;
2213}
2214
2215/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002216 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 */
2218 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002219generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220{
2221 isn_T *isn;
2222 garray_T *stack = &cctx->ctx_type_stack;
2223 type_T *type;
2224
Bram Moolenaar080457c2020-03-03 21:53:32 +01002225 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002226 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002228 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002230 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002232 if (type->tt_type != VAR_DICT && type != &t_any)
2233 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002234 char *tofree;
2235
2236 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2237 name, type_name(type, &tofree));
2238 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002239 return FAIL;
2240 }
2241 // change dict type to dict member type
2242 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002243 {
2244 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2245 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2246 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247
2248 return OK;
2249}
2250
2251/*
2252 * Generate an ISN_ECHO instruction.
2253 */
2254 static int
2255generate_ECHO(cctx_T *cctx, int with_white, int count)
2256{
2257 isn_T *isn;
2258
Bram Moolenaar080457c2020-03-03 21:53:32 +01002259 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2261 return FAIL;
2262 isn->isn_arg.echo.echo_with_white = with_white;
2263 isn->isn_arg.echo.echo_count = count;
2264
2265 return OK;
2266}
2267
Bram Moolenaarad39c092020-02-26 18:23:43 +01002268/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002269 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002270 */
2271 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002272generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002273{
2274 isn_T *isn;
2275
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002276 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002277 return FAIL;
2278 isn->isn_arg.number = count;
2279
2280 return OK;
2281}
2282
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002283/*
2284 * Generate an ISN_PUT instruction.
2285 */
2286 static int
2287generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2288{
2289 isn_T *isn;
2290
2291 RETURN_OK_IF_SKIP(cctx);
2292 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2293 return FAIL;
2294 isn->isn_arg.put.put_regname = regname;
2295 isn->isn_arg.put.put_lnum = lnum;
2296 return OK;
2297}
2298
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002299/*
2300 * Generate an EXEC instruction that takes a string argument.
2301 * A copy is made of "line".
2302 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 static int
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002304generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305{
2306 isn_T *isn;
2307
Bram Moolenaar080457c2020-03-03 21:53:32 +01002308 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002309 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 return FAIL;
2311 isn->isn_arg.string = vim_strsave(line);
2312 return OK;
2313}
2314
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002315/*
2316 * Generate an EXEC instruction that takes a string argument.
2317 * "str" must be allocated, it is consumed.
2318 */
2319 static int
2320generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2321{
2322 isn_T *isn;
2323
2324 if (cctx->ctx_skip == SKIP_YES)
2325 {
2326 vim_free(str);
2327 return OK;
2328 }
2329 if ((isn = generate_instr(cctx, isntype)) == NULL)
2330 {
2331 vim_free(str);
2332 return FAIL;
2333 }
2334 isn->isn_arg.string = str;
2335 return OK;
2336}
2337
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002338 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002339generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2340{
2341 isn_T *isn;
2342 garray_T *stack = &cctx->ctx_type_stack;
2343
2344 RETURN_OK_IF_SKIP(cctx);
2345 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2346 return FAIL;
2347 isn->isn_arg.string = vim_strsave(line);
2348
Bram Moolenaar35578162021-08-02 19:10:38 +02002349 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002350 return FAIL;
2351 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2352 ++stack->ga_len;
2353
2354 return OK;
2355}
2356
2357 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002358generate_EXECCONCAT(cctx_T *cctx, int count)
2359{
2360 isn_T *isn;
2361
2362 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2363 return FAIL;
2364 isn->isn_arg.number = count;
2365 return OK;
2366}
2367
Bram Moolenaar08597872020-12-10 19:43:40 +01002368/*
2369 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2370 */
2371 static int
2372generate_RANGE(cctx_T *cctx, char_u *range)
2373{
2374 isn_T *isn;
2375 garray_T *stack = &cctx->ctx_type_stack;
2376
2377 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2378 return FAIL;
2379 isn->isn_arg.string = range;
2380
Bram Moolenaar35578162021-08-02 19:10:38 +02002381 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002382 return FAIL;
2383 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2384 ++stack->ga_len;
2385 return OK;
2386}
2387
Bram Moolenaar792f7862020-11-23 08:31:18 +01002388 static int
2389generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2390{
2391 isn_T *isn;
2392
2393 RETURN_OK_IF_SKIP(cctx);
2394 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2395 return FAIL;
2396 isn->isn_arg.unpack.unp_count = var_count;
2397 isn->isn_arg.unpack.unp_semicolon = semicolon;
2398 return OK;
2399}
2400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002402 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002403 */
2404 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002405generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002406{
2407 isn_T *isn;
2408
Bram Moolenaar917c46a2021-08-10 19:53:01 +02002409 if (has_cmdmod(cmod, FALSE))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002410 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002411 cctx->ctx_has_cmdmod = TRUE;
2412
2413 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002414 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002415 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2416 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2417 return FAIL;
2418 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002419 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002420 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002421 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002422
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002423 return OK;
2424}
2425
2426 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002427generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002428{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002429 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2430 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002431 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002432 return OK;
2433}
2434
Bram Moolenaarfa984412021-03-25 22:15:28 +01002435 static int
2436misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002437{
2438 garray_T *instr = &cctx->ctx_instr;
2439
Bram Moolenaara91a7132021-03-25 21:12:15 +01002440 if (cctx->ctx_has_cmdmod
2441 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2442 == ISN_CMDMOD)
2443 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002444 emsg(_(e_misplaced_command_modifier));
2445 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002446 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002447 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002448}
2449
2450/*
2451 * Get the index of the current instruction.
Bram Moolenaar093165c2021-08-22 13:35:31 +02002452 * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
Bram Moolenaara91a7132021-03-25 21:12:15 +01002453 */
2454 static int
2455current_instr_idx(cctx_T *cctx)
2456{
2457 garray_T *instr = &cctx->ctx_instr;
2458 int idx = instr->ga_len;
2459
Bram Moolenaare99d4222021-06-13 14:01:26 +02002460 while (idx > 0)
2461 {
2462 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002463 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002464 {
2465 --idx;
2466 continue;
2467 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002468#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002469 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2470 {
2471 --idx;
2472 continue;
2473 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002474#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002475 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2476 {
2477 --idx;
2478 continue;
2479 }
2480 break;
2481 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002482 return idx;
2483}
2484
Bram Moolenaarf002a412021-01-24 13:34:18 +01002485#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002486 static void
2487may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2488{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002489 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002490 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002491}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002492#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002493
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002494/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002496 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002498 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002499reserve_local(
2500 cctx_T *cctx,
2501 char_u *name,
2502 size_t len,
2503 int isConst,
2504 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002507 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002509 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002511 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002512 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 }
2514
Bram Moolenaar35578162021-08-02 19:10:38 +02002515 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002516 return NULL;
2517 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002518 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002520 // Every local variable uses the next entry on the stack. We could re-use
2521 // the last ones when leaving a scope, but then variables used in a closure
2522 // might get overwritten. To keep things simple do not re-use stack
2523 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002524 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2525 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002526
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002527 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 lvar->lv_const = isConst;
2529 lvar->lv_type = type;
2530
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002531 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002532 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002533 return NULL;
2534 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2535 vim_strsave(lvar->lv_name);
2536 ++dfunc->df_var_names.ga_len;
2537
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002538 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539}
2540
2541/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002542 * Remove local variables above "new_top".
2543 */
2544 static void
2545unwind_locals(cctx_T *cctx, int new_top)
2546{
2547 if (cctx->ctx_locals.ga_len > new_top)
2548 {
2549 int idx;
2550 lvar_T *lvar;
2551
2552 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2553 {
2554 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2555 vim_free(lvar->lv_name);
2556 }
2557 }
2558 cctx->ctx_locals.ga_len = new_top;
2559}
2560
2561/*
2562 * Free all local variables.
2563 */
2564 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002565free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002566{
2567 unwind_locals(cctx, 0);
2568 ga_clear(&cctx->ctx_locals);
2569}
2570
2571/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002572 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2573 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2574 * error if the variable was defined with :const.
2575 */
2576 static int
2577check_item_writable(svar_T *sv, int check_writable, char_u *name)
2578{
2579 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2580 || (check_writable == ASSIGN_FINAL
2581 && sv->sv_const == ASSIGN_CONST))
2582 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002583 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002584 return FAIL;
2585 }
2586 return OK;
2587}
2588
2589/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002591 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 * Returns the index in "sn_var_vals" if found.
2593 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002594 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 */
2596 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002597get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598{
2599 hashtab_T *ht;
2600 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002601 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002602 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 int idx;
2604
Bram Moolenaare3d46852020-08-29 13:39:17 +02002605 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002607 if (sid == current_sctx.sc_sid)
2608 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002609 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002610
2611 if (sav == NULL)
2612 return -2;
2613 idx = sav->sav_var_vals_idx;
2614 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002615 if (check_item_writable(sv, check_writable, name) == FAIL)
2616 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002617 return idx;
2618 }
2619
2620 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 ht = &SCRIPT_VARS(sid);
2622 di = find_var_in_ht(ht, 0, name, TRUE);
2623 if (di == NULL)
2624 return -2;
2625
2626 // Now find the svar_T index in sn_var_vals.
2627 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2628 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002629 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 if (sv->sv_tv == &di->di_tv)
2631 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002632 if (check_item_writable(sv, check_writable, name) == FAIL)
2633 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 return idx;
2635 }
2636 }
2637 return -1;
2638}
2639
2640/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002641 * Find "name" in imported items of the current script or in "cctx" if not
2642 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 */
2644 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002645find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 int idx;
2648
Bram Moolenaare3d46852020-08-29 13:39:17 +02002649 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002650 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 if (cctx != NULL)
2652 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2653 {
2654 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2655 + idx;
2656
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002657 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2658 : STRLEN(import->imp_name) == len
2659 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 return import;
2661 }
2662
Bram Moolenaarefa94442020-08-08 22:16:00 +02002663 return find_imported_in_script(name, len, current_sctx.sc_sid);
2664}
2665
2666 imported_T *
2667find_imported_in_script(char_u *name, size_t len, int sid)
2668{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002669 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002670 int idx;
2671
Bram Moolenaare3d46852020-08-29 13:39:17 +02002672 if (!SCRIPT_ID_VALID(sid))
2673 return NULL;
2674 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2676 {
2677 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2678
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002679 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2680 : STRLEN(import->imp_name) == len
2681 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 return import;
2683 }
2684 return NULL;
2685}
2686
2687/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002688 * Free all imported variables.
2689 */
2690 static void
2691free_imported(cctx_T *cctx)
2692{
2693 int idx;
2694
2695 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2696 {
2697 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2698
2699 vim_free(import->imp_name);
2700 }
2701 ga_clear(&cctx->ctx_imports);
2702}
2703
2704/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002705 * Return a pointer to the next line that isn't empty or only contains a
2706 * comment. Skips over white space.
2707 * Returns NULL if there is none.
2708 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002709 char_u *
2710peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002711{
2712 int lnum = cctx->ctx_lnum;
2713
2714 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2715 {
2716 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002717 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002718
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002719 // ignore NULLs inserted for continuation lines
2720 if (line != NULL)
2721 {
2722 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002723 if (vim9_bad_comment(p))
2724 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002725 if (*p != NUL && !vim9_comment_start(p))
2726 return p;
2727 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002728 }
2729 return NULL;
2730}
2731
2732/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002733 * Called when checking for a following operator at "arg". When the rest of
2734 * the line is empty or only a comment, peek the next line. If there is a next
2735 * line return a pointer to it and set "nextp".
2736 * Otherwise skip over white space.
2737 */
2738 static char_u *
2739may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2740{
2741 char_u *p = skipwhite(arg);
2742
2743 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002744 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002745 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002746 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002747 if (*nextp != NULL)
2748 return *nextp;
2749 }
2750 return p;
2751}
2752
2753/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002754 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002755 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002756 * Returns NULL when at the end.
2757 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002758 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002759next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002760{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002761 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002762
2763 do
2764 {
2765 ++cctx->ctx_lnum;
2766 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002767 {
2768 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002769 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002770 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002771 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002772 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002773 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002774 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002775 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002776 return line;
2777}
2778
2779/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002780 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002781 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002782 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002783 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2784 */
2785 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002786may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002787{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002788 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002789 if (vim9_bad_comment(*arg))
2790 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002791 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002792 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002793 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002794
2795 if (next == NULL)
2796 return FAIL;
2797 *arg = skipwhite(next);
2798 }
2799 return OK;
2800}
2801
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002802/*
2803 * Idem, and give an error when failed.
2804 */
2805 static int
2806may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2807{
2808 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2809 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002810 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002811 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002812 return FAIL;
2813 }
2814 return OK;
2815}
2816
2817
Bram Moolenaara5565e42020-05-09 15:44:01 +02002818// Structure passed between the compile_expr* functions to keep track of
2819// constants that have been parsed but for which no code was produced yet. If
2820// possible expressions on these constants are applied at compile time. If
2821// that is not possible, the code to push the constants needs to be generated
2822// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002823// Using 50 should be more than enough of 5 levels of ().
2824#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002825typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002826 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002827 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002828 int pp_is_const; // all generated code was constants, used for a
2829 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002830} ppconst_T;
2831
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002832static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002833static int compile_expr0(char_u **arg, cctx_T *cctx);
2834static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2835
Bram Moolenaara5565e42020-05-09 15:44:01 +02002836/*
2837 * Generate a PUSH instruction for "tv".
2838 * "tv" will be consumed or cleared.
2839 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2840 */
2841 static int
2842generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2843{
2844 if (tv != NULL)
2845 {
2846 switch (tv->v_type)
2847 {
2848 case VAR_UNKNOWN:
2849 break;
2850 case VAR_BOOL:
2851 generate_PUSHBOOL(cctx, tv->vval.v_number);
2852 break;
2853 case VAR_SPECIAL:
2854 generate_PUSHSPEC(cctx, tv->vval.v_number);
2855 break;
2856 case VAR_NUMBER:
2857 generate_PUSHNR(cctx, tv->vval.v_number);
2858 break;
2859#ifdef FEAT_FLOAT
2860 case VAR_FLOAT:
2861 generate_PUSHF(cctx, tv->vval.v_float);
2862 break;
2863#endif
2864 case VAR_BLOB:
2865 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2866 tv->vval.v_blob = NULL;
2867 break;
2868 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002869 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002870 tv->vval.v_string = NULL;
2871 break;
2872 default:
2873 iemsg("constant type not supported");
2874 clear_tv(tv);
2875 return FAIL;
2876 }
2877 tv->v_type = VAR_UNKNOWN;
2878 }
2879 return OK;
2880}
2881
2882/*
2883 * Generate code for any ppconst entries.
2884 */
2885 static int
2886generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2887{
2888 int i;
2889 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002890 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002891
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002892 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002893 for (i = 0; i < ppconst->pp_used; ++i)
2894 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2895 ret = FAIL;
2896 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002897 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002898 return ret;
2899}
2900
2901/*
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02002902 * Check that the last item of "ppconst" is a bool, if there is an item.
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002903 */
2904 static int
2905check_ppconst_bool(ppconst_T *ppconst)
2906{
2907 if (ppconst->pp_used > 0)
2908 {
2909 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002910 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002911
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002912 return check_typval_type(&t_bool, tv, where);
2913 }
2914 return OK;
2915}
2916
2917/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002918 * Clear ppconst constants. Used when failing.
2919 */
2920 static void
2921clear_ppconst(ppconst_T *ppconst)
2922{
2923 int i;
2924
2925 for (i = 0; i < ppconst->pp_used; ++i)
2926 clear_tv(&ppconst->pp_tv[i]);
2927 ppconst->pp_used = 0;
2928}
2929
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002930/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002931 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002932 * indexable value and the index or the two indexes of a slice.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002933 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002934 */
2935 static int
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002936compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002937{
2938 type_T **typep;
2939 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002940 vartype_T vartype;
2941 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002942
Bram Moolenaar261417b2021-05-06 21:04:55 +02002943 // We can index a list, dict and blob. If we don't know the type
2944 // we can use the index value type. If we still don't know use an "ANY"
2945 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002946 typep = ((type_T **)stack->ga_data) + stack->ga_len
2947 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002948 vartype = (*typep)->tt_type;
2949 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002950 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002951 if (*typep == &t_any && idxtype == &t_string)
2952 vartype = VAR_DICT;
2953 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002954 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002955 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002956 return FAIL;
2957 if (is_slice)
2958 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002959 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2960 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002961 FALSE, FALSE) == FAIL)
2962 return FAIL;
2963 }
2964 }
2965
Bram Moolenaar261417b2021-05-06 21:04:55 +02002966 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002967 {
2968 if (is_slice)
2969 {
2970 emsg(_(e_cannot_slice_dictionary));
2971 return FAIL;
2972 }
2973 if ((*typep)->tt_type == VAR_DICT)
2974 {
2975 *typep = (*typep)->tt_member;
2976 if (*typep == &t_unknown)
2977 // empty dict was used
2978 *typep = &t_any;
2979 }
2980 else
2981 {
2982 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2983 FALSE, FALSE) == FAIL)
2984 return FAIL;
2985 *typep = &t_any;
2986 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002987 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002988 return FAIL;
2989 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2990 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002991 if (keeping_dict != NULL)
2992 *keeping_dict = TRUE;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002993 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002994 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002995 {
2996 *typep = &t_string;
2997 if ((is_slice
2998 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2999 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
3000 return FAIL;
3001 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02003002 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003003 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003004 if (is_slice)
3005 {
3006 *typep = &t_blob;
3007 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
3008 return FAIL;
3009 }
3010 else
3011 {
3012 *typep = &t_number;
3013 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
3014 return FAIL;
3015 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02003016 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02003017 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003018 {
3019 if (is_slice)
3020 {
3021 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02003022 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02003023 2) == FAIL)
3024 return FAIL;
3025 }
3026 else
3027 {
3028 if ((*typep)->tt_type == VAR_LIST)
3029 {
3030 *typep = (*typep)->tt_member;
3031 if (*typep == &t_unknown)
3032 // empty list was used
3033 *typep = &t_any;
3034 }
3035 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02003036 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
3037 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003038 return FAIL;
3039 }
3040 }
3041 else
3042 {
3043 emsg(_(e_string_list_dict_or_blob_required));
3044 return FAIL;
3045 }
3046 return OK;
3047}
3048
3049/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003050 * Generate an instruction to load script-local variable "name", without the
3051 * leading "s:".
3052 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 */
3054 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003055compile_load_scriptvar(
3056 cctx_T *cctx,
3057 char_u *name, // variable NUL terminated
3058 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003059 char_u **end, // end of variable
3060 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061{
Bram Moolenaare3d46852020-08-29 13:39:17 +02003062 scriptitem_T *si;
3063 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 imported_T *import;
3065
Bram Moolenaare3d46852020-08-29 13:39:17 +02003066 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3067 return FAIL;
3068 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003069 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003070 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003072 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003073 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3074 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 }
3076 if (idx >= 0)
3077 {
3078 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3079
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003080 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 current_sctx.sc_sid, idx, sv->sv_type);
3082 return OK;
3083 }
3084
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003085 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 if (import != NULL)
3087 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003088 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003089 {
3090 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003091 char_u *exp_name;
3092 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003093 ufunc_T *ufunc;
3094 type_T *type;
3095
3096 // Used "import * as Name", need to lookup the member.
3097 if (*p != '.')
3098 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003099 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003100 return FAIL;
3101 }
3102 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003103 if (VIM_ISWHITE(*p))
3104 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003105 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003106 return FAIL;
3107 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003108
Bram Moolenaar1c991142020-07-04 13:15:31 +02003109 // isolate one name
3110 exp_name = p;
3111 while (eval_isnamec(*p))
3112 ++p;
3113 cc = *p;
3114 *p = NUL;
3115
Bram Moolenaaredba7072021-03-13 21:14:18 +01003116 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3117 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003118 *p = cc;
3119 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003120 *end = p;
3121
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003122 if (idx < 0)
3123 {
3124 if (*p == '(' && ufunc != NULL)
3125 {
3126 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3127 return OK;
3128 }
3129 return FAIL;
3130 }
3131
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003132 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3133 import->imp_sid,
3134 idx,
3135 type);
3136 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003137 else if (import->imp_funcname != NULL)
3138 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003139 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003140 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3141 import->imp_sid,
3142 import->imp_var_vals_idx,
3143 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 return OK;
3145 }
3146
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003147 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003148 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 return FAIL;
3150}
3151
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003152 static int
3153generate_funcref(cctx_T *cctx, char_u *name)
3154{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003155 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003156
3157 if (ufunc == NULL)
3158 return FAIL;
3159
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003160 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003161 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3162 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003163 == FAIL)
3164 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003165 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003166}
3167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168/*
3169 * Compile a variable name into a load instruction.
3170 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003171 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 * When "error" is FALSE do not give an error when not found.
3173 */
3174 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003175compile_load(
3176 char_u **arg,
3177 char_u *end_arg,
3178 cctx_T *cctx,
3179 int is_expr,
3180 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181{
3182 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003183 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003184 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003186 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187
3188 if (*(*arg + 1) == ':')
3189 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003190 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003191 {
3192 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193
Bram Moolenaarfa596382021-04-07 21:58:16 +02003194 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003195 switch (**arg)
3196 {
3197 case 'g': isn_type = ISN_LOADGDICT; break;
3198 case 'w': isn_type = ISN_LOADWDICT; break;
3199 case 't': isn_type = ISN_LOADTDICT; break;
3200 case 'b': isn_type = ISN_LOADBDICT; break;
3201 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003202 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003203 goto theend;
3204 }
3205 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3206 goto theend;
3207 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 else
3210 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003211 isntype_T isn_type = ISN_DROP;
3212
Bram Moolenaarfa596382021-04-07 21:58:16 +02003213 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003214 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3215 if (name == NULL)
3216 return FAIL;
3217
3218 switch (**arg)
3219 {
3220 case 'v': res = generate_LOADV(cctx, name, error);
3221 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003222 case 's': if (is_expr && ASCII_ISUPPER(*name)
3223 && find_func(name, FALSE, cctx) != NULL)
3224 res = generate_funcref(cctx, name);
3225 else
3226 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003227 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003228 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003229 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003230 {
3231 if (is_expr && ASCII_ISUPPER(*name)
3232 && find_func(name, FALSE, cctx) != NULL)
3233 res = generate_funcref(cctx, name);
3234 else
3235 isn_type = ISN_LOADG;
3236 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003237 else
3238 {
3239 isn_type = ISN_LOADAUTO;
3240 vim_free(name);
3241 name = vim_strnsave(*arg, end - *arg);
3242 if (name == NULL)
3243 return FAIL;
3244 }
3245 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003246 case 'w': isn_type = ISN_LOADW; break;
3247 case 't': isn_type = ISN_LOADT; break;
3248 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003249 default: // cannot happen, just in case
3250 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003251 goto theend;
3252 }
3253 if (isn_type != ISN_DROP)
3254 {
3255 // Global, Buffer-local, Window-local and Tabpage-local
3256 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003257 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003258 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 }
3261 }
3262 else
3263 {
3264 size_t len = end - *arg;
3265 int idx;
3266 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003267 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268
3269 name = vim_strnsave(*arg, end - *arg);
3270 if (name == NULL)
3271 return FAIL;
3272
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003273 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3274 {
3275 script_autoload(name, FALSE);
3276 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3277 }
3278 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3279 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003281 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003282 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 }
3284 else
3285 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003286 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003287
Bram Moolenaar709664c2020-12-12 14:33:41 +01003288 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003290 type = lvar.lv_type;
3291 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003292 if (lvar.lv_from_outer != 0)
3293 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003294 else
3295 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 }
3297 else
3298 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003299 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003300 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003301 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003302 || find_imported(name, 0, cctx) != NULL)
3303 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003304
Bram Moolenaar0f769812020-09-12 18:32:34 +02003305 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003306 // uppercase letter it can be a user defined function.
3307 // generate_funcref() will fail if the function can't be found.
3308 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003309 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 }
3311 }
3312 if (gen_load)
3313 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003314 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003315 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003316 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003317 cctx->ctx_outer_used = TRUE;
3318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 }
3320
3321 *arg = end;
3322
3323theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003324 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003325 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 vim_free(name);
3327 return res;
3328}
3329
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003330 static void
3331clear_instr_ga(garray_T *gap)
3332{
3333 int idx;
3334
3335 for (idx = 0; idx < gap->ga_len; ++idx)
3336 delete_instr(((isn_T *)gap->ga_data) + idx);
3337 ga_clear(gap);
3338}
3339
3340/*
3341 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3342 * Returns FAIL if compilation fails.
3343 */
3344 static int
3345compile_string(isn_T *isn, cctx_T *cctx)
3346{
3347 char_u *s = isn->isn_arg.string;
3348 garray_T save_ga = cctx->ctx_instr;
3349 int expr_res;
3350 int trailing_error;
3351 int instr_count;
3352 isn_T *instr = NULL;
3353
Bram Moolenaarcd268012021-07-22 19:11:08 +02003354 // Remove the string type from the stack.
3355 --cctx->ctx_type_stack.ga_len;
3356
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003357 // Temporarily reset the list of instructions so that the jump labels are
3358 // correct.
3359 cctx->ctx_instr.ga_len = 0;
3360 cctx->ctx_instr.ga_maxlen = 0;
3361 cctx->ctx_instr.ga_data = NULL;
3362 expr_res = compile_expr0(&s, cctx);
3363 s = skipwhite(s);
3364 trailing_error = *s != NUL;
3365
Bram Moolenaarff652882021-05-16 15:24:49 +02003366 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003367 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003368 {
3369 if (trailing_error)
3370 semsg(_(e_trailing_arg), s);
3371 clear_instr_ga(&cctx->ctx_instr);
3372 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003373 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003374 return FAIL;
3375 }
3376
3377 // Move the generated instructions into the ISN_INSTR instruction, then
3378 // restore the list of instructions.
3379 instr_count = cctx->ctx_instr.ga_len;
3380 instr = cctx->ctx_instr.ga_data;
3381 instr[instr_count].isn_type = ISN_FINISH;
3382
3383 cctx->ctx_instr = save_ga;
3384 vim_free(isn->isn_arg.string);
3385 isn->isn_type = ISN_INSTR;
3386 isn->isn_arg.instr = instr;
3387 return OK;
3388}
3389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390/*
3391 * Compile the argument expressions.
3392 * "arg" points to just after the "(" and is advanced to after the ")"
3393 */
3394 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003395compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003397 char_u *p = *arg;
3398 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003399 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003400 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401
Bram Moolenaare6085c52020-04-12 20:19:16 +02003402 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003404 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3405 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003406 if (*p == ')')
3407 {
3408 *arg = p + 1;
3409 return OK;
3410 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003411 if (must_end)
3412 {
3413 semsg(_(e_missing_comma_before_argument_str), p);
3414 return FAIL;
3415 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003416
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003417 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003418 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 return FAIL;
3420 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003421
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003422 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003423 && cctx->ctx_instr.ga_len == instr_count + 1)
3424 {
3425 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3426
3427 // {skip} argument of searchpair() can be compiled if not empty
3428 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3429 compile_string(isn, cctx);
3430 }
3431
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003432 if (*p != ',' && *skipwhite(p) == ',')
3433 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003434 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003435 p = skipwhite(p);
3436 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003438 {
3439 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003440 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003441 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003442 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003443 else
3444 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003445 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003446 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003448failret:
Bram Moolenaare1242042021-12-16 20:56:57 +00003449 emsg(_(e_missing_closing_paren));
Bram Moolenaare6085c52020-04-12 20:19:16 +02003450 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451}
3452
3453/*
3454 * Compile a function call: name(arg1, arg2)
3455 * "arg" points to "name", "arg + varlen" to the "(".
3456 * "argcount_init" is 1 for "value->method()"
3457 * Instructions:
3458 * EVAL arg1
3459 * EVAL arg2
3460 * BCALL / DCALL / UCALL
3461 */
3462 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003463compile_call(
3464 char_u **arg,
3465 size_t varlen,
3466 cctx_T *cctx,
3467 ppconst_T *ppconst,
3468 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469{
3470 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003471 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 int argcount = argcount_init;
3473 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003474 char_u fname_buf[FLEN_FIXED + 1];
3475 char_u *tofree = NULL;
3476 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003477 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003478 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003479 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003480 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003482 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003483 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003484 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003485 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003486 {
3487 char_u *s = skipwhite(*arg + varlen + 1);
3488 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003489 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003490
3491 argvars[0].v_type = VAR_UNKNOWN;
3492 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003493 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003494 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003495 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003496 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003497 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003498 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003499 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003500 {
3501 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3502
3503 *arg = s + 1;
3504 argvars[1].v_type = VAR_UNKNOWN;
3505 tv->v_type = VAR_NUMBER;
3506 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003507 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003508 f_has(argvars, tv);
3509 else
3510 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003511 clear_tv(&argvars[0]);
3512 ++ppconst->pp_used;
3513 return OK;
3514 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003515 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003516 if (!is_has)
3517 {
3518 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3519 return FAIL;
3520 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003521 }
3522
3523 if (generate_ppconst(cctx, ppconst) == FAIL)
3524 return FAIL;
3525
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 if (varlen >= sizeof(namebuf))
3527 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003528 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 return FAIL;
3530 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003531 vim_strncpy(namebuf, *arg, varlen);
3532 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003534 // We handle the "skip" argument of searchpair() and searchpairpos()
3535 // differently.
3536 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3537 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3538 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3539 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003542 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003543 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544
Bram Moolenaar03290b82020-12-19 16:30:44 +01003545 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003546 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 {
3548 int idx;
3549
3550 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003551 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003553 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003554 if (STRCMP(name, "flatten") == 0)
3555 {
3556 emsg(_(e_cannot_use_flatten_in_vim9_script));
3557 goto theend;
3558 }
3559
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003560 if (STRCMP(name, "add") == 0 && argcount == 2)
3561 {
3562 garray_T *stack = &cctx->ctx_type_stack;
3563 type_T *type = ((type_T **)stack->ga_data)[
3564 stack->ga_len - 2];
3565
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003566 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003567 if (type->tt_type == VAR_LIST)
3568 {
3569 // inline "add(list, item)" so that the type can be checked
3570 res = generate_LISTAPPEND(cctx);
3571 idx = -1;
3572 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003573 else if (type->tt_type == VAR_BLOB)
3574 {
3575 // inline "add(blob, nr)" so that the type can be checked
3576 res = generate_BLOBAPPEND(cctx);
3577 idx = -1;
3578 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003579 }
3580
3581 if (idx >= 0)
3582 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3583 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003584 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003585 semsg(_(e_unknown_function_str), namebuf);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003586 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 }
3588
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003589 // An argument or local variable can be a function reference, this
3590 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003591 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003592 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003593 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003594 // If we can find the function by name generate the right call.
3595 // Skip global functions here, a local funcref takes precedence.
3596 ufunc = find_func(name, FALSE, cctx);
3597 if (ufunc != NULL && !func_is_global(ufunc))
3598 {
3599 res = generate_CALL(cctx, ufunc, argcount);
3600 goto theend;
3601 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003602 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603
3604 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003605 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003606 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003608 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003609 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003610 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003611 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003612 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003613
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003614 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003615 goto theend;
3616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617
Bram Moolenaar0f769812020-09-12 18:32:34 +02003618 // If we can find a global function by name generate the right call.
3619 if (ufunc != NULL)
3620 {
3621 res = generate_CALL(cctx, ufunc, argcount);
3622 goto theend;
3623 }
3624
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003625 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003626 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003627 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003628 res = generate_UCALL(cctx, name, argcount);
3629 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003630 semsg(_(e_unknown_function_str), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003631
3632theend:
3633 vim_free(tofree);
3634 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635}
3636
3637// like NAMESPACE_CHAR but with 'a' and 'l'.
3638#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3639
3640/*
3641 * Find the end of a variable or function name. Unlike find_name_end() this
3642 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003643 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 * Return a pointer to just after the name. Equal to "arg" if there is no
3645 * valid name.
3646 */
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003647 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003648to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649{
3650 char_u *p;
3651
3652 // Quick check for valid starting character.
3653 if (!eval_isnamec1(*arg))
3654 return arg;
3655
3656 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3657 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3658 // and can be used in slice "[n:]".
3659 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003660 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3662 break;
3663 return p;
3664}
3665
3666/*
3667 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003668 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003669 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 */
3671 char_u *
3672to_name_const_end(char_u *arg)
3673{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003674 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 typval_T rettv;
3676
Bram Moolenaar678b2072021-07-26 21:10:11 +02003677 if (STRNCMP(p, "<SNR>", 5) == 0)
3678 p = skipdigits(p + 5);
3679 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 if (p == arg && *arg == '[')
3681 {
3682
3683 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003684 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 p = arg;
3686 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 return p;
3688}
3689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690/*
3691 * parse a list: [expr, expr]
3692 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003693 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 */
3695 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003696compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697{
3698 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003699 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003701 int is_const;
3702 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003704 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003706 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003707 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003708 semsg(_(e_list_end), *arg);
3709 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003710 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003711 if (*p == ',')
3712 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003713 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003714 return FAIL;
3715 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003716 if (*p == ']')
3717 {
3718 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003719 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003720 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003721 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003722 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003723 if (!is_const)
3724 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 ++count;
3726 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003727 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003729 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3730 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003731 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003732 return FAIL;
3733 }
3734 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003735 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 p = skipwhite(p);
3737 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003738 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003740 ppconst->pp_is_const = is_all_const;
3741 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742}
3743
3744/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003745 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003746 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003747 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 */
3749 static int
3750compile_lambda(char_u **arg, cctx_T *cctx)
3751{
Bram Moolenaare462f522020-12-27 14:43:30 +01003752 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 typval_T rettv;
3754 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003755 evalarg_T evalarg;
3756
Bram Moolenaar844fb642021-10-23 13:32:30 +01003757 init_evalarg(&evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003758 evalarg.eval_flags = EVAL_EVALUATE;
3759 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760
3761 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003762 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3763 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003764 {
3765 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003766 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003767 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003768
Bram Moolenaar65c44152020-12-24 15:14:01 +01003769 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003771 ++ufunc->uf_refcount;
3772 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773
Bram Moolenaara9931532021-06-12 15:58:16 +02003774 // Compile it here to get the return type. The return type is optional,
3775 // when it's missing use t_unknown. This is recognized in
3776 // compile_return().
3777 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3778 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003779 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003781 // When the outer function is compiled for profiling or debugging, the
3782 // lambda may be called without profiling or debugging. Compile it here in
3783 // the right context.
3784 if (cctx->ctx_compile_type == CT_DEBUG
Bram Moolenaar648594e2021-07-11 17:55:01 +02003785#ifdef FEAT_PROFILE
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003786 || cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar648594e2021-07-11 17:55:01 +02003787#endif
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003788 )
3789 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaard9162552021-07-11 15:26:13 +02003790
Bram Moolenaar844fb642021-10-23 13:32:30 +01003791 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
3792 // "*arg" may point into it. Point into the original line to avoid a
3793 // dangling pointer.
3794 if (evalarg.eval_using_cmdline)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003795 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003796 garray_T *gap = &evalarg.eval_tofree_ga;
3797 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003798
3799 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3800 + off;
3801 }
3802
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003803 clear_evalarg(&evalarg, NULL);
3804
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003805 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003806 {
3807 // The return type will now be known.
3808 set_function_type(ufunc);
3809
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003810 // The function reference count will be 1. When the ISN_FUNCREF
3811 // instruction is deleted the reference count is decremented and the
3812 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003813 return generate_FUNCREF(cctx, ufunc);
3814 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003815
3816 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 return FAIL;
3818}
3819
3820/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003821 * Get a lambda and compile it. Uses Vim9 syntax.
3822 */
3823 int
3824get_lambda_tv_and_compile(
3825 char_u **arg,
3826 typval_T *rettv,
3827 int types_optional,
3828 evalarg_T *evalarg)
3829{
3830 int r;
3831 ufunc_T *ufunc;
3832 int save_sc_version = current_sctx.sc_version;
3833
3834 // Get the funcref in "rettv".
3835 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3836 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3837 current_sctx.sc_version = save_sc_version;
3838 if (r != OK)
3839 return r;
3840
3841 // "rettv" will now be a partial referencing the function.
3842 ufunc = rettv->vval.v_partial->pt_func;
3843
3844 // Compile it here to get the return type. The return type is optional,
3845 // when it's missing use t_unknown. This is recognized in
3846 // compile_return().
3847 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3848 ufunc->uf_ret_type = &t_unknown;
3849 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3850
3851 if (ufunc->uf_def_status == UF_COMPILED)
3852 {
3853 // The return type will now be known.
3854 set_function_type(ufunc);
3855 return OK;
3856 }
3857 clear_tv(rettv);
3858 return FAIL;
3859}
3860
3861/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003862 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003864 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 */
3866 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003867compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868{
3869 garray_T *instr = &cctx->ctx_instr;
3870 int count = 0;
3871 dict_T *d = dict_alloc();
3872 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003873 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003874 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003875 int is_const;
3876 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877
3878 if (d == NULL)
3879 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003880 if (generate_ppconst(cctx, ppconst) == FAIL)
3881 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003882 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003884 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885
Bram Moolenaar23c55272020-06-21 16:58:13 +02003886 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003887 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003888 *arg = NULL;
3889 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003890 }
3891
3892 if (**arg == '}')
3893 break;
3894
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003895 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003897 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003899 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003900 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003901 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003904 if (isn->isn_type == ISN_PUSHNR)
3905 {
3906 char buf[NUMBUFLEN];
3907
3908 // Convert to string at compile time.
3909 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3910 isn->isn_type = ISN_PUSHS;
3911 isn->isn_arg.string = vim_strsave((char_u *)buf);
3912 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 if (isn->isn_type == ISN_PUSHS)
3914 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003915 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003916 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003917 *arg = skipwhite(*arg);
3918 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003919 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003920 emsg(_(e_missing_matching_bracket_after_dict_key));
3921 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003922 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003923 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003925 else
3926 {
3927 // {"name": value},
3928 // {'name': value},
3929 // {name: value} use "name" as a literal key
3930 key = get_literal_key(arg);
3931 if (key == NULL)
3932 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003933 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003934 return FAIL;
3935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936
3937 // Check for duplicate keys, if using string keys.
3938 if (key != NULL)
3939 {
3940 item = dict_find(d, key, -1);
3941 if (item != NULL)
3942 {
3943 semsg(_(e_duplicate_key), key);
3944 goto failret;
3945 }
3946 item = dictitem_alloc(key);
3947 if (item != NULL)
3948 {
3949 item->di_tv.v_type = VAR_UNKNOWN;
3950 item->di_tv.v_lock = 0;
3951 if (dict_add(d, item) == FAIL)
3952 dictitem_free(item);
3953 }
3954 }
3955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 if (**arg != ':')
3957 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003958 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003959 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003960 else
3961 semsg(_(e_missing_dict_colon), *arg);
3962 return FAIL;
3963 }
3964 whitep = *arg + 1;
3965 if (!IS_WHITE_OR_NUL(*whitep))
3966 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003967 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 return FAIL;
3969 }
3970
Bram Moolenaar23c55272020-06-21 16:58:13 +02003971 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003972 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003973 *arg = NULL;
3974 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003975 }
3976
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003977 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003979 if (!is_const)
3980 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 ++count;
3982
Bram Moolenaar2c330432020-04-13 14:41:35 +02003983 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003984 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003985 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003986 *arg = NULL;
3987 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003988 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 if (**arg == '}')
3990 break;
3991 if (**arg != ',')
3992 {
3993 semsg(_(e_missing_dict_comma), *arg);
3994 goto failret;
3995 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003996 if (IS_WHITE_OR_NUL(*whitep))
3997 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003998 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003999 return FAIL;
4000 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02004001 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02004002 if (!IS_WHITE_OR_NUL(*whitep))
4003 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01004004 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02004005 return FAIL;
4006 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01004007 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 }
4009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 *arg = *arg + 1;
4011
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004012 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02004013 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004014 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004015 *arg += STRLEN(*arg);
4016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004018 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 return generate_NEWDICT(cctx, count);
4020
4021failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004022 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004023 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004024 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004025 *arg = (char_u *)"";
4026 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 dict_unref(d);
4028 return FAIL;
4029}
4030
4031/*
4032 * Compile "&option".
4033 */
4034 static int
4035compile_get_option(char_u **arg, cctx_T *cctx)
4036{
4037 typval_T rettv;
4038 char_u *start = *arg;
4039 int ret;
4040
4041 // parse the option and get the current value to get the type.
4042 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004043 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 if (ret == OK)
4045 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004046 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01004047 char_u *name = vim_strnsave(start, *arg - start);
4048 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
4049 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050
4051 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4052 vim_free(name);
4053 }
4054 clear_tv(&rettv);
4055
4056 return ret;
4057}
4058
4059/*
4060 * Compile "$VAR".
4061 */
4062 static int
4063compile_get_env(char_u **arg, cctx_T *cctx)
4064{
4065 char_u *start = *arg;
4066 int len;
4067 int ret;
4068 char_u *name;
4069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 ++*arg;
4071 len = get_env_len(arg);
4072 if (len == 0)
4073 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004074 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 return FAIL;
4076 }
4077
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004078 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 name = vim_strnsave(start, len + 1);
4080 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4081 vim_free(name);
4082 return ret;
4083}
4084
4085/*
4086 * Compile "@r".
4087 */
4088 static int
4089compile_get_register(char_u **arg, cctx_T *cctx)
4090{
4091 int ret;
4092
4093 ++*arg;
4094 if (**arg == NUL)
4095 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004096 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 return FAIL;
4098 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004099 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 {
4101 emsg_invreg(**arg);
4102 return FAIL;
4103 }
4104 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4105 ++*arg;
4106 return ret;
4107}
4108
4109/*
4110 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004111 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 */
4113 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004114apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004116 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117
4118 // this works from end to start
4119 while (p > start)
4120 {
4121 --p;
4122 if (*p == '-' || *p == '+')
4123 {
4124 // only '-' has an effect, for '+' we only check the type
4125#ifdef FEAT_FLOAT
4126 if (rettv->v_type == VAR_FLOAT)
4127 {
4128 if (*p == '-')
4129 rettv->vval.v_float = -rettv->vval.v_float;
4130 }
4131 else
4132#endif
4133 {
4134 varnumber_T val;
4135 int error = FALSE;
4136
4137 // tv_get_number_chk() accepts a string, but we don't want that
4138 // here
4139 if (check_not_string(rettv) == FAIL)
4140 return FAIL;
4141 val = tv_get_number_chk(rettv, &error);
4142 clear_tv(rettv);
4143 if (error)
4144 return FAIL;
4145 if (*p == '-')
4146 val = -val;
4147 rettv->v_type = VAR_NUMBER;
4148 rettv->vval.v_number = val;
4149 }
4150 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004151 else if (numeric_only)
4152 {
4153 ++p;
4154 break;
4155 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004156 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 {
4158 int v = tv2bool(rettv);
4159
4160 // '!' is permissive in the type.
4161 clear_tv(rettv);
4162 rettv->v_type = VAR_BOOL;
4163 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4164 }
4165 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004166 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 return OK;
4168}
4169
4170/*
4171 * Recognize v: variables that are constants and set "rettv".
4172 */
4173 static void
4174get_vim_constant(char_u **arg, typval_T *rettv)
4175{
4176 if (STRNCMP(*arg, "v:true", 6) == 0)
4177 {
4178 rettv->v_type = VAR_BOOL;
4179 rettv->vval.v_number = VVAL_TRUE;
4180 *arg += 6;
4181 }
4182 else if (STRNCMP(*arg, "v:false", 7) == 0)
4183 {
4184 rettv->v_type = VAR_BOOL;
4185 rettv->vval.v_number = VVAL_FALSE;
4186 *arg += 7;
4187 }
4188 else if (STRNCMP(*arg, "v:null", 6) == 0)
4189 {
4190 rettv->v_type = VAR_SPECIAL;
4191 rettv->vval.v_number = VVAL_NULL;
4192 *arg += 6;
4193 }
4194 else if (STRNCMP(*arg, "v:none", 6) == 0)
4195 {
4196 rettv->v_type = VAR_SPECIAL;
4197 rettv->vval.v_number = VVAL_NONE;
4198 *arg += 6;
4199 }
4200}
4201
Bram Moolenaar657137c2021-01-09 15:45:23 +01004202 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004203get_compare_type(char_u *p, int *len, int *type_is)
4204{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004205 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004206 int i;
4207
4208 switch (p[0])
4209 {
4210 case '=': if (p[1] == '=')
4211 type = EXPR_EQUAL;
4212 else if (p[1] == '~')
4213 type = EXPR_MATCH;
4214 break;
4215 case '!': if (p[1] == '=')
4216 type = EXPR_NEQUAL;
4217 else if (p[1] == '~')
4218 type = EXPR_NOMATCH;
4219 break;
4220 case '>': if (p[1] != '=')
4221 {
4222 type = EXPR_GREATER;
4223 *len = 1;
4224 }
4225 else
4226 type = EXPR_GEQUAL;
4227 break;
4228 case '<': if (p[1] != '=')
4229 {
4230 type = EXPR_SMALLER;
4231 *len = 1;
4232 }
4233 else
4234 type = EXPR_SEQUAL;
4235 break;
4236 case 'i': if (p[1] == 's')
4237 {
4238 // "is" and "isnot"; but not a prefix of a name
4239 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4240 *len = 5;
4241 i = p[*len];
4242 if (!isalnum(i) && i != '_')
4243 {
4244 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4245 *type_is = TRUE;
4246 }
4247 }
4248 break;
4249 }
4250 return type;
4251}
4252
4253/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004254 * Skip over an expression, ignoring most errors.
4255 */
4256 static void
4257skip_expr_cctx(char_u **arg, cctx_T *cctx)
4258{
4259 evalarg_T evalarg;
4260
Bram Moolenaar844fb642021-10-23 13:32:30 +01004261 init_evalarg(&evalarg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004262 evalarg.eval_cctx = cctx;
4263 skip_expr(arg, &evalarg);
Bram Moolenaar844fb642021-10-23 13:32:30 +01004264 clear_evalarg(&evalarg, NULL);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004265}
4266
4267/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004269 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 */
4271 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004272compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004274 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275
4276 // this works from end to start
4277 while (p > start)
4278 {
4279 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004280 while (VIM_ISWHITE(*p))
4281 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 if (*p == '-' || *p == '+')
4283 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004284 int negate = *p == '-';
4285 isn_T *isn;
4286 garray_T *stack = &cctx->ctx_type_stack;
4287 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004289 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4290 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4291 return FAIL;
4292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4294 {
4295 --p;
4296 if (*p == '-')
4297 negate = !negate;
4298 }
4299 // only '-' has an effect, for '+' we only check the type
4300 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004301 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004303 if (isn == NULL)
4304 return FAIL;
4305 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004307 else if (numeric_only)
4308 {
4309 ++p;
4310 break;
4311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 else
4313 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004314 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004316 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004318 if (p[-1] == '!')
4319 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004322 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 return FAIL;
4324 }
4325 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004326 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 return OK;
4328}
4329
4330/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004331 * Compile "(expression)": recursive!
4332 * Return FAIL/OK.
4333 */
4334 static int
4335compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4336{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004337 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004338 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004339
Bram Moolenaar24156692021-01-14 20:35:49 +01004340 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4341 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004342 if (ppconst->pp_used <= PPSIZE - 10)
4343 {
4344 ret = compile_expr1(arg, cctx, ppconst);
4345 }
4346 else
4347 {
4348 // Not enough space in ppconst, flush constants.
4349 if (generate_ppconst(cctx, ppconst) == FAIL)
4350 return FAIL;
4351 ret = compile_expr0(arg, cctx);
4352 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004353 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4354 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004355 if (**arg == ')')
4356 ++*arg;
4357 else if (ret == OK)
4358 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004359 emsg(_(e_missing_closing_paren));
Bram Moolenaar7e368202020-12-25 21:56:57 +01004360 ret = FAIL;
4361 }
4362 return ret;
4363}
4364
4365/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004367 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 */
4369 static int
4370compile_subscript(
4371 char_u **arg,
4372 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004373 char_u *start_leader,
4374 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004375 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004377 char_u *name_start = *end_leader;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004378 int keeping_dict = FALSE;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 for (;;)
4381 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004382 char_u *p = skipwhite(*arg);
4383
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004384 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004385 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004386 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004387
4388 // If a following line starts with "->{" or "->X" advance to that
4389 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004390 // Also if a following line starts with ".x".
4391 if (next != NULL &&
4392 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004393 && (next[2] == '{'
4394 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004395 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004396 {
4397 next = next_line_from_context(cctx, TRUE);
4398 if (next == NULL)
4399 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004400 *arg = next;
4401 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004402 }
4403 }
4404
Bram Moolenaarcd268012021-07-22 19:11:08 +02004405 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4406 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004407 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004409 garray_T *stack = &cctx->ctx_type_stack;
4410 type_T *type;
4411 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004413 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004414 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004415 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004418 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4419
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004420 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004421 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004423 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004425 if (keeping_dict)
4426 {
4427 keeping_dict = FALSE;
4428 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4429 return FAIL;
4430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004432 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004434 char_u *pstart = p;
4435
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004436 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004437 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004438 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 // something->method()
4441 // Apply the '!', '-' and '+' first:
4442 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004443 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004446 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004447 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004448 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004449 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004450 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004451 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004452 garray_T *stack = &cctx->ctx_type_stack;
4453 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004454 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004455 int expr_isn_start = cctx->ctx_instr.ga_len;
4456 int expr_isn_end;
4457 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004458
4459 // Funcref call: list->(Refs[2])(arg)
4460 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004461 //
4462 // Fist compile the function expression.
4463 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004464 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004465
4466 // Remember the next instruction index, where the instructions
4467 // for arguments are being written.
4468 expr_isn_end = cctx->ctx_instr.ga_len;
4469
4470 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004471 if (**arg != '(')
4472 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004473 if (*skipwhite(*arg) == '(')
4474 emsg(_(e_nowhitespace));
4475 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004476 semsg(_(e_missing_parenthesis_str), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004477 return FAIL;
4478 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004479 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004480 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004481 return FAIL;
4482
Bram Moolenaar2927c072021-04-05 19:41:21 +02004483 // Move the instructions for the arguments to before the
4484 // instructions of the expression and move the type of the
4485 // expression after the argument types. This is what ISN_PCALL
4486 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004487 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004488 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4489 if (arg_isn_count > 0)
4490 {
4491 int expr_isn_count = expr_isn_end - expr_isn_start;
4492 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4493
4494 if (isn == NULL)
4495 return FAIL;
4496 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4497 + expr_isn_start,
4498 sizeof(isn_T) * expr_isn_count);
4499 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4500 + expr_isn_start,
4501 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4502 sizeof(isn_T) * arg_isn_count);
4503 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4504 + expr_isn_start + arg_isn_count,
4505 isn, sizeof(isn_T) * expr_isn_count);
4506 vim_free(isn);
4507
4508 type = ((type_T **)stack->ga_data)[type_idx_start];
4509 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4510 ((type_T **)stack->ga_data) + type_idx_start + 1,
4511 sizeof(type_T *)
4512 * (stack->ga_len - type_idx_start - 1));
4513 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4514 }
4515
Bram Moolenaar7e368202020-12-25 21:56:57 +01004516 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004517 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 return FAIL;
4519 }
4520 else
4521 {
4522 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004523 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004524 if (!eval_isnamec1(*p))
4525 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004526 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004527 return FAIL;
4528 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004529 if (ASCII_ISALPHA(*p) && p[1] == ':')
4530 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004531 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 ;
4533 if (*p != '(')
4534 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004535 semsg(_(e_missing_parenthesis_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 return FAIL;
4537 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004538 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 return FAIL;
4540 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004541 if (keeping_dict)
4542 {
4543 keeping_dict = FALSE;
4544 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4545 return FAIL;
4546 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004548 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004550 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004551
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004552 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004553 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004554 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004555 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004556 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004557 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004558 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004559
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004560 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004561 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004562 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004563 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004564 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004565 // missing first index is equal to zero
4566 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004567 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004568 else
4569 {
4570 if (compile_expr0(arg, cctx) == FAIL)
4571 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004572 if (**arg == ':')
4573 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004574 semsg(_(e_white_space_required_before_and_after_str_at_str),
4575 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004576 return FAIL;
4577 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004578 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004579 return FAIL;
4580 *arg = skipwhite(*arg);
4581 }
4582 if (**arg == ':')
4583 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004584 is_slice = TRUE;
4585 ++*arg;
4586 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4587 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004588 semsg(_(e_white_space_required_before_and_after_str_at_str),
4589 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004590 return FAIL;
4591 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004592 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004593 return FAIL;
4594 if (**arg == ']')
4595 // missing second index is equal to end of string
4596 generate_PUSHNR(cctx, -1);
4597 else
4598 {
4599 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004600 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004601 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004602 return FAIL;
4603 *arg = skipwhite(*arg);
4604 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606
4607 if (**arg != ']')
4608 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004609 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 return FAIL;
4611 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004612 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004614 if (keeping_dict)
4615 {
4616 keeping_dict = FALSE;
4617 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4618 return FAIL;
4619 }
4620 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004621 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004623 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004625 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004626 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004627 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004628 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004629
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004630 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004631 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004632 {
4633 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004634 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004635 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004636 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004637 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 while (eval_isnamec(*p))
4639 MB_PTR_ADV(p);
4640 if (p == *arg)
4641 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004642 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 return FAIL;
4644 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004645 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
4646 return FAIL;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004647 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004649 keeping_dict = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 *arg = p;
4651 }
4652 else
4653 break;
4654 }
4655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 // Turn "dict.Func" into a partial for "Func" bound to "dict".
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004657 // This needs to be done at runtime to be able to check the type.
4658 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
4659 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660
4661 return OK;
4662}
4663
4664/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004665 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4666 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004668 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004669 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004670 *
4671 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 */
4673
4674/*
4675 * number number constant
4676 * 0zFFFFFFFF Blob constant
4677 * "string" string constant
4678 * 'string' literal string constant
4679 * &option-name option value
4680 * @r register contents
4681 * identifier variable value
4682 * function() function call
4683 * $VAR environment variable
4684 * (expression) nested expression
4685 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004686 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 *
4688 * Also handle:
4689 * ! in front logical NOT
4690 * - in front unary minus
4691 * + in front unary plus (ignored)
4692 * trailing (arg) funcref/partial call
4693 * trailing [] subscript in String or List
4694 * trailing .name entry in Dictionary
4695 * trailing ->name() method call
4696 */
4697 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004698compile_expr7(
4699 char_u **arg,
4700 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004701 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 char_u *start_leader, *end_leader;
4704 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004705 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004706 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004708 ppconst->pp_is_const = FALSE;
4709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 /*
4711 * Skip '!', '-' and '+' characters. They are handled later.
4712 */
4713 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004714 if (eval_leader(arg, TRUE) == FAIL)
4715 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 end_leader = *arg;
4717
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004718 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719 switch (**arg)
4720 {
4721 /*
4722 * Number constant.
4723 */
4724 case '0': // also for blob starting with 0z
4725 case '1':
4726 case '2':
4727 case '3':
4728 case '4':
4729 case '5':
4730 case '6':
4731 case '7':
4732 case '8':
4733 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004734 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004736 // Apply "-" and "+" just before the number now, right to
4737 // left. Matters especially when "->" follows. Stops at
4738 // '!'.
4739 if (apply_leader(rettv, TRUE,
4740 start_leader, &end_leader) == FAIL)
4741 {
4742 clear_tv(rettv);
4743 return FAIL;
4744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 break;
4746
4747 /*
4748 * String constant: "string".
4749 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004750 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004751 return FAIL;
4752 break;
4753
4754 /*
4755 * Literal string constant: 'str''ing'.
4756 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004757 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 return FAIL;
4759 break;
4760
4761 /*
4762 * Constant Vim variable.
4763 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004764 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 ret = NOTDONE;
4766 break;
4767
4768 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004769 * "true" constant
4770 */
4771 case 't': if (STRNCMP(*arg, "true", 4) == 0
4772 && !eval_isnamec((*arg)[4]))
4773 {
4774 *arg += 4;
4775 rettv->v_type = VAR_BOOL;
4776 rettv->vval.v_number = VVAL_TRUE;
4777 }
4778 else
4779 ret = NOTDONE;
4780 break;
4781
4782 /*
4783 * "false" constant
4784 */
4785 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4786 && !eval_isnamec((*arg)[5]))
4787 {
4788 *arg += 5;
4789 rettv->v_type = VAR_BOOL;
4790 rettv->vval.v_number = VVAL_FALSE;
4791 }
4792 else
4793 ret = NOTDONE;
4794 break;
4795
4796 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004797 * "null" constant
4798 */
4799 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004800 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004801 {
4802 *arg += 4;
4803 rettv->v_type = VAR_SPECIAL;
4804 rettv->vval.v_number = VVAL_NULL;
4805 }
4806 else
4807 ret = NOTDONE;
4808 break;
4809
4810 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 * List: [expr, expr]
4812 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004813 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4814 return FAIL;
4815 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 break;
4817
4818 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819 * Dictionary: {'key': val, 'key': val}
4820 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004821 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4822 return FAIL;
4823 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 break;
4825
4826 /*
4827 * Option value: &name
4828 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004829 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4830 return FAIL;
4831 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 break;
4833
4834 /*
4835 * Environment variable: $VAR.
4836 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004837 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4838 return FAIL;
4839 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 break;
4841
4842 /*
4843 * Register contents: @r.
4844 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004845 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4846 return FAIL;
4847 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 break;
4849 /*
4850 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004851 * lambda: (arg, arg) => expr
4852 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004854 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4855 ret = compile_lambda(arg, cctx);
4856 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004857 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 break;
4859
4860 default: ret = NOTDONE;
4861 break;
4862 }
4863 if (ret == FAIL)
4864 return FAIL;
4865
Bram Moolenaar1c747212020-05-09 18:28:34 +02004866 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004868 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004869 clear_tv(rettv);
4870 else
4871 // A constant expression can possibly be handled compile time,
4872 // return the value instead of generating code.
4873 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 }
4875 else if (ret == NOTDONE)
4876 {
4877 char_u *p;
4878 int r;
4879
4880 if (!eval_isnamec1(**arg))
4881 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004882 if (!vim9_bad_comment(*arg))
4883 {
4884 if (ends_excmd(*skipwhite(*arg)))
4885 semsg(_(e_empty_expression_str), *arg);
4886 else
4887 semsg(_(e_name_expected_str), *arg);
4888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 return FAIL;
4890 }
4891
4892 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004893 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004894 if (p - *arg == (size_t)1 && **arg == '_')
4895 {
4896 emsg(_(e_cannot_use_underscore_here));
4897 return FAIL;
4898 }
4899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004901 {
4902 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4903 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004905 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02004906 if (cctx->ctx_skip != SKIP_YES
4907 && generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004908 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004909 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004910 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 if (r == FAIL)
4912 return FAIL;
4913 }
4914
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004915 // Handle following "[]", ".member", etc.
4916 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004917 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004918 ppconst) == FAIL)
4919 return FAIL;
4920 if (ppconst->pp_used > 0)
4921 {
4922 // apply the '!', '-' and '+' before the constant
4923 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004924 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004925 return FAIL;
4926 return OK;
4927 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004928 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004930 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931}
4932
4933/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004934 * Give the "white on both sides" error, taking the operator from "p[len]".
4935 */
4936 void
4937error_white_both(char_u *op, int len)
4938{
4939 char_u buf[10];
4940
4941 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004942 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004943}
4944
4945/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004946 * <type>expr7: runtime type check / conversion
4947 */
4948 static int
4949compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4950{
4951 type_T *want_type = NULL;
4952
4953 // Recognize <type>
4954 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4955 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004956 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004957 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4958 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004959 return FAIL;
4960
4961 if (**arg != '>')
4962 {
4963 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004964 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004965 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004966 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004967 return FAIL;
4968 }
4969 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004970 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004971 return FAIL;
4972 }
4973
4974 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4975 return FAIL;
4976
4977 if (want_type != NULL)
4978 {
4979 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004980 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004981 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004982
Bram Moolenaard1103582020-08-14 22:44:25 +02004983 generate_ppconst(cctx, ppconst);
4984 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004985 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004986 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004987 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004988 return FAIL;
4989 }
4990 }
4991
4992 return OK;
4993}
4994
4995/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 * * number multiplication
4997 * / number division
4998 * % number modulo
4999 */
5000 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005001compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002{
5003 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005004 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005005 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005007 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005008 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009 return FAIL;
5010
5011 /*
5012 * Repeat computing, until no "*", "/" or "%" is following.
5013 */
5014 for (;;)
5015 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005016 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 if (*op != '*' && *op != '/' && *op != '%')
5018 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005019 if (next != NULL)
5020 {
5021 *arg = next_line_from_context(cctx, TRUE);
5022 op = skipwhite(*arg);
5023 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005024
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005025 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005027 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005028 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01005030 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005031 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005033 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005034 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005036
5037 if (ppconst->pp_used == ppconst_used + 2
5038 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5039 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005040 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005041 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5042 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
5043 varnumber_T res = 0;
5044 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005046 // both are numbers: compute the result
5047 switch (*op)
5048 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005049 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005050 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005051 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005052 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005053 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005054 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005055 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005056 break;
5057 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005058 if (failed)
5059 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005060 tv1->vval.v_number = res;
5061 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005062 }
5063 else
5064 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005065 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005066 generate_two_op(cctx, op);
5067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 }
5069
5070 return OK;
5071}
5072
5073/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01005074 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 * - number subtraction
5076 * .. string concatenation
5077 */
5078 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005079compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080{
5081 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005082 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005084 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085
5086 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005087 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088 return FAIL;
5089
5090 /*
5091 * Repeat computing, until no "+", "-" or ".." is following.
5092 */
5093 for (;;)
5094 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005095 op = may_peek_next_line(cctx, *arg, &next);
5096 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005098 if (op[0] == op[1] && *op != '.' && next)
5099 // Finding "++" or "--" on the next line is a separate command.
5100 // But ".." is concatenation.
5101 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005103 if (next != NULL)
5104 {
5105 *arg = next_line_from_context(cctx, TRUE);
5106 op = skipwhite(*arg);
5107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005109 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005111 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005112 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 }
5114
Bram Moolenaare0de1712020-12-02 17:36:54 +01005115 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005116 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005118 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005119 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120 return FAIL;
5121
Bram Moolenaara5565e42020-05-09 15:44:01 +02005122 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005123 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005124 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5125 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5126 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5127 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005129 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5130 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005131
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005132 // concat/subtract/add constant numbers
5133 if (*op == '+')
5134 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5135 else if (*op == '-')
5136 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5137 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005138 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005139 // concatenate constant strings
5140 char_u *s1 = tv1->vval.v_string;
5141 char_u *s2 = tv2->vval.v_string;
5142 size_t len1 = STRLEN(s1);
5143
5144 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5145 if (tv1->vval.v_string == NULL)
5146 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005147 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005148 return FAIL;
5149 }
5150 mch_memmove(tv1->vval.v_string, s1, len1);
5151 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005152 vim_free(s1);
5153 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005154 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005155 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005156 }
5157 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005158 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005159 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005160 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005161 if (*op == '.')
5162 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005163 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5164 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005165 return FAIL;
5166 generate_instr_drop(cctx, ISN_CONCAT, 1);
5167 }
5168 else
5169 generate_two_op(cctx, op);
5170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171 }
5172
5173 return OK;
5174}
5175
5176/*
5177 * expr5a == expr5b
5178 * expr5a =~ expr5b
5179 * expr5a != expr5b
5180 * expr5a !~ expr5b
5181 * expr5a > expr5b
5182 * expr5a >= expr5b
5183 * expr5a < expr5b
5184 * expr5a <= expr5b
5185 * expr5a is expr5b
5186 * expr5a isnot expr5b
5187 *
5188 * Produces instructions:
5189 * EVAL expr5a Push result of "expr5a"
5190 * EVAL expr5b Push result of "expr5b"
5191 * COMPARE one of the compare instructions
5192 */
5193 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005194compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005196 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005198 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005201 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202
5203 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005204 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 return FAIL;
5206
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005207 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005208 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209
5210 /*
5211 * If there is a comparative operator, use it.
5212 */
5213 if (type != EXPR_UNKNOWN)
5214 {
5215 int ic = FALSE; // Default: do not ignore case
5216
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005217 if (next != NULL)
5218 {
5219 *arg = next_line_from_context(cctx, TRUE);
5220 p = skipwhite(*arg);
5221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 if (type_is && (p[len] == '?' || p[len] == '#'))
5223 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005224 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 return FAIL;
5226 }
5227 // extra question mark appended: ignore case
5228 if (p[len] == '?')
5229 {
5230 ic = TRUE;
5231 ++len;
5232 }
5233 // extra '#' appended: match case (ignored)
5234 else if (p[len] == '#')
5235 ++len;
5236 // nothing appended: match case
5237
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005238 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005240 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005241 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005242 }
5243
5244 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005245 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005246 return FAIL;
5247
Bram Moolenaara5565e42020-05-09 15:44:01 +02005248 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005249 return FAIL;
5250
Bram Moolenaara5565e42020-05-09 15:44:01 +02005251 if (ppconst->pp_used == ppconst_used + 2)
5252 {
5253 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5254 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5255 int ret;
5256
5257 // Both sides are a constant, compute the result now.
5258 // First check for a valid combination of types, this is more
5259 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005260 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005261 ret = FAIL;
5262 else
5263 {
5264 ret = typval_compare(tv1, tv2, type, ic);
5265 tv1->v_type = VAR_BOOL;
5266 tv1->vval.v_number = tv1->vval.v_number
5267 ? VVAL_TRUE : VVAL_FALSE;
5268 clear_tv(tv2);
5269 --ppconst->pp_used;
5270 }
5271 return ret;
5272 }
5273
5274 generate_ppconst(cctx, ppconst);
5275 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005276 }
5277
5278 return OK;
5279}
5280
Bram Moolenaar7f141552020-05-09 17:35:53 +02005281static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5282
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005283/*
5284 * Compile || or &&.
5285 */
5286 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005287compile_and_or(
5288 char_u **arg,
5289 cctx_T *cctx,
5290 char *op,
5291 ppconst_T *ppconst,
5292 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005293{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005294 char_u *next;
5295 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296 int opchar = *op;
5297
5298 if (p[0] == opchar && p[1] == opchar)
5299 {
5300 garray_T *instr = &cctx->ctx_instr;
5301 garray_T end_ga;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005302 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303
5304 /*
5305 * Repeat until there is no following "||" or "&&"
5306 */
5307 ga_init2(&end_ga, sizeof(int), 10);
5308 while (p[0] == opchar && p[1] == opchar)
5309 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005310 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005311 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005312 int start_ctx_lnum = cctx->ctx_lnum;
5313 int save_lnum;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005314 int const_used;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005315 int status;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005316 jumpwhen_T jump_when = opchar == '|'
5317 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005318
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005319 if (next != NULL)
5320 {
5321 *arg = next_line_from_context(cctx, TRUE);
5322 p = skipwhite(*arg);
5323 }
5324
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005325 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5326 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005327 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005328 op, p);
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005329 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005330 return FAIL;
5331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005332
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005333 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005334 SOURCING_LNUM = start_lnum;
5335 save_lnum = cctx->ctx_lnum;
5336 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005337
5338 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005339 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005340 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005341 // Use the last ppconst if possible.
5342 if (ppconst->pp_used > 0)
5343 {
5344 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5345 int is_true = tv2bool(tv);
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005346
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005347 if ((is_true && opchar == '|')
5348 || (!is_true && opchar == '&'))
5349 {
5350 // For "false && expr" and "true || expr" the "expr"
5351 // does not need to be evaluated.
5352 cctx->ctx_skip = SKIP_YES;
5353 clear_tv(tv);
5354 tv->v_type = VAR_BOOL;
5355 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
5356 }
5357 else
5358 {
5359 // For "true && expr" and "false || expr" only "expr"
5360 // needs to be evaluated.
5361 --ppconst->pp_used;
5362 jump_when = JUMP_NEVER;
5363 }
5364 }
5365 else
5366 {
5367 // Every part must evaluate to a bool.
5368 status = bool_on_stack(cctx);
5369 }
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005370 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005371 if (status != FAIL)
5372 status = ga_grow(&end_ga, 1);
Bram Moolenaara7511c02021-04-03 21:47:07 +02005373 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005374 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005375 {
5376 ga_clear(&end_ga);
5377 return FAIL;
5378 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005379
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005380 if (jump_when != JUMP_NEVER)
5381 {
5382 if (cctx->ctx_skip != SKIP_YES)
5383 {
5384 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5385 ++end_ga.ga_len;
5386 }
5387 generate_JUMP(cctx, jump_when, 0);
5388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389
5390 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005391 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005392 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005393 {
5394 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005395 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005396 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005397
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005398 const_used = ppconst->pp_used;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005399 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5400 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005401 {
5402 ga_clear(&end_ga);
5403 return FAIL;
5404 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005405
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005406 // "0 || 1" results in true, "1 && 0" results in false.
5407 if (ppconst->pp_used == const_used + 1)
5408 {
5409 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5410
5411 if (tv->v_type == VAR_NUMBER
5412 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
5413 {
5414 tv->vval.v_number = tv->vval.v_number == 1
5415 ? VVAL_TRUE : VVAL_FALSE;
5416 tv->v_type = VAR_BOOL;
5417 }
5418 }
5419
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005420 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005422
5423 if (check_ppconst_bool(ppconst) == FAIL)
5424 {
5425 ga_clear(&end_ga);
5426 return FAIL;
5427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005429 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
5430 // Every part must evaluate to a bool.
5431 if (bool_on_stack(cctx) == FAIL)
5432 {
5433 ga_clear(&end_ga);
5434 return FAIL;
5435 }
5436
5437 if (end_ga.ga_len > 0)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005438 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005439 // Fill in the end label in all jumps.
5440 generate_ppconst(cctx, ppconst);
5441 while (end_ga.ga_len > 0)
5442 {
5443 isn_T *isn;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005444
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005445 --end_ga.ga_len;
5446 isn = ((isn_T *)instr->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005448 isn->isn_arg.jump.jump_where = instr->ga_len;
5449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450 }
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005451 ga_clear(&end_ga);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005452
5453 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454 }
5455
5456 return OK;
5457}
5458
5459/*
5460 * expr4a && expr4a && expr4a logical AND
5461 *
5462 * Produces instructions:
5463 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005464 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005465 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005467 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468 * EVAL expr4c Push result of "expr4c"
5469 * end:
5470 */
5471 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005472compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005473{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005474 int ppconst_used = ppconst->pp_used;
5475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005477 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005478 return FAIL;
5479
5480 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005481 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482}
5483
5484/*
5485 * expr3a || expr3b || expr3c logical OR
5486 *
5487 * Produces instructions:
5488 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005489 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005490 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005492 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493 * EVAL expr3c Push result of "expr3c"
5494 * end:
5495 */
5496 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005497compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005498{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005499 int ppconst_used = ppconst->pp_used;
5500
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005501 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005502 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005503 return FAIL;
5504
5505 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005506 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005507}
5508
5509/*
5510 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005511 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005512 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513 * JUMP_IF_FALSE alt jump if false
5514 * EVAL expr1a
5515 * JUMP_ALWAYS end
5516 * alt: EVAL expr1b
5517 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005518 *
5519 * Toplevel expression: expr2 ?? expr1
5520 * Produces instructions:
5521 * EVAL expr2 Push result of "expr2"
5522 * JUMP_AND_KEEP_IF_TRUE end jump if true
5523 * EVAL expr1
5524 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525 */
5526 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005527compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005528{
5529 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005530 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005531 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005532
Bram Moolenaar3988f642020-08-27 22:43:03 +02005533 // Ignore all kinds of errors when not producing code.
5534 if (cctx->ctx_skip == SKIP_YES)
5535 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005536 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005537 return OK;
5538 }
5539
Bram Moolenaar61a89812020-05-07 16:58:17 +02005540 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005541 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542 return FAIL;
5543
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005544 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005545 if (*p == '?')
5546 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005547 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 garray_T *instr = &cctx->ctx_instr;
5549 garray_T *stack = &cctx->ctx_type_stack;
5550 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005551 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005552 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005553 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005554 int has_const_expr = FALSE;
5555 int const_value = FALSE;
5556 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005558 if (next != NULL)
5559 {
5560 *arg = next_line_from_context(cctx, TRUE);
5561 p = skipwhite(*arg);
5562 }
5563
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005564 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005565 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005566 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005567 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005568 return FAIL;
5569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005570
Bram Moolenaara5565e42020-05-09 15:44:01 +02005571 if (ppconst->pp_used == ppconst_used + 1)
5572 {
5573 // the condition is a constant, we know whether the ? or the :
5574 // expression is to be evaluated.
5575 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005576 if (op_falsy)
5577 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5578 else
5579 {
5580 int error = FALSE;
5581
5582 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5583 &error);
5584 if (error)
5585 return FAIL;
5586 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005587 cctx->ctx_skip = save_skip == SKIP_YES ||
5588 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5589
5590 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5591 // "left ?? right" and "left" is truthy: produce "left"
5592 generate_ppconst(cctx, ppconst);
5593 else
5594 {
5595 clear_tv(&ppconst->pp_tv[ppconst_used]);
5596 --ppconst->pp_used;
5597 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005598 }
5599 else
5600 {
5601 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005602 if (op_falsy)
5603 end_idx = instr->ga_len;
5604 generate_JUMP(cctx, op_falsy
5605 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5606 if (op_falsy)
5607 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609
5610 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005611 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005612 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005613 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005614 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005615
Bram Moolenaara5565e42020-05-09 15:44:01 +02005616 if (!has_const_expr)
5617 {
5618 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005619
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005620 if (!op_falsy)
5621 {
5622 // remember the type and drop it
5623 --stack->ga_len;
5624 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005626 end_idx = instr->ga_len;
5627 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005628
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005629 // jump here from JUMP_IF_FALSE
5630 isn = ((isn_T *)instr->ga_data) + alt_idx;
5631 isn->isn_arg.jump.jump_where = instr->ga_len;
5632 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005634
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005635 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005636 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005637 // Check for the ":".
5638 p = may_peek_next_line(cctx, *arg, &next);
5639 if (*p != ':')
5640 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005641 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005642 return FAIL;
5643 }
5644 if (next != NULL)
5645 {
5646 *arg = next_line_from_context(cctx, TRUE);
5647 p = skipwhite(*arg);
5648 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005649
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005650 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5651 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005652 semsg(_(e_white_space_required_before_and_after_str_at_str),
5653 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005654 return FAIL;
5655 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005656
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005657 // evaluate the third expression
5658 if (has_const_expr)
5659 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005660 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005661 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005662 return FAIL;
5663 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5664 return FAIL;
5665 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666
Bram Moolenaara5565e42020-05-09 15:44:01 +02005667 if (!has_const_expr)
5668 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005669 type_T **typep;
5670
Bram Moolenaara5565e42020-05-09 15:44:01 +02005671 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672
Bram Moolenaara5565e42020-05-09 15:44:01 +02005673 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005674 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5675 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005676
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005677 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005678 isn = ((isn_T *)instr->ga_data) + end_idx;
5679 isn->isn_arg.jump.jump_where = instr->ga_len;
5680 }
5681
5682 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005683 }
5684 return OK;
5685}
5686
5687/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005688 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005689 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5690 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005691 */
5692 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005693compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005694{
5695 ppconst_T ppconst;
5696
5697 CLEAR_FIELD(ppconst);
5698 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5699 {
5700 clear_ppconst(&ppconst);
5701 return FAIL;
5702 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005703 if (is_const != NULL)
5704 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005705 if (generate_ppconst(cctx, &ppconst) == FAIL)
5706 return FAIL;
5707 return OK;
5708}
5709
5710/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005711 * Toplevel expression.
5712 */
5713 static int
5714compile_expr0(char_u **arg, cctx_T *cctx)
5715{
5716 return compile_expr0_ext(arg, cctx, NULL);
5717}
5718
5719/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005720 * Compile "return [expr]".
5721 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005722 */
5723 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005724compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005725{
5726 char_u *p = arg;
5727 garray_T *stack = &cctx->ctx_type_stack;
5728 type_T *stack_type;
5729
5730 if (*p != NUL && *p != '|' && *p != '\n')
5731 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005732 if (legacy)
5733 {
5734 int save_flags = cmdmod.cmod_flags;
5735
5736 generate_LEGACY_EVAL(cctx, p);
5737 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5738 0, cctx, FALSE, FALSE) == FAIL)
5739 return NULL;
5740 cmdmod.cmod_flags |= CMOD_LEGACY;
5741 (void)skip_expr(&p, NULL);
5742 cmdmod.cmod_flags = save_flags;
5743 }
5744 else
5745 {
5746 // compile return argument into instructions
5747 if (compile_expr0(&p, cctx) == FAIL)
5748 return NULL;
5749 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005750
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005751 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005752 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005753 // "check_return_type" with uf_ret_type set to &t_unknown is used
5754 // for an inline function without a specified return type. Set the
5755 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005756 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005757 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005758 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5759 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005760 || (!check_return_type
5761 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005762 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005763 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005764 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005765 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005766 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005767 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5768 && stack_type->tt_type != VAR_VOID
5769 && stack_type->tt_type != VAR_UNKNOWN)
5770 {
5771 emsg(_(e_returning_value_in_function_without_return_type));
5772 return NULL;
5773 }
5774 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005775 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005776 return NULL;
5777 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005778 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005779 }
5780 else
5781 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005782 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005783 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005784 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5785 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005786 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005787 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005788 return NULL;
5789 }
5790
5791 // No argument, return zero.
5792 generate_PUSHNR(cctx, 0);
5793 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005794
5795 // Undo any command modifiers.
5796 generate_undo_cmdmods(cctx);
5797
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005798 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005799 return NULL;
5800
5801 // "return val | endif" is possible
5802 return skipwhite(p);
5803}
5804
5805/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005806 * Get a line from the compilation context, compatible with exarg_T getline().
5807 * Return a pointer to the line in allocated memory.
5808 * Return NULL for end-of-file or some error.
5809 */
5810 static char_u *
5811exarg_getline(
5812 int c UNUSED,
5813 void *cookie,
5814 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005815 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005816{
5817 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005818 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005819
Bram Moolenaar66250c92020-08-20 15:02:42 +02005820 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005821 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005822 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005823 return NULL;
5824 ++cctx->ctx_lnum;
5825 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5826 // Comment lines result in NULL pointers, skip them.
5827 if (p != NULL)
5828 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005829 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005830}
5831
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005832 void
5833fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5834{
5835 eap->getline = exarg_getline;
5836 eap->cookie = cctx;
5837}
5838
Bram Moolenaar04b12692020-05-04 23:24:44 +02005839/*
5840 * Compile a nested :def command.
5841 */
5842 static char_u *
5843compile_nested_function(exarg_T *eap, cctx_T *cctx)
5844{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005845 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005846 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005847 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005848 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005849 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005850 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005851 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005852
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005853 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005854 {
5855 emsg(_(e_cannot_use_bang_with_nested_def));
5856 return NULL;
5857 }
5858
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005859 if (*name_start == '/')
5860 {
5861 name_end = skip_regexp(name_start + 1, '/', TRUE);
5862 if (*name_end == '/')
5863 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005864 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005865 }
5866 if (name_end == name_start || *skipwhite(name_end) != '(')
5867 {
5868 if (!ends_excmd2(name_start, name_end))
5869 {
5870 semsg(_(e_invalid_command_str), eap->cmd);
5871 return NULL;
5872 }
5873
5874 // "def" or "def Name": list functions
5875 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5876 return NULL;
5877 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5878 }
5879
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005880 // Only g:Func() can use a namespace.
5881 if (name_start[1] == ':' && !is_global)
5882 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005883 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005884 return NULL;
5885 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005886 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005887 return NULL;
5888
Bram Moolenaar04b12692020-05-04 23:24:44 +02005889 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005890 fill_exarg_from_cctx(eap, cctx);
5891
Bram Moolenaar04b12692020-05-04 23:24:44 +02005892 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00005893 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005894 lambda_name = vim_strsave(get_lambda_name());
5895 if (lambda_name == NULL)
5896 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005897 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005898
Bram Moolenaar822ba242020-05-24 23:00:18 +02005899 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005900 {
5901 r = eap->skip ? OK : FAIL;
5902 goto theend;
5903 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005904
5905 // copy over the block scope IDs before compiling
5906 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5907 {
5908 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5909
5910 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5911 if (ufunc->uf_block_ids != NULL)
5912 {
5913 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5914 sizeof(int) * block_depth);
5915 ufunc->uf_block_depth = block_depth;
5916 }
5917 }
5918
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005919 compile_type = COMPILE_TYPE(ufunc);
5920#ifdef FEAT_PROFILE
5921 // If the outer function is profiled, also compile the nested function for
5922 // profiling.
5923 if (cctx->ctx_compile_type == CT_PROFILE)
5924 compile_type = CT_PROFILE;
5925#endif
5926 if (func_needs_compiling(ufunc, compile_type)
5927 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005928 {
5929 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005930 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005931 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005932
Bram Moolenaar648594e2021-07-11 17:55:01 +02005933#ifdef FEAT_PROFILE
5934 // When the outer function is compiled for profiling, the nested function
5935 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005936 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005937 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5938#endif
5939
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005940 if (is_global)
5941 {
5942 char_u *func_name = vim_strnsave(name_start + 2,
5943 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005944
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005945 if (func_name == NULL)
5946 r = FAIL;
5947 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005948 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005949 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005950 lambda_name = NULL;
5951 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005952 }
5953 else
5954 {
5955 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005956 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005957 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005958
Bram Moolenaareef21022020-08-01 22:16:43 +02005959 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005960 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005961 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005962 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005963 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5964 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005965
5966theend:
5967 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005968 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005969}
5970
5971/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005972 * Return the length of an assignment operator, or zero if there isn't one.
5973 */
5974 int
5975assignment_len(char_u *p, int *heredoc)
5976{
5977 if (*p == '=')
5978 {
5979 if (p[1] == '<' && p[2] == '<')
5980 {
5981 *heredoc = TRUE;
5982 return 3;
5983 }
5984 return 1;
5985 }
5986 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5987 return 2;
5988 if (STRNCMP(p, "..=", 3) == 0)
5989 return 3;
5990 return 0;
5991}
5992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005994 * Generate the load instruction for "name".
5995 */
5996 static void
5997generate_loadvar(
5998 cctx_T *cctx,
5999 assign_dest_T dest,
6000 char_u *name,
6001 lvar_T *lvar,
6002 type_T *type)
6003{
6004 switch (dest)
6005 {
6006 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006007 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006008 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
6009 break;
6010 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01006011 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
6012 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
6013 else
6014 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006015 break;
6016 case dest_buffer:
6017 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
6018 break;
6019 case dest_window:
6020 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
6021 break;
6022 case dest_tab:
6023 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
6024 break;
6025 case dest_script:
6026 compile_load_scriptvar(cctx,
6027 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
6028 break;
6029 case dest_env:
6030 // Include $ in the name here
6031 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
6032 break;
6033 case dest_reg:
6034 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
6035 break;
6036 case dest_vimvar:
6037 generate_LOADV(cctx, name + 2, TRUE);
6038 break;
6039 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01006040 if (lvar->lv_from_outer > 0)
6041 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
6042 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006043 else
6044 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
6045 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006046 case dest_expr:
6047 // list or dict value should already be on the stack.
6048 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006049 }
6050}
6051
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006052/*
6053 * Skip over "[expr]" or ".member".
6054 * Does not check for any errors.
6055 */
6056 static char_u *
6057skip_index(char_u *start)
6058{
6059 char_u *p = start;
6060
6061 if (*p == '[')
6062 {
6063 p = skipwhite(p + 1);
6064 (void)skip_expr(&p, NULL);
6065 p = skipwhite(p);
6066 if (*p == ']')
6067 return p + 1;
6068 return p;
6069 }
6070 // if (*p == '.')
6071 return to_name_end(p + 1, TRUE);
6072}
6073
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006074 void
6075vim9_declare_error(char_u *name)
6076{
6077 char *scope = "";
6078
6079 switch (*name)
6080 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006081 case 'g': scope = _("global"); break;
6082 case 'b': scope = _("buffer"); break;
6083 case 'w': scope = _("window"); break;
6084 case 't': scope = _("tab"); break;
6085 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006086 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006087 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006088 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006089 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006090 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006091 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006092 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006093 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006094 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006095}
6096
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006097/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006098 * For one assignment figure out the type of destination. Return it in "dest".
6099 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006100 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006101 * For a v:var "vimvaridx" is set.
6102 * "type" is set to the destination type if known, unchanted otherwise.
6103 * Return FAIL if an error message was given.
6104 */
6105 static int
6106get_var_dest(
6107 char_u *name,
6108 assign_dest_T *dest,
6109 int cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006110 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006111 int *vimvaridx,
6112 type_T **type,
6113 cctx_T *cctx)
6114{
6115 char_u *p;
6116
6117 if (*name == '&')
6118 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006119 int cc;
6120 long numval;
6121 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006122 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006123
6124 *dest = dest_option;
6125 if (cmdidx == CMD_final || cmdidx == CMD_const)
6126 {
6127 emsg(_(e_const_option));
6128 return FAIL;
6129 }
6130 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006131 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006132 if (p == NULL)
6133 {
6134 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02006135 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006136 return FAIL;
6137 }
6138 cc = *p;
6139 *p = NUL;
6140 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006141 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006142 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006143 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006144 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006145 case gov_unknown:
Bram Moolenaare1242042021-12-16 20:56:57 +00006146 semsg(_(e_unknown_option_str), name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006147 return FAIL;
6148 case gov_string:
6149 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006150 if (opt_p_flags & P_FUNC)
6151 {
6152 // might be a Funcref, check the type later
6153 *type = &t_any;
6154 *dest = dest_func_option;
6155 }
6156 else
6157 {
6158 *type = &t_string;
6159 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006160 break;
6161 case gov_bool:
6162 case gov_hidden_bool:
6163 *type = &t_bool;
6164 break;
6165 case gov_number:
6166 case gov_hidden_number:
6167 *type = &t_number;
6168 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006169 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006170 }
6171 else if (*name == '$')
6172 {
6173 *dest = dest_env;
6174 *type = &t_string;
6175 }
6176 else if (*name == '@')
6177 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006178 if (name[1] != '@'
6179 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006180 {
6181 emsg_invreg(name[1]);
6182 return FAIL;
6183 }
6184 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006185 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006186 }
6187 else if (STRNCMP(name, "g:", 2) == 0)
6188 {
6189 *dest = dest_global;
6190 }
6191 else if (STRNCMP(name, "b:", 2) == 0)
6192 {
6193 *dest = dest_buffer;
6194 }
6195 else if (STRNCMP(name, "w:", 2) == 0)
6196 {
6197 *dest = dest_window;
6198 }
6199 else if (STRNCMP(name, "t:", 2) == 0)
6200 {
6201 *dest = dest_tab;
6202 }
6203 else if (STRNCMP(name, "v:", 2) == 0)
6204 {
6205 typval_T *vtv;
6206 int di_flags;
6207
6208 *vimvaridx = find_vim_var(name + 2, &di_flags);
6209 if (*vimvaridx < 0)
6210 {
6211 semsg(_(e_variable_not_found_str), name);
6212 return FAIL;
6213 }
6214 // We use the current value of "sandbox" here, is that OK?
6215 if (var_check_ro(di_flags, name, FALSE))
6216 return FAIL;
6217 *dest = dest_vimvar;
6218 vtv = get_vim_var_tv(*vimvaridx);
6219 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6220 }
6221 return OK;
6222}
6223
6224/*
6225 * Generate a STORE instruction for "dest", not being "dest_local".
6226 * Return FAIL when out of memory.
6227 */
6228 static int
6229generate_store_var(
6230 cctx_T *cctx,
6231 assign_dest_T dest,
6232 int opt_flags,
6233 int vimvaridx,
6234 int scriptvar_idx,
6235 int scriptvar_sid,
6236 type_T *type,
6237 char_u *name)
6238{
6239 switch (dest)
6240 {
6241 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006242 return generate_STOREOPT(cctx, ISN_STOREOPT,
6243 skip_option_env_lead(name), opt_flags);
6244 case dest_func_option:
6245 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
6246 skip_option_env_lead(name), opt_flags);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006247 case dest_global:
6248 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006249 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6250 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006251 case dest_buffer:
6252 // include b: with the name, easier to execute that way
6253 return generate_STORE(cctx, ISN_STOREB, 0, name);
6254 case dest_window:
6255 // include w: with the name, easier to execute that way
6256 return generate_STORE(cctx, ISN_STOREW, 0, name);
6257 case dest_tab:
6258 // include t: with the name, easier to execute that way
6259 return generate_STORE(cctx, ISN_STORET, 0, name);
6260 case dest_env:
6261 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6262 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006263 return generate_STORE(cctx, ISN_STOREREG,
6264 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006265 case dest_vimvar:
6266 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6267 case dest_script:
6268 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006269 // "s:" may be included in the name.
6270 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006271 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006272 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6273 scriptvar_sid, scriptvar_idx, type);
6274 case dest_local:
6275 case dest_expr:
6276 // cannot happen
6277 break;
6278 }
6279 return FAIL;
6280}
6281
Bram Moolenaar752fc692021-01-04 21:57:11 +01006282 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006283generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6284{
6285 if (lhs->lhs_dest != dest_local)
6286 return generate_store_var(cctx, lhs->lhs_dest,
6287 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6288 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6289 lhs->lhs_type, lhs->lhs_name);
6290
6291 if (lhs->lhs_lvar != NULL)
6292 {
6293 garray_T *instr = &cctx->ctx_instr;
6294 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6295
6296 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6297 // ISN_STORENR
6298 if (lhs->lhs_lvar->lv_from_outer == 0
6299 && instr->ga_len == instr_count + 1
6300 && isn->isn_type == ISN_PUSHNR)
6301 {
6302 varnumber_T val = isn->isn_arg.number;
6303 garray_T *stack = &cctx->ctx_type_stack;
6304
6305 isn->isn_type = ISN_STORENR;
6306 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6307 isn->isn_arg.storenr.stnr_val = val;
6308 if (stack->ga_len > 0)
6309 --stack->ga_len;
6310 }
6311 else if (lhs->lhs_lvar->lv_from_outer > 0)
6312 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6313 lhs->lhs_lvar->lv_from_outer);
6314 else
6315 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6316 }
6317 return OK;
6318}
6319
6320 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006321is_decl_command(int cmdidx)
6322{
6323 return cmdidx == CMD_let || cmdidx == CMD_var
6324 || cmdidx == CMD_final || cmdidx == CMD_const;
6325}
6326
6327/*
6328 * Figure out the LHS type and other properties for an assignment or one item
6329 * of ":unlet" with an index.
6330 * Returns OK or FAIL.
6331 */
6332 static int
6333compile_lhs(
6334 char_u *var_start,
6335 lhs_T *lhs,
6336 int cmdidx,
6337 int heredoc,
6338 int oplen,
6339 cctx_T *cctx)
6340{
6341 char_u *var_end;
6342 int is_decl = is_decl_command(cmdidx);
6343
6344 CLEAR_POINTER(lhs);
6345 lhs->lhs_dest = dest_local;
6346 lhs->lhs_vimvaridx = -1;
6347 lhs->lhs_scriptvar_idx = -1;
6348
6349 // "dest_end" is the end of the destination, including "[expr]" or
6350 // ".name".
6351 // "var_end" is the end of the variable/option/etc. name.
6352 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6353 if (*var_start == '@')
6354 var_end = var_start + 2;
6355 else
6356 {
6357 // skip over the leading "&", "&l:", "&g:" and "$"
6358 var_end = skip_option_env_lead(var_start);
6359 var_end = to_name_end(var_end, TRUE);
6360 }
6361
6362 // "a: type" is declaring variable "a" with a type, not dict "a:".
6363 if (is_decl && lhs->lhs_dest_end == var_start + 2
6364 && lhs->lhs_dest_end[-1] == ':')
6365 --lhs->lhs_dest_end;
6366 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6367 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006368 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006369
6370 // compute the length of the destination without "[expr]" or ".name"
6371 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006372 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006373 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6374 if (lhs->lhs_name == NULL)
6375 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006376
6377 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6378 // Something follows after the variable: "var[idx]" or "var.key".
6379 lhs->lhs_has_index = TRUE;
6380
Bram Moolenaar752fc692021-01-04 21:57:11 +01006381 if (heredoc)
6382 lhs->lhs_type = &t_list_string;
6383 else
6384 lhs->lhs_type = &t_any;
6385
6386 if (cctx->ctx_skip != SKIP_YES)
6387 {
6388 int declare_error = FALSE;
6389
6390 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6391 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6392 &lhs->lhs_type, cctx) == FAIL)
6393 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006394 if (lhs->lhs_dest != dest_local
6395 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006396 {
6397 // Specific kind of variable recognized.
6398 declare_error = is_decl;
6399 }
6400 else
6401 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006402 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006403 if (check_reserved_name(lhs->lhs_name) == FAIL)
6404 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006405
6406 if (lookup_local(var_start, lhs->lhs_varlen,
6407 &lhs->lhs_local_lvar, cctx) == OK)
6408 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6409 else
6410 {
6411 CLEAR_FIELD(lhs->lhs_arg_lvar);
6412 if (arg_exists(var_start, lhs->lhs_varlen,
6413 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6414 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6415 {
6416 if (is_decl)
6417 {
6418 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6419 return FAIL;
6420 }
6421 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6422 }
6423 }
6424 if (lhs->lhs_lvar != NULL)
6425 {
6426 if (is_decl)
6427 {
6428 semsg(_(e_variable_already_declared), lhs->lhs_name);
6429 return FAIL;
6430 }
6431 }
6432 else
6433 {
6434 int script_namespace = lhs->lhs_varlen > 1
6435 && STRNCMP(var_start, "s:", 2) == 0;
6436 int script_var = (script_namespace
6437 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006438 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006439 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006440 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006441 imported_T *import =
6442 find_imported(var_start, lhs->lhs_varlen, cctx);
6443
6444 if (script_namespace || script_var || import != NULL)
6445 {
6446 char_u *rawname = lhs->lhs_name
6447 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6448
6449 if (is_decl)
6450 {
6451 if (script_namespace)
6452 semsg(_(e_cannot_declare_script_variable_in_function),
6453 lhs->lhs_name);
6454 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006455 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006456 lhs->lhs_name);
6457 return FAIL;
6458 }
6459 else if (cctx->ctx_ufunc->uf_script_ctx_version
6460 == SCRIPT_VERSION_VIM9
6461 && script_namespace
6462 && !script_var && import == NULL)
6463 {
6464 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6465 return FAIL;
6466 }
6467
6468 lhs->lhs_dest = dest_script;
6469
6470 // existing script-local variables should have a type
6471 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6472 if (import != NULL)
6473 lhs->lhs_scriptvar_sid = import->imp_sid;
6474 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6475 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006476 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006477 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006478 lhs->lhs_scriptvar_sid, rawname,
6479 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6480 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006481 if (lhs->lhs_scriptvar_idx >= 0)
6482 {
6483 scriptitem_T *si = SCRIPT_ITEM(
6484 lhs->lhs_scriptvar_sid);
6485 svar_T *sv =
6486 ((svar_T *)si->sn_var_vals.ga_data)
6487 + lhs->lhs_scriptvar_idx;
6488 lhs->lhs_type = sv->sv_type;
6489 }
6490 }
6491 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006492 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006493 == FAIL)
6494 return FAIL;
6495 }
6496 }
6497
6498 if (declare_error)
6499 {
6500 vim9_declare_error(lhs->lhs_name);
6501 return FAIL;
6502 }
6503 }
6504
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006505 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01006506 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6507 var_end = lhs->lhs_dest_end;
6508
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006509 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006510 {
6511 if (is_decl && *var_end == ':')
6512 {
6513 char_u *p;
6514
6515 // parse optional type: "let var: type = expr"
6516 if (!VIM_ISWHITE(var_end[1]))
6517 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006518 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006519 return FAIL;
6520 }
6521 p = skipwhite(var_end + 1);
6522 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6523 if (lhs->lhs_type == NULL)
6524 return FAIL;
6525 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006526 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006527 }
6528 else if (lhs->lhs_lvar != NULL)
6529 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6530 }
6531
Bram Moolenaare42939a2021-04-05 17:11:17 +02006532 if (oplen == 3 && !heredoc
6533 && lhs->lhs_dest != dest_global
6534 && !lhs->lhs_has_index
6535 && lhs->lhs_type->tt_type != VAR_STRING
6536 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006537 {
6538 emsg(_(e_can_only_concatenate_to_string));
6539 return FAIL;
6540 }
6541
6542 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6543 && cctx->ctx_skip != SKIP_YES)
6544 {
6545 if (oplen > 1 && !heredoc)
6546 {
6547 // +=, /=, etc. require an existing variable
6548 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6549 return FAIL;
6550 }
6551 if (!is_decl)
6552 {
6553 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6554 return FAIL;
6555 }
6556
Bram Moolenaar3f327882021-03-17 20:56:38 +01006557 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006558 if ((lhs->lhs_type->tt_type == VAR_FUNC
6559 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006560 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006561 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006562
6563 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006564 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6565 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6566 if (lhs->lhs_lvar == NULL)
6567 return FAIL;
6568 lhs->lhs_new_local = TRUE;
6569 }
6570
6571 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006572 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006573 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006574 char_u *after = var_start + lhs->lhs_varlen;
6575 char_u *p;
6576
Bram Moolenaar752fc692021-01-04 21:57:11 +01006577 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006578 if (is_decl)
6579 {
6580 emsg(_(e_cannot_use_index_when_declaring_variable));
6581 return FAIL;
6582 }
6583
Bram Moolenaarc750d912021-11-29 22:02:12 +00006584 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
6585 // Only the last index is used below, if there are others
6586 // before it generate code for the expression. Thus for
6587 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6588 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006589 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006590 p = skip_index(after);
6591 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01006592 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006593 lhs->lhs_varlen_total = p - var_start;
6594 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006595 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006596 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006597 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006598 if (after > var_start + lhs->lhs_varlen)
6599 {
6600 lhs->lhs_varlen = after - var_start;
6601 lhs->lhs_dest = dest_expr;
6602 // We don't know the type before evaluating the expression,
6603 // use "any" until then.
6604 lhs->lhs_type = &t_any;
6605 }
6606
6607 if (lhs->lhs_type->tt_member == NULL)
6608 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006609 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00006610 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006611 }
6612 return OK;
6613}
6614
6615/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006616 * Figure out the LHS and check a few errors.
6617 */
6618 static int
6619compile_assign_lhs(
6620 char_u *var_start,
6621 lhs_T *lhs,
6622 int cmdidx,
6623 int is_decl,
6624 int heredoc,
6625 int oplen,
6626 cctx_T *cctx)
6627{
6628 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6629 return FAIL;
6630
6631 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6632 {
6633 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6634 return FAIL;
6635 }
6636 if (!is_decl && lhs->lhs_lvar != NULL
6637 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6638 {
6639 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6640 return FAIL;
6641 }
6642 return OK;
6643}
6644
6645/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006646 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6647 */
6648 static int
6649has_list_index(char_u *idx_start, cctx_T *cctx)
6650{
6651 char_u *p = idx_start;
6652 int save_skip;
6653
6654 if (*p != '[')
6655 return FALSE;
6656
6657 p = skipwhite(p + 1);
6658 if (*p == ':')
6659 return TRUE;
6660
6661 save_skip = cctx->ctx_skip;
6662 cctx->ctx_skip = SKIP_YES;
6663 (void)compile_expr0(&p, cctx);
6664 cctx->ctx_skip = save_skip;
6665 return *skipwhite(p) == ':';
6666}
6667
6668/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006669 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6670 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006671 */
6672 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006673compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006674 char_u *var_start,
6675 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006676 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006677 cctx_T *cctx)
6678{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006679 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006680 char_u *p;
6681 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006682 int need_white_before = TRUE;
6683 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006684
Bram Moolenaar752fc692021-01-04 21:57:11 +01006685 p = var_start + varlen;
6686 if (*p == '[')
6687 {
6688 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006689 if (*p == ':')
6690 {
6691 // empty first index, push zero
6692 r = generate_PUSHNR(cctx, 0);
6693 need_white_before = FALSE;
6694 }
6695 else
6696 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006697
6698 if (r == OK && *skipwhite(p) == ':')
6699 {
6700 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006701 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006702 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006703 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006704 empty_second = *skipwhite(p + 1) == ']';
6705 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6706 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006707 {
6708 semsg(_(e_white_space_required_before_and_after_str_at_str),
6709 ":", p);
6710 return FAIL;
6711 }
6712 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006713 if (*p == ']')
6714 // empty second index, push "none"
6715 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6716 else
6717 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006718 }
6719
Bram Moolenaar752fc692021-01-04 21:57:11 +01006720 if (r == OK && *skipwhite(p) != ']')
6721 {
6722 // this should not happen
Bram Moolenaare1242042021-12-16 20:56:57 +00006723 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar752fc692021-01-04 21:57:11 +01006724 r = FAIL;
6725 }
6726 }
6727 else // if (*p == '.')
6728 {
6729 char_u *key_end = to_name_end(p + 1, TRUE);
6730 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6731
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006732 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006733 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006734 return r;
6735}
6736
6737/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006738 * For a LHS with an index, load the variable to be indexed.
6739 */
6740 static int
6741compile_load_lhs(
6742 lhs_T *lhs,
6743 char_u *var_start,
6744 type_T *rhs_type,
6745 cctx_T *cctx)
6746{
6747 if (lhs->lhs_dest == dest_expr)
6748 {
6749 size_t varlen = lhs->lhs_varlen;
6750 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006751 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006752 char_u *p = var_start;
6753 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006754 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006755
Bram Moolenaare97976b2021-08-01 13:17:17 +02006756 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6757 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006758 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006759 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6760 res = compile_expr0(&p, cctx);
6761 var_start[varlen] = c;
6762 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6763 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006764 {
6765 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006766 if (res != FAIL)
Bram Moolenaare1242042021-12-16 20:56:57 +00006767 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006768 return FAIL;
6769 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006770
6771 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6772 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6773 // now we can properly check the type
6774 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6775 && rhs_type != &t_void
6776 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6777 FALSE, FALSE) == FAIL)
6778 return FAIL;
6779 }
6780 else
6781 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6782 lhs->lhs_lvar, lhs->lhs_type);
6783 return OK;
6784}
6785
6786/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006787 * Produce code for loading "lhs" and also take care of an index.
6788 * Return OK/FAIL.
6789 */
6790 static int
6791compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6792{
6793 compile_load_lhs(lhs, var_start, NULL, cctx);
6794
6795 if (lhs->lhs_has_index)
6796 {
6797 int range = FALSE;
6798
6799 // Get member from list or dict. First compile the
6800 // index value.
6801 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6802 return FAIL;
6803 if (range)
6804 {
6805 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6806 var_start);
6807 return FAIL;
6808 }
6809
6810 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006811 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006812 return FAIL;
6813 }
6814 return OK;
6815}
6816
6817/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006818 * Assignment to a list or dict member, or ":unlet" for the item, using the
6819 * information in "lhs".
6820 * Returns OK or FAIL.
6821 */
6822 static int
6823compile_assign_unlet(
6824 char_u *var_start,
6825 lhs_T *lhs,
6826 int is_assign,
6827 type_T *rhs_type,
6828 cctx_T *cctx)
6829{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006830 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006831 garray_T *stack = &cctx->ctx_type_stack;
6832 int range = FALSE;
6833
Bram Moolenaar68452172021-04-12 21:21:02 +02006834 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006835 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006836 if (is_assign && range
6837 && lhs->lhs_type->tt_type != VAR_LIST
6838 && lhs->lhs_type != &t_blob
6839 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006840 {
6841 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6842 return FAIL;
6843 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006844
6845 if (lhs->lhs_type == &t_any)
6846 {
6847 // Index on variable of unknown type: check at runtime.
6848 dest_type = VAR_ANY;
6849 }
6850 else
6851 {
6852 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006853 if (dest_type == VAR_DICT && range)
6854 {
6855 emsg(e_cannot_use_range_with_dictionary);
6856 return FAIL;
6857 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006858 if (dest_type == VAR_DICT
6859 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006860 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006861 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006862 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006863 type_T *type;
6864
6865 if (range)
6866 {
6867 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6868 if (need_type(type, &t_number,
6869 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006870 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006871 }
6872 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006873 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006874 && need_type(type, &t_number,
6875 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006876 return FAIL;
6877 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006878 }
6879
6880 // Load the dict or list. On the stack we then have:
6881 // - value (for assignment, not for :unlet)
6882 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006883 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006884 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006885 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6886 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006887
Bram Moolenaar68452172021-04-12 21:21:02 +02006888 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6889 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006890 {
6891 if (is_assign)
6892 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006893 if (range)
6894 {
6895 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6896 return FAIL;
6897 }
6898 else
6899 {
6900 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006901
Bram Moolenaar68452172021-04-12 21:21:02 +02006902 if (isn == NULL)
6903 return FAIL;
6904 isn->isn_arg.vartype = dest_type;
6905 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006906 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006907 else if (range)
6908 {
6909 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6910 return FAIL;
6911 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006912 else
6913 {
6914 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6915 return FAIL;
6916 }
6917 }
6918 else
6919 {
6920 emsg(_(e_indexable_type_required));
6921 return FAIL;
6922 }
6923
6924 return OK;
6925}
6926
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006927/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006928 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006929 * "let name"
6930 * "var name = expr"
6931 * "final name = expr"
6932 * "const name = expr"
6933 * "name = expr"
6934 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006935 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006936 * Return NULL for an error.
6937 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 */
6939 static char_u *
6940compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6941{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006942 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006944 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 char_u *ret = NULL;
6946 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006947 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006949 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006951 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 int oplen = 0;
6954 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006955 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006956 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006958 int is_decl = is_decl_command(cmdidx);
6959 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006960 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006961
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006962 // Skip over the "var" or "[var, var]" to get to any "=".
6963 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6964 if (p == NULL)
6965 return *arg == '[' ? arg : NULL;
6966
Bram Moolenaar752fc692021-01-04 21:57:11 +01006967 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969 sp = p;
6970 p = skipwhite(p);
6971 op = p;
6972 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006973
6974 if (var_count > 0 && oplen == 0)
6975 // can be something like "[1, 2]->func()"
6976 return arg;
6977
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006978 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006980 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006981 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006982 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006983 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6984 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006985 if (VIM_ISWHITE(eap->cmd[2]))
6986 {
6987 semsg(_(e_no_white_space_allowed_after_str_str),
6988 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6989 return NULL;
6990 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006991 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6992 oplen = 2;
6993 incdec = TRUE;
6994 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996 if (heredoc)
6997 {
6998 list_T *l;
6999 listitem_T *li;
7000
7001 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02007002 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02007004 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02007005 if (l == NULL)
7006 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007
Bram Moolenaar078269b2020-09-21 20:35:55 +02007008 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02007010 // Push each line and the create the list.
7011 FOR_ALL_LIST_ITEMS(l, li)
7012 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02007013 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02007014 li->li_tv.vval.v_string = NULL;
7015 }
7016 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 list_free(l);
7019 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007020 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007022 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007024 char_u *wp;
7025
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007026 // for "[var, var] = expr" evaluate the expression here, loop over the
7027 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007028 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02007029
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007030 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01007031 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007032 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007033 if (compile_expr0(&p, cctx) == FAIL)
7034 return NULL;
7035 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007036
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007037 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007039 type_T *stacktype;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007040 int needed_list_len;
7041 int did_check = FALSE;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007042
Bram Moolenaarec5929d2020-04-07 20:53:39 +02007043 stacktype = stack->ga_len == 0 ? &t_void
7044 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007045 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007047 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007048 goto theend;
7049 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01007050 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007051 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007052 goto theend;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007053 // If a constant list was used we can check the length right here.
7054 needed_list_len = semicolon ? var_count - 1 : var_count;
7055 if (instr->ga_len > 0)
7056 {
7057 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
7058
7059 if (isn->isn_type == ISN_NEWLIST)
7060 {
7061 did_check = TRUE;
7062 if (semicolon ? isn->isn_arg.number < needed_list_len
7063 : isn->isn_arg.number != needed_list_len)
7064 {
7065 semsg(_(e_expected_nr_items_but_got_nr),
7066 needed_list_len, isn->isn_arg.number);
7067 goto theend;
7068 }
7069 }
7070 }
7071 if (!did_check)
7072 generate_CHECKLEN(cctx, needed_list_len, semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007073 if (stacktype->tt_member != NULL)
7074 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007075 }
7076 }
7077
7078 /*
7079 * Loop over variables in "[var, var] = expr".
7080 * For "var = expr" and "let var: type" this is done only once.
7081 */
7082 if (var_count > 0)
7083 var_start = skipwhite(arg + 1); // skip over the "["
7084 else
7085 var_start = arg;
7086 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
7087 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007088 int instr_count = -1;
7089 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007090
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02007091 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
7092 {
7093 // Ignore underscore in "[a, _, b] = list".
7094 if (var_count > 0)
7095 {
7096 var_start = skipwhite(var_start + 2);
7097 continue;
7098 }
7099 emsg(_(e_cannot_use_underscore_here));
7100 goto theend;
7101 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007102 vim_free(lhs.lhs_name);
7103
7104 /*
7105 * Figure out the LHS type and other properties.
7106 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007107 if (compile_assign_lhs(var_start, &lhs, cmdidx,
7108 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007109 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02007110 if (heredoc)
7111 {
7112 SOURCING_LNUM = start_lnum;
7113 if (lhs.lhs_has_type
7114 && need_type(&t_list_string, lhs.lhs_type,
7115 -1, 0, cctx, FALSE, FALSE) == FAIL)
7116 goto theend;
7117 }
7118 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007119 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007120 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007121 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007122 if (oplen > 0 && var_count == 0)
7123 {
7124 // skip over the "=" and the expression
7125 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02007126 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007127 }
7128 }
7129 else if (oplen > 0)
7130 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007131 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01007132 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007133
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007134 // for "+=", "*=", "..=" etc. first load the current value
7135 if (*op != '='
7136 && compile_load_lhs_with_index(&lhs, var_start,
7137 cctx) == FAIL)
7138 goto theend;
7139
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007140 // For "var = expr" evaluate the expression.
7141 if (var_count == 0)
7142 {
7143 int r;
7144
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007145 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007146 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007147 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007148 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007149 r = generate_PUSHNR(cctx, 1);
7150 }
7151 else
7152 {
7153 // Temporarily hide the new local variable here, it is
7154 // not available to this expression.
7155 if (lhs.lhs_new_local)
7156 --cctx->ctx_locals.ga_len;
7157 wp = op + oplen;
7158 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7159 {
7160 if (lhs.lhs_new_local)
7161 ++cctx->ctx_locals.ga_len;
7162 goto theend;
7163 }
7164 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007165 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007166 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007167 if (r == FAIL)
7168 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007169 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007170 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007171 else if (semicolon && var_idx == var_count - 1)
7172 {
7173 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007174 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007175 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7176 goto theend;
7177 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007178 else
7179 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007180 // For "[var, var] = expr" get the "var_idx" item from the
7181 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007182 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007183 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007184 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007185
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007186 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007187 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007188 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007189 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007190 if ((rhs_type->tt_type == VAR_FUNC
7191 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007192 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007193 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007194 goto theend;
7195
Bram Moolenaar752fc692021-01-04 21:57:11 +01007196 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007197 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007198 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007199 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007200 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007201 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007202 }
7203 else
7204 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007205 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007206 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007207 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007208 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007209 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007210 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007211 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007212 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007213 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007214 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007215 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007216 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007217 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007218 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007219 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007220 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007221
Bram Moolenaar77709b12021-04-03 21:01:01 +02007222 // Without operator check type here, otherwise below.
7223 // Use the line number of the assignment.
7224 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007225 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7226 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007227 // If assigning to a list or dict member, use the
7228 // member type. Not for "list[:] =".
7229 if (lhs.lhs_has_index
7230 && !has_list_index(var_start + lhs.lhs_varlen,
7231 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007232 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007233 if (need_type_where(rhs_type, use_type, -1, where,
7234 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007235 goto theend;
7236 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007237 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007238 else
7239 {
7240 type_T *lhs_type = lhs.lhs_member_type;
7241
7242 // Special case: assigning to @# can use a number or a
7243 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007244 // Also: can assign a number to a float.
7245 if ((lhs_type == &t_number_or_string
7246 || lhs_type == &t_float)
7247 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007248 lhs_type = &t_number;
7249 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007250 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007251 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007254 else if (cmdidx == CMD_final)
7255 {
7256 emsg(_(e_final_requires_a_value));
7257 goto theend;
7258 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007259 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007261 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007262 goto theend;
7263 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00007264 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option
7265 || lhs.lhs_dest == dest_func_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007266 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007267 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007268 goto theend;
7269 }
7270 else
7271 {
7272 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007273 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007274 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007275 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007276 {
7277 case VAR_BOOL:
7278 generate_PUSHBOOL(cctx, VVAL_FALSE);
7279 break;
7280 case VAR_FLOAT:
7281#ifdef FEAT_FLOAT
7282 generate_PUSHF(cctx, 0.0);
7283#endif
7284 break;
7285 case VAR_STRING:
7286 generate_PUSHS(cctx, NULL);
7287 break;
7288 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007289 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007290 break;
7291 case VAR_FUNC:
7292 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7293 break;
7294 case VAR_LIST:
7295 generate_NEWLIST(cctx, 0);
7296 break;
7297 case VAR_DICT:
7298 generate_NEWDICT(cctx, 0);
7299 break;
7300 case VAR_JOB:
7301 generate_PUSHJOB(cctx, NULL);
7302 break;
7303 case VAR_CHANNEL:
7304 generate_PUSHCHANNEL(cctx, NULL);
7305 break;
7306 case VAR_NUMBER:
7307 case VAR_UNKNOWN:
7308 case VAR_ANY:
7309 case VAR_PARTIAL:
7310 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007311 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007312 case VAR_SPECIAL: // cannot happen
7313 generate_PUSHNR(cctx, 0);
7314 break;
7315 }
7316 }
7317 if (var_count == 0)
7318 end = p;
7319 }
7320
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007321 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007322 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007323 break;
7324
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007325 if (oplen > 0 && *op != '=')
7326 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007327 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007328 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007329
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007330 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007331 {
7332 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7333 goto theend;
7334 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007335 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007336 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007337 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007338 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7339 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007340#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007341 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007342 !(expected == &t_float && (stacktype == &t_number
7343 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007344#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007345 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007346 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007347 goto theend;
7348 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007349
7350 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007351 {
7352 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7353 goto theend;
7354 }
7355 else if (*op == '+')
7356 {
7357 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007358 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007359 lhs.lhs_member_type, stacktype,
7360 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007361 goto theend;
7362 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007363 else if (generate_two_op(cctx, op) == FAIL)
7364 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007367 // Use the line number of the assignment for store instruction.
7368 save_lnum = cctx->ctx_lnum;
7369 cctx->ctx_lnum = start_lnum - 1;
7370
Bram Moolenaar752fc692021-01-04 21:57:11 +01007371 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007372 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007373 // Use the info in "lhs" to store the value at the index in the
7374 // list or dict.
7375 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7376 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007377 {
7378 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007379 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007380 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007381 }
7382 else
7383 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007384 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007385 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007386 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007387 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007388 generate_LOCKCONST(cctx);
7389
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007390 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007391 && (lhs.lhs_type->tt_type == VAR_DICT
7392 || lhs.lhs_type->tt_type == VAR_LIST)
7393 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007394 && !(lhs.lhs_type->tt_member == &t_any
7395 && oplen > 0
7396 && rhs_type != NULL
7397 && rhs_type->tt_type == lhs.lhs_type->tt_type
7398 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007399 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007400 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007401 // also in legacy script. Not for "list<any> = val", then the
7402 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007403 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007404
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007405 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007406 {
7407 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007408 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007409 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007410 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007411 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007412
7413 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00007414 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007415 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007416
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007417 // For "[var, var] = expr" drop the "expr" value.
7418 // Also for "[var, var; _] = expr".
7419 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007420 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007421 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007422 goto theend;
7423 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007424
Bram Moolenaarb2097502020-07-19 17:17:02 +02007425 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426
7427theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007428 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429 return ret;
7430}
7431
7432/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007433 * Check for an assignment at "eap->cmd", compile it if found.
7434 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7435 */
7436 static int
7437may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7438{
7439 char_u *pskip;
7440 char_u *p;
7441
7442 // Assuming the command starts with a variable or function name,
7443 // find what follows.
7444 // Skip over "var.member", "var[idx]" and the like.
7445 // Also "&opt = val", "$ENV = val" and "@r = val".
7446 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7447 ? eap->cmd + 1 : eap->cmd;
7448 p = to_name_end(pskip, TRUE);
7449 if (p > eap->cmd && *p != NUL)
7450 {
7451 char_u *var_end;
7452 int oplen;
7453 int heredoc;
7454
7455 if (eap->cmd[0] == '@')
7456 var_end = eap->cmd + 2;
7457 else
7458 var_end = find_name_end(pskip, NULL, NULL,
7459 FNE_CHECK_START | FNE_INCL_BR);
7460 oplen = assignment_len(skipwhite(var_end), &heredoc);
7461 if (oplen > 0)
7462 {
7463 size_t len = p - eap->cmd;
7464
7465 // Recognize an assignment if we recognize the variable
7466 // name:
7467 // "g:var = expr"
7468 // "local = expr" where "local" is a local var.
7469 // "script = expr" where "script" is a script-local var.
7470 // "import = expr" where "import" is an imported var
7471 // "&opt = expr"
7472 // "$ENV = expr"
7473 // "@r = expr"
7474 if (*eap->cmd == '&'
7475 || *eap->cmd == '$'
7476 || *eap->cmd == '@'
7477 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007478 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007479 {
7480 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7481 if (*line == NULL || *line == eap->cmd)
7482 return FAIL;
7483 return OK;
7484 }
7485 }
7486 }
7487
7488 if (*eap->cmd == '[')
7489 {
7490 // [var, var] = expr
7491 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7492 if (*line == NULL)
7493 return FAIL;
7494 if (*line != eap->cmd)
7495 return OK;
7496 }
7497 return NOTDONE;
7498}
7499
7500/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007501 * Check if "name" can be "unlet".
7502 */
7503 int
7504check_vim9_unlet(char_u *name)
7505{
7506 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7507 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007508 // "unlet s:var" is allowed in legacy script.
7509 if (*name == 's' && !script_is_vim9())
7510 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007511 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007512 return FAIL;
7513 }
7514 return OK;
7515}
7516
7517/*
7518 * Callback passed to ex_unletlock().
7519 */
7520 static int
7521compile_unlet(
7522 lval_T *lvp,
7523 char_u *name_end,
7524 exarg_T *eap,
7525 int deep UNUSED,
7526 void *coookie)
7527{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007528 cctx_T *cctx = coookie;
7529 char_u *p = lvp->ll_name;
7530 int cc = *name_end;
7531 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007532
Bram Moolenaar752fc692021-01-04 21:57:11 +01007533 if (cctx->ctx_skip == SKIP_YES)
7534 return OK;
7535
7536 *name_end = NUL;
7537 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007538 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007539 // :unlet $ENV_VAR
7540 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7541 }
7542 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7543 {
7544 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007545
Bram Moolenaar752fc692021-01-04 21:57:11 +01007546 // This is similar to assigning: lookup the list/dict, compile the
7547 // idx/key. Then instead of storing the value unlet the item.
7548 // unlet {list}[idx]
7549 // unlet {dict}[key] dict.key
7550 //
7551 // Figure out the LHS type and other properties.
7552 //
7553 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7554
7555 // : unlet an indexed item
7556 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007557 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007558 iemsg("called compile_lhs() without an index");
7559 ret = FAIL;
7560 }
7561 else
7562 {
7563 // Use the info in "lhs" to unlet the item at the index in the
7564 // list or dict.
7565 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007566 }
7567
Bram Moolenaar752fc692021-01-04 21:57:11 +01007568 vim_free(lhs.lhs_name);
7569 }
7570 else if (check_vim9_unlet(p) == FAIL)
7571 {
7572 ret = FAIL;
7573 }
7574 else
7575 {
7576 // Normal name. Only supports g:, w:, t: and b: namespaces.
7577 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007578 }
7579
Bram Moolenaar752fc692021-01-04 21:57:11 +01007580 *name_end = cc;
7581 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007582}
7583
7584/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007585 * Callback passed to ex_unletlock().
7586 */
7587 static int
7588compile_lock_unlock(
7589 lval_T *lvp,
7590 char_u *name_end,
7591 exarg_T *eap,
7592 int deep UNUSED,
7593 void *coookie)
7594{
7595 cctx_T *cctx = coookie;
7596 int cc = *name_end;
7597 char_u *p = lvp->ll_name;
7598 int ret = OK;
7599 size_t len;
7600 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007601 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007602
7603 if (cctx->ctx_skip == SKIP_YES)
7604 return OK;
7605
7606 // Cannot use :lockvar and :unlockvar on local variables.
7607 if (p[1] != ':')
7608 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007609 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007610
7611 if (lookup_local(p, end - p, NULL, cctx) == OK)
7612 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007613 char_u *s = p;
7614
7615 if (*end != '.' && *end != '[')
7616 {
7617 emsg(_(e_cannot_lock_unlock_local_variable));
7618 return FAIL;
7619 }
7620
7621 // For "d.member" put the local variable on the stack, it will be
7622 // passed to ex_lockvar() indirectly.
7623 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7624 return FAIL;
7625 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007626 }
7627 }
7628
7629 // Checking is done at runtime.
7630 *name_end = NUL;
7631 len = name_end - p + 20;
7632 buf = alloc(len);
7633 if (buf == NULL)
7634 ret = FAIL;
7635 else
7636 {
7637 vim_snprintf((char *)buf, len, "%s %s",
7638 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7639 p);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00007640 ret = generate_EXEC_copy(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007641
7642 vim_free(buf);
7643 *name_end = cc;
7644 }
7645 return ret;
7646}
7647
7648/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007649 * compile "unlet var", "lock var" and "unlock var"
7650 * "arg" points to "var".
7651 */
7652 static char_u *
7653compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7654{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007655 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7656 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7657 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007658 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7659}
7660
7661/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007662 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7663 */
7664 static int
7665compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7666{
7667 garray_T *instr = &cctx->ctx_instr;
7668 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7669
7670 if (endlabel == NULL)
7671 return FAIL;
7672 endlabel->el_next = *el;
7673 *el = endlabel;
7674 endlabel->el_end_label = instr->ga_len;
7675
7676 generate_JUMP(cctx, when, 0);
7677 return OK;
7678}
7679
7680 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007681compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007682{
7683 garray_T *instr = &cctx->ctx_instr;
7684
7685 while (*el != NULL)
7686 {
7687 endlabel_T *cur = (*el);
7688 isn_T *isn;
7689
7690 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007691 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007692 *el = cur->el_next;
7693 vim_free(cur);
7694 }
7695}
7696
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007697 static void
7698compile_free_jump_to_end(endlabel_T **el)
7699{
7700 while (*el != NULL)
7701 {
7702 endlabel_T *cur = (*el);
7703
7704 *el = cur->el_next;
7705 vim_free(cur);
7706 }
7707}
7708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007709/*
7710 * Create a new scope and set up the generic items.
7711 */
7712 static scope_T *
7713new_scope(cctx_T *cctx, scopetype_T type)
7714{
7715 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7716
7717 if (scope == NULL)
7718 return NULL;
7719 scope->se_outer = cctx->ctx_scope;
7720 cctx->ctx_scope = scope;
7721 scope->se_type = type;
7722 scope->se_local_count = cctx->ctx_locals.ga_len;
7723 return scope;
7724}
7725
7726/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007727 * Free the current scope and go back to the outer scope.
7728 */
7729 static void
7730drop_scope(cctx_T *cctx)
7731{
7732 scope_T *scope = cctx->ctx_scope;
7733
7734 if (scope == NULL)
7735 {
7736 iemsg("calling drop_scope() without a scope");
7737 return;
7738 }
7739 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007740 switch (scope->se_type)
7741 {
7742 case IF_SCOPE:
7743 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7744 case FOR_SCOPE:
7745 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7746 case WHILE_SCOPE:
7747 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7748 case TRY_SCOPE:
7749 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7750 case NO_SCOPE:
7751 case BLOCK_SCOPE:
7752 break;
7753 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007754 vim_free(scope);
7755}
7756
7757/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758 * compile "if expr"
7759 *
7760 * "if expr" Produces instructions:
7761 * EVAL expr Push result of "expr"
7762 * JUMP_IF_FALSE end
7763 * ... body ...
7764 * end:
7765 *
7766 * "if expr | else" Produces instructions:
7767 * EVAL expr Push result of "expr"
7768 * JUMP_IF_FALSE else
7769 * ... body ...
7770 * JUMP_ALWAYS end
7771 * else:
7772 * ... body ...
7773 * end:
7774 *
7775 * "if expr1 | elseif expr2 | else" Produces instructions:
7776 * EVAL expr Push result of "expr"
7777 * JUMP_IF_FALSE elseif
7778 * ... body ...
7779 * JUMP_ALWAYS end
7780 * elseif:
7781 * EVAL expr Push result of "expr"
7782 * JUMP_IF_FALSE else
7783 * ... body ...
7784 * JUMP_ALWAYS end
7785 * else:
7786 * ... body ...
7787 * end:
7788 */
7789 static char_u *
7790compile_if(char_u *arg, cctx_T *cctx)
7791{
7792 char_u *p = arg;
7793 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007794 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007795 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007796 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007797 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798
Bram Moolenaara5565e42020-05-09 15:44:01 +02007799 CLEAR_FIELD(ppconst);
7800 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007801 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007802 clear_ppconst(&ppconst);
7803 return NULL;
7804 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007805 if (!ends_excmd2(arg, skipwhite(p)))
7806 {
7807 semsg(_(e_trailing_arg), p);
7808 return NULL;
7809 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007810 if (cctx->ctx_skip == SKIP_YES)
7811 clear_ppconst(&ppconst);
7812 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007813 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007814 int error = FALSE;
7815 int v;
7816
Bram Moolenaara5565e42020-05-09 15:44:01 +02007817 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007818 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007819 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007820 if (error)
7821 return NULL;
7822 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007823 }
7824 else
7825 {
7826 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007827 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007828 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007829 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007830 if (bool_on_stack(cctx) == FAIL)
7831 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007833
Bram Moolenaara91a7132021-03-25 21:12:15 +01007834 // CMDMOD_REV must come before the jump
7835 generate_undo_cmdmods(cctx);
7836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007837 scope = new_scope(cctx, IF_SCOPE);
7838 if (scope == NULL)
7839 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007840 scope->se_skip_save = skip_save;
7841 // "is_had_return" will be reset if any block does not end in :return
7842 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007843
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007844 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007845 {
7846 // "where" is set when ":elseif", "else" or ":endif" is found
7847 scope->se_u.se_if.is_if_label = instr->ga_len;
7848 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7849 }
7850 else
7851 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007852
Bram Moolenaarced68a02021-01-24 17:53:47 +01007853#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007854 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007855 && skip_save != SKIP_YES)
7856 {
7857 // generated a profile start, need to generate a profile end, since it
7858 // won't be done after returning
7859 cctx->ctx_skip = SKIP_NOT;
7860 generate_instr(cctx, ISN_PROF_END);
7861 cctx->ctx_skip = SKIP_YES;
7862 }
7863#endif
7864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007865 return p;
7866}
7867
7868 static char_u *
7869compile_elseif(char_u *arg, cctx_T *cctx)
7870{
7871 char_u *p = arg;
7872 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar90770b72021-11-30 20:57:38 +00007873 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007874 isn_T *isn;
7875 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007876 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007877 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878
7879 if (scope == NULL || scope->se_type != IF_SCOPE)
7880 {
7881 emsg(_(e_elseif_without_if));
7882 return NULL;
7883 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007884 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007885 if (!cctx->ctx_had_return)
7886 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007887
Bram Moolenaarced68a02021-01-24 17:53:47 +01007888 if (cctx->ctx_skip == SKIP_NOT)
7889 {
7890 // previous block was executed, this one and following will not
7891 cctx->ctx_skip = SKIP_YES;
7892 scope->se_u.se_if.is_seen_skip_not = TRUE;
7893 }
7894 if (scope->se_u.se_if.is_seen_skip_not)
7895 {
7896 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007897 // Do not count the "elseif" for profiling and cmdmod
7898 instr->ga_len = current_instr_idx(cctx);
7899
Bram Moolenaarced68a02021-01-24 17:53:47 +01007900 skip_expr_cctx(&p, cctx);
7901 return p;
7902 }
7903
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007904 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007905 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007906 int moved_cmdmod = FALSE;
7907 int saved_debug = FALSE;
7908 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007909
7910 // Move any CMDMOD instruction to after the jump
7911 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7912 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007913 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007914 return NULL;
7915 ((isn_T *)instr->ga_data)[instr->ga_len] =
7916 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7917 --instr->ga_len;
7918 moved_cmdmod = TRUE;
7919 }
7920
Bram Moolenaar093165c2021-08-22 13:35:31 +02007921 // Remove the already generated ISN_DEBUG, it is written below the
7922 // ISN_FOR instruction.
7923 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7924 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7925 .isn_type == ISN_DEBUG)
7926 {
7927 --instr->ga_len;
7928 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7929 saved_debug = TRUE;
7930 }
7931
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007932 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007933 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007934 return NULL;
7935 // previous "if" or "elseif" jumps here
7936 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7937 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007938
Bram Moolenaara91a7132021-03-25 21:12:15 +01007939 if (moved_cmdmod)
7940 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007941
7942 if (saved_debug)
7943 {
7944 // move the debug instruction here
7945 if (GA_GROW_FAILS(instr, 1))
7946 return NULL;
7947 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7948 ++instr->ga_len;
7949 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007950 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007951
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007952 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007953 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007954 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007955 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007956 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007957#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007958 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007959 // the previous block was skipped, need to profile this line
7960 generate_instr(cctx, ISN_PROF_START);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007961#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007962 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007963 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007964 generate_instr_debug(cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007965 }
Bram Moolenaar90770b72021-11-30 20:57:38 +00007966
7967 instr_count = instr->ga_len;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007968 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007969 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007970 clear_ppconst(&ppconst);
7971 return NULL;
7972 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007973 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007974 if (!ends_excmd2(arg, skipwhite(p)))
7975 {
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007976 clear_ppconst(&ppconst);
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007977 semsg(_(e_trailing_arg), p);
7978 return NULL;
7979 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007980 if (scope->se_skip_save == SKIP_YES)
7981 clear_ppconst(&ppconst);
7982 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007983 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007984 int error = FALSE;
7985 int v;
7986
Bram Moolenaarfad27422021-11-30 21:58:19 +00007987 // The expression result is a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007988 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7989 if (error)
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007990 {
7991 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007992 return NULL;
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007993 }
Bram Moolenaar13106602020-10-04 16:06:05 +02007994 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007995 clear_ppconst(&ppconst);
7996 scope->se_u.se_if.is_if_label = -1;
7997 }
7998 else
7999 {
8000 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008001 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02008002 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008003 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008004 if (bool_on_stack(cctx) == FAIL)
8005 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008006
Bram Moolenaara91a7132021-03-25 21:12:15 +01008007 // CMDMOD_REV must come before the jump
8008 generate_undo_cmdmods(cctx);
8009
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008010 // "where" is set when ":elseif", "else" or ":endif" is found
8011 scope->se_u.se_if.is_if_label = instr->ga_len;
8012 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
8013 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008014
8015 return p;
8016}
8017
8018 static char_u *
8019compile_else(char_u *arg, cctx_T *cctx)
8020{
8021 char_u *p = arg;
8022 garray_T *instr = &cctx->ctx_instr;
8023 isn_T *isn;
8024 scope_T *scope = cctx->ctx_scope;
8025
8026 if (scope == NULL || scope->se_type != IF_SCOPE)
8027 {
8028 emsg(_(e_else_without_if));
8029 return NULL;
8030 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01008031 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008032 if (!cctx->ctx_had_return)
8033 scope->se_u.se_if.is_had_return = FALSE;
8034 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008035
Bram Moolenaarced68a02021-01-24 17:53:47 +01008036#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008037 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01008038 {
8039 if (cctx->ctx_skip == SKIP_NOT
8040 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8041 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02008042 // the previous block was executed, do not count "else" for
8043 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01008044 --instr->ga_len;
8045 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
8046 {
8047 // the previous block was not executed, this one will, do count the
8048 // "else" for profiling
8049 cctx->ctx_skip = SKIP_NOT;
8050 generate_instr(cctx, ISN_PROF_END);
8051 generate_instr(cctx, ISN_PROF_START);
8052 cctx->ctx_skip = SKIP_YES;
8053 }
8054 }
8055#endif
8056
8057 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008058 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008059 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008060 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008061 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008062 if (!cctx->ctx_had_return
8063 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
8064 JUMP_ALWAYS, cctx) == FAIL)
8065 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008066 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008067
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008068 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008069 {
8070 if (scope->se_u.se_if.is_if_label >= 0)
8071 {
8072 // previous "if" or "elseif" jumps here
8073 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8074 isn->isn_arg.jump.jump_where = instr->ga_len;
8075 scope->se_u.se_if.is_if_label = -1;
8076 }
8077 }
8078
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008079 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008080 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
8081 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008082
8083 return p;
8084}
8085
8086 static char_u *
8087compile_endif(char_u *arg, cctx_T *cctx)
8088{
8089 scope_T *scope = cctx->ctx_scope;
8090 ifscope_T *ifscope;
8091 garray_T *instr = &cctx->ctx_instr;
8092 isn_T *isn;
8093
Bram Moolenaarfa984412021-03-25 22:15:28 +01008094 if (misplaced_cmdmod(cctx))
8095 return NULL;
8096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097 if (scope == NULL || scope->se_type != IF_SCOPE)
8098 {
8099 emsg(_(e_endif_without_if));
8100 return NULL;
8101 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008102 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008103 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008104 if (!cctx->ctx_had_return)
8105 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008106
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008107 if (scope->se_u.se_if.is_if_label >= 0)
8108 {
8109 // previous "if" or "elseif" jumps here
8110 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8111 isn->isn_arg.jump.jump_where = instr->ga_len;
8112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008113 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008114 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01008115
8116#ifdef FEAT_PROFILE
8117 // even when skipping we count the endif as executed, unless the block it's
8118 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02008119 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01008120 && scope->se_skip_save != SKIP_YES)
8121 {
8122 cctx->ctx_skip = SKIP_NOT;
8123 generate_instr(cctx, ISN_PROF_START);
8124 }
8125#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02008126 cctx->ctx_skip = scope->se_skip_save;
8127
8128 // If all the blocks end in :return and there is an :else then the
8129 // had_return flag is set.
8130 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008131
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008132 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008133 return arg;
8134}
8135
8136/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01008137 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008138 *
8139 * Produces instructions:
8140 * PUSHNR -1
8141 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01008142 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008143 * top: FOR loop-idx, end Increment index, use list on bottom of stack
8144 * - if beyond end, jump to "end"
8145 * - otherwise get item from list and push it
8146 * STORE var Store item in "var"
8147 * ... body ...
8148 * JUMP top Jump back to repeat
8149 * end: DROP Drop the result of "expr"
8150 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008151 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
8152 * UNPACK 2 Split item in 2
8153 * STORE var1 Store item in "var1"
8154 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155 */
8156 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008157compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008158{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008159 char_u *arg;
8160 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008161 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008162 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008163 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008164 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008165 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008166 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008167 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008168 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008169 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008170 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008171 lvar_T *loop_lvar; // loop iteration variable
8172 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008173 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008174 type_T *item_type = &t_any;
8175 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008176 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177
Bram Moolenaar792f7862020-11-23 08:31:18 +01008178 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008179 if (p == NULL)
8180 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008181 if (var_count == 0)
8182 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008183 else
8184 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008185
8186 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008187 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008188 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8189 return NULL;
8190 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008191 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008192 if (*p == ':' && wp != p)
8193 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8194 else
8195 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008196 return NULL;
8197 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008198 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008199 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8200 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008201
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008202 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8203 // instruction.
8204 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8205 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8206 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008207 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008208 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008209 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8210 .isn_arg.debug.dbg_break_lnum;
8211 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008213 scope = new_scope(cctx, FOR_SCOPE);
8214 if (scope == NULL)
8215 return NULL;
8216
Bram Moolenaar792f7862020-11-23 08:31:18 +01008217 // Reserve a variable to store the loop iteration counter and initialize it
8218 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008219 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8220 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008221 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008222 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008223 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008224 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008225 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008226 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008227
8228 // compile "expr", it remains on the stack until "endfor"
8229 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008230 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008231 {
8232 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008233 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008234 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008235 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008236
rbtnnbebf0692021-08-21 17:26:50 +02008237 if (cctx->ctx_skip != SKIP_YES)
8238 {
8239 // If we know the type of "var" and it is a not a supported type we can
8240 // give an error now.
8241 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8242 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008243 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008244 {
rbtnnbebf0692021-08-21 17:26:50 +02008245 semsg(_(e_for_loop_on_str_not_supported),
8246 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008247 drop_scope(cctx);
8248 return NULL;
8249 }
rbtnnbebf0692021-08-21 17:26:50 +02008250
8251 if (vartype->tt_type == VAR_STRING)
8252 item_type = &t_string;
8253 else if (vartype->tt_type == VAR_BLOB)
8254 item_type = &t_number;
8255 else if (vartype->tt_type == VAR_LIST
8256 && vartype->tt_member->tt_type != VAR_ANY)
8257 {
8258 if (!var_list)
8259 item_type = vartype->tt_member;
8260 else if (vartype->tt_member->tt_type == VAR_LIST
8261 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
rbtnnbebf0692021-08-21 17:26:50 +02008262 item_type = vartype->tt_member->tt_member;
8263 }
8264
8265 // CMDMOD_REV must come before the FOR instruction.
8266 generate_undo_cmdmods(cctx);
8267
8268 // "for_end" is set when ":endfor" is found
8269 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8270
8271 generate_FOR(cctx, loop_lvar->lv_idx);
8272
8273 arg = arg_start;
8274 if (var_list)
8275 {
8276 generate_UNPACK(cctx, var_count, semicolon);
8277 arg = skipwhite(arg + 1); // skip white after '['
8278
8279 // the list item is replaced by a number of items
8280 if (GA_GROW_FAILS(stack, var_count - 1))
8281 {
8282 drop_scope(cctx);
8283 return NULL;
8284 }
8285 --stack->ga_len;
8286 for (idx = 0; idx < var_count; ++idx)
8287 {
8288 ((type_T **)stack->ga_data)[stack->ga_len] =
8289 (semicolon && idx == 0) ? vartype : item_type;
8290 ++stack->ga_len;
8291 }
8292 }
8293
Bram Moolenaar792f7862020-11-23 08:31:18 +01008294 for (idx = 0; idx < var_count; ++idx)
8295 {
rbtnnbebf0692021-08-21 17:26:50 +02008296 assign_dest_T dest = dest_local;
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008297 int opt_flags = 0;
8298 int vimvaridx = -1;
rbtnnbebf0692021-08-21 17:26:50 +02008299 type_T *type = &t_any;
8300 type_T *lhs_type = &t_any;
8301 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008302
rbtnnbebf0692021-08-21 17:26:50 +02008303 p = skip_var_one(arg, FALSE);
8304 varlen = p - arg;
8305 name = vim_strnsave(arg, varlen);
8306 if (name == NULL)
8307 goto failed;
8308 if (*p == ':')
8309 {
8310 p = skipwhite(p + 1);
8311 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8312 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008313
Bram Moolenaarfad27422021-11-30 21:58:19 +00008314 // Script var is not supported.
rbtnnbebf0692021-08-21 17:26:50 +02008315 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008316 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008317 goto failed;
8318 if (dest != dest_local)
8319 {
8320 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008321 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008322 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008323 }
rbtnnbebf0692021-08-21 17:26:50 +02008324 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008325 {
rbtnnbebf0692021-08-21 17:26:50 +02008326 // Assigning to "_": drop the value.
8327 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8328 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008329 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008330 else
rbtnnbebf0692021-08-21 17:26:50 +02008331 {
Mike Williamscc9d7252021-11-23 12:35:57 +00008332 if (!valid_varname(arg, (int)varlen, FALSE))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008333 goto failed;
rbtnnbebf0692021-08-21 17:26:50 +02008334 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8335 {
8336 semsg(_(e_variable_already_declared), arg);
8337 goto failed;
8338 }
8339
8340 if (STRNCMP(name, "s:", 2) == 0)
8341 {
8342 semsg(_(e_cannot_declare_script_variable_in_function), name);
8343 goto failed;
8344 }
8345
8346 // Reserve a variable to store "var".
8347 where.wt_index = var_list ? idx + 1 : 0;
8348 where.wt_variable = TRUE;
8349 if (lhs_type == &t_any)
8350 lhs_type = item_type;
8351 else if (item_type != &t_unknown
8352 && (item_type == &t_any
8353 ? need_type(item_type, lhs_type,
8354 -1, 0, cctx, FALSE, FALSE)
8355 : check_type(lhs_type, item_type, TRUE, where))
8356 == FAIL)
8357 goto failed;
8358 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8359 if (var_lvar == NULL)
8360 // out of memory or used as an argument
8361 goto failed;
8362
8363 if (semicolon && idx == var_count - 1)
8364 var_lvar->lv_type = vartype;
8365 else
8366 var_lvar->lv_type = item_type;
8367 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8368 }
8369
8370 if (*p == ',' || *p == ';')
8371 ++p;
8372 arg = skipwhite(p);
8373 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008374 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008375
rbtnnbebf0692021-08-21 17:26:50 +02008376 if (cctx->ctx_compile_type == CT_DEBUG)
8377 {
8378 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008379
rbtnnbebf0692021-08-21 17:26:50 +02008380 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8381 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8382 cctx->ctx_prev_lnum = prev_lnum;
8383 generate_instr_debug(cctx);
8384 cctx->ctx_prev_lnum = save_prev_lnum;
8385 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008386 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008387
Bram Moolenaar792f7862020-11-23 08:31:18 +01008388 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008389
8390failed:
8391 vim_free(name);
8392 drop_scope(cctx);
8393 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008394}
8395
8396/*
8397 * compile "endfor"
8398 */
8399 static char_u *
8400compile_endfor(char_u *arg, cctx_T *cctx)
8401{
8402 garray_T *instr = &cctx->ctx_instr;
8403 scope_T *scope = cctx->ctx_scope;
8404 forscope_T *forscope;
8405 isn_T *isn;
8406
Bram Moolenaarfa984412021-03-25 22:15:28 +01008407 if (misplaced_cmdmod(cctx))
8408 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008410 if (scope == NULL || scope->se_type != FOR_SCOPE)
8411 {
8412 emsg(_(e_for));
8413 return NULL;
8414 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008415 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008416 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008417 if (cctx->ctx_skip != SKIP_YES)
8418 {
8419 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008420
rbtnnbebf0692021-08-21 17:26:50 +02008421 // At end of ":for" scope jump back to the FOR instruction.
8422 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008423
rbtnnbebf0692021-08-21 17:26:50 +02008424 // Fill in the "end" label in the FOR statement so it can jump here.
8425 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8426 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008427
rbtnnbebf0692021-08-21 17:26:50 +02008428 // Fill in the "end" label any BREAK statements
8429 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008430
rbtnnbebf0692021-08-21 17:26:50 +02008431 // Below the ":for" scope drop the "expr" list from the stack.
8432 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8433 return NULL;
8434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008435
8436 vim_free(scope);
8437
8438 return arg;
8439}
8440
8441/*
8442 * compile "while expr"
8443 *
8444 * Produces instructions:
8445 * top: EVAL expr Push result of "expr"
8446 * JUMP_IF_FALSE end jump if false
8447 * ... body ...
8448 * JUMP top Jump back to repeat
8449 * end:
8450 *
8451 */
8452 static char_u *
8453compile_while(char_u *arg, cctx_T *cctx)
8454{
8455 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008456 scope_T *scope;
8457
8458 scope = new_scope(cctx, WHILE_SCOPE);
8459 if (scope == NULL)
8460 return NULL;
8461
Bram Moolenaara91a7132021-03-25 21:12:15 +01008462 // "endwhile" jumps back here, one before when profiling or using cmdmods
8463 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008464
8465 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008466 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008467 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008468
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008469 if (!ends_excmd2(arg, skipwhite(p)))
8470 {
8471 semsg(_(e_trailing_arg), p);
8472 return NULL;
8473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008474
rbtnnd895b1d2021-08-20 20:54:25 +02008475 if (cctx->ctx_skip != SKIP_YES)
8476 {
8477 if (bool_on_stack(cctx) == FAIL)
8478 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008479
rbtnnd895b1d2021-08-20 20:54:25 +02008480 // CMDMOD_REV must come before the jump
8481 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008482
rbtnnd895b1d2021-08-20 20:54:25 +02008483 // "while_end" is set when ":endwhile" is found
8484 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008485 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008486 return FAIL;
8487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008488
8489 return p;
8490}
8491
8492/*
8493 * compile "endwhile"
8494 */
8495 static char_u *
8496compile_endwhile(char_u *arg, cctx_T *cctx)
8497{
8498 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008499 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008500
Bram Moolenaarfa984412021-03-25 22:15:28 +01008501 if (misplaced_cmdmod(cctx))
8502 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008503 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8504 {
8505 emsg(_(e_while));
8506 return NULL;
8507 }
8508 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008509 if (cctx->ctx_skip != SKIP_YES)
8510 {
8511 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008512
Bram Moolenaarf002a412021-01-24 13:34:18 +01008513#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008514 // count the endwhile before jumping
8515 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008516#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008517
rbtnnd895b1d2021-08-20 20:54:25 +02008518 // At end of ":for" scope jump back to the FOR instruction.
8519 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008520
rbtnnd895b1d2021-08-20 20:54:25 +02008521 // Fill in the "end" label in the WHILE statement so it can jump here.
8522 // And in any jumps for ":break"
8523 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008524 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008525 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008526
8527 vim_free(scope);
8528
8529 return arg;
8530}
8531
8532/*
8533 * compile "continue"
8534 */
8535 static char_u *
8536compile_continue(char_u *arg, cctx_T *cctx)
8537{
8538 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008539 int try_scopes = 0;
8540 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008541
8542 for (;;)
8543 {
8544 if (scope == NULL)
8545 {
8546 emsg(_(e_continue));
8547 return NULL;
8548 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008549 if (scope->se_type == FOR_SCOPE)
8550 {
8551 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008553 }
8554 if (scope->se_type == WHILE_SCOPE)
8555 {
8556 loop_label = scope->se_u.se_while.ws_top_label;
8557 break;
8558 }
8559 if (scope->se_type == TRY_SCOPE)
8560 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008561 scope = scope->se_outer;
8562 }
8563
Bram Moolenaarc150c092021-02-13 15:02:46 +01008564 if (try_scopes > 0)
8565 // Inside one or more try/catch blocks we first need to jump to the
8566 // "finally" or "endtry" to cleanup.
8567 generate_TRYCONT(cctx, try_scopes, loop_label);
8568 else
8569 // Jump back to the FOR or WHILE instruction.
8570 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008572 return arg;
8573}
8574
8575/*
8576 * compile "break"
8577 */
8578 static char_u *
8579compile_break(char_u *arg, cctx_T *cctx)
8580{
8581 scope_T *scope = cctx->ctx_scope;
8582 endlabel_T **el;
8583
8584 for (;;)
8585 {
8586 if (scope == NULL)
8587 {
8588 emsg(_(e_break));
8589 return NULL;
8590 }
8591 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8592 break;
8593 scope = scope->se_outer;
8594 }
8595
8596 // Jump to the end of the FOR or WHILE loop.
8597 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008598 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008599 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008600 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008601 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8602 return FAIL;
8603
8604 return arg;
8605}
8606
8607/*
8608 * compile "{" start of block
8609 */
8610 static char_u *
8611compile_block(char_u *arg, cctx_T *cctx)
8612{
8613 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8614 return NULL;
8615 return skipwhite(arg + 1);
8616}
8617
8618/*
8619 * compile end of block: drop one scope
8620 */
8621 static void
8622compile_endblock(cctx_T *cctx)
8623{
8624 scope_T *scope = cctx->ctx_scope;
8625
8626 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008627 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008628 vim_free(scope);
8629}
8630
8631/*
8632 * compile "try"
8633 * Creates a new scope for the try-endtry, pointing to the first catch and
8634 * finally.
8635 * Creates another scope for the "try" block itself.
8636 * TRY instruction sets up exception handling at runtime.
8637 *
8638 * "try"
8639 * TRY -> catch1, -> finally push trystack entry
8640 * ... try block
8641 * "throw {exception}"
8642 * EVAL {exception}
8643 * THROW create exception
8644 * ... try block
8645 * " catch {expr}"
8646 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008647 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008648 * EVAL {expr}
8649 * MATCH
8650 * JUMP nomatch -> catch2
8651 * CATCH remove exception
8652 * ... catch block
8653 * " catch"
8654 * JUMP -> finally
8655 * catch2: CATCH remove exception
8656 * ... catch block
8657 * " finally"
8658 * finally:
8659 * ... finally block
8660 * " endtry"
8661 * ENDTRY pop trystack entry, may rethrow
8662 */
8663 static char_u *
8664compile_try(char_u *arg, cctx_T *cctx)
8665{
8666 garray_T *instr = &cctx->ctx_instr;
8667 scope_T *try_scope;
8668 scope_T *scope;
8669
Bram Moolenaarfa984412021-03-25 22:15:28 +01008670 if (misplaced_cmdmod(cctx))
8671 return NULL;
8672
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008673 // scope that holds the jumps that go to catch/finally/endtry
8674 try_scope = new_scope(cctx, TRY_SCOPE);
8675 if (try_scope == NULL)
8676 return NULL;
8677
Bram Moolenaar69f70502021-01-01 16:10:46 +01008678 if (cctx->ctx_skip != SKIP_YES)
8679 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008680 isn_T *isn;
8681
8682 // "try_catch" is set when the first ":catch" is found or when no catch
8683 // is found and ":finally" is found.
8684 // "try_finally" is set when ":finally" is found
8685 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008686 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008687 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8688 return NULL;
8689 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8690 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008691 return NULL;
8692 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008693
8694 // scope for the try block itself
8695 scope = new_scope(cctx, BLOCK_SCOPE);
8696 if (scope == NULL)
8697 return NULL;
8698
8699 return arg;
8700}
8701
8702/*
8703 * compile "catch {expr}"
8704 */
8705 static char_u *
8706compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8707{
8708 scope_T *scope = cctx->ctx_scope;
8709 garray_T *instr = &cctx->ctx_instr;
8710 char_u *p;
8711 isn_T *isn;
8712
Bram Moolenaarfa984412021-03-25 22:15:28 +01008713 if (misplaced_cmdmod(cctx))
8714 return NULL;
8715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008716 // end block scope from :try or :catch
8717 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8718 compile_endblock(cctx);
8719 scope = cctx->ctx_scope;
8720
8721 // Error if not in a :try scope
8722 if (scope == NULL || scope->se_type != TRY_SCOPE)
8723 {
8724 emsg(_(e_catch));
8725 return NULL;
8726 }
8727
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008728 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008729 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008730 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008731 return NULL;
8732 }
8733
Bram Moolenaar69f70502021-01-01 16:10:46 +01008734 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008735 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008736#ifdef FEAT_PROFILE
8737 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008738 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008739 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008740 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008741 .isn_type == ISN_PROF_START)
8742 --instr->ga_len;
8743#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008744 // Jump from end of previous block to :finally or :endtry
8745 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8746 JUMP_ALWAYS, cctx) == FAIL)
8747 return NULL;
8748
8749 // End :try or :catch scope: set value in ISN_TRY instruction
8750 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008751 if (isn->isn_arg.try.try_ref->try_catch == 0)
8752 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008753 if (scope->se_u.se_try.ts_catch_label != 0)
8754 {
8755 // Previous catch without match jumps here
8756 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8757 isn->isn_arg.jump.jump_where = instr->ga_len;
8758 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008759#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008760 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008761 {
8762 // a "throw" that jumps here needs to be counted
8763 generate_instr(cctx, ISN_PROF_END);
8764 // the "catch" is also counted
8765 generate_instr(cctx, ISN_PROF_START);
8766 }
8767#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008768 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008769 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008770 }
8771
8772 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008773 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008774 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008775 scope->se_u.se_try.ts_caught_all = TRUE;
8776 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008777 }
8778 else
8779 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008780 char_u *end;
8781 char_u *pat;
8782 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008783 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008784 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008786 // Push v:exception, push {expr} and MATCH
8787 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8788
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008789 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008790 if (*end != *p)
8791 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008792 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008793 vim_free(tofree);
8794 return FAIL;
8795 }
8796 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008797 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008798 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008799 len = (int)(end - tofree);
8800 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008801 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008802 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008803 if (pat == NULL)
8804 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008805 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008806 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008808 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8809 return NULL;
8810
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008811 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008812 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8813 return NULL;
8814 }
8815
Bram Moolenaar69f70502021-01-01 16:10:46 +01008816 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008817 return NULL;
8818
8819 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8820 return NULL;
8821 return p;
8822}
8823
8824 static char_u *
8825compile_finally(char_u *arg, cctx_T *cctx)
8826{
8827 scope_T *scope = cctx->ctx_scope;
8828 garray_T *instr = &cctx->ctx_instr;
8829 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008830 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008831
Bram Moolenaarfa984412021-03-25 22:15:28 +01008832 if (misplaced_cmdmod(cctx))
8833 return NULL;
8834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008835 // end block scope from :try or :catch
8836 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8837 compile_endblock(cctx);
8838 scope = cctx->ctx_scope;
8839
8840 // Error if not in a :try scope
8841 if (scope == NULL || scope->se_type != TRY_SCOPE)
8842 {
8843 emsg(_(e_finally));
8844 return NULL;
8845 }
8846
rbtnn84934992021-08-07 13:26:53 +02008847 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008848 {
rbtnn84934992021-08-07 13:26:53 +02008849 // End :catch or :finally scope: set value in ISN_TRY instruction
8850 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8851 if (isn->isn_arg.try.try_ref->try_finally != 0)
8852 {
8853 emsg(_(e_finally_dup));
8854 return NULL;
8855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008856
rbtnn84934992021-08-07 13:26:53 +02008857 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008858#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008859 if (cctx->ctx_compile_type == CT_PROFILE
8860 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008861 .isn_type == ISN_PROF_START)
rbtnn84934992021-08-07 13:26:53 +02008862 {
8863 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008864 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008865
8866 // jump to the profile end above it
8867 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008868 .isn_type == ISN_PROF_END)
rbtnn84934992021-08-07 13:26:53 +02008869 --this_instr;
8870 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008871#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008872
rbtnn84934992021-08-07 13:26:53 +02008873 // Fill in the "end" label in jumps at the end of the blocks.
8874 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarfad27422021-11-30 21:58:19 +00008875 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008876
rbtnn84934992021-08-07 13:26:53 +02008877 // If there is no :catch then an exception jumps to :finally.
8878 if (isn->isn_arg.try.try_ref->try_catch == 0)
8879 isn->isn_arg.try.try_ref->try_catch = this_instr;
8880 isn->isn_arg.try.try_ref->try_finally = this_instr;
8881 if (scope->se_u.se_try.ts_catch_label != 0)
8882 {
8883 // Previous catch without match jumps here
8884 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8885 isn->isn_arg.jump.jump_where = this_instr;
8886 scope->se_u.se_try.ts_catch_label = 0;
8887 }
8888 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8889 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008890 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008891
8892 return arg;
8893}
8894
8895 static char_u *
8896compile_endtry(char_u *arg, cctx_T *cctx)
8897{
8898 scope_T *scope = cctx->ctx_scope;
8899 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008900 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008901
Bram Moolenaarfa984412021-03-25 22:15:28 +01008902 if (misplaced_cmdmod(cctx))
8903 return NULL;
8904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008905 // end block scope from :catch or :finally
8906 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8907 compile_endblock(cctx);
8908 scope = cctx->ctx_scope;
8909
8910 // Error if not in a :try scope
8911 if (scope == NULL || scope->se_type != TRY_SCOPE)
8912 {
8913 if (scope == NULL)
8914 emsg(_(e_no_endtry));
8915 else if (scope->se_type == WHILE_SCOPE)
8916 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008917 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008918 emsg(_(e_endfor));
8919 else
8920 emsg(_(e_endif));
8921 return NULL;
8922 }
8923
Bram Moolenaarc150c092021-02-13 15:02:46 +01008924 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008925 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008926 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008927 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8928 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008929 {
8930 emsg(_(e_missing_catch_or_finally));
8931 return NULL;
8932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008933
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008934#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008935 if (cctx->ctx_compile_type == CT_PROFILE
8936 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008937 .isn_type == ISN_PROF_START)
8938 // move the profile start after "endtry" so that it's not counted when
8939 // the exception is rethrown.
8940 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008941#endif
8942
Bram Moolenaar69f70502021-01-01 16:10:46 +01008943 // Fill in the "end" label in jumps at the end of the blocks, if not
8944 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008945 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8946 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008947
Bram Moolenaar69f70502021-01-01 16:10:46 +01008948 if (scope->se_u.se_try.ts_catch_label != 0)
8949 {
8950 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008951 isn_T *isn = ((isn_T *)instr->ga_data)
8952 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008953 isn->isn_arg.jump.jump_where = instr->ga_len;
8954 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008955 }
8956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008957 compile_endblock(cctx);
8958
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008959 if (cctx->ctx_skip != SKIP_YES)
8960 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008961 // End :catch or :finally scope: set instruction index in ISN_TRY
8962 // instruction
8963 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008964 if (cctx->ctx_skip != SKIP_YES
8965 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8966 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008967#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008968 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008969 generate_instr(cctx, ISN_PROF_START);
8970#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008972 return arg;
8973}
8974
8975/*
8976 * compile "throw {expr}"
8977 */
8978 static char_u *
8979compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8980{
8981 char_u *p = skipwhite(arg);
8982
Bram Moolenaara5565e42020-05-09 15:44:01 +02008983 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008984 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008985 if (cctx->ctx_skip == SKIP_YES)
8986 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008987 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008988 return NULL;
8989 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8990 return NULL;
8991
8992 return p;
8993}
8994
Bram Moolenaarc3235272021-07-10 19:42:03 +02008995 static char_u *
8996compile_eval(char_u *arg, cctx_T *cctx)
8997{
8998 char_u *p = arg;
8999 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02009000 long lnum = SOURCING_LNUM;
9001
9002 // find_ex_command() will consider a variable name an expression, assuming
9003 // that something follows on the next line. Check that something actually
9004 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02009005 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02009006
Bram Moolenaarc3235272021-07-10 19:42:03 +02009007 if (compile_expr0(&p, cctx) == FAIL)
9008 return NULL;
9009
9010 if (name_only && lnum == SOURCING_LNUM)
9011 {
9012 semsg(_(e_expression_without_effect_str), arg);
9013 return NULL;
9014 }
9015
9016 // drop the result
9017 generate_instr_drop(cctx, ISN_DROP, 1);
9018
9019 return skipwhite(p);
9020}
9021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009022/*
9023 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009024 * compile "echomsg expr"
9025 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02009026 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01009027 * compile "execute expr"
9028 */
9029 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009030compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009031{
9032 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01009033 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009034 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009035 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009036 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009037 garray_T *stack = &cctx->ctx_type_stack;
9038 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009039
9040 for (;;)
9041 {
Bram Moolenaare4984292020-12-13 14:19:25 +01009042 if (ends_excmd2(prev, p))
9043 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009044 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009045 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009046 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009047
9048 if (cctx->ctx_skip != SKIP_YES)
9049 {
9050 // check for non-void type
9051 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
9052 if (type->tt_type == VAR_VOID)
9053 {
9054 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
9055 return NULL;
9056 }
9057 }
9058
Bram Moolenaarad39c092020-02-26 18:23:43 +01009059 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02009060 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009061 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009062 }
9063
Bram Moolenaare4984292020-12-13 14:19:25 +01009064 if (count > 0)
9065 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009066 long save_lnum = cctx->ctx_lnum;
9067
9068 // Use the line number where the command started.
9069 cctx->ctx_lnum = start_ctx_lnum;
9070
Bram Moolenaare4984292020-12-13 14:19:25 +01009071 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
9072 generate_ECHO(cctx, cmdidx == CMD_echo, count);
9073 else if (cmdidx == CMD_execute)
9074 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
9075 else if (cmdidx == CMD_echomsg)
9076 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02009077 else if (cmdidx == CMD_echoconsole)
9078 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01009079 else
9080 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009081
9082 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01009083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009084 return p;
9085}
9086
9087/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01009088 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01009089 * instruction to compute it and return OK.
9090 * Otherwise return FAIL, the caller must deal with any range.
9091 */
9092 static int
9093compile_variable_range(exarg_T *eap, cctx_T *cctx)
9094{
9095 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
9096 char_u *p = skipdigits(eap->cmd);
9097
9098 if (p == range_end)
9099 return FAIL;
9100 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
9101}
9102
9103/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009104 * :put r
9105 * :put ={expr}
9106 */
9107 static char_u *
9108compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
9109{
9110 char_u *line = arg;
9111 linenr_T lnum;
9112 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009113 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009114
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009115 eap->regname = *line;
9116
9117 if (eap->regname == '=')
9118 {
9119 char_u *p = line + 1;
9120
9121 if (compile_expr0(&p, cctx) == FAIL)
9122 return NULL;
9123 line = p;
9124 }
9125 else if (eap->regname != NUL)
9126 ++line;
9127
Bram Moolenaar08597872020-12-10 19:43:40 +01009128 if (compile_variable_range(eap, cctx) == OK)
9129 {
9130 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
9131 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009132 else
Bram Moolenaar08597872020-12-10 19:43:40 +01009133 {
9134 // Either no range or a number.
9135 // "errormsg" will not be set because the range is ADDR_LINES.
9136 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01009137 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01009138 return NULL;
9139 if (eap->addr_count == 0)
9140 lnum = -1;
9141 else
9142 lnum = eap->line2;
9143 if (above)
9144 --lnum;
9145 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009146
9147 generate_PUT(cctx, eap->regname, lnum);
9148 return line;
9149}
9150
9151/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009152 * A command that is not compiled, execute with legacy code.
9153 */
9154 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009155compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009156{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009157 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009158 char_u *p;
9159 int has_expr = FALSE;
9160 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009161 char_u *tofree = NULL;
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009162 char_u *cmd_arg = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009163
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009164 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009165 goto theend;
9166
Bram Moolenaare729ce22021-06-06 21:38:09 +02009167 // If there was a prececing command modifier, drop it and include it in the
9168 // EXEC command.
9169 if (cctx->ctx_has_cmdmod)
9170 {
9171 garray_T *instr = &cctx->ctx_instr;
9172 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9173
9174 if (isn->isn_type == ISN_CMDMOD)
9175 {
9176 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9177 ->cmod_filter_regmatch.regprog);
9178 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9179 --instr->ga_len;
9180 cctx->ctx_has_cmdmod = FALSE;
9181 }
9182 }
9183
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009184 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009185 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009186 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009187 int usefilter = FALSE;
9188
9189 has_expr = argt & (EX_XFILE | EX_EXPAND);
9190
9191 // If the command can be followed by a bar, find the bar and truncate
9192 // it, so that the following command can be compiled.
9193 // The '|' is overwritten with a NUL, it is put back below.
9194 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9195 && *eap->arg == '!')
9196 // :w !filter or :r !filter or :r! filter
9197 usefilter = TRUE;
9198 if ((argt & EX_TRLBAR) && !usefilter)
9199 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009200 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009201 separate_nextcmd(eap);
9202 if (eap->nextcmd != NULL)
9203 nextcmd = eap->nextcmd;
9204 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009205 else if (eap->cmdidx == CMD_wincmd)
9206 {
9207 p = eap->arg;
9208 if (*p != NUL)
9209 ++p;
9210 if (*p == 'g' || *p == Ctrl_G)
9211 ++p;
9212 p = skipwhite(p);
9213 if (*p == '|')
9214 {
9215 *p = NUL;
9216 nextcmd = p + 1;
9217 }
9218 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009219 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9220 {
9221 // If there is a trailing '{' read lines until the '}'
9222 p = eap->arg + STRLEN(eap->arg) - 1;
9223 while (p > eap->arg && VIM_ISWHITE(*p))
9224 --p;
9225 if (*p == '{')
9226 {
9227 exarg_T ea;
9228 int flags; // unused
9229 int start_lnum = SOURCING_LNUM;
9230
9231 CLEAR_FIELD(ea);
9232 ea.arg = eap->arg;
9233 fill_exarg_from_cctx(&ea, cctx);
9234 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9235 if (tofree != NULL)
9236 {
9237 *p = NUL;
9238 line = concat_str(line, tofree);
9239 if (line == NULL)
9240 goto theend;
9241 vim_free(tofree);
9242 tofree = line;
9243 SOURCING_LNUM = start_lnum;
9244 }
9245 }
9246 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009247 }
9248
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009249 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9250 {
9251 // expand filename in "syntax include [@group] filename"
9252 has_expr = TRUE;
9253 eap->arg = skipwhite(eap->arg + 7);
9254 if (*eap->arg == '@')
9255 eap->arg = skiptowhite(eap->arg);
9256 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009257
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009258 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9259 && STRLEN(eap->arg) > 4)
9260 {
9261 int delim = *eap->arg;
9262
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009263 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009264 if (*p == delim)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009265 cmd_arg = p + 1;
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009266 }
9267
Bram Moolenaarecac5912021-01-05 19:23:28 +01009268 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009269 cmd_arg = eap->arg;
9270
9271 if (cmd_arg != NULL)
Bram Moolenaarecac5912021-01-05 19:23:28 +01009272 {
Bram Moolenaarfad27422021-11-30 21:58:19 +00009273 exarg_T nea;
9274
9275 CLEAR_FIELD(nea);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009276 nea.cmd = cmd_arg;
Bram Moolenaarfad27422021-11-30 21:58:19 +00009277 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009278 if (nea.cmdidx < CMD_SIZE)
Bram Moolenaarfad27422021-11-30 21:58:19 +00009279 {
9280 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
9281 if (has_expr)
9282 eap->arg = skiptowhite(eap->arg);
9283 }
Bram Moolenaarecac5912021-01-05 19:23:28 +01009284 }
9285
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009286 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009287 {
9288 int count = 0;
9289 char_u *start = skipwhite(line);
9290
9291 // :cmd xxx`=expr1`yyy`=expr2`zzz
9292 // PUSHS ":cmd xxx"
9293 // eval expr1
9294 // PUSHS "yyy"
9295 // eval expr2
9296 // PUSHS "zzz"
9297 // EXECCONCAT 5
9298 for (;;)
9299 {
9300 if (p > start)
9301 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009302 char_u *val = vim_strnsave(start, p - start);
9303
9304 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009305 ++count;
9306 }
9307 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009308 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009309 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009310 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009311 ++count;
9312 p = skipwhite(p);
9313 if (*p != '`')
9314 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009315 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009316 return NULL;
9317 }
9318 start = p + 1;
9319
9320 p = (char_u *)strstr((char *)start, "`=");
9321 if (p == NULL)
9322 {
9323 if (*skipwhite(start) != NUL)
9324 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009325 char_u *val = vim_strsave(start);
9326
9327 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009328 ++count;
9329 }
9330 break;
9331 }
9332 }
9333 generate_EXECCONCAT(cctx, count);
9334 }
9335 else
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009336 generate_EXEC_copy(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009337
9338theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009339 if (*nextcmd != NUL)
9340 {
9341 // the parser expects a pointer to the bar, put it back
9342 --nextcmd;
9343 *nextcmd = '|';
9344 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009345 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009346
9347 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009348}
9349
Bram Moolenaar20677332021-06-06 17:02:53 +02009350/*
9351 * A script command with heredoc, e.g.
9352 * ruby << EOF
9353 * command
9354 * EOF
9355 * Has been turned into one long line with NL characters by
9356 * get_function_body():
9357 * ruby << EOF<NL> command<NL>EOF
9358 */
9359 static char_u *
9360compile_script(char_u *line, cctx_T *cctx)
9361{
9362 if (cctx->ctx_skip != SKIP_YES)
9363 {
9364 isn_T *isn;
9365
9366 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9367 return NULL;
9368 isn->isn_arg.string = vim_strsave(line);
9369 }
9370 return (char_u *)"";
9371}
9372
Bram Moolenaar8238f082021-04-20 21:10:48 +02009373
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009374/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009375 * :s/pat/repl/
9376 */
9377 static char_u *
9378compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9379{
9380 char_u *cmd = eap->arg;
9381 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9382
9383 if (expr != NULL)
9384 {
9385 int delimiter = *cmd++;
9386
9387 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009388 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009389 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9390 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009391 garray_T save_ga = cctx->ctx_instr;
9392 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009393 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009394 int trailing_error;
9395 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009396 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009397 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009398
9399 cmd += 3;
9400 end = skip_substitute(cmd, delimiter);
9401
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009402 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009403 // labels are correct.
9404 cctx->ctx_instr.ga_len = 0;
9405 cctx->ctx_instr.ga_maxlen = 0;
9406 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009407 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009408 if (end[-1] == NUL)
9409 end[-1] = delimiter;
9410 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009411 trailing_error = *cmd != delimiter && *cmd != NUL;
9412
Bram Moolenaar169502f2021-04-21 12:19:35 +02009413 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009414 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009415 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009416 if (trailing_error)
9417 semsg(_(e_trailing_arg), cmd);
9418 clear_instr_ga(&cctx->ctx_instr);
9419 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009420 return NULL;
9421 }
9422
Bram Moolenaar8238f082021-04-20 21:10:48 +02009423 // Move the generated instructions into the ISN_SUBSTITUTE
9424 // instructions, then restore the list of instructions before
9425 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009426 instr_count = cctx->ctx_instr.ga_len;
9427 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009428 instr[instr_count].isn_type = ISN_FINISH;
9429
9430 cctx->ctx_instr = save_ga;
9431 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9432 {
9433 int idx;
9434
9435 for (idx = 0; idx < instr_count; ++idx)
9436 delete_instr(instr + idx);
9437 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009438 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009439 }
9440 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9441 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009442
9443 // skip over flags
9444 if (*end == '&')
9445 ++end;
9446 while (ASCII_ISALPHA(*end) || *end == '#')
9447 ++end;
9448 return end;
9449 }
9450 }
9451
9452 return compile_exec(arg, eap, cctx);
9453}
9454
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009455 static char_u *
9456compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9457{
9458 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009459 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009460
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009461 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009462 {
9463 if (STRNCMP(arg, "END", 3) == 0)
9464 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009465 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009466 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009467 // First load the current variable value.
9468 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9469 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009470 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009471 }
9472
9473 // Gets the redirected text and put it on the stack, then store it
9474 // in the variable.
9475 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9476
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009477 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009478 generate_instr_drop(cctx, ISN_CONCAT, 1);
9479
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009480 if (lhs->lhs_has_index)
9481 {
9482 // Use the info in "lhs" to store the value at the index in the
9483 // list or dict.
9484 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9485 &t_string, cctx) == FAIL)
9486 return NULL;
9487 }
9488 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009489 return NULL;
9490
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009491 VIM_CLEAR(lhs->lhs_name);
9492 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009493 return arg + 3;
9494 }
9495 emsg(_(e_cannot_nest_redir));
9496 return NULL;
9497 }
9498
9499 if (arg[0] == '=' && arg[1] == '>')
9500 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009501 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009502
9503 // redirect to a variable is compiled
9504 arg += 2;
9505 if (*arg == '>')
9506 {
9507 ++arg;
9508 append = TRUE;
9509 }
9510 arg = skipwhite(arg);
9511
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009512 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009513 FALSE, FALSE, 1, cctx) == FAIL)
9514 return NULL;
9515 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009516 lhs->lhs_append = append;
9517 if (lhs->lhs_has_index)
9518 {
9519 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9520 if (lhs->lhs_whole == NULL)
9521 return NULL;
9522 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009523
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009524 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009525 }
9526
9527 // other redirects are handled like at script level
9528 return compile_exec(line, eap, cctx);
9529}
9530
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009531#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009532 static char_u *
9533compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9534{
9535 isn_T *isn;
9536 char_u *p;
9537
9538 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9539 if (isn == NULL)
9540 return NULL;
9541 isn->isn_arg.number = eap->cmdidx;
9542
9543 p = eap->arg;
9544 if (compile_expr0(&p, cctx) == FAIL)
9545 return NULL;
9546
9547 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9548 if (isn == NULL)
9549 return NULL;
9550 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9551 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9552 return NULL;
9553 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9554 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9555 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9556
9557 return p;
9558}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009559#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009560
Bram Moolenaar4c137212021-04-19 16:48:48 +02009561/*
Bram Moolenaar7b829262021-10-13 15:04:34 +01009562 * Check if the separator for a :global or :substitute command is OK.
9563 */
9564 int
9565check_global_and_subst(char_u *cmd, char_u *arg)
9566{
Bram Moolenaar7f320922021-10-13 15:28:28 +01009567 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
Bram Moolenaar7b829262021-10-13 15:04:34 +01009568 {
9569 semsg(_(e_separator_not_supported_str), arg);
9570 return FAIL;
9571 }
9572 if (VIM_ISWHITE(cmd[1]))
9573 {
9574 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
9575 return FAIL;
9576 }
9577 return OK;
9578}
9579
9580
9581/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009582 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009583 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009584 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009585 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009586add_def_function(ufunc_T *ufunc)
9587{
9588 dfunc_T *dfunc;
9589
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009590 if (def_functions.ga_len == 0)
9591 {
9592 // The first position is not used, so that a zero uf_dfunc_idx means it
9593 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009594 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009595 return FAIL;
9596 ++def_functions.ga_len;
9597 }
9598
Bram Moolenaar09689a02020-05-09 22:50:08 +02009599 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009600 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009601 return FAIL;
9602 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9603 CLEAR_POINTER(dfunc);
9604 dfunc->df_idx = def_functions.ga_len;
9605 ufunc->uf_dfunc_idx = dfunc->df_idx;
9606 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009607 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009608 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009609 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009610 ++def_functions.ga_len;
9611 return OK;
9612}
9613
9614/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009615 * After ex_function() has collected all the function lines: parse and compile
9616 * the lines into instructions.
9617 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009618 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9619 * the return statement (used for lambda). When uf_ret_type is already set
9620 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009621 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009622 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009623 * This can be used recursively through compile_lambda(), which may reallocate
9624 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009625 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009626 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009627 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009628compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009629 ufunc_T *ufunc,
9630 int check_return_type,
9631 compiletype_T compile_type,
9632 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009633{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009634 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009635 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009636 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009637 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009638 cctx_T cctx;
9639 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009640 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009641 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009642 int ret = FAIL;
9643 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009644 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009645 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009646 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009647 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009648#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009649 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009650#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009651 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009652
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009653 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009654 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009655 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009656 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009657 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9658 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009659 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009660
9661 switch (compile_type)
9662 {
9663 case CT_PROFILE:
9664#ifdef FEAT_PROFILE
9665 instr_dest = dfunc->df_instr_prof; break;
9666#endif
9667 case CT_NONE: instr_dest = dfunc->df_instr; break;
9668 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9669 }
9670 if (instr_dest != NULL)
9671 // Was compiled in this mode before: Free old instructions.
9672 delete_def_function_contents(dfunc, FALSE);
9673 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009674 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009675 else
9676 {
9677 if (add_def_function(ufunc) == FAIL)
9678 return FAIL;
9679 new_def_function = TRUE;
9680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009681
Bram Moolenaar985116a2020-07-12 17:31:09 +02009682 ufunc->uf_def_status = UF_COMPILING;
9683
Bram Moolenaara80faa82020-04-12 19:37:17 +02009684 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009685
Bram Moolenaare99d4222021-06-13 14:01:26 +02009686 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009687 cctx.ctx_ufunc = ufunc;
9688 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009689 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009690 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9691 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9692 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9693 cctx.ctx_type_list = &ufunc->uf_type_list;
9694 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9695 instr = &cctx.ctx_instr;
9696
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009697 // Set the context to the function, it may be compiled when called from
9698 // another script. Set the script version to the most modern one.
9699 // The line number will be set in next_line_from_context().
9700 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009701 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9702
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009703 // Don't use the flag from ":legacy" here.
9704 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9705
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009706 // Make sure error messages are OK.
9707 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9708 if (do_estack_push)
9709 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009710 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009711
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009712 if (ufunc->uf_def_args.ga_len > 0)
9713 {
9714 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009715 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009716 int i;
9717 char_u *arg;
9718 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009719 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009720
9721 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009722 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009723 for (i = 0; i < count; ++i)
9724 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009725 garray_T *stack = &cctx.ctx_type_stack;
9726 type_T *val_type;
9727 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009728 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009729 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009730 int jump_instr_idx = instr->ga_len;
9731 isn_T *isn;
9732
9733 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9734 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9735 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009736
9737 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009738 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009739
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009740 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009741 r = compile_expr0(&arg, &cctx);
9742
Bram Moolenaar12bce952021-03-11 20:04:04 +01009743 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009744 goto erret;
9745
9746 // If no type specified use the type of the default value.
9747 // Otherwise check that the default value type matches the
9748 // specified type.
9749 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009750 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009751 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009752 {
9753 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009754 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009755 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009756 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009757 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009758 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009759
9760 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009761 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009762
9763 // set instruction index in JUMP_IF_ARG_SET to here
9764 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9765 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009766 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009767
9768 if (did_set_arg_type)
9769 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009770 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009771 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009772
9773 /*
9774 * Loop over all the lines of the function and generate instructions.
9775 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009776 for (;;)
9777 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009778 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009779 int starts_with_colon = FALSE;
9780 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009781 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009782
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009783 // Bail out on the first error to avoid a flood of errors and report
9784 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009785 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009786 goto erret;
9787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009788 if (line != NULL && *line == '|')
9789 // the line continues after a '|'
9790 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009791 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009792 && !(*line == '#' && (line == cctx.ctx_line_start
9793 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009794 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009795 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009796 goto erret;
9797 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009798 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9799 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009800 else
9801 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009802 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009803 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009804 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009805 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009806#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009807 if (cctx.ctx_skip != SKIP_YES)
9808 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009809#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009810 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009811 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009812 // Make a copy, splitting off nextcmd and removing trailing spaces
9813 // may change it.
9814 if (line != NULL)
9815 {
9816 line = vim_strsave(line);
9817 vim_free(line_to_free);
9818 line_to_free = line;
9819 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009820 }
9821
Bram Moolenaara80faa82020-04-12 19:37:17 +02009822 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009823 ea.cmdlinep = &line;
9824 ea.cmd = skipwhite(line);
9825
Bram Moolenaarb2049902021-01-24 12:53:53 +01009826 if (*ea.cmd == '#')
9827 {
9828 // "#" starts a comment
9829 line = (char_u *)"";
9830 continue;
9831 }
9832
Bram Moolenaarf002a412021-01-24 13:34:18 +01009833#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009834 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9835 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009836 {
9837 may_generate_prof_end(&cctx, prof_lnum);
9838
9839 prof_lnum = cctx.ctx_lnum;
9840 generate_instr(&cctx, ISN_PROF_START);
9841 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009842#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009843 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9844 && cctx.ctx_skip != SKIP_YES)
9845 {
9846 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009847 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009848 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009849 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009850
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009851 // Some things can be recognized by the first character.
9852 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009853 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009854 case '}':
9855 {
9856 // "}" ends a block scope
9857 scopetype_T stype = cctx.ctx_scope == NULL
9858 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009859
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009860 if (stype == BLOCK_SCOPE)
9861 {
9862 compile_endblock(&cctx);
9863 line = ea.cmd;
9864 }
9865 else
9866 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009867 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009868 goto erret;
9869 }
9870 if (line != NULL)
9871 line = skipwhite(ea.cmd + 1);
9872 continue;
9873 }
9874
9875 case '{':
9876 // "{" starts a block scope
9877 // "{'a': 1}->func() is something else
9878 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9879 {
9880 line = compile_block(ea.cmd, &cctx);
9881 continue;
9882 }
9883 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009884 }
9885
9886 /*
9887 * COMMAND MODIFIERS
9888 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009889 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009890 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9891 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009892 {
9893 if (errormsg != NULL)
9894 goto erret;
9895 // empty line or comment
9896 line = (char_u *)"";
9897 continue;
9898 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009899 generate_cmdmods(&cctx, &local_cmdmod);
9900 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009901
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009902 // Check if there was a colon after the last command modifier or before
9903 // the current position.
9904 for (p = ea.cmd; p >= line; --p)
9905 {
9906 if (*p == ':')
9907 starts_with_colon = TRUE;
9908 if (p < ea.cmd && !VIM_ISWHITE(*p))
9909 break;
9910 }
9911
Bram Moolenaarce024c32021-06-26 13:00:49 +02009912 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009913 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009914 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009915 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009916 if (checkforcmd(&ea.cmd, "call", 3))
9917 {
9918 if (*ea.cmd == '(')
9919 // not for "call()"
9920 ea.cmd = p;
9921 else
9922 ea.cmd = skipwhite(ea.cmd);
9923 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009924
Bram Moolenaarce024c32021-06-26 13:00:49 +02009925 if (!starts_with_colon)
9926 {
9927 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009928
Bram Moolenaarce024c32021-06-26 13:00:49 +02009929 // Check for assignment after command modifiers.
9930 assign = may_compile_assignment(&ea, &line, &cctx);
9931 if (assign == OK)
9932 goto nextline;
9933 if (assign == FAIL)
9934 goto erret;
9935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009936 }
9937
9938 /*
9939 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009940 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009941 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009942 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009943 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009944 cmd = ea.cmd;
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009945 if ((*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009946 && (starts_with_colon || !(*cmd == '\''
9947 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009948 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009949 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009950 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009951 {
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009952 if (!starts_with_colon
9953 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009954 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009955 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009956 goto erret;
9957 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009958 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009959 if (ends_excmd2(line, ea.cmd))
9960 {
9961 // A range without a command: jump to the line.
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009962 generate_EXEC(&cctx, ISN_EXECRANGE,
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009963 vim_strnsave(cmd, ea.cmd - cmd));
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009964 line = ea.cmd;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009965 goto nextline;
9966 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009967 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009968 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009969 p = find_ex_command(&ea, NULL,
9970 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009971 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009972
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009973 if (p == NULL)
9974 {
9975 if (cctx.ctx_skip != SKIP_YES)
9976 emsg(_(e_ambiguous_use_of_user_defined_command));
9977 goto erret;
9978 }
9979
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009980 // When using ":legacy cmd" always use compile_exec().
9981 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009982 {
9983 char_u *start = ea.cmd;
9984
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009985 switch (ea.cmdidx)
9986 {
9987 case CMD_if:
9988 case CMD_elseif:
9989 case CMD_else:
9990 case CMD_endif:
9991 case CMD_for:
9992 case CMD_endfor:
9993 case CMD_continue:
9994 case CMD_break:
9995 case CMD_while:
9996 case CMD_endwhile:
9997 case CMD_try:
9998 case CMD_catch:
9999 case CMD_finally:
10000 case CMD_endtry:
10001 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
10002 goto erret;
10003 default: break;
10004 }
10005
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010006 // ":legacy return expr" needs to be handled differently.
10007 if (checkforcmd(&start, "return", 4))
10008 ea.cmdidx = CMD_return;
10009 else
10010 ea.cmdidx = CMD_legacy;
10011 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +020010012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010013 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
10014 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +020010015 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010016 {
10017 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +010010018 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010019 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +020010020 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010021 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +010010022 // CMD_var cannot happen, compile_assignment() above would be
10023 // used. Most likely an assignment to a non-existing variable.
10024 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010025 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010026 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010027 }
10028
Bram Moolenaar3988f642020-08-27 22:43:03 +020010029 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010030 && ea.cmdidx != CMD_elseif
10031 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +020010032 && ea.cmdidx != CMD_endif
10033 && ea.cmdidx != CMD_endfor
10034 && ea.cmdidx != CMD_endwhile
10035 && ea.cmdidx != CMD_catch
10036 && ea.cmdidx != CMD_finally
10037 && ea.cmdidx != CMD_endtry)
10038 {
Bram Moolenaar3988f642020-08-27 22:43:03 +020010039 emsg(_(e_unreachable_code_after_return));
10040 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +020010041 }
10042
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010043 p = skipwhite(p);
10044 if (ea.cmdidx != CMD_SIZE
10045 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
10046 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +020010047 if (ea.cmdidx >= 0)
10048 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010049 if ((ea.argt & EX_BANG) && *p == '!')
10050 {
10051 ea.forceit = TRUE;
10052 p = skipwhite(p + 1);
10053 }
10054 }
10055
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010056 switch (ea.cmdidx)
10057 {
10058 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +000010059 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +020010060 ea.arg = p;
10061 line = compile_nested_function(&ea, &cctx);
10062 break;
10063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010064 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010065 line = compile_return(p, check_return_type,
10066 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010067 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010068 break;
10069
10070 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +020010071 emsg(_(e_cannot_use_let_in_vim9_script));
10072 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +020010073 case CMD_var:
10074 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010075 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +020010076 case CMD_increment:
10077 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010078 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +020010079 if (line == p)
10080 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010081 break;
10082
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010083 case CMD_unlet:
10084 case CMD_unlockvar:
10085 case CMD_lockvar:
10086 line = compile_unletlock(p, &ea, &cctx);
10087 break;
10088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010089 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +020010090 emsg(_(e_import_can_only_be_used_in_script));
10091 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010092 break;
10093
10094 case CMD_if:
10095 line = compile_if(p, &cctx);
10096 break;
10097 case CMD_elseif:
10098 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010099 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010100 break;
10101 case CMD_else:
10102 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010103 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010104 break;
10105 case CMD_endif:
10106 line = compile_endif(p, &cctx);
10107 break;
10108
10109 case CMD_while:
10110 line = compile_while(p, &cctx);
10111 break;
10112 case CMD_endwhile:
10113 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010114 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010115 break;
10116
10117 case CMD_for:
10118 line = compile_for(p, &cctx);
10119 break;
10120 case CMD_endfor:
10121 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010122 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010123 break;
10124 case CMD_continue:
10125 line = compile_continue(p, &cctx);
10126 break;
10127 case CMD_break:
10128 line = compile_break(p, &cctx);
10129 break;
10130
10131 case CMD_try:
10132 line = compile_try(p, &cctx);
10133 break;
10134 case CMD_catch:
10135 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010136 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010137 break;
10138 case CMD_finally:
10139 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010140 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010141 break;
10142 case CMD_endtry:
10143 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010144 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010145 break;
10146 case CMD_throw:
10147 line = compile_throw(p, &cctx);
10148 break;
10149
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010150 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +020010151 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010152 break;
10153
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010154 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010155 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +010010156 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010157 case CMD_echomsg:
10158 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010159 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010160 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +010010161 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010162
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010163 case CMD_put:
10164 ea.cmd = cmd;
10165 line = compile_put(p, &ea, &cctx);
10166 break;
10167
Bram Moolenaar4c137212021-04-19 16:48:48 +020010168 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +010010169 if (check_global_and_subst(ea.cmd, p) == FAIL)
10170 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +020010171 if (cctx.ctx_skip == SKIP_YES)
10172 line = (char_u *)"";
10173 else
10174 {
10175 ea.arg = p;
10176 line = compile_substitute(line, &ea, &cctx);
10177 }
10178 break;
10179
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010180 case CMD_redir:
10181 ea.arg = p;
10182 line = compile_redir(line, &ea, &cctx);
10183 break;
10184
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010185 case CMD_cexpr:
10186 case CMD_lexpr:
10187 case CMD_caddexpr:
10188 case CMD_laddexpr:
10189 case CMD_cgetexpr:
10190 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010191#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010192 ea.arg = p;
10193 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010194#else
10195 ex_ni(&ea);
10196 line = NULL;
10197#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010198 break;
10199
Bram Moolenaarae616492020-07-28 20:07:27 +020010200 case CMD_append:
10201 case CMD_change:
10202 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010203 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010204 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010205 case CMD_xit:
10206 not_in_vim9(&ea);
10207 goto erret;
10208
Bram Moolenaar002262f2020-07-08 17:47:57 +020010209 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010210 if (cctx.ctx_skip != SKIP_YES)
10211 {
10212 semsg(_(e_invalid_command_str), ea.cmd);
10213 goto erret;
10214 }
10215 // We don't check for a next command here.
10216 line = (char_u *)"";
10217 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010218
Bram Moolenaar20677332021-06-06 17:02:53 +020010219 case CMD_lua:
10220 case CMD_mzscheme:
10221 case CMD_perl:
10222 case CMD_py3:
10223 case CMD_python3:
10224 case CMD_python:
10225 case CMD_pythonx:
10226 case CMD_ruby:
10227 case CMD_tcl:
10228 ea.arg = p;
10229 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010230 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010231 else
10232 // heredoc lines have been concatenated with NL
10233 // characters in get_function_body()
10234 line = compile_script(line, &cctx);
10235 break;
10236
Bram Moolenaar7b829262021-10-13 15:04:34 +010010237 case CMD_global:
10238 if (check_global_and_subst(ea.cmd, p) == FAIL)
10239 goto erret;
10240 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +020010241 default:
10242 // Not recognized, execute with do_cmdline_cmd().
10243 ea.arg = p;
10244 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010245 break;
10246 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010247nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010248 if (line == NULL)
10249 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010250 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010251
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010252 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010253 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010254
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010255 if (cctx.ctx_type_stack.ga_len < 0)
10256 {
10257 iemsg("Type stack underflow");
10258 goto erret;
10259 }
10260 }
10261
10262 if (cctx.ctx_scope != NULL)
10263 {
10264 if (cctx.ctx_scope->se_type == IF_SCOPE)
10265 emsg(_(e_endif));
10266 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10267 emsg(_(e_endwhile));
10268 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10269 emsg(_(e_endfor));
10270 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010271 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010272 goto erret;
10273 }
10274
Bram Moolenaarefd88552020-06-18 20:50:10 +020010275 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010276 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010277 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10278 ufunc->uf_ret_type = &t_void;
10279 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010280 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010281 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010282 goto erret;
10283 }
10284
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010285 // Return void if there is no return at the end.
10286 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010287 }
10288
Bram Moolenaar599410c2021-04-10 14:03:43 +020010289 // When compiled with ":silent!" and there was an error don't consider the
10290 // function compiled.
10291 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010292 {
10293 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10294 + ufunc->uf_dfunc_idx;
10295 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010296 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010297#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010298 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010299 {
10300 dfunc->df_instr_prof = instr->ga_data;
10301 dfunc->df_instr_prof_count = instr->ga_len;
10302 }
10303 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010304#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010305 if (cctx.ctx_compile_type == CT_DEBUG)
10306 {
10307 dfunc->df_instr_debug = instr->ga_data;
10308 dfunc->df_instr_debug_count = instr->ga_len;
10309 }
10310 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010311 {
10312 dfunc->df_instr = instr->ga_data;
10313 dfunc->df_instr_count = instr->ga_len;
10314 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010315 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010316 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010317 if (cctx.ctx_outer_used)
10318 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010319 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010321
10322 ret = OK;
10323
10324erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010325 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010326 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010327 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10328 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010329
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010330 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010331 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010332 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010333 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010334
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010335 // If using the last entry in the table and it was added above, we
10336 // might as well remove it.
10337 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010338 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010339 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010340 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010341 ufunc->uf_dfunc_idx = 0;
10342 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010343 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010344
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010345 while (cctx.ctx_scope != NULL)
10346 drop_scope(&cctx);
10347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010348 if (errormsg != NULL)
10349 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010350 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010351 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010352 }
10353
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010354 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10355 {
10356 if (ret == OK)
10357 {
10358 emsg(_(e_missing_redir_end));
10359 ret = FAIL;
10360 }
10361 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010362 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010363 }
10364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010365 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010366 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010367 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010368 if (do_estack_push)
10369 estack_pop();
10370
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010371 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010372 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010373 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010374 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010375 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010376}
10377
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010378 void
10379set_function_type(ufunc_T *ufunc)
10380{
10381 int varargs = ufunc->uf_va_name != NULL;
10382 int argcount = ufunc->uf_args.ga_len;
10383
10384 // Create a type for the function, with the return type and any
10385 // argument types.
10386 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10387 // The type is included in "tt_args".
10388 if (argcount > 0 || varargs)
10389 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010390 if (ufunc->uf_type_list.ga_itemsize == 0)
10391 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010392 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10393 argcount, &ufunc->uf_type_list);
10394 // Add argument types to the function type.
10395 if (func_type_add_arg_types(ufunc->uf_func_type,
10396 argcount + varargs,
10397 &ufunc->uf_type_list) == FAIL)
10398 return;
10399 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10400 ufunc->uf_func_type->tt_min_argcount =
10401 argcount - ufunc->uf_def_args.ga_len;
10402 if (ufunc->uf_arg_types == NULL)
10403 {
10404 int i;
10405
10406 // lambda does not have argument types.
10407 for (i = 0; i < argcount; ++i)
10408 ufunc->uf_func_type->tt_args[i] = &t_any;
10409 }
10410 else
10411 mch_memmove(ufunc->uf_func_type->tt_args,
10412 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10413 if (varargs)
10414 {
10415 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010416 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010417 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10418 }
10419 }
10420 else
10421 // No arguments, can use a predefined type.
10422 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10423 argcount, &ufunc->uf_type_list);
10424}
10425
10426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010427/*
10428 * Delete an instruction, free what it contains.
10429 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010430 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010431delete_instr(isn_T *isn)
10432{
10433 switch (isn->isn_type)
10434 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010435 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010436 case ISN_EXEC:
Bram Moolenaare4eed8c2021-12-01 15:22:56 +000010437 case ISN_EXECRANGE:
Bram Moolenaar20677332021-06-06 17:02:53 +020010438 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010439 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010440 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010441 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010442 case ISN_LOADENV:
10443 case ISN_LOADG:
10444 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010445 case ISN_LOADT:
10446 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010447 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010448 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010449 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010450 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010451 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010452 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010453 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010454 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010455 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010456 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010457 case ISN_STOREW:
10458 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010459 vim_free(isn->isn_arg.string);
10460 break;
10461
Bram Moolenaar4c137212021-04-19 16:48:48 +020010462 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010463 {
10464 int idx;
10465 isn_T *list = isn->isn_arg.subs.subs_instr;
10466
10467 vim_free(isn->isn_arg.subs.subs_cmd);
10468 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10469 delete_instr(list + idx);
10470 vim_free(list);
10471 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010472 break;
10473
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010474 case ISN_INSTR:
10475 {
10476 int idx;
10477 isn_T *list = isn->isn_arg.instr;
10478
10479 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10480 delete_instr(list + idx);
10481 vim_free(list);
10482 }
10483 break;
10484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010485 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010486 case ISN_STORES:
10487 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010488 break;
10489
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010490 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010491 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010492 vim_free(isn->isn_arg.unlet.ul_name);
10493 break;
10494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010495 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +000010496 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010497 vim_free(isn->isn_arg.storeopt.so_name);
10498 break;
10499
10500 case ISN_PUSHBLOB: // push blob isn_arg.blob
10501 blob_unref(isn->isn_arg.blob);
10502 break;
10503
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010504 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010505#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010506 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010507#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010508 break;
10509
10510 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010511#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010512 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010513#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010514 break;
10515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010516 case ISN_UCALL:
10517 vim_free(isn->isn_arg.ufunc.cuf_name);
10518 break;
10519
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010520 case ISN_FUNCREF:
10521 {
Bram Moolenaar38453522021-11-28 22:00:12 +000010522 if (isn->isn_arg.funcref.fr_func_name == NULL)
10523 {
10524 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10525 + isn->isn_arg.funcref.fr_dfunc_idx;
10526 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010527
Bram Moolenaar38453522021-11-28 22:00:12 +000010528 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10529 func_ptr_unref(ufunc);
10530 }
10531 else
10532 {
10533 char_u *name = isn->isn_arg.funcref.fr_func_name;
10534
10535 if (name != NULL)
10536 func_unref(name);
10537 vim_free(isn->isn_arg.funcref.fr_func_name);
10538 }
Bram Moolenaara05e5242020-09-19 18:19:19 +020010539 }
10540 break;
10541
10542 case ISN_DCALL:
10543 {
10544 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10545 + isn->isn_arg.dfunc.cdf_idx;
10546
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010547 if (dfunc->df_ufunc != NULL
10548 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010549 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010550 }
10551 break;
10552
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010553 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010554 {
10555 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10556 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10557
10558 if (ufunc != NULL)
10559 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010560 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010561 func_ptr_unref(ufunc);
10562 }
10563
10564 vim_free(lambda);
10565 vim_free(isn->isn_arg.newfunc.nf_global);
10566 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010567 break;
10568
Bram Moolenaar5e654232020-09-16 15:22:00 +020010569 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010570 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010571 free_type(isn->isn_arg.type.ct_type);
10572 break;
10573
Bram Moolenaar02194d22020-10-24 23:08:38 +020010574 case ISN_CMDMOD:
10575 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10576 ->cmod_filter_regmatch.regprog);
10577 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10578 break;
10579
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010580 case ISN_LOADSCRIPT:
10581 case ISN_STORESCRIPT:
10582 vim_free(isn->isn_arg.script.scriptref);
10583 break;
10584
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010585 case ISN_TRY:
10586 vim_free(isn->isn_arg.try.try_ref);
10587 break;
10588
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010589 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010590 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010591 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10592 break;
10593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010594 case ISN_2BOOL:
10595 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010596 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010597 case ISN_ADDBLOB:
10598 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010599 case ISN_ANYINDEX:
10600 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010601 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010602 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010603 case ISN_BLOBINDEX:
10604 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010605 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010606 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010607 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010608 case ISN_CHECKNR:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010609 case ISN_CLEARDICT:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010610 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010611 case ISN_COMPAREANY:
10612 case ISN_COMPAREBLOB:
10613 case ISN_COMPAREBOOL:
10614 case ISN_COMPAREDICT:
10615 case ISN_COMPAREFLOAT:
10616 case ISN_COMPAREFUNC:
10617 case ISN_COMPARELIST:
10618 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010619 case ISN_COMPARESPECIAL:
10620 case ISN_COMPARESTRING:
10621 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010622 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010623 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010624 case ISN_DROP:
10625 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010626 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010627 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010628 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010629 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010630 case ISN_EXECCONCAT:
10631 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010632 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010633 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010634 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010635 case ISN_GETITEM:
10636 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010637 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010638 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010639 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010640 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010641 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010642 case ISN_LOADBDICT:
10643 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010644 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010645 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010646 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010647 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010648 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010649 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010650 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010651 case ISN_NEGATENR:
10652 case ISN_NEWDICT:
10653 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010654 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010655 case ISN_OPFLOAT:
10656 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010657 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010658 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010659 case ISN_PROF_END:
10660 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010661 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010662 case ISN_PUSHF:
10663 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010664 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010665 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010666 case ISN_REDIREND:
10667 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010668 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010669 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010670 case ISN_SHUFFLE:
10671 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010672 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010673 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010674 case ISN_STORENR:
10675 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010676 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010677 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010678 case ISN_STOREV:
10679 case ISN_STRINDEX:
10680 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010681 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010682 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010683 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010684 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010685 case ISN_UNPACK:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010686 case ISN_USEDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010687 // nothing allocated
10688 break;
10689 }
10690}
10691
10692/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010693 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010694 */
10695 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010696delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010697{
10698 int idx;
10699
10700 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010701 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010702
10703 if (dfunc->df_instr != NULL)
10704 {
10705 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10706 delete_instr(dfunc->df_instr + idx);
10707 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010708 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010709 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010710 if (dfunc->df_instr_debug != NULL)
10711 {
10712 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10713 delete_instr(dfunc->df_instr_debug + idx);
10714 VIM_CLEAR(dfunc->df_instr_debug);
10715 dfunc->df_instr_debug = NULL;
10716 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010717#ifdef FEAT_PROFILE
10718 if (dfunc->df_instr_prof != NULL)
10719 {
10720 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10721 delete_instr(dfunc->df_instr_prof + idx);
10722 VIM_CLEAR(dfunc->df_instr_prof);
10723 dfunc->df_instr_prof = NULL;
10724 }
10725#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010726
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010727 if (mark_deleted)
10728 dfunc->df_deleted = TRUE;
10729 if (dfunc->df_ufunc != NULL)
10730 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010731}
10732
10733/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010734 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010735 * function, unless another user function still uses it.
10736 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010737 */
10738 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010739unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010740{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010741 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010742 {
10743 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10744 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010745
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010746 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010747 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010748 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010749 ufunc->uf_dfunc_idx = 0;
10750 if (dfunc->df_ufunc == ufunc)
10751 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010752 }
10753}
10754
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010755/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010756 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010757 */
10758 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010759link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010760{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010761 if (ufunc->uf_dfunc_idx > 0)
10762 {
10763 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10764 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010765
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010766 ++dfunc->df_refcount;
10767 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010768}
10769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010770#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010771/*
10772 * Free all functions defined with ":def".
10773 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010774 void
10775free_def_functions(void)
10776{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010777 int idx;
10778
10779 for (idx = 0; idx < def_functions.ga_len; ++idx)
10780 {
10781 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10782
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010783 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010784 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010785 }
10786
10787 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010788}
10789#endif
10790
10791
10792#endif // FEAT_EVAL