blob: 2b27f514cddfe77fa216461bb11643418af9162f [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 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00003046 switch (vartype)
3047 {
3048 case VAR_FUNC:
3049 case VAR_PARTIAL:
3050 emsg(_(e_cannot_index_a_funcref));
3051 break;
3052 case VAR_BOOL:
3053 case VAR_SPECIAL:
3054 case VAR_JOB:
3055 case VAR_CHANNEL:
3056 case VAR_INSTR:
3057 case VAR_UNKNOWN:
3058 case VAR_ANY:
3059 case VAR_VOID:
3060 emsg(_(e_cannot_index_special_variable));
3061 break;
3062 default:
3063 emsg(_(e_string_list_dict_or_blob_required));
3064 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02003065 return FAIL;
3066 }
3067 return OK;
3068}
3069
3070/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003071 * Generate an instruction to load script-local variable "name", without the
3072 * leading "s:".
3073 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 */
3075 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003076compile_load_scriptvar(
3077 cctx_T *cctx,
3078 char_u *name, // variable NUL terminated
3079 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003080 char_u **end, // end of variable
3081 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082{
Bram Moolenaare3d46852020-08-29 13:39:17 +02003083 scriptitem_T *si;
3084 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 imported_T *import;
3086
Bram Moolenaare3d46852020-08-29 13:39:17 +02003087 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3088 return FAIL;
3089 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003090 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003091 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003093 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003094 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3095 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 }
3097 if (idx >= 0)
3098 {
3099 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3100
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003101 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 current_sctx.sc_sid, idx, sv->sv_type);
3103 return OK;
3104 }
3105
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003106 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 if (import != NULL)
3108 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003109 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003110 {
3111 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003112 char_u *exp_name;
3113 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003114 ufunc_T *ufunc;
3115 type_T *type;
3116
3117 // Used "import * as Name", need to lookup the member.
3118 if (*p != '.')
3119 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003120 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003121 return FAIL;
3122 }
3123 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003124 if (VIM_ISWHITE(*p))
3125 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003126 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003127 return FAIL;
3128 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003129
Bram Moolenaar1c991142020-07-04 13:15:31 +02003130 // isolate one name
3131 exp_name = p;
3132 while (eval_isnamec(*p))
3133 ++p;
3134 cc = *p;
3135 *p = NUL;
3136
Bram Moolenaaredba7072021-03-13 21:14:18 +01003137 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3138 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003139 *p = cc;
3140 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003141 *end = p;
3142
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003143 if (idx < 0)
3144 {
3145 if (*p == '(' && ufunc != NULL)
3146 {
3147 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3148 return OK;
3149 }
3150 return FAIL;
3151 }
3152
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003153 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3154 import->imp_sid,
3155 idx,
3156 type);
3157 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003158 else if (import->imp_funcname != NULL)
3159 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003160 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003161 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3162 import->imp_sid,
3163 import->imp_var_vals_idx,
3164 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 return OK;
3166 }
3167
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003168 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003169 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 return FAIL;
3171}
3172
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003173 static int
3174generate_funcref(cctx_T *cctx, char_u *name)
3175{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003176 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003177
3178 if (ufunc == NULL)
3179 return FAIL;
3180
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003181 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003182 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3183 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003184 == FAIL)
3185 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003186 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003187}
3188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189/*
3190 * Compile a variable name into a load instruction.
3191 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003192 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 * When "error" is FALSE do not give an error when not found.
3194 */
3195 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003196compile_load(
3197 char_u **arg,
3198 char_u *end_arg,
3199 cctx_T *cctx,
3200 int is_expr,
3201 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202{
3203 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003204 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003205 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003207 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208
3209 if (*(*arg + 1) == ':')
3210 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003211 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003212 {
3213 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214
Bram Moolenaarfa596382021-04-07 21:58:16 +02003215 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003216 switch (**arg)
3217 {
3218 case 'g': isn_type = ISN_LOADGDICT; break;
3219 case 'w': isn_type = ISN_LOADWDICT; break;
3220 case 't': isn_type = ISN_LOADTDICT; break;
3221 case 'b': isn_type = ISN_LOADBDICT; break;
3222 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003223 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003224 goto theend;
3225 }
3226 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3227 goto theend;
3228 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003229 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 else
3231 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003232 isntype_T isn_type = ISN_DROP;
3233
Bram Moolenaarfa596382021-04-07 21:58:16 +02003234 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003235 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3236 if (name == NULL)
3237 return FAIL;
3238
3239 switch (**arg)
3240 {
3241 case 'v': res = generate_LOADV(cctx, name, error);
3242 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003243 case 's': if (is_expr && ASCII_ISUPPER(*name)
3244 && find_func(name, FALSE, cctx) != NULL)
3245 res = generate_funcref(cctx, name);
3246 else
3247 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003248 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003249 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003250 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003251 {
3252 if (is_expr && ASCII_ISUPPER(*name)
3253 && find_func(name, FALSE, cctx) != NULL)
3254 res = generate_funcref(cctx, name);
3255 else
3256 isn_type = ISN_LOADG;
3257 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003258 else
3259 {
3260 isn_type = ISN_LOADAUTO;
3261 vim_free(name);
3262 name = vim_strnsave(*arg, end - *arg);
3263 if (name == NULL)
3264 return FAIL;
3265 }
3266 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003267 case 'w': isn_type = ISN_LOADW; break;
3268 case 't': isn_type = ISN_LOADT; break;
3269 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003270 default: // cannot happen, just in case
3271 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003272 goto theend;
3273 }
3274 if (isn_type != ISN_DROP)
3275 {
3276 // Global, Buffer-local, Window-local and Tabpage-local
3277 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003278 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003279 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 }
3282 }
3283 else
3284 {
3285 size_t len = end - *arg;
3286 int idx;
3287 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003288 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289
3290 name = vim_strnsave(*arg, end - *arg);
3291 if (name == NULL)
3292 return FAIL;
3293
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003294 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3295 {
3296 script_autoload(name, FALSE);
3297 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3298 }
3299 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3300 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003302 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003303 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 }
3305 else
3306 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003307 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003308
Bram Moolenaar709664c2020-12-12 14:33:41 +01003309 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003311 type = lvar.lv_type;
3312 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003313 if (lvar.lv_from_outer != 0)
3314 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003315 else
3316 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 }
3318 else
3319 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003320 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003321 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003322 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003323 || find_imported(name, 0, cctx) != NULL)
3324 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003325
Bram Moolenaar0f769812020-09-12 18:32:34 +02003326 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003327 // uppercase letter it can be a user defined function.
3328 // generate_funcref() will fail if the function can't be found.
3329 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003330 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 }
3332 }
3333 if (gen_load)
3334 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003335 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003336 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003337 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003338 cctx->ctx_outer_used = TRUE;
3339 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 }
3341
3342 *arg = end;
3343
3344theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003345 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003346 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 vim_free(name);
3348 return res;
3349}
3350
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003351 static void
3352clear_instr_ga(garray_T *gap)
3353{
3354 int idx;
3355
3356 for (idx = 0; idx < gap->ga_len; ++idx)
3357 delete_instr(((isn_T *)gap->ga_data) + idx);
3358 ga_clear(gap);
3359}
3360
3361/*
3362 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3363 * Returns FAIL if compilation fails.
3364 */
3365 static int
3366compile_string(isn_T *isn, cctx_T *cctx)
3367{
3368 char_u *s = isn->isn_arg.string;
3369 garray_T save_ga = cctx->ctx_instr;
3370 int expr_res;
3371 int trailing_error;
3372 int instr_count;
3373 isn_T *instr = NULL;
3374
Bram Moolenaarcd268012021-07-22 19:11:08 +02003375 // Remove the string type from the stack.
3376 --cctx->ctx_type_stack.ga_len;
3377
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003378 // Temporarily reset the list of instructions so that the jump labels are
3379 // correct.
3380 cctx->ctx_instr.ga_len = 0;
3381 cctx->ctx_instr.ga_maxlen = 0;
3382 cctx->ctx_instr.ga_data = NULL;
3383 expr_res = compile_expr0(&s, cctx);
3384 s = skipwhite(s);
3385 trailing_error = *s != NUL;
3386
Bram Moolenaarff652882021-05-16 15:24:49 +02003387 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003388 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003389 {
3390 if (trailing_error)
3391 semsg(_(e_trailing_arg), s);
3392 clear_instr_ga(&cctx->ctx_instr);
3393 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003394 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003395 return FAIL;
3396 }
3397
3398 // Move the generated instructions into the ISN_INSTR instruction, then
3399 // restore the list of instructions.
3400 instr_count = cctx->ctx_instr.ga_len;
3401 instr = cctx->ctx_instr.ga_data;
3402 instr[instr_count].isn_type = ISN_FINISH;
3403
3404 cctx->ctx_instr = save_ga;
3405 vim_free(isn->isn_arg.string);
3406 isn->isn_type = ISN_INSTR;
3407 isn->isn_arg.instr = instr;
3408 return OK;
3409}
3410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411/*
3412 * Compile the argument expressions.
3413 * "arg" points to just after the "(" and is advanced to after the ")"
3414 */
3415 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003416compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003418 char_u *p = *arg;
3419 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003420 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003421 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422
Bram Moolenaare6085c52020-04-12 20:19:16 +02003423 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003425 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3426 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003427 if (*p == ')')
3428 {
3429 *arg = p + 1;
3430 return OK;
3431 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003432 if (must_end)
3433 {
3434 semsg(_(e_missing_comma_before_argument_str), p);
3435 return FAIL;
3436 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003437
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003438 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003439 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 return FAIL;
3441 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003442
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003443 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003444 && cctx->ctx_instr.ga_len == instr_count + 1)
3445 {
3446 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3447
3448 // {skip} argument of searchpair() can be compiled if not empty
3449 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3450 compile_string(isn, cctx);
3451 }
3452
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003453 if (*p != ',' && *skipwhite(p) == ',')
3454 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003455 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003456 p = skipwhite(p);
3457 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003459 {
3460 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003461 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003462 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003463 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003464 else
3465 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003466 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003467 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003469failret:
Bram Moolenaare1242042021-12-16 20:56:57 +00003470 emsg(_(e_missing_closing_paren));
Bram Moolenaare6085c52020-04-12 20:19:16 +02003471 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472}
3473
3474/*
3475 * Compile a function call: name(arg1, arg2)
3476 * "arg" points to "name", "arg + varlen" to the "(".
3477 * "argcount_init" is 1 for "value->method()"
3478 * Instructions:
3479 * EVAL arg1
3480 * EVAL arg2
3481 * BCALL / DCALL / UCALL
3482 */
3483 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003484compile_call(
3485 char_u **arg,
3486 size_t varlen,
3487 cctx_T *cctx,
3488 ppconst_T *ppconst,
3489 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490{
3491 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003492 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 int argcount = argcount_init;
3494 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003495 char_u fname_buf[FLEN_FIXED + 1];
3496 char_u *tofree = NULL;
3497 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003498 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003499 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003500 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003501 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003503 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003504 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003505 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003506 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003507 {
3508 char_u *s = skipwhite(*arg + varlen + 1);
3509 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003510 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003511
3512 argvars[0].v_type = VAR_UNKNOWN;
3513 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003514 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003515 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003516 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003517 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003518 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003519 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003520 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003521 {
3522 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3523
3524 *arg = s + 1;
3525 argvars[1].v_type = VAR_UNKNOWN;
3526 tv->v_type = VAR_NUMBER;
3527 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003528 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003529 f_has(argvars, tv);
3530 else
3531 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003532 clear_tv(&argvars[0]);
3533 ++ppconst->pp_used;
3534 return OK;
3535 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003536 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003537 if (!is_has)
3538 {
3539 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3540 return FAIL;
3541 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003542 }
3543
3544 if (generate_ppconst(cctx, ppconst) == FAIL)
3545 return FAIL;
3546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 if (varlen >= sizeof(namebuf))
3548 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003549 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 return FAIL;
3551 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003552 vim_strncpy(namebuf, *arg, varlen);
3553 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003555 // We handle the "skip" argument of searchpair() and searchpairpos()
3556 // differently.
3557 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3558 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3559 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3560 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003563 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003564 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565
Bram Moolenaar03290b82020-12-19 16:30:44 +01003566 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003567 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 {
3569 int idx;
3570
3571 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003572 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003574 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003575 if (STRCMP(name, "flatten") == 0)
3576 {
3577 emsg(_(e_cannot_use_flatten_in_vim9_script));
3578 goto theend;
3579 }
3580
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003581 if (STRCMP(name, "add") == 0 && argcount == 2)
3582 {
3583 garray_T *stack = &cctx->ctx_type_stack;
3584 type_T *type = ((type_T **)stack->ga_data)[
3585 stack->ga_len - 2];
3586
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003587 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003588 if (type->tt_type == VAR_LIST)
3589 {
3590 // inline "add(list, item)" so that the type can be checked
3591 res = generate_LISTAPPEND(cctx);
3592 idx = -1;
3593 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003594 else if (type->tt_type == VAR_BLOB)
3595 {
3596 // inline "add(blob, nr)" so that the type can be checked
3597 res = generate_BLOBAPPEND(cctx);
3598 idx = -1;
3599 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003600 }
3601
3602 if (idx >= 0)
3603 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3604 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003605 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003606 semsg(_(e_unknown_function_str), namebuf);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003607 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 }
3609
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003610 // An argument or local variable can be a function reference, this
3611 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003612 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003613 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003614 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003615 // If we can find the function by name generate the right call.
3616 // Skip global functions here, a local funcref takes precedence.
3617 ufunc = find_func(name, FALSE, cctx);
3618 if (ufunc != NULL && !func_is_global(ufunc))
3619 {
3620 res = generate_CALL(cctx, ufunc, argcount);
3621 goto theend;
3622 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003623 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624
3625 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003626 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003627 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003629 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003630 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003631 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003632 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003633 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003634
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003635 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003636 goto theend;
3637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638
Bram Moolenaar0f769812020-09-12 18:32:34 +02003639 // If we can find a global function by name generate the right call.
3640 if (ufunc != NULL)
3641 {
3642 res = generate_CALL(cctx, ufunc, argcount);
3643 goto theend;
3644 }
3645
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003646 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003647 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003648 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003649 res = generate_UCALL(cctx, name, argcount);
3650 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003651 semsg(_(e_unknown_function_str), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003652
3653theend:
3654 vim_free(tofree);
3655 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656}
3657
3658// like NAMESPACE_CHAR but with 'a' and 'l'.
3659#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3660
3661/*
3662 * Find the end of a variable or function name. Unlike find_name_end() this
3663 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003664 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 * Return a pointer to just after the name. Equal to "arg" if there is no
3666 * valid name.
3667 */
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003668 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003669to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670{
3671 char_u *p;
3672
3673 // Quick check for valid starting character.
3674 if (!eval_isnamec1(*arg))
3675 return arg;
3676
3677 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3678 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3679 // and can be used in slice "[n:]".
3680 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003681 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3683 break;
3684 return p;
3685}
3686
3687/*
3688 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003689 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003690 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 */
3692 char_u *
3693to_name_const_end(char_u *arg)
3694{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003695 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 typval_T rettv;
3697
Bram Moolenaar678b2072021-07-26 21:10:11 +02003698 if (STRNCMP(p, "<SNR>", 5) == 0)
3699 p = skipdigits(p + 5);
3700 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 if (p == arg && *arg == '[')
3702 {
3703
3704 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003705 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 p = arg;
3707 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 return p;
3709}
3710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711/*
3712 * parse a list: [expr, expr]
3713 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003714 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 */
3716 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003717compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718{
3719 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003720 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003722 int is_const;
3723 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003725 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003727 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003728 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003729 semsg(_(e_list_end), *arg);
3730 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003731 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003732 if (*p == ',')
3733 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003734 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003735 return FAIL;
3736 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003737 if (*p == ']')
3738 {
3739 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003740 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003741 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003742 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003743 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003744 if (!is_const)
3745 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 ++count;
3747 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003748 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003750 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3751 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003752 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003753 return FAIL;
3754 }
3755 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003756 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 p = skipwhite(p);
3758 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003759 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003761 ppconst->pp_is_const = is_all_const;
3762 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763}
3764
3765/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003766 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003767 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003768 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 */
3770 static int
3771compile_lambda(char_u **arg, cctx_T *cctx)
3772{
Bram Moolenaare462f522020-12-27 14:43:30 +01003773 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 typval_T rettv;
3775 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003776 evalarg_T evalarg;
3777
Bram Moolenaar844fb642021-10-23 13:32:30 +01003778 init_evalarg(&evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003779 evalarg.eval_flags = EVAL_EVALUATE;
3780 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781
3782 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003783 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3784 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003785 {
3786 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003787 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003788 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003789
Bram Moolenaar65c44152020-12-24 15:14:01 +01003790 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003792 ++ufunc->uf_refcount;
3793 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794
Bram Moolenaara9931532021-06-12 15:58:16 +02003795 // Compile it here to get the return type. The return type is optional,
3796 // when it's missing use t_unknown. This is recognized in
3797 // compile_return().
3798 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3799 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003800 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003802 // When the outer function is compiled for profiling or debugging, the
3803 // lambda may be called without profiling or debugging. Compile it here in
3804 // the right context.
3805 if (cctx->ctx_compile_type == CT_DEBUG
Bram Moolenaar648594e2021-07-11 17:55:01 +02003806#ifdef FEAT_PROFILE
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003807 || cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar648594e2021-07-11 17:55:01 +02003808#endif
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003809 )
3810 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaard9162552021-07-11 15:26:13 +02003811
Bram Moolenaar844fb642021-10-23 13:32:30 +01003812 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
3813 // "*arg" may point into it. Point into the original line to avoid a
3814 // dangling pointer.
3815 if (evalarg.eval_using_cmdline)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003816 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003817 garray_T *gap = &evalarg.eval_tofree_ga;
3818 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003819
3820 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3821 + off;
3822 }
3823
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003824 clear_evalarg(&evalarg, NULL);
3825
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003826 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003827 {
3828 // The return type will now be known.
3829 set_function_type(ufunc);
3830
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003831 // The function reference count will be 1. When the ISN_FUNCREF
3832 // instruction is deleted the reference count is decremented and the
3833 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003834 return generate_FUNCREF(cctx, ufunc);
3835 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003836
3837 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 return FAIL;
3839}
3840
3841/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003842 * Get a lambda and compile it. Uses Vim9 syntax.
3843 */
3844 int
3845get_lambda_tv_and_compile(
3846 char_u **arg,
3847 typval_T *rettv,
3848 int types_optional,
3849 evalarg_T *evalarg)
3850{
3851 int r;
3852 ufunc_T *ufunc;
3853 int save_sc_version = current_sctx.sc_version;
3854
3855 // Get the funcref in "rettv".
3856 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3857 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3858 current_sctx.sc_version = save_sc_version;
3859 if (r != OK)
3860 return r;
3861
3862 // "rettv" will now be a partial referencing the function.
3863 ufunc = rettv->vval.v_partial->pt_func;
3864
3865 // Compile it here to get the return type. The return type is optional,
3866 // when it's missing use t_unknown. This is recognized in
3867 // compile_return().
3868 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3869 ufunc->uf_ret_type = &t_unknown;
3870 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3871
3872 if (ufunc->uf_def_status == UF_COMPILED)
3873 {
3874 // The return type will now be known.
3875 set_function_type(ufunc);
3876 return OK;
3877 }
3878 clear_tv(rettv);
3879 return FAIL;
3880}
3881
3882/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003883 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003885 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 */
3887 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003888compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889{
3890 garray_T *instr = &cctx->ctx_instr;
3891 int count = 0;
3892 dict_T *d = dict_alloc();
3893 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003894 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003895 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003896 int is_const;
3897 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898
3899 if (d == NULL)
3900 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003901 if (generate_ppconst(cctx, ppconst) == FAIL)
3902 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003903 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003905 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906
Bram Moolenaar23c55272020-06-21 16:58:13 +02003907 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003908 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003909 *arg = NULL;
3910 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003911 }
3912
3913 if (**arg == '}')
3914 break;
3915
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003916 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003918 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003920 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003921 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003922 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003925 if (isn->isn_type == ISN_PUSHNR)
3926 {
3927 char buf[NUMBUFLEN];
3928
3929 // Convert to string at compile time.
3930 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3931 isn->isn_type = ISN_PUSHS;
3932 isn->isn_arg.string = vim_strsave((char_u *)buf);
3933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 if (isn->isn_type == ISN_PUSHS)
3935 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003936 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003937 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003938 *arg = skipwhite(*arg);
3939 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003940 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003941 emsg(_(e_missing_matching_bracket_after_dict_key));
3942 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003943 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003944 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003946 else
3947 {
3948 // {"name": value},
3949 // {'name': value},
3950 // {name: value} use "name" as a literal key
3951 key = get_literal_key(arg);
3952 if (key == NULL)
3953 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003954 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003955 return FAIL;
3956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957
3958 // Check for duplicate keys, if using string keys.
3959 if (key != NULL)
3960 {
3961 item = dict_find(d, key, -1);
3962 if (item != NULL)
3963 {
3964 semsg(_(e_duplicate_key), key);
3965 goto failret;
3966 }
3967 item = dictitem_alloc(key);
3968 if (item != NULL)
3969 {
3970 item->di_tv.v_type = VAR_UNKNOWN;
3971 item->di_tv.v_lock = 0;
3972 if (dict_add(d, item) == FAIL)
3973 dictitem_free(item);
3974 }
3975 }
3976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 if (**arg != ':')
3978 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003979 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003980 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003981 else
3982 semsg(_(e_missing_dict_colon), *arg);
3983 return FAIL;
3984 }
3985 whitep = *arg + 1;
3986 if (!IS_WHITE_OR_NUL(*whitep))
3987 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003988 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 return FAIL;
3990 }
3991
Bram Moolenaar23c55272020-06-21 16:58:13 +02003992 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003993 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003994 *arg = NULL;
3995 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003996 }
3997
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003998 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004000 if (!is_const)
4001 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 ++count;
4003
Bram Moolenaar2c330432020-04-13 14:41:35 +02004004 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02004005 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004006 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004007 *arg = NULL;
4008 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004009 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 if (**arg == '}')
4011 break;
4012 if (**arg != ',')
4013 {
4014 semsg(_(e_missing_dict_comma), *arg);
4015 goto failret;
4016 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02004017 if (IS_WHITE_OR_NUL(*whitep))
4018 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004019 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02004020 return FAIL;
4021 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02004022 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02004023 if (!IS_WHITE_OR_NUL(*whitep))
4024 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01004025 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02004026 return FAIL;
4027 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01004028 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 }
4030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 *arg = *arg + 1;
4032
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004033 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02004034 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004035 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004036 *arg += STRLEN(*arg);
4037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004039 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 return generate_NEWDICT(cctx, count);
4041
4042failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004043 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004044 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004045 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004046 *arg = (char_u *)"";
4047 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 dict_unref(d);
4049 return FAIL;
4050}
4051
4052/*
4053 * Compile "&option".
4054 */
4055 static int
4056compile_get_option(char_u **arg, cctx_T *cctx)
4057{
4058 typval_T rettv;
4059 char_u *start = *arg;
4060 int ret;
4061
4062 // parse the option and get the current value to get the type.
4063 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004064 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 if (ret == OK)
4066 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004067 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01004068 char_u *name = vim_strnsave(start, *arg - start);
4069 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
4070 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071
4072 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4073 vim_free(name);
4074 }
4075 clear_tv(&rettv);
4076
4077 return ret;
4078}
4079
4080/*
4081 * Compile "$VAR".
4082 */
4083 static int
4084compile_get_env(char_u **arg, cctx_T *cctx)
4085{
4086 char_u *start = *arg;
4087 int len;
4088 int ret;
4089 char_u *name;
4090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 ++*arg;
4092 len = get_env_len(arg);
4093 if (len == 0)
4094 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004095 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 return FAIL;
4097 }
4098
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004099 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 name = vim_strnsave(start, len + 1);
4101 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4102 vim_free(name);
4103 return ret;
4104}
4105
4106/*
4107 * Compile "@r".
4108 */
4109 static int
4110compile_get_register(char_u **arg, cctx_T *cctx)
4111{
4112 int ret;
4113
4114 ++*arg;
4115 if (**arg == NUL)
4116 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004117 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 return FAIL;
4119 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004120 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 {
4122 emsg_invreg(**arg);
4123 return FAIL;
4124 }
4125 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4126 ++*arg;
4127 return ret;
4128}
4129
4130/*
4131 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004132 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 */
4134 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004135apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004137 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138
4139 // this works from end to start
4140 while (p > start)
4141 {
4142 --p;
4143 if (*p == '-' || *p == '+')
4144 {
4145 // only '-' has an effect, for '+' we only check the type
4146#ifdef FEAT_FLOAT
4147 if (rettv->v_type == VAR_FLOAT)
4148 {
4149 if (*p == '-')
4150 rettv->vval.v_float = -rettv->vval.v_float;
4151 }
4152 else
4153#endif
4154 {
4155 varnumber_T val;
4156 int error = FALSE;
4157
4158 // tv_get_number_chk() accepts a string, but we don't want that
4159 // here
4160 if (check_not_string(rettv) == FAIL)
4161 return FAIL;
4162 val = tv_get_number_chk(rettv, &error);
4163 clear_tv(rettv);
4164 if (error)
4165 return FAIL;
4166 if (*p == '-')
4167 val = -val;
4168 rettv->v_type = VAR_NUMBER;
4169 rettv->vval.v_number = val;
4170 }
4171 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004172 else if (numeric_only)
4173 {
4174 ++p;
4175 break;
4176 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004177 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 {
4179 int v = tv2bool(rettv);
4180
4181 // '!' is permissive in the type.
4182 clear_tv(rettv);
4183 rettv->v_type = VAR_BOOL;
4184 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4185 }
4186 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004187 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188 return OK;
4189}
4190
4191/*
4192 * Recognize v: variables that are constants and set "rettv".
4193 */
4194 static void
4195get_vim_constant(char_u **arg, typval_T *rettv)
4196{
4197 if (STRNCMP(*arg, "v:true", 6) == 0)
4198 {
4199 rettv->v_type = VAR_BOOL;
4200 rettv->vval.v_number = VVAL_TRUE;
4201 *arg += 6;
4202 }
4203 else if (STRNCMP(*arg, "v:false", 7) == 0)
4204 {
4205 rettv->v_type = VAR_BOOL;
4206 rettv->vval.v_number = VVAL_FALSE;
4207 *arg += 7;
4208 }
4209 else if (STRNCMP(*arg, "v:null", 6) == 0)
4210 {
4211 rettv->v_type = VAR_SPECIAL;
4212 rettv->vval.v_number = VVAL_NULL;
4213 *arg += 6;
4214 }
4215 else if (STRNCMP(*arg, "v:none", 6) == 0)
4216 {
4217 rettv->v_type = VAR_SPECIAL;
4218 rettv->vval.v_number = VVAL_NONE;
4219 *arg += 6;
4220 }
4221}
4222
Bram Moolenaar657137c2021-01-09 15:45:23 +01004223 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004224get_compare_type(char_u *p, int *len, int *type_is)
4225{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004226 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004227 int i;
4228
4229 switch (p[0])
4230 {
4231 case '=': if (p[1] == '=')
4232 type = EXPR_EQUAL;
4233 else if (p[1] == '~')
4234 type = EXPR_MATCH;
4235 break;
4236 case '!': if (p[1] == '=')
4237 type = EXPR_NEQUAL;
4238 else if (p[1] == '~')
4239 type = EXPR_NOMATCH;
4240 break;
4241 case '>': if (p[1] != '=')
4242 {
4243 type = EXPR_GREATER;
4244 *len = 1;
4245 }
4246 else
4247 type = EXPR_GEQUAL;
4248 break;
4249 case '<': if (p[1] != '=')
4250 {
4251 type = EXPR_SMALLER;
4252 *len = 1;
4253 }
4254 else
4255 type = EXPR_SEQUAL;
4256 break;
4257 case 'i': if (p[1] == 's')
4258 {
4259 // "is" and "isnot"; but not a prefix of a name
4260 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4261 *len = 5;
4262 i = p[*len];
4263 if (!isalnum(i) && i != '_')
4264 {
4265 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4266 *type_is = TRUE;
4267 }
4268 }
4269 break;
4270 }
4271 return type;
4272}
4273
4274/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004275 * Skip over an expression, ignoring most errors.
4276 */
4277 static void
4278skip_expr_cctx(char_u **arg, cctx_T *cctx)
4279{
4280 evalarg_T evalarg;
4281
Bram Moolenaar844fb642021-10-23 13:32:30 +01004282 init_evalarg(&evalarg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004283 evalarg.eval_cctx = cctx;
4284 skip_expr(arg, &evalarg);
Bram Moolenaar844fb642021-10-23 13:32:30 +01004285 clear_evalarg(&evalarg, NULL);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004286}
4287
4288/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004290 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 */
4292 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004293compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004295 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296
4297 // this works from end to start
4298 while (p > start)
4299 {
4300 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004301 while (VIM_ISWHITE(*p))
4302 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 if (*p == '-' || *p == '+')
4304 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004305 int negate = *p == '-';
4306 isn_T *isn;
4307 garray_T *stack = &cctx->ctx_type_stack;
4308 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004310 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarfea43e42021-12-19 21:34:05 +00004311 if (type != &t_float && need_type(type, &t_number,
4312 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004313 return FAIL;
4314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4316 {
4317 --p;
4318 if (*p == '-')
4319 negate = !negate;
4320 }
4321 // only '-' has an effect, for '+' we only check the type
4322 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004323 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004325 if (isn == NULL)
4326 return FAIL;
4327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004329 else if (numeric_only)
4330 {
4331 ++p;
4332 break;
4333 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 else
4335 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004336 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004338 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004340 if (p[-1] == '!')
4341 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004344 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 return FAIL;
4346 }
4347 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004348 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 return OK;
4350}
4351
4352/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004353 * Compile "(expression)": recursive!
4354 * Return FAIL/OK.
4355 */
4356 static int
4357compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4358{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004359 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004360 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004361
Bram Moolenaar24156692021-01-14 20:35:49 +01004362 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4363 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004364 if (ppconst->pp_used <= PPSIZE - 10)
4365 {
4366 ret = compile_expr1(arg, cctx, ppconst);
4367 }
4368 else
4369 {
4370 // Not enough space in ppconst, flush constants.
4371 if (generate_ppconst(cctx, ppconst) == FAIL)
4372 return FAIL;
4373 ret = compile_expr0(arg, cctx);
4374 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004375 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4376 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004377 if (**arg == ')')
4378 ++*arg;
4379 else if (ret == OK)
4380 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004381 emsg(_(e_missing_closing_paren));
Bram Moolenaar7e368202020-12-25 21:56:57 +01004382 ret = FAIL;
4383 }
4384 return ret;
4385}
4386
4387/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004389 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 */
4391 static int
4392compile_subscript(
4393 char_u **arg,
4394 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004395 char_u *start_leader,
4396 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004397 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004399 char_u *name_start = *end_leader;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004400 int keeping_dict = FALSE;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 for (;;)
4403 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004404 char_u *p = skipwhite(*arg);
4405
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004406 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004407 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004408 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004409
4410 // If a following line starts with "->{" or "->X" advance to that
4411 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004412 // Also if a following line starts with ".x".
4413 if (next != NULL &&
4414 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004415 && (next[2] == '{'
4416 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004417 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004418 {
4419 next = next_line_from_context(cctx, TRUE);
4420 if (next == NULL)
4421 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004422 *arg = next;
4423 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004424 }
4425 }
4426
Bram Moolenaarcd268012021-07-22 19:11:08 +02004427 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4428 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004429 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004431 garray_T *stack = &cctx->ctx_type_stack;
4432 type_T *type;
4433 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004435 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004436 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004437 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004440 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4441
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004442 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004443 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004445 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004447 if (keeping_dict)
4448 {
4449 keeping_dict = FALSE;
4450 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4451 return FAIL;
4452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004454 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004456 char_u *pstart = p;
4457
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004458 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004459 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004460 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 // something->method()
4463 // Apply the '!', '-' and '+' first:
4464 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004465 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004468 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004469 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004470 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004471 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004472 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004473 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004474 garray_T *stack = &cctx->ctx_type_stack;
4475 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004476 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004477 int expr_isn_start = cctx->ctx_instr.ga_len;
4478 int expr_isn_end;
4479 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004480
4481 // Funcref call: list->(Refs[2])(arg)
4482 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004483 //
4484 // Fist compile the function expression.
4485 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004486 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004487
4488 // Remember the next instruction index, where the instructions
4489 // for arguments are being written.
4490 expr_isn_end = cctx->ctx_instr.ga_len;
4491
4492 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004493 if (**arg != '(')
4494 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004495 if (*skipwhite(*arg) == '(')
4496 emsg(_(e_nowhitespace));
4497 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004498 semsg(_(e_missing_parenthesis_str), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004499 return FAIL;
4500 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004501 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004502 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004503 return FAIL;
4504
Bram Moolenaar2927c072021-04-05 19:41:21 +02004505 // Move the instructions for the arguments to before the
4506 // instructions of the expression and move the type of the
4507 // expression after the argument types. This is what ISN_PCALL
4508 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004509 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004510 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4511 if (arg_isn_count > 0)
4512 {
4513 int expr_isn_count = expr_isn_end - expr_isn_start;
4514 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4515
4516 if (isn == NULL)
4517 return FAIL;
4518 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4519 + expr_isn_start,
4520 sizeof(isn_T) * expr_isn_count);
4521 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4522 + expr_isn_start,
4523 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4524 sizeof(isn_T) * arg_isn_count);
4525 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4526 + expr_isn_start + arg_isn_count,
4527 isn, sizeof(isn_T) * expr_isn_count);
4528 vim_free(isn);
4529
4530 type = ((type_T **)stack->ga_data)[type_idx_start];
4531 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4532 ((type_T **)stack->ga_data) + type_idx_start + 1,
4533 sizeof(type_T *)
4534 * (stack->ga_len - type_idx_start - 1));
4535 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4536 }
4537
Bram Moolenaar7e368202020-12-25 21:56:57 +01004538 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004539 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 return FAIL;
4541 }
4542 else
4543 {
4544 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004545 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004546 if (!eval_isnamec1(*p))
4547 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004548 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004549 return FAIL;
4550 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004551 if (ASCII_ISALPHA(*p) && p[1] == ':')
4552 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004553 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 ;
4555 if (*p != '(')
4556 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004557 semsg(_(e_missing_parenthesis_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 return FAIL;
4559 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004560 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 return FAIL;
4562 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004563 if (keeping_dict)
4564 {
4565 keeping_dict = FALSE;
4566 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4567 return FAIL;
4568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004570 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004572 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004573
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004574 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004575 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004576 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004577 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004578 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004579 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004580 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004581
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004582 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004583 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004584 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004585 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004586 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004587 // missing first index is equal to zero
4588 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004589 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004590 else
4591 {
4592 if (compile_expr0(arg, cctx) == FAIL)
4593 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004594 if (**arg == ':')
4595 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004596 semsg(_(e_white_space_required_before_and_after_str_at_str),
4597 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004598 return FAIL;
4599 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004600 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004601 return FAIL;
4602 *arg = skipwhite(*arg);
4603 }
4604 if (**arg == ':')
4605 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004606 is_slice = TRUE;
4607 ++*arg;
4608 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4609 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004610 semsg(_(e_white_space_required_before_and_after_str_at_str),
4611 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004612 return FAIL;
4613 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004614 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004615 return FAIL;
4616 if (**arg == ']')
4617 // missing second index is equal to end of string
4618 generate_PUSHNR(cctx, -1);
4619 else
4620 {
4621 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004622 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004623 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004624 return FAIL;
4625 *arg = skipwhite(*arg);
4626 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628
4629 if (**arg != ']')
4630 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004631 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 return FAIL;
4633 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004634 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004636 if (keeping_dict)
4637 {
4638 keeping_dict = FALSE;
4639 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4640 return FAIL;
4641 }
4642 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004643 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004645 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004647 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004648 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004649 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004650 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004651
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004652 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004653 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004654 {
4655 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004656 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004657 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004658 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004659 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 while (eval_isnamec(*p))
4661 MB_PTR_ADV(p);
4662 if (p == *arg)
4663 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004664 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 return FAIL;
4666 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004667 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
4668 return FAIL;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004669 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004671 keeping_dict = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 *arg = p;
4673 }
4674 else
4675 break;
4676 }
4677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 // Turn "dict.Func" into a partial for "Func" bound to "dict".
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004679 // This needs to be done at runtime to be able to check the type.
4680 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
4681 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682
4683 return OK;
4684}
4685
4686/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004687 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4688 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004690 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004691 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004692 *
4693 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 */
4695
4696/*
4697 * number number constant
4698 * 0zFFFFFFFF Blob constant
4699 * "string" string constant
4700 * 'string' literal string constant
4701 * &option-name option value
4702 * @r register contents
4703 * identifier variable value
4704 * function() function call
4705 * $VAR environment variable
4706 * (expression) nested expression
4707 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004708 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 *
4710 * Also handle:
4711 * ! in front logical NOT
4712 * - in front unary minus
4713 * + in front unary plus (ignored)
4714 * trailing (arg) funcref/partial call
4715 * trailing [] subscript in String or List
4716 * trailing .name entry in Dictionary
4717 * trailing ->name() method call
4718 */
4719 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004720compile_expr7(
4721 char_u **arg,
4722 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004723 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 char_u *start_leader, *end_leader;
4726 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004727 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004728 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004730 ppconst->pp_is_const = FALSE;
4731
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 /*
4733 * Skip '!', '-' and '+' characters. They are handled later.
4734 */
4735 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004736 if (eval_leader(arg, TRUE) == FAIL)
4737 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 end_leader = *arg;
4739
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004740 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741 switch (**arg)
4742 {
4743 /*
4744 * Number constant.
4745 */
4746 case '0': // also for blob starting with 0z
4747 case '1':
4748 case '2':
4749 case '3':
4750 case '4':
4751 case '5':
4752 case '6':
4753 case '7':
4754 case '8':
4755 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004756 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004758 // Apply "-" and "+" just before the number now, right to
4759 // left. Matters especially when "->" follows. Stops at
4760 // '!'.
4761 if (apply_leader(rettv, TRUE,
4762 start_leader, &end_leader) == FAIL)
4763 {
4764 clear_tv(rettv);
4765 return FAIL;
4766 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767 break;
4768
4769 /*
4770 * String constant: "string".
4771 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004772 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 return FAIL;
4774 break;
4775
4776 /*
4777 * Literal string constant: 'str''ing'.
4778 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004779 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 return FAIL;
4781 break;
4782
4783 /*
4784 * Constant Vim variable.
4785 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004786 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 ret = NOTDONE;
4788 break;
4789
4790 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004791 * "true" constant
4792 */
4793 case 't': if (STRNCMP(*arg, "true", 4) == 0
4794 && !eval_isnamec((*arg)[4]))
4795 {
4796 *arg += 4;
4797 rettv->v_type = VAR_BOOL;
4798 rettv->vval.v_number = VVAL_TRUE;
4799 }
4800 else
4801 ret = NOTDONE;
4802 break;
4803
4804 /*
4805 * "false" constant
4806 */
4807 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4808 && !eval_isnamec((*arg)[5]))
4809 {
4810 *arg += 5;
4811 rettv->v_type = VAR_BOOL;
4812 rettv->vval.v_number = VVAL_FALSE;
4813 }
4814 else
4815 ret = NOTDONE;
4816 break;
4817
4818 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004819 * "null" constant
4820 */
4821 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004822 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004823 {
4824 *arg += 4;
4825 rettv->v_type = VAR_SPECIAL;
4826 rettv->vval.v_number = VVAL_NULL;
4827 }
4828 else
4829 ret = NOTDONE;
4830 break;
4831
4832 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 * List: [expr, expr]
4834 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004835 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4836 return FAIL;
4837 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 break;
4839
4840 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841 * Dictionary: {'key': val, 'key': val}
4842 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004843 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4844 return FAIL;
4845 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 break;
4847
4848 /*
4849 * Option value: &name
4850 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004851 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4852 return FAIL;
4853 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854 break;
4855
4856 /*
4857 * Environment variable: $VAR.
4858 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004859 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4860 return FAIL;
4861 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 break;
4863
4864 /*
4865 * Register contents: @r.
4866 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004867 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4868 return FAIL;
4869 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 break;
4871 /*
4872 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004873 * lambda: (arg, arg) => expr
4874 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004876 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4877 ret = compile_lambda(arg, cctx);
4878 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004879 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 break;
4881
4882 default: ret = NOTDONE;
4883 break;
4884 }
4885 if (ret == FAIL)
4886 return FAIL;
4887
Bram Moolenaar1c747212020-05-09 18:28:34 +02004888 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004890 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004891 clear_tv(rettv);
4892 else
4893 // A constant expression can possibly be handled compile time,
4894 // return the value instead of generating code.
4895 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 }
4897 else if (ret == NOTDONE)
4898 {
4899 char_u *p;
4900 int r;
4901
4902 if (!eval_isnamec1(**arg))
4903 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004904 if (!vim9_bad_comment(*arg))
4905 {
4906 if (ends_excmd(*skipwhite(*arg)))
4907 semsg(_(e_empty_expression_str), *arg);
4908 else
4909 semsg(_(e_name_expected_str), *arg);
4910 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 return FAIL;
4912 }
4913
4914 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004915 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004916 if (p - *arg == (size_t)1 && **arg == '_')
4917 {
4918 emsg(_(e_cannot_use_underscore_here));
4919 return FAIL;
4920 }
4921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004923 {
4924 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004927 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02004928 if (cctx->ctx_skip != SKIP_YES
4929 && generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004930 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004931 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 if (r == FAIL)
4934 return FAIL;
4935 }
4936
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004937 // Handle following "[]", ".member", etc.
4938 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004939 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004940 ppconst) == FAIL)
4941 return FAIL;
4942 if (ppconst->pp_used > 0)
4943 {
4944 // apply the '!', '-' and '+' before the constant
4945 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004946 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004947 return FAIL;
4948 return OK;
4949 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004950 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004952 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953}
4954
4955/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004956 * Give the "white on both sides" error, taking the operator from "p[len]".
4957 */
4958 void
4959error_white_both(char_u *op, int len)
4960{
4961 char_u buf[10];
4962
4963 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004964 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004965}
4966
4967/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004968 * <type>expr7: runtime type check / conversion
4969 */
4970 static int
4971compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4972{
4973 type_T *want_type = NULL;
4974
4975 // Recognize <type>
4976 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4977 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004978 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004979 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4980 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004981 return FAIL;
4982
4983 if (**arg != '>')
4984 {
4985 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004986 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004987 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004988 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004989 return FAIL;
4990 }
4991 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004992 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004993 return FAIL;
4994 }
4995
4996 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4997 return FAIL;
4998
4999 if (want_type != NULL)
5000 {
5001 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02005002 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005003 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005004
Bram Moolenaard1103582020-08-14 22:44:25 +02005005 generate_ppconst(cctx, ppconst);
5006 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005007 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005008 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01005009 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005010 return FAIL;
5011 }
5012 }
5013
5014 return OK;
5015}
5016
5017/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 * * number multiplication
5019 * / number division
5020 * % number modulo
5021 */
5022 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005023compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024{
5025 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005026 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005027 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005029 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005030 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 return FAIL;
5032
5033 /*
5034 * Repeat computing, until no "*", "/" or "%" is following.
5035 */
5036 for (;;)
5037 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005038 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 if (*op != '*' && *op != '/' && *op != '%')
5040 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005041 if (next != NULL)
5042 {
5043 *arg = next_line_from_context(cctx, TRUE);
5044 op = skipwhite(*arg);
5045 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005046
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005047 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005049 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005050 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01005052 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005053 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005055 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005056 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005058
5059 if (ppconst->pp_used == ppconst_used + 2
5060 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5061 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005062 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005063 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5064 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
5065 varnumber_T res = 0;
5066 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005068 // both are numbers: compute the result
5069 switch (*op)
5070 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005071 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005072 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005073 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005074 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005075 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005076 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005077 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005078 break;
5079 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005080 if (failed)
5081 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005082 tv1->vval.v_number = res;
5083 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005084 }
5085 else
5086 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005087 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005088 generate_two_op(cctx, op);
5089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 }
5091
5092 return OK;
5093}
5094
5095/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01005096 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 * - number subtraction
5098 * .. string concatenation
5099 */
5100 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005101compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102{
5103 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005104 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005106 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107
5108 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005109 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 return FAIL;
5111
5112 /*
5113 * Repeat computing, until no "+", "-" or ".." is following.
5114 */
5115 for (;;)
5116 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005117 op = may_peek_next_line(cctx, *arg, &next);
5118 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005120 if (op[0] == op[1] && *op != '.' && next)
5121 // Finding "++" or "--" on the next line is a separate command.
5122 // But ".." is concatenation.
5123 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005125 if (next != NULL)
5126 {
5127 *arg = next_line_from_context(cctx, TRUE);
5128 op = skipwhite(*arg);
5129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005131 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005133 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005134 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005135 }
5136
Bram Moolenaare0de1712020-12-02 17:36:54 +01005137 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005138 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005140 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005141 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142 return FAIL;
5143
Bram Moolenaara5565e42020-05-09 15:44:01 +02005144 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005145 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005146 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5147 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5148 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5149 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005150 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005151 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5152 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005153
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005154 // concat/subtract/add constant numbers
5155 if (*op == '+')
5156 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5157 else if (*op == '-')
5158 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5159 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005160 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005161 // concatenate constant strings
5162 char_u *s1 = tv1->vval.v_string;
5163 char_u *s2 = tv2->vval.v_string;
5164 size_t len1 = STRLEN(s1);
5165
5166 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5167 if (tv1->vval.v_string == NULL)
5168 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005169 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005170 return FAIL;
5171 }
5172 mch_memmove(tv1->vval.v_string, s1, len1);
5173 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005174 vim_free(s1);
5175 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005176 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005177 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178 }
5179 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005180 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005181 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005182 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005183 if (*op == '.')
5184 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005185 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5186 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005187 return FAIL;
5188 generate_instr_drop(cctx, ISN_CONCAT, 1);
5189 }
5190 else
5191 generate_two_op(cctx, op);
5192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 }
5194
5195 return OK;
5196}
5197
5198/*
5199 * expr5a == expr5b
5200 * expr5a =~ expr5b
5201 * expr5a != expr5b
5202 * expr5a !~ expr5b
5203 * expr5a > expr5b
5204 * expr5a >= expr5b
5205 * expr5a < expr5b
5206 * expr5a <= expr5b
5207 * expr5a is expr5b
5208 * expr5a isnot expr5b
5209 *
5210 * Produces instructions:
5211 * EVAL expr5a Push result of "expr5a"
5212 * EVAL expr5b Push result of "expr5b"
5213 * COMPARE one of the compare instructions
5214 */
5215 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005216compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005218 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005220 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005223 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005224
5225 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005226 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227 return FAIL;
5228
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005229 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005230 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231
5232 /*
5233 * If there is a comparative operator, use it.
5234 */
5235 if (type != EXPR_UNKNOWN)
5236 {
5237 int ic = FALSE; // Default: do not ignore case
5238
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005239 if (next != NULL)
5240 {
5241 *arg = next_line_from_context(cctx, TRUE);
5242 p = skipwhite(*arg);
5243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005244 if (type_is && (p[len] == '?' || p[len] == '#'))
5245 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005246 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 return FAIL;
5248 }
5249 // extra question mark appended: ignore case
5250 if (p[len] == '?')
5251 {
5252 ic = TRUE;
5253 ++len;
5254 }
5255 // extra '#' appended: match case (ignored)
5256 else if (p[len] == '#')
5257 ++len;
5258 // nothing appended: match case
5259
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005260 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005262 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005263 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005264 }
5265
5266 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005267 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005268 return FAIL;
5269
Bram Moolenaara5565e42020-05-09 15:44:01 +02005270 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 return FAIL;
5272
Bram Moolenaara5565e42020-05-09 15:44:01 +02005273 if (ppconst->pp_used == ppconst_used + 2)
5274 {
5275 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5276 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5277 int ret;
5278
5279 // Both sides are a constant, compute the result now.
5280 // First check for a valid combination of types, this is more
5281 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005282 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005283 ret = FAIL;
5284 else
5285 {
5286 ret = typval_compare(tv1, tv2, type, ic);
5287 tv1->v_type = VAR_BOOL;
5288 tv1->vval.v_number = tv1->vval.v_number
5289 ? VVAL_TRUE : VVAL_FALSE;
5290 clear_tv(tv2);
5291 --ppconst->pp_used;
5292 }
5293 return ret;
5294 }
5295
5296 generate_ppconst(cctx, ppconst);
5297 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005298 }
5299
5300 return OK;
5301}
5302
Bram Moolenaar7f141552020-05-09 17:35:53 +02005303static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005305/*
5306 * Compile || or &&.
5307 */
5308 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005309compile_and_or(
5310 char_u **arg,
5311 cctx_T *cctx,
5312 char *op,
5313 ppconst_T *ppconst,
5314 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005316 char_u *next;
5317 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 int opchar = *op;
5319
5320 if (p[0] == opchar && p[1] == opchar)
5321 {
5322 garray_T *instr = &cctx->ctx_instr;
5323 garray_T end_ga;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005324 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005325
5326 /*
5327 * Repeat until there is no following "||" or "&&"
5328 */
5329 ga_init2(&end_ga, sizeof(int), 10);
5330 while (p[0] == opchar && p[1] == opchar)
5331 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005332 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005333 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005334 int start_ctx_lnum = cctx->ctx_lnum;
5335 int save_lnum;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005336 int const_used;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005337 int status;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005338 jumpwhen_T jump_when = opchar == '|'
5339 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005340
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005341 if (next != NULL)
5342 {
5343 *arg = next_line_from_context(cctx, TRUE);
5344 p = skipwhite(*arg);
5345 }
5346
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005347 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5348 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005349 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005350 op, p);
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005351 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005352 return FAIL;
5353 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005355 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005356 SOURCING_LNUM = start_lnum;
5357 save_lnum = cctx->ctx_lnum;
5358 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005359
5360 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005361 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005362 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005363 // Use the last ppconst if possible.
5364 if (ppconst->pp_used > 0)
5365 {
5366 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5367 int is_true = tv2bool(tv);
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005368
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005369 if ((is_true && opchar == '|')
5370 || (!is_true && opchar == '&'))
5371 {
5372 // For "false && expr" and "true || expr" the "expr"
5373 // does not need to be evaluated.
5374 cctx->ctx_skip = SKIP_YES;
5375 clear_tv(tv);
5376 tv->v_type = VAR_BOOL;
5377 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
5378 }
5379 else
5380 {
5381 // For "true && expr" and "false || expr" only "expr"
5382 // needs to be evaluated.
5383 --ppconst->pp_used;
5384 jump_when = JUMP_NEVER;
5385 }
5386 }
5387 else
5388 {
5389 // Every part must evaluate to a bool.
5390 status = bool_on_stack(cctx);
5391 }
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005392 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005393 if (status != FAIL)
5394 status = ga_grow(&end_ga, 1);
Bram Moolenaara7511c02021-04-03 21:47:07 +02005395 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005396 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005397 {
5398 ga_clear(&end_ga);
5399 return FAIL;
5400 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005401
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005402 if (jump_when != JUMP_NEVER)
5403 {
5404 if (cctx->ctx_skip != SKIP_YES)
5405 {
5406 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5407 ++end_ga.ga_len;
5408 }
5409 generate_JUMP(cctx, jump_when, 0);
5410 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005411
5412 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005413 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005414 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005415 {
5416 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005417 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005418 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005419
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005420 const_used = ppconst->pp_used;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005421 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5422 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 {
5424 ga_clear(&end_ga);
5425 return FAIL;
5426 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005427
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005428 // "0 || 1" results in true, "1 && 0" results in false.
5429 if (ppconst->pp_used == const_used + 1)
5430 {
5431 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5432
5433 if (tv->v_type == VAR_NUMBER
5434 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
5435 {
5436 tv->vval.v_number = tv->vval.v_number == 1
5437 ? VVAL_TRUE : VVAL_FALSE;
5438 tv->v_type = VAR_BOOL;
5439 }
5440 }
5441
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005442 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005443 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005444
5445 if (check_ppconst_bool(ppconst) == FAIL)
5446 {
5447 ga_clear(&end_ga);
5448 return FAIL;
5449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005451 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
5452 // Every part must evaluate to a bool.
5453 if (bool_on_stack(cctx) == FAIL)
5454 {
5455 ga_clear(&end_ga);
5456 return FAIL;
5457 }
5458
5459 if (end_ga.ga_len > 0)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005460 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005461 // Fill in the end label in all jumps.
5462 generate_ppconst(cctx, ppconst);
5463 while (end_ga.ga_len > 0)
5464 {
5465 isn_T *isn;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005466
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005467 --end_ga.ga_len;
5468 isn = ((isn_T *)instr->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005469 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005470 isn->isn_arg.jump.jump_where = instr->ga_len;
5471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005472 }
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005473 ga_clear(&end_ga);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005474
5475 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476 }
5477
5478 return OK;
5479}
5480
5481/*
5482 * expr4a && expr4a && expr4a logical AND
5483 *
5484 * Produces instructions:
5485 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005486 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005487 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005488 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005489 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490 * EVAL expr4c Push result of "expr4c"
5491 * end:
5492 */
5493 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005494compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005495{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005496 int ppconst_used = ppconst->pp_used;
5497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005498 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005499 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005500 return FAIL;
5501
5502 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005503 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005504}
5505
5506/*
5507 * expr3a || expr3b || expr3c logical OR
5508 *
5509 * Produces instructions:
5510 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005511 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005512 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005514 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515 * EVAL expr3c Push result of "expr3c"
5516 * end:
5517 */
5518 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005519compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005520{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005521 int ppconst_used = ppconst->pp_used;
5522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005523 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005524 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525 return FAIL;
5526
5527 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005528 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005529}
5530
5531/*
5532 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005534 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535 * JUMP_IF_FALSE alt jump if false
5536 * EVAL expr1a
5537 * JUMP_ALWAYS end
5538 * alt: EVAL expr1b
5539 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005540 *
5541 * Toplevel expression: expr2 ?? expr1
5542 * Produces instructions:
5543 * EVAL expr2 Push result of "expr2"
5544 * JUMP_AND_KEEP_IF_TRUE end jump if true
5545 * EVAL expr1
5546 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005547 */
5548 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005549compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550{
5551 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005552 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005553 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005554
Bram Moolenaar3988f642020-08-27 22:43:03 +02005555 // Ignore all kinds of errors when not producing code.
5556 if (cctx->ctx_skip == SKIP_YES)
5557 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005558 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005559 return OK;
5560 }
5561
Bram Moolenaar61a89812020-05-07 16:58:17 +02005562 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005563 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005564 return FAIL;
5565
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005566 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005567 if (*p == '?')
5568 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005569 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005570 garray_T *instr = &cctx->ctx_instr;
5571 garray_T *stack = &cctx->ctx_type_stack;
5572 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005573 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005574 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005575 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005576 int has_const_expr = FALSE;
5577 int const_value = FALSE;
5578 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005580 if (next != NULL)
5581 {
5582 *arg = next_line_from_context(cctx, TRUE);
5583 p = skipwhite(*arg);
5584 }
5585
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005586 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005587 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005588 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005589 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005590 return FAIL;
5591 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592
Bram Moolenaara5565e42020-05-09 15:44:01 +02005593 if (ppconst->pp_used == ppconst_used + 1)
5594 {
5595 // the condition is a constant, we know whether the ? or the :
5596 // expression is to be evaluated.
5597 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005598 if (op_falsy)
5599 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5600 else
5601 {
5602 int error = FALSE;
5603
5604 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5605 &error);
5606 if (error)
5607 return FAIL;
5608 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005609 cctx->ctx_skip = save_skip == SKIP_YES ||
5610 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5611
5612 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5613 // "left ?? right" and "left" is truthy: produce "left"
5614 generate_ppconst(cctx, ppconst);
5615 else
5616 {
5617 clear_tv(&ppconst->pp_tv[ppconst_used]);
5618 --ppconst->pp_used;
5619 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005620 }
5621 else
5622 {
5623 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005624 if (op_falsy)
5625 end_idx = instr->ga_len;
5626 generate_JUMP(cctx, op_falsy
5627 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5628 if (op_falsy)
5629 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005630 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005631
5632 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005633 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005634 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005635 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005636 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005637
Bram Moolenaara5565e42020-05-09 15:44:01 +02005638 if (!has_const_expr)
5639 {
5640 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005642 if (!op_falsy)
5643 {
5644 // remember the type and drop it
5645 --stack->ga_len;
5646 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005648 end_idx = instr->ga_len;
5649 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005650
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005651 // jump here from JUMP_IF_FALSE
5652 isn = ((isn_T *)instr->ga_data) + alt_idx;
5653 isn->isn_arg.jump.jump_where = instr->ga_len;
5654 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005655 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005656
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005657 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005659 // Check for the ":".
5660 p = may_peek_next_line(cctx, *arg, &next);
5661 if (*p != ':')
5662 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005663 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005664 return FAIL;
5665 }
5666 if (next != NULL)
5667 {
5668 *arg = next_line_from_context(cctx, TRUE);
5669 p = skipwhite(*arg);
5670 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005671
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005672 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5673 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005674 semsg(_(e_white_space_required_before_and_after_str_at_str),
5675 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005676 return FAIL;
5677 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005678
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005679 // evaluate the third expression
5680 if (has_const_expr)
5681 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005682 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005683 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005684 return FAIL;
5685 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5686 return FAIL;
5687 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005688
Bram Moolenaara5565e42020-05-09 15:44:01 +02005689 if (!has_const_expr)
5690 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005691 type_T **typep;
5692
Bram Moolenaara5565e42020-05-09 15:44:01 +02005693 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005694
Bram Moolenaara5565e42020-05-09 15:44:01 +02005695 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005696 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5697 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005698
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005699 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005700 isn = ((isn_T *)instr->ga_data) + end_idx;
5701 isn->isn_arg.jump.jump_where = instr->ga_len;
5702 }
5703
5704 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005705 }
5706 return OK;
5707}
5708
5709/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005710 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005711 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5712 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005713 */
5714 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005715compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005716{
5717 ppconst_T ppconst;
5718
5719 CLEAR_FIELD(ppconst);
5720 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5721 {
5722 clear_ppconst(&ppconst);
5723 return FAIL;
5724 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005725 if (is_const != NULL)
5726 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005727 if (generate_ppconst(cctx, &ppconst) == FAIL)
5728 return FAIL;
5729 return OK;
5730}
5731
5732/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005733 * Toplevel expression.
5734 */
5735 static int
5736compile_expr0(char_u **arg, cctx_T *cctx)
5737{
5738 return compile_expr0_ext(arg, cctx, NULL);
5739}
5740
5741/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005742 * Compile "return [expr]".
5743 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005744 */
5745 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005746compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005747{
5748 char_u *p = arg;
5749 garray_T *stack = &cctx->ctx_type_stack;
5750 type_T *stack_type;
5751
5752 if (*p != NUL && *p != '|' && *p != '\n')
5753 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005754 if (legacy)
5755 {
5756 int save_flags = cmdmod.cmod_flags;
5757
5758 generate_LEGACY_EVAL(cctx, p);
5759 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5760 0, cctx, FALSE, FALSE) == FAIL)
5761 return NULL;
5762 cmdmod.cmod_flags |= CMOD_LEGACY;
5763 (void)skip_expr(&p, NULL);
5764 cmdmod.cmod_flags = save_flags;
5765 }
5766 else
5767 {
5768 // compile return argument into instructions
5769 if (compile_expr0(&p, cctx) == FAIL)
5770 return NULL;
5771 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005772
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005773 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005774 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005775 // "check_return_type" with uf_ret_type set to &t_unknown is used
5776 // for an inline function without a specified return type. Set the
5777 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005778 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005779 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005780 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5781 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005782 || (!check_return_type
5783 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005784 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005785 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005786 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005787 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005788 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005789 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5790 && stack_type->tt_type != VAR_VOID
5791 && stack_type->tt_type != VAR_UNKNOWN)
5792 {
5793 emsg(_(e_returning_value_in_function_without_return_type));
5794 return NULL;
5795 }
5796 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005797 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005798 return NULL;
5799 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005801 }
5802 else
5803 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005804 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005805 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005806 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5807 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005808 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005809 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005810 return NULL;
5811 }
5812
5813 // No argument, return zero.
5814 generate_PUSHNR(cctx, 0);
5815 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005816
5817 // Undo any command modifiers.
5818 generate_undo_cmdmods(cctx);
5819
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005820 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005821 return NULL;
5822
5823 // "return val | endif" is possible
5824 return skipwhite(p);
5825}
5826
5827/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005828 * Get a line from the compilation context, compatible with exarg_T getline().
5829 * Return a pointer to the line in allocated memory.
5830 * Return NULL for end-of-file or some error.
5831 */
5832 static char_u *
5833exarg_getline(
5834 int c UNUSED,
5835 void *cookie,
5836 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005837 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005838{
5839 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005840 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005841
Bram Moolenaar66250c92020-08-20 15:02:42 +02005842 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005843 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005844 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005845 return NULL;
5846 ++cctx->ctx_lnum;
5847 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5848 // Comment lines result in NULL pointers, skip them.
5849 if (p != NULL)
5850 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005851 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005852}
5853
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005854 void
5855fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5856{
5857 eap->getline = exarg_getline;
5858 eap->cookie = cctx;
5859}
5860
Bram Moolenaar04b12692020-05-04 23:24:44 +02005861/*
5862 * Compile a nested :def command.
5863 */
5864 static char_u *
5865compile_nested_function(exarg_T *eap, cctx_T *cctx)
5866{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005867 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005868 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005869 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005870 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005871 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005872 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005873 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005874
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005875 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005876 {
5877 emsg(_(e_cannot_use_bang_with_nested_def));
5878 return NULL;
5879 }
5880
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005881 if (*name_start == '/')
5882 {
5883 name_end = skip_regexp(name_start + 1, '/', TRUE);
5884 if (*name_end == '/')
5885 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005886 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005887 }
5888 if (name_end == name_start || *skipwhite(name_end) != '(')
5889 {
5890 if (!ends_excmd2(name_start, name_end))
5891 {
5892 semsg(_(e_invalid_command_str), eap->cmd);
5893 return NULL;
5894 }
5895
5896 // "def" or "def Name": list functions
5897 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5898 return NULL;
5899 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5900 }
5901
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005902 // Only g:Func() can use a namespace.
5903 if (name_start[1] == ':' && !is_global)
5904 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005905 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005906 return NULL;
5907 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005908 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005909 return NULL;
5910
Bram Moolenaar04b12692020-05-04 23:24:44 +02005911 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005912 fill_exarg_from_cctx(eap, cctx);
5913
Bram Moolenaar04b12692020-05-04 23:24:44 +02005914 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00005915 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005916 lambda_name = vim_strsave(get_lambda_name());
5917 if (lambda_name == NULL)
5918 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005919 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005920
Bram Moolenaar822ba242020-05-24 23:00:18 +02005921 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005922 {
5923 r = eap->skip ? OK : FAIL;
5924 goto theend;
5925 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005926
5927 // copy over the block scope IDs before compiling
5928 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5929 {
5930 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5931
5932 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5933 if (ufunc->uf_block_ids != NULL)
5934 {
5935 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5936 sizeof(int) * block_depth);
5937 ufunc->uf_block_depth = block_depth;
5938 }
5939 }
5940
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005941 compile_type = COMPILE_TYPE(ufunc);
5942#ifdef FEAT_PROFILE
5943 // If the outer function is profiled, also compile the nested function for
5944 // profiling.
5945 if (cctx->ctx_compile_type == CT_PROFILE)
5946 compile_type = CT_PROFILE;
5947#endif
5948 if (func_needs_compiling(ufunc, compile_type)
5949 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005950 {
5951 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005952 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005953 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005954
Bram Moolenaar648594e2021-07-11 17:55:01 +02005955#ifdef FEAT_PROFILE
5956 // When the outer function is compiled for profiling, the nested function
5957 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005958 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005959 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5960#endif
5961
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005962 if (is_global)
5963 {
5964 char_u *func_name = vim_strnsave(name_start + 2,
5965 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005966
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005967 if (func_name == NULL)
5968 r = FAIL;
5969 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005970 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005971 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005972 lambda_name = NULL;
5973 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005974 }
5975 else
5976 {
5977 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005978 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005979 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005980
Bram Moolenaareef21022020-08-01 22:16:43 +02005981 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005982 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005983 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005984 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005985 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5986 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005987
5988theend:
5989 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005990 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005991}
5992
5993/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994 * Return the length of an assignment operator, or zero if there isn't one.
5995 */
5996 int
5997assignment_len(char_u *p, int *heredoc)
5998{
5999 if (*p == '=')
6000 {
6001 if (p[1] == '<' && p[2] == '<')
6002 {
6003 *heredoc = TRUE;
6004 return 3;
6005 }
6006 return 1;
6007 }
6008 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
6009 return 2;
6010 if (STRNCMP(p, "..=", 3) == 0)
6011 return 3;
6012 return 0;
6013}
6014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006016 * Generate the load instruction for "name".
6017 */
6018 static void
6019generate_loadvar(
6020 cctx_T *cctx,
6021 assign_dest_T dest,
6022 char_u *name,
6023 lvar_T *lvar,
6024 type_T *type)
6025{
6026 switch (dest)
6027 {
6028 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006029 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006030 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
6031 break;
6032 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01006033 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
6034 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
6035 else
6036 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006037 break;
6038 case dest_buffer:
6039 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
6040 break;
6041 case dest_window:
6042 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
6043 break;
6044 case dest_tab:
6045 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
6046 break;
6047 case dest_script:
6048 compile_load_scriptvar(cctx,
6049 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
6050 break;
6051 case dest_env:
6052 // Include $ in the name here
6053 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
6054 break;
6055 case dest_reg:
6056 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
6057 break;
6058 case dest_vimvar:
6059 generate_LOADV(cctx, name + 2, TRUE);
6060 break;
6061 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01006062 if (lvar->lv_from_outer > 0)
6063 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
6064 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006065 else
6066 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
6067 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006068 case dest_expr:
6069 // list or dict value should already be on the stack.
6070 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006071 }
6072}
6073
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006074/*
6075 * Skip over "[expr]" or ".member".
6076 * Does not check for any errors.
6077 */
6078 static char_u *
6079skip_index(char_u *start)
6080{
6081 char_u *p = start;
6082
6083 if (*p == '[')
6084 {
6085 p = skipwhite(p + 1);
6086 (void)skip_expr(&p, NULL);
6087 p = skipwhite(p);
6088 if (*p == ']')
6089 return p + 1;
6090 return p;
6091 }
6092 // if (*p == '.')
6093 return to_name_end(p + 1, TRUE);
6094}
6095
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006096 void
6097vim9_declare_error(char_u *name)
6098{
6099 char *scope = "";
6100
6101 switch (*name)
6102 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006103 case 'g': scope = _("global"); break;
6104 case 'b': scope = _("buffer"); break;
6105 case 'w': scope = _("window"); break;
6106 case 't': scope = _("tab"); break;
6107 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006108 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006109 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006110 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006111 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006112 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006113 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006114 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006115 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006116 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006117}
6118
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006119/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006120 * For one assignment figure out the type of destination. Return it in "dest".
6121 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006122 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006123 * For a v:var "vimvaridx" is set.
6124 * "type" is set to the destination type if known, unchanted otherwise.
6125 * Return FAIL if an error message was given.
6126 */
6127 static int
6128get_var_dest(
6129 char_u *name,
6130 assign_dest_T *dest,
6131 int cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006132 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006133 int *vimvaridx,
6134 type_T **type,
6135 cctx_T *cctx)
6136{
6137 char_u *p;
6138
6139 if (*name == '&')
6140 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006141 int cc;
6142 long numval;
6143 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006144 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006145
6146 *dest = dest_option;
6147 if (cmdidx == CMD_final || cmdidx == CMD_const)
6148 {
6149 emsg(_(e_const_option));
6150 return FAIL;
6151 }
6152 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006153 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006154 if (p == NULL)
6155 {
6156 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02006157 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006158 return FAIL;
6159 }
6160 cc = *p;
6161 *p = NUL;
6162 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006163 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006164 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006165 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006166 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006167 case gov_unknown:
Bram Moolenaare1242042021-12-16 20:56:57 +00006168 semsg(_(e_unknown_option_str), name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006169 return FAIL;
6170 case gov_string:
6171 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006172 if (opt_p_flags & P_FUNC)
6173 {
6174 // might be a Funcref, check the type later
6175 *type = &t_any;
6176 *dest = dest_func_option;
6177 }
6178 else
6179 {
6180 *type = &t_string;
6181 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006182 break;
6183 case gov_bool:
6184 case gov_hidden_bool:
6185 *type = &t_bool;
6186 break;
6187 case gov_number:
6188 case gov_hidden_number:
6189 *type = &t_number;
6190 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006191 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006192 }
6193 else if (*name == '$')
6194 {
6195 *dest = dest_env;
6196 *type = &t_string;
6197 }
6198 else if (*name == '@')
6199 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006200 if (name[1] != '@'
6201 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006202 {
6203 emsg_invreg(name[1]);
6204 return FAIL;
6205 }
6206 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006207 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006208 }
6209 else if (STRNCMP(name, "g:", 2) == 0)
6210 {
6211 *dest = dest_global;
6212 }
6213 else if (STRNCMP(name, "b:", 2) == 0)
6214 {
6215 *dest = dest_buffer;
6216 }
6217 else if (STRNCMP(name, "w:", 2) == 0)
6218 {
6219 *dest = dest_window;
6220 }
6221 else if (STRNCMP(name, "t:", 2) == 0)
6222 {
6223 *dest = dest_tab;
6224 }
6225 else if (STRNCMP(name, "v:", 2) == 0)
6226 {
6227 typval_T *vtv;
6228 int di_flags;
6229
6230 *vimvaridx = find_vim_var(name + 2, &di_flags);
6231 if (*vimvaridx < 0)
6232 {
6233 semsg(_(e_variable_not_found_str), name);
6234 return FAIL;
6235 }
6236 // We use the current value of "sandbox" here, is that OK?
6237 if (var_check_ro(di_flags, name, FALSE))
6238 return FAIL;
6239 *dest = dest_vimvar;
6240 vtv = get_vim_var_tv(*vimvaridx);
6241 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6242 }
6243 return OK;
6244}
6245
6246/*
6247 * Generate a STORE instruction for "dest", not being "dest_local".
6248 * Return FAIL when out of memory.
6249 */
6250 static int
6251generate_store_var(
6252 cctx_T *cctx,
6253 assign_dest_T dest,
6254 int opt_flags,
6255 int vimvaridx,
6256 int scriptvar_idx,
6257 int scriptvar_sid,
6258 type_T *type,
6259 char_u *name)
6260{
6261 switch (dest)
6262 {
6263 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006264 return generate_STOREOPT(cctx, ISN_STOREOPT,
6265 skip_option_env_lead(name), opt_flags);
6266 case dest_func_option:
6267 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
6268 skip_option_env_lead(name), opt_flags);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006269 case dest_global:
6270 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006271 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6272 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006273 case dest_buffer:
6274 // include b: with the name, easier to execute that way
6275 return generate_STORE(cctx, ISN_STOREB, 0, name);
6276 case dest_window:
6277 // include w: with the name, easier to execute that way
6278 return generate_STORE(cctx, ISN_STOREW, 0, name);
6279 case dest_tab:
6280 // include t: with the name, easier to execute that way
6281 return generate_STORE(cctx, ISN_STORET, 0, name);
6282 case dest_env:
6283 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6284 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006285 return generate_STORE(cctx, ISN_STOREREG,
6286 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006287 case dest_vimvar:
6288 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6289 case dest_script:
6290 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006291 // "s:" may be included in the name.
6292 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006293 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006294 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6295 scriptvar_sid, scriptvar_idx, type);
6296 case dest_local:
6297 case dest_expr:
6298 // cannot happen
6299 break;
6300 }
6301 return FAIL;
6302}
6303
Bram Moolenaar752fc692021-01-04 21:57:11 +01006304 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006305generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6306{
6307 if (lhs->lhs_dest != dest_local)
6308 return generate_store_var(cctx, lhs->lhs_dest,
6309 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6310 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6311 lhs->lhs_type, lhs->lhs_name);
6312
6313 if (lhs->lhs_lvar != NULL)
6314 {
6315 garray_T *instr = &cctx->ctx_instr;
6316 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6317
6318 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6319 // ISN_STORENR
6320 if (lhs->lhs_lvar->lv_from_outer == 0
6321 && instr->ga_len == instr_count + 1
6322 && isn->isn_type == ISN_PUSHNR)
6323 {
6324 varnumber_T val = isn->isn_arg.number;
6325 garray_T *stack = &cctx->ctx_type_stack;
6326
6327 isn->isn_type = ISN_STORENR;
6328 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6329 isn->isn_arg.storenr.stnr_val = val;
6330 if (stack->ga_len > 0)
6331 --stack->ga_len;
6332 }
6333 else if (lhs->lhs_lvar->lv_from_outer > 0)
6334 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6335 lhs->lhs_lvar->lv_from_outer);
6336 else
6337 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6338 }
6339 return OK;
6340}
6341
6342 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006343is_decl_command(int cmdidx)
6344{
6345 return cmdidx == CMD_let || cmdidx == CMD_var
6346 || cmdidx == CMD_final || cmdidx == CMD_const;
6347}
6348
6349/*
6350 * Figure out the LHS type and other properties for an assignment or one item
6351 * of ":unlet" with an index.
6352 * Returns OK or FAIL.
6353 */
6354 static int
6355compile_lhs(
6356 char_u *var_start,
6357 lhs_T *lhs,
6358 int cmdidx,
6359 int heredoc,
6360 int oplen,
6361 cctx_T *cctx)
6362{
6363 char_u *var_end;
6364 int is_decl = is_decl_command(cmdidx);
6365
6366 CLEAR_POINTER(lhs);
6367 lhs->lhs_dest = dest_local;
6368 lhs->lhs_vimvaridx = -1;
6369 lhs->lhs_scriptvar_idx = -1;
6370
6371 // "dest_end" is the end of the destination, including "[expr]" or
6372 // ".name".
6373 // "var_end" is the end of the variable/option/etc. name.
6374 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6375 if (*var_start == '@')
6376 var_end = var_start + 2;
6377 else
6378 {
6379 // skip over the leading "&", "&l:", "&g:" and "$"
6380 var_end = skip_option_env_lead(var_start);
6381 var_end = to_name_end(var_end, TRUE);
6382 }
6383
6384 // "a: type" is declaring variable "a" with a type, not dict "a:".
6385 if (is_decl && lhs->lhs_dest_end == var_start + 2
6386 && lhs->lhs_dest_end[-1] == ':')
6387 --lhs->lhs_dest_end;
6388 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6389 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006390 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006391
6392 // compute the length of the destination without "[expr]" or ".name"
6393 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006394 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006395 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6396 if (lhs->lhs_name == NULL)
6397 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006398
6399 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6400 // Something follows after the variable: "var[idx]" or "var.key".
6401 lhs->lhs_has_index = TRUE;
6402
Bram Moolenaar752fc692021-01-04 21:57:11 +01006403 if (heredoc)
6404 lhs->lhs_type = &t_list_string;
6405 else
6406 lhs->lhs_type = &t_any;
6407
6408 if (cctx->ctx_skip != SKIP_YES)
6409 {
6410 int declare_error = FALSE;
6411
6412 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6413 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6414 &lhs->lhs_type, cctx) == FAIL)
6415 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006416 if (lhs->lhs_dest != dest_local
6417 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006418 {
6419 // Specific kind of variable recognized.
6420 declare_error = is_decl;
6421 }
6422 else
6423 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006424 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006425 if (check_reserved_name(lhs->lhs_name) == FAIL)
6426 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006427
6428 if (lookup_local(var_start, lhs->lhs_varlen,
6429 &lhs->lhs_local_lvar, cctx) == OK)
6430 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6431 else
6432 {
6433 CLEAR_FIELD(lhs->lhs_arg_lvar);
6434 if (arg_exists(var_start, lhs->lhs_varlen,
6435 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6436 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6437 {
6438 if (is_decl)
6439 {
6440 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6441 return FAIL;
6442 }
6443 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6444 }
6445 }
6446 if (lhs->lhs_lvar != NULL)
6447 {
6448 if (is_decl)
6449 {
6450 semsg(_(e_variable_already_declared), lhs->lhs_name);
6451 return FAIL;
6452 }
6453 }
6454 else
6455 {
6456 int script_namespace = lhs->lhs_varlen > 1
6457 && STRNCMP(var_start, "s:", 2) == 0;
6458 int script_var = (script_namespace
6459 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006460 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006461 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006462 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006463 imported_T *import =
6464 find_imported(var_start, lhs->lhs_varlen, cctx);
6465
6466 if (script_namespace || script_var || import != NULL)
6467 {
6468 char_u *rawname = lhs->lhs_name
6469 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6470
6471 if (is_decl)
6472 {
6473 if (script_namespace)
6474 semsg(_(e_cannot_declare_script_variable_in_function),
6475 lhs->lhs_name);
6476 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006477 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006478 lhs->lhs_name);
6479 return FAIL;
6480 }
6481 else if (cctx->ctx_ufunc->uf_script_ctx_version
6482 == SCRIPT_VERSION_VIM9
6483 && script_namespace
6484 && !script_var && import == NULL)
6485 {
6486 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6487 return FAIL;
6488 }
6489
6490 lhs->lhs_dest = dest_script;
6491
6492 // existing script-local variables should have a type
6493 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6494 if (import != NULL)
6495 lhs->lhs_scriptvar_sid = import->imp_sid;
6496 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6497 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006498 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006499 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006500 lhs->lhs_scriptvar_sid, rawname,
6501 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6502 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006503 if (lhs->lhs_scriptvar_idx >= 0)
6504 {
6505 scriptitem_T *si = SCRIPT_ITEM(
6506 lhs->lhs_scriptvar_sid);
6507 svar_T *sv =
6508 ((svar_T *)si->sn_var_vals.ga_data)
6509 + lhs->lhs_scriptvar_idx;
6510 lhs->lhs_type = sv->sv_type;
6511 }
6512 }
6513 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006514 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006515 == FAIL)
6516 return FAIL;
6517 }
6518 }
6519
6520 if (declare_error)
6521 {
6522 vim9_declare_error(lhs->lhs_name);
6523 return FAIL;
6524 }
6525 }
6526
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006527 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01006528 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6529 var_end = lhs->lhs_dest_end;
6530
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006531 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006532 {
6533 if (is_decl && *var_end == ':')
6534 {
6535 char_u *p;
6536
6537 // parse optional type: "let var: type = expr"
6538 if (!VIM_ISWHITE(var_end[1]))
6539 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006540 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006541 return FAIL;
6542 }
6543 p = skipwhite(var_end + 1);
6544 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6545 if (lhs->lhs_type == NULL)
6546 return FAIL;
6547 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006548 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006549 }
6550 else if (lhs->lhs_lvar != NULL)
6551 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6552 }
6553
Bram Moolenaare42939a2021-04-05 17:11:17 +02006554 if (oplen == 3 && !heredoc
6555 && lhs->lhs_dest != dest_global
6556 && !lhs->lhs_has_index
6557 && lhs->lhs_type->tt_type != VAR_STRING
6558 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006559 {
6560 emsg(_(e_can_only_concatenate_to_string));
6561 return FAIL;
6562 }
6563
6564 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6565 && cctx->ctx_skip != SKIP_YES)
6566 {
6567 if (oplen > 1 && !heredoc)
6568 {
6569 // +=, /=, etc. require an existing variable
6570 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6571 return FAIL;
6572 }
6573 if (!is_decl)
6574 {
6575 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6576 return FAIL;
6577 }
6578
Bram Moolenaar3f327882021-03-17 20:56:38 +01006579 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006580 if ((lhs->lhs_type->tt_type == VAR_FUNC
6581 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006582 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006583 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006584
6585 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006586 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6587 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6588 if (lhs->lhs_lvar == NULL)
6589 return FAIL;
6590 lhs->lhs_new_local = TRUE;
6591 }
6592
6593 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006594 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006595 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006596 char_u *after = var_start + lhs->lhs_varlen;
6597 char_u *p;
6598
Bram Moolenaar752fc692021-01-04 21:57:11 +01006599 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006600 if (is_decl)
6601 {
6602 emsg(_(e_cannot_use_index_when_declaring_variable));
6603 return FAIL;
6604 }
6605
Bram Moolenaarc750d912021-11-29 22:02:12 +00006606 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
6607 // Only the last index is used below, if there are others
6608 // before it generate code for the expression. Thus for
6609 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6610 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006611 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006612 p = skip_index(after);
6613 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01006614 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006615 lhs->lhs_varlen_total = p - var_start;
6616 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006617 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006618 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006619 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006620 if (after > var_start + lhs->lhs_varlen)
6621 {
6622 lhs->lhs_varlen = after - var_start;
6623 lhs->lhs_dest = dest_expr;
6624 // We don't know the type before evaluating the expression,
6625 // use "any" until then.
6626 lhs->lhs_type = &t_any;
6627 }
6628
6629 if (lhs->lhs_type->tt_member == NULL)
6630 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006631 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00006632 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006633 }
6634 return OK;
6635}
6636
6637/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006638 * Figure out the LHS and check a few errors.
6639 */
6640 static int
6641compile_assign_lhs(
6642 char_u *var_start,
6643 lhs_T *lhs,
6644 int cmdidx,
6645 int is_decl,
6646 int heredoc,
6647 int oplen,
6648 cctx_T *cctx)
6649{
6650 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6651 return FAIL;
6652
6653 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6654 {
6655 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6656 return FAIL;
6657 }
6658 if (!is_decl && lhs->lhs_lvar != NULL
6659 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6660 {
6661 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6662 return FAIL;
6663 }
6664 return OK;
6665}
6666
6667/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006668 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6669 */
6670 static int
6671has_list_index(char_u *idx_start, cctx_T *cctx)
6672{
6673 char_u *p = idx_start;
6674 int save_skip;
6675
6676 if (*p != '[')
6677 return FALSE;
6678
6679 p = skipwhite(p + 1);
6680 if (*p == ':')
6681 return TRUE;
6682
6683 save_skip = cctx->ctx_skip;
6684 cctx->ctx_skip = SKIP_YES;
6685 (void)compile_expr0(&p, cctx);
6686 cctx->ctx_skip = save_skip;
6687 return *skipwhite(p) == ':';
6688}
6689
6690/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006691 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6692 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006693 */
6694 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006695compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006696 char_u *var_start,
6697 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006698 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006699 cctx_T *cctx)
6700{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006701 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006702 char_u *p;
6703 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006704 int need_white_before = TRUE;
6705 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006706
Bram Moolenaar752fc692021-01-04 21:57:11 +01006707 p = var_start + varlen;
6708 if (*p == '[')
6709 {
6710 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006711 if (*p == ':')
6712 {
6713 // empty first index, push zero
6714 r = generate_PUSHNR(cctx, 0);
6715 need_white_before = FALSE;
6716 }
6717 else
6718 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006719
6720 if (r == OK && *skipwhite(p) == ':')
6721 {
6722 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006723 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006724 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006725 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006726 empty_second = *skipwhite(p + 1) == ']';
6727 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6728 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006729 {
6730 semsg(_(e_white_space_required_before_and_after_str_at_str),
6731 ":", p);
6732 return FAIL;
6733 }
6734 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006735 if (*p == ']')
6736 // empty second index, push "none"
6737 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6738 else
6739 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006740 }
6741
Bram Moolenaar752fc692021-01-04 21:57:11 +01006742 if (r == OK && *skipwhite(p) != ']')
6743 {
6744 // this should not happen
Bram Moolenaare1242042021-12-16 20:56:57 +00006745 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar752fc692021-01-04 21:57:11 +01006746 r = FAIL;
6747 }
6748 }
6749 else // if (*p == '.')
6750 {
6751 char_u *key_end = to_name_end(p + 1, TRUE);
6752 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6753
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006754 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006755 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006756 return r;
6757}
6758
6759/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006760 * For a LHS with an index, load the variable to be indexed.
6761 */
6762 static int
6763compile_load_lhs(
6764 lhs_T *lhs,
6765 char_u *var_start,
6766 type_T *rhs_type,
6767 cctx_T *cctx)
6768{
6769 if (lhs->lhs_dest == dest_expr)
6770 {
6771 size_t varlen = lhs->lhs_varlen;
6772 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006773 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006774 char_u *p = var_start;
6775 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006776 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006777
Bram Moolenaare97976b2021-08-01 13:17:17 +02006778 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6779 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006780 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006781 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6782 res = compile_expr0(&p, cctx);
6783 var_start[varlen] = c;
6784 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6785 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006786 {
6787 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006788 if (res != FAIL)
Bram Moolenaare1242042021-12-16 20:56:57 +00006789 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006790 return FAIL;
6791 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006792
6793 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6794 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6795 // now we can properly check the type
6796 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6797 && rhs_type != &t_void
6798 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6799 FALSE, FALSE) == FAIL)
6800 return FAIL;
6801 }
6802 else
6803 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6804 lhs->lhs_lvar, lhs->lhs_type);
6805 return OK;
6806}
6807
6808/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006809 * Produce code for loading "lhs" and also take care of an index.
6810 * Return OK/FAIL.
6811 */
6812 static int
6813compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6814{
6815 compile_load_lhs(lhs, var_start, NULL, cctx);
6816
6817 if (lhs->lhs_has_index)
6818 {
6819 int range = FALSE;
6820
6821 // Get member from list or dict. First compile the
6822 // index value.
6823 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6824 return FAIL;
6825 if (range)
6826 {
6827 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6828 var_start);
6829 return FAIL;
6830 }
6831
6832 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006833 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006834 return FAIL;
6835 }
6836 return OK;
6837}
6838
6839/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006840 * Assignment to a list or dict member, or ":unlet" for the item, using the
6841 * information in "lhs".
6842 * Returns OK or FAIL.
6843 */
6844 static int
6845compile_assign_unlet(
6846 char_u *var_start,
6847 lhs_T *lhs,
6848 int is_assign,
6849 type_T *rhs_type,
6850 cctx_T *cctx)
6851{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006852 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006853 garray_T *stack = &cctx->ctx_type_stack;
6854 int range = FALSE;
6855
Bram Moolenaar68452172021-04-12 21:21:02 +02006856 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006857 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006858 if (is_assign && range
6859 && lhs->lhs_type->tt_type != VAR_LIST
6860 && lhs->lhs_type != &t_blob
6861 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006862 {
6863 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6864 return FAIL;
6865 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006866
6867 if (lhs->lhs_type == &t_any)
6868 {
6869 // Index on variable of unknown type: check at runtime.
6870 dest_type = VAR_ANY;
6871 }
6872 else
6873 {
6874 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006875 if (dest_type == VAR_DICT && range)
6876 {
6877 emsg(e_cannot_use_range_with_dictionary);
6878 return FAIL;
6879 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006880 if (dest_type == VAR_DICT
6881 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006882 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006883 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006884 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006885 type_T *type;
6886
6887 if (range)
6888 {
6889 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6890 if (need_type(type, &t_number,
6891 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006892 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006893 }
6894 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006895 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006896 && need_type(type, &t_number,
6897 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006898 return FAIL;
6899 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006900 }
6901
6902 // Load the dict or list. On the stack we then have:
6903 // - value (for assignment, not for :unlet)
6904 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006905 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006906 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006907 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6908 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006909
Bram Moolenaar68452172021-04-12 21:21:02 +02006910 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6911 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006912 {
6913 if (is_assign)
6914 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006915 if (range)
6916 {
6917 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6918 return FAIL;
6919 }
6920 else
6921 {
6922 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006923
Bram Moolenaar68452172021-04-12 21:21:02 +02006924 if (isn == NULL)
6925 return FAIL;
6926 isn->isn_arg.vartype = dest_type;
6927 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006928 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006929 else if (range)
6930 {
6931 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6932 return FAIL;
6933 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006934 else
6935 {
6936 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6937 return FAIL;
6938 }
6939 }
6940 else
6941 {
6942 emsg(_(e_indexable_type_required));
6943 return FAIL;
6944 }
6945
6946 return OK;
6947}
6948
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006949/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006950 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006951 * "let name"
6952 * "var name = expr"
6953 * "final name = expr"
6954 * "const name = expr"
6955 * "name = expr"
6956 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006957 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006958 * Return NULL for an error.
6959 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006960 */
6961 static char_u *
6962compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6963{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006964 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006965 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006966 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967 char_u *ret = NULL;
6968 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006969 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006971 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006972 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006973 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975 int oplen = 0;
6976 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006977 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006978 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006980 int is_decl = is_decl_command(cmdidx);
6981 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006982 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006984 // Skip over the "var" or "[var, var]" to get to any "=".
6985 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6986 if (p == NULL)
6987 return *arg == '[' ? arg : NULL;
6988
Bram Moolenaar752fc692021-01-04 21:57:11 +01006989 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991 sp = p;
6992 p = skipwhite(p);
6993 op = p;
6994 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006995
6996 if (var_count > 0 && oplen == 0)
6997 // can be something like "[1, 2]->func()"
6998 return arg;
6999
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007000 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02007002 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007003 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007004 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007005 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
7006 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02007007 if (VIM_ISWHITE(eap->cmd[2]))
7008 {
7009 semsg(_(e_no_white_space_allowed_after_str_str),
7010 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
7011 return NULL;
7012 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007013 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
7014 oplen = 2;
7015 incdec = TRUE;
7016 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 if (heredoc)
7019 {
7020 list_T *l;
7021 listitem_T *li;
7022
7023 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02007024 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02007026 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02007027 if (l == NULL)
7028 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029
Bram Moolenaar078269b2020-09-21 20:35:55 +02007030 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02007032 // Push each line and the create the list.
7033 FOR_ALL_LIST_ITEMS(l, li)
7034 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02007035 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02007036 li->li_tv.vval.v_string = NULL;
7037 }
7038 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007039 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 list_free(l);
7041 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007042 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007044 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007046 char_u *wp;
7047
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007048 // for "[var, var] = expr" evaluate the expression here, loop over the
7049 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007050 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02007051
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007052 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01007053 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007054 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007055 if (compile_expr0(&p, cctx) == FAIL)
7056 return NULL;
7057 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007059 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007061 type_T *stacktype;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007062 int needed_list_len;
7063 int did_check = FALSE;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007064
Bram Moolenaarec5929d2020-04-07 20:53:39 +02007065 stacktype = stack->ga_len == 0 ? &t_void
7066 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007067 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007069 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007070 goto theend;
7071 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01007072 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007073 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007074 goto theend;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007075 // If a constant list was used we can check the length right here.
7076 needed_list_len = semicolon ? var_count - 1 : var_count;
7077 if (instr->ga_len > 0)
7078 {
7079 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
7080
7081 if (isn->isn_type == ISN_NEWLIST)
7082 {
7083 did_check = TRUE;
7084 if (semicolon ? isn->isn_arg.number < needed_list_len
7085 : isn->isn_arg.number != needed_list_len)
7086 {
7087 semsg(_(e_expected_nr_items_but_got_nr),
7088 needed_list_len, isn->isn_arg.number);
7089 goto theend;
7090 }
7091 }
7092 }
7093 if (!did_check)
7094 generate_CHECKLEN(cctx, needed_list_len, semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007095 if (stacktype->tt_member != NULL)
7096 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007097 }
7098 }
7099
7100 /*
7101 * Loop over variables in "[var, var] = expr".
7102 * For "var = expr" and "let var: type" this is done only once.
7103 */
7104 if (var_count > 0)
7105 var_start = skipwhite(arg + 1); // skip over the "["
7106 else
7107 var_start = arg;
7108 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
7109 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007110 int instr_count = -1;
7111 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007112
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02007113 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
7114 {
7115 // Ignore underscore in "[a, _, b] = list".
7116 if (var_count > 0)
7117 {
7118 var_start = skipwhite(var_start + 2);
7119 continue;
7120 }
7121 emsg(_(e_cannot_use_underscore_here));
7122 goto theend;
7123 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007124 vim_free(lhs.lhs_name);
7125
7126 /*
7127 * Figure out the LHS type and other properties.
7128 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007129 if (compile_assign_lhs(var_start, &lhs, cmdidx,
7130 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007131 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02007132 if (heredoc)
7133 {
7134 SOURCING_LNUM = start_lnum;
7135 if (lhs.lhs_has_type
7136 && need_type(&t_list_string, lhs.lhs_type,
7137 -1, 0, cctx, FALSE, FALSE) == FAIL)
7138 goto theend;
7139 }
7140 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007141 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007142 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007143 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007144 if (oplen > 0 && var_count == 0)
7145 {
7146 // skip over the "=" and the expression
7147 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02007148 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007149 }
7150 }
7151 else if (oplen > 0)
7152 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007153 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01007154 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007155
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007156 // for "+=", "*=", "..=" etc. first load the current value
7157 if (*op != '='
7158 && compile_load_lhs_with_index(&lhs, var_start,
7159 cctx) == FAIL)
7160 goto theend;
7161
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007162 // For "var = expr" evaluate the expression.
7163 if (var_count == 0)
7164 {
7165 int r;
7166
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007167 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007168 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007169 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007170 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007171 r = generate_PUSHNR(cctx, 1);
7172 }
7173 else
7174 {
7175 // Temporarily hide the new local variable here, it is
7176 // not available to this expression.
7177 if (lhs.lhs_new_local)
7178 --cctx->ctx_locals.ga_len;
7179 wp = op + oplen;
7180 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7181 {
7182 if (lhs.lhs_new_local)
7183 ++cctx->ctx_locals.ga_len;
7184 goto theend;
7185 }
7186 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007187 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007188 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007189 if (r == FAIL)
7190 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007191 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007192 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007193 else if (semicolon && var_idx == var_count - 1)
7194 {
7195 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007196 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007197 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7198 goto theend;
7199 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007200 else
7201 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007202 // For "[var, var] = expr" get the "var_idx" item from the
7203 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007204 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007205 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007206 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007207
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007208 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007209 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007210 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007211 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007212 if ((rhs_type->tt_type == VAR_FUNC
7213 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007214 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007215 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007216 goto theend;
7217
Bram Moolenaar752fc692021-01-04 21:57:11 +01007218 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007219 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007220 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007221 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007222 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007223 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007224 }
7225 else
7226 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007227 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007228 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007229 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007230 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007231 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007232 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007233 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007234 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007235 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007236 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007237 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007238 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007239 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007240 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007241 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007242 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007243
Bram Moolenaar77709b12021-04-03 21:01:01 +02007244 // Without operator check type here, otherwise below.
7245 // Use the line number of the assignment.
7246 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007247 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7248 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007249 // If assigning to a list or dict member, use the
7250 // member type. Not for "list[:] =".
7251 if (lhs.lhs_has_index
7252 && !has_list_index(var_start + lhs.lhs_varlen,
7253 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007254 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007255 if (need_type_where(rhs_type, use_type, -1, where,
7256 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007257 goto theend;
7258 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007259 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007260 else
7261 {
7262 type_T *lhs_type = lhs.lhs_member_type;
7263
7264 // Special case: assigning to @# can use a number or a
7265 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007266 // Also: can assign a number to a float.
7267 if ((lhs_type == &t_number_or_string
7268 || lhs_type == &t_float)
7269 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007270 lhs_type = &t_number;
7271 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007272 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007273 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007274 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007275 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007276 else if (cmdidx == CMD_final)
7277 {
7278 emsg(_(e_final_requires_a_value));
7279 goto theend;
7280 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007281 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007282 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007283 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007284 goto theend;
7285 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00007286 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option
7287 || lhs.lhs_dest == dest_func_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007288 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007289 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007290 goto theend;
7291 }
7292 else
7293 {
7294 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007295 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007296 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007297 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007298 {
7299 case VAR_BOOL:
7300 generate_PUSHBOOL(cctx, VVAL_FALSE);
7301 break;
7302 case VAR_FLOAT:
7303#ifdef FEAT_FLOAT
7304 generate_PUSHF(cctx, 0.0);
7305#endif
7306 break;
7307 case VAR_STRING:
7308 generate_PUSHS(cctx, NULL);
7309 break;
7310 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007311 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007312 break;
7313 case VAR_FUNC:
7314 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7315 break;
7316 case VAR_LIST:
7317 generate_NEWLIST(cctx, 0);
7318 break;
7319 case VAR_DICT:
7320 generate_NEWDICT(cctx, 0);
7321 break;
7322 case VAR_JOB:
7323 generate_PUSHJOB(cctx, NULL);
7324 break;
7325 case VAR_CHANNEL:
7326 generate_PUSHCHANNEL(cctx, NULL);
7327 break;
7328 case VAR_NUMBER:
7329 case VAR_UNKNOWN:
7330 case VAR_ANY:
7331 case VAR_PARTIAL:
7332 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007333 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007334 case VAR_SPECIAL: // cannot happen
7335 generate_PUSHNR(cctx, 0);
7336 break;
7337 }
7338 }
7339 if (var_count == 0)
7340 end = p;
7341 }
7342
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007343 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007344 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007345 break;
7346
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007347 if (oplen > 0 && *op != '=')
7348 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007349 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007350 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007351
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007352 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007353 {
7354 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7355 goto theend;
7356 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007357 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007358 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007359 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007360 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7361 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007362#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007363 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007364 !(expected == &t_float && (stacktype == &t_number
7365 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007366#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007367 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007368 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007369 goto theend;
7370 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007371
7372 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007373 {
7374 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7375 goto theend;
7376 }
7377 else if (*op == '+')
7378 {
7379 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007380 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007381 lhs.lhs_member_type, stacktype,
7382 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007383 goto theend;
7384 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007385 else if (generate_two_op(cctx, op) == FAIL)
7386 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007389 // Use the line number of the assignment for store instruction.
7390 save_lnum = cctx->ctx_lnum;
7391 cctx->ctx_lnum = start_lnum - 1;
7392
Bram Moolenaar752fc692021-01-04 21:57:11 +01007393 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007394 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007395 // Use the info in "lhs" to store the value at the index in the
7396 // list or dict.
7397 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7398 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007399 {
7400 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007401 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007402 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007403 }
7404 else
7405 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007406 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007407 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007408 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007409 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007410 generate_LOCKCONST(cctx);
7411
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007412 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007413 && (lhs.lhs_type->tt_type == VAR_DICT
7414 || lhs.lhs_type->tt_type == VAR_LIST)
7415 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007416 && !(lhs.lhs_type->tt_member == &t_any
7417 && oplen > 0
7418 && rhs_type != NULL
7419 && rhs_type->tt_type == lhs.lhs_type->tt_type
7420 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007421 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007422 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007423 // also in legacy script. Not for "list<any> = val", then the
7424 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007425 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007426
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007427 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007428 {
7429 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007430 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007431 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007432 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007433 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007434
7435 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00007436 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007437 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007438
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007439 // For "[var, var] = expr" drop the "expr" value.
7440 // Also for "[var, var; _] = expr".
7441 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007442 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007443 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007444 goto theend;
7445 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007446
Bram Moolenaarb2097502020-07-19 17:17:02 +02007447 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007448
7449theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007450 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007451 return ret;
7452}
7453
7454/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007455 * Check for an assignment at "eap->cmd", compile it if found.
7456 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7457 */
7458 static int
7459may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7460{
7461 char_u *pskip;
7462 char_u *p;
7463
7464 // Assuming the command starts with a variable or function name,
7465 // find what follows.
7466 // Skip over "var.member", "var[idx]" and the like.
7467 // Also "&opt = val", "$ENV = val" and "@r = val".
7468 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7469 ? eap->cmd + 1 : eap->cmd;
7470 p = to_name_end(pskip, TRUE);
7471 if (p > eap->cmd && *p != NUL)
7472 {
7473 char_u *var_end;
7474 int oplen;
7475 int heredoc;
7476
7477 if (eap->cmd[0] == '@')
7478 var_end = eap->cmd + 2;
7479 else
7480 var_end = find_name_end(pskip, NULL, NULL,
7481 FNE_CHECK_START | FNE_INCL_BR);
7482 oplen = assignment_len(skipwhite(var_end), &heredoc);
7483 if (oplen > 0)
7484 {
7485 size_t len = p - eap->cmd;
7486
7487 // Recognize an assignment if we recognize the variable
7488 // name:
7489 // "g:var = expr"
7490 // "local = expr" where "local" is a local var.
7491 // "script = expr" where "script" is a script-local var.
7492 // "import = expr" where "import" is an imported var
7493 // "&opt = expr"
7494 // "$ENV = expr"
7495 // "@r = expr"
7496 if (*eap->cmd == '&'
7497 || *eap->cmd == '$'
7498 || *eap->cmd == '@'
7499 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007500 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007501 {
7502 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7503 if (*line == NULL || *line == eap->cmd)
7504 return FAIL;
7505 return OK;
7506 }
7507 }
7508 }
7509
7510 if (*eap->cmd == '[')
7511 {
7512 // [var, var] = expr
7513 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7514 if (*line == NULL)
7515 return FAIL;
7516 if (*line != eap->cmd)
7517 return OK;
7518 }
7519 return NOTDONE;
7520}
7521
7522/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007523 * Check if "name" can be "unlet".
7524 */
7525 int
7526check_vim9_unlet(char_u *name)
7527{
7528 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7529 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007530 // "unlet s:var" is allowed in legacy script.
7531 if (*name == 's' && !script_is_vim9())
7532 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007533 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007534 return FAIL;
7535 }
7536 return OK;
7537}
7538
7539/*
7540 * Callback passed to ex_unletlock().
7541 */
7542 static int
7543compile_unlet(
7544 lval_T *lvp,
7545 char_u *name_end,
7546 exarg_T *eap,
7547 int deep UNUSED,
7548 void *coookie)
7549{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007550 cctx_T *cctx = coookie;
7551 char_u *p = lvp->ll_name;
7552 int cc = *name_end;
7553 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007554
Bram Moolenaar752fc692021-01-04 21:57:11 +01007555 if (cctx->ctx_skip == SKIP_YES)
7556 return OK;
7557
7558 *name_end = NUL;
7559 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007560 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007561 // :unlet $ENV_VAR
7562 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7563 }
7564 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7565 {
7566 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007567
Bram Moolenaar752fc692021-01-04 21:57:11 +01007568 // This is similar to assigning: lookup the list/dict, compile the
7569 // idx/key. Then instead of storing the value unlet the item.
7570 // unlet {list}[idx]
7571 // unlet {dict}[key] dict.key
7572 //
7573 // Figure out the LHS type and other properties.
7574 //
7575 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7576
7577 // : unlet an indexed item
7578 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007579 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007580 iemsg("called compile_lhs() without an index");
7581 ret = FAIL;
7582 }
7583 else
7584 {
7585 // Use the info in "lhs" to unlet the item at the index in the
7586 // list or dict.
7587 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007588 }
7589
Bram Moolenaar752fc692021-01-04 21:57:11 +01007590 vim_free(lhs.lhs_name);
7591 }
7592 else if (check_vim9_unlet(p) == FAIL)
7593 {
7594 ret = FAIL;
7595 }
7596 else
7597 {
7598 // Normal name. Only supports g:, w:, t: and b: namespaces.
7599 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007600 }
7601
Bram Moolenaar752fc692021-01-04 21:57:11 +01007602 *name_end = cc;
7603 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007604}
7605
7606/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007607 * Callback passed to ex_unletlock().
7608 */
7609 static int
7610compile_lock_unlock(
7611 lval_T *lvp,
7612 char_u *name_end,
7613 exarg_T *eap,
7614 int deep UNUSED,
7615 void *coookie)
7616{
7617 cctx_T *cctx = coookie;
7618 int cc = *name_end;
7619 char_u *p = lvp->ll_name;
7620 int ret = OK;
7621 size_t len;
7622 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007623 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007624
7625 if (cctx->ctx_skip == SKIP_YES)
7626 return OK;
7627
7628 // Cannot use :lockvar and :unlockvar on local variables.
7629 if (p[1] != ':')
7630 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007631 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007632
7633 if (lookup_local(p, end - p, NULL, cctx) == OK)
7634 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007635 char_u *s = p;
7636
7637 if (*end != '.' && *end != '[')
7638 {
7639 emsg(_(e_cannot_lock_unlock_local_variable));
7640 return FAIL;
7641 }
7642
7643 // For "d.member" put the local variable on the stack, it will be
7644 // passed to ex_lockvar() indirectly.
7645 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7646 return FAIL;
7647 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007648 }
7649 }
7650
7651 // Checking is done at runtime.
7652 *name_end = NUL;
7653 len = name_end - p + 20;
7654 buf = alloc(len);
7655 if (buf == NULL)
7656 ret = FAIL;
7657 else
7658 {
7659 vim_snprintf((char *)buf, len, "%s %s",
7660 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7661 p);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00007662 ret = generate_EXEC_copy(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007663
7664 vim_free(buf);
7665 *name_end = cc;
7666 }
7667 return ret;
7668}
7669
7670/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007671 * compile "unlet var", "lock var" and "unlock var"
7672 * "arg" points to "var".
7673 */
7674 static char_u *
7675compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7676{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007677 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7678 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7679 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007680 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7681}
7682
7683/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7685 */
7686 static int
7687compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7688{
7689 garray_T *instr = &cctx->ctx_instr;
7690 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7691
7692 if (endlabel == NULL)
7693 return FAIL;
7694 endlabel->el_next = *el;
7695 *el = endlabel;
7696 endlabel->el_end_label = instr->ga_len;
7697
7698 generate_JUMP(cctx, when, 0);
7699 return OK;
7700}
7701
7702 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007703compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704{
7705 garray_T *instr = &cctx->ctx_instr;
7706
7707 while (*el != NULL)
7708 {
7709 endlabel_T *cur = (*el);
7710 isn_T *isn;
7711
7712 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007713 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 *el = cur->el_next;
7715 vim_free(cur);
7716 }
7717}
7718
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007719 static void
7720compile_free_jump_to_end(endlabel_T **el)
7721{
7722 while (*el != NULL)
7723 {
7724 endlabel_T *cur = (*el);
7725
7726 *el = cur->el_next;
7727 vim_free(cur);
7728 }
7729}
7730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731/*
7732 * Create a new scope and set up the generic items.
7733 */
7734 static scope_T *
7735new_scope(cctx_T *cctx, scopetype_T type)
7736{
7737 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7738
7739 if (scope == NULL)
7740 return NULL;
7741 scope->se_outer = cctx->ctx_scope;
7742 cctx->ctx_scope = scope;
7743 scope->se_type = type;
7744 scope->se_local_count = cctx->ctx_locals.ga_len;
7745 return scope;
7746}
7747
7748/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007749 * Free the current scope and go back to the outer scope.
7750 */
7751 static void
7752drop_scope(cctx_T *cctx)
7753{
7754 scope_T *scope = cctx->ctx_scope;
7755
7756 if (scope == NULL)
7757 {
7758 iemsg("calling drop_scope() without a scope");
7759 return;
7760 }
7761 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007762 switch (scope->se_type)
7763 {
7764 case IF_SCOPE:
7765 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7766 case FOR_SCOPE:
7767 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7768 case WHILE_SCOPE:
7769 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7770 case TRY_SCOPE:
7771 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7772 case NO_SCOPE:
7773 case BLOCK_SCOPE:
7774 break;
7775 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007776 vim_free(scope);
7777}
7778
7779/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007780 * compile "if expr"
7781 *
7782 * "if expr" Produces instructions:
7783 * EVAL expr Push result of "expr"
7784 * JUMP_IF_FALSE end
7785 * ... body ...
7786 * end:
7787 *
7788 * "if expr | else" Produces instructions:
7789 * EVAL expr Push result of "expr"
7790 * JUMP_IF_FALSE else
7791 * ... body ...
7792 * JUMP_ALWAYS end
7793 * else:
7794 * ... body ...
7795 * end:
7796 *
7797 * "if expr1 | elseif expr2 | else" Produces instructions:
7798 * EVAL expr Push result of "expr"
7799 * JUMP_IF_FALSE elseif
7800 * ... body ...
7801 * JUMP_ALWAYS end
7802 * elseif:
7803 * EVAL expr Push result of "expr"
7804 * JUMP_IF_FALSE else
7805 * ... body ...
7806 * JUMP_ALWAYS end
7807 * else:
7808 * ... body ...
7809 * end:
7810 */
7811 static char_u *
7812compile_if(char_u *arg, cctx_T *cctx)
7813{
7814 char_u *p = arg;
7815 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007816 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007817 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007818 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007819 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820
Bram Moolenaara5565e42020-05-09 15:44:01 +02007821 CLEAR_FIELD(ppconst);
7822 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007823 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007824 clear_ppconst(&ppconst);
7825 return NULL;
7826 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007827 if (!ends_excmd2(arg, skipwhite(p)))
7828 {
7829 semsg(_(e_trailing_arg), p);
7830 return NULL;
7831 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007832 if (cctx->ctx_skip == SKIP_YES)
7833 clear_ppconst(&ppconst);
7834 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007835 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007836 int error = FALSE;
7837 int v;
7838
Bram Moolenaara5565e42020-05-09 15:44:01 +02007839 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007840 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007841 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007842 if (error)
7843 return NULL;
7844 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007845 }
7846 else
7847 {
7848 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007849 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007850 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007851 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007852 if (bool_on_stack(cctx) == FAIL)
7853 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007854 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007855
Bram Moolenaara91a7132021-03-25 21:12:15 +01007856 // CMDMOD_REV must come before the jump
7857 generate_undo_cmdmods(cctx);
7858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007859 scope = new_scope(cctx, IF_SCOPE);
7860 if (scope == NULL)
7861 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007862 scope->se_skip_save = skip_save;
7863 // "is_had_return" will be reset if any block does not end in :return
7864 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007865
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007866 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007867 {
7868 // "where" is set when ":elseif", "else" or ":endif" is found
7869 scope->se_u.se_if.is_if_label = instr->ga_len;
7870 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7871 }
7872 else
7873 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007874
Bram Moolenaarced68a02021-01-24 17:53:47 +01007875#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007876 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007877 && skip_save != SKIP_YES)
7878 {
7879 // generated a profile start, need to generate a profile end, since it
7880 // won't be done after returning
7881 cctx->ctx_skip = SKIP_NOT;
7882 generate_instr(cctx, ISN_PROF_END);
7883 cctx->ctx_skip = SKIP_YES;
7884 }
7885#endif
7886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007887 return p;
7888}
7889
7890 static char_u *
7891compile_elseif(char_u *arg, cctx_T *cctx)
7892{
7893 char_u *p = arg;
7894 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar90770b72021-11-30 20:57:38 +00007895 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007896 isn_T *isn;
7897 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007898 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007899 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007900
7901 if (scope == NULL || scope->se_type != IF_SCOPE)
7902 {
7903 emsg(_(e_elseif_without_if));
7904 return NULL;
7905 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007906 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007907 if (!cctx->ctx_had_return)
7908 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007909
Bram Moolenaarced68a02021-01-24 17:53:47 +01007910 if (cctx->ctx_skip == SKIP_NOT)
7911 {
7912 // previous block was executed, this one and following will not
7913 cctx->ctx_skip = SKIP_YES;
7914 scope->se_u.se_if.is_seen_skip_not = TRUE;
7915 }
7916 if (scope->se_u.se_if.is_seen_skip_not)
7917 {
7918 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007919 // Do not count the "elseif" for profiling and cmdmod
7920 instr->ga_len = current_instr_idx(cctx);
7921
Bram Moolenaarced68a02021-01-24 17:53:47 +01007922 skip_expr_cctx(&p, cctx);
7923 return p;
7924 }
7925
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007926 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007927 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007928 int moved_cmdmod = FALSE;
7929 int saved_debug = FALSE;
7930 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007931
7932 // Move any CMDMOD instruction to after the jump
7933 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7934 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007935 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007936 return NULL;
7937 ((isn_T *)instr->ga_data)[instr->ga_len] =
7938 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7939 --instr->ga_len;
7940 moved_cmdmod = TRUE;
7941 }
7942
Bram Moolenaar093165c2021-08-22 13:35:31 +02007943 // Remove the already generated ISN_DEBUG, it is written below the
7944 // ISN_FOR instruction.
7945 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7946 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7947 .isn_type == ISN_DEBUG)
7948 {
7949 --instr->ga_len;
7950 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7951 saved_debug = TRUE;
7952 }
7953
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007954 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007955 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007956 return NULL;
7957 // previous "if" or "elseif" jumps here
7958 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7959 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007960
Bram Moolenaara91a7132021-03-25 21:12:15 +01007961 if (moved_cmdmod)
7962 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007963
7964 if (saved_debug)
7965 {
7966 // move the debug instruction here
7967 if (GA_GROW_FAILS(instr, 1))
7968 return NULL;
7969 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7970 ++instr->ga_len;
7971 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007972 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007973
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007974 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007975 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007976 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007977 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007978 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007979#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007980 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007981 // the previous block was skipped, need to profile this line
7982 generate_instr(cctx, ISN_PROF_START);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007983#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007984 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007985 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007986 generate_instr_debug(cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007987 }
Bram Moolenaar90770b72021-11-30 20:57:38 +00007988
7989 instr_count = instr->ga_len;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007990 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007991 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007992 clear_ppconst(&ppconst);
7993 return NULL;
7994 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007995 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007996 if (!ends_excmd2(arg, skipwhite(p)))
7997 {
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007998 clear_ppconst(&ppconst);
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007999 semsg(_(e_trailing_arg), p);
8000 return NULL;
8001 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02008002 if (scope->se_skip_save == SKIP_YES)
8003 clear_ppconst(&ppconst);
8004 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02008005 {
Bram Moolenaar13106602020-10-04 16:06:05 +02008006 int error = FALSE;
8007 int v;
8008
Bram Moolenaarfad27422021-11-30 21:58:19 +00008009 // The expression result is a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02008010 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
8011 if (error)
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00008012 {
8013 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02008014 return NULL;
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00008015 }
Bram Moolenaar13106602020-10-04 16:06:05 +02008016 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02008017 clear_ppconst(&ppconst);
8018 scope->se_u.se_if.is_if_label = -1;
8019 }
8020 else
8021 {
8022 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008023 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02008024 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008025 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008026 if (bool_on_stack(cctx) == FAIL)
8027 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008028
Bram Moolenaara91a7132021-03-25 21:12:15 +01008029 // CMDMOD_REV must come before the jump
8030 generate_undo_cmdmods(cctx);
8031
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008032 // "where" is set when ":elseif", "else" or ":endif" is found
8033 scope->se_u.se_if.is_if_label = instr->ga_len;
8034 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
8035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008036
8037 return p;
8038}
8039
8040 static char_u *
8041compile_else(char_u *arg, cctx_T *cctx)
8042{
8043 char_u *p = arg;
8044 garray_T *instr = &cctx->ctx_instr;
8045 isn_T *isn;
8046 scope_T *scope = cctx->ctx_scope;
8047
8048 if (scope == NULL || scope->se_type != IF_SCOPE)
8049 {
8050 emsg(_(e_else_without_if));
8051 return NULL;
8052 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01008053 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008054 if (!cctx->ctx_had_return)
8055 scope->se_u.se_if.is_had_return = FALSE;
8056 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008057
Bram Moolenaarced68a02021-01-24 17:53:47 +01008058#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008059 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01008060 {
8061 if (cctx->ctx_skip == SKIP_NOT
8062 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8063 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02008064 // the previous block was executed, do not count "else" for
8065 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01008066 --instr->ga_len;
8067 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
8068 {
8069 // the previous block was not executed, this one will, do count the
8070 // "else" for profiling
8071 cctx->ctx_skip = SKIP_NOT;
8072 generate_instr(cctx, ISN_PROF_END);
8073 generate_instr(cctx, ISN_PROF_START);
8074 cctx->ctx_skip = SKIP_YES;
8075 }
8076 }
8077#endif
8078
8079 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008080 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008081 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008082 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008083 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008084 if (!cctx->ctx_had_return
8085 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
8086 JUMP_ALWAYS, cctx) == FAIL)
8087 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008088 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008089
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008090 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008091 {
8092 if (scope->se_u.se_if.is_if_label >= 0)
8093 {
8094 // previous "if" or "elseif" jumps here
8095 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8096 isn->isn_arg.jump.jump_where = instr->ga_len;
8097 scope->se_u.se_if.is_if_label = -1;
8098 }
8099 }
8100
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008101 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008102 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
8103 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008104
8105 return p;
8106}
8107
8108 static char_u *
8109compile_endif(char_u *arg, cctx_T *cctx)
8110{
8111 scope_T *scope = cctx->ctx_scope;
8112 ifscope_T *ifscope;
8113 garray_T *instr = &cctx->ctx_instr;
8114 isn_T *isn;
8115
Bram Moolenaarfa984412021-03-25 22:15:28 +01008116 if (misplaced_cmdmod(cctx))
8117 return NULL;
8118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008119 if (scope == NULL || scope->se_type != IF_SCOPE)
8120 {
8121 emsg(_(e_endif_without_if));
8122 return NULL;
8123 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008124 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008125 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008126 if (!cctx->ctx_had_return)
8127 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008128
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008129 if (scope->se_u.se_if.is_if_label >= 0)
8130 {
8131 // previous "if" or "elseif" jumps here
8132 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8133 isn->isn_arg.jump.jump_where = instr->ga_len;
8134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008135 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008136 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01008137
8138#ifdef FEAT_PROFILE
8139 // even when skipping we count the endif as executed, unless the block it's
8140 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02008141 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01008142 && scope->se_skip_save != SKIP_YES)
8143 {
8144 cctx->ctx_skip = SKIP_NOT;
8145 generate_instr(cctx, ISN_PROF_START);
8146 }
8147#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02008148 cctx->ctx_skip = scope->se_skip_save;
8149
8150 // If all the blocks end in :return and there is an :else then the
8151 // had_return flag is set.
8152 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008154 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155 return arg;
8156}
8157
8158/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01008159 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008160 *
8161 * Produces instructions:
8162 * PUSHNR -1
8163 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01008164 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008165 * top: FOR loop-idx, end Increment index, use list on bottom of stack
8166 * - if beyond end, jump to "end"
8167 * - otherwise get item from list and push it
8168 * STORE var Store item in "var"
8169 * ... body ...
8170 * JUMP top Jump back to repeat
8171 * end: DROP Drop the result of "expr"
8172 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008173 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
8174 * UNPACK 2 Split item in 2
8175 * STORE var1 Store item in "var1"
8176 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177 */
8178 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008179compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008180{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008181 char_u *arg;
8182 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008183 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008184 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008185 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008186 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008187 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008188 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008190 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008191 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008192 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008193 lvar_T *loop_lvar; // loop iteration variable
8194 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008195 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008196 type_T *item_type = &t_any;
8197 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008198 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008199
Bram Moolenaar792f7862020-11-23 08:31:18 +01008200 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008201 if (p == NULL)
8202 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008203 if (var_count == 0)
8204 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008205 else
8206 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008207
8208 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008209 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008210 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8211 return NULL;
8212 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008213 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008214 if (*p == ':' && wp != p)
8215 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8216 else
8217 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008218 return NULL;
8219 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008220 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008221 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8222 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008223
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008224 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8225 // instruction.
8226 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8227 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8228 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008229 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008230 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008231 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8232 .isn_arg.debug.dbg_break_lnum;
8233 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008235 scope = new_scope(cctx, FOR_SCOPE);
8236 if (scope == NULL)
8237 return NULL;
8238
Bram Moolenaar792f7862020-11-23 08:31:18 +01008239 // Reserve a variable to store the loop iteration counter and initialize it
8240 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008241 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8242 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008243 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008244 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008245 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008246 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008247 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008248 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008249
8250 // compile "expr", it remains on the stack until "endfor"
8251 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008252 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008253 {
8254 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008255 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008256 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008257 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008258
rbtnnbebf0692021-08-21 17:26:50 +02008259 if (cctx->ctx_skip != SKIP_YES)
8260 {
8261 // If we know the type of "var" and it is a not a supported type we can
8262 // give an error now.
8263 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8264 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008265 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008266 {
rbtnnbebf0692021-08-21 17:26:50 +02008267 semsg(_(e_for_loop_on_str_not_supported),
8268 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008269 drop_scope(cctx);
8270 return NULL;
8271 }
rbtnnbebf0692021-08-21 17:26:50 +02008272
8273 if (vartype->tt_type == VAR_STRING)
8274 item_type = &t_string;
8275 else if (vartype->tt_type == VAR_BLOB)
8276 item_type = &t_number;
8277 else if (vartype->tt_type == VAR_LIST
8278 && vartype->tt_member->tt_type != VAR_ANY)
8279 {
8280 if (!var_list)
8281 item_type = vartype->tt_member;
8282 else if (vartype->tt_member->tt_type == VAR_LIST
8283 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
rbtnnbebf0692021-08-21 17:26:50 +02008284 item_type = vartype->tt_member->tt_member;
8285 }
8286
8287 // CMDMOD_REV must come before the FOR instruction.
8288 generate_undo_cmdmods(cctx);
8289
8290 // "for_end" is set when ":endfor" is found
8291 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8292
8293 generate_FOR(cctx, loop_lvar->lv_idx);
8294
8295 arg = arg_start;
8296 if (var_list)
8297 {
8298 generate_UNPACK(cctx, var_count, semicolon);
8299 arg = skipwhite(arg + 1); // skip white after '['
8300
8301 // the list item is replaced by a number of items
8302 if (GA_GROW_FAILS(stack, var_count - 1))
8303 {
8304 drop_scope(cctx);
8305 return NULL;
8306 }
8307 --stack->ga_len;
8308 for (idx = 0; idx < var_count; ++idx)
8309 {
8310 ((type_T **)stack->ga_data)[stack->ga_len] =
8311 (semicolon && idx == 0) ? vartype : item_type;
8312 ++stack->ga_len;
8313 }
8314 }
8315
Bram Moolenaar792f7862020-11-23 08:31:18 +01008316 for (idx = 0; idx < var_count; ++idx)
8317 {
rbtnnbebf0692021-08-21 17:26:50 +02008318 assign_dest_T dest = dest_local;
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008319 int opt_flags = 0;
8320 int vimvaridx = -1;
rbtnnbebf0692021-08-21 17:26:50 +02008321 type_T *type = &t_any;
8322 type_T *lhs_type = &t_any;
8323 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008324
rbtnnbebf0692021-08-21 17:26:50 +02008325 p = skip_var_one(arg, FALSE);
8326 varlen = p - arg;
8327 name = vim_strnsave(arg, varlen);
8328 if (name == NULL)
8329 goto failed;
8330 if (*p == ':')
8331 {
8332 p = skipwhite(p + 1);
8333 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8334 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008335
Bram Moolenaarfad27422021-11-30 21:58:19 +00008336 // Script var is not supported.
rbtnnbebf0692021-08-21 17:26:50 +02008337 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008338 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008339 goto failed;
8340 if (dest != dest_local)
8341 {
8342 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008343 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008344 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008345 }
rbtnnbebf0692021-08-21 17:26:50 +02008346 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008347 {
rbtnnbebf0692021-08-21 17:26:50 +02008348 // Assigning to "_": drop the value.
8349 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8350 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008351 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008352 else
rbtnnbebf0692021-08-21 17:26:50 +02008353 {
Mike Williamscc9d7252021-11-23 12:35:57 +00008354 if (!valid_varname(arg, (int)varlen, FALSE))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008355 goto failed;
rbtnnbebf0692021-08-21 17:26:50 +02008356 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8357 {
8358 semsg(_(e_variable_already_declared), arg);
8359 goto failed;
8360 }
8361
8362 if (STRNCMP(name, "s:", 2) == 0)
8363 {
8364 semsg(_(e_cannot_declare_script_variable_in_function), name);
8365 goto failed;
8366 }
8367
8368 // Reserve a variable to store "var".
8369 where.wt_index = var_list ? idx + 1 : 0;
8370 where.wt_variable = TRUE;
8371 if (lhs_type == &t_any)
8372 lhs_type = item_type;
8373 else if (item_type != &t_unknown
8374 && (item_type == &t_any
8375 ? need_type(item_type, lhs_type,
8376 -1, 0, cctx, FALSE, FALSE)
8377 : check_type(lhs_type, item_type, TRUE, where))
8378 == FAIL)
8379 goto failed;
8380 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8381 if (var_lvar == NULL)
8382 // out of memory or used as an argument
8383 goto failed;
8384
8385 if (semicolon && idx == var_count - 1)
8386 var_lvar->lv_type = vartype;
8387 else
8388 var_lvar->lv_type = item_type;
8389 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8390 }
8391
8392 if (*p == ',' || *p == ';')
8393 ++p;
8394 arg = skipwhite(p);
8395 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008396 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008397
rbtnnbebf0692021-08-21 17:26:50 +02008398 if (cctx->ctx_compile_type == CT_DEBUG)
8399 {
8400 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008401
rbtnnbebf0692021-08-21 17:26:50 +02008402 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8403 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8404 cctx->ctx_prev_lnum = prev_lnum;
8405 generate_instr_debug(cctx);
8406 cctx->ctx_prev_lnum = save_prev_lnum;
8407 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008408 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008409
Bram Moolenaar792f7862020-11-23 08:31:18 +01008410 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008411
8412failed:
8413 vim_free(name);
8414 drop_scope(cctx);
8415 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008416}
8417
8418/*
8419 * compile "endfor"
8420 */
8421 static char_u *
8422compile_endfor(char_u *arg, cctx_T *cctx)
8423{
8424 garray_T *instr = &cctx->ctx_instr;
8425 scope_T *scope = cctx->ctx_scope;
8426 forscope_T *forscope;
8427 isn_T *isn;
8428
Bram Moolenaarfa984412021-03-25 22:15:28 +01008429 if (misplaced_cmdmod(cctx))
8430 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008432 if (scope == NULL || scope->se_type != FOR_SCOPE)
8433 {
8434 emsg(_(e_for));
8435 return NULL;
8436 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008437 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008438 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008439 if (cctx->ctx_skip != SKIP_YES)
8440 {
8441 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008442
rbtnnbebf0692021-08-21 17:26:50 +02008443 // At end of ":for" scope jump back to the FOR instruction.
8444 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008445
rbtnnbebf0692021-08-21 17:26:50 +02008446 // Fill in the "end" label in the FOR statement so it can jump here.
8447 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8448 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008449
rbtnnbebf0692021-08-21 17:26:50 +02008450 // Fill in the "end" label any BREAK statements
8451 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008452
rbtnnbebf0692021-08-21 17:26:50 +02008453 // Below the ":for" scope drop the "expr" list from the stack.
8454 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8455 return NULL;
8456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008457
8458 vim_free(scope);
8459
8460 return arg;
8461}
8462
8463/*
8464 * compile "while expr"
8465 *
8466 * Produces instructions:
8467 * top: EVAL expr Push result of "expr"
8468 * JUMP_IF_FALSE end jump if false
8469 * ... body ...
8470 * JUMP top Jump back to repeat
8471 * end:
8472 *
8473 */
8474 static char_u *
8475compile_while(char_u *arg, cctx_T *cctx)
8476{
8477 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008478 scope_T *scope;
8479
8480 scope = new_scope(cctx, WHILE_SCOPE);
8481 if (scope == NULL)
8482 return NULL;
8483
Bram Moolenaara91a7132021-03-25 21:12:15 +01008484 // "endwhile" jumps back here, one before when profiling or using cmdmods
8485 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008486
8487 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008488 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008489 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008490
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008491 if (!ends_excmd2(arg, skipwhite(p)))
8492 {
8493 semsg(_(e_trailing_arg), p);
8494 return NULL;
8495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008496
rbtnnd895b1d2021-08-20 20:54:25 +02008497 if (cctx->ctx_skip != SKIP_YES)
8498 {
8499 if (bool_on_stack(cctx) == FAIL)
8500 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008501
rbtnnd895b1d2021-08-20 20:54:25 +02008502 // CMDMOD_REV must come before the jump
8503 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008504
rbtnnd895b1d2021-08-20 20:54:25 +02008505 // "while_end" is set when ":endwhile" is found
8506 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008507 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008508 return FAIL;
8509 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008510
8511 return p;
8512}
8513
8514/*
8515 * compile "endwhile"
8516 */
8517 static char_u *
8518compile_endwhile(char_u *arg, cctx_T *cctx)
8519{
8520 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008521 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008522
Bram Moolenaarfa984412021-03-25 22:15:28 +01008523 if (misplaced_cmdmod(cctx))
8524 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008525 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8526 {
8527 emsg(_(e_while));
8528 return NULL;
8529 }
8530 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008531 if (cctx->ctx_skip != SKIP_YES)
8532 {
8533 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008534
Bram Moolenaarf002a412021-01-24 13:34:18 +01008535#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008536 // count the endwhile before jumping
8537 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008538#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008539
rbtnnd895b1d2021-08-20 20:54:25 +02008540 // At end of ":for" scope jump back to the FOR instruction.
8541 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008542
rbtnnd895b1d2021-08-20 20:54:25 +02008543 // Fill in the "end" label in the WHILE statement so it can jump here.
8544 // And in any jumps for ":break"
8545 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008546 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008548
8549 vim_free(scope);
8550
8551 return arg;
8552}
8553
8554/*
8555 * compile "continue"
8556 */
8557 static char_u *
8558compile_continue(char_u *arg, cctx_T *cctx)
8559{
8560 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008561 int try_scopes = 0;
8562 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008563
8564 for (;;)
8565 {
8566 if (scope == NULL)
8567 {
8568 emsg(_(e_continue));
8569 return NULL;
8570 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008571 if (scope->se_type == FOR_SCOPE)
8572 {
8573 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008574 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008575 }
8576 if (scope->se_type == WHILE_SCOPE)
8577 {
8578 loop_label = scope->se_u.se_while.ws_top_label;
8579 break;
8580 }
8581 if (scope->se_type == TRY_SCOPE)
8582 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008583 scope = scope->se_outer;
8584 }
8585
Bram Moolenaarc150c092021-02-13 15:02:46 +01008586 if (try_scopes > 0)
8587 // Inside one or more try/catch blocks we first need to jump to the
8588 // "finally" or "endtry" to cleanup.
8589 generate_TRYCONT(cctx, try_scopes, loop_label);
8590 else
8591 // Jump back to the FOR or WHILE instruction.
8592 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008594 return arg;
8595}
8596
8597/*
8598 * compile "break"
8599 */
8600 static char_u *
8601compile_break(char_u *arg, cctx_T *cctx)
8602{
8603 scope_T *scope = cctx->ctx_scope;
8604 endlabel_T **el;
8605
8606 for (;;)
8607 {
8608 if (scope == NULL)
8609 {
8610 emsg(_(e_break));
8611 return NULL;
8612 }
8613 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8614 break;
8615 scope = scope->se_outer;
8616 }
8617
8618 // Jump to the end of the FOR or WHILE loop.
8619 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008620 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008621 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008622 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008623 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8624 return FAIL;
8625
8626 return arg;
8627}
8628
8629/*
8630 * compile "{" start of block
8631 */
8632 static char_u *
8633compile_block(char_u *arg, cctx_T *cctx)
8634{
8635 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8636 return NULL;
8637 return skipwhite(arg + 1);
8638}
8639
8640/*
8641 * compile end of block: drop one scope
8642 */
8643 static void
8644compile_endblock(cctx_T *cctx)
8645{
8646 scope_T *scope = cctx->ctx_scope;
8647
8648 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008649 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008650 vim_free(scope);
8651}
8652
8653/*
Bram Moolenaar003312b2021-12-20 10:55:35 +00008654 * Compile "try".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008655 * Creates a new scope for the try-endtry, pointing to the first catch and
8656 * finally.
8657 * Creates another scope for the "try" block itself.
8658 * TRY instruction sets up exception handling at runtime.
8659 *
8660 * "try"
8661 * TRY -> catch1, -> finally push trystack entry
8662 * ... try block
8663 * "throw {exception}"
8664 * EVAL {exception}
8665 * THROW create exception
8666 * ... try block
8667 * " catch {expr}"
8668 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008669 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008670 * EVAL {expr}
8671 * MATCH
8672 * JUMP nomatch -> catch2
8673 * CATCH remove exception
8674 * ... catch block
8675 * " catch"
8676 * JUMP -> finally
8677 * catch2: CATCH remove exception
8678 * ... catch block
8679 * " finally"
8680 * finally:
8681 * ... finally block
8682 * " endtry"
8683 * ENDTRY pop trystack entry, may rethrow
8684 */
8685 static char_u *
8686compile_try(char_u *arg, cctx_T *cctx)
8687{
8688 garray_T *instr = &cctx->ctx_instr;
8689 scope_T *try_scope;
8690 scope_T *scope;
8691
Bram Moolenaarfa984412021-03-25 22:15:28 +01008692 if (misplaced_cmdmod(cctx))
8693 return NULL;
8694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008695 // scope that holds the jumps that go to catch/finally/endtry
8696 try_scope = new_scope(cctx, TRY_SCOPE);
8697 if (try_scope == NULL)
8698 return NULL;
8699
Bram Moolenaar69f70502021-01-01 16:10:46 +01008700 if (cctx->ctx_skip != SKIP_YES)
8701 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008702 isn_T *isn;
8703
8704 // "try_catch" is set when the first ":catch" is found or when no catch
8705 // is found and ":finally" is found.
8706 // "try_finally" is set when ":finally" is found
8707 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008708 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008709 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8710 return NULL;
8711 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8712 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008713 return NULL;
8714 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008715
8716 // scope for the try block itself
8717 scope = new_scope(cctx, BLOCK_SCOPE);
8718 if (scope == NULL)
8719 return NULL;
8720
8721 return arg;
8722}
8723
8724/*
Bram Moolenaar003312b2021-12-20 10:55:35 +00008725 * Compile "catch {expr}".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008726 */
8727 static char_u *
8728compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8729{
8730 scope_T *scope = cctx->ctx_scope;
8731 garray_T *instr = &cctx->ctx_instr;
8732 char_u *p;
8733 isn_T *isn;
8734
Bram Moolenaarfa984412021-03-25 22:15:28 +01008735 if (misplaced_cmdmod(cctx))
8736 return NULL;
8737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008738 // end block scope from :try or :catch
8739 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8740 compile_endblock(cctx);
8741 scope = cctx->ctx_scope;
8742
8743 // Error if not in a :try scope
8744 if (scope == NULL || scope->se_type != TRY_SCOPE)
8745 {
8746 emsg(_(e_catch));
8747 return NULL;
8748 }
8749
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008750 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008751 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008752 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008753 return NULL;
8754 }
8755
Bram Moolenaar69f70502021-01-01 16:10:46 +01008756 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008757 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008758#ifdef FEAT_PROFILE
8759 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008760 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008761 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008762 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008763 .isn_type == ISN_PROF_START)
8764 --instr->ga_len;
8765#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008766 // Jump from end of previous block to :finally or :endtry
8767 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8768 JUMP_ALWAYS, cctx) == FAIL)
8769 return NULL;
8770
8771 // End :try or :catch scope: set value in ISN_TRY instruction
8772 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008773 if (isn->isn_arg.try.try_ref->try_catch == 0)
8774 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008775 if (scope->se_u.se_try.ts_catch_label != 0)
8776 {
8777 // Previous catch without match jumps here
8778 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8779 isn->isn_arg.jump.jump_where = instr->ga_len;
8780 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008781#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008782 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008783 {
8784 // a "throw" that jumps here needs to be counted
8785 generate_instr(cctx, ISN_PROF_END);
8786 // the "catch" is also counted
8787 generate_instr(cctx, ISN_PROF_START);
8788 }
8789#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008790 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008791 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008792 }
8793
8794 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008795 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008796 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008797 scope->se_u.se_try.ts_caught_all = TRUE;
8798 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008799 }
8800 else
8801 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008802 char_u *end;
8803 char_u *pat;
8804 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008805 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008806 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008808 // Push v:exception, push {expr} and MATCH
8809 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8810
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008811 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008812 if (*end != *p)
8813 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008814 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008815 vim_free(tofree);
8816 return FAIL;
8817 }
8818 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008819 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008820 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008821 len = (int)(end - tofree);
8822 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008823 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008824 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008825 if (pat == NULL)
8826 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008827 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008828 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008830 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8831 return NULL;
8832
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008833 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008834 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8835 return NULL;
8836 }
8837
Bram Moolenaar69f70502021-01-01 16:10:46 +01008838 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008839 return NULL;
8840
8841 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8842 return NULL;
8843 return p;
8844}
8845
8846 static char_u *
8847compile_finally(char_u *arg, cctx_T *cctx)
8848{
8849 scope_T *scope = cctx->ctx_scope;
8850 garray_T *instr = &cctx->ctx_instr;
8851 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008852 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008853
Bram Moolenaarfa984412021-03-25 22:15:28 +01008854 if (misplaced_cmdmod(cctx))
8855 return NULL;
8856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008857 // end block scope from :try or :catch
8858 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8859 compile_endblock(cctx);
8860 scope = cctx->ctx_scope;
8861
8862 // Error if not in a :try scope
8863 if (scope == NULL || scope->se_type != TRY_SCOPE)
8864 {
8865 emsg(_(e_finally));
8866 return NULL;
8867 }
8868
rbtnn84934992021-08-07 13:26:53 +02008869 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008870 {
rbtnn84934992021-08-07 13:26:53 +02008871 // End :catch or :finally scope: set value in ISN_TRY instruction
8872 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8873 if (isn->isn_arg.try.try_ref->try_finally != 0)
8874 {
8875 emsg(_(e_finally_dup));
8876 return NULL;
8877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008878
rbtnn84934992021-08-07 13:26:53 +02008879 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008880#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008881 if (cctx->ctx_compile_type == CT_PROFILE
8882 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008883 .isn_type == ISN_PROF_START)
rbtnn84934992021-08-07 13:26:53 +02008884 {
8885 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008886 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008887
8888 // jump to the profile end above it
8889 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008890 .isn_type == ISN_PROF_END)
rbtnn84934992021-08-07 13:26:53 +02008891 --this_instr;
8892 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008893#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008894
rbtnn84934992021-08-07 13:26:53 +02008895 // Fill in the "end" label in jumps at the end of the blocks.
8896 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarfad27422021-11-30 21:58:19 +00008897 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008898
rbtnn84934992021-08-07 13:26:53 +02008899 // If there is no :catch then an exception jumps to :finally.
8900 if (isn->isn_arg.try.try_ref->try_catch == 0)
8901 isn->isn_arg.try.try_ref->try_catch = this_instr;
8902 isn->isn_arg.try.try_ref->try_finally = this_instr;
8903 if (scope->se_u.se_try.ts_catch_label != 0)
8904 {
8905 // Previous catch without match jumps here
8906 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8907 isn->isn_arg.jump.jump_where = this_instr;
8908 scope->se_u.se_try.ts_catch_label = 0;
8909 }
8910 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8911 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008912 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008913
8914 return arg;
8915}
8916
8917 static char_u *
8918compile_endtry(char_u *arg, cctx_T *cctx)
8919{
8920 scope_T *scope = cctx->ctx_scope;
8921 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008922 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008923
Bram Moolenaarfa984412021-03-25 22:15:28 +01008924 if (misplaced_cmdmod(cctx))
8925 return NULL;
8926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008927 // end block scope from :catch or :finally
8928 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8929 compile_endblock(cctx);
8930 scope = cctx->ctx_scope;
8931
8932 // Error if not in a :try scope
8933 if (scope == NULL || scope->se_type != TRY_SCOPE)
8934 {
8935 if (scope == NULL)
8936 emsg(_(e_no_endtry));
8937 else if (scope->se_type == WHILE_SCOPE)
8938 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008939 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008940 emsg(_(e_endfor));
8941 else
8942 emsg(_(e_endif));
8943 return NULL;
8944 }
8945
Bram Moolenaarc150c092021-02-13 15:02:46 +01008946 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008947 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008948 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008949 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8950 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008951 {
8952 emsg(_(e_missing_catch_or_finally));
8953 return NULL;
8954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008955
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008956#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008957 if (cctx->ctx_compile_type == CT_PROFILE
8958 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008959 .isn_type == ISN_PROF_START)
8960 // move the profile start after "endtry" so that it's not counted when
8961 // the exception is rethrown.
8962 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008963#endif
8964
Bram Moolenaar69f70502021-01-01 16:10:46 +01008965 // Fill in the "end" label in jumps at the end of the blocks, if not
8966 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008967 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8968 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008969
Bram Moolenaar69f70502021-01-01 16:10:46 +01008970 if (scope->se_u.se_try.ts_catch_label != 0)
8971 {
8972 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008973 isn_T *isn = ((isn_T *)instr->ga_data)
8974 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008975 isn->isn_arg.jump.jump_where = instr->ga_len;
8976 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008977 }
8978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008979 compile_endblock(cctx);
8980
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008981 if (cctx->ctx_skip != SKIP_YES)
8982 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008983 // End :catch or :finally scope: set instruction index in ISN_TRY
8984 // instruction
8985 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008986 if (cctx->ctx_skip != SKIP_YES
8987 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8988 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008989#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008990 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008991 generate_instr(cctx, ISN_PROF_START);
8992#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008994 return arg;
8995}
8996
8997/*
8998 * compile "throw {expr}"
8999 */
9000 static char_u *
9001compile_throw(char_u *arg, cctx_T *cctx UNUSED)
9002{
9003 char_u *p = skipwhite(arg);
9004
Bram Moolenaara5565e42020-05-09 15:44:01 +02009005 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009006 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01009007 if (cctx->ctx_skip == SKIP_YES)
9008 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009009 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009010 return NULL;
9011 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
9012 return NULL;
9013
9014 return p;
9015}
9016
Bram Moolenaarc3235272021-07-10 19:42:03 +02009017 static char_u *
9018compile_eval(char_u *arg, cctx_T *cctx)
9019{
9020 char_u *p = arg;
9021 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02009022 long lnum = SOURCING_LNUM;
9023
9024 // find_ex_command() will consider a variable name an expression, assuming
9025 // that something follows on the next line. Check that something actually
9026 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02009027 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02009028
Bram Moolenaarc3235272021-07-10 19:42:03 +02009029 if (compile_expr0(&p, cctx) == FAIL)
9030 return NULL;
9031
9032 if (name_only && lnum == SOURCING_LNUM)
9033 {
9034 semsg(_(e_expression_without_effect_str), arg);
9035 return NULL;
9036 }
9037
9038 // drop the result
9039 generate_instr_drop(cctx, ISN_DROP, 1);
9040
9041 return skipwhite(p);
9042}
9043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009044/*
9045 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009046 * compile "echomsg expr"
9047 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02009048 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01009049 * compile "execute expr"
9050 */
9051 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009052compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009053{
9054 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01009055 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009056 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009057 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009058 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009059 garray_T *stack = &cctx->ctx_type_stack;
9060 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009061
9062 for (;;)
9063 {
Bram Moolenaare4984292020-12-13 14:19:25 +01009064 if (ends_excmd2(prev, p))
9065 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009066 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009067 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009068 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009069
9070 if (cctx->ctx_skip != SKIP_YES)
9071 {
9072 // check for non-void type
9073 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
9074 if (type->tt_type == VAR_VOID)
9075 {
9076 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
9077 return NULL;
9078 }
9079 }
9080
Bram Moolenaarad39c092020-02-26 18:23:43 +01009081 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02009082 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009083 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009084 }
9085
Bram Moolenaare4984292020-12-13 14:19:25 +01009086 if (count > 0)
9087 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009088 long save_lnum = cctx->ctx_lnum;
9089
9090 // Use the line number where the command started.
9091 cctx->ctx_lnum = start_ctx_lnum;
9092
Bram Moolenaare4984292020-12-13 14:19:25 +01009093 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
9094 generate_ECHO(cctx, cmdidx == CMD_echo, count);
9095 else if (cmdidx == CMD_execute)
9096 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
9097 else if (cmdidx == CMD_echomsg)
9098 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02009099 else if (cmdidx == CMD_echoconsole)
9100 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01009101 else
9102 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009103
9104 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01009105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009106 return p;
9107}
9108
9109/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01009110 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01009111 * instruction to compute it and return OK.
9112 * Otherwise return FAIL, the caller must deal with any range.
9113 */
9114 static int
9115compile_variable_range(exarg_T *eap, cctx_T *cctx)
9116{
9117 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
9118 char_u *p = skipdigits(eap->cmd);
9119
9120 if (p == range_end)
9121 return FAIL;
9122 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
9123}
9124
9125/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009126 * :put r
9127 * :put ={expr}
9128 */
9129 static char_u *
9130compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
9131{
9132 char_u *line = arg;
9133 linenr_T lnum;
9134 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009135 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009136
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009137 eap->regname = *line;
9138
9139 if (eap->regname == '=')
9140 {
9141 char_u *p = line + 1;
9142
9143 if (compile_expr0(&p, cctx) == FAIL)
9144 return NULL;
9145 line = p;
9146 }
9147 else if (eap->regname != NUL)
9148 ++line;
9149
Bram Moolenaar08597872020-12-10 19:43:40 +01009150 if (compile_variable_range(eap, cctx) == OK)
9151 {
9152 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
9153 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009154 else
Bram Moolenaar08597872020-12-10 19:43:40 +01009155 {
9156 // Either no range or a number.
9157 // "errormsg" will not be set because the range is ADDR_LINES.
9158 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01009159 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01009160 return NULL;
9161 if (eap->addr_count == 0)
9162 lnum = -1;
9163 else
9164 lnum = eap->line2;
9165 if (above)
9166 --lnum;
9167 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009168
9169 generate_PUT(cctx, eap->regname, lnum);
9170 return line;
9171}
9172
9173/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009174 * A command that is not compiled, execute with legacy code.
9175 */
9176 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009177compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009178{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009179 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009180 char_u *p;
9181 int has_expr = FALSE;
9182 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009183 char_u *tofree = NULL;
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009184 char_u *cmd_arg = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009185
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009186 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009187 goto theend;
9188
Bram Moolenaare729ce22021-06-06 21:38:09 +02009189 // If there was a prececing command modifier, drop it and include it in the
9190 // EXEC command.
9191 if (cctx->ctx_has_cmdmod)
9192 {
9193 garray_T *instr = &cctx->ctx_instr;
9194 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9195
9196 if (isn->isn_type == ISN_CMDMOD)
9197 {
9198 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9199 ->cmod_filter_regmatch.regprog);
9200 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9201 --instr->ga_len;
9202 cctx->ctx_has_cmdmod = FALSE;
9203 }
9204 }
9205
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009206 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009207 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009208 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009209 int usefilter = FALSE;
9210
9211 has_expr = argt & (EX_XFILE | EX_EXPAND);
9212
9213 // If the command can be followed by a bar, find the bar and truncate
9214 // it, so that the following command can be compiled.
9215 // The '|' is overwritten with a NUL, it is put back below.
9216 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9217 && *eap->arg == '!')
9218 // :w !filter or :r !filter or :r! filter
9219 usefilter = TRUE;
9220 if ((argt & EX_TRLBAR) && !usefilter)
9221 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009222 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009223 separate_nextcmd(eap);
9224 if (eap->nextcmd != NULL)
9225 nextcmd = eap->nextcmd;
9226 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009227 else if (eap->cmdidx == CMD_wincmd)
9228 {
9229 p = eap->arg;
9230 if (*p != NUL)
9231 ++p;
9232 if (*p == 'g' || *p == Ctrl_G)
9233 ++p;
9234 p = skipwhite(p);
9235 if (*p == '|')
9236 {
9237 *p = NUL;
9238 nextcmd = p + 1;
9239 }
9240 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009241 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9242 {
9243 // If there is a trailing '{' read lines until the '}'
9244 p = eap->arg + STRLEN(eap->arg) - 1;
9245 while (p > eap->arg && VIM_ISWHITE(*p))
9246 --p;
9247 if (*p == '{')
9248 {
9249 exarg_T ea;
9250 int flags; // unused
9251 int start_lnum = SOURCING_LNUM;
9252
9253 CLEAR_FIELD(ea);
9254 ea.arg = eap->arg;
9255 fill_exarg_from_cctx(&ea, cctx);
9256 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9257 if (tofree != NULL)
9258 {
9259 *p = NUL;
9260 line = concat_str(line, tofree);
9261 if (line == NULL)
9262 goto theend;
9263 vim_free(tofree);
9264 tofree = line;
9265 SOURCING_LNUM = start_lnum;
9266 }
9267 }
9268 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009269 }
9270
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009271 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9272 {
9273 // expand filename in "syntax include [@group] filename"
9274 has_expr = TRUE;
9275 eap->arg = skipwhite(eap->arg + 7);
9276 if (*eap->arg == '@')
9277 eap->arg = skiptowhite(eap->arg);
9278 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009279
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009280 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9281 && STRLEN(eap->arg) > 4)
9282 {
9283 int delim = *eap->arg;
9284
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009285 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009286 if (*p == delim)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009287 cmd_arg = p + 1;
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009288 }
9289
Bram Moolenaarecac5912021-01-05 19:23:28 +01009290 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009291 cmd_arg = eap->arg;
9292
9293 if (cmd_arg != NULL)
Bram Moolenaarecac5912021-01-05 19:23:28 +01009294 {
Bram Moolenaarfad27422021-11-30 21:58:19 +00009295 exarg_T nea;
9296
9297 CLEAR_FIELD(nea);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009298 nea.cmd = cmd_arg;
Bram Moolenaarfad27422021-11-30 21:58:19 +00009299 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009300 if (nea.cmdidx < CMD_SIZE)
Bram Moolenaarfad27422021-11-30 21:58:19 +00009301 {
9302 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
9303 if (has_expr)
9304 eap->arg = skiptowhite(eap->arg);
9305 }
Bram Moolenaarecac5912021-01-05 19:23:28 +01009306 }
9307
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009308 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009309 {
9310 int count = 0;
9311 char_u *start = skipwhite(line);
9312
9313 // :cmd xxx`=expr1`yyy`=expr2`zzz
9314 // PUSHS ":cmd xxx"
9315 // eval expr1
9316 // PUSHS "yyy"
9317 // eval expr2
9318 // PUSHS "zzz"
9319 // EXECCONCAT 5
9320 for (;;)
9321 {
9322 if (p > start)
9323 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009324 char_u *val = vim_strnsave(start, p - start);
9325
9326 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009327 ++count;
9328 }
9329 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009330 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009331 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009332 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009333 ++count;
9334 p = skipwhite(p);
9335 if (*p != '`')
9336 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009337 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009338 return NULL;
9339 }
9340 start = p + 1;
9341
9342 p = (char_u *)strstr((char *)start, "`=");
9343 if (p == NULL)
9344 {
9345 if (*skipwhite(start) != NUL)
9346 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009347 char_u *val = vim_strsave(start);
9348
9349 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009350 ++count;
9351 }
9352 break;
9353 }
9354 }
9355 generate_EXECCONCAT(cctx, count);
9356 }
9357 else
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009358 generate_EXEC_copy(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009359
9360theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009361 if (*nextcmd != NUL)
9362 {
9363 // the parser expects a pointer to the bar, put it back
9364 --nextcmd;
9365 *nextcmd = '|';
9366 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009367 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009368
9369 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009370}
9371
Bram Moolenaar20677332021-06-06 17:02:53 +02009372/*
9373 * A script command with heredoc, e.g.
9374 * ruby << EOF
9375 * command
9376 * EOF
9377 * Has been turned into one long line with NL characters by
9378 * get_function_body():
9379 * ruby << EOF<NL> command<NL>EOF
9380 */
9381 static char_u *
9382compile_script(char_u *line, cctx_T *cctx)
9383{
9384 if (cctx->ctx_skip != SKIP_YES)
9385 {
9386 isn_T *isn;
9387
9388 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9389 return NULL;
9390 isn->isn_arg.string = vim_strsave(line);
9391 }
9392 return (char_u *)"";
9393}
9394
Bram Moolenaar8238f082021-04-20 21:10:48 +02009395
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009396/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009397 * :s/pat/repl/
9398 */
9399 static char_u *
9400compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9401{
9402 char_u *cmd = eap->arg;
9403 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9404
9405 if (expr != NULL)
9406 {
9407 int delimiter = *cmd++;
9408
9409 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009410 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009411 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9412 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009413 garray_T save_ga = cctx->ctx_instr;
9414 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009415 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009416 int trailing_error;
9417 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009418 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009419 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009420
9421 cmd += 3;
9422 end = skip_substitute(cmd, delimiter);
9423
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009424 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009425 // labels are correct.
9426 cctx->ctx_instr.ga_len = 0;
9427 cctx->ctx_instr.ga_maxlen = 0;
9428 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009429 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009430 if (end[-1] == NUL)
9431 end[-1] = delimiter;
9432 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009433 trailing_error = *cmd != delimiter && *cmd != NUL;
9434
Bram Moolenaar169502f2021-04-21 12:19:35 +02009435 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009436 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009437 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009438 if (trailing_error)
9439 semsg(_(e_trailing_arg), cmd);
9440 clear_instr_ga(&cctx->ctx_instr);
9441 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009442 return NULL;
9443 }
9444
Bram Moolenaar8238f082021-04-20 21:10:48 +02009445 // Move the generated instructions into the ISN_SUBSTITUTE
9446 // instructions, then restore the list of instructions before
9447 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009448 instr_count = cctx->ctx_instr.ga_len;
9449 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009450 instr[instr_count].isn_type = ISN_FINISH;
9451
9452 cctx->ctx_instr = save_ga;
9453 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9454 {
9455 int idx;
9456
9457 for (idx = 0; idx < instr_count; ++idx)
9458 delete_instr(instr + idx);
9459 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009460 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009461 }
9462 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9463 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009464
9465 // skip over flags
9466 if (*end == '&')
9467 ++end;
9468 while (ASCII_ISALPHA(*end) || *end == '#')
9469 ++end;
9470 return end;
9471 }
9472 }
9473
9474 return compile_exec(arg, eap, cctx);
9475}
9476
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009477 static char_u *
9478compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9479{
Bram Moolenaar003312b2021-12-20 10:55:35 +00009480 char_u *arg = eap->arg;
9481 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009482
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009483 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009484 {
9485 if (STRNCMP(arg, "END", 3) == 0)
9486 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009487 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009488 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009489 // First load the current variable value.
9490 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9491 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009492 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009493 }
9494
9495 // Gets the redirected text and put it on the stack, then store it
9496 // in the variable.
9497 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9498
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009499 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009500 generate_instr_drop(cctx, ISN_CONCAT, 1);
9501
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009502 if (lhs->lhs_has_index)
9503 {
9504 // Use the info in "lhs" to store the value at the index in the
9505 // list or dict.
9506 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9507 &t_string, cctx) == FAIL)
9508 return NULL;
9509 }
9510 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009511 return NULL;
9512
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009513 VIM_CLEAR(lhs->lhs_name);
9514 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009515 return arg + 3;
9516 }
9517 emsg(_(e_cannot_nest_redir));
9518 return NULL;
9519 }
9520
9521 if (arg[0] == '=' && arg[1] == '>')
9522 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009523 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009524
9525 // redirect to a variable is compiled
9526 arg += 2;
9527 if (*arg == '>')
9528 {
9529 ++arg;
9530 append = TRUE;
9531 }
9532 arg = skipwhite(arg);
9533
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009534 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009535 FALSE, FALSE, 1, cctx) == FAIL)
9536 return NULL;
Bram Moolenaar003312b2021-12-20 10:55:35 +00009537 if (need_type(&t_string, lhs->lhs_member_type,
9538 -1, 0, cctx, FALSE, FALSE) == FAIL)
9539 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009540 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009541 lhs->lhs_append = append;
9542 if (lhs->lhs_has_index)
9543 {
9544 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9545 if (lhs->lhs_whole == NULL)
9546 return NULL;
9547 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009548
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009549 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009550 }
9551
9552 // other redirects are handled like at script level
9553 return compile_exec(line, eap, cctx);
9554}
9555
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009556#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009557 static char_u *
9558compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9559{
9560 isn_T *isn;
9561 char_u *p;
9562
9563 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9564 if (isn == NULL)
9565 return NULL;
9566 isn->isn_arg.number = eap->cmdidx;
9567
9568 p = eap->arg;
9569 if (compile_expr0(&p, cctx) == FAIL)
9570 return NULL;
9571
9572 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9573 if (isn == NULL)
9574 return NULL;
9575 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9576 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9577 return NULL;
9578 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9579 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9580 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9581
9582 return p;
9583}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009584#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009585
Bram Moolenaar4c137212021-04-19 16:48:48 +02009586/*
Bram Moolenaar7b829262021-10-13 15:04:34 +01009587 * Check if the separator for a :global or :substitute command is OK.
9588 */
9589 int
9590check_global_and_subst(char_u *cmd, char_u *arg)
9591{
Bram Moolenaar7f320922021-10-13 15:28:28 +01009592 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
Bram Moolenaar7b829262021-10-13 15:04:34 +01009593 {
9594 semsg(_(e_separator_not_supported_str), arg);
9595 return FAIL;
9596 }
9597 if (VIM_ISWHITE(cmd[1]))
9598 {
9599 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
9600 return FAIL;
9601 }
9602 return OK;
9603}
9604
9605
9606/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009607 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009608 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009609 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009610 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009611add_def_function(ufunc_T *ufunc)
9612{
9613 dfunc_T *dfunc;
9614
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009615 if (def_functions.ga_len == 0)
9616 {
9617 // The first position is not used, so that a zero uf_dfunc_idx means it
9618 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009619 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009620 return FAIL;
9621 ++def_functions.ga_len;
9622 }
9623
Bram Moolenaar09689a02020-05-09 22:50:08 +02009624 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009625 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009626 return FAIL;
9627 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9628 CLEAR_POINTER(dfunc);
9629 dfunc->df_idx = def_functions.ga_len;
9630 ufunc->uf_dfunc_idx = dfunc->df_idx;
9631 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009632 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009633 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009634 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009635 ++def_functions.ga_len;
9636 return OK;
9637}
9638
9639/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009640 * After ex_function() has collected all the function lines: parse and compile
9641 * the lines into instructions.
9642 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009643 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9644 * the return statement (used for lambda). When uf_ret_type is already set
9645 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009646 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009647 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009648 * This can be used recursively through compile_lambda(), which may reallocate
9649 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009650 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009651 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009652 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009653compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009654 ufunc_T *ufunc,
9655 int check_return_type,
9656 compiletype_T compile_type,
9657 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009658{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009659 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009660 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009661 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009662 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009663 cctx_T cctx;
9664 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009665 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009666 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009667 int ret = FAIL;
9668 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009669 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009670 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009671 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009672 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009673#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009674 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009675#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009676 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009677
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009678 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009679 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009680 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009681 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009682 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9683 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009684 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009685
9686 switch (compile_type)
9687 {
9688 case CT_PROFILE:
9689#ifdef FEAT_PROFILE
9690 instr_dest = dfunc->df_instr_prof; break;
9691#endif
9692 case CT_NONE: instr_dest = dfunc->df_instr; break;
9693 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9694 }
9695 if (instr_dest != NULL)
9696 // Was compiled in this mode before: Free old instructions.
9697 delete_def_function_contents(dfunc, FALSE);
9698 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009699 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009700 else
9701 {
9702 if (add_def_function(ufunc) == FAIL)
9703 return FAIL;
9704 new_def_function = TRUE;
9705 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009706
Bram Moolenaar985116a2020-07-12 17:31:09 +02009707 ufunc->uf_def_status = UF_COMPILING;
9708
Bram Moolenaara80faa82020-04-12 19:37:17 +02009709 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009710
Bram Moolenaare99d4222021-06-13 14:01:26 +02009711 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009712 cctx.ctx_ufunc = ufunc;
9713 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009714 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009715 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9716 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9717 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9718 cctx.ctx_type_list = &ufunc->uf_type_list;
9719 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9720 instr = &cctx.ctx_instr;
9721
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009722 // Set the context to the function, it may be compiled when called from
9723 // another script. Set the script version to the most modern one.
9724 // The line number will be set in next_line_from_context().
9725 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009726 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9727
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009728 // Don't use the flag from ":legacy" here.
9729 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9730
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009731 // Make sure error messages are OK.
9732 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9733 if (do_estack_push)
9734 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009735 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009736
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009737 if (ufunc->uf_def_args.ga_len > 0)
9738 {
9739 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009740 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009741 int i;
9742 char_u *arg;
9743 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009744 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009745
9746 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009747 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009748 for (i = 0; i < count; ++i)
9749 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009750 garray_T *stack = &cctx.ctx_type_stack;
9751 type_T *val_type;
9752 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009753 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009754 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009755 int jump_instr_idx = instr->ga_len;
9756 isn_T *isn;
9757
9758 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9759 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9760 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009761
9762 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009763 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009764
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009765 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009766 r = compile_expr0(&arg, &cctx);
9767
Bram Moolenaar12bce952021-03-11 20:04:04 +01009768 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009769 goto erret;
9770
9771 // If no type specified use the type of the default value.
9772 // Otherwise check that the default value type matches the
9773 // specified type.
9774 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009775 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009776 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009777 {
9778 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009779 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009780 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009781 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009782 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009783 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009784
9785 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009786 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009787
9788 // set instruction index in JUMP_IF_ARG_SET to here
9789 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9790 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009791 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009792
9793 if (did_set_arg_type)
9794 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009795 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009796 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009797
9798 /*
9799 * Loop over all the lines of the function and generate instructions.
9800 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009801 for (;;)
9802 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009803 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009804 int starts_with_colon = FALSE;
9805 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009806 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009807
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009808 // Bail out on the first error to avoid a flood of errors and report
9809 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009810 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009811 goto erret;
9812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009813 if (line != NULL && *line == '|')
9814 // the line continues after a '|'
9815 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009816 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009817 && !(*line == '#' && (line == cctx.ctx_line_start
9818 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009819 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009820 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009821 goto erret;
9822 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009823 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9824 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009825 else
9826 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009827 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009828 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009829 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009830 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009831#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009832 if (cctx.ctx_skip != SKIP_YES)
9833 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009834#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009835 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009836 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009837 // Make a copy, splitting off nextcmd and removing trailing spaces
9838 // may change it.
9839 if (line != NULL)
9840 {
9841 line = vim_strsave(line);
9842 vim_free(line_to_free);
9843 line_to_free = line;
9844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009845 }
9846
Bram Moolenaara80faa82020-04-12 19:37:17 +02009847 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009848 ea.cmdlinep = &line;
9849 ea.cmd = skipwhite(line);
9850
Bram Moolenaarb2049902021-01-24 12:53:53 +01009851 if (*ea.cmd == '#')
9852 {
9853 // "#" starts a comment
9854 line = (char_u *)"";
9855 continue;
9856 }
9857
Bram Moolenaarf002a412021-01-24 13:34:18 +01009858#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009859 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9860 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009861 {
9862 may_generate_prof_end(&cctx, prof_lnum);
9863
9864 prof_lnum = cctx.ctx_lnum;
9865 generate_instr(&cctx, ISN_PROF_START);
9866 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009867#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009868 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9869 && cctx.ctx_skip != SKIP_YES)
9870 {
9871 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009872 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009873 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009874 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009875
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009876 // Some things can be recognized by the first character.
9877 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009878 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009879 case '}':
9880 {
9881 // "}" ends a block scope
9882 scopetype_T stype = cctx.ctx_scope == NULL
9883 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009884
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009885 if (stype == BLOCK_SCOPE)
9886 {
9887 compile_endblock(&cctx);
9888 line = ea.cmd;
9889 }
9890 else
9891 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009892 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009893 goto erret;
9894 }
9895 if (line != NULL)
9896 line = skipwhite(ea.cmd + 1);
9897 continue;
9898 }
9899
9900 case '{':
9901 // "{" starts a block scope
9902 // "{'a': 1}->func() is something else
9903 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9904 {
9905 line = compile_block(ea.cmd, &cctx);
9906 continue;
9907 }
9908 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009909 }
9910
9911 /*
9912 * COMMAND MODIFIERS
9913 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009914 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009915 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9916 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009917 {
9918 if (errormsg != NULL)
9919 goto erret;
9920 // empty line or comment
9921 line = (char_u *)"";
9922 continue;
9923 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009924 generate_cmdmods(&cctx, &local_cmdmod);
9925 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009926
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009927 // Check if there was a colon after the last command modifier or before
9928 // the current position.
9929 for (p = ea.cmd; p >= line; --p)
9930 {
9931 if (*p == ':')
9932 starts_with_colon = TRUE;
9933 if (p < ea.cmd && !VIM_ISWHITE(*p))
9934 break;
9935 }
9936
Bram Moolenaarce024c32021-06-26 13:00:49 +02009937 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009938 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009939 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009940 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009941 if (checkforcmd(&ea.cmd, "call", 3))
9942 {
9943 if (*ea.cmd == '(')
9944 // not for "call()"
9945 ea.cmd = p;
9946 else
9947 ea.cmd = skipwhite(ea.cmd);
9948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009949
Bram Moolenaarce024c32021-06-26 13:00:49 +02009950 if (!starts_with_colon)
9951 {
9952 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009953
Bram Moolenaarce024c32021-06-26 13:00:49 +02009954 // Check for assignment after command modifiers.
9955 assign = may_compile_assignment(&ea, &line, &cctx);
9956 if (assign == OK)
9957 goto nextline;
9958 if (assign == FAIL)
9959 goto erret;
9960 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009961 }
9962
9963 /*
9964 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009965 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009966 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009967 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009968 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009969 cmd = ea.cmd;
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009970 if ((*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009971 && (starts_with_colon || !(*cmd == '\''
9972 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009973 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009974 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009975 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009976 {
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009977 if (!starts_with_colon
9978 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009979 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009980 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009981 goto erret;
9982 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009983 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009984 if (ends_excmd2(line, ea.cmd))
9985 {
9986 // A range without a command: jump to the line.
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009987 generate_EXEC(&cctx, ISN_EXECRANGE,
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009988 vim_strnsave(cmd, ea.cmd - cmd));
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009989 line = ea.cmd;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009990 goto nextline;
9991 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009992 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009993 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009994 p = find_ex_command(&ea, NULL,
9995 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009996 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009997
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009998 if (p == NULL)
9999 {
10000 if (cctx.ctx_skip != SKIP_YES)
10001 emsg(_(e_ambiguous_use_of_user_defined_command));
10002 goto erret;
10003 }
10004
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +020010005 // When using ":legacy cmd" always use compile_exec().
10006 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010007 {
10008 char_u *start = ea.cmd;
10009
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +020010010 switch (ea.cmdidx)
10011 {
10012 case CMD_if:
10013 case CMD_elseif:
10014 case CMD_else:
10015 case CMD_endif:
10016 case CMD_for:
10017 case CMD_endfor:
10018 case CMD_continue:
10019 case CMD_break:
10020 case CMD_while:
10021 case CMD_endwhile:
10022 case CMD_try:
10023 case CMD_catch:
10024 case CMD_finally:
10025 case CMD_endtry:
10026 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
10027 goto erret;
10028 default: break;
10029 }
10030
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010031 // ":legacy return expr" needs to be handled differently.
10032 if (checkforcmd(&start, "return", 4))
10033 ea.cmdidx = CMD_return;
10034 else
10035 ea.cmdidx = CMD_legacy;
10036 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +020010037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010038 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
10039 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +020010040 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010041 {
10042 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +010010043 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010044 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +020010045 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010046 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +010010047 // CMD_var cannot happen, compile_assignment() above would be
10048 // used. Most likely an assignment to a non-existing variable.
10049 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010050 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010051 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010052 }
10053
Bram Moolenaar3988f642020-08-27 22:43:03 +020010054 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010055 && ea.cmdidx != CMD_elseif
10056 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +020010057 && ea.cmdidx != CMD_endif
10058 && ea.cmdidx != CMD_endfor
10059 && ea.cmdidx != CMD_endwhile
10060 && ea.cmdidx != CMD_catch
10061 && ea.cmdidx != CMD_finally
10062 && ea.cmdidx != CMD_endtry)
10063 {
Bram Moolenaar3988f642020-08-27 22:43:03 +020010064 emsg(_(e_unreachable_code_after_return));
10065 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +020010066 }
10067
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010068 p = skipwhite(p);
10069 if (ea.cmdidx != CMD_SIZE
10070 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
10071 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +020010072 if (ea.cmdidx >= 0)
10073 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010074 if ((ea.argt & EX_BANG) && *p == '!')
10075 {
10076 ea.forceit = TRUE;
10077 p = skipwhite(p + 1);
10078 }
10079 }
10080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010081 switch (ea.cmdidx)
10082 {
10083 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +000010084 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +020010085 ea.arg = p;
10086 line = compile_nested_function(&ea, &cctx);
10087 break;
10088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010089 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010090 line = compile_return(p, check_return_type,
10091 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010092 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010093 break;
10094
10095 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +020010096 emsg(_(e_cannot_use_let_in_vim9_script));
10097 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +020010098 case CMD_var:
10099 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010100 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +020010101 case CMD_increment:
10102 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010103 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +020010104 if (line == p)
10105 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010106 break;
10107
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010108 case CMD_unlet:
10109 case CMD_unlockvar:
10110 case CMD_lockvar:
10111 line = compile_unletlock(p, &ea, &cctx);
10112 break;
10113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010114 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +020010115 emsg(_(e_import_can_only_be_used_in_script));
10116 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010117 break;
10118
10119 case CMD_if:
10120 line = compile_if(p, &cctx);
10121 break;
10122 case CMD_elseif:
10123 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010124 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010125 break;
10126 case CMD_else:
10127 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010128 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010129 break;
10130 case CMD_endif:
10131 line = compile_endif(p, &cctx);
10132 break;
10133
10134 case CMD_while:
10135 line = compile_while(p, &cctx);
10136 break;
10137 case CMD_endwhile:
10138 line = compile_endwhile(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
10142 case CMD_for:
10143 line = compile_for(p, &cctx);
10144 break;
10145 case CMD_endfor:
10146 line = compile_endfor(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_continue:
10150 line = compile_continue(p, &cctx);
10151 break;
10152 case CMD_break:
10153 line = compile_break(p, &cctx);
10154 break;
10155
10156 case CMD_try:
10157 line = compile_try(p, &cctx);
10158 break;
10159 case CMD_catch:
10160 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010161 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010162 break;
10163 case CMD_finally:
10164 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010165 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010166 break;
10167 case CMD_endtry:
10168 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010169 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010170 break;
10171 case CMD_throw:
10172 line = compile_throw(p, &cctx);
10173 break;
10174
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010175 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +020010176 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010177 break;
10178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010179 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010180 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +010010181 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010182 case CMD_echomsg:
10183 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010184 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010185 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +010010186 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010187
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010188 case CMD_put:
10189 ea.cmd = cmd;
10190 line = compile_put(p, &ea, &cctx);
10191 break;
10192
Bram Moolenaar4c137212021-04-19 16:48:48 +020010193 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +010010194 if (check_global_and_subst(ea.cmd, p) == FAIL)
10195 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +020010196 if (cctx.ctx_skip == SKIP_YES)
10197 line = (char_u *)"";
10198 else
10199 {
10200 ea.arg = p;
10201 line = compile_substitute(line, &ea, &cctx);
10202 }
10203 break;
10204
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010205 case CMD_redir:
10206 ea.arg = p;
10207 line = compile_redir(line, &ea, &cctx);
10208 break;
10209
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010210 case CMD_cexpr:
10211 case CMD_lexpr:
10212 case CMD_caddexpr:
10213 case CMD_laddexpr:
10214 case CMD_cgetexpr:
10215 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010216#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010217 ea.arg = p;
10218 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010219#else
10220 ex_ni(&ea);
10221 line = NULL;
10222#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010223 break;
10224
Bram Moolenaarae616492020-07-28 20:07:27 +020010225 case CMD_append:
10226 case CMD_change:
10227 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010228 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010229 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010230 case CMD_xit:
10231 not_in_vim9(&ea);
10232 goto erret;
10233
Bram Moolenaar002262f2020-07-08 17:47:57 +020010234 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010235 if (cctx.ctx_skip != SKIP_YES)
10236 {
10237 semsg(_(e_invalid_command_str), ea.cmd);
10238 goto erret;
10239 }
10240 // We don't check for a next command here.
10241 line = (char_u *)"";
10242 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010243
Bram Moolenaar20677332021-06-06 17:02:53 +020010244 case CMD_lua:
10245 case CMD_mzscheme:
10246 case CMD_perl:
10247 case CMD_py3:
10248 case CMD_python3:
10249 case CMD_python:
10250 case CMD_pythonx:
10251 case CMD_ruby:
10252 case CMD_tcl:
10253 ea.arg = p;
10254 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010255 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010256 else
10257 // heredoc lines have been concatenated with NL
10258 // characters in get_function_body()
10259 line = compile_script(line, &cctx);
10260 break;
10261
Bram Moolenaar7b829262021-10-13 15:04:34 +010010262 case CMD_global:
10263 if (check_global_and_subst(ea.cmd, p) == FAIL)
10264 goto erret;
10265 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +020010266 default:
10267 // Not recognized, execute with do_cmdline_cmd().
10268 ea.arg = p;
10269 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010270 break;
10271 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010272nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010273 if (line == NULL)
10274 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010275 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010276
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010277 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010278 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010279
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010280 if (cctx.ctx_type_stack.ga_len < 0)
10281 {
10282 iemsg("Type stack underflow");
10283 goto erret;
10284 }
10285 }
10286
10287 if (cctx.ctx_scope != NULL)
10288 {
10289 if (cctx.ctx_scope->se_type == IF_SCOPE)
10290 emsg(_(e_endif));
10291 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10292 emsg(_(e_endwhile));
10293 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10294 emsg(_(e_endfor));
10295 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010296 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010297 goto erret;
10298 }
10299
Bram Moolenaarefd88552020-06-18 20:50:10 +020010300 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010301 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010302 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10303 ufunc->uf_ret_type = &t_void;
10304 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010305 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010306 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010307 goto erret;
10308 }
10309
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010310 // Return void if there is no return at the end.
10311 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010312 }
10313
Bram Moolenaar599410c2021-04-10 14:03:43 +020010314 // When compiled with ":silent!" and there was an error don't consider the
10315 // function compiled.
10316 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010317 {
10318 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10319 + ufunc->uf_dfunc_idx;
10320 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010321 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010322#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010323 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010324 {
10325 dfunc->df_instr_prof = instr->ga_data;
10326 dfunc->df_instr_prof_count = instr->ga_len;
10327 }
10328 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010329#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010330 if (cctx.ctx_compile_type == CT_DEBUG)
10331 {
10332 dfunc->df_instr_debug = instr->ga_data;
10333 dfunc->df_instr_debug_count = instr->ga_len;
10334 }
10335 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010336 {
10337 dfunc->df_instr = instr->ga_data;
10338 dfunc->df_instr_count = instr->ga_len;
10339 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010340 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010341 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010342 if (cctx.ctx_outer_used)
10343 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010344 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010345 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010346
10347 ret = OK;
10348
10349erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010350 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010351 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010352 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10353 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010354
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010355 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010356 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010357 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010358 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010359
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010360 // If using the last entry in the table and it was added above, we
10361 // might as well remove it.
10362 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010363 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010364 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010365 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010366 ufunc->uf_dfunc_idx = 0;
10367 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010368 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010369
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010370 while (cctx.ctx_scope != NULL)
10371 drop_scope(&cctx);
10372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010373 if (errormsg != NULL)
10374 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010375 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010376 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010377 }
10378
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010379 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10380 {
10381 if (ret == OK)
10382 {
10383 emsg(_(e_missing_redir_end));
10384 ret = FAIL;
10385 }
10386 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010387 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010388 }
10389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010390 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010391 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010392 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010393 if (do_estack_push)
10394 estack_pop();
10395
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010396 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010397 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010398 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010399 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010400 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010401}
10402
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010403 void
10404set_function_type(ufunc_T *ufunc)
10405{
10406 int varargs = ufunc->uf_va_name != NULL;
10407 int argcount = ufunc->uf_args.ga_len;
10408
10409 // Create a type for the function, with the return type and any
10410 // argument types.
10411 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10412 // The type is included in "tt_args".
10413 if (argcount > 0 || varargs)
10414 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010415 if (ufunc->uf_type_list.ga_itemsize == 0)
10416 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010417 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10418 argcount, &ufunc->uf_type_list);
10419 // Add argument types to the function type.
10420 if (func_type_add_arg_types(ufunc->uf_func_type,
10421 argcount + varargs,
10422 &ufunc->uf_type_list) == FAIL)
10423 return;
10424 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10425 ufunc->uf_func_type->tt_min_argcount =
10426 argcount - ufunc->uf_def_args.ga_len;
10427 if (ufunc->uf_arg_types == NULL)
10428 {
10429 int i;
10430
10431 // lambda does not have argument types.
10432 for (i = 0; i < argcount; ++i)
10433 ufunc->uf_func_type->tt_args[i] = &t_any;
10434 }
10435 else
10436 mch_memmove(ufunc->uf_func_type->tt_args,
10437 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10438 if (varargs)
10439 {
10440 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010441 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010442 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10443 }
10444 }
10445 else
10446 // No arguments, can use a predefined type.
10447 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10448 argcount, &ufunc->uf_type_list);
10449}
10450
10451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010452/*
10453 * Delete an instruction, free what it contains.
10454 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010455 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010456delete_instr(isn_T *isn)
10457{
10458 switch (isn->isn_type)
10459 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010460 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010461 case ISN_EXEC:
Bram Moolenaare4eed8c2021-12-01 15:22:56 +000010462 case ISN_EXECRANGE:
Bram Moolenaar20677332021-06-06 17:02:53 +020010463 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010464 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010465 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010466 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010467 case ISN_LOADENV:
10468 case ISN_LOADG:
10469 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010470 case ISN_LOADT:
10471 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010472 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010473 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010474 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010475 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010476 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010477 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010478 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010479 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010480 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010481 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010482 case ISN_STOREW:
10483 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010484 vim_free(isn->isn_arg.string);
10485 break;
10486
Bram Moolenaar4c137212021-04-19 16:48:48 +020010487 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010488 {
10489 int idx;
10490 isn_T *list = isn->isn_arg.subs.subs_instr;
10491
10492 vim_free(isn->isn_arg.subs.subs_cmd);
10493 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10494 delete_instr(list + idx);
10495 vim_free(list);
10496 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010497 break;
10498
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010499 case ISN_INSTR:
10500 {
10501 int idx;
10502 isn_T *list = isn->isn_arg.instr;
10503
10504 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10505 delete_instr(list + idx);
10506 vim_free(list);
10507 }
10508 break;
10509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010510 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010511 case ISN_STORES:
10512 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010513 break;
10514
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010515 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010516 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010517 vim_free(isn->isn_arg.unlet.ul_name);
10518 break;
10519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010520 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +000010521 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010522 vim_free(isn->isn_arg.storeopt.so_name);
10523 break;
10524
10525 case ISN_PUSHBLOB: // push blob isn_arg.blob
10526 blob_unref(isn->isn_arg.blob);
10527 break;
10528
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010529 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010530#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010531 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010532#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010533 break;
10534
10535 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010536#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010537 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010538#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010539 break;
10540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010541 case ISN_UCALL:
10542 vim_free(isn->isn_arg.ufunc.cuf_name);
10543 break;
10544
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010545 case ISN_FUNCREF:
10546 {
Bram Moolenaar38453522021-11-28 22:00:12 +000010547 if (isn->isn_arg.funcref.fr_func_name == NULL)
10548 {
10549 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10550 + isn->isn_arg.funcref.fr_dfunc_idx;
10551 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010552
Bram Moolenaar38453522021-11-28 22:00:12 +000010553 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10554 func_ptr_unref(ufunc);
10555 }
10556 else
10557 {
10558 char_u *name = isn->isn_arg.funcref.fr_func_name;
10559
10560 if (name != NULL)
10561 func_unref(name);
10562 vim_free(isn->isn_arg.funcref.fr_func_name);
10563 }
Bram Moolenaara05e5242020-09-19 18:19:19 +020010564 }
10565 break;
10566
10567 case ISN_DCALL:
10568 {
10569 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10570 + isn->isn_arg.dfunc.cdf_idx;
10571
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010572 if (dfunc->df_ufunc != NULL
10573 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010574 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010575 }
10576 break;
10577
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010578 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010579 {
10580 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10581 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10582
10583 if (ufunc != NULL)
10584 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010585 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010586 func_ptr_unref(ufunc);
10587 }
10588
10589 vim_free(lambda);
10590 vim_free(isn->isn_arg.newfunc.nf_global);
10591 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010592 break;
10593
Bram Moolenaar5e654232020-09-16 15:22:00 +020010594 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010595 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010596 free_type(isn->isn_arg.type.ct_type);
10597 break;
10598
Bram Moolenaar02194d22020-10-24 23:08:38 +020010599 case ISN_CMDMOD:
10600 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10601 ->cmod_filter_regmatch.regprog);
10602 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10603 break;
10604
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010605 case ISN_LOADSCRIPT:
10606 case ISN_STORESCRIPT:
10607 vim_free(isn->isn_arg.script.scriptref);
10608 break;
10609
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010610 case ISN_TRY:
10611 vim_free(isn->isn_arg.try.try_ref);
10612 break;
10613
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010614 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010615 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010616 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10617 break;
10618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010619 case ISN_2BOOL:
10620 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010621 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010622 case ISN_ADDBLOB:
10623 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010624 case ISN_ANYINDEX:
10625 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010626 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010627 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010628 case ISN_BLOBINDEX:
10629 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010630 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010631 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010632 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010633 case ISN_CHECKNR:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010634 case ISN_CLEARDICT:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010635 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010636 case ISN_COMPAREANY:
10637 case ISN_COMPAREBLOB:
10638 case ISN_COMPAREBOOL:
10639 case ISN_COMPAREDICT:
10640 case ISN_COMPAREFLOAT:
10641 case ISN_COMPAREFUNC:
10642 case ISN_COMPARELIST:
10643 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010644 case ISN_COMPARESPECIAL:
10645 case ISN_COMPARESTRING:
10646 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010647 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010648 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010649 case ISN_DROP:
10650 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010651 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010652 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010653 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010654 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010655 case ISN_EXECCONCAT:
10656 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010657 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010658 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010659 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010660 case ISN_GETITEM:
10661 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010662 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010663 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010664 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010665 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010666 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010667 case ISN_LOADBDICT:
10668 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010669 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010670 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010671 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010672 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010673 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010674 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010675 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010676 case ISN_NEGATENR:
10677 case ISN_NEWDICT:
10678 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010679 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010680 case ISN_OPFLOAT:
10681 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010682 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010683 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010684 case ISN_PROF_END:
10685 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010686 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010687 case ISN_PUSHF:
10688 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010689 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010690 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010691 case ISN_REDIREND:
10692 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010693 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010694 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010695 case ISN_SHUFFLE:
10696 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010697 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010698 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010699 case ISN_STORENR:
10700 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010701 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010702 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010703 case ISN_STOREV:
10704 case ISN_STRINDEX:
10705 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010706 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010707 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010708 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010709 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010710 case ISN_UNPACK:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010711 case ISN_USEDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010712 // nothing allocated
10713 break;
10714 }
10715}
10716
10717/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010718 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010719 */
10720 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010721delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010722{
10723 int idx;
10724
10725 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010726 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010727
10728 if (dfunc->df_instr != NULL)
10729 {
10730 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10731 delete_instr(dfunc->df_instr + idx);
10732 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010733 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010734 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010735 if (dfunc->df_instr_debug != NULL)
10736 {
10737 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10738 delete_instr(dfunc->df_instr_debug + idx);
10739 VIM_CLEAR(dfunc->df_instr_debug);
10740 dfunc->df_instr_debug = NULL;
10741 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010742#ifdef FEAT_PROFILE
10743 if (dfunc->df_instr_prof != NULL)
10744 {
10745 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10746 delete_instr(dfunc->df_instr_prof + idx);
10747 VIM_CLEAR(dfunc->df_instr_prof);
10748 dfunc->df_instr_prof = NULL;
10749 }
10750#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010751
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010752 if (mark_deleted)
10753 dfunc->df_deleted = TRUE;
10754 if (dfunc->df_ufunc != NULL)
10755 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010756}
10757
10758/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010759 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010760 * function, unless another user function still uses it.
10761 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010762 */
10763 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010764unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010765{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010766 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010767 {
10768 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10769 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010770
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010771 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010772 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010773 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010774 ufunc->uf_dfunc_idx = 0;
10775 if (dfunc->df_ufunc == ufunc)
10776 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010777 }
10778}
10779
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010780/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010781 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010782 */
10783 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010784link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010785{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010786 if (ufunc->uf_dfunc_idx > 0)
10787 {
10788 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10789 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010790
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010791 ++dfunc->df_refcount;
10792 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010793}
10794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010795#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010796/*
10797 * Free all functions defined with ":def".
10798 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010799 void
10800free_def_functions(void)
10801{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010802 int idx;
10803
10804 for (idx = 0; idx < def_functions.ga_len; ++idx)
10805 {
10806 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10807
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010808 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010809 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010810 }
10811
10812 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010813}
10814#endif
10815
10816
10817#endif // FEAT_EVAL