blob: d6fc6d85810202b89f9572b2914af619837f4799 [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{
Bram Moolenaar44a89772021-12-18 12:31:33 +00001064 int ret;
1065
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001066 if (expected == &t_bool && actual != &t_bool
1067 && (actual->tt_flags & TTFLAG_BOOL_OK))
1068 {
1069 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1070 // boolean is OK but requires a conversion.
1071 generate_2BOOL(cctx, FALSE, offset);
1072 return OK;
1073 }
1074
Bram Moolenaar44a89772021-12-18 12:31:33 +00001075 ret = check_type_maybe(expected, actual, FALSE, where);
1076 if (ret == OK)
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001077 return OK;
1078
1079 // If the actual type can be the expected type add a runtime check.
1080 // If it's a constant a runtime check makes no sense.
Bram Moolenaar44a89772021-12-18 12:31:33 +00001081 if (ret == MAYBE || ((!actual_is_const || actual == &t_any)
1082 && use_typecheck(actual, expected)))
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001083 {
1084 generate_TYPECHECK(cctx, expected, offset, where.wt_index);
1085 return OK;
1086 }
1087
1088 if (!silent)
1089 type_mismatch_where(expected, actual, where);
1090 return FAIL;
1091}
1092
Bram Moolenaar351ead02021-01-16 16:07:01 +01001093 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001094need_type(
1095 type_T *actual,
1096 type_T *expected,
1097 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001098 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001099 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001100 int silent,
1101 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001102{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001103 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001104
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001105 where.wt_index = arg_idx;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001106 return need_type_where(actual, expected, offset, where,
1107 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001108}
1109
1110/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001111 * Check that the top of the type stack has a type that can be used as a
1112 * condition. Give an error and return FAIL if not.
1113 */
1114 static int
1115bool_on_stack(cctx_T *cctx)
1116{
1117 garray_T *stack = &cctx->ctx_type_stack;
1118 type_T *type;
1119
1120 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1121 if (type == &t_bool)
1122 return OK;
1123
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001124 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001125 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1126 // This requires a runtime type check.
1127 return generate_COND2BOOL(cctx);
1128
Bram Moolenaar351ead02021-01-16 16:07:01 +01001129 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001130}
1131
1132/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 * Generate an ISN_PUSHNR instruction.
1134 */
1135 static int
1136generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1137{
1138 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001139 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140
Bram Moolenaar080457c2020-03-03 21:53:32 +01001141 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1143 return FAIL;
1144 isn->isn_arg.number = number;
1145
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001146 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001147 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001148 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 return OK;
1150}
1151
1152/*
1153 * Generate an ISN_PUSHBOOL instruction.
1154 */
1155 static int
1156generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1157{
1158 isn_T *isn;
1159
Bram Moolenaar080457c2020-03-03 21:53:32 +01001160 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1162 return FAIL;
1163 isn->isn_arg.number = number;
1164
1165 return OK;
1166}
1167
1168/*
1169 * Generate an ISN_PUSHSPEC instruction.
1170 */
1171 static int
1172generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1173{
1174 isn_T *isn;
1175
Bram Moolenaar080457c2020-03-03 21:53:32 +01001176 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1178 return FAIL;
1179 isn->isn_arg.number = number;
1180
1181 return OK;
1182}
1183
1184#ifdef FEAT_FLOAT
1185/*
1186 * Generate an ISN_PUSHF instruction.
1187 */
1188 static int
1189generate_PUSHF(cctx_T *cctx, float_T fnumber)
1190{
1191 isn_T *isn;
1192
Bram Moolenaar080457c2020-03-03 21:53:32 +01001193 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1195 return FAIL;
1196 isn->isn_arg.fnumber = fnumber;
1197
1198 return OK;
1199}
1200#endif
1201
1202/*
1203 * Generate an ISN_PUSHS instruction.
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001204 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205 */
1206 static int
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001207generate_PUSHS(cctx_T *cctx, char_u **str)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208{
1209 isn_T *isn;
1210
Bram Moolenaar508b5612021-01-02 18:17:26 +01001211 if (cctx->ctx_skip == SKIP_YES)
1212 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001213 if (str != NULL)
1214 VIM_CLEAR(*str);
Bram Moolenaar508b5612021-01-02 18:17:26 +01001215 return OK;
1216 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001218 {
1219 if (str != NULL)
1220 VIM_CLEAR(*str);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001222 }
1223 isn->isn_arg.string = str == NULL ? NULL : *str;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224
1225 return OK;
1226}
1227
1228/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001229 * Generate an ISN_PUSHCHANNEL instruction.
1230 * Consumes "channel".
1231 */
1232 static int
1233generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1234{
1235 isn_T *isn;
1236
Bram Moolenaar080457c2020-03-03 21:53:32 +01001237 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001238 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1239 return FAIL;
1240 isn->isn_arg.channel = channel;
1241
1242 return OK;
1243}
1244
1245/*
1246 * Generate an ISN_PUSHJOB instruction.
1247 * Consumes "job".
1248 */
1249 static int
1250generate_PUSHJOB(cctx_T *cctx, job_T *job)
1251{
1252 isn_T *isn;
1253
Bram Moolenaar080457c2020-03-03 21:53:32 +01001254 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001255 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001256 return FAIL;
1257 isn->isn_arg.job = job;
1258
1259 return OK;
1260}
1261
1262/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 * Generate an ISN_PUSHBLOB instruction.
1264 * Consumes "blob".
1265 */
1266 static int
1267generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1268{
1269 isn_T *isn;
1270
Bram Moolenaar080457c2020-03-03 21:53:32 +01001271 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1273 return FAIL;
1274 isn->isn_arg.blob = blob;
1275
1276 return OK;
1277}
1278
1279/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001280 * Generate an ISN_PUSHFUNC instruction with name "name".
1281 * Consumes "name".
1282 */
1283 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001284generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001285{
1286 isn_T *isn;
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001287 char_u *funcname;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001288
Bram Moolenaar080457c2020-03-03 21:53:32 +01001289 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001290 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001291 return FAIL;
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001292 if (name == NULL)
1293 funcname = NULL;
1294 else if (*name == K_SPECIAL) // script-local
1295 funcname = vim_strsave(name);
1296 else
1297 {
1298 funcname = alloc(STRLEN(name) + 3);
1299 if (funcname != NULL)
1300 {
1301 STRCPY(funcname, "g:");
1302 STRCPY(funcname + 2, name);
1303 }
1304 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001305
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001306 isn->isn_arg.string = funcname;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001307 return OK;
1308}
1309
1310/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001311 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001312 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1313 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001314 */
1315 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001316generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001317{
1318 isn_T *isn;
1319 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001320 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1321 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001322 type_T *item_type = &t_any;
1323
1324 RETURN_OK_IF_SKIP(cctx);
1325
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001326 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001327 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001328 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001329 emsg(_(e_listreq));
1330 return FAIL;
1331 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001332 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001333 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1334 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001335 isn->isn_arg.getitem.gi_index = index;
1336 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001337
1338 // add the item type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001339 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001340 return FAIL;
1341 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1342 ++stack->ga_len;
1343 return OK;
1344}
1345
1346/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001347 * Generate an ISN_SLICE instruction with "count".
1348 */
1349 static int
1350generate_SLICE(cctx_T *cctx, int count)
1351{
1352 isn_T *isn;
1353
1354 RETURN_OK_IF_SKIP(cctx);
1355 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1356 return FAIL;
1357 isn->isn_arg.number = count;
1358 return OK;
1359}
1360
1361/*
1362 * Generate an ISN_CHECKLEN instruction with "min_len".
1363 */
1364 static int
1365generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1366{
1367 isn_T *isn;
1368
1369 RETURN_OK_IF_SKIP(cctx);
1370
1371 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1372 return FAIL;
1373 isn->isn_arg.checklen.cl_min_len = min_len;
1374 isn->isn_arg.checklen.cl_more_OK = more_OK;
1375
1376 return OK;
1377}
1378
1379/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 * Generate an ISN_STORE instruction.
1381 */
1382 static int
1383generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1384{
1385 isn_T *isn;
1386
Bram Moolenaar080457c2020-03-03 21:53:32 +01001387 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1389 return FAIL;
1390 if (name != NULL)
1391 isn->isn_arg.string = vim_strsave(name);
1392 else
1393 isn->isn_arg.number = idx;
1394
1395 return OK;
1396}
1397
1398/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001399 * Generate an ISN_STOREOUTER instruction.
1400 */
1401 static int
1402generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1403{
1404 isn_T *isn;
1405
1406 RETURN_OK_IF_SKIP(cctx);
1407 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1408 return FAIL;
1409 isn->isn_arg.outer.outer_idx = idx;
1410 isn->isn_arg.outer.outer_depth = level;
1411
1412 return OK;
1413}
1414
1415/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1417 */
1418 static int
1419generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1420{
1421 isn_T *isn;
1422
Bram Moolenaar080457c2020-03-03 21:53:32 +01001423 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1425 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001426 isn->isn_arg.storenr.stnr_idx = idx;
1427 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428
1429 return OK;
1430}
1431
1432/*
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001433 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 */
1435 static int
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001436generate_STOREOPT(
1437 cctx_T *cctx,
1438 isntype_T isn_type,
1439 char_u *name,
1440 int opt_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441{
1442 isn_T *isn;
1443
Bram Moolenaar080457c2020-03-03 21:53:32 +01001444 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001445 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 return FAIL;
1447 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1448 isn->isn_arg.storeopt.so_flags = opt_flags;
1449
1450 return OK;
1451}
1452
1453/*
1454 * Generate an ISN_LOAD or similar instruction.
1455 */
1456 static int
1457generate_LOAD(
1458 cctx_T *cctx,
1459 isntype_T isn_type,
1460 int idx,
1461 char_u *name,
1462 type_T *type)
1463{
1464 isn_T *isn;
1465
Bram Moolenaar080457c2020-03-03 21:53:32 +01001466 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1468 return FAIL;
1469 if (name != NULL)
1470 isn->isn_arg.string = vim_strsave(name);
1471 else
1472 isn->isn_arg.number = idx;
1473
1474 return OK;
1475}
1476
1477/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001478 * Generate an ISN_LOADOUTER instruction
1479 */
1480 static int
1481generate_LOADOUTER(
1482 cctx_T *cctx,
1483 int idx,
1484 int nesting,
1485 type_T *type)
1486{
1487 isn_T *isn;
1488
1489 RETURN_OK_IF_SKIP(cctx);
1490 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1491 return FAIL;
1492 isn->isn_arg.outer.outer_idx = idx;
1493 isn->isn_arg.outer.outer_depth = nesting;
1494
1495 return OK;
1496}
1497
1498/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001499 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001500 */
1501 static int
1502generate_LOADV(
1503 cctx_T *cctx,
1504 char_u *name,
1505 int error)
1506{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001507 int di_flags;
1508 int vidx = find_vim_var(name, &di_flags);
1509 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001510
Bram Moolenaar080457c2020-03-03 21:53:32 +01001511 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001512 if (vidx < 0)
1513 {
1514 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001515 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001516 return FAIL;
1517 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001518 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001519
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001520 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001521}
1522
1523/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001524 * Generate an ISN_UNLET instruction.
1525 */
1526 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001527generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001528{
1529 isn_T *isn;
1530
1531 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001532 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001533 return FAIL;
1534 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1535 isn->isn_arg.unlet.ul_forceit = forceit;
1536
1537 return OK;
1538}
1539
1540/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001541 * Generate an ISN_LOCKCONST instruction.
1542 */
1543 static int
1544generate_LOCKCONST(cctx_T *cctx)
1545{
1546 isn_T *isn;
1547
1548 RETURN_OK_IF_SKIP(cctx);
1549 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1550 return FAIL;
1551 return OK;
1552}
1553
1554/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 * Generate an ISN_LOADS instruction.
1556 */
1557 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001558generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001560 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001562 int sid,
1563 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564{
1565 isn_T *isn;
1566
Bram Moolenaar080457c2020-03-03 21:53:32 +01001567 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001568 if (isn_type == ISN_LOADS)
1569 isn = generate_instr_type(cctx, isn_type, type);
1570 else
1571 isn = generate_instr_drop(cctx, isn_type, 1);
1572 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001574 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1575 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576
1577 return OK;
1578}
1579
1580/*
1581 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1582 */
1583 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001584generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 cctx_T *cctx,
1586 isntype_T isn_type,
1587 int sid,
1588 int idx,
1589 type_T *type)
1590{
1591 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001592 scriptref_T *sref;
1593 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594
Bram Moolenaar080457c2020-03-03 21:53:32 +01001595 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 if (isn_type == ISN_LOADSCRIPT)
1597 isn = generate_instr_type(cctx, isn_type, type);
1598 else
1599 isn = generate_instr_drop(cctx, isn_type, 1);
1600 if (isn == NULL)
1601 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001602
1603 // This requires three arguments, which doesn't fit in an instruction, thus
1604 // we need to allocate a struct for this.
1605 sref = ALLOC_ONE(scriptref_T);
1606 if (sref == NULL)
1607 return FAIL;
1608 isn->isn_arg.script.scriptref = sref;
1609 sref->sref_sid = sid;
1610 sref->sref_idx = idx;
1611 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001612 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 return OK;
1614}
1615
1616/*
1617 * Generate an ISN_NEWLIST instruction.
1618 */
1619 static int
1620generate_NEWLIST(cctx_T *cctx, int count)
1621{
1622 isn_T *isn;
1623 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 type_T *type;
1625 type_T *member;
1626
Bram Moolenaar080457c2020-03-03 21:53:32 +01001627 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1629 return FAIL;
1630 isn->isn_arg.number = count;
1631
Bram Moolenaar127542b2020-08-09 17:22:04 +02001632 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001633 if (count == 0)
Bram Moolenaar6e48b842021-08-10 22:52:02 +02001634 member = &t_unknown;
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001635 else
1636 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001637 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1638 cctx->ctx_type_list);
1639 type = get_list_type(member, cctx->ctx_type_list);
1640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 // drop the value types
1642 stack->ga_len -= count;
1643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 // add the list type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001645 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 return FAIL;
1647 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1648 ++stack->ga_len;
1649
1650 return OK;
1651}
1652
1653/*
1654 * Generate an ISN_NEWDICT instruction.
1655 */
1656 static int
1657generate_NEWDICT(cctx_T *cctx, int count)
1658{
1659 isn_T *isn;
1660 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661 type_T *type;
1662 type_T *member;
1663
Bram Moolenaar080457c2020-03-03 21:53:32 +01001664 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1666 return FAIL;
1667 isn->isn_arg.number = count;
1668
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001669 if (count == 0)
1670 member = &t_void;
1671 else
1672 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001673 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1674 cctx->ctx_type_list);
1675 type = get_dict_type(member, cctx->ctx_type_list);
1676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 // drop the key and value types
1678 stack->ga_len -= 2 * count;
1679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 // add the dict type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001681 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 return FAIL;
1683 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1684 ++stack->ga_len;
1685
1686 return OK;
1687}
1688
1689/*
1690 * Generate an ISN_FUNCREF instruction.
1691 */
1692 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001693generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694{
1695 isn_T *isn;
1696 garray_T *stack = &cctx->ctx_type_stack;
1697
Bram Moolenaar080457c2020-03-03 21:53:32 +01001698 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1700 return FAIL;
Bram Moolenaar38453522021-11-28 22:00:12 +00001701 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1702 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1703 else
1704 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001705 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001707 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001708 // the nested context, including this one.
1709 if (ufunc->uf_flags & FC_CLOSURE)
1710 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1711
Bram Moolenaar35578162021-08-02 19:10:38 +02001712 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001714 ((type_T **)stack->ga_data)[stack->ga_len] =
1715 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 ++stack->ga_len;
1717
1718 return OK;
1719}
1720
1721/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001722 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001723 * "lambda_name" and "func_name" must be in allocated memory and will be
1724 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001725 */
1726 static int
1727generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1728{
1729 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001730
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001731 if (cctx->ctx_skip == SKIP_YES)
1732 {
1733 vim_free(lambda_name);
1734 vim_free(func_name);
1735 return OK;
1736 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001737 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001738 {
1739 vim_free(lambda_name);
1740 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001741 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001742 }
1743 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001744 isn->isn_arg.newfunc.nf_global = func_name;
1745
1746 return OK;
1747}
1748
1749/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001750 * Generate an ISN_DEF instruction: list functions
1751 */
1752 static int
1753generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1754{
1755 isn_T *isn;
1756
1757 RETURN_OK_IF_SKIP(cctx);
1758 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1759 return FAIL;
1760 if (len > 0)
1761 {
1762 isn->isn_arg.string = vim_strnsave(name, len);
1763 if (isn->isn_arg.string == NULL)
1764 return FAIL;
1765 }
1766 return OK;
1767}
1768
1769/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 * Generate an ISN_JUMP instruction.
1771 */
1772 static int
1773generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1774{
1775 isn_T *isn;
1776 garray_T *stack = &cctx->ctx_type_stack;
1777
Bram Moolenaar080457c2020-03-03 21:53:32 +01001778 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1780 return FAIL;
1781 isn->isn_arg.jump.jump_when = when;
1782 isn->isn_arg.jump.jump_where = where;
1783
1784 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1785 --stack->ga_len;
1786
1787 return OK;
1788}
1789
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001790/*
1791 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1792 */
1793 static int
1794generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1795{
1796 isn_T *isn;
1797
1798 RETURN_OK_IF_SKIP(cctx);
1799 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1800 return FAIL;
1801 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1802 // jump_where is set later
1803 return OK;
1804}
1805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 static int
1807generate_FOR(cctx_T *cctx, int loop_idx)
1808{
1809 isn_T *isn;
1810 garray_T *stack = &cctx->ctx_type_stack;
1811
Bram Moolenaar080457c2020-03-03 21:53:32 +01001812 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1814 return FAIL;
1815 isn->isn_arg.forloop.for_idx = loop_idx;
1816
Bram Moolenaar35578162021-08-02 19:10:38 +02001817 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 return FAIL;
1819 // type doesn't matter, will be stored next
1820 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1821 ++stack->ga_len;
1822
1823 return OK;
1824}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001825/*
1826 * Generate an ISN_TRYCONT instruction.
1827 */
1828 static int
1829generate_TRYCONT(cctx_T *cctx, int levels, int where)
1830{
1831 isn_T *isn;
1832
1833 RETURN_OK_IF_SKIP(cctx);
1834 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1835 return FAIL;
1836 isn->isn_arg.trycont.tct_levels = levels;
1837 isn->isn_arg.trycont.tct_where = where;
1838
1839 return OK;
1840}
1841
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842
1843/*
1844 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001845 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 * Return FAIL if the number of arguments is wrong.
1847 */
1848 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001849generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850{
1851 isn_T *isn;
1852 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001853 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001854 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001855 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001856 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857
Bram Moolenaar080457c2020-03-03 21:53:32 +01001858 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001859 argoff = check_internal_func(func_idx, argcount);
1860 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 return FAIL;
1862
Bram Moolenaar389df252020-07-09 21:20:47 +02001863 if (method_call && argoff > 1)
1864 {
1865 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1866 return FAIL;
1867 isn->isn_arg.shuffle.shfl_item = argcount;
1868 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1869 }
1870
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001871 if (argcount > 0)
1872 {
1873 // Check the types of the arguments.
1874 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001875 if (method_call && argoff > 1)
1876 {
1877 int i;
1878
1879 for (i = 0; i < argcount; ++i)
1880 shuffled_argtypes[i] = (i < argoff - 1)
1881 ? argtypes[i + 1]
1882 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1883 argtypes = shuffled_argtypes;
1884 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001885 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1886 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001887 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001888 if (internal_func_is_map(func_idx))
1889 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001890 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1893 return FAIL;
1894 isn->isn_arg.bfunc.cbf_idx = func_idx;
1895 isn->isn_arg.bfunc.cbf_argcount = argcount;
1896
Bram Moolenaar94738d82020-10-21 14:25:07 +02001897 // Drop the argument types and push the return type.
1898 stack->ga_len -= argcount;
Bram Moolenaar35578162021-08-02 19:10:38 +02001899 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 return FAIL;
1901 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001902 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001903 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001905 if (maptype != NULL && maptype->tt_member != NULL
1906 && maptype->tt_member != &t_any)
1907 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001908 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 return OK;
1911}
1912
1913/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001914 * Generate an ISN_LISTAPPEND instruction. Works like add().
1915 * Argument count is already checked.
1916 */
1917 static int
1918generate_LISTAPPEND(cctx_T *cctx)
1919{
1920 garray_T *stack = &cctx->ctx_type_stack;
1921 type_T *list_type;
1922 type_T *item_type;
1923 type_T *expected;
1924
1925 // Caller already checked that list_type is a list.
1926 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1927 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1928 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001929 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001930 return FAIL;
1931
1932 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1933 return FAIL;
1934
1935 --stack->ga_len; // drop the argument
1936 return OK;
1937}
1938
1939/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001940 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1941 * Argument count is already checked.
1942 */
1943 static int
1944generate_BLOBAPPEND(cctx_T *cctx)
1945{
1946 garray_T *stack = &cctx->ctx_type_stack;
1947 type_T *item_type;
1948
1949 // Caller already checked that blob_type is a blob.
1950 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001951 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001952 return FAIL;
1953
1954 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1955 return FAIL;
1956
1957 --stack->ga_len; // drop the argument
1958 return OK;
1959}
1960
1961/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001962 * Return TRUE if "ufunc" should be compiled, taking into account whether
1963 * "profile" indicates profiling is to be done.
1964 */
1965 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001966func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001967{
1968 switch (ufunc->uf_def_status)
1969 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001970 case UF_TO_BE_COMPILED:
1971 return TRUE;
1972
Bram Moolenaarb2049902021-01-24 12:53:53 +01001973 case UF_COMPILED:
1974 {
1975 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1976 + ufunc->uf_dfunc_idx;
1977
Bram Moolenaare99d4222021-06-13 14:01:26 +02001978 switch (compile_type)
1979 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001980 case CT_PROFILE:
1981#ifdef FEAT_PROFILE
1982 return dfunc->df_instr_prof == NULL;
1983#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001984 case CT_NONE:
1985 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001986 case CT_DEBUG:
1987 return dfunc->df_instr_debug == NULL;
1988 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001989 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001990
1991 case UF_NOT_COMPILED:
1992 case UF_COMPILE_ERROR:
1993 case UF_COMPILING:
1994 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001995 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001996 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001997}
1998
1999/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 * Generate an ISN_DCALL or ISN_UCALL instruction.
2001 * Return FAIL if the number of arguments is wrong.
2002 */
2003 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002004generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005{
2006 isn_T *isn;
2007 garray_T *stack = &cctx->ctx_type_stack;
2008 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002009 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010
Bram Moolenaar080457c2020-03-03 21:53:32 +01002011 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 if (argcount > regular_args && !has_varargs(ufunc))
2013 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002014 semsg(_(e_too_many_arguments_for_function_str),
2015 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 return FAIL;
2017 }
2018 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
2019 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002020 semsg(_(e_not_enough_arguments_for_function_str),
2021 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 return FAIL;
2023 }
2024
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002025 if (ufunc->uf_def_status != UF_NOT_COMPILED
2026 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002027 {
2028 int i;
2029
2030 for (i = 0; i < argcount; ++i)
2031 {
2032 type_T *expected;
2033 type_T *actual;
2034
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002035 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
2036 if (actual == &t_special
2037 && i >= regular_args - ufunc->uf_def_args.ga_len)
2038 {
2039 // assume v:none used for default argument value
2040 continue;
2041 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002042 if (i < regular_args)
2043 {
2044 if (ufunc->uf_arg_types == NULL)
2045 continue;
2046 expected = ufunc->uf_arg_types[i];
2047 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02002048 else if (ufunc->uf_va_type == NULL
2049 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02002050 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02002051 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002052 else
2053 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01002054 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002055 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002056 {
2057 arg_type_mismatch(expected, actual, i + 1);
2058 return FAIL;
2059 }
2060 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002061 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002062 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002063 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002064 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002065 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002066 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2067 {
2068 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2069 ufunc->uf_name);
2070 return FAIL;
2071 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002074 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002075 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002077 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 {
2079 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2080 isn->isn_arg.dfunc.cdf_argcount = argcount;
2081 }
2082 else
2083 {
2084 // A user function may be deleted and redefined later, can't use the
2085 // ufunc pointer, need to look it up again at runtime.
2086 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2087 isn->isn_arg.ufunc.cuf_argcount = argcount;
2088 }
2089
2090 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002091 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 return FAIL;
2093 // add return value
2094 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2095 ++stack->ga_len;
2096
2097 return OK;
2098}
2099
2100/*
2101 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2102 */
2103 static int
2104generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2105{
2106 isn_T *isn;
2107 garray_T *stack = &cctx->ctx_type_stack;
2108
Bram Moolenaar080457c2020-03-03 21:53:32 +01002109 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2111 return FAIL;
2112 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2113 isn->isn_arg.ufunc.cuf_argcount = argcount;
2114
2115 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002116 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002117 return FAIL;
2118 // add return value
2119 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2120 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121
2122 return OK;
2123}
2124
2125/*
2126 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002127 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 */
2129 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002130generate_PCALL(
2131 cctx_T *cctx,
2132 int argcount,
2133 char_u *name,
2134 type_T *type,
2135 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136{
2137 isn_T *isn;
2138 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002139 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140
Bram Moolenaar080457c2020-03-03 21:53:32 +01002141 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002142
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002143 if (type->tt_type == VAR_ANY)
2144 ret_type = &t_any;
2145 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002146 {
2147 if (type->tt_argcount != -1)
2148 {
2149 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2150
2151 if (argcount < type->tt_min_argcount - varargs)
2152 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002153 semsg(_(e_not_enough_arguments_for_function_str), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002154 return FAIL;
2155 }
2156 if (!varargs && argcount > type->tt_argcount)
2157 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002158 semsg(_(e_too_many_arguments_for_function_str), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002159 return FAIL;
2160 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002161 if (type->tt_args != NULL)
2162 {
2163 int i;
2164
2165 for (i = 0; i < argcount; ++i)
2166 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002167 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002168 type_T *actual = ((type_T **)stack->ga_data)[
2169 stack->ga_len + offset];
2170 type_T *expected;
2171
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002172 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002173 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002174 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002175 else if (i >= type->tt_min_argcount
2176 && actual == &t_special)
2177 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002178 else
2179 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002180 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002181 cctx, TRUE, FALSE) == FAIL)
2182 {
2183 arg_type_mismatch(expected, actual, i + 1);
2184 return FAIL;
2185 }
2186 }
2187 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002188 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002189 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002190 if (ret_type == &t_unknown)
2191 // return type not known yet, use a runtime check
2192 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002193 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002194 else
2195 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002196 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002197 return FAIL;
2198 }
2199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2201 return FAIL;
2202 isn->isn_arg.pfunc.cpf_top = at_top;
2203 isn->isn_arg.pfunc.cpf_argcount = argcount;
2204
2205 stack->ga_len -= argcount; // drop the arguments
2206
2207 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002208 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002210 // If partial is above the arguments it must be cleared and replaced with
2211 // the return value.
2212 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2213 return FAIL;
2214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 return OK;
2216}
2217
2218/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002219 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 */
2221 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002222generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223{
2224 isn_T *isn;
2225 garray_T *stack = &cctx->ctx_type_stack;
2226 type_T *type;
2227
Bram Moolenaar080457c2020-03-03 21:53:32 +01002228 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002229 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002231 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002233 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002235 if (type->tt_type != VAR_DICT && type != &t_any)
2236 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002237 char *tofree;
2238
2239 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2240 name, type_name(type, &tofree));
2241 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002242 return FAIL;
2243 }
2244 // change dict type to dict member type
2245 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002246 {
2247 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2248 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2249 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250
2251 return OK;
2252}
2253
2254/*
2255 * Generate an ISN_ECHO instruction.
2256 */
2257 static int
2258generate_ECHO(cctx_T *cctx, int with_white, int count)
2259{
2260 isn_T *isn;
2261
Bram Moolenaar080457c2020-03-03 21:53:32 +01002262 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2264 return FAIL;
2265 isn->isn_arg.echo.echo_with_white = with_white;
2266 isn->isn_arg.echo.echo_count = count;
2267
2268 return OK;
2269}
2270
Bram Moolenaarad39c092020-02-26 18:23:43 +01002271/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002272 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002273 */
2274 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002275generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002276{
2277 isn_T *isn;
2278
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002279 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002280 return FAIL;
2281 isn->isn_arg.number = count;
2282
2283 return OK;
2284}
2285
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002286/*
2287 * Generate an ISN_PUT instruction.
2288 */
2289 static int
2290generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2291{
2292 isn_T *isn;
2293
2294 RETURN_OK_IF_SKIP(cctx);
2295 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2296 return FAIL;
2297 isn->isn_arg.put.put_regname = regname;
2298 isn->isn_arg.put.put_lnum = lnum;
2299 return OK;
2300}
2301
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002302/*
2303 * Generate an EXEC instruction that takes a string argument.
2304 * A copy is made of "line".
2305 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 static int
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002307generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308{
2309 isn_T *isn;
2310
Bram Moolenaar080457c2020-03-03 21:53:32 +01002311 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002312 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 return FAIL;
2314 isn->isn_arg.string = vim_strsave(line);
2315 return OK;
2316}
2317
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002318/*
2319 * Generate an EXEC instruction that takes a string argument.
2320 * "str" must be allocated, it is consumed.
2321 */
2322 static int
2323generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2324{
2325 isn_T *isn;
2326
2327 if (cctx->ctx_skip == SKIP_YES)
2328 {
2329 vim_free(str);
2330 return OK;
2331 }
2332 if ((isn = generate_instr(cctx, isntype)) == NULL)
2333 {
2334 vim_free(str);
2335 return FAIL;
2336 }
2337 isn->isn_arg.string = str;
2338 return OK;
2339}
2340
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002341 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002342generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2343{
2344 isn_T *isn;
2345 garray_T *stack = &cctx->ctx_type_stack;
2346
2347 RETURN_OK_IF_SKIP(cctx);
2348 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2349 return FAIL;
2350 isn->isn_arg.string = vim_strsave(line);
2351
Bram Moolenaar35578162021-08-02 19:10:38 +02002352 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002353 return FAIL;
2354 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2355 ++stack->ga_len;
2356
2357 return OK;
2358}
2359
2360 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002361generate_EXECCONCAT(cctx_T *cctx, int count)
2362{
2363 isn_T *isn;
2364
2365 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2366 return FAIL;
2367 isn->isn_arg.number = count;
2368 return OK;
2369}
2370
Bram Moolenaar08597872020-12-10 19:43:40 +01002371/*
2372 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2373 */
2374 static int
2375generate_RANGE(cctx_T *cctx, char_u *range)
2376{
2377 isn_T *isn;
2378 garray_T *stack = &cctx->ctx_type_stack;
2379
2380 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2381 return FAIL;
2382 isn->isn_arg.string = range;
2383
Bram Moolenaar35578162021-08-02 19:10:38 +02002384 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002385 return FAIL;
2386 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2387 ++stack->ga_len;
2388 return OK;
2389}
2390
Bram Moolenaar792f7862020-11-23 08:31:18 +01002391 static int
2392generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2393{
2394 isn_T *isn;
2395
2396 RETURN_OK_IF_SKIP(cctx);
2397 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2398 return FAIL;
2399 isn->isn_arg.unpack.unp_count = var_count;
2400 isn->isn_arg.unpack.unp_semicolon = semicolon;
2401 return OK;
2402}
2403
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002405 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002406 */
2407 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002408generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002409{
2410 isn_T *isn;
2411
Bram Moolenaar917c46a2021-08-10 19:53:01 +02002412 if (has_cmdmod(cmod, FALSE))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002413 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002414 cctx->ctx_has_cmdmod = TRUE;
2415
2416 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002417 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002418 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2419 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2420 return FAIL;
2421 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002422 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002423 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002424 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002425
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002426 return OK;
2427}
2428
2429 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002430generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002431{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002432 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2433 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002434 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002435 return OK;
2436}
2437
Bram Moolenaarfa984412021-03-25 22:15:28 +01002438 static int
2439misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002440{
2441 garray_T *instr = &cctx->ctx_instr;
2442
Bram Moolenaara91a7132021-03-25 21:12:15 +01002443 if (cctx->ctx_has_cmdmod
2444 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2445 == ISN_CMDMOD)
2446 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002447 emsg(_(e_misplaced_command_modifier));
2448 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002449 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002450 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002451}
2452
2453/*
2454 * Get the index of the current instruction.
Bram Moolenaar093165c2021-08-22 13:35:31 +02002455 * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
Bram Moolenaara91a7132021-03-25 21:12:15 +01002456 */
2457 static int
2458current_instr_idx(cctx_T *cctx)
2459{
2460 garray_T *instr = &cctx->ctx_instr;
2461 int idx = instr->ga_len;
2462
Bram Moolenaare99d4222021-06-13 14:01:26 +02002463 while (idx > 0)
2464 {
2465 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002466 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002467 {
2468 --idx;
2469 continue;
2470 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002471#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002472 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2473 {
2474 --idx;
2475 continue;
2476 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002477#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002478 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2479 {
2480 --idx;
2481 continue;
2482 }
2483 break;
2484 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002485 return idx;
2486}
2487
Bram Moolenaarf002a412021-01-24 13:34:18 +01002488#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002489 static void
2490may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2491{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002492 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002493 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002494}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002495#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002496
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002497/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002499 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002501 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002502reserve_local(
2503 cctx_T *cctx,
2504 char_u *name,
2505 size_t len,
2506 int isConst,
2507 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002510 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002512 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002514 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002515 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 }
2517
Bram Moolenaar35578162021-08-02 19:10:38 +02002518 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002519 return NULL;
2520 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002521 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002523 // Every local variable uses the next entry on the stack. We could re-use
2524 // the last ones when leaving a scope, but then variables used in a closure
2525 // might get overwritten. To keep things simple do not re-use stack
2526 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002527 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2528 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002529
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002530 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 lvar->lv_const = isConst;
2532 lvar->lv_type = type;
2533
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002534 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002535 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002536 return NULL;
2537 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2538 vim_strsave(lvar->lv_name);
2539 ++dfunc->df_var_names.ga_len;
2540
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002541 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542}
2543
2544/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002545 * Remove local variables above "new_top".
2546 */
2547 static void
2548unwind_locals(cctx_T *cctx, int new_top)
2549{
2550 if (cctx->ctx_locals.ga_len > new_top)
2551 {
2552 int idx;
2553 lvar_T *lvar;
2554
2555 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2556 {
2557 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2558 vim_free(lvar->lv_name);
2559 }
2560 }
2561 cctx->ctx_locals.ga_len = new_top;
2562}
2563
2564/*
2565 * Free all local variables.
2566 */
2567 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002568free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002569{
2570 unwind_locals(cctx, 0);
2571 ga_clear(&cctx->ctx_locals);
2572}
2573
2574/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002575 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2576 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2577 * error if the variable was defined with :const.
2578 */
2579 static int
2580check_item_writable(svar_T *sv, int check_writable, char_u *name)
2581{
2582 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2583 || (check_writable == ASSIGN_FINAL
2584 && sv->sv_const == ASSIGN_CONST))
2585 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002586 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002587 return FAIL;
2588 }
2589 return OK;
2590}
2591
2592/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002594 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 * Returns the index in "sn_var_vals" if found.
2596 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002597 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 */
2599 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002600get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601{
2602 hashtab_T *ht;
2603 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002604 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002605 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 int idx;
2607
Bram Moolenaare3d46852020-08-29 13:39:17 +02002608 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002610 if (sid == current_sctx.sc_sid)
2611 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002612 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002613
2614 if (sav == NULL)
2615 return -2;
2616 idx = sav->sav_var_vals_idx;
2617 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002618 if (check_item_writable(sv, check_writable, name) == FAIL)
2619 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002620 return idx;
2621 }
2622
2623 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 ht = &SCRIPT_VARS(sid);
2625 di = find_var_in_ht(ht, 0, name, TRUE);
2626 if (di == NULL)
2627 return -2;
2628
2629 // Now find the svar_T index in sn_var_vals.
2630 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2631 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002632 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 if (sv->sv_tv == &di->di_tv)
2634 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002635 if (check_item_writable(sv, check_writable, name) == FAIL)
2636 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 return idx;
2638 }
2639 }
2640 return -1;
2641}
2642
2643/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002644 * Find "name" in imported items of the current script or in "cctx" if not
2645 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 */
2647 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002648find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650 int idx;
2651
Bram Moolenaare3d46852020-08-29 13:39:17 +02002652 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002653 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 if (cctx != NULL)
2655 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2656 {
2657 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2658 + idx;
2659
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002660 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2661 : STRLEN(import->imp_name) == len
2662 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 return import;
2664 }
2665
Bram Moolenaarefa94442020-08-08 22:16:00 +02002666 return find_imported_in_script(name, len, current_sctx.sc_sid);
2667}
2668
2669 imported_T *
2670find_imported_in_script(char_u *name, size_t len, int sid)
2671{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002672 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002673 int idx;
2674
Bram Moolenaare3d46852020-08-29 13:39:17 +02002675 if (!SCRIPT_ID_VALID(sid))
2676 return NULL;
2677 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2679 {
2680 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2681
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002682 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2683 : STRLEN(import->imp_name) == len
2684 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 return import;
2686 }
2687 return NULL;
2688}
2689
2690/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002691 * Free all imported variables.
2692 */
2693 static void
2694free_imported(cctx_T *cctx)
2695{
2696 int idx;
2697
2698 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2699 {
2700 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2701
2702 vim_free(import->imp_name);
2703 }
2704 ga_clear(&cctx->ctx_imports);
2705}
2706
2707/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002708 * Return a pointer to the next line that isn't empty or only contains a
2709 * comment. Skips over white space.
2710 * Returns NULL if there is none.
2711 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002712 char_u *
2713peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002714{
2715 int lnum = cctx->ctx_lnum;
2716
2717 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2718 {
2719 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002720 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002721
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002722 // ignore NULLs inserted for continuation lines
2723 if (line != NULL)
2724 {
2725 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002726 if (vim9_bad_comment(p))
2727 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002728 if (*p != NUL && !vim9_comment_start(p))
2729 return p;
2730 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002731 }
2732 return NULL;
2733}
2734
2735/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002736 * Called when checking for a following operator at "arg". When the rest of
2737 * the line is empty or only a comment, peek the next line. If there is a next
2738 * line return a pointer to it and set "nextp".
2739 * Otherwise skip over white space.
2740 */
2741 static char_u *
2742may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2743{
2744 char_u *p = skipwhite(arg);
2745
2746 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002747 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002748 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002749 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002750 if (*nextp != NULL)
2751 return *nextp;
2752 }
2753 return p;
2754}
2755
2756/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002757 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002758 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002759 * Returns NULL when at the end.
2760 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002761 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002762next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002763{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002764 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002765
2766 do
2767 {
2768 ++cctx->ctx_lnum;
2769 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002770 {
2771 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002772 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002773 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002774 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002775 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002776 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002777 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002778 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002779 return line;
2780}
2781
2782/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002783 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002784 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002785 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002786 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2787 */
2788 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002789may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002790{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002791 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002792 if (vim9_bad_comment(*arg))
2793 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002794 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002795 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002796 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002797
2798 if (next == NULL)
2799 return FAIL;
2800 *arg = skipwhite(next);
2801 }
2802 return OK;
2803}
2804
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002805/*
2806 * Idem, and give an error when failed.
2807 */
2808 static int
2809may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2810{
2811 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2812 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002813 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002814 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002815 return FAIL;
2816 }
2817 return OK;
2818}
2819
2820
Bram Moolenaara5565e42020-05-09 15:44:01 +02002821// Structure passed between the compile_expr* functions to keep track of
2822// constants that have been parsed but for which no code was produced yet. If
2823// possible expressions on these constants are applied at compile time. If
2824// that is not possible, the code to push the constants needs to be generated
2825// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002826// Using 50 should be more than enough of 5 levels of ().
2827#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002828typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002829 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002830 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002831 int pp_is_const; // all generated code was constants, used for a
2832 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002833} ppconst_T;
2834
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002835static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002836static int compile_expr0(char_u **arg, cctx_T *cctx);
2837static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2838
Bram Moolenaara5565e42020-05-09 15:44:01 +02002839/*
2840 * Generate a PUSH instruction for "tv".
2841 * "tv" will be consumed or cleared.
2842 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2843 */
2844 static int
2845generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2846{
2847 if (tv != NULL)
2848 {
2849 switch (tv->v_type)
2850 {
2851 case VAR_UNKNOWN:
2852 break;
2853 case VAR_BOOL:
2854 generate_PUSHBOOL(cctx, tv->vval.v_number);
2855 break;
2856 case VAR_SPECIAL:
2857 generate_PUSHSPEC(cctx, tv->vval.v_number);
2858 break;
2859 case VAR_NUMBER:
2860 generate_PUSHNR(cctx, tv->vval.v_number);
2861 break;
2862#ifdef FEAT_FLOAT
2863 case VAR_FLOAT:
2864 generate_PUSHF(cctx, tv->vval.v_float);
2865 break;
2866#endif
2867 case VAR_BLOB:
2868 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2869 tv->vval.v_blob = NULL;
2870 break;
2871 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002872 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002873 tv->vval.v_string = NULL;
2874 break;
2875 default:
2876 iemsg("constant type not supported");
2877 clear_tv(tv);
2878 return FAIL;
2879 }
2880 tv->v_type = VAR_UNKNOWN;
2881 }
2882 return OK;
2883}
2884
2885/*
2886 * Generate code for any ppconst entries.
2887 */
2888 static int
2889generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2890{
2891 int i;
2892 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002893 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002894
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002895 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002896 for (i = 0; i < ppconst->pp_used; ++i)
2897 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2898 ret = FAIL;
2899 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002900 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002901 return ret;
2902}
2903
2904/*
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02002905 * Check that the last item of "ppconst" is a bool, if there is an item.
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002906 */
2907 static int
2908check_ppconst_bool(ppconst_T *ppconst)
2909{
2910 if (ppconst->pp_used > 0)
2911 {
2912 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002913 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002914
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002915 return check_typval_type(&t_bool, tv, where);
2916 }
2917 return OK;
2918}
2919
2920/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002921 * Clear ppconst constants. Used when failing.
2922 */
2923 static void
2924clear_ppconst(ppconst_T *ppconst)
2925{
2926 int i;
2927
2928 for (i = 0; i < ppconst->pp_used; ++i)
2929 clear_tv(&ppconst->pp_tv[i]);
2930 ppconst->pp_used = 0;
2931}
2932
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002933/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002934 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002935 * indexable value and the index or the two indexes of a slice.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002936 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002937 */
2938 static int
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002939compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002940{
2941 type_T **typep;
2942 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002943 vartype_T vartype;
2944 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002945
Bram Moolenaar261417b2021-05-06 21:04:55 +02002946 // We can index a list, dict and blob. If we don't know the type
2947 // we can use the index value type. If we still don't know use an "ANY"
2948 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002949 typep = ((type_T **)stack->ga_data) + stack->ga_len
2950 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002951 vartype = (*typep)->tt_type;
2952 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002953 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002954 if (*typep == &t_any && idxtype == &t_string)
2955 vartype = VAR_DICT;
2956 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002957 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002958 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002959 return FAIL;
2960 if (is_slice)
2961 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002962 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2963 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002964 FALSE, FALSE) == FAIL)
2965 return FAIL;
2966 }
2967 }
2968
Bram Moolenaar261417b2021-05-06 21:04:55 +02002969 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002970 {
2971 if (is_slice)
2972 {
2973 emsg(_(e_cannot_slice_dictionary));
2974 return FAIL;
2975 }
2976 if ((*typep)->tt_type == VAR_DICT)
2977 {
2978 *typep = (*typep)->tt_member;
2979 if (*typep == &t_unknown)
2980 // empty dict was used
2981 *typep = &t_any;
2982 }
2983 else
2984 {
2985 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2986 FALSE, FALSE) == FAIL)
2987 return FAIL;
2988 *typep = &t_any;
2989 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002990 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002991 return FAIL;
2992 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2993 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002994 if (keeping_dict != NULL)
2995 *keeping_dict = TRUE;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002996 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002997 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002998 {
2999 *typep = &t_string;
3000 if ((is_slice
3001 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3002 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
3003 return FAIL;
3004 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02003005 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003006 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003007 if (is_slice)
3008 {
3009 *typep = &t_blob;
3010 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
3011 return FAIL;
3012 }
3013 else
3014 {
3015 *typep = &t_number;
3016 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
3017 return FAIL;
3018 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02003019 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02003020 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003021 {
3022 if (is_slice)
3023 {
3024 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02003025 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02003026 2) == FAIL)
3027 return FAIL;
3028 }
3029 else
3030 {
3031 if ((*typep)->tt_type == VAR_LIST)
3032 {
3033 *typep = (*typep)->tt_member;
3034 if (*typep == &t_unknown)
3035 // empty list was used
3036 *typep = &t_any;
3037 }
3038 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02003039 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
3040 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003041 return FAIL;
3042 }
3043 }
3044 else
3045 {
3046 emsg(_(e_string_list_dict_or_blob_required));
3047 return FAIL;
3048 }
3049 return OK;
3050}
3051
3052/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003053 * Generate an instruction to load script-local variable "name", without the
3054 * leading "s:".
3055 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 */
3057 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003058compile_load_scriptvar(
3059 cctx_T *cctx,
3060 char_u *name, // variable NUL terminated
3061 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003062 char_u **end, // end of variable
3063 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064{
Bram Moolenaare3d46852020-08-29 13:39:17 +02003065 scriptitem_T *si;
3066 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 imported_T *import;
3068
Bram Moolenaare3d46852020-08-29 13:39:17 +02003069 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3070 return FAIL;
3071 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003072 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003073 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003075 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003076 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3077 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 }
3079 if (idx >= 0)
3080 {
3081 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3082
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003083 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 current_sctx.sc_sid, idx, sv->sv_type);
3085 return OK;
3086 }
3087
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003088 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 if (import != NULL)
3090 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003091 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003092 {
3093 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003094 char_u *exp_name;
3095 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003096 ufunc_T *ufunc;
3097 type_T *type;
3098
3099 // Used "import * as Name", need to lookup the member.
3100 if (*p != '.')
3101 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003102 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003103 return FAIL;
3104 }
3105 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003106 if (VIM_ISWHITE(*p))
3107 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003108 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003109 return FAIL;
3110 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003111
Bram Moolenaar1c991142020-07-04 13:15:31 +02003112 // isolate one name
3113 exp_name = p;
3114 while (eval_isnamec(*p))
3115 ++p;
3116 cc = *p;
3117 *p = NUL;
3118
Bram Moolenaaredba7072021-03-13 21:14:18 +01003119 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3120 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003121 *p = cc;
3122 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003123 *end = p;
3124
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003125 if (idx < 0)
3126 {
3127 if (*p == '(' && ufunc != NULL)
3128 {
3129 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3130 return OK;
3131 }
3132 return FAIL;
3133 }
3134
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003135 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3136 import->imp_sid,
3137 idx,
3138 type);
3139 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003140 else if (import->imp_funcname != NULL)
3141 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003142 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003143 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3144 import->imp_sid,
3145 import->imp_var_vals_idx,
3146 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 return OK;
3148 }
3149
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003150 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003151 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 return FAIL;
3153}
3154
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003155 static int
3156generate_funcref(cctx_T *cctx, char_u *name)
3157{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003158 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003159
3160 if (ufunc == NULL)
3161 return FAIL;
3162
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003163 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003164 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3165 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003166 == FAIL)
3167 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003168 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003169}
3170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171/*
3172 * Compile a variable name into a load instruction.
3173 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003174 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 * When "error" is FALSE do not give an error when not found.
3176 */
3177 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003178compile_load(
3179 char_u **arg,
3180 char_u *end_arg,
3181 cctx_T *cctx,
3182 int is_expr,
3183 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184{
3185 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003186 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003187 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003189 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190
3191 if (*(*arg + 1) == ':')
3192 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003193 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003194 {
3195 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196
Bram Moolenaarfa596382021-04-07 21:58:16 +02003197 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003198 switch (**arg)
3199 {
3200 case 'g': isn_type = ISN_LOADGDICT; break;
3201 case 'w': isn_type = ISN_LOADWDICT; break;
3202 case 't': isn_type = ISN_LOADTDICT; break;
3203 case 'b': isn_type = ISN_LOADBDICT; break;
3204 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003205 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003206 goto theend;
3207 }
3208 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3209 goto theend;
3210 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 else
3213 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003214 isntype_T isn_type = ISN_DROP;
3215
Bram Moolenaarfa596382021-04-07 21:58:16 +02003216 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003217 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3218 if (name == NULL)
3219 return FAIL;
3220
3221 switch (**arg)
3222 {
3223 case 'v': res = generate_LOADV(cctx, name, error);
3224 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003225 case 's': if (is_expr && ASCII_ISUPPER(*name)
3226 && find_func(name, FALSE, cctx) != NULL)
3227 res = generate_funcref(cctx, name);
3228 else
3229 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003230 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003231 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003232 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003233 {
3234 if (is_expr && ASCII_ISUPPER(*name)
3235 && find_func(name, FALSE, cctx) != NULL)
3236 res = generate_funcref(cctx, name);
3237 else
3238 isn_type = ISN_LOADG;
3239 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003240 else
3241 {
3242 isn_type = ISN_LOADAUTO;
3243 vim_free(name);
3244 name = vim_strnsave(*arg, end - *arg);
3245 if (name == NULL)
3246 return FAIL;
3247 }
3248 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003249 case 'w': isn_type = ISN_LOADW; break;
3250 case 't': isn_type = ISN_LOADT; break;
3251 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003252 default: // cannot happen, just in case
3253 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003254 goto theend;
3255 }
3256 if (isn_type != ISN_DROP)
3257 {
3258 // Global, Buffer-local, Window-local and Tabpage-local
3259 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003260 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003261 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3262 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 }
3264 }
3265 else
3266 {
3267 size_t len = end - *arg;
3268 int idx;
3269 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003270 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271
3272 name = vim_strnsave(*arg, end - *arg);
3273 if (name == NULL)
3274 return FAIL;
3275
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003276 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3277 {
3278 script_autoload(name, FALSE);
3279 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3280 }
3281 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3282 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003284 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003285 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 }
3287 else
3288 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003289 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003290
Bram Moolenaar709664c2020-12-12 14:33:41 +01003291 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003293 type = lvar.lv_type;
3294 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003295 if (lvar.lv_from_outer != 0)
3296 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003297 else
3298 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 }
3300 else
3301 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003302 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003303 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003304 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003305 || find_imported(name, 0, cctx) != NULL)
3306 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003307
Bram Moolenaar0f769812020-09-12 18:32:34 +02003308 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003309 // uppercase letter it can be a user defined function.
3310 // generate_funcref() will fail if the function can't be found.
3311 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003312 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 }
3314 }
3315 if (gen_load)
3316 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003317 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003318 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003319 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003320 cctx->ctx_outer_used = TRUE;
3321 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 }
3323
3324 *arg = end;
3325
3326theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003327 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003328 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 vim_free(name);
3330 return res;
3331}
3332
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003333 static void
3334clear_instr_ga(garray_T *gap)
3335{
3336 int idx;
3337
3338 for (idx = 0; idx < gap->ga_len; ++idx)
3339 delete_instr(((isn_T *)gap->ga_data) + idx);
3340 ga_clear(gap);
3341}
3342
3343/*
3344 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3345 * Returns FAIL if compilation fails.
3346 */
3347 static int
3348compile_string(isn_T *isn, cctx_T *cctx)
3349{
3350 char_u *s = isn->isn_arg.string;
3351 garray_T save_ga = cctx->ctx_instr;
3352 int expr_res;
3353 int trailing_error;
3354 int instr_count;
3355 isn_T *instr = NULL;
3356
Bram Moolenaarcd268012021-07-22 19:11:08 +02003357 // Remove the string type from the stack.
3358 --cctx->ctx_type_stack.ga_len;
3359
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003360 // Temporarily reset the list of instructions so that the jump labels are
3361 // correct.
3362 cctx->ctx_instr.ga_len = 0;
3363 cctx->ctx_instr.ga_maxlen = 0;
3364 cctx->ctx_instr.ga_data = NULL;
3365 expr_res = compile_expr0(&s, cctx);
3366 s = skipwhite(s);
3367 trailing_error = *s != NUL;
3368
Bram Moolenaarff652882021-05-16 15:24:49 +02003369 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003370 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003371 {
3372 if (trailing_error)
3373 semsg(_(e_trailing_arg), s);
3374 clear_instr_ga(&cctx->ctx_instr);
3375 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003376 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003377 return FAIL;
3378 }
3379
3380 // Move the generated instructions into the ISN_INSTR instruction, then
3381 // restore the list of instructions.
3382 instr_count = cctx->ctx_instr.ga_len;
3383 instr = cctx->ctx_instr.ga_data;
3384 instr[instr_count].isn_type = ISN_FINISH;
3385
3386 cctx->ctx_instr = save_ga;
3387 vim_free(isn->isn_arg.string);
3388 isn->isn_type = ISN_INSTR;
3389 isn->isn_arg.instr = instr;
3390 return OK;
3391}
3392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393/*
3394 * Compile the argument expressions.
3395 * "arg" points to just after the "(" and is advanced to after the ")"
3396 */
3397 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003398compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003400 char_u *p = *arg;
3401 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003402 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003403 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404
Bram Moolenaare6085c52020-04-12 20:19:16 +02003405 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003407 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3408 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003409 if (*p == ')')
3410 {
3411 *arg = p + 1;
3412 return OK;
3413 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003414 if (must_end)
3415 {
3416 semsg(_(e_missing_comma_before_argument_str), p);
3417 return FAIL;
3418 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003419
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003420 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003421 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 return FAIL;
3423 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003424
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003425 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003426 && cctx->ctx_instr.ga_len == instr_count + 1)
3427 {
3428 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3429
3430 // {skip} argument of searchpair() can be compiled if not empty
3431 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3432 compile_string(isn, cctx);
3433 }
3434
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003435 if (*p != ',' && *skipwhite(p) == ',')
3436 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003437 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003438 p = skipwhite(p);
3439 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003441 {
3442 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003443 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003444 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003445 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003446 else
3447 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003448 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003449 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003451failret:
Bram Moolenaare1242042021-12-16 20:56:57 +00003452 emsg(_(e_missing_closing_paren));
Bram Moolenaare6085c52020-04-12 20:19:16 +02003453 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454}
3455
3456/*
3457 * Compile a function call: name(arg1, arg2)
3458 * "arg" points to "name", "arg + varlen" to the "(".
3459 * "argcount_init" is 1 for "value->method()"
3460 * Instructions:
3461 * EVAL arg1
3462 * EVAL arg2
3463 * BCALL / DCALL / UCALL
3464 */
3465 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003466compile_call(
3467 char_u **arg,
3468 size_t varlen,
3469 cctx_T *cctx,
3470 ppconst_T *ppconst,
3471 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472{
3473 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003474 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 int argcount = argcount_init;
3476 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003477 char_u fname_buf[FLEN_FIXED + 1];
3478 char_u *tofree = NULL;
3479 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003480 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003481 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003482 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003483 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003485 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003486 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003487 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003488 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003489 {
3490 char_u *s = skipwhite(*arg + varlen + 1);
3491 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003492 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003493
3494 argvars[0].v_type = VAR_UNKNOWN;
3495 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003496 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003497 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003498 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003499 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003500 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003501 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003502 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003503 {
3504 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3505
3506 *arg = s + 1;
3507 argvars[1].v_type = VAR_UNKNOWN;
3508 tv->v_type = VAR_NUMBER;
3509 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003510 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003511 f_has(argvars, tv);
3512 else
3513 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003514 clear_tv(&argvars[0]);
3515 ++ppconst->pp_used;
3516 return OK;
3517 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003518 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003519 if (!is_has)
3520 {
3521 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3522 return FAIL;
3523 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003524 }
3525
3526 if (generate_ppconst(cctx, ppconst) == FAIL)
3527 return FAIL;
3528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 if (varlen >= sizeof(namebuf))
3530 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003531 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 return FAIL;
3533 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003534 vim_strncpy(namebuf, *arg, varlen);
3535 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003537 // We handle the "skip" argument of searchpair() and searchpairpos()
3538 // differently.
3539 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3540 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3541 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3542 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003543
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003545 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003546 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547
Bram Moolenaar03290b82020-12-19 16:30:44 +01003548 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003549 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 {
3551 int idx;
3552
3553 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003554 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003556 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003557 if (STRCMP(name, "flatten") == 0)
3558 {
3559 emsg(_(e_cannot_use_flatten_in_vim9_script));
3560 goto theend;
3561 }
3562
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003563 if (STRCMP(name, "add") == 0 && argcount == 2)
3564 {
3565 garray_T *stack = &cctx->ctx_type_stack;
3566 type_T *type = ((type_T **)stack->ga_data)[
3567 stack->ga_len - 2];
3568
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003569 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003570 if (type->tt_type == VAR_LIST)
3571 {
3572 // inline "add(list, item)" so that the type can be checked
3573 res = generate_LISTAPPEND(cctx);
3574 idx = -1;
3575 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003576 else if (type->tt_type == VAR_BLOB)
3577 {
3578 // inline "add(blob, nr)" so that the type can be checked
3579 res = generate_BLOBAPPEND(cctx);
3580 idx = -1;
3581 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003582 }
3583
3584 if (idx >= 0)
3585 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3586 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003587 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003588 semsg(_(e_unknown_function_str), namebuf);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003589 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 }
3591
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003592 // An argument or local variable can be a function reference, this
3593 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003594 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003595 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003596 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003597 // If we can find the function by name generate the right call.
3598 // Skip global functions here, a local funcref takes precedence.
3599 ufunc = find_func(name, FALSE, cctx);
3600 if (ufunc != NULL && !func_is_global(ufunc))
3601 {
3602 res = generate_CALL(cctx, ufunc, argcount);
3603 goto theend;
3604 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606
3607 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003608 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003609 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003611 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003612 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003613 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003614 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003615 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003616
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003617 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003618 goto theend;
3619 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620
Bram Moolenaar0f769812020-09-12 18:32:34 +02003621 // If we can find a global function by name generate the right call.
3622 if (ufunc != NULL)
3623 {
3624 res = generate_CALL(cctx, ufunc, argcount);
3625 goto theend;
3626 }
3627
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003628 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003629 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003630 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003631 res = generate_UCALL(cctx, name, argcount);
3632 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003633 semsg(_(e_unknown_function_str), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003634
3635theend:
3636 vim_free(tofree);
3637 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638}
3639
3640// like NAMESPACE_CHAR but with 'a' and 'l'.
3641#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3642
3643/*
3644 * Find the end of a variable or function name. Unlike find_name_end() this
3645 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003646 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 * Return a pointer to just after the name. Equal to "arg" if there is no
3648 * valid name.
3649 */
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003650 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003651to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652{
3653 char_u *p;
3654
3655 // Quick check for valid starting character.
3656 if (!eval_isnamec1(*arg))
3657 return arg;
3658
3659 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3660 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3661 // and can be used in slice "[n:]".
3662 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003663 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3665 break;
3666 return p;
3667}
3668
3669/*
3670 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003671 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003672 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 */
3674 char_u *
3675to_name_const_end(char_u *arg)
3676{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003677 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 typval_T rettv;
3679
Bram Moolenaar678b2072021-07-26 21:10:11 +02003680 if (STRNCMP(p, "<SNR>", 5) == 0)
3681 p = skipdigits(p + 5);
3682 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 if (p == arg && *arg == '[')
3684 {
3685
3686 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003687 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 p = arg;
3689 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 return p;
3691}
3692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693/*
3694 * parse a list: [expr, expr]
3695 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003696 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 */
3698 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003699compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700{
3701 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003702 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003704 int is_const;
3705 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003707 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003709 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003710 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003711 semsg(_(e_list_end), *arg);
3712 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003713 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003714 if (*p == ',')
3715 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003716 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003717 return FAIL;
3718 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003719 if (*p == ']')
3720 {
3721 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003722 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003723 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003724 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003725 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003726 if (!is_const)
3727 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 ++count;
3729 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003730 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003732 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3733 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003734 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003735 return FAIL;
3736 }
3737 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003738 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 p = skipwhite(p);
3740 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003741 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003743 ppconst->pp_is_const = is_all_const;
3744 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745}
3746
3747/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003748 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003749 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003750 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 */
3752 static int
3753compile_lambda(char_u **arg, cctx_T *cctx)
3754{
Bram Moolenaare462f522020-12-27 14:43:30 +01003755 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 typval_T rettv;
3757 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003758 evalarg_T evalarg;
3759
Bram Moolenaar844fb642021-10-23 13:32:30 +01003760 init_evalarg(&evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003761 evalarg.eval_flags = EVAL_EVALUATE;
3762 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763
3764 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003765 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3766 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003767 {
3768 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003769 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003770 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003771
Bram Moolenaar65c44152020-12-24 15:14:01 +01003772 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003774 ++ufunc->uf_refcount;
3775 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776
Bram Moolenaara9931532021-06-12 15:58:16 +02003777 // Compile it here to get the return type. The return type is optional,
3778 // when it's missing use t_unknown. This is recognized in
3779 // compile_return().
3780 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3781 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003782 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003784 // When the outer function is compiled for profiling or debugging, the
3785 // lambda may be called without profiling or debugging. Compile it here in
3786 // the right context.
3787 if (cctx->ctx_compile_type == CT_DEBUG
Bram Moolenaar648594e2021-07-11 17:55:01 +02003788#ifdef FEAT_PROFILE
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003789 || cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar648594e2021-07-11 17:55:01 +02003790#endif
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003791 )
3792 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaard9162552021-07-11 15:26:13 +02003793
Bram Moolenaar844fb642021-10-23 13:32:30 +01003794 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
3795 // "*arg" may point into it. Point into the original line to avoid a
3796 // dangling pointer.
3797 if (evalarg.eval_using_cmdline)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003798 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003799 garray_T *gap = &evalarg.eval_tofree_ga;
3800 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003801
3802 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3803 + off;
3804 }
3805
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003806 clear_evalarg(&evalarg, NULL);
3807
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003808 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003809 {
3810 // The return type will now be known.
3811 set_function_type(ufunc);
3812
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003813 // The function reference count will be 1. When the ISN_FUNCREF
3814 // instruction is deleted the reference count is decremented and the
3815 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003816 return generate_FUNCREF(cctx, ufunc);
3817 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003818
3819 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 return FAIL;
3821}
3822
3823/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003824 * Get a lambda and compile it. Uses Vim9 syntax.
3825 */
3826 int
3827get_lambda_tv_and_compile(
3828 char_u **arg,
3829 typval_T *rettv,
3830 int types_optional,
3831 evalarg_T *evalarg)
3832{
3833 int r;
3834 ufunc_T *ufunc;
3835 int save_sc_version = current_sctx.sc_version;
3836
3837 // Get the funcref in "rettv".
3838 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3839 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3840 current_sctx.sc_version = save_sc_version;
3841 if (r != OK)
3842 return r;
3843
3844 // "rettv" will now be a partial referencing the function.
3845 ufunc = rettv->vval.v_partial->pt_func;
3846
3847 // Compile it here to get the return type. The return type is optional,
3848 // when it's missing use t_unknown. This is recognized in
3849 // compile_return().
3850 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3851 ufunc->uf_ret_type = &t_unknown;
3852 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3853
3854 if (ufunc->uf_def_status == UF_COMPILED)
3855 {
3856 // The return type will now be known.
3857 set_function_type(ufunc);
3858 return OK;
3859 }
3860 clear_tv(rettv);
3861 return FAIL;
3862}
3863
3864/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003865 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003867 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 */
3869 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003870compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871{
3872 garray_T *instr = &cctx->ctx_instr;
3873 int count = 0;
3874 dict_T *d = dict_alloc();
3875 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003876 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003877 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003878 int is_const;
3879 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880
3881 if (d == NULL)
3882 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003883 if (generate_ppconst(cctx, ppconst) == FAIL)
3884 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003885 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003887 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888
Bram Moolenaar23c55272020-06-21 16:58:13 +02003889 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003890 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003891 *arg = NULL;
3892 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003893 }
3894
3895 if (**arg == '}')
3896 break;
3897
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003898 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003900 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003902 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003903 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003904 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003907 if (isn->isn_type == ISN_PUSHNR)
3908 {
3909 char buf[NUMBUFLEN];
3910
3911 // Convert to string at compile time.
3912 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3913 isn->isn_type = ISN_PUSHS;
3914 isn->isn_arg.string = vim_strsave((char_u *)buf);
3915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 if (isn->isn_type == ISN_PUSHS)
3917 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003918 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003919 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003920 *arg = skipwhite(*arg);
3921 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003922 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003923 emsg(_(e_missing_matching_bracket_after_dict_key));
3924 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003925 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003926 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003928 else
3929 {
3930 // {"name": value},
3931 // {'name': value},
3932 // {name: value} use "name" as a literal key
3933 key = get_literal_key(arg);
3934 if (key == NULL)
3935 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003936 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003937 return FAIL;
3938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939
3940 // Check for duplicate keys, if using string keys.
3941 if (key != NULL)
3942 {
3943 item = dict_find(d, key, -1);
3944 if (item != NULL)
3945 {
3946 semsg(_(e_duplicate_key), key);
3947 goto failret;
3948 }
3949 item = dictitem_alloc(key);
3950 if (item != NULL)
3951 {
3952 item->di_tv.v_type = VAR_UNKNOWN;
3953 item->di_tv.v_lock = 0;
3954 if (dict_add(d, item) == FAIL)
3955 dictitem_free(item);
3956 }
3957 }
3958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 if (**arg != ':')
3960 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003961 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003962 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003963 else
3964 semsg(_(e_missing_dict_colon), *arg);
3965 return FAIL;
3966 }
3967 whitep = *arg + 1;
3968 if (!IS_WHITE_OR_NUL(*whitep))
3969 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003970 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 return FAIL;
3972 }
3973
Bram Moolenaar23c55272020-06-21 16:58:13 +02003974 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003975 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003976 *arg = NULL;
3977 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003978 }
3979
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003980 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003982 if (!is_const)
3983 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 ++count;
3985
Bram Moolenaar2c330432020-04-13 14:41:35 +02003986 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003987 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003988 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003989 *arg = NULL;
3990 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 if (**arg == '}')
3993 break;
3994 if (**arg != ',')
3995 {
3996 semsg(_(e_missing_dict_comma), *arg);
3997 goto failret;
3998 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003999 if (IS_WHITE_OR_NUL(*whitep))
4000 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004001 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02004002 return FAIL;
4003 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02004004 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02004005 if (!IS_WHITE_OR_NUL(*whitep))
4006 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01004007 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02004008 return FAIL;
4009 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01004010 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 }
4012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 *arg = *arg + 1;
4014
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004015 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02004016 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004017 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004018 *arg += STRLEN(*arg);
4019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004021 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 return generate_NEWDICT(cctx, count);
4023
4024failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004025 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004026 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004027 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004028 *arg = (char_u *)"";
4029 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 dict_unref(d);
4031 return FAIL;
4032}
4033
4034/*
4035 * Compile "&option".
4036 */
4037 static int
4038compile_get_option(char_u **arg, cctx_T *cctx)
4039{
4040 typval_T rettv;
4041 char_u *start = *arg;
4042 int ret;
4043
4044 // parse the option and get the current value to get the type.
4045 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004046 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 if (ret == OK)
4048 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004049 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01004050 char_u *name = vim_strnsave(start, *arg - start);
4051 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
4052 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053
4054 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4055 vim_free(name);
4056 }
4057 clear_tv(&rettv);
4058
4059 return ret;
4060}
4061
4062/*
4063 * Compile "$VAR".
4064 */
4065 static int
4066compile_get_env(char_u **arg, cctx_T *cctx)
4067{
4068 char_u *start = *arg;
4069 int len;
4070 int ret;
4071 char_u *name;
4072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 ++*arg;
4074 len = get_env_len(arg);
4075 if (len == 0)
4076 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004077 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 return FAIL;
4079 }
4080
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004081 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 name = vim_strnsave(start, len + 1);
4083 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4084 vim_free(name);
4085 return ret;
4086}
4087
4088/*
4089 * Compile "@r".
4090 */
4091 static int
4092compile_get_register(char_u **arg, cctx_T *cctx)
4093{
4094 int ret;
4095
4096 ++*arg;
4097 if (**arg == NUL)
4098 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004099 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 return FAIL;
4101 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004102 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 {
4104 emsg_invreg(**arg);
4105 return FAIL;
4106 }
4107 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4108 ++*arg;
4109 return ret;
4110}
4111
4112/*
4113 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004114 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 */
4116 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004117apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004119 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120
4121 // this works from end to start
4122 while (p > start)
4123 {
4124 --p;
4125 if (*p == '-' || *p == '+')
4126 {
4127 // only '-' has an effect, for '+' we only check the type
4128#ifdef FEAT_FLOAT
4129 if (rettv->v_type == VAR_FLOAT)
4130 {
4131 if (*p == '-')
4132 rettv->vval.v_float = -rettv->vval.v_float;
4133 }
4134 else
4135#endif
4136 {
4137 varnumber_T val;
4138 int error = FALSE;
4139
4140 // tv_get_number_chk() accepts a string, but we don't want that
4141 // here
4142 if (check_not_string(rettv) == FAIL)
4143 return FAIL;
4144 val = tv_get_number_chk(rettv, &error);
4145 clear_tv(rettv);
4146 if (error)
4147 return FAIL;
4148 if (*p == '-')
4149 val = -val;
4150 rettv->v_type = VAR_NUMBER;
4151 rettv->vval.v_number = val;
4152 }
4153 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004154 else if (numeric_only)
4155 {
4156 ++p;
4157 break;
4158 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004159 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 {
4161 int v = tv2bool(rettv);
4162
4163 // '!' is permissive in the type.
4164 clear_tv(rettv);
4165 rettv->v_type = VAR_BOOL;
4166 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4167 }
4168 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004169 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 return OK;
4171}
4172
4173/*
4174 * Recognize v: variables that are constants and set "rettv".
4175 */
4176 static void
4177get_vim_constant(char_u **arg, typval_T *rettv)
4178{
4179 if (STRNCMP(*arg, "v:true", 6) == 0)
4180 {
4181 rettv->v_type = VAR_BOOL;
4182 rettv->vval.v_number = VVAL_TRUE;
4183 *arg += 6;
4184 }
4185 else if (STRNCMP(*arg, "v:false", 7) == 0)
4186 {
4187 rettv->v_type = VAR_BOOL;
4188 rettv->vval.v_number = VVAL_FALSE;
4189 *arg += 7;
4190 }
4191 else if (STRNCMP(*arg, "v:null", 6) == 0)
4192 {
4193 rettv->v_type = VAR_SPECIAL;
4194 rettv->vval.v_number = VVAL_NULL;
4195 *arg += 6;
4196 }
4197 else if (STRNCMP(*arg, "v:none", 6) == 0)
4198 {
4199 rettv->v_type = VAR_SPECIAL;
4200 rettv->vval.v_number = VVAL_NONE;
4201 *arg += 6;
4202 }
4203}
4204
Bram Moolenaar657137c2021-01-09 15:45:23 +01004205 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004206get_compare_type(char_u *p, int *len, int *type_is)
4207{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004208 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004209 int i;
4210
4211 switch (p[0])
4212 {
4213 case '=': if (p[1] == '=')
4214 type = EXPR_EQUAL;
4215 else if (p[1] == '~')
4216 type = EXPR_MATCH;
4217 break;
4218 case '!': if (p[1] == '=')
4219 type = EXPR_NEQUAL;
4220 else if (p[1] == '~')
4221 type = EXPR_NOMATCH;
4222 break;
4223 case '>': if (p[1] != '=')
4224 {
4225 type = EXPR_GREATER;
4226 *len = 1;
4227 }
4228 else
4229 type = EXPR_GEQUAL;
4230 break;
4231 case '<': if (p[1] != '=')
4232 {
4233 type = EXPR_SMALLER;
4234 *len = 1;
4235 }
4236 else
4237 type = EXPR_SEQUAL;
4238 break;
4239 case 'i': if (p[1] == 's')
4240 {
4241 // "is" and "isnot"; but not a prefix of a name
4242 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4243 *len = 5;
4244 i = p[*len];
4245 if (!isalnum(i) && i != '_')
4246 {
4247 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4248 *type_is = TRUE;
4249 }
4250 }
4251 break;
4252 }
4253 return type;
4254}
4255
4256/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004257 * Skip over an expression, ignoring most errors.
4258 */
4259 static void
4260skip_expr_cctx(char_u **arg, cctx_T *cctx)
4261{
4262 evalarg_T evalarg;
4263
Bram Moolenaar844fb642021-10-23 13:32:30 +01004264 init_evalarg(&evalarg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004265 evalarg.eval_cctx = cctx;
4266 skip_expr(arg, &evalarg);
Bram Moolenaar844fb642021-10-23 13:32:30 +01004267 clear_evalarg(&evalarg, NULL);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004268}
4269
4270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004272 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 */
4274 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004275compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004277 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278
4279 // this works from end to start
4280 while (p > start)
4281 {
4282 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004283 while (VIM_ISWHITE(*p))
4284 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 if (*p == '-' || *p == '+')
4286 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004287 int negate = *p == '-';
4288 isn_T *isn;
4289 garray_T *stack = &cctx->ctx_type_stack;
4290 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004292 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4293 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4294 return FAIL;
4295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4297 {
4298 --p;
4299 if (*p == '-')
4300 negate = !negate;
4301 }
4302 // only '-' has an effect, for '+' we only check the type
4303 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004304 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004306 if (isn == NULL)
4307 return FAIL;
4308 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004310 else if (numeric_only)
4311 {
4312 ++p;
4313 break;
4314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 else
4316 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004317 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004319 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004321 if (p[-1] == '!')
4322 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004325 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 return FAIL;
4327 }
4328 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004329 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 return OK;
4331}
4332
4333/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004334 * Compile "(expression)": recursive!
4335 * Return FAIL/OK.
4336 */
4337 static int
4338compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4339{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004340 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004341 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004342
Bram Moolenaar24156692021-01-14 20:35:49 +01004343 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4344 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004345 if (ppconst->pp_used <= PPSIZE - 10)
4346 {
4347 ret = compile_expr1(arg, cctx, ppconst);
4348 }
4349 else
4350 {
4351 // Not enough space in ppconst, flush constants.
4352 if (generate_ppconst(cctx, ppconst) == FAIL)
4353 return FAIL;
4354 ret = compile_expr0(arg, cctx);
4355 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004356 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4357 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004358 if (**arg == ')')
4359 ++*arg;
4360 else if (ret == OK)
4361 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004362 emsg(_(e_missing_closing_paren));
Bram Moolenaar7e368202020-12-25 21:56:57 +01004363 ret = FAIL;
4364 }
4365 return ret;
4366}
4367
4368/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004370 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 */
4372 static int
4373compile_subscript(
4374 char_u **arg,
4375 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004376 char_u *start_leader,
4377 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004378 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004380 char_u *name_start = *end_leader;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004381 int keeping_dict = FALSE;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 for (;;)
4384 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004385 char_u *p = skipwhite(*arg);
4386
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004387 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004388 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004389 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004390
4391 // If a following line starts with "->{" or "->X" advance to that
4392 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004393 // Also if a following line starts with ".x".
4394 if (next != NULL &&
4395 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004396 && (next[2] == '{'
4397 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004398 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004399 {
4400 next = next_line_from_context(cctx, TRUE);
4401 if (next == NULL)
4402 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004403 *arg = next;
4404 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004405 }
4406 }
4407
Bram Moolenaarcd268012021-07-22 19:11:08 +02004408 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4409 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004410 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004412 garray_T *stack = &cctx->ctx_type_stack;
4413 type_T *type;
4414 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004416 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004417 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004418 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004421 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4422
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004423 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004424 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004426 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004428 if (keeping_dict)
4429 {
4430 keeping_dict = FALSE;
4431 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4432 return FAIL;
4433 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004435 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004437 char_u *pstart = p;
4438
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004439 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004440 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004441 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 // something->method()
4444 // Apply the '!', '-' and '+' first:
4445 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004446 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004449 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004450 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004451 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004452 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004453 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004454 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004455 garray_T *stack = &cctx->ctx_type_stack;
4456 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004457 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004458 int expr_isn_start = cctx->ctx_instr.ga_len;
4459 int expr_isn_end;
4460 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004461
4462 // Funcref call: list->(Refs[2])(arg)
4463 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004464 //
4465 // Fist compile the function expression.
4466 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004467 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004468
4469 // Remember the next instruction index, where the instructions
4470 // for arguments are being written.
4471 expr_isn_end = cctx->ctx_instr.ga_len;
4472
4473 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004474 if (**arg != '(')
4475 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004476 if (*skipwhite(*arg) == '(')
4477 emsg(_(e_nowhitespace));
4478 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004479 semsg(_(e_missing_parenthesis_str), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004480 return FAIL;
4481 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004482 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004483 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004484 return FAIL;
4485
Bram Moolenaar2927c072021-04-05 19:41:21 +02004486 // Move the instructions for the arguments to before the
4487 // instructions of the expression and move the type of the
4488 // expression after the argument types. This is what ISN_PCALL
4489 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004490 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004491 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4492 if (arg_isn_count > 0)
4493 {
4494 int expr_isn_count = expr_isn_end - expr_isn_start;
4495 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4496
4497 if (isn == NULL)
4498 return FAIL;
4499 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4500 + expr_isn_start,
4501 sizeof(isn_T) * expr_isn_count);
4502 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4503 + expr_isn_start,
4504 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4505 sizeof(isn_T) * arg_isn_count);
4506 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4507 + expr_isn_start + arg_isn_count,
4508 isn, sizeof(isn_T) * expr_isn_count);
4509 vim_free(isn);
4510
4511 type = ((type_T **)stack->ga_data)[type_idx_start];
4512 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4513 ((type_T **)stack->ga_data) + type_idx_start + 1,
4514 sizeof(type_T *)
4515 * (stack->ga_len - type_idx_start - 1));
4516 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4517 }
4518
Bram Moolenaar7e368202020-12-25 21:56:57 +01004519 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004520 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 return FAIL;
4522 }
4523 else
4524 {
4525 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004526 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004527 if (!eval_isnamec1(*p))
4528 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004529 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004530 return FAIL;
4531 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004532 if (ASCII_ISALPHA(*p) && p[1] == ':')
4533 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004534 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 ;
4536 if (*p != '(')
4537 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004538 semsg(_(e_missing_parenthesis_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 return FAIL;
4540 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004541 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 return FAIL;
4543 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004544 if (keeping_dict)
4545 {
4546 keeping_dict = FALSE;
4547 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4548 return FAIL;
4549 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004551 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004553 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004554
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004555 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004556 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004557 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004558 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004559 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004560 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004561 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004562
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004563 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004564 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004565 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004566 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004567 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004568 // missing first index is equal to zero
4569 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004570 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004571 else
4572 {
4573 if (compile_expr0(arg, cctx) == FAIL)
4574 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004575 if (**arg == ':')
4576 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004577 semsg(_(e_white_space_required_before_and_after_str_at_str),
4578 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004579 return FAIL;
4580 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004581 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004582 return FAIL;
4583 *arg = skipwhite(*arg);
4584 }
4585 if (**arg == ':')
4586 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004587 is_slice = TRUE;
4588 ++*arg;
4589 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4590 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004591 semsg(_(e_white_space_required_before_and_after_str_at_str),
4592 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004593 return FAIL;
4594 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004595 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004596 return FAIL;
4597 if (**arg == ']')
4598 // missing second index is equal to end of string
4599 generate_PUSHNR(cctx, -1);
4600 else
4601 {
4602 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004603 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004604 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004605 return FAIL;
4606 *arg = skipwhite(*arg);
4607 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609
4610 if (**arg != ']')
4611 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004612 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 return FAIL;
4614 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004615 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004617 if (keeping_dict)
4618 {
4619 keeping_dict = FALSE;
4620 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4621 return FAIL;
4622 }
4623 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004624 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004626 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004628 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004629 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004630 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004631 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004632
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004633 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004634 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004635 {
4636 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004637 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004638 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004639 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004640 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 while (eval_isnamec(*p))
4642 MB_PTR_ADV(p);
4643 if (p == *arg)
4644 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004645 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 return FAIL;
4647 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004648 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
4649 return FAIL;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004650 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004652 keeping_dict = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 *arg = p;
4654 }
4655 else
4656 break;
4657 }
4658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 // Turn "dict.Func" into a partial for "Func" bound to "dict".
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004660 // This needs to be done at runtime to be able to check the type.
4661 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
4662 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663
4664 return OK;
4665}
4666
4667/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004668 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4669 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004671 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004672 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004673 *
4674 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 */
4676
4677/*
4678 * number number constant
4679 * 0zFFFFFFFF Blob constant
4680 * "string" string constant
4681 * 'string' literal string constant
4682 * &option-name option value
4683 * @r register contents
4684 * identifier variable value
4685 * function() function call
4686 * $VAR environment variable
4687 * (expression) nested expression
4688 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004689 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 *
4691 * Also handle:
4692 * ! in front logical NOT
4693 * - in front unary minus
4694 * + in front unary plus (ignored)
4695 * trailing (arg) funcref/partial call
4696 * trailing [] subscript in String or List
4697 * trailing .name entry in Dictionary
4698 * trailing ->name() method call
4699 */
4700 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004701compile_expr7(
4702 char_u **arg,
4703 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004704 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 char_u *start_leader, *end_leader;
4707 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004708 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004709 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004711 ppconst->pp_is_const = FALSE;
4712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 /*
4714 * Skip '!', '-' and '+' characters. They are handled later.
4715 */
4716 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004717 if (eval_leader(arg, TRUE) == FAIL)
4718 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719 end_leader = *arg;
4720
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004721 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 switch (**arg)
4723 {
4724 /*
4725 * Number constant.
4726 */
4727 case '0': // also for blob starting with 0z
4728 case '1':
4729 case '2':
4730 case '3':
4731 case '4':
4732 case '5':
4733 case '6':
4734 case '7':
4735 case '8':
4736 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004737 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004739 // Apply "-" and "+" just before the number now, right to
4740 // left. Matters especially when "->" follows. Stops at
4741 // '!'.
4742 if (apply_leader(rettv, TRUE,
4743 start_leader, &end_leader) == FAIL)
4744 {
4745 clear_tv(rettv);
4746 return FAIL;
4747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 break;
4749
4750 /*
4751 * String constant: "string".
4752 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004753 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754 return FAIL;
4755 break;
4756
4757 /*
4758 * Literal string constant: 'str''ing'.
4759 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004760 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 return FAIL;
4762 break;
4763
4764 /*
4765 * Constant Vim variable.
4766 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004767 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768 ret = NOTDONE;
4769 break;
4770
4771 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004772 * "true" constant
4773 */
4774 case 't': if (STRNCMP(*arg, "true", 4) == 0
4775 && !eval_isnamec((*arg)[4]))
4776 {
4777 *arg += 4;
4778 rettv->v_type = VAR_BOOL;
4779 rettv->vval.v_number = VVAL_TRUE;
4780 }
4781 else
4782 ret = NOTDONE;
4783 break;
4784
4785 /*
4786 * "false" constant
4787 */
4788 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4789 && !eval_isnamec((*arg)[5]))
4790 {
4791 *arg += 5;
4792 rettv->v_type = VAR_BOOL;
4793 rettv->vval.v_number = VVAL_FALSE;
4794 }
4795 else
4796 ret = NOTDONE;
4797 break;
4798
4799 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004800 * "null" constant
4801 */
4802 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004803 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004804 {
4805 *arg += 4;
4806 rettv->v_type = VAR_SPECIAL;
4807 rettv->vval.v_number = VVAL_NULL;
4808 }
4809 else
4810 ret = NOTDONE;
4811 break;
4812
4813 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 * List: [expr, expr]
4815 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004816 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4817 return FAIL;
4818 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819 break;
4820
4821 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 * Dictionary: {'key': val, 'key': val}
4823 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004824 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4825 return FAIL;
4826 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827 break;
4828
4829 /*
4830 * Option value: &name
4831 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004832 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4833 return FAIL;
4834 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 break;
4836
4837 /*
4838 * Environment variable: $VAR.
4839 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004840 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4841 return FAIL;
4842 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 break;
4844
4845 /*
4846 * Register contents: @r.
4847 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004848 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4849 return FAIL;
4850 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 break;
4852 /*
4853 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004854 * lambda: (arg, arg) => expr
4855 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004857 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4858 ret = compile_lambda(arg, cctx);
4859 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004860 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 break;
4862
4863 default: ret = NOTDONE;
4864 break;
4865 }
4866 if (ret == FAIL)
4867 return FAIL;
4868
Bram Moolenaar1c747212020-05-09 18:28:34 +02004869 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004871 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004872 clear_tv(rettv);
4873 else
4874 // A constant expression can possibly be handled compile time,
4875 // return the value instead of generating code.
4876 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 }
4878 else if (ret == NOTDONE)
4879 {
4880 char_u *p;
4881 int r;
4882
4883 if (!eval_isnamec1(**arg))
4884 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004885 if (!vim9_bad_comment(*arg))
4886 {
4887 if (ends_excmd(*skipwhite(*arg)))
4888 semsg(_(e_empty_expression_str), *arg);
4889 else
4890 semsg(_(e_name_expected_str), *arg);
4891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 return FAIL;
4893 }
4894
4895 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004896 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004897 if (p - *arg == (size_t)1 && **arg == '_')
4898 {
4899 emsg(_(e_cannot_use_underscore_here));
4900 return FAIL;
4901 }
4902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004904 {
4905 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004908 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02004909 if (cctx->ctx_skip != SKIP_YES
4910 && generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004911 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004912 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 if (r == FAIL)
4915 return FAIL;
4916 }
4917
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004918 // Handle following "[]", ".member", etc.
4919 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004920 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004921 ppconst) == FAIL)
4922 return FAIL;
4923 if (ppconst->pp_used > 0)
4924 {
4925 // apply the '!', '-' and '+' before the constant
4926 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004927 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004928 return FAIL;
4929 return OK;
4930 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004931 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004933 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934}
4935
4936/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004937 * Give the "white on both sides" error, taking the operator from "p[len]".
4938 */
4939 void
4940error_white_both(char_u *op, int len)
4941{
4942 char_u buf[10];
4943
4944 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004945 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004946}
4947
4948/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004949 * <type>expr7: runtime type check / conversion
4950 */
4951 static int
4952compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4953{
4954 type_T *want_type = NULL;
4955
4956 // Recognize <type>
4957 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4958 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004959 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004960 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4961 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004962 return FAIL;
4963
4964 if (**arg != '>')
4965 {
4966 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004967 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004968 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004969 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004970 return FAIL;
4971 }
4972 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004973 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004974 return FAIL;
4975 }
4976
4977 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4978 return FAIL;
4979
4980 if (want_type != NULL)
4981 {
4982 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004983 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004984 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004985
Bram Moolenaard1103582020-08-14 22:44:25 +02004986 generate_ppconst(cctx, ppconst);
4987 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004988 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004989 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004990 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004991 return FAIL;
4992 }
4993 }
4994
4995 return OK;
4996}
4997
4998/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 * * number multiplication
5000 * / number division
5001 * % number modulo
5002 */
5003 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005004compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005{
5006 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005007 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005008 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005010 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005011 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 return FAIL;
5013
5014 /*
5015 * Repeat computing, until no "*", "/" or "%" is following.
5016 */
5017 for (;;)
5018 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005019 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 if (*op != '*' && *op != '/' && *op != '%')
5021 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005022 if (next != NULL)
5023 {
5024 *arg = next_line_from_context(cctx, TRUE);
5025 op = skipwhite(*arg);
5026 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005027
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005028 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005030 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005031 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01005033 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005034 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005036 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005037 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005039
5040 if (ppconst->pp_used == ppconst_used + 2
5041 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5042 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005043 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005044 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5045 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
5046 varnumber_T res = 0;
5047 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005049 // both are numbers: compute the result
5050 switch (*op)
5051 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005052 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005053 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005054 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005055 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005056 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005057 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005058 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005059 break;
5060 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005061 if (failed)
5062 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005063 tv1->vval.v_number = res;
5064 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005065 }
5066 else
5067 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005068 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005069 generate_two_op(cctx, op);
5070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071 }
5072
5073 return OK;
5074}
5075
5076/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01005077 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 * - number subtraction
5079 * .. string concatenation
5080 */
5081 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005082compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083{
5084 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005085 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005087 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088
5089 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005090 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 return FAIL;
5092
5093 /*
5094 * Repeat computing, until no "+", "-" or ".." is following.
5095 */
5096 for (;;)
5097 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005098 op = may_peek_next_line(cctx, *arg, &next);
5099 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005101 if (op[0] == op[1] && *op != '.' && next)
5102 // Finding "++" or "--" on the next line is a separate command.
5103 // But ".." is concatenation.
5104 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005106 if (next != NULL)
5107 {
5108 *arg = next_line_from_context(cctx, TRUE);
5109 op = skipwhite(*arg);
5110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005112 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005114 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005115 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 }
5117
Bram Moolenaare0de1712020-12-02 17:36:54 +01005118 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005119 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005121 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005122 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 return FAIL;
5124
Bram Moolenaara5565e42020-05-09 15:44:01 +02005125 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005126 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005127 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5128 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5129 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5130 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005132 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5133 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005134
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005135 // concat/subtract/add constant numbers
5136 if (*op == '+')
5137 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5138 else if (*op == '-')
5139 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5140 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005141 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005142 // concatenate constant strings
5143 char_u *s1 = tv1->vval.v_string;
5144 char_u *s2 = tv2->vval.v_string;
5145 size_t len1 = STRLEN(s1);
5146
5147 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5148 if (tv1->vval.v_string == NULL)
5149 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005150 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005151 return FAIL;
5152 }
5153 mch_memmove(tv1->vval.v_string, s1, len1);
5154 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005155 vim_free(s1);
5156 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005157 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005158 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005159 }
5160 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005161 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005162 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005163 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005164 if (*op == '.')
5165 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005166 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5167 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005168 return FAIL;
5169 generate_instr_drop(cctx, ISN_CONCAT, 1);
5170 }
5171 else
5172 generate_two_op(cctx, op);
5173 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005174 }
5175
5176 return OK;
5177}
5178
5179/*
5180 * expr5a == expr5b
5181 * expr5a =~ expr5b
5182 * expr5a != expr5b
5183 * expr5a !~ expr5b
5184 * expr5a > expr5b
5185 * expr5a >= expr5b
5186 * expr5a < expr5b
5187 * expr5a <= expr5b
5188 * expr5a is expr5b
5189 * expr5a isnot expr5b
5190 *
5191 * Produces instructions:
5192 * EVAL expr5a Push result of "expr5a"
5193 * EVAL expr5b Push result of "expr5b"
5194 * COMPARE one of the compare instructions
5195 */
5196 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005197compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005199 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005201 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005204 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205
5206 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005207 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 return FAIL;
5209
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005210 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005211 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212
5213 /*
5214 * If there is a comparative operator, use it.
5215 */
5216 if (type != EXPR_UNKNOWN)
5217 {
5218 int ic = FALSE; // Default: do not ignore case
5219
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005220 if (next != NULL)
5221 {
5222 *arg = next_line_from_context(cctx, TRUE);
5223 p = skipwhite(*arg);
5224 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 if (type_is && (p[len] == '?' || p[len] == '#'))
5226 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005227 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 return FAIL;
5229 }
5230 // extra question mark appended: ignore case
5231 if (p[len] == '?')
5232 {
5233 ic = TRUE;
5234 ++len;
5235 }
5236 // extra '#' appended: match case (ignored)
5237 else if (p[len] == '#')
5238 ++len;
5239 // nothing appended: match case
5240
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005241 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005242 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005243 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005244 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 }
5246
5247 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005248 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005249 return FAIL;
5250
Bram Moolenaara5565e42020-05-09 15:44:01 +02005251 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252 return FAIL;
5253
Bram Moolenaara5565e42020-05-09 15:44:01 +02005254 if (ppconst->pp_used == ppconst_used + 2)
5255 {
5256 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5257 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5258 int ret;
5259
5260 // Both sides are a constant, compute the result now.
5261 // First check for a valid combination of types, this is more
5262 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005263 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005264 ret = FAIL;
5265 else
5266 {
5267 ret = typval_compare(tv1, tv2, type, ic);
5268 tv1->v_type = VAR_BOOL;
5269 tv1->vval.v_number = tv1->vval.v_number
5270 ? VVAL_TRUE : VVAL_FALSE;
5271 clear_tv(tv2);
5272 --ppconst->pp_used;
5273 }
5274 return ret;
5275 }
5276
5277 generate_ppconst(cctx, ppconst);
5278 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279 }
5280
5281 return OK;
5282}
5283
Bram Moolenaar7f141552020-05-09 17:35:53 +02005284static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286/*
5287 * Compile || or &&.
5288 */
5289 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005290compile_and_or(
5291 char_u **arg,
5292 cctx_T *cctx,
5293 char *op,
5294 ppconst_T *ppconst,
5295 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005297 char_u *next;
5298 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005299 int opchar = *op;
5300
5301 if (p[0] == opchar && p[1] == opchar)
5302 {
5303 garray_T *instr = &cctx->ctx_instr;
5304 garray_T end_ga;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005305 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306
5307 /*
5308 * Repeat until there is no following "||" or "&&"
5309 */
5310 ga_init2(&end_ga, sizeof(int), 10);
5311 while (p[0] == opchar && p[1] == opchar)
5312 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005313 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005314 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005315 int start_ctx_lnum = cctx->ctx_lnum;
5316 int save_lnum;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005317 int const_used;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005318 int status;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005319 jumpwhen_T jump_when = opchar == '|'
5320 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005321
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005322 if (next != NULL)
5323 {
5324 *arg = next_line_from_context(cctx, TRUE);
5325 p = skipwhite(*arg);
5326 }
5327
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005328 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5329 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005330 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005331 op, p);
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005332 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005333 return FAIL;
5334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005336 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005337 SOURCING_LNUM = start_lnum;
5338 save_lnum = cctx->ctx_lnum;
5339 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005340
5341 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005342 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005343 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005344 // Use the last ppconst if possible.
5345 if (ppconst->pp_used > 0)
5346 {
5347 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5348 int is_true = tv2bool(tv);
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005349
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005350 if ((is_true && opchar == '|')
5351 || (!is_true && opchar == '&'))
5352 {
5353 // For "false && expr" and "true || expr" the "expr"
5354 // does not need to be evaluated.
5355 cctx->ctx_skip = SKIP_YES;
5356 clear_tv(tv);
5357 tv->v_type = VAR_BOOL;
5358 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
5359 }
5360 else
5361 {
5362 // For "true && expr" and "false || expr" only "expr"
5363 // needs to be evaluated.
5364 --ppconst->pp_used;
5365 jump_when = JUMP_NEVER;
5366 }
5367 }
5368 else
5369 {
5370 // Every part must evaluate to a bool.
5371 status = bool_on_stack(cctx);
5372 }
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005373 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005374 if (status != FAIL)
5375 status = ga_grow(&end_ga, 1);
Bram Moolenaara7511c02021-04-03 21:47:07 +02005376 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005377 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378 {
5379 ga_clear(&end_ga);
5380 return FAIL;
5381 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005382
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005383 if (jump_when != JUMP_NEVER)
5384 {
5385 if (cctx->ctx_skip != SKIP_YES)
5386 {
5387 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5388 ++end_ga.ga_len;
5389 }
5390 generate_JUMP(cctx, jump_when, 0);
5391 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005392
5393 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005394 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005395 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005396 {
5397 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005398 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005399 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005400
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005401 const_used = ppconst->pp_used;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005402 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5403 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005404 {
5405 ga_clear(&end_ga);
5406 return FAIL;
5407 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005408
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005409 // "0 || 1" results in true, "1 && 0" results in false.
5410 if (ppconst->pp_used == const_used + 1)
5411 {
5412 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5413
5414 if (tv->v_type == VAR_NUMBER
5415 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
5416 {
5417 tv->vval.v_number = tv->vval.v_number == 1
5418 ? VVAL_TRUE : VVAL_FALSE;
5419 tv->v_type = VAR_BOOL;
5420 }
5421 }
5422
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005423 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005425
5426 if (check_ppconst_bool(ppconst) == FAIL)
5427 {
5428 ga_clear(&end_ga);
5429 return FAIL;
5430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005432 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
5433 // Every part must evaluate to a bool.
5434 if (bool_on_stack(cctx) == FAIL)
5435 {
5436 ga_clear(&end_ga);
5437 return FAIL;
5438 }
5439
5440 if (end_ga.ga_len > 0)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005441 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005442 // Fill in the end label in all jumps.
5443 generate_ppconst(cctx, ppconst);
5444 while (end_ga.ga_len > 0)
5445 {
5446 isn_T *isn;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005447
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005448 --end_ga.ga_len;
5449 isn = ((isn_T *)instr->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005451 isn->isn_arg.jump.jump_where = instr->ga_len;
5452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005453 }
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005454 ga_clear(&end_ga);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005455
5456 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457 }
5458
5459 return OK;
5460}
5461
5462/*
5463 * expr4a && expr4a && expr4a logical AND
5464 *
5465 * Produces instructions:
5466 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005467 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005468 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005469 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005470 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005471 * EVAL expr4c Push result of "expr4c"
5472 * end:
5473 */
5474 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005475compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005477 int ppconst_used = ppconst->pp_used;
5478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005479 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005480 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005481 return FAIL;
5482
5483 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005484 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005485}
5486
5487/*
5488 * expr3a || expr3b || expr3c logical OR
5489 *
5490 * Produces instructions:
5491 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005492 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005493 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005495 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496 * EVAL expr3c Push result of "expr3c"
5497 * end:
5498 */
5499 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005500compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005501{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005502 int ppconst_used = ppconst->pp_used;
5503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005504 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005505 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005506 return FAIL;
5507
5508 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005509 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005510}
5511
5512/*
5513 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005514 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005515 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516 * JUMP_IF_FALSE alt jump if false
5517 * EVAL expr1a
5518 * JUMP_ALWAYS end
5519 * alt: EVAL expr1b
5520 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005521 *
5522 * Toplevel expression: expr2 ?? expr1
5523 * Produces instructions:
5524 * EVAL expr2 Push result of "expr2"
5525 * JUMP_AND_KEEP_IF_TRUE end jump if true
5526 * EVAL expr1
5527 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005528 */
5529 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005530compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531{
5532 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005533 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005534 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535
Bram Moolenaar3988f642020-08-27 22:43:03 +02005536 // Ignore all kinds of errors when not producing code.
5537 if (cctx->ctx_skip == SKIP_YES)
5538 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005539 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005540 return OK;
5541 }
5542
Bram Moolenaar61a89812020-05-07 16:58:17 +02005543 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005544 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005545 return FAIL;
5546
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005547 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 if (*p == '?')
5549 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005550 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005551 garray_T *instr = &cctx->ctx_instr;
5552 garray_T *stack = &cctx->ctx_type_stack;
5553 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005554 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005555 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005556 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005557 int has_const_expr = FALSE;
5558 int const_value = FALSE;
5559 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005560
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005561 if (next != NULL)
5562 {
5563 *arg = next_line_from_context(cctx, TRUE);
5564 p = skipwhite(*arg);
5565 }
5566
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005567 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005568 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005569 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005570 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005571 return FAIL;
5572 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005573
Bram Moolenaara5565e42020-05-09 15:44:01 +02005574 if (ppconst->pp_used == ppconst_used + 1)
5575 {
5576 // the condition is a constant, we know whether the ? or the :
5577 // expression is to be evaluated.
5578 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005579 if (op_falsy)
5580 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5581 else
5582 {
5583 int error = FALSE;
5584
5585 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5586 &error);
5587 if (error)
5588 return FAIL;
5589 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005590 cctx->ctx_skip = save_skip == SKIP_YES ||
5591 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5592
5593 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5594 // "left ?? right" and "left" is truthy: produce "left"
5595 generate_ppconst(cctx, ppconst);
5596 else
5597 {
5598 clear_tv(&ppconst->pp_tv[ppconst_used]);
5599 --ppconst->pp_used;
5600 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005601 }
5602 else
5603 {
5604 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005605 if (op_falsy)
5606 end_idx = instr->ga_len;
5607 generate_JUMP(cctx, op_falsy
5608 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5609 if (op_falsy)
5610 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005611 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612
5613 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005614 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005615 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005616 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005617 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618
Bram Moolenaara5565e42020-05-09 15:44:01 +02005619 if (!has_const_expr)
5620 {
5621 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005622
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005623 if (!op_falsy)
5624 {
5625 // remember the type and drop it
5626 --stack->ga_len;
5627 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005628
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005629 end_idx = instr->ga_len;
5630 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005631
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005632 // jump here from JUMP_IF_FALSE
5633 isn = ((isn_T *)instr->ga_data) + alt_idx;
5634 isn->isn_arg.jump.jump_where = instr->ga_len;
5635 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005637
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005638 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005639 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005640 // Check for the ":".
5641 p = may_peek_next_line(cctx, *arg, &next);
5642 if (*p != ':')
5643 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005644 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005645 return FAIL;
5646 }
5647 if (next != NULL)
5648 {
5649 *arg = next_line_from_context(cctx, TRUE);
5650 p = skipwhite(*arg);
5651 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005652
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005653 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5654 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005655 semsg(_(e_white_space_required_before_and_after_str_at_str),
5656 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005657 return FAIL;
5658 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005659
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005660 // evaluate the third expression
5661 if (has_const_expr)
5662 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005663 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005664 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005665 return FAIL;
5666 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5667 return FAIL;
5668 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005669
Bram Moolenaara5565e42020-05-09 15:44:01 +02005670 if (!has_const_expr)
5671 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005672 type_T **typep;
5673
Bram Moolenaara5565e42020-05-09 15:44:01 +02005674 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005675
Bram Moolenaara5565e42020-05-09 15:44:01 +02005676 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005677 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5678 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005679
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005680 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005681 isn = ((isn_T *)instr->ga_data) + end_idx;
5682 isn->isn_arg.jump.jump_where = instr->ga_len;
5683 }
5684
5685 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005686 }
5687 return OK;
5688}
5689
5690/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005691 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005692 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5693 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005694 */
5695 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005696compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005697{
5698 ppconst_T ppconst;
5699
5700 CLEAR_FIELD(ppconst);
5701 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5702 {
5703 clear_ppconst(&ppconst);
5704 return FAIL;
5705 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005706 if (is_const != NULL)
5707 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005708 if (generate_ppconst(cctx, &ppconst) == FAIL)
5709 return FAIL;
5710 return OK;
5711}
5712
5713/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005714 * Toplevel expression.
5715 */
5716 static int
5717compile_expr0(char_u **arg, cctx_T *cctx)
5718{
5719 return compile_expr0_ext(arg, cctx, NULL);
5720}
5721
5722/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005723 * Compile "return [expr]".
5724 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005725 */
5726 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005727compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005728{
5729 char_u *p = arg;
5730 garray_T *stack = &cctx->ctx_type_stack;
5731 type_T *stack_type;
5732
5733 if (*p != NUL && *p != '|' && *p != '\n')
5734 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005735 if (legacy)
5736 {
5737 int save_flags = cmdmod.cmod_flags;
5738
5739 generate_LEGACY_EVAL(cctx, p);
5740 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5741 0, cctx, FALSE, FALSE) == FAIL)
5742 return NULL;
5743 cmdmod.cmod_flags |= CMOD_LEGACY;
5744 (void)skip_expr(&p, NULL);
5745 cmdmod.cmod_flags = save_flags;
5746 }
5747 else
5748 {
5749 // compile return argument into instructions
5750 if (compile_expr0(&p, cctx) == FAIL)
5751 return NULL;
5752 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005753
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005754 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005755 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005756 // "check_return_type" with uf_ret_type set to &t_unknown is used
5757 // for an inline function without a specified return type. Set the
5758 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005759 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005760 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005761 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5762 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005763 || (!check_return_type
5764 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005765 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005766 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005767 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005768 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005769 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005770 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5771 && stack_type->tt_type != VAR_VOID
5772 && stack_type->tt_type != VAR_UNKNOWN)
5773 {
5774 emsg(_(e_returning_value_in_function_without_return_type));
5775 return NULL;
5776 }
5777 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005778 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005779 return NULL;
5780 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005782 }
5783 else
5784 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005785 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005786 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005787 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5788 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005789 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005790 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005791 return NULL;
5792 }
5793
5794 // No argument, return zero.
5795 generate_PUSHNR(cctx, 0);
5796 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005797
5798 // Undo any command modifiers.
5799 generate_undo_cmdmods(cctx);
5800
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005801 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005802 return NULL;
5803
5804 // "return val | endif" is possible
5805 return skipwhite(p);
5806}
5807
5808/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005809 * Get a line from the compilation context, compatible with exarg_T getline().
5810 * Return a pointer to the line in allocated memory.
5811 * Return NULL for end-of-file or some error.
5812 */
5813 static char_u *
5814exarg_getline(
5815 int c UNUSED,
5816 void *cookie,
5817 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005818 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005819{
5820 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005821 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005822
Bram Moolenaar66250c92020-08-20 15:02:42 +02005823 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005824 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005825 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005826 return NULL;
5827 ++cctx->ctx_lnum;
5828 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5829 // Comment lines result in NULL pointers, skip them.
5830 if (p != NULL)
5831 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005832 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005833}
5834
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005835 void
5836fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5837{
5838 eap->getline = exarg_getline;
5839 eap->cookie = cctx;
5840}
5841
Bram Moolenaar04b12692020-05-04 23:24:44 +02005842/*
5843 * Compile a nested :def command.
5844 */
5845 static char_u *
5846compile_nested_function(exarg_T *eap, cctx_T *cctx)
5847{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005848 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005849 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005850 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005851 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005852 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005853 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005854 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005855
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005856 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005857 {
5858 emsg(_(e_cannot_use_bang_with_nested_def));
5859 return NULL;
5860 }
5861
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005862 if (*name_start == '/')
5863 {
5864 name_end = skip_regexp(name_start + 1, '/', TRUE);
5865 if (*name_end == '/')
5866 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005867 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005868 }
5869 if (name_end == name_start || *skipwhite(name_end) != '(')
5870 {
5871 if (!ends_excmd2(name_start, name_end))
5872 {
5873 semsg(_(e_invalid_command_str), eap->cmd);
5874 return NULL;
5875 }
5876
5877 // "def" or "def Name": list functions
5878 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5879 return NULL;
5880 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5881 }
5882
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005883 // Only g:Func() can use a namespace.
5884 if (name_start[1] == ':' && !is_global)
5885 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005886 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005887 return NULL;
5888 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005889 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005890 return NULL;
5891
Bram Moolenaar04b12692020-05-04 23:24:44 +02005892 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005893 fill_exarg_from_cctx(eap, cctx);
5894
Bram Moolenaar04b12692020-05-04 23:24:44 +02005895 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00005896 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005897 lambda_name = vim_strsave(get_lambda_name());
5898 if (lambda_name == NULL)
5899 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005900 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005901
Bram Moolenaar822ba242020-05-24 23:00:18 +02005902 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005903 {
5904 r = eap->skip ? OK : FAIL;
5905 goto theend;
5906 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005907
5908 // copy over the block scope IDs before compiling
5909 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5910 {
5911 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5912
5913 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5914 if (ufunc->uf_block_ids != NULL)
5915 {
5916 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5917 sizeof(int) * block_depth);
5918 ufunc->uf_block_depth = block_depth;
5919 }
5920 }
5921
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005922 compile_type = COMPILE_TYPE(ufunc);
5923#ifdef FEAT_PROFILE
5924 // If the outer function is profiled, also compile the nested function for
5925 // profiling.
5926 if (cctx->ctx_compile_type == CT_PROFILE)
5927 compile_type = CT_PROFILE;
5928#endif
5929 if (func_needs_compiling(ufunc, compile_type)
5930 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005931 {
5932 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005933 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005934 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005935
Bram Moolenaar648594e2021-07-11 17:55:01 +02005936#ifdef FEAT_PROFILE
5937 // When the outer function is compiled for profiling, the nested function
5938 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005939 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005940 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5941#endif
5942
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005943 if (is_global)
5944 {
5945 char_u *func_name = vim_strnsave(name_start + 2,
5946 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005947
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005948 if (func_name == NULL)
5949 r = FAIL;
5950 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005951 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005952 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005953 lambda_name = NULL;
5954 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005955 }
5956 else
5957 {
5958 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005959 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005960 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005961
Bram Moolenaareef21022020-08-01 22:16:43 +02005962 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005963 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005964 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005965 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005966 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5967 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005968
5969theend:
5970 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005971 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005972}
5973
5974/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975 * Return the length of an assignment operator, or zero if there isn't one.
5976 */
5977 int
5978assignment_len(char_u *p, int *heredoc)
5979{
5980 if (*p == '=')
5981 {
5982 if (p[1] == '<' && p[2] == '<')
5983 {
5984 *heredoc = TRUE;
5985 return 3;
5986 }
5987 return 1;
5988 }
5989 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5990 return 2;
5991 if (STRNCMP(p, "..=", 3) == 0)
5992 return 3;
5993 return 0;
5994}
5995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005996/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005997 * Generate the load instruction for "name".
5998 */
5999 static void
6000generate_loadvar(
6001 cctx_T *cctx,
6002 assign_dest_T dest,
6003 char_u *name,
6004 lvar_T *lvar,
6005 type_T *type)
6006{
6007 switch (dest)
6008 {
6009 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006010 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006011 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
6012 break;
6013 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01006014 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
6015 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
6016 else
6017 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006018 break;
6019 case dest_buffer:
6020 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
6021 break;
6022 case dest_window:
6023 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
6024 break;
6025 case dest_tab:
6026 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
6027 break;
6028 case dest_script:
6029 compile_load_scriptvar(cctx,
6030 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
6031 break;
6032 case dest_env:
6033 // Include $ in the name here
6034 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
6035 break;
6036 case dest_reg:
6037 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
6038 break;
6039 case dest_vimvar:
6040 generate_LOADV(cctx, name + 2, TRUE);
6041 break;
6042 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01006043 if (lvar->lv_from_outer > 0)
6044 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
6045 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006046 else
6047 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
6048 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006049 case dest_expr:
6050 // list or dict value should already be on the stack.
6051 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006052 }
6053}
6054
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006055/*
6056 * Skip over "[expr]" or ".member".
6057 * Does not check for any errors.
6058 */
6059 static char_u *
6060skip_index(char_u *start)
6061{
6062 char_u *p = start;
6063
6064 if (*p == '[')
6065 {
6066 p = skipwhite(p + 1);
6067 (void)skip_expr(&p, NULL);
6068 p = skipwhite(p);
6069 if (*p == ']')
6070 return p + 1;
6071 return p;
6072 }
6073 // if (*p == '.')
6074 return to_name_end(p + 1, TRUE);
6075}
6076
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006077 void
6078vim9_declare_error(char_u *name)
6079{
6080 char *scope = "";
6081
6082 switch (*name)
6083 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006084 case 'g': scope = _("global"); break;
6085 case 'b': scope = _("buffer"); break;
6086 case 'w': scope = _("window"); break;
6087 case 't': scope = _("tab"); break;
6088 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006089 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006090 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006091 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006092 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006093 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006094 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006095 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006096 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006097 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006098}
6099
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006100/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006101 * For one assignment figure out the type of destination. Return it in "dest".
6102 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006103 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006104 * For a v:var "vimvaridx" is set.
6105 * "type" is set to the destination type if known, unchanted otherwise.
6106 * Return FAIL if an error message was given.
6107 */
6108 static int
6109get_var_dest(
6110 char_u *name,
6111 assign_dest_T *dest,
6112 int cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006113 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006114 int *vimvaridx,
6115 type_T **type,
6116 cctx_T *cctx)
6117{
6118 char_u *p;
6119
6120 if (*name == '&')
6121 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006122 int cc;
6123 long numval;
6124 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006125 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006126
6127 *dest = dest_option;
6128 if (cmdidx == CMD_final || cmdidx == CMD_const)
6129 {
6130 emsg(_(e_const_option));
6131 return FAIL;
6132 }
6133 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006134 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006135 if (p == NULL)
6136 {
6137 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02006138 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006139 return FAIL;
6140 }
6141 cc = *p;
6142 *p = NUL;
6143 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006144 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006145 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006146 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006147 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006148 case gov_unknown:
Bram Moolenaare1242042021-12-16 20:56:57 +00006149 semsg(_(e_unknown_option_str), name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006150 return FAIL;
6151 case gov_string:
6152 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006153 if (opt_p_flags & P_FUNC)
6154 {
6155 // might be a Funcref, check the type later
6156 *type = &t_any;
6157 *dest = dest_func_option;
6158 }
6159 else
6160 {
6161 *type = &t_string;
6162 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006163 break;
6164 case gov_bool:
6165 case gov_hidden_bool:
6166 *type = &t_bool;
6167 break;
6168 case gov_number:
6169 case gov_hidden_number:
6170 *type = &t_number;
6171 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006172 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006173 }
6174 else if (*name == '$')
6175 {
6176 *dest = dest_env;
6177 *type = &t_string;
6178 }
6179 else if (*name == '@')
6180 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006181 if (name[1] != '@'
6182 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006183 {
6184 emsg_invreg(name[1]);
6185 return FAIL;
6186 }
6187 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006188 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006189 }
6190 else if (STRNCMP(name, "g:", 2) == 0)
6191 {
6192 *dest = dest_global;
6193 }
6194 else if (STRNCMP(name, "b:", 2) == 0)
6195 {
6196 *dest = dest_buffer;
6197 }
6198 else if (STRNCMP(name, "w:", 2) == 0)
6199 {
6200 *dest = dest_window;
6201 }
6202 else if (STRNCMP(name, "t:", 2) == 0)
6203 {
6204 *dest = dest_tab;
6205 }
6206 else if (STRNCMP(name, "v:", 2) == 0)
6207 {
6208 typval_T *vtv;
6209 int di_flags;
6210
6211 *vimvaridx = find_vim_var(name + 2, &di_flags);
6212 if (*vimvaridx < 0)
6213 {
6214 semsg(_(e_variable_not_found_str), name);
6215 return FAIL;
6216 }
6217 // We use the current value of "sandbox" here, is that OK?
6218 if (var_check_ro(di_flags, name, FALSE))
6219 return FAIL;
6220 *dest = dest_vimvar;
6221 vtv = get_vim_var_tv(*vimvaridx);
6222 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6223 }
6224 return OK;
6225}
6226
6227/*
6228 * Generate a STORE instruction for "dest", not being "dest_local".
6229 * Return FAIL when out of memory.
6230 */
6231 static int
6232generate_store_var(
6233 cctx_T *cctx,
6234 assign_dest_T dest,
6235 int opt_flags,
6236 int vimvaridx,
6237 int scriptvar_idx,
6238 int scriptvar_sid,
6239 type_T *type,
6240 char_u *name)
6241{
6242 switch (dest)
6243 {
6244 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006245 return generate_STOREOPT(cctx, ISN_STOREOPT,
6246 skip_option_env_lead(name), opt_flags);
6247 case dest_func_option:
6248 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
6249 skip_option_env_lead(name), opt_flags);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006250 case dest_global:
6251 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006252 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6253 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006254 case dest_buffer:
6255 // include b: with the name, easier to execute that way
6256 return generate_STORE(cctx, ISN_STOREB, 0, name);
6257 case dest_window:
6258 // include w: with the name, easier to execute that way
6259 return generate_STORE(cctx, ISN_STOREW, 0, name);
6260 case dest_tab:
6261 // include t: with the name, easier to execute that way
6262 return generate_STORE(cctx, ISN_STORET, 0, name);
6263 case dest_env:
6264 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6265 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006266 return generate_STORE(cctx, ISN_STOREREG,
6267 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006268 case dest_vimvar:
6269 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6270 case dest_script:
6271 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006272 // "s:" may be included in the name.
6273 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006274 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006275 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6276 scriptvar_sid, scriptvar_idx, type);
6277 case dest_local:
6278 case dest_expr:
6279 // cannot happen
6280 break;
6281 }
6282 return FAIL;
6283}
6284
Bram Moolenaar752fc692021-01-04 21:57:11 +01006285 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006286generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6287{
6288 if (lhs->lhs_dest != dest_local)
6289 return generate_store_var(cctx, lhs->lhs_dest,
6290 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6291 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6292 lhs->lhs_type, lhs->lhs_name);
6293
6294 if (lhs->lhs_lvar != NULL)
6295 {
6296 garray_T *instr = &cctx->ctx_instr;
6297 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6298
6299 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6300 // ISN_STORENR
6301 if (lhs->lhs_lvar->lv_from_outer == 0
6302 && instr->ga_len == instr_count + 1
6303 && isn->isn_type == ISN_PUSHNR)
6304 {
6305 varnumber_T val = isn->isn_arg.number;
6306 garray_T *stack = &cctx->ctx_type_stack;
6307
6308 isn->isn_type = ISN_STORENR;
6309 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6310 isn->isn_arg.storenr.stnr_val = val;
6311 if (stack->ga_len > 0)
6312 --stack->ga_len;
6313 }
6314 else if (lhs->lhs_lvar->lv_from_outer > 0)
6315 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6316 lhs->lhs_lvar->lv_from_outer);
6317 else
6318 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6319 }
6320 return OK;
6321}
6322
6323 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006324is_decl_command(int cmdidx)
6325{
6326 return cmdidx == CMD_let || cmdidx == CMD_var
6327 || cmdidx == CMD_final || cmdidx == CMD_const;
6328}
6329
6330/*
6331 * Figure out the LHS type and other properties for an assignment or one item
6332 * of ":unlet" with an index.
6333 * Returns OK or FAIL.
6334 */
6335 static int
6336compile_lhs(
6337 char_u *var_start,
6338 lhs_T *lhs,
6339 int cmdidx,
6340 int heredoc,
6341 int oplen,
6342 cctx_T *cctx)
6343{
6344 char_u *var_end;
6345 int is_decl = is_decl_command(cmdidx);
6346
6347 CLEAR_POINTER(lhs);
6348 lhs->lhs_dest = dest_local;
6349 lhs->lhs_vimvaridx = -1;
6350 lhs->lhs_scriptvar_idx = -1;
6351
6352 // "dest_end" is the end of the destination, including "[expr]" or
6353 // ".name".
6354 // "var_end" is the end of the variable/option/etc. name.
6355 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6356 if (*var_start == '@')
6357 var_end = var_start + 2;
6358 else
6359 {
6360 // skip over the leading "&", "&l:", "&g:" and "$"
6361 var_end = skip_option_env_lead(var_start);
6362 var_end = to_name_end(var_end, TRUE);
6363 }
6364
6365 // "a: type" is declaring variable "a" with a type, not dict "a:".
6366 if (is_decl && lhs->lhs_dest_end == var_start + 2
6367 && lhs->lhs_dest_end[-1] == ':')
6368 --lhs->lhs_dest_end;
6369 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6370 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006371 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006372
6373 // compute the length of the destination without "[expr]" or ".name"
6374 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006375 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006376 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6377 if (lhs->lhs_name == NULL)
6378 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006379
6380 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6381 // Something follows after the variable: "var[idx]" or "var.key".
6382 lhs->lhs_has_index = TRUE;
6383
Bram Moolenaar752fc692021-01-04 21:57:11 +01006384 if (heredoc)
6385 lhs->lhs_type = &t_list_string;
6386 else
6387 lhs->lhs_type = &t_any;
6388
6389 if (cctx->ctx_skip != SKIP_YES)
6390 {
6391 int declare_error = FALSE;
6392
6393 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6394 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6395 &lhs->lhs_type, cctx) == FAIL)
6396 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006397 if (lhs->lhs_dest != dest_local
6398 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006399 {
6400 // Specific kind of variable recognized.
6401 declare_error = is_decl;
6402 }
6403 else
6404 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006405 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006406 if (check_reserved_name(lhs->lhs_name) == FAIL)
6407 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006408
6409 if (lookup_local(var_start, lhs->lhs_varlen,
6410 &lhs->lhs_local_lvar, cctx) == OK)
6411 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6412 else
6413 {
6414 CLEAR_FIELD(lhs->lhs_arg_lvar);
6415 if (arg_exists(var_start, lhs->lhs_varlen,
6416 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6417 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6418 {
6419 if (is_decl)
6420 {
6421 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6422 return FAIL;
6423 }
6424 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6425 }
6426 }
6427 if (lhs->lhs_lvar != NULL)
6428 {
6429 if (is_decl)
6430 {
6431 semsg(_(e_variable_already_declared), lhs->lhs_name);
6432 return FAIL;
6433 }
6434 }
6435 else
6436 {
6437 int script_namespace = lhs->lhs_varlen > 1
6438 && STRNCMP(var_start, "s:", 2) == 0;
6439 int script_var = (script_namespace
6440 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006441 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006442 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006443 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006444 imported_T *import =
6445 find_imported(var_start, lhs->lhs_varlen, cctx);
6446
6447 if (script_namespace || script_var || import != NULL)
6448 {
6449 char_u *rawname = lhs->lhs_name
6450 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6451
6452 if (is_decl)
6453 {
6454 if (script_namespace)
6455 semsg(_(e_cannot_declare_script_variable_in_function),
6456 lhs->lhs_name);
6457 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006458 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006459 lhs->lhs_name);
6460 return FAIL;
6461 }
6462 else if (cctx->ctx_ufunc->uf_script_ctx_version
6463 == SCRIPT_VERSION_VIM9
6464 && script_namespace
6465 && !script_var && import == NULL)
6466 {
6467 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6468 return FAIL;
6469 }
6470
6471 lhs->lhs_dest = dest_script;
6472
6473 // existing script-local variables should have a type
6474 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6475 if (import != NULL)
6476 lhs->lhs_scriptvar_sid = import->imp_sid;
6477 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6478 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006479 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006480 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006481 lhs->lhs_scriptvar_sid, rawname,
6482 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6483 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006484 if (lhs->lhs_scriptvar_idx >= 0)
6485 {
6486 scriptitem_T *si = SCRIPT_ITEM(
6487 lhs->lhs_scriptvar_sid);
6488 svar_T *sv =
6489 ((svar_T *)si->sn_var_vals.ga_data)
6490 + lhs->lhs_scriptvar_idx;
6491 lhs->lhs_type = sv->sv_type;
6492 }
6493 }
6494 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006495 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006496 == FAIL)
6497 return FAIL;
6498 }
6499 }
6500
6501 if (declare_error)
6502 {
6503 vim9_declare_error(lhs->lhs_name);
6504 return FAIL;
6505 }
6506 }
6507
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006508 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01006509 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6510 var_end = lhs->lhs_dest_end;
6511
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006512 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006513 {
6514 if (is_decl && *var_end == ':')
6515 {
6516 char_u *p;
6517
6518 // parse optional type: "let var: type = expr"
6519 if (!VIM_ISWHITE(var_end[1]))
6520 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006521 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006522 return FAIL;
6523 }
6524 p = skipwhite(var_end + 1);
6525 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6526 if (lhs->lhs_type == NULL)
6527 return FAIL;
6528 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006529 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006530 }
6531 else if (lhs->lhs_lvar != NULL)
6532 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6533 }
6534
Bram Moolenaare42939a2021-04-05 17:11:17 +02006535 if (oplen == 3 && !heredoc
6536 && lhs->lhs_dest != dest_global
6537 && !lhs->lhs_has_index
6538 && lhs->lhs_type->tt_type != VAR_STRING
6539 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006540 {
6541 emsg(_(e_can_only_concatenate_to_string));
6542 return FAIL;
6543 }
6544
6545 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6546 && cctx->ctx_skip != SKIP_YES)
6547 {
6548 if (oplen > 1 && !heredoc)
6549 {
6550 // +=, /=, etc. require an existing variable
6551 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6552 return FAIL;
6553 }
6554 if (!is_decl)
6555 {
6556 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6557 return FAIL;
6558 }
6559
Bram Moolenaar3f327882021-03-17 20:56:38 +01006560 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006561 if ((lhs->lhs_type->tt_type == VAR_FUNC
6562 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006563 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006564 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006565
6566 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006567 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6568 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6569 if (lhs->lhs_lvar == NULL)
6570 return FAIL;
6571 lhs->lhs_new_local = TRUE;
6572 }
6573
6574 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006575 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006576 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006577 char_u *after = var_start + lhs->lhs_varlen;
6578 char_u *p;
6579
Bram Moolenaar752fc692021-01-04 21:57:11 +01006580 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006581 if (is_decl)
6582 {
6583 emsg(_(e_cannot_use_index_when_declaring_variable));
6584 return FAIL;
6585 }
6586
Bram Moolenaarc750d912021-11-29 22:02:12 +00006587 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
6588 // Only the last index is used below, if there are others
6589 // before it generate code for the expression. Thus for
6590 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6591 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006592 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006593 p = skip_index(after);
6594 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01006595 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006596 lhs->lhs_varlen_total = p - var_start;
6597 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006598 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006599 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006600 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006601 if (after > var_start + lhs->lhs_varlen)
6602 {
6603 lhs->lhs_varlen = after - var_start;
6604 lhs->lhs_dest = dest_expr;
6605 // We don't know the type before evaluating the expression,
6606 // use "any" until then.
6607 lhs->lhs_type = &t_any;
6608 }
6609
6610 if (lhs->lhs_type->tt_member == NULL)
6611 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006612 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00006613 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006614 }
6615 return OK;
6616}
6617
6618/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006619 * Figure out the LHS and check a few errors.
6620 */
6621 static int
6622compile_assign_lhs(
6623 char_u *var_start,
6624 lhs_T *lhs,
6625 int cmdidx,
6626 int is_decl,
6627 int heredoc,
6628 int oplen,
6629 cctx_T *cctx)
6630{
6631 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6632 return FAIL;
6633
6634 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6635 {
6636 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6637 return FAIL;
6638 }
6639 if (!is_decl && lhs->lhs_lvar != NULL
6640 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6641 {
6642 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6643 return FAIL;
6644 }
6645 return OK;
6646}
6647
6648/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006649 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6650 */
6651 static int
6652has_list_index(char_u *idx_start, cctx_T *cctx)
6653{
6654 char_u *p = idx_start;
6655 int save_skip;
6656
6657 if (*p != '[')
6658 return FALSE;
6659
6660 p = skipwhite(p + 1);
6661 if (*p == ':')
6662 return TRUE;
6663
6664 save_skip = cctx->ctx_skip;
6665 cctx->ctx_skip = SKIP_YES;
6666 (void)compile_expr0(&p, cctx);
6667 cctx->ctx_skip = save_skip;
6668 return *skipwhite(p) == ':';
6669}
6670
6671/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006672 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6673 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006674 */
6675 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006676compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006677 char_u *var_start,
6678 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006679 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006680 cctx_T *cctx)
6681{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006682 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006683 char_u *p;
6684 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006685 int need_white_before = TRUE;
6686 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006687
Bram Moolenaar752fc692021-01-04 21:57:11 +01006688 p = var_start + varlen;
6689 if (*p == '[')
6690 {
6691 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006692 if (*p == ':')
6693 {
6694 // empty first index, push zero
6695 r = generate_PUSHNR(cctx, 0);
6696 need_white_before = FALSE;
6697 }
6698 else
6699 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006700
6701 if (r == OK && *skipwhite(p) == ':')
6702 {
6703 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006704 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006705 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006706 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006707 empty_second = *skipwhite(p + 1) == ']';
6708 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6709 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006710 {
6711 semsg(_(e_white_space_required_before_and_after_str_at_str),
6712 ":", p);
6713 return FAIL;
6714 }
6715 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006716 if (*p == ']')
6717 // empty second index, push "none"
6718 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6719 else
6720 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006721 }
6722
Bram Moolenaar752fc692021-01-04 21:57:11 +01006723 if (r == OK && *skipwhite(p) != ']')
6724 {
6725 // this should not happen
Bram Moolenaare1242042021-12-16 20:56:57 +00006726 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar752fc692021-01-04 21:57:11 +01006727 r = FAIL;
6728 }
6729 }
6730 else // if (*p == '.')
6731 {
6732 char_u *key_end = to_name_end(p + 1, TRUE);
6733 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6734
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006735 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006736 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006737 return r;
6738}
6739
6740/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006741 * For a LHS with an index, load the variable to be indexed.
6742 */
6743 static int
6744compile_load_lhs(
6745 lhs_T *lhs,
6746 char_u *var_start,
6747 type_T *rhs_type,
6748 cctx_T *cctx)
6749{
6750 if (lhs->lhs_dest == dest_expr)
6751 {
6752 size_t varlen = lhs->lhs_varlen;
6753 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006754 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006755 char_u *p = var_start;
6756 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006757 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006758
Bram Moolenaare97976b2021-08-01 13:17:17 +02006759 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6760 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006761 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006762 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6763 res = compile_expr0(&p, cctx);
6764 var_start[varlen] = c;
6765 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6766 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006767 {
6768 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006769 if (res != FAIL)
Bram Moolenaare1242042021-12-16 20:56:57 +00006770 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006771 return FAIL;
6772 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006773
6774 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6775 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6776 // now we can properly check the type
6777 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6778 && rhs_type != &t_void
6779 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6780 FALSE, FALSE) == FAIL)
6781 return FAIL;
6782 }
6783 else
6784 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6785 lhs->lhs_lvar, lhs->lhs_type);
6786 return OK;
6787}
6788
6789/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006790 * Produce code for loading "lhs" and also take care of an index.
6791 * Return OK/FAIL.
6792 */
6793 static int
6794compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6795{
6796 compile_load_lhs(lhs, var_start, NULL, cctx);
6797
6798 if (lhs->lhs_has_index)
6799 {
6800 int range = FALSE;
6801
6802 // Get member from list or dict. First compile the
6803 // index value.
6804 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6805 return FAIL;
6806 if (range)
6807 {
6808 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6809 var_start);
6810 return FAIL;
6811 }
6812
6813 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006814 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006815 return FAIL;
6816 }
6817 return OK;
6818}
6819
6820/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006821 * Assignment to a list or dict member, or ":unlet" for the item, using the
6822 * information in "lhs".
6823 * Returns OK or FAIL.
6824 */
6825 static int
6826compile_assign_unlet(
6827 char_u *var_start,
6828 lhs_T *lhs,
6829 int is_assign,
6830 type_T *rhs_type,
6831 cctx_T *cctx)
6832{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006833 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006834 garray_T *stack = &cctx->ctx_type_stack;
6835 int range = FALSE;
6836
Bram Moolenaar68452172021-04-12 21:21:02 +02006837 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006838 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006839 if (is_assign && range
6840 && lhs->lhs_type->tt_type != VAR_LIST
6841 && lhs->lhs_type != &t_blob
6842 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006843 {
6844 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6845 return FAIL;
6846 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006847
6848 if (lhs->lhs_type == &t_any)
6849 {
6850 // Index on variable of unknown type: check at runtime.
6851 dest_type = VAR_ANY;
6852 }
6853 else
6854 {
6855 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006856 if (dest_type == VAR_DICT && range)
6857 {
6858 emsg(e_cannot_use_range_with_dictionary);
6859 return FAIL;
6860 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006861 if (dest_type == VAR_DICT
6862 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006863 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006864 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006865 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006866 type_T *type;
6867
6868 if (range)
6869 {
6870 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6871 if (need_type(type, &t_number,
6872 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006873 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006874 }
6875 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006876 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006877 && need_type(type, &t_number,
6878 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006879 return FAIL;
6880 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006881 }
6882
6883 // Load the dict or list. On the stack we then have:
6884 // - value (for assignment, not for :unlet)
6885 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006886 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006887 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006888 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6889 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006890
Bram Moolenaar68452172021-04-12 21:21:02 +02006891 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6892 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006893 {
6894 if (is_assign)
6895 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006896 if (range)
6897 {
6898 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6899 return FAIL;
6900 }
6901 else
6902 {
6903 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006904
Bram Moolenaar68452172021-04-12 21:21:02 +02006905 if (isn == NULL)
6906 return FAIL;
6907 isn->isn_arg.vartype = dest_type;
6908 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006909 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006910 else if (range)
6911 {
6912 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6913 return FAIL;
6914 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006915 else
6916 {
6917 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6918 return FAIL;
6919 }
6920 }
6921 else
6922 {
6923 emsg(_(e_indexable_type_required));
6924 return FAIL;
6925 }
6926
6927 return OK;
6928}
6929
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006930/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006931 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006932 * "let name"
6933 * "var name = expr"
6934 * "final name = expr"
6935 * "const name = expr"
6936 * "name = expr"
6937 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006938 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006939 * Return NULL for an error.
6940 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 */
6942 static char_u *
6943compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6944{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006945 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006946 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006947 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 char_u *ret = NULL;
6949 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006950 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006951 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006952 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006954 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956 int oplen = 0;
6957 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006958 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006959 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006960 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006961 int is_decl = is_decl_command(cmdidx);
6962 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006963 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006964
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006965 // Skip over the "var" or "[var, var]" to get to any "=".
6966 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6967 if (p == NULL)
6968 return *arg == '[' ? arg : NULL;
6969
Bram Moolenaar752fc692021-01-04 21:57:11 +01006970 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006972 sp = p;
6973 p = skipwhite(p);
6974 op = p;
6975 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006976
6977 if (var_count > 0 && oplen == 0)
6978 // can be something like "[1, 2]->func()"
6979 return arg;
6980
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006981 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006983 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006984 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006985 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006986 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6987 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006988 if (VIM_ISWHITE(eap->cmd[2]))
6989 {
6990 semsg(_(e_no_white_space_allowed_after_str_str),
6991 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6992 return NULL;
6993 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006994 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6995 oplen = 2;
6996 incdec = TRUE;
6997 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 if (heredoc)
7000 {
7001 list_T *l;
7002 listitem_T *li;
7003
7004 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02007005 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007006 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02007007 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02007008 if (l == NULL)
7009 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010
Bram Moolenaar078269b2020-09-21 20:35:55 +02007011 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007012 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02007013 // Push each line and the create the list.
7014 FOR_ALL_LIST_ITEMS(l, li)
7015 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02007016 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02007017 li->li_tv.vval.v_string = NULL;
7018 }
7019 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021 list_free(l);
7022 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007023 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007025 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007027 char_u *wp;
7028
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007029 // for "[var, var] = expr" evaluate the expression here, loop over the
7030 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007031 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02007032
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007033 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01007034 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007035 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007036 if (compile_expr0(&p, cctx) == FAIL)
7037 return NULL;
7038 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007039
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007040 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007042 type_T *stacktype;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007043 int needed_list_len;
7044 int did_check = FALSE;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007045
Bram Moolenaarec5929d2020-04-07 20:53:39 +02007046 stacktype = stack->ga_len == 0 ? &t_void
7047 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007048 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007050 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007051 goto theend;
7052 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01007053 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007054 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007055 goto theend;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007056 // If a constant list was used we can check the length right here.
7057 needed_list_len = semicolon ? var_count - 1 : var_count;
7058 if (instr->ga_len > 0)
7059 {
7060 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
7061
7062 if (isn->isn_type == ISN_NEWLIST)
7063 {
7064 did_check = TRUE;
7065 if (semicolon ? isn->isn_arg.number < needed_list_len
7066 : isn->isn_arg.number != needed_list_len)
7067 {
7068 semsg(_(e_expected_nr_items_but_got_nr),
7069 needed_list_len, isn->isn_arg.number);
7070 goto theend;
7071 }
7072 }
7073 }
7074 if (!did_check)
7075 generate_CHECKLEN(cctx, needed_list_len, semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007076 if (stacktype->tt_member != NULL)
7077 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007078 }
7079 }
7080
7081 /*
7082 * Loop over variables in "[var, var] = expr".
7083 * For "var = expr" and "let var: type" this is done only once.
7084 */
7085 if (var_count > 0)
7086 var_start = skipwhite(arg + 1); // skip over the "["
7087 else
7088 var_start = arg;
7089 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
7090 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007091 int instr_count = -1;
7092 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007093
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02007094 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
7095 {
7096 // Ignore underscore in "[a, _, b] = list".
7097 if (var_count > 0)
7098 {
7099 var_start = skipwhite(var_start + 2);
7100 continue;
7101 }
7102 emsg(_(e_cannot_use_underscore_here));
7103 goto theend;
7104 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007105 vim_free(lhs.lhs_name);
7106
7107 /*
7108 * Figure out the LHS type and other properties.
7109 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007110 if (compile_assign_lhs(var_start, &lhs, cmdidx,
7111 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007112 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02007113 if (heredoc)
7114 {
7115 SOURCING_LNUM = start_lnum;
7116 if (lhs.lhs_has_type
7117 && need_type(&t_list_string, lhs.lhs_type,
7118 -1, 0, cctx, FALSE, FALSE) == FAIL)
7119 goto theend;
7120 }
7121 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007122 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007123 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007124 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007125 if (oplen > 0 && var_count == 0)
7126 {
7127 // skip over the "=" and the expression
7128 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02007129 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007130 }
7131 }
7132 else if (oplen > 0)
7133 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007134 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01007135 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007136
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007137 // for "+=", "*=", "..=" etc. first load the current value
7138 if (*op != '='
7139 && compile_load_lhs_with_index(&lhs, var_start,
7140 cctx) == FAIL)
7141 goto theend;
7142
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007143 // For "var = expr" evaluate the expression.
7144 if (var_count == 0)
7145 {
7146 int r;
7147
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007148 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007149 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007150 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007151 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007152 r = generate_PUSHNR(cctx, 1);
7153 }
7154 else
7155 {
7156 // Temporarily hide the new local variable here, it is
7157 // not available to this expression.
7158 if (lhs.lhs_new_local)
7159 --cctx->ctx_locals.ga_len;
7160 wp = op + oplen;
7161 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7162 {
7163 if (lhs.lhs_new_local)
7164 ++cctx->ctx_locals.ga_len;
7165 goto theend;
7166 }
7167 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007168 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007169 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007170 if (r == FAIL)
7171 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007172 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007173 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007174 else if (semicolon && var_idx == var_count - 1)
7175 {
7176 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007177 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007178 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7179 goto theend;
7180 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007181 else
7182 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007183 // For "[var, var] = expr" get the "var_idx" item from the
7184 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007185 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007186 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007187 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007188
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007189 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007190 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007191 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007192 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007193 if ((rhs_type->tt_type == VAR_FUNC
7194 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007195 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007196 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007197 goto theend;
7198
Bram Moolenaar752fc692021-01-04 21:57:11 +01007199 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007200 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007201 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007202 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007203 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007204 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007205 }
7206 else
7207 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007208 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007209 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007210 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007211 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007212 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007213 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007214 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007215 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007216 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007217 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007218 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007219 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007220 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007221 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007222 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007223 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007224
Bram Moolenaar77709b12021-04-03 21:01:01 +02007225 // Without operator check type here, otherwise below.
7226 // Use the line number of the assignment.
7227 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007228 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7229 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007230 // If assigning to a list or dict member, use the
7231 // member type. Not for "list[:] =".
7232 if (lhs.lhs_has_index
7233 && !has_list_index(var_start + lhs.lhs_varlen,
7234 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007235 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007236 if (need_type_where(rhs_type, use_type, -1, where,
7237 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007238 goto theend;
7239 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007240 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007241 else
7242 {
7243 type_T *lhs_type = lhs.lhs_member_type;
7244
7245 // Special case: assigning to @# can use a number or a
7246 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007247 // Also: can assign a number to a float.
7248 if ((lhs_type == &t_number_or_string
7249 || lhs_type == &t_float)
7250 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007251 lhs_type = &t_number;
7252 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007253 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007254 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007255 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007257 else if (cmdidx == CMD_final)
7258 {
7259 emsg(_(e_final_requires_a_value));
7260 goto theend;
7261 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007262 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007264 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007265 goto theend;
7266 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00007267 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option
7268 || lhs.lhs_dest == dest_func_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007269 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007270 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007271 goto theend;
7272 }
7273 else
7274 {
7275 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007276 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007277 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007278 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007279 {
7280 case VAR_BOOL:
7281 generate_PUSHBOOL(cctx, VVAL_FALSE);
7282 break;
7283 case VAR_FLOAT:
7284#ifdef FEAT_FLOAT
7285 generate_PUSHF(cctx, 0.0);
7286#endif
7287 break;
7288 case VAR_STRING:
7289 generate_PUSHS(cctx, NULL);
7290 break;
7291 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007292 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007293 break;
7294 case VAR_FUNC:
7295 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7296 break;
7297 case VAR_LIST:
7298 generate_NEWLIST(cctx, 0);
7299 break;
7300 case VAR_DICT:
7301 generate_NEWDICT(cctx, 0);
7302 break;
7303 case VAR_JOB:
7304 generate_PUSHJOB(cctx, NULL);
7305 break;
7306 case VAR_CHANNEL:
7307 generate_PUSHCHANNEL(cctx, NULL);
7308 break;
7309 case VAR_NUMBER:
7310 case VAR_UNKNOWN:
7311 case VAR_ANY:
7312 case VAR_PARTIAL:
7313 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007314 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007315 case VAR_SPECIAL: // cannot happen
7316 generate_PUSHNR(cctx, 0);
7317 break;
7318 }
7319 }
7320 if (var_count == 0)
7321 end = p;
7322 }
7323
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007324 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007325 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007326 break;
7327
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007328 if (oplen > 0 && *op != '=')
7329 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007330 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007331 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007332
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007333 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007334 {
7335 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7336 goto theend;
7337 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007338 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007339 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007340 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007341 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7342 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007343#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007344 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007345 !(expected == &t_float && (stacktype == &t_number
7346 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007347#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007348 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007349 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007350 goto theend;
7351 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007352
7353 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007354 {
7355 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7356 goto theend;
7357 }
7358 else if (*op == '+')
7359 {
7360 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007361 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007362 lhs.lhs_member_type, stacktype,
7363 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007364 goto theend;
7365 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007366 else if (generate_two_op(cctx, op) == FAIL)
7367 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007370 // Use the line number of the assignment for store instruction.
7371 save_lnum = cctx->ctx_lnum;
7372 cctx->ctx_lnum = start_lnum - 1;
7373
Bram Moolenaar752fc692021-01-04 21:57:11 +01007374 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007375 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007376 // Use the info in "lhs" to store the value at the index in the
7377 // list or dict.
7378 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7379 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007380 {
7381 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007382 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007383 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007384 }
7385 else
7386 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007387 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007388 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007389 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007390 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007391 generate_LOCKCONST(cctx);
7392
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007393 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007394 && (lhs.lhs_type->tt_type == VAR_DICT
7395 || lhs.lhs_type->tt_type == VAR_LIST)
7396 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007397 && !(lhs.lhs_type->tt_member == &t_any
7398 && oplen > 0
7399 && rhs_type != NULL
7400 && rhs_type->tt_type == lhs.lhs_type->tt_type
7401 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007402 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007403 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007404 // also in legacy script. Not for "list<any> = val", then the
7405 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007406 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007407
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007408 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007409 {
7410 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007411 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007412 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007413 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007414 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007415
7416 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00007417 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007418 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007419
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007420 // For "[var, var] = expr" drop the "expr" value.
7421 // Also for "[var, var; _] = expr".
7422 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007423 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007424 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007425 goto theend;
7426 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007427
Bram Moolenaarb2097502020-07-19 17:17:02 +02007428 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429
7430theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007431 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 return ret;
7433}
7434
7435/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007436 * Check for an assignment at "eap->cmd", compile it if found.
7437 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7438 */
7439 static int
7440may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7441{
7442 char_u *pskip;
7443 char_u *p;
7444
7445 // Assuming the command starts with a variable or function name,
7446 // find what follows.
7447 // Skip over "var.member", "var[idx]" and the like.
7448 // Also "&opt = val", "$ENV = val" and "@r = val".
7449 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7450 ? eap->cmd + 1 : eap->cmd;
7451 p = to_name_end(pskip, TRUE);
7452 if (p > eap->cmd && *p != NUL)
7453 {
7454 char_u *var_end;
7455 int oplen;
7456 int heredoc;
7457
7458 if (eap->cmd[0] == '@')
7459 var_end = eap->cmd + 2;
7460 else
7461 var_end = find_name_end(pskip, NULL, NULL,
7462 FNE_CHECK_START | FNE_INCL_BR);
7463 oplen = assignment_len(skipwhite(var_end), &heredoc);
7464 if (oplen > 0)
7465 {
7466 size_t len = p - eap->cmd;
7467
7468 // Recognize an assignment if we recognize the variable
7469 // name:
7470 // "g:var = expr"
7471 // "local = expr" where "local" is a local var.
7472 // "script = expr" where "script" is a script-local var.
7473 // "import = expr" where "import" is an imported var
7474 // "&opt = expr"
7475 // "$ENV = expr"
7476 // "@r = expr"
7477 if (*eap->cmd == '&'
7478 || *eap->cmd == '$'
7479 || *eap->cmd == '@'
7480 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007481 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007482 {
7483 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7484 if (*line == NULL || *line == eap->cmd)
7485 return FAIL;
7486 return OK;
7487 }
7488 }
7489 }
7490
7491 if (*eap->cmd == '[')
7492 {
7493 // [var, var] = expr
7494 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7495 if (*line == NULL)
7496 return FAIL;
7497 if (*line != eap->cmd)
7498 return OK;
7499 }
7500 return NOTDONE;
7501}
7502
7503/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007504 * Check if "name" can be "unlet".
7505 */
7506 int
7507check_vim9_unlet(char_u *name)
7508{
7509 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7510 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007511 // "unlet s:var" is allowed in legacy script.
7512 if (*name == 's' && !script_is_vim9())
7513 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007514 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007515 return FAIL;
7516 }
7517 return OK;
7518}
7519
7520/*
7521 * Callback passed to ex_unletlock().
7522 */
7523 static int
7524compile_unlet(
7525 lval_T *lvp,
7526 char_u *name_end,
7527 exarg_T *eap,
7528 int deep UNUSED,
7529 void *coookie)
7530{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007531 cctx_T *cctx = coookie;
7532 char_u *p = lvp->ll_name;
7533 int cc = *name_end;
7534 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007535
Bram Moolenaar752fc692021-01-04 21:57:11 +01007536 if (cctx->ctx_skip == SKIP_YES)
7537 return OK;
7538
7539 *name_end = NUL;
7540 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007541 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007542 // :unlet $ENV_VAR
7543 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7544 }
7545 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7546 {
7547 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007548
Bram Moolenaar752fc692021-01-04 21:57:11 +01007549 // This is similar to assigning: lookup the list/dict, compile the
7550 // idx/key. Then instead of storing the value unlet the item.
7551 // unlet {list}[idx]
7552 // unlet {dict}[key] dict.key
7553 //
7554 // Figure out the LHS type and other properties.
7555 //
7556 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7557
7558 // : unlet an indexed item
7559 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007560 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007561 iemsg("called compile_lhs() without an index");
7562 ret = FAIL;
7563 }
7564 else
7565 {
7566 // Use the info in "lhs" to unlet the item at the index in the
7567 // list or dict.
7568 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007569 }
7570
Bram Moolenaar752fc692021-01-04 21:57:11 +01007571 vim_free(lhs.lhs_name);
7572 }
7573 else if (check_vim9_unlet(p) == FAIL)
7574 {
7575 ret = FAIL;
7576 }
7577 else
7578 {
7579 // Normal name. Only supports g:, w:, t: and b: namespaces.
7580 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007581 }
7582
Bram Moolenaar752fc692021-01-04 21:57:11 +01007583 *name_end = cc;
7584 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007585}
7586
7587/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007588 * Callback passed to ex_unletlock().
7589 */
7590 static int
7591compile_lock_unlock(
7592 lval_T *lvp,
7593 char_u *name_end,
7594 exarg_T *eap,
7595 int deep UNUSED,
7596 void *coookie)
7597{
7598 cctx_T *cctx = coookie;
7599 int cc = *name_end;
7600 char_u *p = lvp->ll_name;
7601 int ret = OK;
7602 size_t len;
7603 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007604 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007605
7606 if (cctx->ctx_skip == SKIP_YES)
7607 return OK;
7608
7609 // Cannot use :lockvar and :unlockvar on local variables.
7610 if (p[1] != ':')
7611 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007612 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007613
7614 if (lookup_local(p, end - p, NULL, cctx) == OK)
7615 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007616 char_u *s = p;
7617
7618 if (*end != '.' && *end != '[')
7619 {
7620 emsg(_(e_cannot_lock_unlock_local_variable));
7621 return FAIL;
7622 }
7623
7624 // For "d.member" put the local variable on the stack, it will be
7625 // passed to ex_lockvar() indirectly.
7626 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7627 return FAIL;
7628 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007629 }
7630 }
7631
7632 // Checking is done at runtime.
7633 *name_end = NUL;
7634 len = name_end - p + 20;
7635 buf = alloc(len);
7636 if (buf == NULL)
7637 ret = FAIL;
7638 else
7639 {
7640 vim_snprintf((char *)buf, len, "%s %s",
7641 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7642 p);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00007643 ret = generate_EXEC_copy(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007644
7645 vim_free(buf);
7646 *name_end = cc;
7647 }
7648 return ret;
7649}
7650
7651/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007652 * compile "unlet var", "lock var" and "unlock var"
7653 * "arg" points to "var".
7654 */
7655 static char_u *
7656compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7657{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007658 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7659 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7660 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007661 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7662}
7663
7664/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007665 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7666 */
7667 static int
7668compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7669{
7670 garray_T *instr = &cctx->ctx_instr;
7671 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7672
7673 if (endlabel == NULL)
7674 return FAIL;
7675 endlabel->el_next = *el;
7676 *el = endlabel;
7677 endlabel->el_end_label = instr->ga_len;
7678
7679 generate_JUMP(cctx, when, 0);
7680 return OK;
7681}
7682
7683 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007684compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007685{
7686 garray_T *instr = &cctx->ctx_instr;
7687
7688 while (*el != NULL)
7689 {
7690 endlabel_T *cur = (*el);
7691 isn_T *isn;
7692
7693 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007694 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007695 *el = cur->el_next;
7696 vim_free(cur);
7697 }
7698}
7699
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007700 static void
7701compile_free_jump_to_end(endlabel_T **el)
7702{
7703 while (*el != NULL)
7704 {
7705 endlabel_T *cur = (*el);
7706
7707 *el = cur->el_next;
7708 vim_free(cur);
7709 }
7710}
7711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007712/*
7713 * Create a new scope and set up the generic items.
7714 */
7715 static scope_T *
7716new_scope(cctx_T *cctx, scopetype_T type)
7717{
7718 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7719
7720 if (scope == NULL)
7721 return NULL;
7722 scope->se_outer = cctx->ctx_scope;
7723 cctx->ctx_scope = scope;
7724 scope->se_type = type;
7725 scope->se_local_count = cctx->ctx_locals.ga_len;
7726 return scope;
7727}
7728
7729/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007730 * Free the current scope and go back to the outer scope.
7731 */
7732 static void
7733drop_scope(cctx_T *cctx)
7734{
7735 scope_T *scope = cctx->ctx_scope;
7736
7737 if (scope == NULL)
7738 {
7739 iemsg("calling drop_scope() without a scope");
7740 return;
7741 }
7742 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007743 switch (scope->se_type)
7744 {
7745 case IF_SCOPE:
7746 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7747 case FOR_SCOPE:
7748 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7749 case WHILE_SCOPE:
7750 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7751 case TRY_SCOPE:
7752 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7753 case NO_SCOPE:
7754 case BLOCK_SCOPE:
7755 break;
7756 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007757 vim_free(scope);
7758}
7759
7760/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007761 * compile "if expr"
7762 *
7763 * "if expr" Produces instructions:
7764 * EVAL expr Push result of "expr"
7765 * JUMP_IF_FALSE end
7766 * ... body ...
7767 * end:
7768 *
7769 * "if expr | else" Produces instructions:
7770 * EVAL expr Push result of "expr"
7771 * JUMP_IF_FALSE else
7772 * ... body ...
7773 * JUMP_ALWAYS end
7774 * else:
7775 * ... body ...
7776 * end:
7777 *
7778 * "if expr1 | elseif expr2 | else" Produces instructions:
7779 * EVAL expr Push result of "expr"
7780 * JUMP_IF_FALSE elseif
7781 * ... body ...
7782 * JUMP_ALWAYS end
7783 * elseif:
7784 * EVAL expr Push result of "expr"
7785 * JUMP_IF_FALSE else
7786 * ... body ...
7787 * JUMP_ALWAYS end
7788 * else:
7789 * ... body ...
7790 * end:
7791 */
7792 static char_u *
7793compile_if(char_u *arg, cctx_T *cctx)
7794{
7795 char_u *p = arg;
7796 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007797 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007799 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007800 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007801
Bram Moolenaara5565e42020-05-09 15:44:01 +02007802 CLEAR_FIELD(ppconst);
7803 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007804 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007805 clear_ppconst(&ppconst);
7806 return NULL;
7807 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007808 if (!ends_excmd2(arg, skipwhite(p)))
7809 {
7810 semsg(_(e_trailing_arg), p);
7811 return NULL;
7812 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007813 if (cctx->ctx_skip == SKIP_YES)
7814 clear_ppconst(&ppconst);
7815 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007816 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007817 int error = FALSE;
7818 int v;
7819
Bram Moolenaara5565e42020-05-09 15:44:01 +02007820 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007821 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007822 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007823 if (error)
7824 return NULL;
7825 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007826 }
7827 else
7828 {
7829 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007830 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007831 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007832 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007833 if (bool_on_stack(cctx) == FAIL)
7834 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007835 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836
Bram Moolenaara91a7132021-03-25 21:12:15 +01007837 // CMDMOD_REV must come before the jump
7838 generate_undo_cmdmods(cctx);
7839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007840 scope = new_scope(cctx, IF_SCOPE);
7841 if (scope == NULL)
7842 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007843 scope->se_skip_save = skip_save;
7844 // "is_had_return" will be reset if any block does not end in :return
7845 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007846
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007847 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007848 {
7849 // "where" is set when ":elseif", "else" or ":endif" is found
7850 scope->se_u.se_if.is_if_label = instr->ga_len;
7851 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7852 }
7853 else
7854 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007855
Bram Moolenaarced68a02021-01-24 17:53:47 +01007856#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007857 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007858 && skip_save != SKIP_YES)
7859 {
7860 // generated a profile start, need to generate a profile end, since it
7861 // won't be done after returning
7862 cctx->ctx_skip = SKIP_NOT;
7863 generate_instr(cctx, ISN_PROF_END);
7864 cctx->ctx_skip = SKIP_YES;
7865 }
7866#endif
7867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868 return p;
7869}
7870
7871 static char_u *
7872compile_elseif(char_u *arg, cctx_T *cctx)
7873{
7874 char_u *p = arg;
7875 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar90770b72021-11-30 20:57:38 +00007876 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007877 isn_T *isn;
7878 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007879 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007880 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881
7882 if (scope == NULL || scope->se_type != IF_SCOPE)
7883 {
7884 emsg(_(e_elseif_without_if));
7885 return NULL;
7886 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007887 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007888 if (!cctx->ctx_had_return)
7889 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007890
Bram Moolenaarced68a02021-01-24 17:53:47 +01007891 if (cctx->ctx_skip == SKIP_NOT)
7892 {
7893 // previous block was executed, this one and following will not
7894 cctx->ctx_skip = SKIP_YES;
7895 scope->se_u.se_if.is_seen_skip_not = TRUE;
7896 }
7897 if (scope->se_u.se_if.is_seen_skip_not)
7898 {
7899 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007900 // Do not count the "elseif" for profiling and cmdmod
7901 instr->ga_len = current_instr_idx(cctx);
7902
Bram Moolenaarced68a02021-01-24 17:53:47 +01007903 skip_expr_cctx(&p, cctx);
7904 return p;
7905 }
7906
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007907 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007908 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007909 int moved_cmdmod = FALSE;
7910 int saved_debug = FALSE;
7911 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007912
7913 // Move any CMDMOD instruction to after the jump
7914 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7915 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007916 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007917 return NULL;
7918 ((isn_T *)instr->ga_data)[instr->ga_len] =
7919 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7920 --instr->ga_len;
7921 moved_cmdmod = TRUE;
7922 }
7923
Bram Moolenaar093165c2021-08-22 13:35:31 +02007924 // Remove the already generated ISN_DEBUG, it is written below the
7925 // ISN_FOR instruction.
7926 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7927 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7928 .isn_type == ISN_DEBUG)
7929 {
7930 --instr->ga_len;
7931 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7932 saved_debug = TRUE;
7933 }
7934
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007935 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007936 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007937 return NULL;
7938 // previous "if" or "elseif" jumps here
7939 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7940 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007941
Bram Moolenaara91a7132021-03-25 21:12:15 +01007942 if (moved_cmdmod)
7943 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007944
7945 if (saved_debug)
7946 {
7947 // move the debug instruction here
7948 if (GA_GROW_FAILS(instr, 1))
7949 return NULL;
7950 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7951 ++instr->ga_len;
7952 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007955 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007956 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007957 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007958 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007959 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007960#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007961 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007962 // the previous block was skipped, need to profile this line
7963 generate_instr(cctx, ISN_PROF_START);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007964#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007965 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007966 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007967 generate_instr_debug(cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007968 }
Bram Moolenaar90770b72021-11-30 20:57:38 +00007969
7970 instr_count = instr->ga_len;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007971 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007972 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007973 clear_ppconst(&ppconst);
7974 return NULL;
7975 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007976 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007977 if (!ends_excmd2(arg, skipwhite(p)))
7978 {
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007979 clear_ppconst(&ppconst);
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007980 semsg(_(e_trailing_arg), p);
7981 return NULL;
7982 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007983 if (scope->se_skip_save == SKIP_YES)
7984 clear_ppconst(&ppconst);
7985 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007986 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007987 int error = FALSE;
7988 int v;
7989
Bram Moolenaarfad27422021-11-30 21:58:19 +00007990 // The expression result is a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007991 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7992 if (error)
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007993 {
7994 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007995 return NULL;
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007996 }
Bram Moolenaar13106602020-10-04 16:06:05 +02007997 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007998 clear_ppconst(&ppconst);
7999 scope->se_u.se_if.is_if_label = -1;
8000 }
8001 else
8002 {
8003 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008004 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02008005 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008006 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008007 if (bool_on_stack(cctx) == FAIL)
8008 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008009
Bram Moolenaara91a7132021-03-25 21:12:15 +01008010 // CMDMOD_REV must come before the jump
8011 generate_undo_cmdmods(cctx);
8012
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008013 // "where" is set when ":elseif", "else" or ":endif" is found
8014 scope->se_u.se_if.is_if_label = instr->ga_len;
8015 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
8016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008017
8018 return p;
8019}
8020
8021 static char_u *
8022compile_else(char_u *arg, cctx_T *cctx)
8023{
8024 char_u *p = arg;
8025 garray_T *instr = &cctx->ctx_instr;
8026 isn_T *isn;
8027 scope_T *scope = cctx->ctx_scope;
8028
8029 if (scope == NULL || scope->se_type != IF_SCOPE)
8030 {
8031 emsg(_(e_else_without_if));
8032 return NULL;
8033 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01008034 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008035 if (!cctx->ctx_had_return)
8036 scope->se_u.se_if.is_had_return = FALSE;
8037 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008038
Bram Moolenaarced68a02021-01-24 17:53:47 +01008039#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008040 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01008041 {
8042 if (cctx->ctx_skip == SKIP_NOT
8043 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8044 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02008045 // the previous block was executed, do not count "else" for
8046 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01008047 --instr->ga_len;
8048 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
8049 {
8050 // the previous block was not executed, this one will, do count the
8051 // "else" for profiling
8052 cctx->ctx_skip = SKIP_NOT;
8053 generate_instr(cctx, ISN_PROF_END);
8054 generate_instr(cctx, ISN_PROF_START);
8055 cctx->ctx_skip = SKIP_YES;
8056 }
8057 }
8058#endif
8059
8060 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008061 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008062 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008063 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008064 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008065 if (!cctx->ctx_had_return
8066 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
8067 JUMP_ALWAYS, cctx) == FAIL)
8068 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008069 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008070
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008071 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008072 {
8073 if (scope->se_u.se_if.is_if_label >= 0)
8074 {
8075 // previous "if" or "elseif" jumps here
8076 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8077 isn->isn_arg.jump.jump_where = instr->ga_len;
8078 scope->se_u.se_if.is_if_label = -1;
8079 }
8080 }
8081
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008082 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008083 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
8084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008085
8086 return p;
8087}
8088
8089 static char_u *
8090compile_endif(char_u *arg, cctx_T *cctx)
8091{
8092 scope_T *scope = cctx->ctx_scope;
8093 ifscope_T *ifscope;
8094 garray_T *instr = &cctx->ctx_instr;
8095 isn_T *isn;
8096
Bram Moolenaarfa984412021-03-25 22:15:28 +01008097 if (misplaced_cmdmod(cctx))
8098 return NULL;
8099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008100 if (scope == NULL || scope->se_type != IF_SCOPE)
8101 {
8102 emsg(_(e_endif_without_if));
8103 return NULL;
8104 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008105 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008106 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008107 if (!cctx->ctx_had_return)
8108 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008109
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008110 if (scope->se_u.se_if.is_if_label >= 0)
8111 {
8112 // previous "if" or "elseif" jumps here
8113 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8114 isn->isn_arg.jump.jump_where = instr->ga_len;
8115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008116 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008117 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01008118
8119#ifdef FEAT_PROFILE
8120 // even when skipping we count the endif as executed, unless the block it's
8121 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02008122 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01008123 && scope->se_skip_save != SKIP_YES)
8124 {
8125 cctx->ctx_skip = SKIP_NOT;
8126 generate_instr(cctx, ISN_PROF_START);
8127 }
8128#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02008129 cctx->ctx_skip = scope->se_skip_save;
8130
8131 // If all the blocks end in :return and there is an :else then the
8132 // had_return flag is set.
8133 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008134
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008135 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008136 return arg;
8137}
8138
8139/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01008140 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008141 *
8142 * Produces instructions:
8143 * PUSHNR -1
8144 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01008145 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008146 * top: FOR loop-idx, end Increment index, use list on bottom of stack
8147 * - if beyond end, jump to "end"
8148 * - otherwise get item from list and push it
8149 * STORE var Store item in "var"
8150 * ... body ...
8151 * JUMP top Jump back to repeat
8152 * end: DROP Drop the result of "expr"
8153 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008154 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
8155 * UNPACK 2 Split item in 2
8156 * STORE var1 Store item in "var1"
8157 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008158 */
8159 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008160compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008161{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008162 char_u *arg;
8163 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008164 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008165 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008166 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008167 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008168 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008169 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008170 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008171 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008172 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008173 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008174 lvar_T *loop_lvar; // loop iteration variable
8175 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008176 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008177 type_T *item_type = &t_any;
8178 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008179 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008180
Bram Moolenaar792f7862020-11-23 08:31:18 +01008181 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008182 if (p == NULL)
8183 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008184 if (var_count == 0)
8185 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008186 else
8187 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188
8189 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008190 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008191 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8192 return NULL;
8193 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008194 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008195 if (*p == ':' && wp != p)
8196 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8197 else
8198 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008199 return NULL;
8200 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008201 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008202 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8203 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008204
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008205 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8206 // instruction.
8207 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8208 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8209 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008210 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008211 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008212 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8213 .isn_arg.debug.dbg_break_lnum;
8214 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008216 scope = new_scope(cctx, FOR_SCOPE);
8217 if (scope == NULL)
8218 return NULL;
8219
Bram Moolenaar792f7862020-11-23 08:31:18 +01008220 // Reserve a variable to store the loop iteration counter and initialize it
8221 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008222 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8223 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008224 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008225 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008226 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008227 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008228 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008229 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008230
8231 // compile "expr", it remains on the stack until "endfor"
8232 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008233 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008234 {
8235 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008236 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008237 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008238 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008239
rbtnnbebf0692021-08-21 17:26:50 +02008240 if (cctx->ctx_skip != SKIP_YES)
8241 {
8242 // If we know the type of "var" and it is a not a supported type we can
8243 // give an error now.
8244 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8245 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008246 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008247 {
rbtnnbebf0692021-08-21 17:26:50 +02008248 semsg(_(e_for_loop_on_str_not_supported),
8249 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008250 drop_scope(cctx);
8251 return NULL;
8252 }
rbtnnbebf0692021-08-21 17:26:50 +02008253
8254 if (vartype->tt_type == VAR_STRING)
8255 item_type = &t_string;
8256 else if (vartype->tt_type == VAR_BLOB)
8257 item_type = &t_number;
8258 else if (vartype->tt_type == VAR_LIST
8259 && vartype->tt_member->tt_type != VAR_ANY)
8260 {
8261 if (!var_list)
8262 item_type = vartype->tt_member;
8263 else if (vartype->tt_member->tt_type == VAR_LIST
8264 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
rbtnnbebf0692021-08-21 17:26:50 +02008265 item_type = vartype->tt_member->tt_member;
8266 }
8267
8268 // CMDMOD_REV must come before the FOR instruction.
8269 generate_undo_cmdmods(cctx);
8270
8271 // "for_end" is set when ":endfor" is found
8272 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8273
8274 generate_FOR(cctx, loop_lvar->lv_idx);
8275
8276 arg = arg_start;
8277 if (var_list)
8278 {
8279 generate_UNPACK(cctx, var_count, semicolon);
8280 arg = skipwhite(arg + 1); // skip white after '['
8281
8282 // the list item is replaced by a number of items
8283 if (GA_GROW_FAILS(stack, var_count - 1))
8284 {
8285 drop_scope(cctx);
8286 return NULL;
8287 }
8288 --stack->ga_len;
8289 for (idx = 0; idx < var_count; ++idx)
8290 {
8291 ((type_T **)stack->ga_data)[stack->ga_len] =
8292 (semicolon && idx == 0) ? vartype : item_type;
8293 ++stack->ga_len;
8294 }
8295 }
8296
Bram Moolenaar792f7862020-11-23 08:31:18 +01008297 for (idx = 0; idx < var_count; ++idx)
8298 {
rbtnnbebf0692021-08-21 17:26:50 +02008299 assign_dest_T dest = dest_local;
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008300 int opt_flags = 0;
8301 int vimvaridx = -1;
rbtnnbebf0692021-08-21 17:26:50 +02008302 type_T *type = &t_any;
8303 type_T *lhs_type = &t_any;
8304 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008305
rbtnnbebf0692021-08-21 17:26:50 +02008306 p = skip_var_one(arg, FALSE);
8307 varlen = p - arg;
8308 name = vim_strnsave(arg, varlen);
8309 if (name == NULL)
8310 goto failed;
8311 if (*p == ':')
8312 {
8313 p = skipwhite(p + 1);
8314 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8315 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008316
Bram Moolenaarfad27422021-11-30 21:58:19 +00008317 // Script var is not supported.
rbtnnbebf0692021-08-21 17:26:50 +02008318 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008319 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008320 goto failed;
8321 if (dest != dest_local)
8322 {
8323 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008324 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008325 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008326 }
rbtnnbebf0692021-08-21 17:26:50 +02008327 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008328 {
rbtnnbebf0692021-08-21 17:26:50 +02008329 // Assigning to "_": drop the value.
8330 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8331 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008332 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008333 else
rbtnnbebf0692021-08-21 17:26:50 +02008334 {
Mike Williamscc9d7252021-11-23 12:35:57 +00008335 if (!valid_varname(arg, (int)varlen, FALSE))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008336 goto failed;
rbtnnbebf0692021-08-21 17:26:50 +02008337 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8338 {
8339 semsg(_(e_variable_already_declared), arg);
8340 goto failed;
8341 }
8342
8343 if (STRNCMP(name, "s:", 2) == 0)
8344 {
8345 semsg(_(e_cannot_declare_script_variable_in_function), name);
8346 goto failed;
8347 }
8348
8349 // Reserve a variable to store "var".
8350 where.wt_index = var_list ? idx + 1 : 0;
8351 where.wt_variable = TRUE;
8352 if (lhs_type == &t_any)
8353 lhs_type = item_type;
8354 else if (item_type != &t_unknown
8355 && (item_type == &t_any
8356 ? need_type(item_type, lhs_type,
8357 -1, 0, cctx, FALSE, FALSE)
8358 : check_type(lhs_type, item_type, TRUE, where))
8359 == FAIL)
8360 goto failed;
8361 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8362 if (var_lvar == NULL)
8363 // out of memory or used as an argument
8364 goto failed;
8365
8366 if (semicolon && idx == var_count - 1)
8367 var_lvar->lv_type = vartype;
8368 else
8369 var_lvar->lv_type = item_type;
8370 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8371 }
8372
8373 if (*p == ',' || *p == ';')
8374 ++p;
8375 arg = skipwhite(p);
8376 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008377 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008378
rbtnnbebf0692021-08-21 17:26:50 +02008379 if (cctx->ctx_compile_type == CT_DEBUG)
8380 {
8381 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008382
rbtnnbebf0692021-08-21 17:26:50 +02008383 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8384 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8385 cctx->ctx_prev_lnum = prev_lnum;
8386 generate_instr_debug(cctx);
8387 cctx->ctx_prev_lnum = save_prev_lnum;
8388 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008389 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008390
Bram Moolenaar792f7862020-11-23 08:31:18 +01008391 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008392
8393failed:
8394 vim_free(name);
8395 drop_scope(cctx);
8396 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008397}
8398
8399/*
8400 * compile "endfor"
8401 */
8402 static char_u *
8403compile_endfor(char_u *arg, cctx_T *cctx)
8404{
8405 garray_T *instr = &cctx->ctx_instr;
8406 scope_T *scope = cctx->ctx_scope;
8407 forscope_T *forscope;
8408 isn_T *isn;
8409
Bram Moolenaarfa984412021-03-25 22:15:28 +01008410 if (misplaced_cmdmod(cctx))
8411 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008413 if (scope == NULL || scope->se_type != FOR_SCOPE)
8414 {
8415 emsg(_(e_for));
8416 return NULL;
8417 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008418 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008419 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008420 if (cctx->ctx_skip != SKIP_YES)
8421 {
8422 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008423
rbtnnbebf0692021-08-21 17:26:50 +02008424 // At end of ":for" scope jump back to the FOR instruction.
8425 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008426
rbtnnbebf0692021-08-21 17:26:50 +02008427 // Fill in the "end" label in the FOR statement so it can jump here.
8428 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8429 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008430
rbtnnbebf0692021-08-21 17:26:50 +02008431 // Fill in the "end" label any BREAK statements
8432 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433
rbtnnbebf0692021-08-21 17:26:50 +02008434 // Below the ":for" scope drop the "expr" list from the stack.
8435 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8436 return NULL;
8437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008438
8439 vim_free(scope);
8440
8441 return arg;
8442}
8443
8444/*
8445 * compile "while expr"
8446 *
8447 * Produces instructions:
8448 * top: EVAL expr Push result of "expr"
8449 * JUMP_IF_FALSE end jump if false
8450 * ... body ...
8451 * JUMP top Jump back to repeat
8452 * end:
8453 *
8454 */
8455 static char_u *
8456compile_while(char_u *arg, cctx_T *cctx)
8457{
8458 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008459 scope_T *scope;
8460
8461 scope = new_scope(cctx, WHILE_SCOPE);
8462 if (scope == NULL)
8463 return NULL;
8464
Bram Moolenaara91a7132021-03-25 21:12:15 +01008465 // "endwhile" jumps back here, one before when profiling or using cmdmods
8466 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008467
8468 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008469 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008470 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008471
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008472 if (!ends_excmd2(arg, skipwhite(p)))
8473 {
8474 semsg(_(e_trailing_arg), p);
8475 return NULL;
8476 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008477
rbtnnd895b1d2021-08-20 20:54:25 +02008478 if (cctx->ctx_skip != SKIP_YES)
8479 {
8480 if (bool_on_stack(cctx) == FAIL)
8481 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008482
rbtnnd895b1d2021-08-20 20:54:25 +02008483 // CMDMOD_REV must come before the jump
8484 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008485
rbtnnd895b1d2021-08-20 20:54:25 +02008486 // "while_end" is set when ":endwhile" is found
8487 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008488 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008489 return FAIL;
8490 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008491
8492 return p;
8493}
8494
8495/*
8496 * compile "endwhile"
8497 */
8498 static char_u *
8499compile_endwhile(char_u *arg, cctx_T *cctx)
8500{
8501 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008502 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008503
Bram Moolenaarfa984412021-03-25 22:15:28 +01008504 if (misplaced_cmdmod(cctx))
8505 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008506 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8507 {
8508 emsg(_(e_while));
8509 return NULL;
8510 }
8511 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008512 if (cctx->ctx_skip != SKIP_YES)
8513 {
8514 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008515
Bram Moolenaarf002a412021-01-24 13:34:18 +01008516#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008517 // count the endwhile before jumping
8518 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008519#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008520
rbtnnd895b1d2021-08-20 20:54:25 +02008521 // At end of ":for" scope jump back to the FOR instruction.
8522 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008523
rbtnnd895b1d2021-08-20 20:54:25 +02008524 // Fill in the "end" label in the WHILE statement so it can jump here.
8525 // And in any jumps for ":break"
8526 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008527 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008528 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008529
8530 vim_free(scope);
8531
8532 return arg;
8533}
8534
8535/*
8536 * compile "continue"
8537 */
8538 static char_u *
8539compile_continue(char_u *arg, cctx_T *cctx)
8540{
8541 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008542 int try_scopes = 0;
8543 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008544
8545 for (;;)
8546 {
8547 if (scope == NULL)
8548 {
8549 emsg(_(e_continue));
8550 return NULL;
8551 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008552 if (scope->se_type == FOR_SCOPE)
8553 {
8554 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008555 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008556 }
8557 if (scope->se_type == WHILE_SCOPE)
8558 {
8559 loop_label = scope->se_u.se_while.ws_top_label;
8560 break;
8561 }
8562 if (scope->se_type == TRY_SCOPE)
8563 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008564 scope = scope->se_outer;
8565 }
8566
Bram Moolenaarc150c092021-02-13 15:02:46 +01008567 if (try_scopes > 0)
8568 // Inside one or more try/catch blocks we first need to jump to the
8569 // "finally" or "endtry" to cleanup.
8570 generate_TRYCONT(cctx, try_scopes, loop_label);
8571 else
8572 // Jump back to the FOR or WHILE instruction.
8573 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008575 return arg;
8576}
8577
8578/*
8579 * compile "break"
8580 */
8581 static char_u *
8582compile_break(char_u *arg, cctx_T *cctx)
8583{
8584 scope_T *scope = cctx->ctx_scope;
8585 endlabel_T **el;
8586
8587 for (;;)
8588 {
8589 if (scope == NULL)
8590 {
8591 emsg(_(e_break));
8592 return NULL;
8593 }
8594 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8595 break;
8596 scope = scope->se_outer;
8597 }
8598
8599 // Jump to the end of the FOR or WHILE loop.
8600 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008601 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008602 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008603 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008604 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8605 return FAIL;
8606
8607 return arg;
8608}
8609
8610/*
8611 * compile "{" start of block
8612 */
8613 static char_u *
8614compile_block(char_u *arg, cctx_T *cctx)
8615{
8616 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8617 return NULL;
8618 return skipwhite(arg + 1);
8619}
8620
8621/*
8622 * compile end of block: drop one scope
8623 */
8624 static void
8625compile_endblock(cctx_T *cctx)
8626{
8627 scope_T *scope = cctx->ctx_scope;
8628
8629 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008630 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008631 vim_free(scope);
8632}
8633
8634/*
8635 * compile "try"
8636 * Creates a new scope for the try-endtry, pointing to the first catch and
8637 * finally.
8638 * Creates another scope for the "try" block itself.
8639 * TRY instruction sets up exception handling at runtime.
8640 *
8641 * "try"
8642 * TRY -> catch1, -> finally push trystack entry
8643 * ... try block
8644 * "throw {exception}"
8645 * EVAL {exception}
8646 * THROW create exception
8647 * ... try block
8648 * " catch {expr}"
8649 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008650 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008651 * EVAL {expr}
8652 * MATCH
8653 * JUMP nomatch -> catch2
8654 * CATCH remove exception
8655 * ... catch block
8656 * " catch"
8657 * JUMP -> finally
8658 * catch2: CATCH remove exception
8659 * ... catch block
8660 * " finally"
8661 * finally:
8662 * ... finally block
8663 * " endtry"
8664 * ENDTRY pop trystack entry, may rethrow
8665 */
8666 static char_u *
8667compile_try(char_u *arg, cctx_T *cctx)
8668{
8669 garray_T *instr = &cctx->ctx_instr;
8670 scope_T *try_scope;
8671 scope_T *scope;
8672
Bram Moolenaarfa984412021-03-25 22:15:28 +01008673 if (misplaced_cmdmod(cctx))
8674 return NULL;
8675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008676 // scope that holds the jumps that go to catch/finally/endtry
8677 try_scope = new_scope(cctx, TRY_SCOPE);
8678 if (try_scope == NULL)
8679 return NULL;
8680
Bram Moolenaar69f70502021-01-01 16:10:46 +01008681 if (cctx->ctx_skip != SKIP_YES)
8682 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008683 isn_T *isn;
8684
8685 // "try_catch" is set when the first ":catch" is found or when no catch
8686 // is found and ":finally" is found.
8687 // "try_finally" is set when ":finally" is found
8688 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008689 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008690 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8691 return NULL;
8692 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8693 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008694 return NULL;
8695 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008696
8697 // scope for the try block itself
8698 scope = new_scope(cctx, BLOCK_SCOPE);
8699 if (scope == NULL)
8700 return NULL;
8701
8702 return arg;
8703}
8704
8705/*
8706 * compile "catch {expr}"
8707 */
8708 static char_u *
8709compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8710{
8711 scope_T *scope = cctx->ctx_scope;
8712 garray_T *instr = &cctx->ctx_instr;
8713 char_u *p;
8714 isn_T *isn;
8715
Bram Moolenaarfa984412021-03-25 22:15:28 +01008716 if (misplaced_cmdmod(cctx))
8717 return NULL;
8718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008719 // end block scope from :try or :catch
8720 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8721 compile_endblock(cctx);
8722 scope = cctx->ctx_scope;
8723
8724 // Error if not in a :try scope
8725 if (scope == NULL || scope->se_type != TRY_SCOPE)
8726 {
8727 emsg(_(e_catch));
8728 return NULL;
8729 }
8730
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008731 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008732 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008733 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008734 return NULL;
8735 }
8736
Bram Moolenaar69f70502021-01-01 16:10:46 +01008737 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008738 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008739#ifdef FEAT_PROFILE
8740 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008741 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008742 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008743 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008744 .isn_type == ISN_PROF_START)
8745 --instr->ga_len;
8746#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008747 // Jump from end of previous block to :finally or :endtry
8748 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8749 JUMP_ALWAYS, cctx) == FAIL)
8750 return NULL;
8751
8752 // End :try or :catch scope: set value in ISN_TRY instruction
8753 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008754 if (isn->isn_arg.try.try_ref->try_catch == 0)
8755 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008756 if (scope->se_u.se_try.ts_catch_label != 0)
8757 {
8758 // Previous catch without match jumps here
8759 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8760 isn->isn_arg.jump.jump_where = instr->ga_len;
8761 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008762#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008763 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008764 {
8765 // a "throw" that jumps here needs to be counted
8766 generate_instr(cctx, ISN_PROF_END);
8767 // the "catch" is also counted
8768 generate_instr(cctx, ISN_PROF_START);
8769 }
8770#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008771 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008772 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008773 }
8774
8775 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008776 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008777 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008778 scope->se_u.se_try.ts_caught_all = TRUE;
8779 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008780 }
8781 else
8782 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008783 char_u *end;
8784 char_u *pat;
8785 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008786 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008787 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008789 // Push v:exception, push {expr} and MATCH
8790 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8791
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008792 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008793 if (*end != *p)
8794 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008795 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008796 vim_free(tofree);
8797 return FAIL;
8798 }
8799 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008800 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008801 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008802 len = (int)(end - tofree);
8803 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008804 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008805 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008806 if (pat == NULL)
8807 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008808 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008809 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008810
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008811 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8812 return NULL;
8813
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008814 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008815 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8816 return NULL;
8817 }
8818
Bram Moolenaar69f70502021-01-01 16:10:46 +01008819 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008820 return NULL;
8821
8822 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8823 return NULL;
8824 return p;
8825}
8826
8827 static char_u *
8828compile_finally(char_u *arg, cctx_T *cctx)
8829{
8830 scope_T *scope = cctx->ctx_scope;
8831 garray_T *instr = &cctx->ctx_instr;
8832 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008833 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008834
Bram Moolenaarfa984412021-03-25 22:15:28 +01008835 if (misplaced_cmdmod(cctx))
8836 return NULL;
8837
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008838 // end block scope from :try or :catch
8839 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8840 compile_endblock(cctx);
8841 scope = cctx->ctx_scope;
8842
8843 // Error if not in a :try scope
8844 if (scope == NULL || scope->se_type != TRY_SCOPE)
8845 {
8846 emsg(_(e_finally));
8847 return NULL;
8848 }
8849
rbtnn84934992021-08-07 13:26:53 +02008850 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008851 {
rbtnn84934992021-08-07 13:26:53 +02008852 // End :catch or :finally scope: set value in ISN_TRY instruction
8853 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8854 if (isn->isn_arg.try.try_ref->try_finally != 0)
8855 {
8856 emsg(_(e_finally_dup));
8857 return NULL;
8858 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008859
rbtnn84934992021-08-07 13:26:53 +02008860 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008861#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008862 if (cctx->ctx_compile_type == CT_PROFILE
8863 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008864 .isn_type == ISN_PROF_START)
rbtnn84934992021-08-07 13:26:53 +02008865 {
8866 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008867 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008868
8869 // jump to the profile end above it
8870 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008871 .isn_type == ISN_PROF_END)
rbtnn84934992021-08-07 13:26:53 +02008872 --this_instr;
8873 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008874#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008875
rbtnn84934992021-08-07 13:26:53 +02008876 // Fill in the "end" label in jumps at the end of the blocks.
8877 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarfad27422021-11-30 21:58:19 +00008878 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008879
rbtnn84934992021-08-07 13:26:53 +02008880 // If there is no :catch then an exception jumps to :finally.
8881 if (isn->isn_arg.try.try_ref->try_catch == 0)
8882 isn->isn_arg.try.try_ref->try_catch = this_instr;
8883 isn->isn_arg.try.try_ref->try_finally = this_instr;
8884 if (scope->se_u.se_try.ts_catch_label != 0)
8885 {
8886 // Previous catch without match jumps here
8887 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8888 isn->isn_arg.jump.jump_where = this_instr;
8889 scope->se_u.se_try.ts_catch_label = 0;
8890 }
8891 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8892 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008894
8895 return arg;
8896}
8897
8898 static char_u *
8899compile_endtry(char_u *arg, cctx_T *cctx)
8900{
8901 scope_T *scope = cctx->ctx_scope;
8902 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008903 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008904
Bram Moolenaarfa984412021-03-25 22:15:28 +01008905 if (misplaced_cmdmod(cctx))
8906 return NULL;
8907
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008908 // end block scope from :catch or :finally
8909 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8910 compile_endblock(cctx);
8911 scope = cctx->ctx_scope;
8912
8913 // Error if not in a :try scope
8914 if (scope == NULL || scope->se_type != TRY_SCOPE)
8915 {
8916 if (scope == NULL)
8917 emsg(_(e_no_endtry));
8918 else if (scope->se_type == WHILE_SCOPE)
8919 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008920 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008921 emsg(_(e_endfor));
8922 else
8923 emsg(_(e_endif));
8924 return NULL;
8925 }
8926
Bram Moolenaarc150c092021-02-13 15:02:46 +01008927 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008928 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008929 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008930 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8931 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008932 {
8933 emsg(_(e_missing_catch_or_finally));
8934 return NULL;
8935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008936
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008937#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008938 if (cctx->ctx_compile_type == CT_PROFILE
8939 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008940 .isn_type == ISN_PROF_START)
8941 // move the profile start after "endtry" so that it's not counted when
8942 // the exception is rethrown.
8943 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008944#endif
8945
Bram Moolenaar69f70502021-01-01 16:10:46 +01008946 // Fill in the "end" label in jumps at the end of the blocks, if not
8947 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008948 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8949 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008950
Bram Moolenaar69f70502021-01-01 16:10:46 +01008951 if (scope->se_u.se_try.ts_catch_label != 0)
8952 {
8953 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008954 isn_T *isn = ((isn_T *)instr->ga_data)
8955 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008956 isn->isn_arg.jump.jump_where = instr->ga_len;
8957 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008958 }
8959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008960 compile_endblock(cctx);
8961
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008962 if (cctx->ctx_skip != SKIP_YES)
8963 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008964 // End :catch or :finally scope: set instruction index in ISN_TRY
8965 // instruction
8966 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008967 if (cctx->ctx_skip != SKIP_YES
8968 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8969 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008970#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008971 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008972 generate_instr(cctx, ISN_PROF_START);
8973#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008974 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008975 return arg;
8976}
8977
8978/*
8979 * compile "throw {expr}"
8980 */
8981 static char_u *
8982compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8983{
8984 char_u *p = skipwhite(arg);
8985
Bram Moolenaara5565e42020-05-09 15:44:01 +02008986 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008987 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008988 if (cctx->ctx_skip == SKIP_YES)
8989 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008990 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008991 return NULL;
8992 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8993 return NULL;
8994
8995 return p;
8996}
8997
Bram Moolenaarc3235272021-07-10 19:42:03 +02008998 static char_u *
8999compile_eval(char_u *arg, cctx_T *cctx)
9000{
9001 char_u *p = arg;
9002 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02009003 long lnum = SOURCING_LNUM;
9004
9005 // find_ex_command() will consider a variable name an expression, assuming
9006 // that something follows on the next line. Check that something actually
9007 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02009008 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02009009
Bram Moolenaarc3235272021-07-10 19:42:03 +02009010 if (compile_expr0(&p, cctx) == FAIL)
9011 return NULL;
9012
9013 if (name_only && lnum == SOURCING_LNUM)
9014 {
9015 semsg(_(e_expression_without_effect_str), arg);
9016 return NULL;
9017 }
9018
9019 // drop the result
9020 generate_instr_drop(cctx, ISN_DROP, 1);
9021
9022 return skipwhite(p);
9023}
9024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009025/*
9026 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009027 * compile "echomsg expr"
9028 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02009029 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01009030 * compile "execute expr"
9031 */
9032 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009033compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009034{
9035 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01009036 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009037 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009038 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009039 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009040 garray_T *stack = &cctx->ctx_type_stack;
9041 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009042
9043 for (;;)
9044 {
Bram Moolenaare4984292020-12-13 14:19:25 +01009045 if (ends_excmd2(prev, p))
9046 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009047 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009048 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009049 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009050
9051 if (cctx->ctx_skip != SKIP_YES)
9052 {
9053 // check for non-void type
9054 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
9055 if (type->tt_type == VAR_VOID)
9056 {
9057 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
9058 return NULL;
9059 }
9060 }
9061
Bram Moolenaarad39c092020-02-26 18:23:43 +01009062 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02009063 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009064 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009065 }
9066
Bram Moolenaare4984292020-12-13 14:19:25 +01009067 if (count > 0)
9068 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009069 long save_lnum = cctx->ctx_lnum;
9070
9071 // Use the line number where the command started.
9072 cctx->ctx_lnum = start_ctx_lnum;
9073
Bram Moolenaare4984292020-12-13 14:19:25 +01009074 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
9075 generate_ECHO(cctx, cmdidx == CMD_echo, count);
9076 else if (cmdidx == CMD_execute)
9077 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
9078 else if (cmdidx == CMD_echomsg)
9079 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02009080 else if (cmdidx == CMD_echoconsole)
9081 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01009082 else
9083 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009084
9085 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01009086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009087 return p;
9088}
9089
9090/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01009091 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01009092 * instruction to compute it and return OK.
9093 * Otherwise return FAIL, the caller must deal with any range.
9094 */
9095 static int
9096compile_variable_range(exarg_T *eap, cctx_T *cctx)
9097{
9098 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
9099 char_u *p = skipdigits(eap->cmd);
9100
9101 if (p == range_end)
9102 return FAIL;
9103 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
9104}
9105
9106/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009107 * :put r
9108 * :put ={expr}
9109 */
9110 static char_u *
9111compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
9112{
9113 char_u *line = arg;
9114 linenr_T lnum;
9115 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009116 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009117
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009118 eap->regname = *line;
9119
9120 if (eap->regname == '=')
9121 {
9122 char_u *p = line + 1;
9123
9124 if (compile_expr0(&p, cctx) == FAIL)
9125 return NULL;
9126 line = p;
9127 }
9128 else if (eap->regname != NUL)
9129 ++line;
9130
Bram Moolenaar08597872020-12-10 19:43:40 +01009131 if (compile_variable_range(eap, cctx) == OK)
9132 {
9133 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
9134 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009135 else
Bram Moolenaar08597872020-12-10 19:43:40 +01009136 {
9137 // Either no range or a number.
9138 // "errormsg" will not be set because the range is ADDR_LINES.
9139 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01009140 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01009141 return NULL;
9142 if (eap->addr_count == 0)
9143 lnum = -1;
9144 else
9145 lnum = eap->line2;
9146 if (above)
9147 --lnum;
9148 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009149
9150 generate_PUT(cctx, eap->regname, lnum);
9151 return line;
9152}
9153
9154/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009155 * A command that is not compiled, execute with legacy code.
9156 */
9157 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009158compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009159{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009160 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009161 char_u *p;
9162 int has_expr = FALSE;
9163 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009164 char_u *tofree = NULL;
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009165 char_u *cmd_arg = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009166
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009167 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009168 goto theend;
9169
Bram Moolenaare729ce22021-06-06 21:38:09 +02009170 // If there was a prececing command modifier, drop it and include it in the
9171 // EXEC command.
9172 if (cctx->ctx_has_cmdmod)
9173 {
9174 garray_T *instr = &cctx->ctx_instr;
9175 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9176
9177 if (isn->isn_type == ISN_CMDMOD)
9178 {
9179 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9180 ->cmod_filter_regmatch.regprog);
9181 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9182 --instr->ga_len;
9183 cctx->ctx_has_cmdmod = FALSE;
9184 }
9185 }
9186
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009187 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009188 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009189 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009190 int usefilter = FALSE;
9191
9192 has_expr = argt & (EX_XFILE | EX_EXPAND);
9193
9194 // If the command can be followed by a bar, find the bar and truncate
9195 // it, so that the following command can be compiled.
9196 // The '|' is overwritten with a NUL, it is put back below.
9197 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9198 && *eap->arg == '!')
9199 // :w !filter or :r !filter or :r! filter
9200 usefilter = TRUE;
9201 if ((argt & EX_TRLBAR) && !usefilter)
9202 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009203 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009204 separate_nextcmd(eap);
9205 if (eap->nextcmd != NULL)
9206 nextcmd = eap->nextcmd;
9207 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009208 else if (eap->cmdidx == CMD_wincmd)
9209 {
9210 p = eap->arg;
9211 if (*p != NUL)
9212 ++p;
9213 if (*p == 'g' || *p == Ctrl_G)
9214 ++p;
9215 p = skipwhite(p);
9216 if (*p == '|')
9217 {
9218 *p = NUL;
9219 nextcmd = p + 1;
9220 }
9221 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009222 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9223 {
9224 // If there is a trailing '{' read lines until the '}'
9225 p = eap->arg + STRLEN(eap->arg) - 1;
9226 while (p > eap->arg && VIM_ISWHITE(*p))
9227 --p;
9228 if (*p == '{')
9229 {
9230 exarg_T ea;
9231 int flags; // unused
9232 int start_lnum = SOURCING_LNUM;
9233
9234 CLEAR_FIELD(ea);
9235 ea.arg = eap->arg;
9236 fill_exarg_from_cctx(&ea, cctx);
9237 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9238 if (tofree != NULL)
9239 {
9240 *p = NUL;
9241 line = concat_str(line, tofree);
9242 if (line == NULL)
9243 goto theend;
9244 vim_free(tofree);
9245 tofree = line;
9246 SOURCING_LNUM = start_lnum;
9247 }
9248 }
9249 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009250 }
9251
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009252 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9253 {
9254 // expand filename in "syntax include [@group] filename"
9255 has_expr = TRUE;
9256 eap->arg = skipwhite(eap->arg + 7);
9257 if (*eap->arg == '@')
9258 eap->arg = skiptowhite(eap->arg);
9259 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009260
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009261 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9262 && STRLEN(eap->arg) > 4)
9263 {
9264 int delim = *eap->arg;
9265
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009266 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009267 if (*p == delim)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009268 cmd_arg = p + 1;
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009269 }
9270
Bram Moolenaarecac5912021-01-05 19:23:28 +01009271 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009272 cmd_arg = eap->arg;
9273
9274 if (cmd_arg != NULL)
Bram Moolenaarecac5912021-01-05 19:23:28 +01009275 {
Bram Moolenaarfad27422021-11-30 21:58:19 +00009276 exarg_T nea;
9277
9278 CLEAR_FIELD(nea);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009279 nea.cmd = cmd_arg;
Bram Moolenaarfad27422021-11-30 21:58:19 +00009280 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009281 if (nea.cmdidx < CMD_SIZE)
Bram Moolenaarfad27422021-11-30 21:58:19 +00009282 {
9283 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
9284 if (has_expr)
9285 eap->arg = skiptowhite(eap->arg);
9286 }
Bram Moolenaarecac5912021-01-05 19:23:28 +01009287 }
9288
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009289 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009290 {
9291 int count = 0;
9292 char_u *start = skipwhite(line);
9293
9294 // :cmd xxx`=expr1`yyy`=expr2`zzz
9295 // PUSHS ":cmd xxx"
9296 // eval expr1
9297 // PUSHS "yyy"
9298 // eval expr2
9299 // PUSHS "zzz"
9300 // EXECCONCAT 5
9301 for (;;)
9302 {
9303 if (p > start)
9304 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009305 char_u *val = vim_strnsave(start, p - start);
9306
9307 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009308 ++count;
9309 }
9310 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009311 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009312 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009313 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009314 ++count;
9315 p = skipwhite(p);
9316 if (*p != '`')
9317 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009318 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009319 return NULL;
9320 }
9321 start = p + 1;
9322
9323 p = (char_u *)strstr((char *)start, "`=");
9324 if (p == NULL)
9325 {
9326 if (*skipwhite(start) != NUL)
9327 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009328 char_u *val = vim_strsave(start);
9329
9330 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009331 ++count;
9332 }
9333 break;
9334 }
9335 }
9336 generate_EXECCONCAT(cctx, count);
9337 }
9338 else
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009339 generate_EXEC_copy(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009340
9341theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009342 if (*nextcmd != NUL)
9343 {
9344 // the parser expects a pointer to the bar, put it back
9345 --nextcmd;
9346 *nextcmd = '|';
9347 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009348 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009349
9350 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009351}
9352
Bram Moolenaar20677332021-06-06 17:02:53 +02009353/*
9354 * A script command with heredoc, e.g.
9355 * ruby << EOF
9356 * command
9357 * EOF
9358 * Has been turned into one long line with NL characters by
9359 * get_function_body():
9360 * ruby << EOF<NL> command<NL>EOF
9361 */
9362 static char_u *
9363compile_script(char_u *line, cctx_T *cctx)
9364{
9365 if (cctx->ctx_skip != SKIP_YES)
9366 {
9367 isn_T *isn;
9368
9369 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9370 return NULL;
9371 isn->isn_arg.string = vim_strsave(line);
9372 }
9373 return (char_u *)"";
9374}
9375
Bram Moolenaar8238f082021-04-20 21:10:48 +02009376
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009377/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009378 * :s/pat/repl/
9379 */
9380 static char_u *
9381compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9382{
9383 char_u *cmd = eap->arg;
9384 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9385
9386 if (expr != NULL)
9387 {
9388 int delimiter = *cmd++;
9389
9390 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009391 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009392 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9393 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009394 garray_T save_ga = cctx->ctx_instr;
9395 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009396 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009397 int trailing_error;
9398 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009399 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009400 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009401
9402 cmd += 3;
9403 end = skip_substitute(cmd, delimiter);
9404
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009405 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009406 // labels are correct.
9407 cctx->ctx_instr.ga_len = 0;
9408 cctx->ctx_instr.ga_maxlen = 0;
9409 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009410 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009411 if (end[-1] == NUL)
9412 end[-1] = delimiter;
9413 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009414 trailing_error = *cmd != delimiter && *cmd != NUL;
9415
Bram Moolenaar169502f2021-04-21 12:19:35 +02009416 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009417 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009418 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009419 if (trailing_error)
9420 semsg(_(e_trailing_arg), cmd);
9421 clear_instr_ga(&cctx->ctx_instr);
9422 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009423 return NULL;
9424 }
9425
Bram Moolenaar8238f082021-04-20 21:10:48 +02009426 // Move the generated instructions into the ISN_SUBSTITUTE
9427 // instructions, then restore the list of instructions before
9428 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009429 instr_count = cctx->ctx_instr.ga_len;
9430 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009431 instr[instr_count].isn_type = ISN_FINISH;
9432
9433 cctx->ctx_instr = save_ga;
9434 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9435 {
9436 int idx;
9437
9438 for (idx = 0; idx < instr_count; ++idx)
9439 delete_instr(instr + idx);
9440 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009441 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009442 }
9443 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9444 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009445
9446 // skip over flags
9447 if (*end == '&')
9448 ++end;
9449 while (ASCII_ISALPHA(*end) || *end == '#')
9450 ++end;
9451 return end;
9452 }
9453 }
9454
9455 return compile_exec(arg, eap, cctx);
9456}
9457
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009458 static char_u *
9459compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9460{
9461 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009462 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009463
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009464 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009465 {
9466 if (STRNCMP(arg, "END", 3) == 0)
9467 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009468 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009469 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009470 // First load the current variable value.
9471 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9472 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009473 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009474 }
9475
9476 // Gets the redirected text and put it on the stack, then store it
9477 // in the variable.
9478 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9479
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009480 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009481 generate_instr_drop(cctx, ISN_CONCAT, 1);
9482
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009483 if (lhs->lhs_has_index)
9484 {
9485 // Use the info in "lhs" to store the value at the index in the
9486 // list or dict.
9487 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9488 &t_string, cctx) == FAIL)
9489 return NULL;
9490 }
9491 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009492 return NULL;
9493
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009494 VIM_CLEAR(lhs->lhs_name);
9495 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009496 return arg + 3;
9497 }
9498 emsg(_(e_cannot_nest_redir));
9499 return NULL;
9500 }
9501
9502 if (arg[0] == '=' && arg[1] == '>')
9503 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009504 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009505
9506 // redirect to a variable is compiled
9507 arg += 2;
9508 if (*arg == '>')
9509 {
9510 ++arg;
9511 append = TRUE;
9512 }
9513 arg = skipwhite(arg);
9514
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009515 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009516 FALSE, FALSE, 1, cctx) == FAIL)
9517 return NULL;
9518 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009519 lhs->lhs_append = append;
9520 if (lhs->lhs_has_index)
9521 {
9522 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9523 if (lhs->lhs_whole == NULL)
9524 return NULL;
9525 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009526
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009527 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009528 }
9529
9530 // other redirects are handled like at script level
9531 return compile_exec(line, eap, cctx);
9532}
9533
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009534#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009535 static char_u *
9536compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9537{
9538 isn_T *isn;
9539 char_u *p;
9540
9541 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9542 if (isn == NULL)
9543 return NULL;
9544 isn->isn_arg.number = eap->cmdidx;
9545
9546 p = eap->arg;
9547 if (compile_expr0(&p, cctx) == FAIL)
9548 return NULL;
9549
9550 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9551 if (isn == NULL)
9552 return NULL;
9553 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9554 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9555 return NULL;
9556 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9557 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9558 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9559
9560 return p;
9561}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009562#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009563
Bram Moolenaar4c137212021-04-19 16:48:48 +02009564/*
Bram Moolenaar7b829262021-10-13 15:04:34 +01009565 * Check if the separator for a :global or :substitute command is OK.
9566 */
9567 int
9568check_global_and_subst(char_u *cmd, char_u *arg)
9569{
Bram Moolenaar7f320922021-10-13 15:28:28 +01009570 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
Bram Moolenaar7b829262021-10-13 15:04:34 +01009571 {
9572 semsg(_(e_separator_not_supported_str), arg);
9573 return FAIL;
9574 }
9575 if (VIM_ISWHITE(cmd[1]))
9576 {
9577 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
9578 return FAIL;
9579 }
9580 return OK;
9581}
9582
9583
9584/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009585 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009586 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009587 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009588 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009589add_def_function(ufunc_T *ufunc)
9590{
9591 dfunc_T *dfunc;
9592
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009593 if (def_functions.ga_len == 0)
9594 {
9595 // The first position is not used, so that a zero uf_dfunc_idx means it
9596 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009597 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009598 return FAIL;
9599 ++def_functions.ga_len;
9600 }
9601
Bram Moolenaar09689a02020-05-09 22:50:08 +02009602 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009603 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009604 return FAIL;
9605 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9606 CLEAR_POINTER(dfunc);
9607 dfunc->df_idx = def_functions.ga_len;
9608 ufunc->uf_dfunc_idx = dfunc->df_idx;
9609 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009610 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009611 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009612 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009613 ++def_functions.ga_len;
9614 return OK;
9615}
9616
9617/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009618 * After ex_function() has collected all the function lines: parse and compile
9619 * the lines into instructions.
9620 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009621 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9622 * the return statement (used for lambda). When uf_ret_type is already set
9623 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009624 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009625 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009626 * This can be used recursively through compile_lambda(), which may reallocate
9627 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009628 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009629 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009630 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009631compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009632 ufunc_T *ufunc,
9633 int check_return_type,
9634 compiletype_T compile_type,
9635 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009636{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009637 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009638 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009639 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009640 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009641 cctx_T cctx;
9642 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009643 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009644 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009645 int ret = FAIL;
9646 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009647 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009648 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009649 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009650 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009651#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009652 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009653#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009654 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009655
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009656 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009657 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009658 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009659 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009660 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9661 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009662 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009663
9664 switch (compile_type)
9665 {
9666 case CT_PROFILE:
9667#ifdef FEAT_PROFILE
9668 instr_dest = dfunc->df_instr_prof; break;
9669#endif
9670 case CT_NONE: instr_dest = dfunc->df_instr; break;
9671 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9672 }
9673 if (instr_dest != NULL)
9674 // Was compiled in this mode before: Free old instructions.
9675 delete_def_function_contents(dfunc, FALSE);
9676 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009677 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009678 else
9679 {
9680 if (add_def_function(ufunc) == FAIL)
9681 return FAIL;
9682 new_def_function = TRUE;
9683 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009684
Bram Moolenaar985116a2020-07-12 17:31:09 +02009685 ufunc->uf_def_status = UF_COMPILING;
9686
Bram Moolenaara80faa82020-04-12 19:37:17 +02009687 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009688
Bram Moolenaare99d4222021-06-13 14:01:26 +02009689 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009690 cctx.ctx_ufunc = ufunc;
9691 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009692 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009693 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9694 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9695 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9696 cctx.ctx_type_list = &ufunc->uf_type_list;
9697 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9698 instr = &cctx.ctx_instr;
9699
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009700 // Set the context to the function, it may be compiled when called from
9701 // another script. Set the script version to the most modern one.
9702 // The line number will be set in next_line_from_context().
9703 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009704 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9705
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009706 // Don't use the flag from ":legacy" here.
9707 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9708
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009709 // Make sure error messages are OK.
9710 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9711 if (do_estack_push)
9712 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009713 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009714
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009715 if (ufunc->uf_def_args.ga_len > 0)
9716 {
9717 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009718 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009719 int i;
9720 char_u *arg;
9721 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009722 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009723
9724 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009725 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009726 for (i = 0; i < count; ++i)
9727 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009728 garray_T *stack = &cctx.ctx_type_stack;
9729 type_T *val_type;
9730 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009731 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009732 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009733 int jump_instr_idx = instr->ga_len;
9734 isn_T *isn;
9735
9736 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9737 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9738 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009739
9740 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009741 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009742
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009743 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009744 r = compile_expr0(&arg, &cctx);
9745
Bram Moolenaar12bce952021-03-11 20:04:04 +01009746 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009747 goto erret;
9748
9749 // If no type specified use the type of the default value.
9750 // Otherwise check that the default value type matches the
9751 // specified type.
9752 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009753 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009754 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009755 {
9756 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009757 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009758 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009759 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009760 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009761 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009762
9763 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009764 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009765
9766 // set instruction index in JUMP_IF_ARG_SET to here
9767 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9768 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009769 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009770
9771 if (did_set_arg_type)
9772 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009773 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009774 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009775
9776 /*
9777 * Loop over all the lines of the function and generate instructions.
9778 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009779 for (;;)
9780 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009781 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009782 int starts_with_colon = FALSE;
9783 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009784 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009785
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009786 // Bail out on the first error to avoid a flood of errors and report
9787 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009788 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009789 goto erret;
9790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009791 if (line != NULL && *line == '|')
9792 // the line continues after a '|'
9793 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009794 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009795 && !(*line == '#' && (line == cctx.ctx_line_start
9796 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009797 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009798 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009799 goto erret;
9800 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009801 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9802 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009803 else
9804 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009805 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009806 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009807 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009808 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009809#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009810 if (cctx.ctx_skip != SKIP_YES)
9811 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009812#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009813 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009814 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009815 // Make a copy, splitting off nextcmd and removing trailing spaces
9816 // may change it.
9817 if (line != NULL)
9818 {
9819 line = vim_strsave(line);
9820 vim_free(line_to_free);
9821 line_to_free = line;
9822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009823 }
9824
Bram Moolenaara80faa82020-04-12 19:37:17 +02009825 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009826 ea.cmdlinep = &line;
9827 ea.cmd = skipwhite(line);
9828
Bram Moolenaarb2049902021-01-24 12:53:53 +01009829 if (*ea.cmd == '#')
9830 {
9831 // "#" starts a comment
9832 line = (char_u *)"";
9833 continue;
9834 }
9835
Bram Moolenaarf002a412021-01-24 13:34:18 +01009836#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009837 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9838 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009839 {
9840 may_generate_prof_end(&cctx, prof_lnum);
9841
9842 prof_lnum = cctx.ctx_lnum;
9843 generate_instr(&cctx, ISN_PROF_START);
9844 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009845#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009846 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9847 && cctx.ctx_skip != SKIP_YES)
9848 {
9849 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009850 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009851 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009852 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009853
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009854 // Some things can be recognized by the first character.
9855 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009856 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009857 case '}':
9858 {
9859 // "}" ends a block scope
9860 scopetype_T stype = cctx.ctx_scope == NULL
9861 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009862
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009863 if (stype == BLOCK_SCOPE)
9864 {
9865 compile_endblock(&cctx);
9866 line = ea.cmd;
9867 }
9868 else
9869 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009870 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009871 goto erret;
9872 }
9873 if (line != NULL)
9874 line = skipwhite(ea.cmd + 1);
9875 continue;
9876 }
9877
9878 case '{':
9879 // "{" starts a block scope
9880 // "{'a': 1}->func() is something else
9881 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9882 {
9883 line = compile_block(ea.cmd, &cctx);
9884 continue;
9885 }
9886 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009887 }
9888
9889 /*
9890 * COMMAND MODIFIERS
9891 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009892 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009893 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9894 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009895 {
9896 if (errormsg != NULL)
9897 goto erret;
9898 // empty line or comment
9899 line = (char_u *)"";
9900 continue;
9901 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009902 generate_cmdmods(&cctx, &local_cmdmod);
9903 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009904
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009905 // Check if there was a colon after the last command modifier or before
9906 // the current position.
9907 for (p = ea.cmd; p >= line; --p)
9908 {
9909 if (*p == ':')
9910 starts_with_colon = TRUE;
9911 if (p < ea.cmd && !VIM_ISWHITE(*p))
9912 break;
9913 }
9914
Bram Moolenaarce024c32021-06-26 13:00:49 +02009915 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009916 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009917 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009918 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009919 if (checkforcmd(&ea.cmd, "call", 3))
9920 {
9921 if (*ea.cmd == '(')
9922 // not for "call()"
9923 ea.cmd = p;
9924 else
9925 ea.cmd = skipwhite(ea.cmd);
9926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009927
Bram Moolenaarce024c32021-06-26 13:00:49 +02009928 if (!starts_with_colon)
9929 {
9930 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009931
Bram Moolenaarce024c32021-06-26 13:00:49 +02009932 // Check for assignment after command modifiers.
9933 assign = may_compile_assignment(&ea, &line, &cctx);
9934 if (assign == OK)
9935 goto nextline;
9936 if (assign == FAIL)
9937 goto erret;
9938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009939 }
9940
9941 /*
9942 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009943 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009944 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009945 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009946 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009947 cmd = ea.cmd;
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009948 if ((*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009949 && (starts_with_colon || !(*cmd == '\''
9950 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009951 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009952 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009953 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009954 {
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009955 if (!starts_with_colon
9956 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009957 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009958 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009959 goto erret;
9960 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009961 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009962 if (ends_excmd2(line, ea.cmd))
9963 {
9964 // A range without a command: jump to the line.
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009965 generate_EXEC(&cctx, ISN_EXECRANGE,
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009966 vim_strnsave(cmd, ea.cmd - cmd));
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009967 line = ea.cmd;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009968 goto nextline;
9969 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009970 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009971 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009972 p = find_ex_command(&ea, NULL,
9973 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009974 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009975
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009976 if (p == NULL)
9977 {
9978 if (cctx.ctx_skip != SKIP_YES)
9979 emsg(_(e_ambiguous_use_of_user_defined_command));
9980 goto erret;
9981 }
9982
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009983 // When using ":legacy cmd" always use compile_exec().
9984 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009985 {
9986 char_u *start = ea.cmd;
9987
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009988 switch (ea.cmdidx)
9989 {
9990 case CMD_if:
9991 case CMD_elseif:
9992 case CMD_else:
9993 case CMD_endif:
9994 case CMD_for:
9995 case CMD_endfor:
9996 case CMD_continue:
9997 case CMD_break:
9998 case CMD_while:
9999 case CMD_endwhile:
10000 case CMD_try:
10001 case CMD_catch:
10002 case CMD_finally:
10003 case CMD_endtry:
10004 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
10005 goto erret;
10006 default: break;
10007 }
10008
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010009 // ":legacy return expr" needs to be handled differently.
10010 if (checkforcmd(&start, "return", 4))
10011 ea.cmdidx = CMD_return;
10012 else
10013 ea.cmdidx = CMD_legacy;
10014 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +020010015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010016 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
10017 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +020010018 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010019 {
10020 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +010010021 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010022 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +020010023 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010024 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +010010025 // CMD_var cannot happen, compile_assignment() above would be
10026 // used. Most likely an assignment to a non-existing variable.
10027 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010028 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010029 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010030 }
10031
Bram Moolenaar3988f642020-08-27 22:43:03 +020010032 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010033 && ea.cmdidx != CMD_elseif
10034 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +020010035 && ea.cmdidx != CMD_endif
10036 && ea.cmdidx != CMD_endfor
10037 && ea.cmdidx != CMD_endwhile
10038 && ea.cmdidx != CMD_catch
10039 && ea.cmdidx != CMD_finally
10040 && ea.cmdidx != CMD_endtry)
10041 {
Bram Moolenaar3988f642020-08-27 22:43:03 +020010042 emsg(_(e_unreachable_code_after_return));
10043 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +020010044 }
10045
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010046 p = skipwhite(p);
10047 if (ea.cmdidx != CMD_SIZE
10048 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
10049 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +020010050 if (ea.cmdidx >= 0)
10051 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010052 if ((ea.argt & EX_BANG) && *p == '!')
10053 {
10054 ea.forceit = TRUE;
10055 p = skipwhite(p + 1);
10056 }
10057 }
10058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010059 switch (ea.cmdidx)
10060 {
10061 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +000010062 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +020010063 ea.arg = p;
10064 line = compile_nested_function(&ea, &cctx);
10065 break;
10066
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010067 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010068 line = compile_return(p, check_return_type,
10069 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010070 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010071 break;
10072
10073 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +020010074 emsg(_(e_cannot_use_let_in_vim9_script));
10075 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +020010076 case CMD_var:
10077 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010078 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +020010079 case CMD_increment:
10080 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010081 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +020010082 if (line == p)
10083 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010084 break;
10085
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010086 case CMD_unlet:
10087 case CMD_unlockvar:
10088 case CMD_lockvar:
10089 line = compile_unletlock(p, &ea, &cctx);
10090 break;
10091
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010092 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +020010093 emsg(_(e_import_can_only_be_used_in_script));
10094 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010095 break;
10096
10097 case CMD_if:
10098 line = compile_if(p, &cctx);
10099 break;
10100 case CMD_elseif:
10101 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010102 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010103 break;
10104 case CMD_else:
10105 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010106 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010107 break;
10108 case CMD_endif:
10109 line = compile_endif(p, &cctx);
10110 break;
10111
10112 case CMD_while:
10113 line = compile_while(p, &cctx);
10114 break;
10115 case CMD_endwhile:
10116 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010117 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010118 break;
10119
10120 case CMD_for:
10121 line = compile_for(p, &cctx);
10122 break;
10123 case CMD_endfor:
10124 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010125 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010126 break;
10127 case CMD_continue:
10128 line = compile_continue(p, &cctx);
10129 break;
10130 case CMD_break:
10131 line = compile_break(p, &cctx);
10132 break;
10133
10134 case CMD_try:
10135 line = compile_try(p, &cctx);
10136 break;
10137 case CMD_catch:
10138 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010139 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010140 break;
10141 case CMD_finally:
10142 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010143 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010144 break;
10145 case CMD_endtry:
10146 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010147 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010148 break;
10149 case CMD_throw:
10150 line = compile_throw(p, &cctx);
10151 break;
10152
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010153 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +020010154 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010155 break;
10156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010157 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010158 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +010010159 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010160 case CMD_echomsg:
10161 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010162 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010163 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +010010164 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010165
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010166 case CMD_put:
10167 ea.cmd = cmd;
10168 line = compile_put(p, &ea, &cctx);
10169 break;
10170
Bram Moolenaar4c137212021-04-19 16:48:48 +020010171 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +010010172 if (check_global_and_subst(ea.cmd, p) == FAIL)
10173 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +020010174 if (cctx.ctx_skip == SKIP_YES)
10175 line = (char_u *)"";
10176 else
10177 {
10178 ea.arg = p;
10179 line = compile_substitute(line, &ea, &cctx);
10180 }
10181 break;
10182
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010183 case CMD_redir:
10184 ea.arg = p;
10185 line = compile_redir(line, &ea, &cctx);
10186 break;
10187
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010188 case CMD_cexpr:
10189 case CMD_lexpr:
10190 case CMD_caddexpr:
10191 case CMD_laddexpr:
10192 case CMD_cgetexpr:
10193 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010194#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010195 ea.arg = p;
10196 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010197#else
10198 ex_ni(&ea);
10199 line = NULL;
10200#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010201 break;
10202
Bram Moolenaarae616492020-07-28 20:07:27 +020010203 case CMD_append:
10204 case CMD_change:
10205 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010206 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010207 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010208 case CMD_xit:
10209 not_in_vim9(&ea);
10210 goto erret;
10211
Bram Moolenaar002262f2020-07-08 17:47:57 +020010212 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010213 if (cctx.ctx_skip != SKIP_YES)
10214 {
10215 semsg(_(e_invalid_command_str), ea.cmd);
10216 goto erret;
10217 }
10218 // We don't check for a next command here.
10219 line = (char_u *)"";
10220 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010221
Bram Moolenaar20677332021-06-06 17:02:53 +020010222 case CMD_lua:
10223 case CMD_mzscheme:
10224 case CMD_perl:
10225 case CMD_py3:
10226 case CMD_python3:
10227 case CMD_python:
10228 case CMD_pythonx:
10229 case CMD_ruby:
10230 case CMD_tcl:
10231 ea.arg = p;
10232 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010233 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010234 else
10235 // heredoc lines have been concatenated with NL
10236 // characters in get_function_body()
10237 line = compile_script(line, &cctx);
10238 break;
10239
Bram Moolenaar7b829262021-10-13 15:04:34 +010010240 case CMD_global:
10241 if (check_global_and_subst(ea.cmd, p) == FAIL)
10242 goto erret;
10243 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +020010244 default:
10245 // Not recognized, execute with do_cmdline_cmd().
10246 ea.arg = p;
10247 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010248 break;
10249 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010250nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010251 if (line == NULL)
10252 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010253 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010254
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010255 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010256 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010258 if (cctx.ctx_type_stack.ga_len < 0)
10259 {
10260 iemsg("Type stack underflow");
10261 goto erret;
10262 }
10263 }
10264
10265 if (cctx.ctx_scope != NULL)
10266 {
10267 if (cctx.ctx_scope->se_type == IF_SCOPE)
10268 emsg(_(e_endif));
10269 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10270 emsg(_(e_endwhile));
10271 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10272 emsg(_(e_endfor));
10273 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010274 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010275 goto erret;
10276 }
10277
Bram Moolenaarefd88552020-06-18 20:50:10 +020010278 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010279 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010280 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10281 ufunc->uf_ret_type = &t_void;
10282 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010283 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010284 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010285 goto erret;
10286 }
10287
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010288 // Return void if there is no return at the end.
10289 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010290 }
10291
Bram Moolenaar599410c2021-04-10 14:03:43 +020010292 // When compiled with ":silent!" and there was an error don't consider the
10293 // function compiled.
10294 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010295 {
10296 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10297 + ufunc->uf_dfunc_idx;
10298 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010299 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010300#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010301 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010302 {
10303 dfunc->df_instr_prof = instr->ga_data;
10304 dfunc->df_instr_prof_count = instr->ga_len;
10305 }
10306 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010307#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010308 if (cctx.ctx_compile_type == CT_DEBUG)
10309 {
10310 dfunc->df_instr_debug = instr->ga_data;
10311 dfunc->df_instr_debug_count = instr->ga_len;
10312 }
10313 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010314 {
10315 dfunc->df_instr = instr->ga_data;
10316 dfunc->df_instr_count = instr->ga_len;
10317 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010318 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010319 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010320 if (cctx.ctx_outer_used)
10321 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010322 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010323 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010324
10325 ret = OK;
10326
10327erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010328 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010329 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010330 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10331 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010332
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010333 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010334 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010335 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010336 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010337
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010338 // If using the last entry in the table and it was added above, we
10339 // might as well remove it.
10340 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010341 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010342 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010343 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010344 ufunc->uf_dfunc_idx = 0;
10345 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010346 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010347
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010348 while (cctx.ctx_scope != NULL)
10349 drop_scope(&cctx);
10350
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010351 if (errormsg != NULL)
10352 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010353 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010354 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010355 }
10356
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010357 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10358 {
10359 if (ret == OK)
10360 {
10361 emsg(_(e_missing_redir_end));
10362 ret = FAIL;
10363 }
10364 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010365 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010366 }
10367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010368 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010369 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010370 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010371 if (do_estack_push)
10372 estack_pop();
10373
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010374 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010375 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010376 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010377 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010378 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010379}
10380
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010381 void
10382set_function_type(ufunc_T *ufunc)
10383{
10384 int varargs = ufunc->uf_va_name != NULL;
10385 int argcount = ufunc->uf_args.ga_len;
10386
10387 // Create a type for the function, with the return type and any
10388 // argument types.
10389 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10390 // The type is included in "tt_args".
10391 if (argcount > 0 || varargs)
10392 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010393 if (ufunc->uf_type_list.ga_itemsize == 0)
10394 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010395 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10396 argcount, &ufunc->uf_type_list);
10397 // Add argument types to the function type.
10398 if (func_type_add_arg_types(ufunc->uf_func_type,
10399 argcount + varargs,
10400 &ufunc->uf_type_list) == FAIL)
10401 return;
10402 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10403 ufunc->uf_func_type->tt_min_argcount =
10404 argcount - ufunc->uf_def_args.ga_len;
10405 if (ufunc->uf_arg_types == NULL)
10406 {
10407 int i;
10408
10409 // lambda does not have argument types.
10410 for (i = 0; i < argcount; ++i)
10411 ufunc->uf_func_type->tt_args[i] = &t_any;
10412 }
10413 else
10414 mch_memmove(ufunc->uf_func_type->tt_args,
10415 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10416 if (varargs)
10417 {
10418 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010419 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010420 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10421 }
10422 }
10423 else
10424 // No arguments, can use a predefined type.
10425 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10426 argcount, &ufunc->uf_type_list);
10427}
10428
10429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010430/*
10431 * Delete an instruction, free what it contains.
10432 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010433 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010434delete_instr(isn_T *isn)
10435{
10436 switch (isn->isn_type)
10437 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010438 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010439 case ISN_EXEC:
Bram Moolenaare4eed8c2021-12-01 15:22:56 +000010440 case ISN_EXECRANGE:
Bram Moolenaar20677332021-06-06 17:02:53 +020010441 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010442 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010443 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010444 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010445 case ISN_LOADENV:
10446 case ISN_LOADG:
10447 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010448 case ISN_LOADT:
10449 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010450 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010451 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010452 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010453 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010454 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010455 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010456 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010457 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010458 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010459 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010460 case ISN_STOREW:
10461 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010462 vim_free(isn->isn_arg.string);
10463 break;
10464
Bram Moolenaar4c137212021-04-19 16:48:48 +020010465 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010466 {
10467 int idx;
10468 isn_T *list = isn->isn_arg.subs.subs_instr;
10469
10470 vim_free(isn->isn_arg.subs.subs_cmd);
10471 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10472 delete_instr(list + idx);
10473 vim_free(list);
10474 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010475 break;
10476
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010477 case ISN_INSTR:
10478 {
10479 int idx;
10480 isn_T *list = isn->isn_arg.instr;
10481
10482 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10483 delete_instr(list + idx);
10484 vim_free(list);
10485 }
10486 break;
10487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010488 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010489 case ISN_STORES:
10490 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010491 break;
10492
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010493 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010494 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010495 vim_free(isn->isn_arg.unlet.ul_name);
10496 break;
10497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010498 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +000010499 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010500 vim_free(isn->isn_arg.storeopt.so_name);
10501 break;
10502
10503 case ISN_PUSHBLOB: // push blob isn_arg.blob
10504 blob_unref(isn->isn_arg.blob);
10505 break;
10506
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010507 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010508#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010509 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010510#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010511 break;
10512
10513 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010514#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010515 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010516#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010517 break;
10518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010519 case ISN_UCALL:
10520 vim_free(isn->isn_arg.ufunc.cuf_name);
10521 break;
10522
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010523 case ISN_FUNCREF:
10524 {
Bram Moolenaar38453522021-11-28 22:00:12 +000010525 if (isn->isn_arg.funcref.fr_func_name == NULL)
10526 {
10527 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10528 + isn->isn_arg.funcref.fr_dfunc_idx;
10529 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010530
Bram Moolenaar38453522021-11-28 22:00:12 +000010531 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10532 func_ptr_unref(ufunc);
10533 }
10534 else
10535 {
10536 char_u *name = isn->isn_arg.funcref.fr_func_name;
10537
10538 if (name != NULL)
10539 func_unref(name);
10540 vim_free(isn->isn_arg.funcref.fr_func_name);
10541 }
Bram Moolenaara05e5242020-09-19 18:19:19 +020010542 }
10543 break;
10544
10545 case ISN_DCALL:
10546 {
10547 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10548 + isn->isn_arg.dfunc.cdf_idx;
10549
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010550 if (dfunc->df_ufunc != NULL
10551 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010552 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010553 }
10554 break;
10555
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010556 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010557 {
10558 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10559 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10560
10561 if (ufunc != NULL)
10562 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010563 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010564 func_ptr_unref(ufunc);
10565 }
10566
10567 vim_free(lambda);
10568 vim_free(isn->isn_arg.newfunc.nf_global);
10569 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010570 break;
10571
Bram Moolenaar5e654232020-09-16 15:22:00 +020010572 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010573 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010574 free_type(isn->isn_arg.type.ct_type);
10575 break;
10576
Bram Moolenaar02194d22020-10-24 23:08:38 +020010577 case ISN_CMDMOD:
10578 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10579 ->cmod_filter_regmatch.regprog);
10580 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10581 break;
10582
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010583 case ISN_LOADSCRIPT:
10584 case ISN_STORESCRIPT:
10585 vim_free(isn->isn_arg.script.scriptref);
10586 break;
10587
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010588 case ISN_TRY:
10589 vim_free(isn->isn_arg.try.try_ref);
10590 break;
10591
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010592 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010593 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010594 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10595 break;
10596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010597 case ISN_2BOOL:
10598 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010599 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010600 case ISN_ADDBLOB:
10601 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010602 case ISN_ANYINDEX:
10603 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010604 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010605 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010606 case ISN_BLOBINDEX:
10607 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010608 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010609 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010610 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010611 case ISN_CHECKNR:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010612 case ISN_CLEARDICT:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010613 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010614 case ISN_COMPAREANY:
10615 case ISN_COMPAREBLOB:
10616 case ISN_COMPAREBOOL:
10617 case ISN_COMPAREDICT:
10618 case ISN_COMPAREFLOAT:
10619 case ISN_COMPAREFUNC:
10620 case ISN_COMPARELIST:
10621 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010622 case ISN_COMPARESPECIAL:
10623 case ISN_COMPARESTRING:
10624 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010625 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010626 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010627 case ISN_DROP:
10628 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010629 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010630 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010631 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010632 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010633 case ISN_EXECCONCAT:
10634 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010635 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010636 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010637 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010638 case ISN_GETITEM:
10639 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010640 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010641 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010642 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010643 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010644 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010645 case ISN_LOADBDICT:
10646 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010647 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010648 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010649 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010650 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010651 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010652 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010653 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010654 case ISN_NEGATENR:
10655 case ISN_NEWDICT:
10656 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010657 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010658 case ISN_OPFLOAT:
10659 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010660 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010661 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010662 case ISN_PROF_END:
10663 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010664 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010665 case ISN_PUSHF:
10666 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010667 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010668 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010669 case ISN_REDIREND:
10670 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010671 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010672 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010673 case ISN_SHUFFLE:
10674 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010675 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010676 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010677 case ISN_STORENR:
10678 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010679 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010680 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010681 case ISN_STOREV:
10682 case ISN_STRINDEX:
10683 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010684 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010685 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010686 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010687 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010688 case ISN_UNPACK:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010689 case ISN_USEDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010690 // nothing allocated
10691 break;
10692 }
10693}
10694
10695/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010696 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010697 */
10698 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010699delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010700{
10701 int idx;
10702
10703 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010704 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010705
10706 if (dfunc->df_instr != NULL)
10707 {
10708 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10709 delete_instr(dfunc->df_instr + idx);
10710 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010711 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010712 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010713 if (dfunc->df_instr_debug != NULL)
10714 {
10715 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10716 delete_instr(dfunc->df_instr_debug + idx);
10717 VIM_CLEAR(dfunc->df_instr_debug);
10718 dfunc->df_instr_debug = NULL;
10719 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010720#ifdef FEAT_PROFILE
10721 if (dfunc->df_instr_prof != NULL)
10722 {
10723 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10724 delete_instr(dfunc->df_instr_prof + idx);
10725 VIM_CLEAR(dfunc->df_instr_prof);
10726 dfunc->df_instr_prof = NULL;
10727 }
10728#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010729
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010730 if (mark_deleted)
10731 dfunc->df_deleted = TRUE;
10732 if (dfunc->df_ufunc != NULL)
10733 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010734}
10735
10736/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010737 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010738 * function, unless another user function still uses it.
10739 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010740 */
10741 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010742unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010743{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010744 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010745 {
10746 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10747 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010748
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010749 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010750 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010751 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010752 ufunc->uf_dfunc_idx = 0;
10753 if (dfunc->df_ufunc == ufunc)
10754 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010755 }
10756}
10757
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010758/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010759 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010760 */
10761 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010762link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010763{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010764 if (ufunc->uf_dfunc_idx > 0)
10765 {
10766 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10767 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010768
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010769 ++dfunc->df_refcount;
10770 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010771}
10772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010773#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010774/*
10775 * Free all functions defined with ":def".
10776 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010777 void
10778free_def_functions(void)
10779{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010780 int idx;
10781
10782 for (idx = 0; idx < def_functions.ga_len; ++idx)
10783 {
10784 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10785
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010786 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010787 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010788 }
10789
10790 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010791}
10792#endif
10793
10794
10795#endif // FEAT_EVAL