blob: 50dc7560521b405ced9a7c78e1ffe5575ed88ccf [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 Moolenaarb185a402020-09-18 22:42:00 +02002011 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 return FAIL;
2013 }
2014 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
2015 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02002016 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 return FAIL;
2018 }
2019
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002020 if (ufunc->uf_def_status != UF_NOT_COMPILED
2021 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002022 {
2023 int i;
2024
2025 for (i = 0; i < argcount; ++i)
2026 {
2027 type_T *expected;
2028 type_T *actual;
2029
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002030 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
2031 if (actual == &t_special
2032 && i >= regular_args - ufunc->uf_def_args.ga_len)
2033 {
2034 // assume v:none used for default argument value
2035 continue;
2036 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002037 if (i < regular_args)
2038 {
2039 if (ufunc->uf_arg_types == NULL)
2040 continue;
2041 expected = ufunc->uf_arg_types[i];
2042 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02002043 else if (ufunc->uf_va_type == NULL
2044 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02002045 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02002046 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002047 else
2048 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01002049 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002050 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002051 {
2052 arg_type_mismatch(expected, actual, i + 1);
2053 return FAIL;
2054 }
2055 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002056 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002057 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002058 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002059 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002060 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002061 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2062 {
2063 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2064 ufunc->uf_name);
2065 return FAIL;
2066 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002069 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002070 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002072 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 {
2074 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2075 isn->isn_arg.dfunc.cdf_argcount = argcount;
2076 }
2077 else
2078 {
2079 // A user function may be deleted and redefined later, can't use the
2080 // ufunc pointer, need to look it up again at runtime.
2081 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2082 isn->isn_arg.ufunc.cuf_argcount = argcount;
2083 }
2084
2085 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002086 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 return FAIL;
2088 // add return value
2089 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2090 ++stack->ga_len;
2091
2092 return OK;
2093}
2094
2095/*
2096 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2097 */
2098 static int
2099generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2100{
2101 isn_T *isn;
2102 garray_T *stack = &cctx->ctx_type_stack;
2103
Bram Moolenaar080457c2020-03-03 21:53:32 +01002104 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2106 return FAIL;
2107 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2108 isn->isn_arg.ufunc.cuf_argcount = argcount;
2109
2110 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002111 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002112 return FAIL;
2113 // add return value
2114 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2115 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116
2117 return OK;
2118}
2119
2120/*
2121 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002122 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 */
2124 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002125generate_PCALL(
2126 cctx_T *cctx,
2127 int argcount,
2128 char_u *name,
2129 type_T *type,
2130 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131{
2132 isn_T *isn;
2133 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002134 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135
Bram Moolenaar080457c2020-03-03 21:53:32 +01002136 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002137
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002138 if (type->tt_type == VAR_ANY)
2139 ret_type = &t_any;
2140 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002141 {
2142 if (type->tt_argcount != -1)
2143 {
2144 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2145
2146 if (argcount < type->tt_min_argcount - varargs)
2147 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002148 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002149 return FAIL;
2150 }
2151 if (!varargs && argcount > type->tt_argcount)
2152 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002153 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002154 return FAIL;
2155 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002156 if (type->tt_args != NULL)
2157 {
2158 int i;
2159
2160 for (i = 0; i < argcount; ++i)
2161 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002162 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002163 type_T *actual = ((type_T **)stack->ga_data)[
2164 stack->ga_len + offset];
2165 type_T *expected;
2166
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002167 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002168 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002169 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002170 else if (i >= type->tt_min_argcount
2171 && actual == &t_special)
2172 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002173 else
2174 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002175 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002176 cctx, TRUE, FALSE) == FAIL)
2177 {
2178 arg_type_mismatch(expected, actual, i + 1);
2179 return FAIL;
2180 }
2181 }
2182 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002183 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002184 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002185 if (ret_type == &t_unknown)
2186 // return type not known yet, use a runtime check
2187 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002188 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002189 else
2190 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002191 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002192 return FAIL;
2193 }
2194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2196 return FAIL;
2197 isn->isn_arg.pfunc.cpf_top = at_top;
2198 isn->isn_arg.pfunc.cpf_argcount = argcount;
2199
2200 stack->ga_len -= argcount; // drop the arguments
2201
2202 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002203 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002205 // If partial is above the arguments it must be cleared and replaced with
2206 // the return value.
2207 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2208 return FAIL;
2209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 return OK;
2211}
2212
2213/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002214 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 */
2216 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002217generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218{
2219 isn_T *isn;
2220 garray_T *stack = &cctx->ctx_type_stack;
2221 type_T *type;
2222
Bram Moolenaar080457c2020-03-03 21:53:32 +01002223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002224 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002226 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002228 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002230 if (type->tt_type != VAR_DICT && type != &t_any)
2231 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002232 char *tofree;
2233
2234 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2235 name, type_name(type, &tofree));
2236 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002237 return FAIL;
2238 }
2239 // change dict type to dict member type
2240 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002241 {
2242 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2243 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245
2246 return OK;
2247}
2248
2249/*
2250 * Generate an ISN_ECHO instruction.
2251 */
2252 static int
2253generate_ECHO(cctx_T *cctx, int with_white, int count)
2254{
2255 isn_T *isn;
2256
Bram Moolenaar080457c2020-03-03 21:53:32 +01002257 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2259 return FAIL;
2260 isn->isn_arg.echo.echo_with_white = with_white;
2261 isn->isn_arg.echo.echo_count = count;
2262
2263 return OK;
2264}
2265
Bram Moolenaarad39c092020-02-26 18:23:43 +01002266/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002267 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002268 */
2269 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002270generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002271{
2272 isn_T *isn;
2273
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002274 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002275 return FAIL;
2276 isn->isn_arg.number = count;
2277
2278 return OK;
2279}
2280
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002281/*
2282 * Generate an ISN_PUT instruction.
2283 */
2284 static int
2285generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2286{
2287 isn_T *isn;
2288
2289 RETURN_OK_IF_SKIP(cctx);
2290 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2291 return FAIL;
2292 isn->isn_arg.put.put_regname = regname;
2293 isn->isn_arg.put.put_lnum = lnum;
2294 return OK;
2295}
2296
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002297/*
2298 * Generate an EXEC instruction that takes a string argument.
2299 * A copy is made of "line".
2300 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 static int
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002302generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303{
2304 isn_T *isn;
2305
Bram Moolenaar080457c2020-03-03 21:53:32 +01002306 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002307 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 return FAIL;
2309 isn->isn_arg.string = vim_strsave(line);
2310 return OK;
2311}
2312
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002313/*
2314 * Generate an EXEC instruction that takes a string argument.
2315 * "str" must be allocated, it is consumed.
2316 */
2317 static int
2318generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2319{
2320 isn_T *isn;
2321
2322 if (cctx->ctx_skip == SKIP_YES)
2323 {
2324 vim_free(str);
2325 return OK;
2326 }
2327 if ((isn = generate_instr(cctx, isntype)) == NULL)
2328 {
2329 vim_free(str);
2330 return FAIL;
2331 }
2332 isn->isn_arg.string = str;
2333 return OK;
2334}
2335
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002336 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002337generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2338{
2339 isn_T *isn;
2340 garray_T *stack = &cctx->ctx_type_stack;
2341
2342 RETURN_OK_IF_SKIP(cctx);
2343 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2344 return FAIL;
2345 isn->isn_arg.string = vim_strsave(line);
2346
Bram Moolenaar35578162021-08-02 19:10:38 +02002347 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002348 return FAIL;
2349 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2350 ++stack->ga_len;
2351
2352 return OK;
2353}
2354
2355 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002356generate_EXECCONCAT(cctx_T *cctx, int count)
2357{
2358 isn_T *isn;
2359
2360 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2361 return FAIL;
2362 isn->isn_arg.number = count;
2363 return OK;
2364}
2365
Bram Moolenaar08597872020-12-10 19:43:40 +01002366/*
2367 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2368 */
2369 static int
2370generate_RANGE(cctx_T *cctx, char_u *range)
2371{
2372 isn_T *isn;
2373 garray_T *stack = &cctx->ctx_type_stack;
2374
2375 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2376 return FAIL;
2377 isn->isn_arg.string = range;
2378
Bram Moolenaar35578162021-08-02 19:10:38 +02002379 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002380 return FAIL;
2381 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2382 ++stack->ga_len;
2383 return OK;
2384}
2385
Bram Moolenaar792f7862020-11-23 08:31:18 +01002386 static int
2387generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2388{
2389 isn_T *isn;
2390
2391 RETURN_OK_IF_SKIP(cctx);
2392 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2393 return FAIL;
2394 isn->isn_arg.unpack.unp_count = var_count;
2395 isn->isn_arg.unpack.unp_semicolon = semicolon;
2396 return OK;
2397}
2398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002400 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002401 */
2402 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002403generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002404{
2405 isn_T *isn;
2406
Bram Moolenaar917c46a2021-08-10 19:53:01 +02002407 if (has_cmdmod(cmod, FALSE))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002408 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002409 cctx->ctx_has_cmdmod = TRUE;
2410
2411 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002412 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002413 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2414 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2415 return FAIL;
2416 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002417 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002418 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002419 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002420
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002421 return OK;
2422}
2423
2424 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002425generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002426{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002427 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2428 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002429 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002430 return OK;
2431}
2432
Bram Moolenaarfa984412021-03-25 22:15:28 +01002433 static int
2434misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002435{
2436 garray_T *instr = &cctx->ctx_instr;
2437
Bram Moolenaara91a7132021-03-25 21:12:15 +01002438 if (cctx->ctx_has_cmdmod
2439 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2440 == ISN_CMDMOD)
2441 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002442 emsg(_(e_misplaced_command_modifier));
2443 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002444 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002445 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002446}
2447
2448/*
2449 * Get the index of the current instruction.
Bram Moolenaar093165c2021-08-22 13:35:31 +02002450 * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
Bram Moolenaara91a7132021-03-25 21:12:15 +01002451 */
2452 static int
2453current_instr_idx(cctx_T *cctx)
2454{
2455 garray_T *instr = &cctx->ctx_instr;
2456 int idx = instr->ga_len;
2457
Bram Moolenaare99d4222021-06-13 14:01:26 +02002458 while (idx > 0)
2459 {
2460 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002461 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002462 {
2463 --idx;
2464 continue;
2465 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002466#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002467 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2468 {
2469 --idx;
2470 continue;
2471 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002472#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002473 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2474 {
2475 --idx;
2476 continue;
2477 }
2478 break;
2479 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002480 return idx;
2481}
2482
Bram Moolenaarf002a412021-01-24 13:34:18 +01002483#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002484 static void
2485may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2486{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002487 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002488 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002489}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002490#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002491
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002492/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002494 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002496 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002497reserve_local(
2498 cctx_T *cctx,
2499 char_u *name,
2500 size_t len,
2501 int isConst,
2502 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002505 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002507 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002509 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002510 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 }
2512
Bram Moolenaar35578162021-08-02 19:10:38 +02002513 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002514 return NULL;
2515 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002516 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002518 // Every local variable uses the next entry on the stack. We could re-use
2519 // the last ones when leaving a scope, but then variables used in a closure
2520 // might get overwritten. To keep things simple do not re-use stack
2521 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002522 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2523 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002524
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002525 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 lvar->lv_const = isConst;
2527 lvar->lv_type = type;
2528
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002529 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002530 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002531 return NULL;
2532 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2533 vim_strsave(lvar->lv_name);
2534 ++dfunc->df_var_names.ga_len;
2535
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002536 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537}
2538
2539/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002540 * Remove local variables above "new_top".
2541 */
2542 static void
2543unwind_locals(cctx_T *cctx, int new_top)
2544{
2545 if (cctx->ctx_locals.ga_len > new_top)
2546 {
2547 int idx;
2548 lvar_T *lvar;
2549
2550 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2551 {
2552 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2553 vim_free(lvar->lv_name);
2554 }
2555 }
2556 cctx->ctx_locals.ga_len = new_top;
2557}
2558
2559/*
2560 * Free all local variables.
2561 */
2562 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002563free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002564{
2565 unwind_locals(cctx, 0);
2566 ga_clear(&cctx->ctx_locals);
2567}
2568
2569/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002570 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2571 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2572 * error if the variable was defined with :const.
2573 */
2574 static int
2575check_item_writable(svar_T *sv, int check_writable, char_u *name)
2576{
2577 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2578 || (check_writable == ASSIGN_FINAL
2579 && sv->sv_const == ASSIGN_CONST))
2580 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002581 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002582 return FAIL;
2583 }
2584 return OK;
2585}
2586
2587/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002589 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 * Returns the index in "sn_var_vals" if found.
2591 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002592 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 */
2594 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002595get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596{
2597 hashtab_T *ht;
2598 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002599 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002600 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 int idx;
2602
Bram Moolenaare3d46852020-08-29 13:39:17 +02002603 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002605 if (sid == current_sctx.sc_sid)
2606 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002607 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002608
2609 if (sav == NULL)
2610 return -2;
2611 idx = sav->sav_var_vals_idx;
2612 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002613 if (check_item_writable(sv, check_writable, name) == FAIL)
2614 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002615 return idx;
2616 }
2617
2618 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 ht = &SCRIPT_VARS(sid);
2620 di = find_var_in_ht(ht, 0, name, TRUE);
2621 if (di == NULL)
2622 return -2;
2623
2624 // Now find the svar_T index in sn_var_vals.
2625 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2626 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002627 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 if (sv->sv_tv == &di->di_tv)
2629 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002630 if (check_item_writable(sv, check_writable, name) == FAIL)
2631 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 return idx;
2633 }
2634 }
2635 return -1;
2636}
2637
2638/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002639 * Find "name" in imported items of the current script or in "cctx" if not
2640 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 */
2642 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002643find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 int idx;
2646
Bram Moolenaare3d46852020-08-29 13:39:17 +02002647 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002648 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 if (cctx != NULL)
2650 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2651 {
2652 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2653 + idx;
2654
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002655 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2656 : STRLEN(import->imp_name) == len
2657 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 return import;
2659 }
2660
Bram Moolenaarefa94442020-08-08 22:16:00 +02002661 return find_imported_in_script(name, len, current_sctx.sc_sid);
2662}
2663
2664 imported_T *
2665find_imported_in_script(char_u *name, size_t len, int sid)
2666{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002667 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002668 int idx;
2669
Bram Moolenaare3d46852020-08-29 13:39:17 +02002670 if (!SCRIPT_ID_VALID(sid))
2671 return NULL;
2672 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2674 {
2675 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2676
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002677 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2678 : STRLEN(import->imp_name) == len
2679 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 return import;
2681 }
2682 return NULL;
2683}
2684
2685/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002686 * Free all imported variables.
2687 */
2688 static void
2689free_imported(cctx_T *cctx)
2690{
2691 int idx;
2692
2693 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2694 {
2695 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2696
2697 vim_free(import->imp_name);
2698 }
2699 ga_clear(&cctx->ctx_imports);
2700}
2701
2702/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002703 * Return a pointer to the next line that isn't empty or only contains a
2704 * comment. Skips over white space.
2705 * Returns NULL if there is none.
2706 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002707 char_u *
2708peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002709{
2710 int lnum = cctx->ctx_lnum;
2711
2712 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2713 {
2714 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002715 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002716
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002717 // ignore NULLs inserted for continuation lines
2718 if (line != NULL)
2719 {
2720 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002721 if (vim9_bad_comment(p))
2722 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002723 if (*p != NUL && !vim9_comment_start(p))
2724 return p;
2725 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002726 }
2727 return NULL;
2728}
2729
2730/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002731 * Called when checking for a following operator at "arg". When the rest of
2732 * the line is empty or only a comment, peek the next line. If there is a next
2733 * line return a pointer to it and set "nextp".
2734 * Otherwise skip over white space.
2735 */
2736 static char_u *
2737may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2738{
2739 char_u *p = skipwhite(arg);
2740
2741 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002742 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002743 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002744 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002745 if (*nextp != NULL)
2746 return *nextp;
2747 }
2748 return p;
2749}
2750
2751/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002752 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002753 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002754 * Returns NULL when at the end.
2755 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002756 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002757next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002758{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002759 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002760
2761 do
2762 {
2763 ++cctx->ctx_lnum;
2764 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002765 {
2766 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002767 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002768 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002769 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002770 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002771 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002772 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002773 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002774 return line;
2775}
2776
2777/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002778 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002779 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002780 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002781 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2782 */
2783 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002784may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002785{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002786 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002787 if (vim9_bad_comment(*arg))
2788 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002789 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002790 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002791 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002792
2793 if (next == NULL)
2794 return FAIL;
2795 *arg = skipwhite(next);
2796 }
2797 return OK;
2798}
2799
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002800/*
2801 * Idem, and give an error when failed.
2802 */
2803 static int
2804may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2805{
2806 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2807 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002808 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002809 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002810 return FAIL;
2811 }
2812 return OK;
2813}
2814
2815
Bram Moolenaara5565e42020-05-09 15:44:01 +02002816// Structure passed between the compile_expr* functions to keep track of
2817// constants that have been parsed but for which no code was produced yet. If
2818// possible expressions on these constants are applied at compile time. If
2819// that is not possible, the code to push the constants needs to be generated
2820// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002821// Using 50 should be more than enough of 5 levels of ().
2822#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002823typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002824 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002825 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002826 int pp_is_const; // all generated code was constants, used for a
2827 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002828} ppconst_T;
2829
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002830static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002831static int compile_expr0(char_u **arg, cctx_T *cctx);
2832static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2833
Bram Moolenaara5565e42020-05-09 15:44:01 +02002834/*
2835 * Generate a PUSH instruction for "tv".
2836 * "tv" will be consumed or cleared.
2837 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2838 */
2839 static int
2840generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2841{
2842 if (tv != NULL)
2843 {
2844 switch (tv->v_type)
2845 {
2846 case VAR_UNKNOWN:
2847 break;
2848 case VAR_BOOL:
2849 generate_PUSHBOOL(cctx, tv->vval.v_number);
2850 break;
2851 case VAR_SPECIAL:
2852 generate_PUSHSPEC(cctx, tv->vval.v_number);
2853 break;
2854 case VAR_NUMBER:
2855 generate_PUSHNR(cctx, tv->vval.v_number);
2856 break;
2857#ifdef FEAT_FLOAT
2858 case VAR_FLOAT:
2859 generate_PUSHF(cctx, tv->vval.v_float);
2860 break;
2861#endif
2862 case VAR_BLOB:
2863 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2864 tv->vval.v_blob = NULL;
2865 break;
2866 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002867 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002868 tv->vval.v_string = NULL;
2869 break;
2870 default:
2871 iemsg("constant type not supported");
2872 clear_tv(tv);
2873 return FAIL;
2874 }
2875 tv->v_type = VAR_UNKNOWN;
2876 }
2877 return OK;
2878}
2879
2880/*
2881 * Generate code for any ppconst entries.
2882 */
2883 static int
2884generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2885{
2886 int i;
2887 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002888 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002889
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002890 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002891 for (i = 0; i < ppconst->pp_used; ++i)
2892 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2893 ret = FAIL;
2894 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002895 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002896 return ret;
2897}
2898
2899/*
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02002900 * Check that the last item of "ppconst" is a bool, if there is an item.
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002901 */
2902 static int
2903check_ppconst_bool(ppconst_T *ppconst)
2904{
2905 if (ppconst->pp_used > 0)
2906 {
2907 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002908 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002909
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002910 return check_typval_type(&t_bool, tv, where);
2911 }
2912 return OK;
2913}
2914
2915/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002916 * Clear ppconst constants. Used when failing.
2917 */
2918 static void
2919clear_ppconst(ppconst_T *ppconst)
2920{
2921 int i;
2922
2923 for (i = 0; i < ppconst->pp_used; ++i)
2924 clear_tv(&ppconst->pp_tv[i]);
2925 ppconst->pp_used = 0;
2926}
2927
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002928/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002929 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002930 * indexable value and the index or the two indexes of a slice.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002931 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002932 */
2933 static int
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002934compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002935{
2936 type_T **typep;
2937 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002938 vartype_T vartype;
2939 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002940
Bram Moolenaar261417b2021-05-06 21:04:55 +02002941 // We can index a list, dict and blob. If we don't know the type
2942 // we can use the index value type. If we still don't know use an "ANY"
2943 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002944 typep = ((type_T **)stack->ga_data) + stack->ga_len
2945 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002946 vartype = (*typep)->tt_type;
2947 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002948 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002949 if (*typep == &t_any && idxtype == &t_string)
2950 vartype = VAR_DICT;
2951 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002952 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002953 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002954 return FAIL;
2955 if (is_slice)
2956 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002957 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2958 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002959 FALSE, FALSE) == FAIL)
2960 return FAIL;
2961 }
2962 }
2963
Bram Moolenaar261417b2021-05-06 21:04:55 +02002964 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002965 {
2966 if (is_slice)
2967 {
2968 emsg(_(e_cannot_slice_dictionary));
2969 return FAIL;
2970 }
2971 if ((*typep)->tt_type == VAR_DICT)
2972 {
2973 *typep = (*typep)->tt_member;
2974 if (*typep == &t_unknown)
2975 // empty dict was used
2976 *typep = &t_any;
2977 }
2978 else
2979 {
2980 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2981 FALSE, FALSE) == FAIL)
2982 return FAIL;
2983 *typep = &t_any;
2984 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002985 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002986 return FAIL;
2987 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2988 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002989 if (keeping_dict != NULL)
2990 *keeping_dict = TRUE;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002991 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002992 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002993 {
2994 *typep = &t_string;
2995 if ((is_slice
2996 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2997 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2998 return FAIL;
2999 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02003000 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003001 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003002 if (is_slice)
3003 {
3004 *typep = &t_blob;
3005 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
3006 return FAIL;
3007 }
3008 else
3009 {
3010 *typep = &t_number;
3011 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
3012 return FAIL;
3013 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02003014 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02003015 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003016 {
3017 if (is_slice)
3018 {
3019 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02003020 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02003021 2) == FAIL)
3022 return FAIL;
3023 }
3024 else
3025 {
3026 if ((*typep)->tt_type == VAR_LIST)
3027 {
3028 *typep = (*typep)->tt_member;
3029 if (*typep == &t_unknown)
3030 // empty list was used
3031 *typep = &t_any;
3032 }
3033 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02003034 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
3035 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003036 return FAIL;
3037 }
3038 }
3039 else
3040 {
3041 emsg(_(e_string_list_dict_or_blob_required));
3042 return FAIL;
3043 }
3044 return OK;
3045}
3046
3047/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003048 * Generate an instruction to load script-local variable "name", without the
3049 * leading "s:".
3050 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 */
3052 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003053compile_load_scriptvar(
3054 cctx_T *cctx,
3055 char_u *name, // variable NUL terminated
3056 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003057 char_u **end, // end of variable
3058 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059{
Bram Moolenaare3d46852020-08-29 13:39:17 +02003060 scriptitem_T *si;
3061 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 imported_T *import;
3063
Bram Moolenaare3d46852020-08-29 13:39:17 +02003064 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3065 return FAIL;
3066 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003067 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003068 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003070 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003071 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3072 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 }
3074 if (idx >= 0)
3075 {
3076 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3077
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003078 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 current_sctx.sc_sid, idx, sv->sv_type);
3080 return OK;
3081 }
3082
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003083 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 if (import != NULL)
3085 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003086 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003087 {
3088 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003089 char_u *exp_name;
3090 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003091 ufunc_T *ufunc;
3092 type_T *type;
3093
3094 // Used "import * as Name", need to lookup the member.
3095 if (*p != '.')
3096 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003097 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003098 return FAIL;
3099 }
3100 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003101 if (VIM_ISWHITE(*p))
3102 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003103 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003104 return FAIL;
3105 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003106
Bram Moolenaar1c991142020-07-04 13:15:31 +02003107 // isolate one name
3108 exp_name = p;
3109 while (eval_isnamec(*p))
3110 ++p;
3111 cc = *p;
3112 *p = NUL;
3113
Bram Moolenaaredba7072021-03-13 21:14:18 +01003114 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3115 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003116 *p = cc;
3117 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003118 *end = p;
3119
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003120 if (idx < 0)
3121 {
3122 if (*p == '(' && ufunc != NULL)
3123 {
3124 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3125 return OK;
3126 }
3127 return FAIL;
3128 }
3129
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003130 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3131 import->imp_sid,
3132 idx,
3133 type);
3134 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003135 else if (import->imp_funcname != NULL)
3136 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003137 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003138 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3139 import->imp_sid,
3140 import->imp_var_vals_idx,
3141 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 return OK;
3143 }
3144
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003145 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003146 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 return FAIL;
3148}
3149
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003150 static int
3151generate_funcref(cctx_T *cctx, char_u *name)
3152{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003153 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003154
3155 if (ufunc == NULL)
3156 return FAIL;
3157
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003158 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003159 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3160 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003161 == FAIL)
3162 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003163 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003164}
3165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166/*
3167 * Compile a variable name into a load instruction.
3168 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003169 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 * When "error" is FALSE do not give an error when not found.
3171 */
3172 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003173compile_load(
3174 char_u **arg,
3175 char_u *end_arg,
3176 cctx_T *cctx,
3177 int is_expr,
3178 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179{
3180 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003181 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003182 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003184 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185
3186 if (*(*arg + 1) == ':')
3187 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003188 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003189 {
3190 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191
Bram Moolenaarfa596382021-04-07 21:58:16 +02003192 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003193 switch (**arg)
3194 {
3195 case 'g': isn_type = ISN_LOADGDICT; break;
3196 case 'w': isn_type = ISN_LOADWDICT; break;
3197 case 't': isn_type = ISN_LOADTDICT; break;
3198 case 'b': isn_type = ISN_LOADBDICT; break;
3199 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003200 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003201 goto theend;
3202 }
3203 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3204 goto theend;
3205 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 else
3208 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003209 isntype_T isn_type = ISN_DROP;
3210
Bram Moolenaarfa596382021-04-07 21:58:16 +02003211 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003212 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3213 if (name == NULL)
3214 return FAIL;
3215
3216 switch (**arg)
3217 {
3218 case 'v': res = generate_LOADV(cctx, name, error);
3219 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003220 case 's': if (is_expr && ASCII_ISUPPER(*name)
3221 && find_func(name, FALSE, cctx) != NULL)
3222 res = generate_funcref(cctx, name);
3223 else
3224 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003225 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003226 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003227 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003228 {
3229 if (is_expr && ASCII_ISUPPER(*name)
3230 && find_func(name, FALSE, cctx) != NULL)
3231 res = generate_funcref(cctx, name);
3232 else
3233 isn_type = ISN_LOADG;
3234 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003235 else
3236 {
3237 isn_type = ISN_LOADAUTO;
3238 vim_free(name);
3239 name = vim_strnsave(*arg, end - *arg);
3240 if (name == NULL)
3241 return FAIL;
3242 }
3243 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003244 case 'w': isn_type = ISN_LOADW; break;
3245 case 't': isn_type = ISN_LOADT; break;
3246 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003247 default: // cannot happen, just in case
3248 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003249 goto theend;
3250 }
3251 if (isn_type != ISN_DROP)
3252 {
3253 // Global, Buffer-local, Window-local and Tabpage-local
3254 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003255 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003256 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 }
3259 }
3260 else
3261 {
3262 size_t len = end - *arg;
3263 int idx;
3264 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003265 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266
3267 name = vim_strnsave(*arg, end - *arg);
3268 if (name == NULL)
3269 return FAIL;
3270
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003271 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3272 {
3273 script_autoload(name, FALSE);
3274 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3275 }
3276 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3277 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003279 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003280 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 }
3282 else
3283 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003284 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003285
Bram Moolenaar709664c2020-12-12 14:33:41 +01003286 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003288 type = lvar.lv_type;
3289 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003290 if (lvar.lv_from_outer != 0)
3291 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003292 else
3293 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 }
3295 else
3296 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003297 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003298 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003299 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003300 || find_imported(name, 0, cctx) != NULL)
3301 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003302
Bram Moolenaar0f769812020-09-12 18:32:34 +02003303 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003304 // uppercase letter it can be a user defined function.
3305 // generate_funcref() will fail if the function can't be found.
3306 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003307 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 }
3309 }
3310 if (gen_load)
3311 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003312 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003313 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003314 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003315 cctx->ctx_outer_used = TRUE;
3316 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 }
3318
3319 *arg = end;
3320
3321theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003322 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003323 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 vim_free(name);
3325 return res;
3326}
3327
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003328 static void
3329clear_instr_ga(garray_T *gap)
3330{
3331 int idx;
3332
3333 for (idx = 0; idx < gap->ga_len; ++idx)
3334 delete_instr(((isn_T *)gap->ga_data) + idx);
3335 ga_clear(gap);
3336}
3337
3338/*
3339 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3340 * Returns FAIL if compilation fails.
3341 */
3342 static int
3343compile_string(isn_T *isn, cctx_T *cctx)
3344{
3345 char_u *s = isn->isn_arg.string;
3346 garray_T save_ga = cctx->ctx_instr;
3347 int expr_res;
3348 int trailing_error;
3349 int instr_count;
3350 isn_T *instr = NULL;
3351
Bram Moolenaarcd268012021-07-22 19:11:08 +02003352 // Remove the string type from the stack.
3353 --cctx->ctx_type_stack.ga_len;
3354
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003355 // Temporarily reset the list of instructions so that the jump labels are
3356 // correct.
3357 cctx->ctx_instr.ga_len = 0;
3358 cctx->ctx_instr.ga_maxlen = 0;
3359 cctx->ctx_instr.ga_data = NULL;
3360 expr_res = compile_expr0(&s, cctx);
3361 s = skipwhite(s);
3362 trailing_error = *s != NUL;
3363
Bram Moolenaarff652882021-05-16 15:24:49 +02003364 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003365 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003366 {
3367 if (trailing_error)
3368 semsg(_(e_trailing_arg), s);
3369 clear_instr_ga(&cctx->ctx_instr);
3370 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003371 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003372 return FAIL;
3373 }
3374
3375 // Move the generated instructions into the ISN_INSTR instruction, then
3376 // restore the list of instructions.
3377 instr_count = cctx->ctx_instr.ga_len;
3378 instr = cctx->ctx_instr.ga_data;
3379 instr[instr_count].isn_type = ISN_FINISH;
3380
3381 cctx->ctx_instr = save_ga;
3382 vim_free(isn->isn_arg.string);
3383 isn->isn_type = ISN_INSTR;
3384 isn->isn_arg.instr = instr;
3385 return OK;
3386}
3387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388/*
3389 * Compile the argument expressions.
3390 * "arg" points to just after the "(" and is advanced to after the ")"
3391 */
3392 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003393compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003395 char_u *p = *arg;
3396 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003397 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003398 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399
Bram Moolenaare6085c52020-04-12 20:19:16 +02003400 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003402 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3403 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003404 if (*p == ')')
3405 {
3406 *arg = p + 1;
3407 return OK;
3408 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003409 if (must_end)
3410 {
3411 semsg(_(e_missing_comma_before_argument_str), p);
3412 return FAIL;
3413 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003414
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003415 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003416 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 return FAIL;
3418 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003419
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003420 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003421 && cctx->ctx_instr.ga_len == instr_count + 1)
3422 {
3423 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3424
3425 // {skip} argument of searchpair() can be compiled if not empty
3426 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3427 compile_string(isn, cctx);
3428 }
3429
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003430 if (*p != ',' && *skipwhite(p) == ',')
3431 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003432 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003433 p = skipwhite(p);
3434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003436 {
3437 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003438 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003439 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003440 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003441 else
3442 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003443 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003444 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003446failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003447 emsg(_(e_missing_close));
3448 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449}
3450
3451/*
3452 * Compile a function call: name(arg1, arg2)
3453 * "arg" points to "name", "arg + varlen" to the "(".
3454 * "argcount_init" is 1 for "value->method()"
3455 * Instructions:
3456 * EVAL arg1
3457 * EVAL arg2
3458 * BCALL / DCALL / UCALL
3459 */
3460 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003461compile_call(
3462 char_u **arg,
3463 size_t varlen,
3464 cctx_T *cctx,
3465 ppconst_T *ppconst,
3466 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467{
3468 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003469 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 int argcount = argcount_init;
3471 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003472 char_u fname_buf[FLEN_FIXED + 1];
3473 char_u *tofree = NULL;
3474 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003475 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003476 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003477 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003478 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003480 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003481 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003482 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003483 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003484 {
3485 char_u *s = skipwhite(*arg + varlen + 1);
3486 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003487 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003488
3489 argvars[0].v_type = VAR_UNKNOWN;
3490 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003491 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003492 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003493 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003494 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003495 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003496 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003497 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003498 {
3499 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3500
3501 *arg = s + 1;
3502 argvars[1].v_type = VAR_UNKNOWN;
3503 tv->v_type = VAR_NUMBER;
3504 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003505 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003506 f_has(argvars, tv);
3507 else
3508 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003509 clear_tv(&argvars[0]);
3510 ++ppconst->pp_used;
3511 return OK;
3512 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003513 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003514 if (!is_has)
3515 {
3516 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3517 return FAIL;
3518 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003519 }
3520
3521 if (generate_ppconst(cctx, ppconst) == FAIL)
3522 return FAIL;
3523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 if (varlen >= sizeof(namebuf))
3525 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003526 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 return FAIL;
3528 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003529 vim_strncpy(namebuf, *arg, varlen);
3530 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003532 // We handle the "skip" argument of searchpair() and searchpairpos()
3533 // differently.
3534 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3535 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3536 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3537 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003540 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003541 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542
Bram Moolenaar03290b82020-12-19 16:30:44 +01003543 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003544 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 {
3546 int idx;
3547
3548 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003549 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003551 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003552 if (STRCMP(name, "flatten") == 0)
3553 {
3554 emsg(_(e_cannot_use_flatten_in_vim9_script));
3555 goto theend;
3556 }
3557
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003558 if (STRCMP(name, "add") == 0 && argcount == 2)
3559 {
3560 garray_T *stack = &cctx->ctx_type_stack;
3561 type_T *type = ((type_T **)stack->ga_data)[
3562 stack->ga_len - 2];
3563
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003564 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003565 if (type->tt_type == VAR_LIST)
3566 {
3567 // inline "add(list, item)" so that the type can be checked
3568 res = generate_LISTAPPEND(cctx);
3569 idx = -1;
3570 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003571 else if (type->tt_type == VAR_BLOB)
3572 {
3573 // inline "add(blob, nr)" so that the type can be checked
3574 res = generate_BLOBAPPEND(cctx);
3575 idx = -1;
3576 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003577 }
3578
3579 if (idx >= 0)
3580 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3581 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003582 else
3583 semsg(_(e_unknownfunc), namebuf);
3584 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 }
3586
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003587 // An argument or local variable can be a function reference, this
3588 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003589 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003590 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003591 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003592 // If we can find the function by name generate the right call.
3593 // Skip global functions here, a local funcref takes precedence.
3594 ufunc = find_func(name, FALSE, cctx);
3595 if (ufunc != NULL && !func_is_global(ufunc))
3596 {
3597 res = generate_CALL(cctx, ufunc, argcount);
3598 goto theend;
3599 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601
3602 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003603 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003604 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003606 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003607 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003608 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003609 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003610 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003611
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003612 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003613 goto theend;
3614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615
Bram Moolenaar0f769812020-09-12 18:32:34 +02003616 // If we can find a global function by name generate the right call.
3617 if (ufunc != NULL)
3618 {
3619 res = generate_CALL(cctx, ufunc, argcount);
3620 goto theend;
3621 }
3622
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003623 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003624 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003625 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003626 res = generate_UCALL(cctx, name, argcount);
3627 else
3628 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003629
3630theend:
3631 vim_free(tofree);
3632 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633}
3634
3635// like NAMESPACE_CHAR but with 'a' and 'l'.
3636#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3637
3638/*
3639 * Find the end of a variable or function name. Unlike find_name_end() this
3640 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003641 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 * Return a pointer to just after the name. Equal to "arg" if there is no
3643 * valid name.
3644 */
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003645 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003646to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647{
3648 char_u *p;
3649
3650 // Quick check for valid starting character.
3651 if (!eval_isnamec1(*arg))
3652 return arg;
3653
3654 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3655 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3656 // and can be used in slice "[n:]".
3657 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003658 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3660 break;
3661 return p;
3662}
3663
3664/*
3665 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003666 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003667 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 */
3669 char_u *
3670to_name_const_end(char_u *arg)
3671{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003672 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 typval_T rettv;
3674
Bram Moolenaar678b2072021-07-26 21:10:11 +02003675 if (STRNCMP(p, "<SNR>", 5) == 0)
3676 p = skipdigits(p + 5);
3677 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 if (p == arg && *arg == '[')
3679 {
3680
3681 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003682 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 p = arg;
3684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 return p;
3686}
3687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688/*
3689 * parse a list: [expr, expr]
3690 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003691 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 */
3693 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003694compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695{
3696 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003697 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003699 int is_const;
3700 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003702 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003704 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003705 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003706 semsg(_(e_list_end), *arg);
3707 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003708 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003709 if (*p == ',')
3710 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003711 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003712 return FAIL;
3713 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003714 if (*p == ']')
3715 {
3716 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003717 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003718 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003719 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003720 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003721 if (!is_const)
3722 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 ++count;
3724 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003725 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003727 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3728 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003729 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003730 return FAIL;
3731 }
3732 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003733 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 p = skipwhite(p);
3735 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003736 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003738 ppconst->pp_is_const = is_all_const;
3739 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740}
3741
3742/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003743 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003744 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003745 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 */
3747 static int
3748compile_lambda(char_u **arg, cctx_T *cctx)
3749{
Bram Moolenaare462f522020-12-27 14:43:30 +01003750 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 typval_T rettv;
3752 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003753 evalarg_T evalarg;
3754
Bram Moolenaar844fb642021-10-23 13:32:30 +01003755 init_evalarg(&evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003756 evalarg.eval_flags = EVAL_EVALUATE;
3757 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758
3759 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003760 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3761 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003762 {
3763 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003764 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003765 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003766
Bram Moolenaar65c44152020-12-24 15:14:01 +01003767 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003769 ++ufunc->uf_refcount;
3770 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771
Bram Moolenaara9931532021-06-12 15:58:16 +02003772 // Compile it here to get the return type. The return type is optional,
3773 // when it's missing use t_unknown. This is recognized in
3774 // compile_return().
3775 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3776 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003777 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003779 // When the outer function is compiled for profiling or debugging, the
3780 // lambda may be called without profiling or debugging. Compile it here in
3781 // the right context.
3782 if (cctx->ctx_compile_type == CT_DEBUG
Bram Moolenaar648594e2021-07-11 17:55:01 +02003783#ifdef FEAT_PROFILE
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003784 || cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar648594e2021-07-11 17:55:01 +02003785#endif
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003786 )
3787 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaard9162552021-07-11 15:26:13 +02003788
Bram Moolenaar844fb642021-10-23 13:32:30 +01003789 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
3790 // "*arg" may point into it. Point into the original line to avoid a
3791 // dangling pointer.
3792 if (evalarg.eval_using_cmdline)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003793 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003794 garray_T *gap = &evalarg.eval_tofree_ga;
3795 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003796
3797 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3798 + off;
3799 }
3800
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003801 clear_evalarg(&evalarg, NULL);
3802
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003803 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003804 {
3805 // The return type will now be known.
3806 set_function_type(ufunc);
3807
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003808 // The function reference count will be 1. When the ISN_FUNCREF
3809 // instruction is deleted the reference count is decremented and the
3810 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003811 return generate_FUNCREF(cctx, ufunc);
3812 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003813
3814 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 return FAIL;
3816}
3817
3818/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003819 * Get a lambda and compile it. Uses Vim9 syntax.
3820 */
3821 int
3822get_lambda_tv_and_compile(
3823 char_u **arg,
3824 typval_T *rettv,
3825 int types_optional,
3826 evalarg_T *evalarg)
3827{
3828 int r;
3829 ufunc_T *ufunc;
3830 int save_sc_version = current_sctx.sc_version;
3831
3832 // Get the funcref in "rettv".
3833 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3834 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3835 current_sctx.sc_version = save_sc_version;
3836 if (r != OK)
3837 return r;
3838
3839 // "rettv" will now be a partial referencing the function.
3840 ufunc = rettv->vval.v_partial->pt_func;
3841
3842 // Compile it here to get the return type. The return type is optional,
3843 // when it's missing use t_unknown. This is recognized in
3844 // compile_return().
3845 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3846 ufunc->uf_ret_type = &t_unknown;
3847 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3848
3849 if (ufunc->uf_def_status == UF_COMPILED)
3850 {
3851 // The return type will now be known.
3852 set_function_type(ufunc);
3853 return OK;
3854 }
3855 clear_tv(rettv);
3856 return FAIL;
3857}
3858
3859/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003860 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003862 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 */
3864 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003865compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866{
3867 garray_T *instr = &cctx->ctx_instr;
3868 int count = 0;
3869 dict_T *d = dict_alloc();
3870 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003871 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003872 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003873 int is_const;
3874 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875
3876 if (d == NULL)
3877 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003878 if (generate_ppconst(cctx, ppconst) == FAIL)
3879 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003880 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003882 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883
Bram Moolenaar23c55272020-06-21 16:58:13 +02003884 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003885 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003886 *arg = NULL;
3887 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003888 }
3889
3890 if (**arg == '}')
3891 break;
3892
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003893 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003895 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003897 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003898 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003899 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003902 if (isn->isn_type == ISN_PUSHNR)
3903 {
3904 char buf[NUMBUFLEN];
3905
3906 // Convert to string at compile time.
3907 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3908 isn->isn_type = ISN_PUSHS;
3909 isn->isn_arg.string = vim_strsave((char_u *)buf);
3910 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 if (isn->isn_type == ISN_PUSHS)
3912 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003913 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003914 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003915 *arg = skipwhite(*arg);
3916 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003917 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003918 emsg(_(e_missing_matching_bracket_after_dict_key));
3919 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003920 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003921 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003923 else
3924 {
3925 // {"name": value},
3926 // {'name': value},
3927 // {name: value} use "name" as a literal key
3928 key = get_literal_key(arg);
3929 if (key == NULL)
3930 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003931 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003932 return FAIL;
3933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934
3935 // Check for duplicate keys, if using string keys.
3936 if (key != NULL)
3937 {
3938 item = dict_find(d, key, -1);
3939 if (item != NULL)
3940 {
3941 semsg(_(e_duplicate_key), key);
3942 goto failret;
3943 }
3944 item = dictitem_alloc(key);
3945 if (item != NULL)
3946 {
3947 item->di_tv.v_type = VAR_UNKNOWN;
3948 item->di_tv.v_lock = 0;
3949 if (dict_add(d, item) == FAIL)
3950 dictitem_free(item);
3951 }
3952 }
3953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 if (**arg != ':')
3955 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003956 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003957 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003958 else
3959 semsg(_(e_missing_dict_colon), *arg);
3960 return FAIL;
3961 }
3962 whitep = *arg + 1;
3963 if (!IS_WHITE_OR_NUL(*whitep))
3964 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003965 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 return FAIL;
3967 }
3968
Bram Moolenaar23c55272020-06-21 16:58:13 +02003969 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003970 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003971 *arg = NULL;
3972 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003973 }
3974
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003975 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003977 if (!is_const)
3978 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 ++count;
3980
Bram Moolenaar2c330432020-04-13 14:41:35 +02003981 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003982 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003983 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003984 *arg = NULL;
3985 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 if (**arg == '}')
3988 break;
3989 if (**arg != ',')
3990 {
3991 semsg(_(e_missing_dict_comma), *arg);
3992 goto failret;
3993 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003994 if (IS_WHITE_OR_NUL(*whitep))
3995 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003996 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003997 return FAIL;
3998 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003999 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02004000 if (!IS_WHITE_OR_NUL(*whitep))
4001 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01004002 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02004003 return FAIL;
4004 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01004005 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 }
4007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 *arg = *arg + 1;
4009
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004010 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02004011 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004012 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004013 *arg += STRLEN(*arg);
4014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004016 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 return generate_NEWDICT(cctx, count);
4018
4019failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004020 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004021 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004022 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004023 *arg = (char_u *)"";
4024 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 dict_unref(d);
4026 return FAIL;
4027}
4028
4029/*
4030 * Compile "&option".
4031 */
4032 static int
4033compile_get_option(char_u **arg, cctx_T *cctx)
4034{
4035 typval_T rettv;
4036 char_u *start = *arg;
4037 int ret;
4038
4039 // parse the option and get the current value to get the type.
4040 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004041 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 if (ret == OK)
4043 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004044 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01004045 char_u *name = vim_strnsave(start, *arg - start);
4046 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
4047 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048
4049 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4050 vim_free(name);
4051 }
4052 clear_tv(&rettv);
4053
4054 return ret;
4055}
4056
4057/*
4058 * Compile "$VAR".
4059 */
4060 static int
4061compile_get_env(char_u **arg, cctx_T *cctx)
4062{
4063 char_u *start = *arg;
4064 int len;
4065 int ret;
4066 char_u *name;
4067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 ++*arg;
4069 len = get_env_len(arg);
4070 if (len == 0)
4071 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004072 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 return FAIL;
4074 }
4075
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004076 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 name = vim_strnsave(start, len + 1);
4078 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4079 vim_free(name);
4080 return ret;
4081}
4082
4083/*
4084 * Compile "@r".
4085 */
4086 static int
4087compile_get_register(char_u **arg, cctx_T *cctx)
4088{
4089 int ret;
4090
4091 ++*arg;
4092 if (**arg == NUL)
4093 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004094 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 return FAIL;
4096 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004097 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 {
4099 emsg_invreg(**arg);
4100 return FAIL;
4101 }
4102 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4103 ++*arg;
4104 return ret;
4105}
4106
4107/*
4108 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004109 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 */
4111 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004112apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004114 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115
4116 // this works from end to start
4117 while (p > start)
4118 {
4119 --p;
4120 if (*p == '-' || *p == '+')
4121 {
4122 // only '-' has an effect, for '+' we only check the type
4123#ifdef FEAT_FLOAT
4124 if (rettv->v_type == VAR_FLOAT)
4125 {
4126 if (*p == '-')
4127 rettv->vval.v_float = -rettv->vval.v_float;
4128 }
4129 else
4130#endif
4131 {
4132 varnumber_T val;
4133 int error = FALSE;
4134
4135 // tv_get_number_chk() accepts a string, but we don't want that
4136 // here
4137 if (check_not_string(rettv) == FAIL)
4138 return FAIL;
4139 val = tv_get_number_chk(rettv, &error);
4140 clear_tv(rettv);
4141 if (error)
4142 return FAIL;
4143 if (*p == '-')
4144 val = -val;
4145 rettv->v_type = VAR_NUMBER;
4146 rettv->vval.v_number = val;
4147 }
4148 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004149 else if (numeric_only)
4150 {
4151 ++p;
4152 break;
4153 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004154 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 {
4156 int v = tv2bool(rettv);
4157
4158 // '!' is permissive in the type.
4159 clear_tv(rettv);
4160 rettv->v_type = VAR_BOOL;
4161 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4162 }
4163 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004164 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 return OK;
4166}
4167
4168/*
4169 * Recognize v: variables that are constants and set "rettv".
4170 */
4171 static void
4172get_vim_constant(char_u **arg, typval_T *rettv)
4173{
4174 if (STRNCMP(*arg, "v:true", 6) == 0)
4175 {
4176 rettv->v_type = VAR_BOOL;
4177 rettv->vval.v_number = VVAL_TRUE;
4178 *arg += 6;
4179 }
4180 else if (STRNCMP(*arg, "v:false", 7) == 0)
4181 {
4182 rettv->v_type = VAR_BOOL;
4183 rettv->vval.v_number = VVAL_FALSE;
4184 *arg += 7;
4185 }
4186 else if (STRNCMP(*arg, "v:null", 6) == 0)
4187 {
4188 rettv->v_type = VAR_SPECIAL;
4189 rettv->vval.v_number = VVAL_NULL;
4190 *arg += 6;
4191 }
4192 else if (STRNCMP(*arg, "v:none", 6) == 0)
4193 {
4194 rettv->v_type = VAR_SPECIAL;
4195 rettv->vval.v_number = VVAL_NONE;
4196 *arg += 6;
4197 }
4198}
4199
Bram Moolenaar657137c2021-01-09 15:45:23 +01004200 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004201get_compare_type(char_u *p, int *len, int *type_is)
4202{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004203 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004204 int i;
4205
4206 switch (p[0])
4207 {
4208 case '=': if (p[1] == '=')
4209 type = EXPR_EQUAL;
4210 else if (p[1] == '~')
4211 type = EXPR_MATCH;
4212 break;
4213 case '!': if (p[1] == '=')
4214 type = EXPR_NEQUAL;
4215 else if (p[1] == '~')
4216 type = EXPR_NOMATCH;
4217 break;
4218 case '>': if (p[1] != '=')
4219 {
4220 type = EXPR_GREATER;
4221 *len = 1;
4222 }
4223 else
4224 type = EXPR_GEQUAL;
4225 break;
4226 case '<': if (p[1] != '=')
4227 {
4228 type = EXPR_SMALLER;
4229 *len = 1;
4230 }
4231 else
4232 type = EXPR_SEQUAL;
4233 break;
4234 case 'i': if (p[1] == 's')
4235 {
4236 // "is" and "isnot"; but not a prefix of a name
4237 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4238 *len = 5;
4239 i = p[*len];
4240 if (!isalnum(i) && i != '_')
4241 {
4242 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4243 *type_is = TRUE;
4244 }
4245 }
4246 break;
4247 }
4248 return type;
4249}
4250
4251/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004252 * Skip over an expression, ignoring most errors.
4253 */
4254 static void
4255skip_expr_cctx(char_u **arg, cctx_T *cctx)
4256{
4257 evalarg_T evalarg;
4258
Bram Moolenaar844fb642021-10-23 13:32:30 +01004259 init_evalarg(&evalarg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004260 evalarg.eval_cctx = cctx;
4261 skip_expr(arg, &evalarg);
Bram Moolenaar844fb642021-10-23 13:32:30 +01004262 clear_evalarg(&evalarg, NULL);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004263}
4264
4265/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004267 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 */
4269 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004270compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004272 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273
4274 // this works from end to start
4275 while (p > start)
4276 {
4277 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004278 while (VIM_ISWHITE(*p))
4279 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 if (*p == '-' || *p == '+')
4281 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004282 int negate = *p == '-';
4283 isn_T *isn;
4284 garray_T *stack = &cctx->ctx_type_stack;
4285 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004287 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4288 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4289 return FAIL;
4290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4292 {
4293 --p;
4294 if (*p == '-')
4295 negate = !negate;
4296 }
4297 // only '-' has an effect, for '+' we only check the type
4298 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004299 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004301 if (isn == NULL)
4302 return FAIL;
4303 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004305 else if (numeric_only)
4306 {
4307 ++p;
4308 break;
4309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 else
4311 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004312 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004314 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004316 if (p[-1] == '!')
4317 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004320 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 return FAIL;
4322 }
4323 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004324 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 return OK;
4326}
4327
4328/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004329 * Compile "(expression)": recursive!
4330 * Return FAIL/OK.
4331 */
4332 static int
4333compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4334{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004335 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004336 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004337
Bram Moolenaar24156692021-01-14 20:35:49 +01004338 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4339 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004340 if (ppconst->pp_used <= PPSIZE - 10)
4341 {
4342 ret = compile_expr1(arg, cctx, ppconst);
4343 }
4344 else
4345 {
4346 // Not enough space in ppconst, flush constants.
4347 if (generate_ppconst(cctx, ppconst) == FAIL)
4348 return FAIL;
4349 ret = compile_expr0(arg, cctx);
4350 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004351 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4352 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004353 if (**arg == ')')
4354 ++*arg;
4355 else if (ret == OK)
4356 {
4357 emsg(_(e_missing_close));
4358 ret = FAIL;
4359 }
4360 return ret;
4361}
4362
4363/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004365 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 */
4367 static int
4368compile_subscript(
4369 char_u **arg,
4370 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004371 char_u *start_leader,
4372 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004373 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004375 char_u *name_start = *end_leader;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004376 int keeping_dict = FALSE;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 for (;;)
4379 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004380 char_u *p = skipwhite(*arg);
4381
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004382 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004383 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004384 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004385
4386 // If a following line starts with "->{" or "->X" advance to that
4387 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004388 // Also if a following line starts with ".x".
4389 if (next != NULL &&
4390 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004391 && (next[2] == '{'
4392 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004393 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004394 {
4395 next = next_line_from_context(cctx, TRUE);
4396 if (next == NULL)
4397 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004398 *arg = next;
4399 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004400 }
4401 }
4402
Bram Moolenaarcd268012021-07-22 19:11:08 +02004403 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4404 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004405 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004407 garray_T *stack = &cctx->ctx_type_stack;
4408 type_T *type;
4409 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004411 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004412 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004413 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004416 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4417
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004418 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004419 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004421 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004423 if (keeping_dict)
4424 {
4425 keeping_dict = FALSE;
4426 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4427 return FAIL;
4428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004430 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004432 char_u *pstart = p;
4433
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004434 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004435 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004436 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 // something->method()
4439 // Apply the '!', '-' and '+' first:
4440 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004441 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004444 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004445 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004446 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004447 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004448 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004449 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004450 garray_T *stack = &cctx->ctx_type_stack;
4451 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004452 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004453 int expr_isn_start = cctx->ctx_instr.ga_len;
4454 int expr_isn_end;
4455 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004456
4457 // Funcref call: list->(Refs[2])(arg)
4458 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004459 //
4460 // Fist compile the function expression.
4461 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004462 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004463
4464 // Remember the next instruction index, where the instructions
4465 // for arguments are being written.
4466 expr_isn_end = cctx->ctx_instr.ga_len;
4467
4468 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004469 if (**arg != '(')
4470 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004471 if (*skipwhite(*arg) == '(')
4472 emsg(_(e_nowhitespace));
4473 else
4474 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004475 return FAIL;
4476 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004477 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004478 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004479 return FAIL;
4480
Bram Moolenaar2927c072021-04-05 19:41:21 +02004481 // Move the instructions for the arguments to before the
4482 // instructions of the expression and move the type of the
4483 // expression after the argument types. This is what ISN_PCALL
4484 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004485 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004486 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4487 if (arg_isn_count > 0)
4488 {
4489 int expr_isn_count = expr_isn_end - expr_isn_start;
4490 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4491
4492 if (isn == NULL)
4493 return FAIL;
4494 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4495 + expr_isn_start,
4496 sizeof(isn_T) * expr_isn_count);
4497 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4498 + expr_isn_start,
4499 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4500 sizeof(isn_T) * arg_isn_count);
4501 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4502 + expr_isn_start + arg_isn_count,
4503 isn, sizeof(isn_T) * expr_isn_count);
4504 vim_free(isn);
4505
4506 type = ((type_T **)stack->ga_data)[type_idx_start];
4507 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4508 ((type_T **)stack->ga_data) + type_idx_start + 1,
4509 sizeof(type_T *)
4510 * (stack->ga_len - type_idx_start - 1));
4511 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4512 }
4513
Bram Moolenaar7e368202020-12-25 21:56:57 +01004514 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004515 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 return FAIL;
4517 }
4518 else
4519 {
4520 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004521 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004522 if (!eval_isnamec1(*p))
4523 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004524 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004525 return FAIL;
4526 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004527 if (ASCII_ISALPHA(*p) && p[1] == ':')
4528 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004529 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 ;
4531 if (*p != '(')
4532 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004533 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 return FAIL;
4535 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004536 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 return FAIL;
4538 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004539 if (keeping_dict)
4540 {
4541 keeping_dict = FALSE;
4542 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4543 return FAIL;
4544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004546 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004548 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004549
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004550 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004551 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004552 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004553 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004554 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004555 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004556 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004557
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004558 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004559 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004560 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004561 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004562 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004563 // missing first index is equal to zero
4564 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004565 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004566 else
4567 {
4568 if (compile_expr0(arg, cctx) == FAIL)
4569 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004570 if (**arg == ':')
4571 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004572 semsg(_(e_white_space_required_before_and_after_str_at_str),
4573 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004574 return FAIL;
4575 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004576 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004577 return FAIL;
4578 *arg = skipwhite(*arg);
4579 }
4580 if (**arg == ':')
4581 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004582 is_slice = TRUE;
4583 ++*arg;
4584 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4585 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004586 semsg(_(e_white_space_required_before_and_after_str_at_str),
4587 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004588 return FAIL;
4589 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004590 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004591 return FAIL;
4592 if (**arg == ']')
4593 // missing second index is equal to end of string
4594 generate_PUSHNR(cctx, -1);
4595 else
4596 {
4597 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004598 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004599 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004600 return FAIL;
4601 *arg = skipwhite(*arg);
4602 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604
4605 if (**arg != ']')
4606 {
4607 emsg(_(e_missbrac));
4608 return FAIL;
4609 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004610 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004612 if (keeping_dict)
4613 {
4614 keeping_dict = FALSE;
4615 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4616 return FAIL;
4617 }
4618 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004619 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004621 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004623 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004624 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004625 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004626 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004627
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004628 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004629 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004630 {
4631 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004632 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004633 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004634 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004635 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 while (eval_isnamec(*p))
4637 MB_PTR_ADV(p);
4638 if (p == *arg)
4639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004640 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 return FAIL;
4642 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004643 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
4644 return FAIL;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004645 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004647 keeping_dict = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 *arg = p;
4649 }
4650 else
4651 break;
4652 }
4653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 // Turn "dict.Func" into a partial for "Func" bound to "dict".
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004655 // This needs to be done at runtime to be able to check the type.
4656 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
4657 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658
4659 return OK;
4660}
4661
4662/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004663 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4664 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004666 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004667 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004668 *
4669 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 */
4671
4672/*
4673 * number number constant
4674 * 0zFFFFFFFF Blob constant
4675 * "string" string constant
4676 * 'string' literal string constant
4677 * &option-name option value
4678 * @r register contents
4679 * identifier variable value
4680 * function() function call
4681 * $VAR environment variable
4682 * (expression) nested expression
4683 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004684 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 *
4686 * Also handle:
4687 * ! in front logical NOT
4688 * - in front unary minus
4689 * + in front unary plus (ignored)
4690 * trailing (arg) funcref/partial call
4691 * trailing [] subscript in String or List
4692 * trailing .name entry in Dictionary
4693 * trailing ->name() method call
4694 */
4695 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004696compile_expr7(
4697 char_u **arg,
4698 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004699 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701 char_u *start_leader, *end_leader;
4702 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004703 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004704 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004706 ppconst->pp_is_const = FALSE;
4707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 /*
4709 * Skip '!', '-' and '+' characters. They are handled later.
4710 */
4711 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004712 if (eval_leader(arg, TRUE) == FAIL)
4713 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 end_leader = *arg;
4715
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004716 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 switch (**arg)
4718 {
4719 /*
4720 * Number constant.
4721 */
4722 case '0': // also for blob starting with 0z
4723 case '1':
4724 case '2':
4725 case '3':
4726 case '4':
4727 case '5':
4728 case '6':
4729 case '7':
4730 case '8':
4731 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004732 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004734 // Apply "-" and "+" just before the number now, right to
4735 // left. Matters especially when "->" follows. Stops at
4736 // '!'.
4737 if (apply_leader(rettv, TRUE,
4738 start_leader, &end_leader) == FAIL)
4739 {
4740 clear_tv(rettv);
4741 return FAIL;
4742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 break;
4744
4745 /*
4746 * String constant: "string".
4747 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004748 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 return FAIL;
4750 break;
4751
4752 /*
4753 * Literal string constant: 'str''ing'.
4754 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004755 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756 return FAIL;
4757 break;
4758
4759 /*
4760 * Constant Vim variable.
4761 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004762 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 ret = NOTDONE;
4764 break;
4765
4766 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004767 * "true" constant
4768 */
4769 case 't': if (STRNCMP(*arg, "true", 4) == 0
4770 && !eval_isnamec((*arg)[4]))
4771 {
4772 *arg += 4;
4773 rettv->v_type = VAR_BOOL;
4774 rettv->vval.v_number = VVAL_TRUE;
4775 }
4776 else
4777 ret = NOTDONE;
4778 break;
4779
4780 /*
4781 * "false" constant
4782 */
4783 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4784 && !eval_isnamec((*arg)[5]))
4785 {
4786 *arg += 5;
4787 rettv->v_type = VAR_BOOL;
4788 rettv->vval.v_number = VVAL_FALSE;
4789 }
4790 else
4791 ret = NOTDONE;
4792 break;
4793
4794 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004795 * "null" constant
4796 */
4797 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004798 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004799 {
4800 *arg += 4;
4801 rettv->v_type = VAR_SPECIAL;
4802 rettv->vval.v_number = VVAL_NULL;
4803 }
4804 else
4805 ret = NOTDONE;
4806 break;
4807
4808 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 * List: [expr, expr]
4810 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004811 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4812 return FAIL;
4813 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 break;
4815
4816 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817 * Dictionary: {'key': val, 'key': val}
4818 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004819 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4820 return FAIL;
4821 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 break;
4823
4824 /*
4825 * Option value: &name
4826 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004827 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4828 return FAIL;
4829 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 break;
4831
4832 /*
4833 * Environment variable: $VAR.
4834 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004835 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4836 return FAIL;
4837 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 break;
4839
4840 /*
4841 * Register contents: @r.
4842 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004843 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4844 return FAIL;
4845 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 break;
4847 /*
4848 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004849 * lambda: (arg, arg) => expr
4850 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004852 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4853 ret = compile_lambda(arg, cctx);
4854 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004855 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 break;
4857
4858 default: ret = NOTDONE;
4859 break;
4860 }
4861 if (ret == FAIL)
4862 return FAIL;
4863
Bram Moolenaar1c747212020-05-09 18:28:34 +02004864 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004866 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004867 clear_tv(rettv);
4868 else
4869 // A constant expression can possibly be handled compile time,
4870 // return the value instead of generating code.
4871 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 }
4873 else if (ret == NOTDONE)
4874 {
4875 char_u *p;
4876 int r;
4877
4878 if (!eval_isnamec1(**arg))
4879 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004880 if (!vim9_bad_comment(*arg))
4881 {
4882 if (ends_excmd(*skipwhite(*arg)))
4883 semsg(_(e_empty_expression_str), *arg);
4884 else
4885 semsg(_(e_name_expected_str), *arg);
4886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 return FAIL;
4888 }
4889
4890 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004891 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004892 if (p - *arg == (size_t)1 && **arg == '_')
4893 {
4894 emsg(_(e_cannot_use_underscore_here));
4895 return FAIL;
4896 }
4897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004899 {
4900 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004903 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02004904 if (cctx->ctx_skip != SKIP_YES
4905 && generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004906 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004907 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004908 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 if (r == FAIL)
4910 return FAIL;
4911 }
4912
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004913 // Handle following "[]", ".member", etc.
4914 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004915 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004916 ppconst) == FAIL)
4917 return FAIL;
4918 if (ppconst->pp_used > 0)
4919 {
4920 // apply the '!', '-' and '+' before the constant
4921 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004922 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004923 return FAIL;
4924 return OK;
4925 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004926 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004927 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004928 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929}
4930
4931/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004932 * Give the "white on both sides" error, taking the operator from "p[len]".
4933 */
4934 void
4935error_white_both(char_u *op, int len)
4936{
4937 char_u buf[10];
4938
4939 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004940 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004941}
4942
4943/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004944 * <type>expr7: runtime type check / conversion
4945 */
4946 static int
4947compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4948{
4949 type_T *want_type = NULL;
4950
4951 // Recognize <type>
4952 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4953 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004954 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004955 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4956 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004957 return FAIL;
4958
4959 if (**arg != '>')
4960 {
4961 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004962 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004963 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004964 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004965 return FAIL;
4966 }
4967 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004968 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004969 return FAIL;
4970 }
4971
4972 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4973 return FAIL;
4974
4975 if (want_type != NULL)
4976 {
4977 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004978 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004979 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004980
Bram Moolenaard1103582020-08-14 22:44:25 +02004981 generate_ppconst(cctx, ppconst);
4982 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004983 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004984 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004985 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004986 return FAIL;
4987 }
4988 }
4989
4990 return OK;
4991}
4992
4993/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 * * number multiplication
4995 * / number division
4996 * % number modulo
4997 */
4998 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004999compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000{
5001 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005002 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005003 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005005 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005006 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007 return FAIL;
5008
5009 /*
5010 * Repeat computing, until no "*", "/" or "%" is following.
5011 */
5012 for (;;)
5013 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005014 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 if (*op != '*' && *op != '/' && *op != '%')
5016 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005017 if (next != NULL)
5018 {
5019 *arg = next_line_from_context(cctx, TRUE);
5020 op = skipwhite(*arg);
5021 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005022
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005023 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005025 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005026 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01005028 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005029 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005031 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005032 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005034
5035 if (ppconst->pp_used == ppconst_used + 2
5036 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5037 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005038 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005039 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5040 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
5041 varnumber_T res = 0;
5042 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005044 // both are numbers: compute the result
5045 switch (*op)
5046 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005047 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005048 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005049 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005050 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005051 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005052 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005053 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005054 break;
5055 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005056 if (failed)
5057 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005058 tv1->vval.v_number = res;
5059 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005060 }
5061 else
5062 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005063 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005064 generate_two_op(cctx, op);
5065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 }
5067
5068 return OK;
5069}
5070
5071/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01005072 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 * - number subtraction
5074 * .. string concatenation
5075 */
5076 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005077compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078{
5079 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005080 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005082 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083
5084 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005085 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 return FAIL;
5087
5088 /*
5089 * Repeat computing, until no "+", "-" or ".." is following.
5090 */
5091 for (;;)
5092 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005093 op = may_peek_next_line(cctx, *arg, &next);
5094 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005096 if (op[0] == op[1] && *op != '.' && next)
5097 // Finding "++" or "--" on the next line is a separate command.
5098 // But ".." is concatenation.
5099 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005101 if (next != NULL)
5102 {
5103 *arg = next_line_from_context(cctx, TRUE);
5104 op = skipwhite(*arg);
5105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005107 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005109 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005110 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 }
5112
Bram Moolenaare0de1712020-12-02 17:36:54 +01005113 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005114 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005116 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005117 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118 return FAIL;
5119
Bram Moolenaara5565e42020-05-09 15:44:01 +02005120 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005121 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005122 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5123 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5124 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5125 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005127 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5128 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005129
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005130 // concat/subtract/add constant numbers
5131 if (*op == '+')
5132 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5133 else if (*op == '-')
5134 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5135 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005136 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005137 // concatenate constant strings
5138 char_u *s1 = tv1->vval.v_string;
5139 char_u *s2 = tv2->vval.v_string;
5140 size_t len1 = STRLEN(s1);
5141
5142 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5143 if (tv1->vval.v_string == NULL)
5144 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005145 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005146 return FAIL;
5147 }
5148 mch_memmove(tv1->vval.v_string, s1, len1);
5149 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005150 vim_free(s1);
5151 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005152 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005153 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005154 }
5155 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005156 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005157 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005158 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005159 if (*op == '.')
5160 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005161 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5162 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005163 return FAIL;
5164 generate_instr_drop(cctx, ISN_CONCAT, 1);
5165 }
5166 else
5167 generate_two_op(cctx, op);
5168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005169 }
5170
5171 return OK;
5172}
5173
5174/*
5175 * expr5a == expr5b
5176 * expr5a =~ expr5b
5177 * expr5a != expr5b
5178 * expr5a !~ expr5b
5179 * expr5a > expr5b
5180 * expr5a >= expr5b
5181 * expr5a < expr5b
5182 * expr5a <= expr5b
5183 * expr5a is expr5b
5184 * expr5a isnot expr5b
5185 *
5186 * Produces instructions:
5187 * EVAL expr5a Push result of "expr5a"
5188 * EVAL expr5b Push result of "expr5b"
5189 * COMPARE one of the compare instructions
5190 */
5191 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005192compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005194 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005196 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005199 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200
5201 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005202 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203 return FAIL;
5204
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005205 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005206 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207
5208 /*
5209 * If there is a comparative operator, use it.
5210 */
5211 if (type != EXPR_UNKNOWN)
5212 {
5213 int ic = FALSE; // Default: do not ignore case
5214
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005215 if (next != NULL)
5216 {
5217 *arg = next_line_from_context(cctx, TRUE);
5218 p = skipwhite(*arg);
5219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 if (type_is && (p[len] == '?' || p[len] == '#'))
5221 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005222 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 return FAIL;
5224 }
5225 // extra question mark appended: ignore case
5226 if (p[len] == '?')
5227 {
5228 ic = TRUE;
5229 ++len;
5230 }
5231 // extra '#' appended: match case (ignored)
5232 else if (p[len] == '#')
5233 ++len;
5234 // nothing appended: match case
5235
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005236 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005238 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005239 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240 }
5241
5242 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005243 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005244 return FAIL;
5245
Bram Moolenaara5565e42020-05-09 15:44:01 +02005246 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 return FAIL;
5248
Bram Moolenaara5565e42020-05-09 15:44:01 +02005249 if (ppconst->pp_used == ppconst_used + 2)
5250 {
5251 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5252 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5253 int ret;
5254
5255 // Both sides are a constant, compute the result now.
5256 // First check for a valid combination of types, this is more
5257 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005258 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005259 ret = FAIL;
5260 else
5261 {
5262 ret = typval_compare(tv1, tv2, type, ic);
5263 tv1->v_type = VAR_BOOL;
5264 tv1->vval.v_number = tv1->vval.v_number
5265 ? VVAL_TRUE : VVAL_FALSE;
5266 clear_tv(tv2);
5267 --ppconst->pp_used;
5268 }
5269 return ret;
5270 }
5271
5272 generate_ppconst(cctx, ppconst);
5273 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005274 }
5275
5276 return OK;
5277}
5278
Bram Moolenaar7f141552020-05-09 17:35:53 +02005279static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005281/*
5282 * Compile || or &&.
5283 */
5284 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005285compile_and_or(
5286 char_u **arg,
5287 cctx_T *cctx,
5288 char *op,
5289 ppconst_T *ppconst,
5290 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005292 char_u *next;
5293 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005294 int opchar = *op;
5295
5296 if (p[0] == opchar && p[1] == opchar)
5297 {
5298 garray_T *instr = &cctx->ctx_instr;
5299 garray_T end_ga;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005300 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301
5302 /*
5303 * Repeat until there is no following "||" or "&&"
5304 */
5305 ga_init2(&end_ga, sizeof(int), 10);
5306 while (p[0] == opchar && p[1] == opchar)
5307 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005308 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005309 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005310 int start_ctx_lnum = cctx->ctx_lnum;
5311 int save_lnum;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005312 int const_used;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005313 int status;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005314 jumpwhen_T jump_when = opchar == '|'
5315 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005316
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005317 if (next != NULL)
5318 {
5319 *arg = next_line_from_context(cctx, TRUE);
5320 p = skipwhite(*arg);
5321 }
5322
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005323 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5324 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005325 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005326 op, p);
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005327 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005328 return FAIL;
5329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005330
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005331 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005332 SOURCING_LNUM = start_lnum;
5333 save_lnum = cctx->ctx_lnum;
5334 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005335
5336 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005337 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005338 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005339 // Use the last ppconst if possible.
5340 if (ppconst->pp_used > 0)
5341 {
5342 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5343 int is_true = tv2bool(tv);
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005344
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005345 if ((is_true && opchar == '|')
5346 || (!is_true && opchar == '&'))
5347 {
5348 // For "false && expr" and "true || expr" the "expr"
5349 // does not need to be evaluated.
5350 cctx->ctx_skip = SKIP_YES;
5351 clear_tv(tv);
5352 tv->v_type = VAR_BOOL;
5353 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
5354 }
5355 else
5356 {
5357 // For "true && expr" and "false || expr" only "expr"
5358 // needs to be evaluated.
5359 --ppconst->pp_used;
5360 jump_when = JUMP_NEVER;
5361 }
5362 }
5363 else
5364 {
5365 // Every part must evaluate to a bool.
5366 status = bool_on_stack(cctx);
5367 }
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005368 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005369 if (status != FAIL)
5370 status = ga_grow(&end_ga, 1);
Bram Moolenaara7511c02021-04-03 21:47:07 +02005371 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005372 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373 {
5374 ga_clear(&end_ga);
5375 return FAIL;
5376 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005377
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005378 if (jump_when != JUMP_NEVER)
5379 {
5380 if (cctx->ctx_skip != SKIP_YES)
5381 {
5382 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5383 ++end_ga.ga_len;
5384 }
5385 generate_JUMP(cctx, jump_when, 0);
5386 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387
5388 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005389 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005390 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005391 {
5392 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005393 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005394 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005395
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005396 const_used = ppconst->pp_used;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005397 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5398 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005399 {
5400 ga_clear(&end_ga);
5401 return FAIL;
5402 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005403
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005404 // "0 || 1" results in true, "1 && 0" results in false.
5405 if (ppconst->pp_used == const_used + 1)
5406 {
5407 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5408
5409 if (tv->v_type == VAR_NUMBER
5410 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
5411 {
5412 tv->vval.v_number = tv->vval.v_number == 1
5413 ? VVAL_TRUE : VVAL_FALSE;
5414 tv->v_type = VAR_BOOL;
5415 }
5416 }
5417
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005418 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005419 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005420
5421 if (check_ppconst_bool(ppconst) == FAIL)
5422 {
5423 ga_clear(&end_ga);
5424 return FAIL;
5425 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005427 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
5428 // Every part must evaluate to a bool.
5429 if (bool_on_stack(cctx) == FAIL)
5430 {
5431 ga_clear(&end_ga);
5432 return FAIL;
5433 }
5434
5435 if (end_ga.ga_len > 0)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005436 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005437 // Fill in the end label in all jumps.
5438 generate_ppconst(cctx, ppconst);
5439 while (end_ga.ga_len > 0)
5440 {
5441 isn_T *isn;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005442
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005443 --end_ga.ga_len;
5444 isn = ((isn_T *)instr->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005445 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005446 isn->isn_arg.jump.jump_where = instr->ga_len;
5447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448 }
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005449 ga_clear(&end_ga);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005450
5451 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005452 }
5453
5454 return OK;
5455}
5456
5457/*
5458 * expr4a && expr4a && expr4a logical AND
5459 *
5460 * Produces instructions:
5461 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005462 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005463 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005464 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005465 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466 * EVAL expr4c Push result of "expr4c"
5467 * end:
5468 */
5469 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005470compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005471{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005472 int ppconst_used = ppconst->pp_used;
5473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005475 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476 return FAIL;
5477
5478 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005479 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005480}
5481
5482/*
5483 * expr3a || expr3b || expr3c logical OR
5484 *
5485 * Produces instructions:
5486 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005487 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005488 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005490 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491 * EVAL expr3c Push result of "expr3c"
5492 * end:
5493 */
5494 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005495compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005497 int ppconst_used = ppconst->pp_used;
5498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005499 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005500 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005501 return FAIL;
5502
5503 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005504 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005505}
5506
5507/*
5508 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005510 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005511 * JUMP_IF_FALSE alt jump if false
5512 * EVAL expr1a
5513 * JUMP_ALWAYS end
5514 * alt: EVAL expr1b
5515 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005516 *
5517 * Toplevel expression: expr2 ?? expr1
5518 * Produces instructions:
5519 * EVAL expr2 Push result of "expr2"
5520 * JUMP_AND_KEEP_IF_TRUE end jump if true
5521 * EVAL expr1
5522 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005523 */
5524 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005525compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005526{
5527 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005528 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005529 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005530
Bram Moolenaar3988f642020-08-27 22:43:03 +02005531 // Ignore all kinds of errors when not producing code.
5532 if (cctx->ctx_skip == SKIP_YES)
5533 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005534 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005535 return OK;
5536 }
5537
Bram Moolenaar61a89812020-05-07 16:58:17 +02005538 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005539 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005540 return FAIL;
5541
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005542 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005543 if (*p == '?')
5544 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005545 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 garray_T *instr = &cctx->ctx_instr;
5547 garray_T *stack = &cctx->ctx_type_stack;
5548 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005549 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005551 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005552 int has_const_expr = FALSE;
5553 int const_value = FALSE;
5554 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005555
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005556 if (next != NULL)
5557 {
5558 *arg = next_line_from_context(cctx, TRUE);
5559 p = skipwhite(*arg);
5560 }
5561
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005562 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005563 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005564 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005565 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005566 return FAIL;
5567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005568
Bram Moolenaara5565e42020-05-09 15:44:01 +02005569 if (ppconst->pp_used == ppconst_used + 1)
5570 {
5571 // the condition is a constant, we know whether the ? or the :
5572 // expression is to be evaluated.
5573 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005574 if (op_falsy)
5575 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5576 else
5577 {
5578 int error = FALSE;
5579
5580 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5581 &error);
5582 if (error)
5583 return FAIL;
5584 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005585 cctx->ctx_skip = save_skip == SKIP_YES ||
5586 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5587
5588 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5589 // "left ?? right" and "left" is truthy: produce "left"
5590 generate_ppconst(cctx, ppconst);
5591 else
5592 {
5593 clear_tv(&ppconst->pp_tv[ppconst_used]);
5594 --ppconst->pp_used;
5595 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005596 }
5597 else
5598 {
5599 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005600 if (op_falsy)
5601 end_idx = instr->ga_len;
5602 generate_JUMP(cctx, op_falsy
5603 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5604 if (op_falsy)
5605 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607
5608 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005609 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005610 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005611 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005612 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613
Bram Moolenaara5565e42020-05-09 15:44:01 +02005614 if (!has_const_expr)
5615 {
5616 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005617
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005618 if (!op_falsy)
5619 {
5620 // remember the type and drop it
5621 --stack->ga_len;
5622 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005623
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005624 end_idx = instr->ga_len;
5625 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005626
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005627 // jump here from JUMP_IF_FALSE
5628 isn = ((isn_T *)instr->ga_data) + alt_idx;
5629 isn->isn_arg.jump.jump_where = instr->ga_len;
5630 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005632
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005633 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005634 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005635 // Check for the ":".
5636 p = may_peek_next_line(cctx, *arg, &next);
5637 if (*p != ':')
5638 {
5639 emsg(_(e_missing_colon));
5640 return FAIL;
5641 }
5642 if (next != NULL)
5643 {
5644 *arg = next_line_from_context(cctx, TRUE);
5645 p = skipwhite(*arg);
5646 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005647
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005648 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5649 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005650 semsg(_(e_white_space_required_before_and_after_str_at_str),
5651 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005652 return FAIL;
5653 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005654
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005655 // evaluate the third expression
5656 if (has_const_expr)
5657 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005658 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005659 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005660 return FAIL;
5661 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5662 return FAIL;
5663 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005664
Bram Moolenaara5565e42020-05-09 15:44:01 +02005665 if (!has_const_expr)
5666 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005667 type_T **typep;
5668
Bram Moolenaara5565e42020-05-09 15:44:01 +02005669 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005670
Bram Moolenaara5565e42020-05-09 15:44:01 +02005671 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005672 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5673 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005674
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005675 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005676 isn = ((isn_T *)instr->ga_data) + end_idx;
5677 isn->isn_arg.jump.jump_where = instr->ga_len;
5678 }
5679
5680 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681 }
5682 return OK;
5683}
5684
5685/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005686 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005687 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5688 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005689 */
5690 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005691compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005692{
5693 ppconst_T ppconst;
5694
5695 CLEAR_FIELD(ppconst);
5696 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5697 {
5698 clear_ppconst(&ppconst);
5699 return FAIL;
5700 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005701 if (is_const != NULL)
5702 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005703 if (generate_ppconst(cctx, &ppconst) == FAIL)
5704 return FAIL;
5705 return OK;
5706}
5707
5708/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005709 * Toplevel expression.
5710 */
5711 static int
5712compile_expr0(char_u **arg, cctx_T *cctx)
5713{
5714 return compile_expr0_ext(arg, cctx, NULL);
5715}
5716
5717/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005718 * Compile "return [expr]".
5719 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005720 */
5721 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005722compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005723{
5724 char_u *p = arg;
5725 garray_T *stack = &cctx->ctx_type_stack;
5726 type_T *stack_type;
5727
5728 if (*p != NUL && *p != '|' && *p != '\n')
5729 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005730 if (legacy)
5731 {
5732 int save_flags = cmdmod.cmod_flags;
5733
5734 generate_LEGACY_EVAL(cctx, p);
5735 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5736 0, cctx, FALSE, FALSE) == FAIL)
5737 return NULL;
5738 cmdmod.cmod_flags |= CMOD_LEGACY;
5739 (void)skip_expr(&p, NULL);
5740 cmdmod.cmod_flags = save_flags;
5741 }
5742 else
5743 {
5744 // compile return argument into instructions
5745 if (compile_expr0(&p, cctx) == FAIL)
5746 return NULL;
5747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005748
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005749 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005750 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005751 // "check_return_type" with uf_ret_type set to &t_unknown is used
5752 // for an inline function without a specified return type. Set the
5753 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005754 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005755 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005756 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5757 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005758 || (!check_return_type
5759 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005760 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005761 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005762 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005763 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005764 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005765 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5766 && stack_type->tt_type != VAR_VOID
5767 && stack_type->tt_type != VAR_UNKNOWN)
5768 {
5769 emsg(_(e_returning_value_in_function_without_return_type));
5770 return NULL;
5771 }
5772 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005773 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005774 return NULL;
5775 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777 }
5778 else
5779 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005780 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005781 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005782 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5783 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005784 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005785 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005786 return NULL;
5787 }
5788
5789 // No argument, return zero.
5790 generate_PUSHNR(cctx, 0);
5791 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005792
5793 // Undo any command modifiers.
5794 generate_undo_cmdmods(cctx);
5795
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005796 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005797 return NULL;
5798
5799 // "return val | endif" is possible
5800 return skipwhite(p);
5801}
5802
5803/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005804 * Get a line from the compilation context, compatible with exarg_T getline().
5805 * Return a pointer to the line in allocated memory.
5806 * Return NULL for end-of-file or some error.
5807 */
5808 static char_u *
5809exarg_getline(
5810 int c UNUSED,
5811 void *cookie,
5812 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005813 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005814{
5815 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005816 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005817
Bram Moolenaar66250c92020-08-20 15:02:42 +02005818 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005819 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005820 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005821 return NULL;
5822 ++cctx->ctx_lnum;
5823 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5824 // Comment lines result in NULL pointers, skip them.
5825 if (p != NULL)
5826 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005827 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005828}
5829
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005830 void
5831fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5832{
5833 eap->getline = exarg_getline;
5834 eap->cookie = cctx;
5835}
5836
Bram Moolenaar04b12692020-05-04 23:24:44 +02005837/*
5838 * Compile a nested :def command.
5839 */
5840 static char_u *
5841compile_nested_function(exarg_T *eap, cctx_T *cctx)
5842{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005843 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005844 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005845 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005846 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005847 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005848 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005849 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005850
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005851 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005852 {
5853 emsg(_(e_cannot_use_bang_with_nested_def));
5854 return NULL;
5855 }
5856
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005857 if (*name_start == '/')
5858 {
5859 name_end = skip_regexp(name_start + 1, '/', TRUE);
5860 if (*name_end == '/')
5861 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005862 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005863 }
5864 if (name_end == name_start || *skipwhite(name_end) != '(')
5865 {
5866 if (!ends_excmd2(name_start, name_end))
5867 {
5868 semsg(_(e_invalid_command_str), eap->cmd);
5869 return NULL;
5870 }
5871
5872 // "def" or "def Name": list functions
5873 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5874 return NULL;
5875 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5876 }
5877
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005878 // Only g:Func() can use a namespace.
5879 if (name_start[1] == ':' && !is_global)
5880 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005881 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005882 return NULL;
5883 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005884 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005885 return NULL;
5886
Bram Moolenaar04b12692020-05-04 23:24:44 +02005887 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005888 fill_exarg_from_cctx(eap, cctx);
5889
Bram Moolenaar04b12692020-05-04 23:24:44 +02005890 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00005891 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005892 lambda_name = vim_strsave(get_lambda_name());
5893 if (lambda_name == NULL)
5894 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005895 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005896
Bram Moolenaar822ba242020-05-24 23:00:18 +02005897 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005898 {
5899 r = eap->skip ? OK : FAIL;
5900 goto theend;
5901 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005902
5903 // copy over the block scope IDs before compiling
5904 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5905 {
5906 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5907
5908 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5909 if (ufunc->uf_block_ids != NULL)
5910 {
5911 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5912 sizeof(int) * block_depth);
5913 ufunc->uf_block_depth = block_depth;
5914 }
5915 }
5916
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005917 compile_type = COMPILE_TYPE(ufunc);
5918#ifdef FEAT_PROFILE
5919 // If the outer function is profiled, also compile the nested function for
5920 // profiling.
5921 if (cctx->ctx_compile_type == CT_PROFILE)
5922 compile_type = CT_PROFILE;
5923#endif
5924 if (func_needs_compiling(ufunc, compile_type)
5925 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005926 {
5927 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005928 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005929 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005930
Bram Moolenaar648594e2021-07-11 17:55:01 +02005931#ifdef FEAT_PROFILE
5932 // When the outer function is compiled for profiling, the nested function
5933 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005934 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005935 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5936#endif
5937
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005938 if (is_global)
5939 {
5940 char_u *func_name = vim_strnsave(name_start + 2,
5941 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005942
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005943 if (func_name == NULL)
5944 r = FAIL;
5945 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005946 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005947 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005948 lambda_name = NULL;
5949 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005950 }
5951 else
5952 {
5953 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005954 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005955 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005956
Bram Moolenaareef21022020-08-01 22:16:43 +02005957 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005958 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005959 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005960 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005961 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5962 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005963
5964theend:
5965 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005966 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005967}
5968
5969/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 * Return the length of an assignment operator, or zero if there isn't one.
5971 */
5972 int
5973assignment_len(char_u *p, int *heredoc)
5974{
5975 if (*p == '=')
5976 {
5977 if (p[1] == '<' && p[2] == '<')
5978 {
5979 *heredoc = TRUE;
5980 return 3;
5981 }
5982 return 1;
5983 }
5984 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5985 return 2;
5986 if (STRNCMP(p, "..=", 3) == 0)
5987 return 3;
5988 return 0;
5989}
5990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005991/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005992 * Generate the load instruction for "name".
5993 */
5994 static void
5995generate_loadvar(
5996 cctx_T *cctx,
5997 assign_dest_T dest,
5998 char_u *name,
5999 lvar_T *lvar,
6000 type_T *type)
6001{
6002 switch (dest)
6003 {
6004 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006005 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006006 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
6007 break;
6008 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01006009 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
6010 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
6011 else
6012 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006013 break;
6014 case dest_buffer:
6015 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
6016 break;
6017 case dest_window:
6018 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
6019 break;
6020 case dest_tab:
6021 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
6022 break;
6023 case dest_script:
6024 compile_load_scriptvar(cctx,
6025 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
6026 break;
6027 case dest_env:
6028 // Include $ in the name here
6029 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
6030 break;
6031 case dest_reg:
6032 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
6033 break;
6034 case dest_vimvar:
6035 generate_LOADV(cctx, name + 2, TRUE);
6036 break;
6037 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01006038 if (lvar->lv_from_outer > 0)
6039 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
6040 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006041 else
6042 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
6043 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006044 case dest_expr:
6045 // list or dict value should already be on the stack.
6046 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006047 }
6048}
6049
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006050/*
6051 * Skip over "[expr]" or ".member".
6052 * Does not check for any errors.
6053 */
6054 static char_u *
6055skip_index(char_u *start)
6056{
6057 char_u *p = start;
6058
6059 if (*p == '[')
6060 {
6061 p = skipwhite(p + 1);
6062 (void)skip_expr(&p, NULL);
6063 p = skipwhite(p);
6064 if (*p == ']')
6065 return p + 1;
6066 return p;
6067 }
6068 // if (*p == '.')
6069 return to_name_end(p + 1, TRUE);
6070}
6071
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006072 void
6073vim9_declare_error(char_u *name)
6074{
6075 char *scope = "";
6076
6077 switch (*name)
6078 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006079 case 'g': scope = _("global"); break;
6080 case 'b': scope = _("buffer"); break;
6081 case 'w': scope = _("window"); break;
6082 case 't': scope = _("tab"); break;
6083 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006084 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006085 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006086 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006087 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006088 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006089 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006090 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006091 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006092 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006093}
6094
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006095/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006096 * For one assignment figure out the type of destination. Return it in "dest".
6097 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006098 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006099 * For a v:var "vimvaridx" is set.
6100 * "type" is set to the destination type if known, unchanted otherwise.
6101 * Return FAIL if an error message was given.
6102 */
6103 static int
6104get_var_dest(
6105 char_u *name,
6106 assign_dest_T *dest,
6107 int cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006108 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006109 int *vimvaridx,
6110 type_T **type,
6111 cctx_T *cctx)
6112{
6113 char_u *p;
6114
6115 if (*name == '&')
6116 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006117 int cc;
6118 long numval;
6119 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006120 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006121
6122 *dest = dest_option;
6123 if (cmdidx == CMD_final || cmdidx == CMD_const)
6124 {
6125 emsg(_(e_const_option));
6126 return FAIL;
6127 }
6128 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006129 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006130 if (p == NULL)
6131 {
6132 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02006133 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006134 return FAIL;
6135 }
6136 cc = *p;
6137 *p = NUL;
6138 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006139 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006140 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006141 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006142 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006143 case gov_unknown:
6144 semsg(_(e_unknown_option), name);
6145 return FAIL;
6146 case gov_string:
6147 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006148 if (opt_p_flags & P_FUNC)
6149 {
6150 // might be a Funcref, check the type later
6151 *type = &t_any;
6152 *dest = dest_func_option;
6153 }
6154 else
6155 {
6156 *type = &t_string;
6157 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006158 break;
6159 case gov_bool:
6160 case gov_hidden_bool:
6161 *type = &t_bool;
6162 break;
6163 case gov_number:
6164 case gov_hidden_number:
6165 *type = &t_number;
6166 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006167 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006168 }
6169 else if (*name == '$')
6170 {
6171 *dest = dest_env;
6172 *type = &t_string;
6173 }
6174 else if (*name == '@')
6175 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006176 if (name[1] != '@'
6177 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006178 {
6179 emsg_invreg(name[1]);
6180 return FAIL;
6181 }
6182 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006183 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006184 }
6185 else if (STRNCMP(name, "g:", 2) == 0)
6186 {
6187 *dest = dest_global;
6188 }
6189 else if (STRNCMP(name, "b:", 2) == 0)
6190 {
6191 *dest = dest_buffer;
6192 }
6193 else if (STRNCMP(name, "w:", 2) == 0)
6194 {
6195 *dest = dest_window;
6196 }
6197 else if (STRNCMP(name, "t:", 2) == 0)
6198 {
6199 *dest = dest_tab;
6200 }
6201 else if (STRNCMP(name, "v:", 2) == 0)
6202 {
6203 typval_T *vtv;
6204 int di_flags;
6205
6206 *vimvaridx = find_vim_var(name + 2, &di_flags);
6207 if (*vimvaridx < 0)
6208 {
6209 semsg(_(e_variable_not_found_str), name);
6210 return FAIL;
6211 }
6212 // We use the current value of "sandbox" here, is that OK?
6213 if (var_check_ro(di_flags, name, FALSE))
6214 return FAIL;
6215 *dest = dest_vimvar;
6216 vtv = get_vim_var_tv(*vimvaridx);
6217 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6218 }
6219 return OK;
6220}
6221
6222/*
6223 * Generate a STORE instruction for "dest", not being "dest_local".
6224 * Return FAIL when out of memory.
6225 */
6226 static int
6227generate_store_var(
6228 cctx_T *cctx,
6229 assign_dest_T dest,
6230 int opt_flags,
6231 int vimvaridx,
6232 int scriptvar_idx,
6233 int scriptvar_sid,
6234 type_T *type,
6235 char_u *name)
6236{
6237 switch (dest)
6238 {
6239 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006240 return generate_STOREOPT(cctx, ISN_STOREOPT,
6241 skip_option_env_lead(name), opt_flags);
6242 case dest_func_option:
6243 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
6244 skip_option_env_lead(name), opt_flags);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006245 case dest_global:
6246 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006247 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6248 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006249 case dest_buffer:
6250 // include b: with the name, easier to execute that way
6251 return generate_STORE(cctx, ISN_STOREB, 0, name);
6252 case dest_window:
6253 // include w: with the name, easier to execute that way
6254 return generate_STORE(cctx, ISN_STOREW, 0, name);
6255 case dest_tab:
6256 // include t: with the name, easier to execute that way
6257 return generate_STORE(cctx, ISN_STORET, 0, name);
6258 case dest_env:
6259 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6260 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006261 return generate_STORE(cctx, ISN_STOREREG,
6262 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006263 case dest_vimvar:
6264 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6265 case dest_script:
6266 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006267 // "s:" may be included in the name.
6268 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006269 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006270 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6271 scriptvar_sid, scriptvar_idx, type);
6272 case dest_local:
6273 case dest_expr:
6274 // cannot happen
6275 break;
6276 }
6277 return FAIL;
6278}
6279
Bram Moolenaar752fc692021-01-04 21:57:11 +01006280 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006281generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6282{
6283 if (lhs->lhs_dest != dest_local)
6284 return generate_store_var(cctx, lhs->lhs_dest,
6285 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6286 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6287 lhs->lhs_type, lhs->lhs_name);
6288
6289 if (lhs->lhs_lvar != NULL)
6290 {
6291 garray_T *instr = &cctx->ctx_instr;
6292 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6293
6294 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6295 // ISN_STORENR
6296 if (lhs->lhs_lvar->lv_from_outer == 0
6297 && instr->ga_len == instr_count + 1
6298 && isn->isn_type == ISN_PUSHNR)
6299 {
6300 varnumber_T val = isn->isn_arg.number;
6301 garray_T *stack = &cctx->ctx_type_stack;
6302
6303 isn->isn_type = ISN_STORENR;
6304 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6305 isn->isn_arg.storenr.stnr_val = val;
6306 if (stack->ga_len > 0)
6307 --stack->ga_len;
6308 }
6309 else if (lhs->lhs_lvar->lv_from_outer > 0)
6310 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6311 lhs->lhs_lvar->lv_from_outer);
6312 else
6313 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6314 }
6315 return OK;
6316}
6317
6318 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006319is_decl_command(int cmdidx)
6320{
6321 return cmdidx == CMD_let || cmdidx == CMD_var
6322 || cmdidx == CMD_final || cmdidx == CMD_const;
6323}
6324
6325/*
6326 * Figure out the LHS type and other properties for an assignment or one item
6327 * of ":unlet" with an index.
6328 * Returns OK or FAIL.
6329 */
6330 static int
6331compile_lhs(
6332 char_u *var_start,
6333 lhs_T *lhs,
6334 int cmdidx,
6335 int heredoc,
6336 int oplen,
6337 cctx_T *cctx)
6338{
6339 char_u *var_end;
6340 int is_decl = is_decl_command(cmdidx);
6341
6342 CLEAR_POINTER(lhs);
6343 lhs->lhs_dest = dest_local;
6344 lhs->lhs_vimvaridx = -1;
6345 lhs->lhs_scriptvar_idx = -1;
6346
6347 // "dest_end" is the end of the destination, including "[expr]" or
6348 // ".name".
6349 // "var_end" is the end of the variable/option/etc. name.
6350 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6351 if (*var_start == '@')
6352 var_end = var_start + 2;
6353 else
6354 {
6355 // skip over the leading "&", "&l:", "&g:" and "$"
6356 var_end = skip_option_env_lead(var_start);
6357 var_end = to_name_end(var_end, TRUE);
6358 }
6359
6360 // "a: type" is declaring variable "a" with a type, not dict "a:".
6361 if (is_decl && lhs->lhs_dest_end == var_start + 2
6362 && lhs->lhs_dest_end[-1] == ':')
6363 --lhs->lhs_dest_end;
6364 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6365 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006366 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006367
6368 // compute the length of the destination without "[expr]" or ".name"
6369 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006370 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006371 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6372 if (lhs->lhs_name == NULL)
6373 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006374
6375 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6376 // Something follows after the variable: "var[idx]" or "var.key".
6377 lhs->lhs_has_index = TRUE;
6378
Bram Moolenaar752fc692021-01-04 21:57:11 +01006379 if (heredoc)
6380 lhs->lhs_type = &t_list_string;
6381 else
6382 lhs->lhs_type = &t_any;
6383
6384 if (cctx->ctx_skip != SKIP_YES)
6385 {
6386 int declare_error = FALSE;
6387
6388 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6389 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6390 &lhs->lhs_type, cctx) == FAIL)
6391 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006392 if (lhs->lhs_dest != dest_local
6393 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006394 {
6395 // Specific kind of variable recognized.
6396 declare_error = is_decl;
6397 }
6398 else
6399 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006400 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006401 if (check_reserved_name(lhs->lhs_name) == FAIL)
6402 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006403
6404 if (lookup_local(var_start, lhs->lhs_varlen,
6405 &lhs->lhs_local_lvar, cctx) == OK)
6406 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6407 else
6408 {
6409 CLEAR_FIELD(lhs->lhs_arg_lvar);
6410 if (arg_exists(var_start, lhs->lhs_varlen,
6411 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6412 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6413 {
6414 if (is_decl)
6415 {
6416 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6417 return FAIL;
6418 }
6419 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6420 }
6421 }
6422 if (lhs->lhs_lvar != NULL)
6423 {
6424 if (is_decl)
6425 {
6426 semsg(_(e_variable_already_declared), lhs->lhs_name);
6427 return FAIL;
6428 }
6429 }
6430 else
6431 {
6432 int script_namespace = lhs->lhs_varlen > 1
6433 && STRNCMP(var_start, "s:", 2) == 0;
6434 int script_var = (script_namespace
6435 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006436 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006437 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006438 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006439 imported_T *import =
6440 find_imported(var_start, lhs->lhs_varlen, cctx);
6441
6442 if (script_namespace || script_var || import != NULL)
6443 {
6444 char_u *rawname = lhs->lhs_name
6445 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6446
6447 if (is_decl)
6448 {
6449 if (script_namespace)
6450 semsg(_(e_cannot_declare_script_variable_in_function),
6451 lhs->lhs_name);
6452 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006453 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006454 lhs->lhs_name);
6455 return FAIL;
6456 }
6457 else if (cctx->ctx_ufunc->uf_script_ctx_version
6458 == SCRIPT_VERSION_VIM9
6459 && script_namespace
6460 && !script_var && import == NULL)
6461 {
6462 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6463 return FAIL;
6464 }
6465
6466 lhs->lhs_dest = dest_script;
6467
6468 // existing script-local variables should have a type
6469 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6470 if (import != NULL)
6471 lhs->lhs_scriptvar_sid = import->imp_sid;
6472 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6473 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006474 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006475 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006476 lhs->lhs_scriptvar_sid, rawname,
6477 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6478 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006479 if (lhs->lhs_scriptvar_idx >= 0)
6480 {
6481 scriptitem_T *si = SCRIPT_ITEM(
6482 lhs->lhs_scriptvar_sid);
6483 svar_T *sv =
6484 ((svar_T *)si->sn_var_vals.ga_data)
6485 + lhs->lhs_scriptvar_idx;
6486 lhs->lhs_type = sv->sv_type;
6487 }
6488 }
6489 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006490 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006491 == FAIL)
6492 return FAIL;
6493 }
6494 }
6495
6496 if (declare_error)
6497 {
6498 vim9_declare_error(lhs->lhs_name);
6499 return FAIL;
6500 }
6501 }
6502
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006503 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01006504 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6505 var_end = lhs->lhs_dest_end;
6506
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006507 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006508 {
6509 if (is_decl && *var_end == ':')
6510 {
6511 char_u *p;
6512
6513 // parse optional type: "let var: type = expr"
6514 if (!VIM_ISWHITE(var_end[1]))
6515 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006516 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006517 return FAIL;
6518 }
6519 p = skipwhite(var_end + 1);
6520 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6521 if (lhs->lhs_type == NULL)
6522 return FAIL;
6523 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006524 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006525 }
6526 else if (lhs->lhs_lvar != NULL)
6527 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6528 }
6529
Bram Moolenaare42939a2021-04-05 17:11:17 +02006530 if (oplen == 3 && !heredoc
6531 && lhs->lhs_dest != dest_global
6532 && !lhs->lhs_has_index
6533 && lhs->lhs_type->tt_type != VAR_STRING
6534 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006535 {
6536 emsg(_(e_can_only_concatenate_to_string));
6537 return FAIL;
6538 }
6539
6540 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6541 && cctx->ctx_skip != SKIP_YES)
6542 {
6543 if (oplen > 1 && !heredoc)
6544 {
6545 // +=, /=, etc. require an existing variable
6546 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6547 return FAIL;
6548 }
6549 if (!is_decl)
6550 {
6551 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6552 return FAIL;
6553 }
6554
Bram Moolenaar3f327882021-03-17 20:56:38 +01006555 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006556 if ((lhs->lhs_type->tt_type == VAR_FUNC
6557 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006558 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006559 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006560
6561 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006562 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6563 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6564 if (lhs->lhs_lvar == NULL)
6565 return FAIL;
6566 lhs->lhs_new_local = TRUE;
6567 }
6568
6569 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006570 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006571 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006572 char_u *after = var_start + lhs->lhs_varlen;
6573 char_u *p;
6574
Bram Moolenaar752fc692021-01-04 21:57:11 +01006575 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006576 if (is_decl)
6577 {
6578 emsg(_(e_cannot_use_index_when_declaring_variable));
6579 return FAIL;
6580 }
6581
Bram Moolenaarc750d912021-11-29 22:02:12 +00006582 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
6583 // Only the last index is used below, if there are others
6584 // before it generate code for the expression. Thus for
6585 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6586 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006587 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006588 p = skip_index(after);
6589 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01006590 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006591 lhs->lhs_varlen_total = p - var_start;
6592 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006593 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006594 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006595 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006596 if (after > var_start + lhs->lhs_varlen)
6597 {
6598 lhs->lhs_varlen = after - var_start;
6599 lhs->lhs_dest = dest_expr;
6600 // We don't know the type before evaluating the expression,
6601 // use "any" until then.
6602 lhs->lhs_type = &t_any;
6603 }
6604
6605 if (lhs->lhs_type->tt_member == NULL)
6606 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006607 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00006608 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006609 }
6610 return OK;
6611}
6612
6613/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006614 * Figure out the LHS and check a few errors.
6615 */
6616 static int
6617compile_assign_lhs(
6618 char_u *var_start,
6619 lhs_T *lhs,
6620 int cmdidx,
6621 int is_decl,
6622 int heredoc,
6623 int oplen,
6624 cctx_T *cctx)
6625{
6626 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6627 return FAIL;
6628
6629 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6630 {
6631 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6632 return FAIL;
6633 }
6634 if (!is_decl && lhs->lhs_lvar != NULL
6635 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6636 {
6637 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6638 return FAIL;
6639 }
6640 return OK;
6641}
6642
6643/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006644 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6645 */
6646 static int
6647has_list_index(char_u *idx_start, cctx_T *cctx)
6648{
6649 char_u *p = idx_start;
6650 int save_skip;
6651
6652 if (*p != '[')
6653 return FALSE;
6654
6655 p = skipwhite(p + 1);
6656 if (*p == ':')
6657 return TRUE;
6658
6659 save_skip = cctx->ctx_skip;
6660 cctx->ctx_skip = SKIP_YES;
6661 (void)compile_expr0(&p, cctx);
6662 cctx->ctx_skip = save_skip;
6663 return *skipwhite(p) == ':';
6664}
6665
6666/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006667 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6668 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006669 */
6670 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006671compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006672 char_u *var_start,
6673 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006674 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006675 cctx_T *cctx)
6676{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006677 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006678 char_u *p;
6679 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006680 int need_white_before = TRUE;
6681 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006682
Bram Moolenaar752fc692021-01-04 21:57:11 +01006683 p = var_start + varlen;
6684 if (*p == '[')
6685 {
6686 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006687 if (*p == ':')
6688 {
6689 // empty first index, push zero
6690 r = generate_PUSHNR(cctx, 0);
6691 need_white_before = FALSE;
6692 }
6693 else
6694 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006695
6696 if (r == OK && *skipwhite(p) == ':')
6697 {
6698 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006699 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006700 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006701 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006702 empty_second = *skipwhite(p + 1) == ']';
6703 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6704 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006705 {
6706 semsg(_(e_white_space_required_before_and_after_str_at_str),
6707 ":", p);
6708 return FAIL;
6709 }
6710 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006711 if (*p == ']')
6712 // empty second index, push "none"
6713 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6714 else
6715 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006716 }
6717
Bram Moolenaar752fc692021-01-04 21:57:11 +01006718 if (r == OK && *skipwhite(p) != ']')
6719 {
6720 // this should not happen
6721 emsg(_(e_missbrac));
6722 r = FAIL;
6723 }
6724 }
6725 else // if (*p == '.')
6726 {
6727 char_u *key_end = to_name_end(p + 1, TRUE);
6728 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6729
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006730 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006731 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006732 return r;
6733}
6734
6735/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006736 * For a LHS with an index, load the variable to be indexed.
6737 */
6738 static int
6739compile_load_lhs(
6740 lhs_T *lhs,
6741 char_u *var_start,
6742 type_T *rhs_type,
6743 cctx_T *cctx)
6744{
6745 if (lhs->lhs_dest == dest_expr)
6746 {
6747 size_t varlen = lhs->lhs_varlen;
6748 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006749 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006750 char_u *p = var_start;
6751 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006752 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006753
Bram Moolenaare97976b2021-08-01 13:17:17 +02006754 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6755 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006756 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006757 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6758 res = compile_expr0(&p, cctx);
6759 var_start[varlen] = c;
6760 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6761 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006762 {
6763 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006764 if (res != FAIL)
6765 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006766 return FAIL;
6767 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006768
6769 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6770 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6771 // now we can properly check the type
6772 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6773 && rhs_type != &t_void
6774 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6775 FALSE, FALSE) == FAIL)
6776 return FAIL;
6777 }
6778 else
6779 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6780 lhs->lhs_lvar, lhs->lhs_type);
6781 return OK;
6782}
6783
6784/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006785 * Produce code for loading "lhs" and also take care of an index.
6786 * Return OK/FAIL.
6787 */
6788 static int
6789compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6790{
6791 compile_load_lhs(lhs, var_start, NULL, cctx);
6792
6793 if (lhs->lhs_has_index)
6794 {
6795 int range = FALSE;
6796
6797 // Get member from list or dict. First compile the
6798 // index value.
6799 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6800 return FAIL;
6801 if (range)
6802 {
6803 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6804 var_start);
6805 return FAIL;
6806 }
6807
6808 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006809 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006810 return FAIL;
6811 }
6812 return OK;
6813}
6814
6815/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006816 * Assignment to a list or dict member, or ":unlet" for the item, using the
6817 * information in "lhs".
6818 * Returns OK or FAIL.
6819 */
6820 static int
6821compile_assign_unlet(
6822 char_u *var_start,
6823 lhs_T *lhs,
6824 int is_assign,
6825 type_T *rhs_type,
6826 cctx_T *cctx)
6827{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006828 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006829 garray_T *stack = &cctx->ctx_type_stack;
6830 int range = FALSE;
6831
Bram Moolenaar68452172021-04-12 21:21:02 +02006832 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006833 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006834 if (is_assign && range
6835 && lhs->lhs_type->tt_type != VAR_LIST
6836 && lhs->lhs_type != &t_blob
6837 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006838 {
6839 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6840 return FAIL;
6841 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006842
6843 if (lhs->lhs_type == &t_any)
6844 {
6845 // Index on variable of unknown type: check at runtime.
6846 dest_type = VAR_ANY;
6847 }
6848 else
6849 {
6850 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006851 if (dest_type == VAR_DICT && range)
6852 {
6853 emsg(e_cannot_use_range_with_dictionary);
6854 return FAIL;
6855 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006856 if (dest_type == VAR_DICT
6857 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006858 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006859 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006860 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006861 type_T *type;
6862
6863 if (range)
6864 {
6865 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6866 if (need_type(type, &t_number,
6867 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006868 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006869 }
6870 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006871 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006872 && need_type(type, &t_number,
6873 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006874 return FAIL;
6875 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006876 }
6877
6878 // Load the dict or list. On the stack we then have:
6879 // - value (for assignment, not for :unlet)
6880 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006881 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006882 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006883 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6884 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006885
Bram Moolenaar68452172021-04-12 21:21:02 +02006886 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6887 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006888 {
6889 if (is_assign)
6890 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006891 if (range)
6892 {
6893 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6894 return FAIL;
6895 }
6896 else
6897 {
6898 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006899
Bram Moolenaar68452172021-04-12 21:21:02 +02006900 if (isn == NULL)
6901 return FAIL;
6902 isn->isn_arg.vartype = dest_type;
6903 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006904 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006905 else if (range)
6906 {
6907 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6908 return FAIL;
6909 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006910 else
6911 {
6912 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6913 return FAIL;
6914 }
6915 }
6916 else
6917 {
6918 emsg(_(e_indexable_type_required));
6919 return FAIL;
6920 }
6921
6922 return OK;
6923}
6924
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006925/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006926 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006927 * "let name"
6928 * "var name = expr"
6929 * "final name = expr"
6930 * "const name = expr"
6931 * "name = expr"
6932 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006933 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006934 * Return NULL for an error.
6935 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 */
6937 static char_u *
6938compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6939{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006940 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006942 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943 char_u *ret = NULL;
6944 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006945 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006946 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006947 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006949 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006951 int oplen = 0;
6952 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006953 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006954 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006956 int is_decl = is_decl_command(cmdidx);
6957 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006958 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006960 // Skip over the "var" or "[var, var]" to get to any "=".
6961 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6962 if (p == NULL)
6963 return *arg == '[' ? arg : NULL;
6964
Bram Moolenaar752fc692021-01-04 21:57:11 +01006965 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967 sp = p;
6968 p = skipwhite(p);
6969 op = p;
6970 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006971
6972 if (var_count > 0 && oplen == 0)
6973 // can be something like "[1, 2]->func()"
6974 return arg;
6975
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006976 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006978 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006979 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006980 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006981 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6982 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006983 if (VIM_ISWHITE(eap->cmd[2]))
6984 {
6985 semsg(_(e_no_white_space_allowed_after_str_str),
6986 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6987 return NULL;
6988 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006989 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6990 oplen = 2;
6991 incdec = TRUE;
6992 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006994 if (heredoc)
6995 {
6996 list_T *l;
6997 listitem_T *li;
6998
6999 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02007000 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02007002 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02007003 if (l == NULL)
7004 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005
Bram Moolenaar078269b2020-09-21 20:35:55 +02007006 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02007008 // Push each line and the create the list.
7009 FOR_ALL_LIST_ITEMS(l, li)
7010 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02007011 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02007012 li->li_tv.vval.v_string = NULL;
7013 }
7014 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007016 list_free(l);
7017 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007018 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007020 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007022 char_u *wp;
7023
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007024 // for "[var, var] = expr" evaluate the expression here, loop over the
7025 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007026 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02007027
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007028 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01007029 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007030 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007031 if (compile_expr0(&p, cctx) == FAIL)
7032 return NULL;
7033 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007035 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007036 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007037 type_T *stacktype;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007038 int needed_list_len;
7039 int did_check = FALSE;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007040
Bram Moolenaarec5929d2020-04-07 20:53:39 +02007041 stacktype = stack->ga_len == 0 ? &t_void
7042 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007043 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007044 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007045 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007046 goto theend;
7047 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01007048 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007049 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007050 goto theend;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007051 // If a constant list was used we can check the length right here.
7052 needed_list_len = semicolon ? var_count - 1 : var_count;
7053 if (instr->ga_len > 0)
7054 {
7055 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
7056
7057 if (isn->isn_type == ISN_NEWLIST)
7058 {
7059 did_check = TRUE;
7060 if (semicolon ? isn->isn_arg.number < needed_list_len
7061 : isn->isn_arg.number != needed_list_len)
7062 {
7063 semsg(_(e_expected_nr_items_but_got_nr),
7064 needed_list_len, isn->isn_arg.number);
7065 goto theend;
7066 }
7067 }
7068 }
7069 if (!did_check)
7070 generate_CHECKLEN(cctx, needed_list_len, semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007071 if (stacktype->tt_member != NULL)
7072 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007073 }
7074 }
7075
7076 /*
7077 * Loop over variables in "[var, var] = expr".
7078 * For "var = expr" and "let var: type" this is done only once.
7079 */
7080 if (var_count > 0)
7081 var_start = skipwhite(arg + 1); // skip over the "["
7082 else
7083 var_start = arg;
7084 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
7085 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007086 int instr_count = -1;
7087 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007088
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02007089 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
7090 {
7091 // Ignore underscore in "[a, _, b] = list".
7092 if (var_count > 0)
7093 {
7094 var_start = skipwhite(var_start + 2);
7095 continue;
7096 }
7097 emsg(_(e_cannot_use_underscore_here));
7098 goto theend;
7099 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007100 vim_free(lhs.lhs_name);
7101
7102 /*
7103 * Figure out the LHS type and other properties.
7104 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007105 if (compile_assign_lhs(var_start, &lhs, cmdidx,
7106 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007107 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02007108 if (heredoc)
7109 {
7110 SOURCING_LNUM = start_lnum;
7111 if (lhs.lhs_has_type
7112 && need_type(&t_list_string, lhs.lhs_type,
7113 -1, 0, cctx, FALSE, FALSE) == FAIL)
7114 goto theend;
7115 }
7116 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007117 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007118 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007119 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007120 if (oplen > 0 && var_count == 0)
7121 {
7122 // skip over the "=" and the expression
7123 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02007124 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007125 }
7126 }
7127 else if (oplen > 0)
7128 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007129 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01007130 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007131
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007132 // for "+=", "*=", "..=" etc. first load the current value
7133 if (*op != '='
7134 && compile_load_lhs_with_index(&lhs, var_start,
7135 cctx) == FAIL)
7136 goto theend;
7137
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007138 // For "var = expr" evaluate the expression.
7139 if (var_count == 0)
7140 {
7141 int r;
7142
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007143 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007144 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007145 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007146 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007147 r = generate_PUSHNR(cctx, 1);
7148 }
7149 else
7150 {
7151 // Temporarily hide the new local variable here, it is
7152 // not available to this expression.
7153 if (lhs.lhs_new_local)
7154 --cctx->ctx_locals.ga_len;
7155 wp = op + oplen;
7156 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7157 {
7158 if (lhs.lhs_new_local)
7159 ++cctx->ctx_locals.ga_len;
7160 goto theend;
7161 }
7162 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007163 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007164 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007165 if (r == FAIL)
7166 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007167 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007168 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007169 else if (semicolon && var_idx == var_count - 1)
7170 {
7171 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007172 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007173 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7174 goto theend;
7175 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007176 else
7177 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007178 // For "[var, var] = expr" get the "var_idx" item from the
7179 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007180 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007181 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007182 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007183
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007184 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007185 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007186 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007187 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007188 if ((rhs_type->tt_type == VAR_FUNC
7189 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007190 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007191 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007192 goto theend;
7193
Bram Moolenaar752fc692021-01-04 21:57:11 +01007194 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007195 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007196 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007197 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007198 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007199 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007200 }
7201 else
7202 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007203 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007204 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007205 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007206 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007207 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007208 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007209 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007210 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007211 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007212 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007213 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007214 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007215 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007216 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007217 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007218 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007219
Bram Moolenaar77709b12021-04-03 21:01:01 +02007220 // Without operator check type here, otherwise below.
7221 // Use the line number of the assignment.
7222 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007223 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7224 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007225 // If assigning to a list or dict member, use the
7226 // member type. Not for "list[:] =".
7227 if (lhs.lhs_has_index
7228 && !has_list_index(var_start + lhs.lhs_varlen,
7229 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007230 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007231 if (need_type_where(rhs_type, use_type, -1, where,
7232 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007233 goto theend;
7234 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007235 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007236 else
7237 {
7238 type_T *lhs_type = lhs.lhs_member_type;
7239
7240 // Special case: assigning to @# can use a number or a
7241 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007242 // Also: can assign a number to a float.
7243 if ((lhs_type == &t_number_or_string
7244 || lhs_type == &t_float)
7245 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007246 lhs_type = &t_number;
7247 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007248 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007249 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007250 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007251 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007252 else if (cmdidx == CMD_final)
7253 {
7254 emsg(_(e_final_requires_a_value));
7255 goto theend;
7256 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007257 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007259 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007260 goto theend;
7261 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00007262 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option
7263 || lhs.lhs_dest == dest_func_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007264 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007265 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007266 goto theend;
7267 }
7268 else
7269 {
7270 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007271 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007272 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007273 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007274 {
7275 case VAR_BOOL:
7276 generate_PUSHBOOL(cctx, VVAL_FALSE);
7277 break;
7278 case VAR_FLOAT:
7279#ifdef FEAT_FLOAT
7280 generate_PUSHF(cctx, 0.0);
7281#endif
7282 break;
7283 case VAR_STRING:
7284 generate_PUSHS(cctx, NULL);
7285 break;
7286 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007287 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007288 break;
7289 case VAR_FUNC:
7290 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7291 break;
7292 case VAR_LIST:
7293 generate_NEWLIST(cctx, 0);
7294 break;
7295 case VAR_DICT:
7296 generate_NEWDICT(cctx, 0);
7297 break;
7298 case VAR_JOB:
7299 generate_PUSHJOB(cctx, NULL);
7300 break;
7301 case VAR_CHANNEL:
7302 generate_PUSHCHANNEL(cctx, NULL);
7303 break;
7304 case VAR_NUMBER:
7305 case VAR_UNKNOWN:
7306 case VAR_ANY:
7307 case VAR_PARTIAL:
7308 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007309 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007310 case VAR_SPECIAL: // cannot happen
7311 generate_PUSHNR(cctx, 0);
7312 break;
7313 }
7314 }
7315 if (var_count == 0)
7316 end = p;
7317 }
7318
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007319 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007320 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007321 break;
7322
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007323 if (oplen > 0 && *op != '=')
7324 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007325 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007326 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007327
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007328 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007329 {
7330 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7331 goto theend;
7332 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007333 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007334 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007335 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007336 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7337 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007338#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007339 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007340 !(expected == &t_float && (stacktype == &t_number
7341 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007342#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007343 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007344 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007345 goto theend;
7346 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007347
7348 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007349 {
7350 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7351 goto theend;
7352 }
7353 else if (*op == '+')
7354 {
7355 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007356 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007357 lhs.lhs_member_type, stacktype,
7358 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007359 goto theend;
7360 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007361 else if (generate_two_op(cctx, op) == FAIL)
7362 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007365 // Use the line number of the assignment for store instruction.
7366 save_lnum = cctx->ctx_lnum;
7367 cctx->ctx_lnum = start_lnum - 1;
7368
Bram Moolenaar752fc692021-01-04 21:57:11 +01007369 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007370 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007371 // Use the info in "lhs" to store the value at the index in the
7372 // list or dict.
7373 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7374 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007375 {
7376 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007377 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007378 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007379 }
7380 else
7381 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007382 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007383 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007384 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007385 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007386 generate_LOCKCONST(cctx);
7387
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007388 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007389 && (lhs.lhs_type->tt_type == VAR_DICT
7390 || lhs.lhs_type->tt_type == VAR_LIST)
7391 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007392 && !(lhs.lhs_type->tt_member == &t_any
7393 && oplen > 0
7394 && rhs_type != NULL
7395 && rhs_type->tt_type == lhs.lhs_type->tt_type
7396 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007397 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007398 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007399 // also in legacy script. Not for "list<any> = val", then the
7400 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007401 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007402
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007403 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007404 {
7405 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007406 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007407 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007408 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007409 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007410
7411 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00007412 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007414
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007415 // For "[var, var] = expr" drop the "expr" value.
7416 // Also for "[var, var; _] = expr".
7417 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007418 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007419 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007420 goto theend;
7421 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007422
Bram Moolenaarb2097502020-07-19 17:17:02 +02007423 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424
7425theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007426 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 return ret;
7428}
7429
7430/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007431 * Check for an assignment at "eap->cmd", compile it if found.
7432 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7433 */
7434 static int
7435may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7436{
7437 char_u *pskip;
7438 char_u *p;
7439
7440 // Assuming the command starts with a variable or function name,
7441 // find what follows.
7442 // Skip over "var.member", "var[idx]" and the like.
7443 // Also "&opt = val", "$ENV = val" and "@r = val".
7444 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7445 ? eap->cmd + 1 : eap->cmd;
7446 p = to_name_end(pskip, TRUE);
7447 if (p > eap->cmd && *p != NUL)
7448 {
7449 char_u *var_end;
7450 int oplen;
7451 int heredoc;
7452
7453 if (eap->cmd[0] == '@')
7454 var_end = eap->cmd + 2;
7455 else
7456 var_end = find_name_end(pskip, NULL, NULL,
7457 FNE_CHECK_START | FNE_INCL_BR);
7458 oplen = assignment_len(skipwhite(var_end), &heredoc);
7459 if (oplen > 0)
7460 {
7461 size_t len = p - eap->cmd;
7462
7463 // Recognize an assignment if we recognize the variable
7464 // name:
7465 // "g:var = expr"
7466 // "local = expr" where "local" is a local var.
7467 // "script = expr" where "script" is a script-local var.
7468 // "import = expr" where "import" is an imported var
7469 // "&opt = expr"
7470 // "$ENV = expr"
7471 // "@r = expr"
7472 if (*eap->cmd == '&'
7473 || *eap->cmd == '$'
7474 || *eap->cmd == '@'
7475 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007476 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007477 {
7478 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7479 if (*line == NULL || *line == eap->cmd)
7480 return FAIL;
7481 return OK;
7482 }
7483 }
7484 }
7485
7486 if (*eap->cmd == '[')
7487 {
7488 // [var, var] = expr
7489 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7490 if (*line == NULL)
7491 return FAIL;
7492 if (*line != eap->cmd)
7493 return OK;
7494 }
7495 return NOTDONE;
7496}
7497
7498/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007499 * Check if "name" can be "unlet".
7500 */
7501 int
7502check_vim9_unlet(char_u *name)
7503{
7504 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7505 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007506 // "unlet s:var" is allowed in legacy script.
7507 if (*name == 's' && !script_is_vim9())
7508 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007509 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007510 return FAIL;
7511 }
7512 return OK;
7513}
7514
7515/*
7516 * Callback passed to ex_unletlock().
7517 */
7518 static int
7519compile_unlet(
7520 lval_T *lvp,
7521 char_u *name_end,
7522 exarg_T *eap,
7523 int deep UNUSED,
7524 void *coookie)
7525{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007526 cctx_T *cctx = coookie;
7527 char_u *p = lvp->ll_name;
7528 int cc = *name_end;
7529 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007530
Bram Moolenaar752fc692021-01-04 21:57:11 +01007531 if (cctx->ctx_skip == SKIP_YES)
7532 return OK;
7533
7534 *name_end = NUL;
7535 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007536 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007537 // :unlet $ENV_VAR
7538 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7539 }
7540 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7541 {
7542 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007543
Bram Moolenaar752fc692021-01-04 21:57:11 +01007544 // This is similar to assigning: lookup the list/dict, compile the
7545 // idx/key. Then instead of storing the value unlet the item.
7546 // unlet {list}[idx]
7547 // unlet {dict}[key] dict.key
7548 //
7549 // Figure out the LHS type and other properties.
7550 //
7551 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7552
7553 // : unlet an indexed item
7554 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007555 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007556 iemsg("called compile_lhs() without an index");
7557 ret = FAIL;
7558 }
7559 else
7560 {
7561 // Use the info in "lhs" to unlet the item at the index in the
7562 // list or dict.
7563 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007564 }
7565
Bram Moolenaar752fc692021-01-04 21:57:11 +01007566 vim_free(lhs.lhs_name);
7567 }
7568 else if (check_vim9_unlet(p) == FAIL)
7569 {
7570 ret = FAIL;
7571 }
7572 else
7573 {
7574 // Normal name. Only supports g:, w:, t: and b: namespaces.
7575 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007576 }
7577
Bram Moolenaar752fc692021-01-04 21:57:11 +01007578 *name_end = cc;
7579 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007580}
7581
7582/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007583 * Callback passed to ex_unletlock().
7584 */
7585 static int
7586compile_lock_unlock(
7587 lval_T *lvp,
7588 char_u *name_end,
7589 exarg_T *eap,
7590 int deep UNUSED,
7591 void *coookie)
7592{
7593 cctx_T *cctx = coookie;
7594 int cc = *name_end;
7595 char_u *p = lvp->ll_name;
7596 int ret = OK;
7597 size_t len;
7598 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007599 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007600
7601 if (cctx->ctx_skip == SKIP_YES)
7602 return OK;
7603
7604 // Cannot use :lockvar and :unlockvar on local variables.
7605 if (p[1] != ':')
7606 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007607 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007608
7609 if (lookup_local(p, end - p, NULL, cctx) == OK)
7610 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007611 char_u *s = p;
7612
7613 if (*end != '.' && *end != '[')
7614 {
7615 emsg(_(e_cannot_lock_unlock_local_variable));
7616 return FAIL;
7617 }
7618
7619 // For "d.member" put the local variable on the stack, it will be
7620 // passed to ex_lockvar() indirectly.
7621 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7622 return FAIL;
7623 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007624 }
7625 }
7626
7627 // Checking is done at runtime.
7628 *name_end = NUL;
7629 len = name_end - p + 20;
7630 buf = alloc(len);
7631 if (buf == NULL)
7632 ret = FAIL;
7633 else
7634 {
7635 vim_snprintf((char *)buf, len, "%s %s",
7636 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7637 p);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00007638 ret = generate_EXEC_copy(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007639
7640 vim_free(buf);
7641 *name_end = cc;
7642 }
7643 return ret;
7644}
7645
7646/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007647 * compile "unlet var", "lock var" and "unlock var"
7648 * "arg" points to "var".
7649 */
7650 static char_u *
7651compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7652{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007653 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7654 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7655 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007656 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7657}
7658
7659/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7661 */
7662 static int
7663compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7664{
7665 garray_T *instr = &cctx->ctx_instr;
7666 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7667
7668 if (endlabel == NULL)
7669 return FAIL;
7670 endlabel->el_next = *el;
7671 *el = endlabel;
7672 endlabel->el_end_label = instr->ga_len;
7673
7674 generate_JUMP(cctx, when, 0);
7675 return OK;
7676}
7677
7678 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007679compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007680{
7681 garray_T *instr = &cctx->ctx_instr;
7682
7683 while (*el != NULL)
7684 {
7685 endlabel_T *cur = (*el);
7686 isn_T *isn;
7687
7688 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007689 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007690 *el = cur->el_next;
7691 vim_free(cur);
7692 }
7693}
7694
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007695 static void
7696compile_free_jump_to_end(endlabel_T **el)
7697{
7698 while (*el != NULL)
7699 {
7700 endlabel_T *cur = (*el);
7701
7702 *el = cur->el_next;
7703 vim_free(cur);
7704 }
7705}
7706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007707/*
7708 * Create a new scope and set up the generic items.
7709 */
7710 static scope_T *
7711new_scope(cctx_T *cctx, scopetype_T type)
7712{
7713 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7714
7715 if (scope == NULL)
7716 return NULL;
7717 scope->se_outer = cctx->ctx_scope;
7718 cctx->ctx_scope = scope;
7719 scope->se_type = type;
7720 scope->se_local_count = cctx->ctx_locals.ga_len;
7721 return scope;
7722}
7723
7724/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007725 * Free the current scope and go back to the outer scope.
7726 */
7727 static void
7728drop_scope(cctx_T *cctx)
7729{
7730 scope_T *scope = cctx->ctx_scope;
7731
7732 if (scope == NULL)
7733 {
7734 iemsg("calling drop_scope() without a scope");
7735 return;
7736 }
7737 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007738 switch (scope->se_type)
7739 {
7740 case IF_SCOPE:
7741 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7742 case FOR_SCOPE:
7743 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7744 case WHILE_SCOPE:
7745 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7746 case TRY_SCOPE:
7747 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7748 case NO_SCOPE:
7749 case BLOCK_SCOPE:
7750 break;
7751 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007752 vim_free(scope);
7753}
7754
7755/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756 * compile "if expr"
7757 *
7758 * "if expr" Produces instructions:
7759 * EVAL expr Push result of "expr"
7760 * JUMP_IF_FALSE end
7761 * ... body ...
7762 * end:
7763 *
7764 * "if expr | else" Produces instructions:
7765 * EVAL expr Push result of "expr"
7766 * JUMP_IF_FALSE else
7767 * ... body ...
7768 * JUMP_ALWAYS end
7769 * else:
7770 * ... body ...
7771 * end:
7772 *
7773 * "if expr1 | elseif expr2 | else" Produces instructions:
7774 * EVAL expr Push result of "expr"
7775 * JUMP_IF_FALSE elseif
7776 * ... body ...
7777 * JUMP_ALWAYS end
7778 * elseif:
7779 * EVAL expr Push result of "expr"
7780 * JUMP_IF_FALSE else
7781 * ... body ...
7782 * JUMP_ALWAYS end
7783 * else:
7784 * ... body ...
7785 * end:
7786 */
7787 static char_u *
7788compile_if(char_u *arg, cctx_T *cctx)
7789{
7790 char_u *p = arg;
7791 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007792 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007793 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007794 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007795 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007796
Bram Moolenaara5565e42020-05-09 15:44:01 +02007797 CLEAR_FIELD(ppconst);
7798 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007799 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007800 clear_ppconst(&ppconst);
7801 return NULL;
7802 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007803 if (!ends_excmd2(arg, skipwhite(p)))
7804 {
7805 semsg(_(e_trailing_arg), p);
7806 return NULL;
7807 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007808 if (cctx->ctx_skip == SKIP_YES)
7809 clear_ppconst(&ppconst);
7810 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007811 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007812 int error = FALSE;
7813 int v;
7814
Bram Moolenaara5565e42020-05-09 15:44:01 +02007815 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007816 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007817 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007818 if (error)
7819 return NULL;
7820 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007821 }
7822 else
7823 {
7824 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007825 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007826 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007827 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007828 if (bool_on_stack(cctx) == FAIL)
7829 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007831
Bram Moolenaara91a7132021-03-25 21:12:15 +01007832 // CMDMOD_REV must come before the jump
7833 generate_undo_cmdmods(cctx);
7834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835 scope = new_scope(cctx, IF_SCOPE);
7836 if (scope == NULL)
7837 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007838 scope->se_skip_save = skip_save;
7839 // "is_had_return" will be reset if any block does not end in :return
7840 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007841
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007842 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007843 {
7844 // "where" is set when ":elseif", "else" or ":endif" is found
7845 scope->se_u.se_if.is_if_label = instr->ga_len;
7846 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7847 }
7848 else
7849 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850
Bram Moolenaarced68a02021-01-24 17:53:47 +01007851#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007852 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007853 && skip_save != SKIP_YES)
7854 {
7855 // generated a profile start, need to generate a profile end, since it
7856 // won't be done after returning
7857 cctx->ctx_skip = SKIP_NOT;
7858 generate_instr(cctx, ISN_PROF_END);
7859 cctx->ctx_skip = SKIP_YES;
7860 }
7861#endif
7862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007863 return p;
7864}
7865
7866 static char_u *
7867compile_elseif(char_u *arg, cctx_T *cctx)
7868{
7869 char_u *p = arg;
7870 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar90770b72021-11-30 20:57:38 +00007871 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007872 isn_T *isn;
7873 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007874 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007875 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007876
7877 if (scope == NULL || scope->se_type != IF_SCOPE)
7878 {
7879 emsg(_(e_elseif_without_if));
7880 return NULL;
7881 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007882 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007883 if (!cctx->ctx_had_return)
7884 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007885
Bram Moolenaarced68a02021-01-24 17:53:47 +01007886 if (cctx->ctx_skip == SKIP_NOT)
7887 {
7888 // previous block was executed, this one and following will not
7889 cctx->ctx_skip = SKIP_YES;
7890 scope->se_u.se_if.is_seen_skip_not = TRUE;
7891 }
7892 if (scope->se_u.se_if.is_seen_skip_not)
7893 {
7894 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007895 // Do not count the "elseif" for profiling and cmdmod
7896 instr->ga_len = current_instr_idx(cctx);
7897
Bram Moolenaarced68a02021-01-24 17:53:47 +01007898 skip_expr_cctx(&p, cctx);
7899 return p;
7900 }
7901
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007902 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007903 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007904 int moved_cmdmod = FALSE;
7905 int saved_debug = FALSE;
7906 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007907
7908 // Move any CMDMOD instruction to after the jump
7909 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7910 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007911 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007912 return NULL;
7913 ((isn_T *)instr->ga_data)[instr->ga_len] =
7914 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7915 --instr->ga_len;
7916 moved_cmdmod = TRUE;
7917 }
7918
Bram Moolenaar093165c2021-08-22 13:35:31 +02007919 // Remove the already generated ISN_DEBUG, it is written below the
7920 // ISN_FOR instruction.
7921 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7922 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7923 .isn_type == ISN_DEBUG)
7924 {
7925 --instr->ga_len;
7926 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7927 saved_debug = TRUE;
7928 }
7929
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007930 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007931 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007932 return NULL;
7933 // previous "if" or "elseif" jumps here
7934 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7935 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007936
Bram Moolenaara91a7132021-03-25 21:12:15 +01007937 if (moved_cmdmod)
7938 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007939
7940 if (saved_debug)
7941 {
7942 // move the debug instruction here
7943 if (GA_GROW_FAILS(instr, 1))
7944 return NULL;
7945 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7946 ++instr->ga_len;
7947 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007949
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007950 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007951 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007952 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007953 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007954 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007955#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007956 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007957 // the previous block was skipped, need to profile this line
7958 generate_instr(cctx, ISN_PROF_START);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007959#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007960 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007961 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007962 generate_instr_debug(cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007963 }
Bram Moolenaar90770b72021-11-30 20:57:38 +00007964
7965 instr_count = instr->ga_len;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007966 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007967 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007968 clear_ppconst(&ppconst);
7969 return NULL;
7970 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007971 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007972 if (!ends_excmd2(arg, skipwhite(p)))
7973 {
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007974 clear_ppconst(&ppconst);
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007975 semsg(_(e_trailing_arg), p);
7976 return NULL;
7977 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007978 if (scope->se_skip_save == SKIP_YES)
7979 clear_ppconst(&ppconst);
7980 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007981 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007982 int error = FALSE;
7983 int v;
7984
Bram Moolenaarfad27422021-11-30 21:58:19 +00007985 // The expression result is a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007986 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7987 if (error)
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007988 {
7989 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007990 return NULL;
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007991 }
Bram Moolenaar13106602020-10-04 16:06:05 +02007992 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007993 clear_ppconst(&ppconst);
7994 scope->se_u.se_if.is_if_label = -1;
7995 }
7996 else
7997 {
7998 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007999 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02008000 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008001 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008002 if (bool_on_stack(cctx) == FAIL)
8003 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008004
Bram Moolenaara91a7132021-03-25 21:12:15 +01008005 // CMDMOD_REV must come before the jump
8006 generate_undo_cmdmods(cctx);
8007
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008008 // "where" is set when ":elseif", "else" or ":endif" is found
8009 scope->se_u.se_if.is_if_label = instr->ga_len;
8010 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
8011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008012
8013 return p;
8014}
8015
8016 static char_u *
8017compile_else(char_u *arg, cctx_T *cctx)
8018{
8019 char_u *p = arg;
8020 garray_T *instr = &cctx->ctx_instr;
8021 isn_T *isn;
8022 scope_T *scope = cctx->ctx_scope;
8023
8024 if (scope == NULL || scope->se_type != IF_SCOPE)
8025 {
8026 emsg(_(e_else_without_if));
8027 return NULL;
8028 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01008029 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008030 if (!cctx->ctx_had_return)
8031 scope->se_u.se_if.is_had_return = FALSE;
8032 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008033
Bram Moolenaarced68a02021-01-24 17:53:47 +01008034#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008035 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01008036 {
8037 if (cctx->ctx_skip == SKIP_NOT
8038 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8039 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02008040 // the previous block was executed, do not count "else" for
8041 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01008042 --instr->ga_len;
8043 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
8044 {
8045 // the previous block was not executed, this one will, do count the
8046 // "else" for profiling
8047 cctx->ctx_skip = SKIP_NOT;
8048 generate_instr(cctx, ISN_PROF_END);
8049 generate_instr(cctx, ISN_PROF_START);
8050 cctx->ctx_skip = SKIP_YES;
8051 }
8052 }
8053#endif
8054
8055 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008056 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008057 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008058 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008059 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008060 if (!cctx->ctx_had_return
8061 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
8062 JUMP_ALWAYS, cctx) == FAIL)
8063 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008064 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008065
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008066 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008067 {
8068 if (scope->se_u.se_if.is_if_label >= 0)
8069 {
8070 // previous "if" or "elseif" jumps here
8071 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8072 isn->isn_arg.jump.jump_where = instr->ga_len;
8073 scope->se_u.se_if.is_if_label = -1;
8074 }
8075 }
8076
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008077 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008078 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
8079 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008080
8081 return p;
8082}
8083
8084 static char_u *
8085compile_endif(char_u *arg, cctx_T *cctx)
8086{
8087 scope_T *scope = cctx->ctx_scope;
8088 ifscope_T *ifscope;
8089 garray_T *instr = &cctx->ctx_instr;
8090 isn_T *isn;
8091
Bram Moolenaarfa984412021-03-25 22:15:28 +01008092 if (misplaced_cmdmod(cctx))
8093 return NULL;
8094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008095 if (scope == NULL || scope->se_type != IF_SCOPE)
8096 {
8097 emsg(_(e_endif_without_if));
8098 return NULL;
8099 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008100 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008101 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008102 if (!cctx->ctx_had_return)
8103 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008104
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008105 if (scope->se_u.se_if.is_if_label >= 0)
8106 {
8107 // previous "if" or "elseif" jumps here
8108 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8109 isn->isn_arg.jump.jump_where = instr->ga_len;
8110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008111 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008112 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01008113
8114#ifdef FEAT_PROFILE
8115 // even when skipping we count the endif as executed, unless the block it's
8116 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02008117 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01008118 && scope->se_skip_save != SKIP_YES)
8119 {
8120 cctx->ctx_skip = SKIP_NOT;
8121 generate_instr(cctx, ISN_PROF_START);
8122 }
8123#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02008124 cctx->ctx_skip = scope->se_skip_save;
8125
8126 // If all the blocks end in :return and there is an :else then the
8127 // had_return flag is set.
8128 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008130 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008131 return arg;
8132}
8133
8134/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01008135 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008136 *
8137 * Produces instructions:
8138 * PUSHNR -1
8139 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01008140 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008141 * top: FOR loop-idx, end Increment index, use list on bottom of stack
8142 * - if beyond end, jump to "end"
8143 * - otherwise get item from list and push it
8144 * STORE var Store item in "var"
8145 * ... body ...
8146 * JUMP top Jump back to repeat
8147 * end: DROP Drop the result of "expr"
8148 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008149 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
8150 * UNPACK 2 Split item in 2
8151 * STORE var1 Store item in "var1"
8152 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153 */
8154 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008155compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008156{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008157 char_u *arg;
8158 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008159 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008160 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008161 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008162 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008163 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008164 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008165 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008167 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008168 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008169 lvar_T *loop_lvar; // loop iteration variable
8170 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008171 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008172 type_T *item_type = &t_any;
8173 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008174 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008175
Bram Moolenaar792f7862020-11-23 08:31:18 +01008176 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008177 if (p == NULL)
8178 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008179 if (var_count == 0)
8180 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008181 else
8182 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008183
8184 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008185 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008186 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8187 return NULL;
8188 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008190 if (*p == ':' && wp != p)
8191 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8192 else
8193 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008194 return NULL;
8195 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008196 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008197 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8198 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008199
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008200 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8201 // instruction.
8202 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8203 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8204 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008205 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008206 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008207 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8208 .isn_arg.debug.dbg_break_lnum;
8209 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008211 scope = new_scope(cctx, FOR_SCOPE);
8212 if (scope == NULL)
8213 return NULL;
8214
Bram Moolenaar792f7862020-11-23 08:31:18 +01008215 // Reserve a variable to store the loop iteration counter and initialize it
8216 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008217 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8218 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008219 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008220 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008221 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008222 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008223 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008224 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008225
8226 // compile "expr", it remains on the stack until "endfor"
8227 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008228 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008229 {
8230 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008231 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008232 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008233 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008234
rbtnnbebf0692021-08-21 17:26:50 +02008235 if (cctx->ctx_skip != SKIP_YES)
8236 {
8237 // If we know the type of "var" and it is a not a supported type we can
8238 // give an error now.
8239 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8240 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008241 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008242 {
rbtnnbebf0692021-08-21 17:26:50 +02008243 semsg(_(e_for_loop_on_str_not_supported),
8244 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008245 drop_scope(cctx);
8246 return NULL;
8247 }
rbtnnbebf0692021-08-21 17:26:50 +02008248
8249 if (vartype->tt_type == VAR_STRING)
8250 item_type = &t_string;
8251 else if (vartype->tt_type == VAR_BLOB)
8252 item_type = &t_number;
8253 else if (vartype->tt_type == VAR_LIST
8254 && vartype->tt_member->tt_type != VAR_ANY)
8255 {
8256 if (!var_list)
8257 item_type = vartype->tt_member;
8258 else if (vartype->tt_member->tt_type == VAR_LIST
8259 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
rbtnnbebf0692021-08-21 17:26:50 +02008260 item_type = vartype->tt_member->tt_member;
8261 }
8262
8263 // CMDMOD_REV must come before the FOR instruction.
8264 generate_undo_cmdmods(cctx);
8265
8266 // "for_end" is set when ":endfor" is found
8267 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8268
8269 generate_FOR(cctx, loop_lvar->lv_idx);
8270
8271 arg = arg_start;
8272 if (var_list)
8273 {
8274 generate_UNPACK(cctx, var_count, semicolon);
8275 arg = skipwhite(arg + 1); // skip white after '['
8276
8277 // the list item is replaced by a number of items
8278 if (GA_GROW_FAILS(stack, var_count - 1))
8279 {
8280 drop_scope(cctx);
8281 return NULL;
8282 }
8283 --stack->ga_len;
8284 for (idx = 0; idx < var_count; ++idx)
8285 {
8286 ((type_T **)stack->ga_data)[stack->ga_len] =
8287 (semicolon && idx == 0) ? vartype : item_type;
8288 ++stack->ga_len;
8289 }
8290 }
8291
Bram Moolenaar792f7862020-11-23 08:31:18 +01008292 for (idx = 0; idx < var_count; ++idx)
8293 {
rbtnnbebf0692021-08-21 17:26:50 +02008294 assign_dest_T dest = dest_local;
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008295 int opt_flags = 0;
8296 int vimvaridx = -1;
rbtnnbebf0692021-08-21 17:26:50 +02008297 type_T *type = &t_any;
8298 type_T *lhs_type = &t_any;
8299 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008300
rbtnnbebf0692021-08-21 17:26:50 +02008301 p = skip_var_one(arg, FALSE);
8302 varlen = p - arg;
8303 name = vim_strnsave(arg, varlen);
8304 if (name == NULL)
8305 goto failed;
8306 if (*p == ':')
8307 {
8308 p = skipwhite(p + 1);
8309 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8310 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008311
Bram Moolenaarfad27422021-11-30 21:58:19 +00008312 // Script var is not supported.
rbtnnbebf0692021-08-21 17:26:50 +02008313 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008314 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008315 goto failed;
8316 if (dest != dest_local)
8317 {
8318 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008319 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008320 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008321 }
rbtnnbebf0692021-08-21 17:26:50 +02008322 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008323 {
rbtnnbebf0692021-08-21 17:26:50 +02008324 // Assigning to "_": drop the value.
8325 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8326 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008327 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008328 else
rbtnnbebf0692021-08-21 17:26:50 +02008329 {
Mike Williamscc9d7252021-11-23 12:35:57 +00008330 if (!valid_varname(arg, (int)varlen, FALSE))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008331 goto failed;
rbtnnbebf0692021-08-21 17:26:50 +02008332 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8333 {
8334 semsg(_(e_variable_already_declared), arg);
8335 goto failed;
8336 }
8337
8338 if (STRNCMP(name, "s:", 2) == 0)
8339 {
8340 semsg(_(e_cannot_declare_script_variable_in_function), name);
8341 goto failed;
8342 }
8343
8344 // Reserve a variable to store "var".
8345 where.wt_index = var_list ? idx + 1 : 0;
8346 where.wt_variable = TRUE;
8347 if (lhs_type == &t_any)
8348 lhs_type = item_type;
8349 else if (item_type != &t_unknown
8350 && (item_type == &t_any
8351 ? need_type(item_type, lhs_type,
8352 -1, 0, cctx, FALSE, FALSE)
8353 : check_type(lhs_type, item_type, TRUE, where))
8354 == FAIL)
8355 goto failed;
8356 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8357 if (var_lvar == NULL)
8358 // out of memory or used as an argument
8359 goto failed;
8360
8361 if (semicolon && idx == var_count - 1)
8362 var_lvar->lv_type = vartype;
8363 else
8364 var_lvar->lv_type = item_type;
8365 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8366 }
8367
8368 if (*p == ',' || *p == ';')
8369 ++p;
8370 arg = skipwhite(p);
8371 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008372 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008373
rbtnnbebf0692021-08-21 17:26:50 +02008374 if (cctx->ctx_compile_type == CT_DEBUG)
8375 {
8376 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008377
rbtnnbebf0692021-08-21 17:26:50 +02008378 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8379 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8380 cctx->ctx_prev_lnum = prev_lnum;
8381 generate_instr_debug(cctx);
8382 cctx->ctx_prev_lnum = save_prev_lnum;
8383 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008384 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008385
Bram Moolenaar792f7862020-11-23 08:31:18 +01008386 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008387
8388failed:
8389 vim_free(name);
8390 drop_scope(cctx);
8391 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008392}
8393
8394/*
8395 * compile "endfor"
8396 */
8397 static char_u *
8398compile_endfor(char_u *arg, cctx_T *cctx)
8399{
8400 garray_T *instr = &cctx->ctx_instr;
8401 scope_T *scope = cctx->ctx_scope;
8402 forscope_T *forscope;
8403 isn_T *isn;
8404
Bram Moolenaarfa984412021-03-25 22:15:28 +01008405 if (misplaced_cmdmod(cctx))
8406 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008408 if (scope == NULL || scope->se_type != FOR_SCOPE)
8409 {
8410 emsg(_(e_for));
8411 return NULL;
8412 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008413 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008414 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008415 if (cctx->ctx_skip != SKIP_YES)
8416 {
8417 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418
rbtnnbebf0692021-08-21 17:26:50 +02008419 // At end of ":for" scope jump back to the FOR instruction.
8420 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008421
rbtnnbebf0692021-08-21 17:26:50 +02008422 // Fill in the "end" label in the FOR statement so it can jump here.
8423 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8424 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008425
rbtnnbebf0692021-08-21 17:26:50 +02008426 // Fill in the "end" label any BREAK statements
8427 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008428
rbtnnbebf0692021-08-21 17:26:50 +02008429 // Below the ":for" scope drop the "expr" list from the stack.
8430 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8431 return NULL;
8432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433
8434 vim_free(scope);
8435
8436 return arg;
8437}
8438
8439/*
8440 * compile "while expr"
8441 *
8442 * Produces instructions:
8443 * top: EVAL expr Push result of "expr"
8444 * JUMP_IF_FALSE end jump if false
8445 * ... body ...
8446 * JUMP top Jump back to repeat
8447 * end:
8448 *
8449 */
8450 static char_u *
8451compile_while(char_u *arg, cctx_T *cctx)
8452{
8453 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008454 scope_T *scope;
8455
8456 scope = new_scope(cctx, WHILE_SCOPE);
8457 if (scope == NULL)
8458 return NULL;
8459
Bram Moolenaara91a7132021-03-25 21:12:15 +01008460 // "endwhile" jumps back here, one before when profiling or using cmdmods
8461 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008462
8463 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008464 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008465 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008466
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008467 if (!ends_excmd2(arg, skipwhite(p)))
8468 {
8469 semsg(_(e_trailing_arg), p);
8470 return NULL;
8471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008472
rbtnnd895b1d2021-08-20 20:54:25 +02008473 if (cctx->ctx_skip != SKIP_YES)
8474 {
8475 if (bool_on_stack(cctx) == FAIL)
8476 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008477
rbtnnd895b1d2021-08-20 20:54:25 +02008478 // CMDMOD_REV must come before the jump
8479 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008480
rbtnnd895b1d2021-08-20 20:54:25 +02008481 // "while_end" is set when ":endwhile" is found
8482 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008483 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008484 return FAIL;
8485 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008486
8487 return p;
8488}
8489
8490/*
8491 * compile "endwhile"
8492 */
8493 static char_u *
8494compile_endwhile(char_u *arg, cctx_T *cctx)
8495{
8496 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008497 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008498
Bram Moolenaarfa984412021-03-25 22:15:28 +01008499 if (misplaced_cmdmod(cctx))
8500 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008501 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8502 {
8503 emsg(_(e_while));
8504 return NULL;
8505 }
8506 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008507 if (cctx->ctx_skip != SKIP_YES)
8508 {
8509 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008510
Bram Moolenaarf002a412021-01-24 13:34:18 +01008511#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008512 // count the endwhile before jumping
8513 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008514#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008515
rbtnnd895b1d2021-08-20 20:54:25 +02008516 // At end of ":for" scope jump back to the FOR instruction.
8517 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008518
rbtnnd895b1d2021-08-20 20:54:25 +02008519 // Fill in the "end" label in the WHILE statement so it can jump here.
8520 // And in any jumps for ":break"
8521 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008522 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008524
8525 vim_free(scope);
8526
8527 return arg;
8528}
8529
8530/*
8531 * compile "continue"
8532 */
8533 static char_u *
8534compile_continue(char_u *arg, cctx_T *cctx)
8535{
8536 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008537 int try_scopes = 0;
8538 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008539
8540 for (;;)
8541 {
8542 if (scope == NULL)
8543 {
8544 emsg(_(e_continue));
8545 return NULL;
8546 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008547 if (scope->se_type == FOR_SCOPE)
8548 {
8549 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008550 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008551 }
8552 if (scope->se_type == WHILE_SCOPE)
8553 {
8554 loop_label = scope->se_u.se_while.ws_top_label;
8555 break;
8556 }
8557 if (scope->se_type == TRY_SCOPE)
8558 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008559 scope = scope->se_outer;
8560 }
8561
Bram Moolenaarc150c092021-02-13 15:02:46 +01008562 if (try_scopes > 0)
8563 // Inside one or more try/catch blocks we first need to jump to the
8564 // "finally" or "endtry" to cleanup.
8565 generate_TRYCONT(cctx, try_scopes, loop_label);
8566 else
8567 // Jump back to the FOR or WHILE instruction.
8568 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008570 return arg;
8571}
8572
8573/*
8574 * compile "break"
8575 */
8576 static char_u *
8577compile_break(char_u *arg, cctx_T *cctx)
8578{
8579 scope_T *scope = cctx->ctx_scope;
8580 endlabel_T **el;
8581
8582 for (;;)
8583 {
8584 if (scope == NULL)
8585 {
8586 emsg(_(e_break));
8587 return NULL;
8588 }
8589 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8590 break;
8591 scope = scope->se_outer;
8592 }
8593
8594 // Jump to the end of the FOR or WHILE loop.
8595 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008596 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008597 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008598 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008599 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8600 return FAIL;
8601
8602 return arg;
8603}
8604
8605/*
8606 * compile "{" start of block
8607 */
8608 static char_u *
8609compile_block(char_u *arg, cctx_T *cctx)
8610{
8611 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8612 return NULL;
8613 return skipwhite(arg + 1);
8614}
8615
8616/*
8617 * compile end of block: drop one scope
8618 */
8619 static void
8620compile_endblock(cctx_T *cctx)
8621{
8622 scope_T *scope = cctx->ctx_scope;
8623
8624 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008625 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008626 vim_free(scope);
8627}
8628
8629/*
8630 * compile "try"
8631 * Creates a new scope for the try-endtry, pointing to the first catch and
8632 * finally.
8633 * Creates another scope for the "try" block itself.
8634 * TRY instruction sets up exception handling at runtime.
8635 *
8636 * "try"
8637 * TRY -> catch1, -> finally push trystack entry
8638 * ... try block
8639 * "throw {exception}"
8640 * EVAL {exception}
8641 * THROW create exception
8642 * ... try block
8643 * " catch {expr}"
8644 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008645 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008646 * EVAL {expr}
8647 * MATCH
8648 * JUMP nomatch -> catch2
8649 * CATCH remove exception
8650 * ... catch block
8651 * " catch"
8652 * JUMP -> finally
8653 * catch2: CATCH remove exception
8654 * ... catch block
8655 * " finally"
8656 * finally:
8657 * ... finally block
8658 * " endtry"
8659 * ENDTRY pop trystack entry, may rethrow
8660 */
8661 static char_u *
8662compile_try(char_u *arg, cctx_T *cctx)
8663{
8664 garray_T *instr = &cctx->ctx_instr;
8665 scope_T *try_scope;
8666 scope_T *scope;
8667
Bram Moolenaarfa984412021-03-25 22:15:28 +01008668 if (misplaced_cmdmod(cctx))
8669 return NULL;
8670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008671 // scope that holds the jumps that go to catch/finally/endtry
8672 try_scope = new_scope(cctx, TRY_SCOPE);
8673 if (try_scope == NULL)
8674 return NULL;
8675
Bram Moolenaar69f70502021-01-01 16:10:46 +01008676 if (cctx->ctx_skip != SKIP_YES)
8677 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008678 isn_T *isn;
8679
8680 // "try_catch" is set when the first ":catch" is found or when no catch
8681 // is found and ":finally" is found.
8682 // "try_finally" is set when ":finally" is found
8683 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008684 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008685 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8686 return NULL;
8687 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8688 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008689 return NULL;
8690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008691
8692 // scope for the try block itself
8693 scope = new_scope(cctx, BLOCK_SCOPE);
8694 if (scope == NULL)
8695 return NULL;
8696
8697 return arg;
8698}
8699
8700/*
8701 * compile "catch {expr}"
8702 */
8703 static char_u *
8704compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8705{
8706 scope_T *scope = cctx->ctx_scope;
8707 garray_T *instr = &cctx->ctx_instr;
8708 char_u *p;
8709 isn_T *isn;
8710
Bram Moolenaarfa984412021-03-25 22:15:28 +01008711 if (misplaced_cmdmod(cctx))
8712 return NULL;
8713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008714 // end block scope from :try or :catch
8715 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8716 compile_endblock(cctx);
8717 scope = cctx->ctx_scope;
8718
8719 // Error if not in a :try scope
8720 if (scope == NULL || scope->se_type != TRY_SCOPE)
8721 {
8722 emsg(_(e_catch));
8723 return NULL;
8724 }
8725
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008726 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008727 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008728 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008729 return NULL;
8730 }
8731
Bram Moolenaar69f70502021-01-01 16:10:46 +01008732 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008733 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008734#ifdef FEAT_PROFILE
8735 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008736 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008737 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008738 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008739 .isn_type == ISN_PROF_START)
8740 --instr->ga_len;
8741#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008742 // Jump from end of previous block to :finally or :endtry
8743 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8744 JUMP_ALWAYS, cctx) == FAIL)
8745 return NULL;
8746
8747 // End :try or :catch scope: set value in ISN_TRY instruction
8748 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008749 if (isn->isn_arg.try.try_ref->try_catch == 0)
8750 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008751 if (scope->se_u.se_try.ts_catch_label != 0)
8752 {
8753 // Previous catch without match jumps here
8754 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8755 isn->isn_arg.jump.jump_where = instr->ga_len;
8756 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008757#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008758 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008759 {
8760 // a "throw" that jumps here needs to be counted
8761 generate_instr(cctx, ISN_PROF_END);
8762 // the "catch" is also counted
8763 generate_instr(cctx, ISN_PROF_START);
8764 }
8765#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008766 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008767 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008768 }
8769
8770 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008771 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008772 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008773 scope->se_u.se_try.ts_caught_all = TRUE;
8774 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008775 }
8776 else
8777 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008778 char_u *end;
8779 char_u *pat;
8780 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008781 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008782 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008784 // Push v:exception, push {expr} and MATCH
8785 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8786
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008787 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008788 if (*end != *p)
8789 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008790 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008791 vim_free(tofree);
8792 return FAIL;
8793 }
8794 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008795 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008796 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008797 len = (int)(end - tofree);
8798 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008799 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008800 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008801 if (pat == NULL)
8802 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008803 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008804 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008806 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8807 return NULL;
8808
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008809 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008810 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8811 return NULL;
8812 }
8813
Bram Moolenaar69f70502021-01-01 16:10:46 +01008814 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008815 return NULL;
8816
8817 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8818 return NULL;
8819 return p;
8820}
8821
8822 static char_u *
8823compile_finally(char_u *arg, cctx_T *cctx)
8824{
8825 scope_T *scope = cctx->ctx_scope;
8826 garray_T *instr = &cctx->ctx_instr;
8827 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008828 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008829
Bram Moolenaarfa984412021-03-25 22:15:28 +01008830 if (misplaced_cmdmod(cctx))
8831 return NULL;
8832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008833 // end block scope from :try or :catch
8834 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8835 compile_endblock(cctx);
8836 scope = cctx->ctx_scope;
8837
8838 // Error if not in a :try scope
8839 if (scope == NULL || scope->se_type != TRY_SCOPE)
8840 {
8841 emsg(_(e_finally));
8842 return NULL;
8843 }
8844
rbtnn84934992021-08-07 13:26:53 +02008845 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008846 {
rbtnn84934992021-08-07 13:26:53 +02008847 // End :catch or :finally scope: set value in ISN_TRY instruction
8848 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8849 if (isn->isn_arg.try.try_ref->try_finally != 0)
8850 {
8851 emsg(_(e_finally_dup));
8852 return NULL;
8853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008854
rbtnn84934992021-08-07 13:26:53 +02008855 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008856#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008857 if (cctx->ctx_compile_type == CT_PROFILE
8858 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008859 .isn_type == ISN_PROF_START)
rbtnn84934992021-08-07 13:26:53 +02008860 {
8861 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008862 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008863
8864 // jump to the profile end above it
8865 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008866 .isn_type == ISN_PROF_END)
rbtnn84934992021-08-07 13:26:53 +02008867 --this_instr;
8868 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008869#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008870
rbtnn84934992021-08-07 13:26:53 +02008871 // Fill in the "end" label in jumps at the end of the blocks.
8872 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarfad27422021-11-30 21:58:19 +00008873 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008874
rbtnn84934992021-08-07 13:26:53 +02008875 // If there is no :catch then an exception jumps to :finally.
8876 if (isn->isn_arg.try.try_ref->try_catch == 0)
8877 isn->isn_arg.try.try_ref->try_catch = this_instr;
8878 isn->isn_arg.try.try_ref->try_finally = this_instr;
8879 if (scope->se_u.se_try.ts_catch_label != 0)
8880 {
8881 // Previous catch without match jumps here
8882 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8883 isn->isn_arg.jump.jump_where = this_instr;
8884 scope->se_u.se_try.ts_catch_label = 0;
8885 }
8886 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8887 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008889
8890 return arg;
8891}
8892
8893 static char_u *
8894compile_endtry(char_u *arg, cctx_T *cctx)
8895{
8896 scope_T *scope = cctx->ctx_scope;
8897 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008898 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008899
Bram Moolenaarfa984412021-03-25 22:15:28 +01008900 if (misplaced_cmdmod(cctx))
8901 return NULL;
8902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008903 // end block scope from :catch or :finally
8904 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8905 compile_endblock(cctx);
8906 scope = cctx->ctx_scope;
8907
8908 // Error if not in a :try scope
8909 if (scope == NULL || scope->se_type != TRY_SCOPE)
8910 {
8911 if (scope == NULL)
8912 emsg(_(e_no_endtry));
8913 else if (scope->se_type == WHILE_SCOPE)
8914 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008915 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008916 emsg(_(e_endfor));
8917 else
8918 emsg(_(e_endif));
8919 return NULL;
8920 }
8921
Bram Moolenaarc150c092021-02-13 15:02:46 +01008922 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008923 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008924 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008925 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8926 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008927 {
8928 emsg(_(e_missing_catch_or_finally));
8929 return NULL;
8930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008931
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008932#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008933 if (cctx->ctx_compile_type == CT_PROFILE
8934 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008935 .isn_type == ISN_PROF_START)
8936 // move the profile start after "endtry" so that it's not counted when
8937 // the exception is rethrown.
8938 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008939#endif
8940
Bram Moolenaar69f70502021-01-01 16:10:46 +01008941 // Fill in the "end" label in jumps at the end of the blocks, if not
8942 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008943 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8944 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008945
Bram Moolenaar69f70502021-01-01 16:10:46 +01008946 if (scope->se_u.se_try.ts_catch_label != 0)
8947 {
8948 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008949 isn_T *isn = ((isn_T *)instr->ga_data)
8950 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008951 isn->isn_arg.jump.jump_where = instr->ga_len;
8952 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008953 }
8954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008955 compile_endblock(cctx);
8956
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008957 if (cctx->ctx_skip != SKIP_YES)
8958 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008959 // End :catch or :finally scope: set instruction index in ISN_TRY
8960 // instruction
8961 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008962 if (cctx->ctx_skip != SKIP_YES
8963 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8964 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008965#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008966 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008967 generate_instr(cctx, ISN_PROF_START);
8968#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008970 return arg;
8971}
8972
8973/*
8974 * compile "throw {expr}"
8975 */
8976 static char_u *
8977compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8978{
8979 char_u *p = skipwhite(arg);
8980
Bram Moolenaara5565e42020-05-09 15:44:01 +02008981 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008982 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008983 if (cctx->ctx_skip == SKIP_YES)
8984 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008985 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008986 return NULL;
8987 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8988 return NULL;
8989
8990 return p;
8991}
8992
Bram Moolenaarc3235272021-07-10 19:42:03 +02008993 static char_u *
8994compile_eval(char_u *arg, cctx_T *cctx)
8995{
8996 char_u *p = arg;
8997 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02008998 long lnum = SOURCING_LNUM;
8999
9000 // find_ex_command() will consider a variable name an expression, assuming
9001 // that something follows on the next line. Check that something actually
9002 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02009003 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02009004
Bram Moolenaarc3235272021-07-10 19:42:03 +02009005 if (compile_expr0(&p, cctx) == FAIL)
9006 return NULL;
9007
9008 if (name_only && lnum == SOURCING_LNUM)
9009 {
9010 semsg(_(e_expression_without_effect_str), arg);
9011 return NULL;
9012 }
9013
9014 // drop the result
9015 generate_instr_drop(cctx, ISN_DROP, 1);
9016
9017 return skipwhite(p);
9018}
9019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009020/*
9021 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009022 * compile "echomsg expr"
9023 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02009024 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01009025 * compile "execute expr"
9026 */
9027 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009028compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009029{
9030 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01009031 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009032 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009033 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009034 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009035 garray_T *stack = &cctx->ctx_type_stack;
9036 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009037
9038 for (;;)
9039 {
Bram Moolenaare4984292020-12-13 14:19:25 +01009040 if (ends_excmd2(prev, p))
9041 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009042 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009043 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009044 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009045
9046 if (cctx->ctx_skip != SKIP_YES)
9047 {
9048 // check for non-void type
9049 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
9050 if (type->tt_type == VAR_VOID)
9051 {
9052 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
9053 return NULL;
9054 }
9055 }
9056
Bram Moolenaarad39c092020-02-26 18:23:43 +01009057 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02009058 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009059 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009060 }
9061
Bram Moolenaare4984292020-12-13 14:19:25 +01009062 if (count > 0)
9063 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009064 long save_lnum = cctx->ctx_lnum;
9065
9066 // Use the line number where the command started.
9067 cctx->ctx_lnum = start_ctx_lnum;
9068
Bram Moolenaare4984292020-12-13 14:19:25 +01009069 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
9070 generate_ECHO(cctx, cmdidx == CMD_echo, count);
9071 else if (cmdidx == CMD_execute)
9072 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
9073 else if (cmdidx == CMD_echomsg)
9074 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02009075 else if (cmdidx == CMD_echoconsole)
9076 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01009077 else
9078 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009079
9080 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01009081 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009082 return p;
9083}
9084
9085/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01009086 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01009087 * instruction to compute it and return OK.
9088 * Otherwise return FAIL, the caller must deal with any range.
9089 */
9090 static int
9091compile_variable_range(exarg_T *eap, cctx_T *cctx)
9092{
9093 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
9094 char_u *p = skipdigits(eap->cmd);
9095
9096 if (p == range_end)
9097 return FAIL;
9098 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
9099}
9100
9101/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009102 * :put r
9103 * :put ={expr}
9104 */
9105 static char_u *
9106compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
9107{
9108 char_u *line = arg;
9109 linenr_T lnum;
9110 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009111 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009112
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009113 eap->regname = *line;
9114
9115 if (eap->regname == '=')
9116 {
9117 char_u *p = line + 1;
9118
9119 if (compile_expr0(&p, cctx) == FAIL)
9120 return NULL;
9121 line = p;
9122 }
9123 else if (eap->regname != NUL)
9124 ++line;
9125
Bram Moolenaar08597872020-12-10 19:43:40 +01009126 if (compile_variable_range(eap, cctx) == OK)
9127 {
9128 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
9129 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009130 else
Bram Moolenaar08597872020-12-10 19:43:40 +01009131 {
9132 // Either no range or a number.
9133 // "errormsg" will not be set because the range is ADDR_LINES.
9134 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01009135 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01009136 return NULL;
9137 if (eap->addr_count == 0)
9138 lnum = -1;
9139 else
9140 lnum = eap->line2;
9141 if (above)
9142 --lnum;
9143 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009144
9145 generate_PUT(cctx, eap->regname, lnum);
9146 return line;
9147}
9148
9149/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009150 * A command that is not compiled, execute with legacy code.
9151 */
9152 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009153compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009154{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009155 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009156 char_u *p;
9157 int has_expr = FALSE;
9158 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009159 char_u *tofree = NULL;
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009160 char_u *cmd_arg = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009161
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009162 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009163 goto theend;
9164
Bram Moolenaare729ce22021-06-06 21:38:09 +02009165 // If there was a prececing command modifier, drop it and include it in the
9166 // EXEC command.
9167 if (cctx->ctx_has_cmdmod)
9168 {
9169 garray_T *instr = &cctx->ctx_instr;
9170 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9171
9172 if (isn->isn_type == ISN_CMDMOD)
9173 {
9174 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9175 ->cmod_filter_regmatch.regprog);
9176 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9177 --instr->ga_len;
9178 cctx->ctx_has_cmdmod = FALSE;
9179 }
9180 }
9181
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009182 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009183 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009184 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009185 int usefilter = FALSE;
9186
9187 has_expr = argt & (EX_XFILE | EX_EXPAND);
9188
9189 // If the command can be followed by a bar, find the bar and truncate
9190 // it, so that the following command can be compiled.
9191 // The '|' is overwritten with a NUL, it is put back below.
9192 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9193 && *eap->arg == '!')
9194 // :w !filter or :r !filter or :r! filter
9195 usefilter = TRUE;
9196 if ((argt & EX_TRLBAR) && !usefilter)
9197 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009198 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009199 separate_nextcmd(eap);
9200 if (eap->nextcmd != NULL)
9201 nextcmd = eap->nextcmd;
9202 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009203 else if (eap->cmdidx == CMD_wincmd)
9204 {
9205 p = eap->arg;
9206 if (*p != NUL)
9207 ++p;
9208 if (*p == 'g' || *p == Ctrl_G)
9209 ++p;
9210 p = skipwhite(p);
9211 if (*p == '|')
9212 {
9213 *p = NUL;
9214 nextcmd = p + 1;
9215 }
9216 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009217 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9218 {
9219 // If there is a trailing '{' read lines until the '}'
9220 p = eap->arg + STRLEN(eap->arg) - 1;
9221 while (p > eap->arg && VIM_ISWHITE(*p))
9222 --p;
9223 if (*p == '{')
9224 {
9225 exarg_T ea;
9226 int flags; // unused
9227 int start_lnum = SOURCING_LNUM;
9228
9229 CLEAR_FIELD(ea);
9230 ea.arg = eap->arg;
9231 fill_exarg_from_cctx(&ea, cctx);
9232 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9233 if (tofree != NULL)
9234 {
9235 *p = NUL;
9236 line = concat_str(line, tofree);
9237 if (line == NULL)
9238 goto theend;
9239 vim_free(tofree);
9240 tofree = line;
9241 SOURCING_LNUM = start_lnum;
9242 }
9243 }
9244 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009245 }
9246
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009247 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9248 {
9249 // expand filename in "syntax include [@group] filename"
9250 has_expr = TRUE;
9251 eap->arg = skipwhite(eap->arg + 7);
9252 if (*eap->arg == '@')
9253 eap->arg = skiptowhite(eap->arg);
9254 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009255
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009256 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9257 && STRLEN(eap->arg) > 4)
9258 {
9259 int delim = *eap->arg;
9260
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009261 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009262 if (*p == delim)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009263 cmd_arg = p + 1;
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009264 }
9265
Bram Moolenaarecac5912021-01-05 19:23:28 +01009266 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009267 cmd_arg = eap->arg;
9268
9269 if (cmd_arg != NULL)
Bram Moolenaarecac5912021-01-05 19:23:28 +01009270 {
Bram Moolenaarfad27422021-11-30 21:58:19 +00009271 exarg_T nea;
9272
9273 CLEAR_FIELD(nea);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009274 nea.cmd = cmd_arg;
Bram Moolenaarfad27422021-11-30 21:58:19 +00009275 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009276 if (nea.cmdidx < CMD_SIZE)
Bram Moolenaarfad27422021-11-30 21:58:19 +00009277 {
9278 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
9279 if (has_expr)
9280 eap->arg = skiptowhite(eap->arg);
9281 }
Bram Moolenaarecac5912021-01-05 19:23:28 +01009282 }
9283
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009284 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009285 {
9286 int count = 0;
9287 char_u *start = skipwhite(line);
9288
9289 // :cmd xxx`=expr1`yyy`=expr2`zzz
9290 // PUSHS ":cmd xxx"
9291 // eval expr1
9292 // PUSHS "yyy"
9293 // eval expr2
9294 // PUSHS "zzz"
9295 // EXECCONCAT 5
9296 for (;;)
9297 {
9298 if (p > start)
9299 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009300 char_u *val = vim_strnsave(start, p - start);
9301
9302 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009303 ++count;
9304 }
9305 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009306 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009307 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009308 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009309 ++count;
9310 p = skipwhite(p);
9311 if (*p != '`')
9312 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009313 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009314 return NULL;
9315 }
9316 start = p + 1;
9317
9318 p = (char_u *)strstr((char *)start, "`=");
9319 if (p == NULL)
9320 {
9321 if (*skipwhite(start) != NUL)
9322 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009323 char_u *val = vim_strsave(start);
9324
9325 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009326 ++count;
9327 }
9328 break;
9329 }
9330 }
9331 generate_EXECCONCAT(cctx, count);
9332 }
9333 else
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009334 generate_EXEC_copy(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009335
9336theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009337 if (*nextcmd != NUL)
9338 {
9339 // the parser expects a pointer to the bar, put it back
9340 --nextcmd;
9341 *nextcmd = '|';
9342 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009343 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009344
9345 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009346}
9347
Bram Moolenaar20677332021-06-06 17:02:53 +02009348/*
9349 * A script command with heredoc, e.g.
9350 * ruby << EOF
9351 * command
9352 * EOF
9353 * Has been turned into one long line with NL characters by
9354 * get_function_body():
9355 * ruby << EOF<NL> command<NL>EOF
9356 */
9357 static char_u *
9358compile_script(char_u *line, cctx_T *cctx)
9359{
9360 if (cctx->ctx_skip != SKIP_YES)
9361 {
9362 isn_T *isn;
9363
9364 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9365 return NULL;
9366 isn->isn_arg.string = vim_strsave(line);
9367 }
9368 return (char_u *)"";
9369}
9370
Bram Moolenaar8238f082021-04-20 21:10:48 +02009371
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009372/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009373 * :s/pat/repl/
9374 */
9375 static char_u *
9376compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9377{
9378 char_u *cmd = eap->arg;
9379 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9380
9381 if (expr != NULL)
9382 {
9383 int delimiter = *cmd++;
9384
9385 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009386 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009387 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9388 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009389 garray_T save_ga = cctx->ctx_instr;
9390 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009391 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009392 int trailing_error;
9393 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009394 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009395 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009396
9397 cmd += 3;
9398 end = skip_substitute(cmd, delimiter);
9399
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009400 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009401 // labels are correct.
9402 cctx->ctx_instr.ga_len = 0;
9403 cctx->ctx_instr.ga_maxlen = 0;
9404 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009405 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009406 if (end[-1] == NUL)
9407 end[-1] = delimiter;
9408 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009409 trailing_error = *cmd != delimiter && *cmd != NUL;
9410
Bram Moolenaar169502f2021-04-21 12:19:35 +02009411 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009412 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009413 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009414 if (trailing_error)
9415 semsg(_(e_trailing_arg), cmd);
9416 clear_instr_ga(&cctx->ctx_instr);
9417 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009418 return NULL;
9419 }
9420
Bram Moolenaar8238f082021-04-20 21:10:48 +02009421 // Move the generated instructions into the ISN_SUBSTITUTE
9422 // instructions, then restore the list of instructions before
9423 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009424 instr_count = cctx->ctx_instr.ga_len;
9425 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009426 instr[instr_count].isn_type = ISN_FINISH;
9427
9428 cctx->ctx_instr = save_ga;
9429 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9430 {
9431 int idx;
9432
9433 for (idx = 0; idx < instr_count; ++idx)
9434 delete_instr(instr + idx);
9435 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009436 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009437 }
9438 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9439 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009440
9441 // skip over flags
9442 if (*end == '&')
9443 ++end;
9444 while (ASCII_ISALPHA(*end) || *end == '#')
9445 ++end;
9446 return end;
9447 }
9448 }
9449
9450 return compile_exec(arg, eap, cctx);
9451}
9452
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009453 static char_u *
9454compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9455{
9456 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009457 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009458
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009459 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009460 {
9461 if (STRNCMP(arg, "END", 3) == 0)
9462 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009463 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009464 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009465 // First load the current variable value.
9466 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9467 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009468 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009469 }
9470
9471 // Gets the redirected text and put it on the stack, then store it
9472 // in the variable.
9473 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9474
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009475 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009476 generate_instr_drop(cctx, ISN_CONCAT, 1);
9477
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009478 if (lhs->lhs_has_index)
9479 {
9480 // Use the info in "lhs" to store the value at the index in the
9481 // list or dict.
9482 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9483 &t_string, cctx) == FAIL)
9484 return NULL;
9485 }
9486 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009487 return NULL;
9488
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009489 VIM_CLEAR(lhs->lhs_name);
9490 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009491 return arg + 3;
9492 }
9493 emsg(_(e_cannot_nest_redir));
9494 return NULL;
9495 }
9496
9497 if (arg[0] == '=' && arg[1] == '>')
9498 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009499 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009500
9501 // redirect to a variable is compiled
9502 arg += 2;
9503 if (*arg == '>')
9504 {
9505 ++arg;
9506 append = TRUE;
9507 }
9508 arg = skipwhite(arg);
9509
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009510 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009511 FALSE, FALSE, 1, cctx) == FAIL)
9512 return NULL;
9513 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009514 lhs->lhs_append = append;
9515 if (lhs->lhs_has_index)
9516 {
9517 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9518 if (lhs->lhs_whole == NULL)
9519 return NULL;
9520 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009521
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009522 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009523 }
9524
9525 // other redirects are handled like at script level
9526 return compile_exec(line, eap, cctx);
9527}
9528
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009529#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009530 static char_u *
9531compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9532{
9533 isn_T *isn;
9534 char_u *p;
9535
9536 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9537 if (isn == NULL)
9538 return NULL;
9539 isn->isn_arg.number = eap->cmdidx;
9540
9541 p = eap->arg;
9542 if (compile_expr0(&p, cctx) == FAIL)
9543 return NULL;
9544
9545 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9546 if (isn == NULL)
9547 return NULL;
9548 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9549 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9550 return NULL;
9551 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9552 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9553 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9554
9555 return p;
9556}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009557#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009558
Bram Moolenaar4c137212021-04-19 16:48:48 +02009559/*
Bram Moolenaar7b829262021-10-13 15:04:34 +01009560 * Check if the separator for a :global or :substitute command is OK.
9561 */
9562 int
9563check_global_and_subst(char_u *cmd, char_u *arg)
9564{
Bram Moolenaar7f320922021-10-13 15:28:28 +01009565 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
Bram Moolenaar7b829262021-10-13 15:04:34 +01009566 {
9567 semsg(_(e_separator_not_supported_str), arg);
9568 return FAIL;
9569 }
9570 if (VIM_ISWHITE(cmd[1]))
9571 {
9572 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
9573 return FAIL;
9574 }
9575 return OK;
9576}
9577
9578
9579/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009580 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009581 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009582 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009583 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009584add_def_function(ufunc_T *ufunc)
9585{
9586 dfunc_T *dfunc;
9587
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009588 if (def_functions.ga_len == 0)
9589 {
9590 // The first position is not used, so that a zero uf_dfunc_idx means it
9591 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009592 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009593 return FAIL;
9594 ++def_functions.ga_len;
9595 }
9596
Bram Moolenaar09689a02020-05-09 22:50:08 +02009597 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009598 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009599 return FAIL;
9600 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9601 CLEAR_POINTER(dfunc);
9602 dfunc->df_idx = def_functions.ga_len;
9603 ufunc->uf_dfunc_idx = dfunc->df_idx;
9604 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009605 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009606 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009607 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009608 ++def_functions.ga_len;
9609 return OK;
9610}
9611
9612/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009613 * After ex_function() has collected all the function lines: parse and compile
9614 * the lines into instructions.
9615 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009616 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9617 * the return statement (used for lambda). When uf_ret_type is already set
9618 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009619 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009620 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009621 * This can be used recursively through compile_lambda(), which may reallocate
9622 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009623 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009624 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009625 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009626compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009627 ufunc_T *ufunc,
9628 int check_return_type,
9629 compiletype_T compile_type,
9630 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009631{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009632 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009633 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009634 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009635 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009636 cctx_T cctx;
9637 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009638 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009639 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009640 int ret = FAIL;
9641 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009642 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009643 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009644 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009645 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009646#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009647 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009648#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009649 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009650
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009651 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009652 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009653 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009654 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009655 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9656 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009657 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009658
9659 switch (compile_type)
9660 {
9661 case CT_PROFILE:
9662#ifdef FEAT_PROFILE
9663 instr_dest = dfunc->df_instr_prof; break;
9664#endif
9665 case CT_NONE: instr_dest = dfunc->df_instr; break;
9666 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9667 }
9668 if (instr_dest != NULL)
9669 // Was compiled in this mode before: Free old instructions.
9670 delete_def_function_contents(dfunc, FALSE);
9671 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009672 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009673 else
9674 {
9675 if (add_def_function(ufunc) == FAIL)
9676 return FAIL;
9677 new_def_function = TRUE;
9678 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009679
Bram Moolenaar985116a2020-07-12 17:31:09 +02009680 ufunc->uf_def_status = UF_COMPILING;
9681
Bram Moolenaara80faa82020-04-12 19:37:17 +02009682 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009683
Bram Moolenaare99d4222021-06-13 14:01:26 +02009684 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009685 cctx.ctx_ufunc = ufunc;
9686 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009687 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009688 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9689 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9690 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9691 cctx.ctx_type_list = &ufunc->uf_type_list;
9692 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9693 instr = &cctx.ctx_instr;
9694
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009695 // Set the context to the function, it may be compiled when called from
9696 // another script. Set the script version to the most modern one.
9697 // The line number will be set in next_line_from_context().
9698 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009699 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9700
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009701 // Don't use the flag from ":legacy" here.
9702 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9703
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009704 // Make sure error messages are OK.
9705 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9706 if (do_estack_push)
9707 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009708 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009709
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009710 if (ufunc->uf_def_args.ga_len > 0)
9711 {
9712 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009713 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009714 int i;
9715 char_u *arg;
9716 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009717 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009718
9719 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009720 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009721 for (i = 0; i < count; ++i)
9722 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009723 garray_T *stack = &cctx.ctx_type_stack;
9724 type_T *val_type;
9725 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009726 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009727 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009728 int jump_instr_idx = instr->ga_len;
9729 isn_T *isn;
9730
9731 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9732 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9733 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009734
9735 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009736 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009737
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009738 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009739 r = compile_expr0(&arg, &cctx);
9740
Bram Moolenaar12bce952021-03-11 20:04:04 +01009741 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009742 goto erret;
9743
9744 // If no type specified use the type of the default value.
9745 // Otherwise check that the default value type matches the
9746 // specified type.
9747 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009748 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009749 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009750 {
9751 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009752 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009753 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009754 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009755 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009756 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009757
9758 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009759 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009760
9761 // set instruction index in JUMP_IF_ARG_SET to here
9762 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9763 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009764 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009765
9766 if (did_set_arg_type)
9767 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009768 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009769 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009770
9771 /*
9772 * Loop over all the lines of the function and generate instructions.
9773 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009774 for (;;)
9775 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009776 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009777 int starts_with_colon = FALSE;
9778 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009779 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009780
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009781 // Bail out on the first error to avoid a flood of errors and report
9782 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009783 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009784 goto erret;
9785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009786 if (line != NULL && *line == '|')
9787 // the line continues after a '|'
9788 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009789 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009790 && !(*line == '#' && (line == cctx.ctx_line_start
9791 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009792 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009793 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009794 goto erret;
9795 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009796 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9797 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009798 else
9799 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009800 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009801 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009802 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009803 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009804#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009805 if (cctx.ctx_skip != SKIP_YES)
9806 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009807#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009808 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009809 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009810 // Make a copy, splitting off nextcmd and removing trailing spaces
9811 // may change it.
9812 if (line != NULL)
9813 {
9814 line = vim_strsave(line);
9815 vim_free(line_to_free);
9816 line_to_free = line;
9817 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009818 }
9819
Bram Moolenaara80faa82020-04-12 19:37:17 +02009820 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009821 ea.cmdlinep = &line;
9822 ea.cmd = skipwhite(line);
9823
Bram Moolenaarb2049902021-01-24 12:53:53 +01009824 if (*ea.cmd == '#')
9825 {
9826 // "#" starts a comment
9827 line = (char_u *)"";
9828 continue;
9829 }
9830
Bram Moolenaarf002a412021-01-24 13:34:18 +01009831#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009832 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9833 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009834 {
9835 may_generate_prof_end(&cctx, prof_lnum);
9836
9837 prof_lnum = cctx.ctx_lnum;
9838 generate_instr(&cctx, ISN_PROF_START);
9839 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009840#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009841 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9842 && cctx.ctx_skip != SKIP_YES)
9843 {
9844 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009845 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009846 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009847 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009848
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009849 // Some things can be recognized by the first character.
9850 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009851 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009852 case '}':
9853 {
9854 // "}" ends a block scope
9855 scopetype_T stype = cctx.ctx_scope == NULL
9856 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009857
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009858 if (stype == BLOCK_SCOPE)
9859 {
9860 compile_endblock(&cctx);
9861 line = ea.cmd;
9862 }
9863 else
9864 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009865 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009866 goto erret;
9867 }
9868 if (line != NULL)
9869 line = skipwhite(ea.cmd + 1);
9870 continue;
9871 }
9872
9873 case '{':
9874 // "{" starts a block scope
9875 // "{'a': 1}->func() is something else
9876 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9877 {
9878 line = compile_block(ea.cmd, &cctx);
9879 continue;
9880 }
9881 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009882 }
9883
9884 /*
9885 * COMMAND MODIFIERS
9886 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009887 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009888 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9889 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009890 {
9891 if (errormsg != NULL)
9892 goto erret;
9893 // empty line or comment
9894 line = (char_u *)"";
9895 continue;
9896 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009897 generate_cmdmods(&cctx, &local_cmdmod);
9898 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009899
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009900 // Check if there was a colon after the last command modifier or before
9901 // the current position.
9902 for (p = ea.cmd; p >= line; --p)
9903 {
9904 if (*p == ':')
9905 starts_with_colon = TRUE;
9906 if (p < ea.cmd && !VIM_ISWHITE(*p))
9907 break;
9908 }
9909
Bram Moolenaarce024c32021-06-26 13:00:49 +02009910 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009911 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009912 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009913 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009914 if (checkforcmd(&ea.cmd, "call", 3))
9915 {
9916 if (*ea.cmd == '(')
9917 // not for "call()"
9918 ea.cmd = p;
9919 else
9920 ea.cmd = skipwhite(ea.cmd);
9921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009922
Bram Moolenaarce024c32021-06-26 13:00:49 +02009923 if (!starts_with_colon)
9924 {
9925 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009926
Bram Moolenaarce024c32021-06-26 13:00:49 +02009927 // Check for assignment after command modifiers.
9928 assign = may_compile_assignment(&ea, &line, &cctx);
9929 if (assign == OK)
9930 goto nextline;
9931 if (assign == FAIL)
9932 goto erret;
9933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009934 }
9935
9936 /*
9937 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009938 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009939 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009940 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009941 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009942 cmd = ea.cmd;
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009943 if ((*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009944 && (starts_with_colon || !(*cmd == '\''
9945 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009946 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009947 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009948 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009949 {
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009950 if (!starts_with_colon
9951 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009952 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009953 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009954 goto erret;
9955 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009956 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009957 if (ends_excmd2(line, ea.cmd))
9958 {
9959 // A range without a command: jump to the line.
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009960 generate_EXEC(&cctx, ISN_EXECRANGE,
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009961 vim_strnsave(cmd, ea.cmd - cmd));
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009962 line = ea.cmd;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009963 goto nextline;
9964 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009965 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009966 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009967 p = find_ex_command(&ea, NULL,
9968 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009969 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009970
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009971 if (p == NULL)
9972 {
9973 if (cctx.ctx_skip != SKIP_YES)
9974 emsg(_(e_ambiguous_use_of_user_defined_command));
9975 goto erret;
9976 }
9977
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009978 // When using ":legacy cmd" always use compile_exec().
9979 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009980 {
9981 char_u *start = ea.cmd;
9982
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009983 switch (ea.cmdidx)
9984 {
9985 case CMD_if:
9986 case CMD_elseif:
9987 case CMD_else:
9988 case CMD_endif:
9989 case CMD_for:
9990 case CMD_endfor:
9991 case CMD_continue:
9992 case CMD_break:
9993 case CMD_while:
9994 case CMD_endwhile:
9995 case CMD_try:
9996 case CMD_catch:
9997 case CMD_finally:
9998 case CMD_endtry:
9999 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
10000 goto erret;
10001 default: break;
10002 }
10003
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010004 // ":legacy return expr" needs to be handled differently.
10005 if (checkforcmd(&start, "return", 4))
10006 ea.cmdidx = CMD_return;
10007 else
10008 ea.cmdidx = CMD_legacy;
10009 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +020010010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010011 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
10012 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +020010013 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010014 {
10015 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +010010016 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010017 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +020010018 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010019 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +010010020 // CMD_var cannot happen, compile_assignment() above would be
10021 // used. Most likely an assignment to a non-existing variable.
10022 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010023 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010024 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010025 }
10026
Bram Moolenaar3988f642020-08-27 22:43:03 +020010027 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010028 && ea.cmdidx != CMD_elseif
10029 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +020010030 && ea.cmdidx != CMD_endif
10031 && ea.cmdidx != CMD_endfor
10032 && ea.cmdidx != CMD_endwhile
10033 && ea.cmdidx != CMD_catch
10034 && ea.cmdidx != CMD_finally
10035 && ea.cmdidx != CMD_endtry)
10036 {
Bram Moolenaar3988f642020-08-27 22:43:03 +020010037 emsg(_(e_unreachable_code_after_return));
10038 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +020010039 }
10040
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010041 p = skipwhite(p);
10042 if (ea.cmdidx != CMD_SIZE
10043 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
10044 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +020010045 if (ea.cmdidx >= 0)
10046 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010047 if ((ea.argt & EX_BANG) && *p == '!')
10048 {
10049 ea.forceit = TRUE;
10050 p = skipwhite(p + 1);
10051 }
10052 }
10053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010054 switch (ea.cmdidx)
10055 {
10056 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +000010057 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +020010058 ea.arg = p;
10059 line = compile_nested_function(&ea, &cctx);
10060 break;
10061
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010062 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010063 line = compile_return(p, check_return_type,
10064 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010065 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010066 break;
10067
10068 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +020010069 emsg(_(e_cannot_use_let_in_vim9_script));
10070 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +020010071 case CMD_var:
10072 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010073 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +020010074 case CMD_increment:
10075 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010076 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +020010077 if (line == p)
10078 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010079 break;
10080
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010081 case CMD_unlet:
10082 case CMD_unlockvar:
10083 case CMD_lockvar:
10084 line = compile_unletlock(p, &ea, &cctx);
10085 break;
10086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010087 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +020010088 emsg(_(e_import_can_only_be_used_in_script));
10089 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010090 break;
10091
10092 case CMD_if:
10093 line = compile_if(p, &cctx);
10094 break;
10095 case CMD_elseif:
10096 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010097 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010098 break;
10099 case CMD_else:
10100 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010101 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010102 break;
10103 case CMD_endif:
10104 line = compile_endif(p, &cctx);
10105 break;
10106
10107 case CMD_while:
10108 line = compile_while(p, &cctx);
10109 break;
10110 case CMD_endwhile:
10111 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010112 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010113 break;
10114
10115 case CMD_for:
10116 line = compile_for(p, &cctx);
10117 break;
10118 case CMD_endfor:
10119 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010120 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010121 break;
10122 case CMD_continue:
10123 line = compile_continue(p, &cctx);
10124 break;
10125 case CMD_break:
10126 line = compile_break(p, &cctx);
10127 break;
10128
10129 case CMD_try:
10130 line = compile_try(p, &cctx);
10131 break;
10132 case CMD_catch:
10133 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010134 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010135 break;
10136 case CMD_finally:
10137 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010138 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010139 break;
10140 case CMD_endtry:
10141 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010142 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010143 break;
10144 case CMD_throw:
10145 line = compile_throw(p, &cctx);
10146 break;
10147
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010148 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +020010149 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010150 break;
10151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010152 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010153 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +010010154 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010155 case CMD_echomsg:
10156 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010157 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010158 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +010010159 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010160
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010161 case CMD_put:
10162 ea.cmd = cmd;
10163 line = compile_put(p, &ea, &cctx);
10164 break;
10165
Bram Moolenaar4c137212021-04-19 16:48:48 +020010166 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +010010167 if (check_global_and_subst(ea.cmd, p) == FAIL)
10168 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +020010169 if (cctx.ctx_skip == SKIP_YES)
10170 line = (char_u *)"";
10171 else
10172 {
10173 ea.arg = p;
10174 line = compile_substitute(line, &ea, &cctx);
10175 }
10176 break;
10177
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010178 case CMD_redir:
10179 ea.arg = p;
10180 line = compile_redir(line, &ea, &cctx);
10181 break;
10182
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010183 case CMD_cexpr:
10184 case CMD_lexpr:
10185 case CMD_caddexpr:
10186 case CMD_laddexpr:
10187 case CMD_cgetexpr:
10188 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010189#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010190 ea.arg = p;
10191 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010192#else
10193 ex_ni(&ea);
10194 line = NULL;
10195#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010196 break;
10197
Bram Moolenaarae616492020-07-28 20:07:27 +020010198 case CMD_append:
10199 case CMD_change:
10200 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010201 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010202 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010203 case CMD_xit:
10204 not_in_vim9(&ea);
10205 goto erret;
10206
Bram Moolenaar002262f2020-07-08 17:47:57 +020010207 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010208 if (cctx.ctx_skip != SKIP_YES)
10209 {
10210 semsg(_(e_invalid_command_str), ea.cmd);
10211 goto erret;
10212 }
10213 // We don't check for a next command here.
10214 line = (char_u *)"";
10215 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010216
Bram Moolenaar20677332021-06-06 17:02:53 +020010217 case CMD_lua:
10218 case CMD_mzscheme:
10219 case CMD_perl:
10220 case CMD_py3:
10221 case CMD_python3:
10222 case CMD_python:
10223 case CMD_pythonx:
10224 case CMD_ruby:
10225 case CMD_tcl:
10226 ea.arg = p;
10227 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010228 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010229 else
10230 // heredoc lines have been concatenated with NL
10231 // characters in get_function_body()
10232 line = compile_script(line, &cctx);
10233 break;
10234
Bram Moolenaar7b829262021-10-13 15:04:34 +010010235 case CMD_global:
10236 if (check_global_and_subst(ea.cmd, p) == FAIL)
10237 goto erret;
10238 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +020010239 default:
10240 // Not recognized, execute with do_cmdline_cmd().
10241 ea.arg = p;
10242 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010243 break;
10244 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010245nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010246 if (line == NULL)
10247 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010248 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010249
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010250 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010251 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010253 if (cctx.ctx_type_stack.ga_len < 0)
10254 {
10255 iemsg("Type stack underflow");
10256 goto erret;
10257 }
10258 }
10259
10260 if (cctx.ctx_scope != NULL)
10261 {
10262 if (cctx.ctx_scope->se_type == IF_SCOPE)
10263 emsg(_(e_endif));
10264 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10265 emsg(_(e_endwhile));
10266 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10267 emsg(_(e_endfor));
10268 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010269 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010270 goto erret;
10271 }
10272
Bram Moolenaarefd88552020-06-18 20:50:10 +020010273 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010274 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010275 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10276 ufunc->uf_ret_type = &t_void;
10277 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010278 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010279 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010280 goto erret;
10281 }
10282
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010283 // Return void if there is no return at the end.
10284 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010285 }
10286
Bram Moolenaar599410c2021-04-10 14:03:43 +020010287 // When compiled with ":silent!" and there was an error don't consider the
10288 // function compiled.
10289 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010290 {
10291 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10292 + ufunc->uf_dfunc_idx;
10293 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010294 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010295#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010296 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010297 {
10298 dfunc->df_instr_prof = instr->ga_data;
10299 dfunc->df_instr_prof_count = instr->ga_len;
10300 }
10301 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010302#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010303 if (cctx.ctx_compile_type == CT_DEBUG)
10304 {
10305 dfunc->df_instr_debug = instr->ga_data;
10306 dfunc->df_instr_debug_count = instr->ga_len;
10307 }
10308 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010309 {
10310 dfunc->df_instr = instr->ga_data;
10311 dfunc->df_instr_count = instr->ga_len;
10312 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010313 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010314 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010315 if (cctx.ctx_outer_used)
10316 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010317 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010319
10320 ret = OK;
10321
10322erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010323 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010324 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010325 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10326 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010327
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010328 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010329 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010330 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010331 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010332
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010333 // If using the last entry in the table and it was added above, we
10334 // might as well remove it.
10335 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010336 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010337 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010338 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010339 ufunc->uf_dfunc_idx = 0;
10340 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010341 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010342
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010343 while (cctx.ctx_scope != NULL)
10344 drop_scope(&cctx);
10345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010346 if (errormsg != NULL)
10347 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010348 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010349 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010350 }
10351
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010352 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10353 {
10354 if (ret == OK)
10355 {
10356 emsg(_(e_missing_redir_end));
10357 ret = FAIL;
10358 }
10359 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010360 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010361 }
10362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010363 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010364 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010365 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010366 if (do_estack_push)
10367 estack_pop();
10368
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010369 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010370 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010371 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010372 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010373 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010374}
10375
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010376 void
10377set_function_type(ufunc_T *ufunc)
10378{
10379 int varargs = ufunc->uf_va_name != NULL;
10380 int argcount = ufunc->uf_args.ga_len;
10381
10382 // Create a type for the function, with the return type and any
10383 // argument types.
10384 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10385 // The type is included in "tt_args".
10386 if (argcount > 0 || varargs)
10387 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010388 if (ufunc->uf_type_list.ga_itemsize == 0)
10389 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010390 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10391 argcount, &ufunc->uf_type_list);
10392 // Add argument types to the function type.
10393 if (func_type_add_arg_types(ufunc->uf_func_type,
10394 argcount + varargs,
10395 &ufunc->uf_type_list) == FAIL)
10396 return;
10397 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10398 ufunc->uf_func_type->tt_min_argcount =
10399 argcount - ufunc->uf_def_args.ga_len;
10400 if (ufunc->uf_arg_types == NULL)
10401 {
10402 int i;
10403
10404 // lambda does not have argument types.
10405 for (i = 0; i < argcount; ++i)
10406 ufunc->uf_func_type->tt_args[i] = &t_any;
10407 }
10408 else
10409 mch_memmove(ufunc->uf_func_type->tt_args,
10410 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10411 if (varargs)
10412 {
10413 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010414 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010415 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10416 }
10417 }
10418 else
10419 // No arguments, can use a predefined type.
10420 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10421 argcount, &ufunc->uf_type_list);
10422}
10423
10424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010425/*
10426 * Delete an instruction, free what it contains.
10427 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010428 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010429delete_instr(isn_T *isn)
10430{
10431 switch (isn->isn_type)
10432 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010433 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010434 case ISN_EXEC:
Bram Moolenaare4eed8c2021-12-01 15:22:56 +000010435 case ISN_EXECRANGE:
Bram Moolenaar20677332021-06-06 17:02:53 +020010436 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010437 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010438 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010439 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010440 case ISN_LOADENV:
10441 case ISN_LOADG:
10442 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010443 case ISN_LOADT:
10444 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010445 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010446 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010447 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010448 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010449 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010450 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010451 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010452 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010453 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010454 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010455 case ISN_STOREW:
10456 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010457 vim_free(isn->isn_arg.string);
10458 break;
10459
Bram Moolenaar4c137212021-04-19 16:48:48 +020010460 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010461 {
10462 int idx;
10463 isn_T *list = isn->isn_arg.subs.subs_instr;
10464
10465 vim_free(isn->isn_arg.subs.subs_cmd);
10466 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10467 delete_instr(list + idx);
10468 vim_free(list);
10469 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010470 break;
10471
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010472 case ISN_INSTR:
10473 {
10474 int idx;
10475 isn_T *list = isn->isn_arg.instr;
10476
10477 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10478 delete_instr(list + idx);
10479 vim_free(list);
10480 }
10481 break;
10482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010483 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010484 case ISN_STORES:
10485 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010486 break;
10487
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010488 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010489 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010490 vim_free(isn->isn_arg.unlet.ul_name);
10491 break;
10492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010493 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +000010494 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010495 vim_free(isn->isn_arg.storeopt.so_name);
10496 break;
10497
10498 case ISN_PUSHBLOB: // push blob isn_arg.blob
10499 blob_unref(isn->isn_arg.blob);
10500 break;
10501
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010502 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010503#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010504 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010505#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010506 break;
10507
10508 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010509#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010510 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010511#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010512 break;
10513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010514 case ISN_UCALL:
10515 vim_free(isn->isn_arg.ufunc.cuf_name);
10516 break;
10517
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010518 case ISN_FUNCREF:
10519 {
Bram Moolenaar38453522021-11-28 22:00:12 +000010520 if (isn->isn_arg.funcref.fr_func_name == NULL)
10521 {
10522 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10523 + isn->isn_arg.funcref.fr_dfunc_idx;
10524 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010525
Bram Moolenaar38453522021-11-28 22:00:12 +000010526 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10527 func_ptr_unref(ufunc);
10528 }
10529 else
10530 {
10531 char_u *name = isn->isn_arg.funcref.fr_func_name;
10532
10533 if (name != NULL)
10534 func_unref(name);
10535 vim_free(isn->isn_arg.funcref.fr_func_name);
10536 }
Bram Moolenaara05e5242020-09-19 18:19:19 +020010537 }
10538 break;
10539
10540 case ISN_DCALL:
10541 {
10542 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10543 + isn->isn_arg.dfunc.cdf_idx;
10544
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010545 if (dfunc->df_ufunc != NULL
10546 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010547 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010548 }
10549 break;
10550
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010551 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010552 {
10553 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10554 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10555
10556 if (ufunc != NULL)
10557 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010558 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010559 func_ptr_unref(ufunc);
10560 }
10561
10562 vim_free(lambda);
10563 vim_free(isn->isn_arg.newfunc.nf_global);
10564 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010565 break;
10566
Bram Moolenaar5e654232020-09-16 15:22:00 +020010567 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010568 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010569 free_type(isn->isn_arg.type.ct_type);
10570 break;
10571
Bram Moolenaar02194d22020-10-24 23:08:38 +020010572 case ISN_CMDMOD:
10573 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10574 ->cmod_filter_regmatch.regprog);
10575 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10576 break;
10577
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010578 case ISN_LOADSCRIPT:
10579 case ISN_STORESCRIPT:
10580 vim_free(isn->isn_arg.script.scriptref);
10581 break;
10582
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010583 case ISN_TRY:
10584 vim_free(isn->isn_arg.try.try_ref);
10585 break;
10586
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010587 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010588 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010589 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10590 break;
10591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010592 case ISN_2BOOL:
10593 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010594 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010595 case ISN_ADDBLOB:
10596 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010597 case ISN_ANYINDEX:
10598 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010599 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010600 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010601 case ISN_BLOBINDEX:
10602 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010603 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010604 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010605 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010606 case ISN_CHECKNR:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010607 case ISN_CLEARDICT:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010608 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010609 case ISN_COMPAREANY:
10610 case ISN_COMPAREBLOB:
10611 case ISN_COMPAREBOOL:
10612 case ISN_COMPAREDICT:
10613 case ISN_COMPAREFLOAT:
10614 case ISN_COMPAREFUNC:
10615 case ISN_COMPARELIST:
10616 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010617 case ISN_COMPARESPECIAL:
10618 case ISN_COMPARESTRING:
10619 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010620 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010621 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010622 case ISN_DROP:
10623 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010624 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010625 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010626 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010627 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010628 case ISN_EXECCONCAT:
10629 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010630 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010631 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010632 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010633 case ISN_GETITEM:
10634 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010635 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010636 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010637 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010638 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010639 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010640 case ISN_LOADBDICT:
10641 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010642 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010643 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010644 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010645 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010646 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010647 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010648 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010649 case ISN_NEGATENR:
10650 case ISN_NEWDICT:
10651 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010652 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010653 case ISN_OPFLOAT:
10654 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010655 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010656 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010657 case ISN_PROF_END:
10658 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010659 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010660 case ISN_PUSHF:
10661 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010662 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010663 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010664 case ISN_REDIREND:
10665 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010666 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010667 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010668 case ISN_SHUFFLE:
10669 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010670 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010671 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010672 case ISN_STORENR:
10673 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010674 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010675 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010676 case ISN_STOREV:
10677 case ISN_STRINDEX:
10678 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010679 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010680 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010681 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010682 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010683 case ISN_UNPACK:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010684 case ISN_USEDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010685 // nothing allocated
10686 break;
10687 }
10688}
10689
10690/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010691 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010692 */
10693 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010694delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010695{
10696 int idx;
10697
10698 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010699 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010700
10701 if (dfunc->df_instr != NULL)
10702 {
10703 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10704 delete_instr(dfunc->df_instr + idx);
10705 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010706 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010707 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010708 if (dfunc->df_instr_debug != NULL)
10709 {
10710 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10711 delete_instr(dfunc->df_instr_debug + idx);
10712 VIM_CLEAR(dfunc->df_instr_debug);
10713 dfunc->df_instr_debug = NULL;
10714 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010715#ifdef FEAT_PROFILE
10716 if (dfunc->df_instr_prof != NULL)
10717 {
10718 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10719 delete_instr(dfunc->df_instr_prof + idx);
10720 VIM_CLEAR(dfunc->df_instr_prof);
10721 dfunc->df_instr_prof = NULL;
10722 }
10723#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010724
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010725 if (mark_deleted)
10726 dfunc->df_deleted = TRUE;
10727 if (dfunc->df_ufunc != NULL)
10728 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010729}
10730
10731/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010732 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010733 * function, unless another user function still uses it.
10734 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010735 */
10736 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010737unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010738{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010739 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010740 {
10741 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10742 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010743
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010744 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010745 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010746 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010747 ufunc->uf_dfunc_idx = 0;
10748 if (dfunc->df_ufunc == ufunc)
10749 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010750 }
10751}
10752
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010753/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010754 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010755 */
10756 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010757link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010758{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010759 if (ufunc->uf_dfunc_idx > 0)
10760 {
10761 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10762 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010763
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010764 ++dfunc->df_refcount;
10765 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010766}
10767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010768#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010769/*
10770 * Free all functions defined with ":def".
10771 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010772 void
10773free_def_functions(void)
10774{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010775 int idx;
10776
10777 for (idx = 0; idx < def_functions.ga_len; ++idx)
10778 {
10779 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10780
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010781 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010782 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010783 }
10784
10785 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010786}
10787#endif
10788
10789
10790#endif // FEAT_EVAL