blob: ff96b103d574cc1744c9394a8053ad1086cde49a [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];
4311 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4312 return FAIL;
4313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4315 {
4316 --p;
4317 if (*p == '-')
4318 negate = !negate;
4319 }
4320 // only '-' has an effect, for '+' we only check the type
4321 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004322 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004324 if (isn == NULL)
4325 return FAIL;
4326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004328 else if (numeric_only)
4329 {
4330 ++p;
4331 break;
4332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 else
4334 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004335 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004337 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004339 if (p[-1] == '!')
4340 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004343 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 return FAIL;
4345 }
4346 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004347 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 return OK;
4349}
4350
4351/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004352 * Compile "(expression)": recursive!
4353 * Return FAIL/OK.
4354 */
4355 static int
4356compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4357{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004358 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004359 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004360
Bram Moolenaar24156692021-01-14 20:35:49 +01004361 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4362 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004363 if (ppconst->pp_used <= PPSIZE - 10)
4364 {
4365 ret = compile_expr1(arg, cctx, ppconst);
4366 }
4367 else
4368 {
4369 // Not enough space in ppconst, flush constants.
4370 if (generate_ppconst(cctx, ppconst) == FAIL)
4371 return FAIL;
4372 ret = compile_expr0(arg, cctx);
4373 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004374 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4375 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004376 if (**arg == ')')
4377 ++*arg;
4378 else if (ret == OK)
4379 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004380 emsg(_(e_missing_closing_paren));
Bram Moolenaar7e368202020-12-25 21:56:57 +01004381 ret = FAIL;
4382 }
4383 return ret;
4384}
4385
4386/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004388 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 */
4390 static int
4391compile_subscript(
4392 char_u **arg,
4393 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004394 char_u *start_leader,
4395 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004396 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004398 char_u *name_start = *end_leader;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004399 int keeping_dict = FALSE;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 for (;;)
4402 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004403 char_u *p = skipwhite(*arg);
4404
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004405 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004406 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004407 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004408
4409 // If a following line starts with "->{" or "->X" advance to that
4410 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004411 // Also if a following line starts with ".x".
4412 if (next != NULL &&
4413 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004414 && (next[2] == '{'
4415 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004416 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004417 {
4418 next = next_line_from_context(cctx, TRUE);
4419 if (next == NULL)
4420 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004421 *arg = next;
4422 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004423 }
4424 }
4425
Bram Moolenaarcd268012021-07-22 19:11:08 +02004426 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4427 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004428 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004430 garray_T *stack = &cctx->ctx_type_stack;
4431 type_T *type;
4432 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004434 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004435 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004436 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004439 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4440
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004441 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004442 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004444 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004446 if (keeping_dict)
4447 {
4448 keeping_dict = FALSE;
4449 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4450 return FAIL;
4451 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004453 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004455 char_u *pstart = p;
4456
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004457 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004458 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004459 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 // something->method()
4462 // Apply the '!', '-' and '+' first:
4463 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004464 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004467 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004468 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004469 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004470 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004471 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004472 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004473 garray_T *stack = &cctx->ctx_type_stack;
4474 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004475 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004476 int expr_isn_start = cctx->ctx_instr.ga_len;
4477 int expr_isn_end;
4478 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004479
4480 // Funcref call: list->(Refs[2])(arg)
4481 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004482 //
4483 // Fist compile the function expression.
4484 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004485 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004486
4487 // Remember the next instruction index, where the instructions
4488 // for arguments are being written.
4489 expr_isn_end = cctx->ctx_instr.ga_len;
4490
4491 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004492 if (**arg != '(')
4493 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004494 if (*skipwhite(*arg) == '(')
4495 emsg(_(e_nowhitespace));
4496 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004497 semsg(_(e_missing_parenthesis_str), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004498 return FAIL;
4499 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004500 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004501 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004502 return FAIL;
4503
Bram Moolenaar2927c072021-04-05 19:41:21 +02004504 // Move the instructions for the arguments to before the
4505 // instructions of the expression and move the type of the
4506 // expression after the argument types. This is what ISN_PCALL
4507 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004508 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004509 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4510 if (arg_isn_count > 0)
4511 {
4512 int expr_isn_count = expr_isn_end - expr_isn_start;
4513 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4514
4515 if (isn == NULL)
4516 return FAIL;
4517 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4518 + expr_isn_start,
4519 sizeof(isn_T) * expr_isn_count);
4520 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4521 + expr_isn_start,
4522 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4523 sizeof(isn_T) * arg_isn_count);
4524 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4525 + expr_isn_start + arg_isn_count,
4526 isn, sizeof(isn_T) * expr_isn_count);
4527 vim_free(isn);
4528
4529 type = ((type_T **)stack->ga_data)[type_idx_start];
4530 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4531 ((type_T **)stack->ga_data) + type_idx_start + 1,
4532 sizeof(type_T *)
4533 * (stack->ga_len - type_idx_start - 1));
4534 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4535 }
4536
Bram Moolenaar7e368202020-12-25 21:56:57 +01004537 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004538 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 return FAIL;
4540 }
4541 else
4542 {
4543 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004544 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004545 if (!eval_isnamec1(*p))
4546 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004547 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004548 return FAIL;
4549 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004550 if (ASCII_ISALPHA(*p) && p[1] == ':')
4551 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004552 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 ;
4554 if (*p != '(')
4555 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004556 semsg(_(e_missing_parenthesis_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 return FAIL;
4558 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004559 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 return FAIL;
4561 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004562 if (keeping_dict)
4563 {
4564 keeping_dict = FALSE;
4565 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4566 return FAIL;
4567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004569 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004571 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004572
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004573 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004574 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004575 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004576 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004577 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004578 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004579 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004580
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004581 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004582 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004583 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004584 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004585 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004586 // missing first index is equal to zero
4587 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004588 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004589 else
4590 {
4591 if (compile_expr0(arg, cctx) == FAIL)
4592 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004593 if (**arg == ':')
4594 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004595 semsg(_(e_white_space_required_before_and_after_str_at_str),
4596 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004597 return FAIL;
4598 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004599 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004600 return FAIL;
4601 *arg = skipwhite(*arg);
4602 }
4603 if (**arg == ':')
4604 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004605 is_slice = TRUE;
4606 ++*arg;
4607 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4608 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004609 semsg(_(e_white_space_required_before_and_after_str_at_str),
4610 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004611 return FAIL;
4612 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004613 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004614 return FAIL;
4615 if (**arg == ']')
4616 // missing second index is equal to end of string
4617 generate_PUSHNR(cctx, -1);
4618 else
4619 {
4620 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004621 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004622 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004623 return FAIL;
4624 *arg = skipwhite(*arg);
4625 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627
4628 if (**arg != ']')
4629 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004630 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 return FAIL;
4632 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004633 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004635 if (keeping_dict)
4636 {
4637 keeping_dict = FALSE;
4638 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4639 return FAIL;
4640 }
4641 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004642 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004644 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004646 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004647 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004648 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004649 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004650
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004651 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004652 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004653 {
4654 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004655 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004656 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004657 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004658 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 while (eval_isnamec(*p))
4660 MB_PTR_ADV(p);
4661 if (p == *arg)
4662 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004663 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 return FAIL;
4665 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004666 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
4667 return FAIL;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004668 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004670 keeping_dict = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 *arg = p;
4672 }
4673 else
4674 break;
4675 }
4676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 // Turn "dict.Func" into a partial for "Func" bound to "dict".
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004678 // This needs to be done at runtime to be able to check the type.
4679 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
4680 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681
4682 return OK;
4683}
4684
4685/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004686 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4687 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004689 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004690 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004691 *
4692 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 */
4694
4695/*
4696 * number number constant
4697 * 0zFFFFFFFF Blob constant
4698 * "string" string constant
4699 * 'string' literal string constant
4700 * &option-name option value
4701 * @r register contents
4702 * identifier variable value
4703 * function() function call
4704 * $VAR environment variable
4705 * (expression) nested expression
4706 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004707 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 *
4709 * Also handle:
4710 * ! in front logical NOT
4711 * - in front unary minus
4712 * + in front unary plus (ignored)
4713 * trailing (arg) funcref/partial call
4714 * trailing [] subscript in String or List
4715 * trailing .name entry in Dictionary
4716 * trailing ->name() method call
4717 */
4718 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004719compile_expr7(
4720 char_u **arg,
4721 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004722 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 char_u *start_leader, *end_leader;
4725 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004726 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004727 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004729 ppconst->pp_is_const = FALSE;
4730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 /*
4732 * Skip '!', '-' and '+' characters. They are handled later.
4733 */
4734 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004735 if (eval_leader(arg, TRUE) == FAIL)
4736 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 end_leader = *arg;
4738
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004739 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 switch (**arg)
4741 {
4742 /*
4743 * Number constant.
4744 */
4745 case '0': // also for blob starting with 0z
4746 case '1':
4747 case '2':
4748 case '3':
4749 case '4':
4750 case '5':
4751 case '6':
4752 case '7':
4753 case '8':
4754 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004755 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004757 // Apply "-" and "+" just before the number now, right to
4758 // left. Matters especially when "->" follows. Stops at
4759 // '!'.
4760 if (apply_leader(rettv, TRUE,
4761 start_leader, &end_leader) == FAIL)
4762 {
4763 clear_tv(rettv);
4764 return FAIL;
4765 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 break;
4767
4768 /*
4769 * String constant: "string".
4770 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004771 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 return FAIL;
4773 break;
4774
4775 /*
4776 * Literal string constant: 'str''ing'.
4777 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004778 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 return FAIL;
4780 break;
4781
4782 /*
4783 * Constant Vim variable.
4784 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004785 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 ret = NOTDONE;
4787 break;
4788
4789 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004790 * "true" constant
4791 */
4792 case 't': if (STRNCMP(*arg, "true", 4) == 0
4793 && !eval_isnamec((*arg)[4]))
4794 {
4795 *arg += 4;
4796 rettv->v_type = VAR_BOOL;
4797 rettv->vval.v_number = VVAL_TRUE;
4798 }
4799 else
4800 ret = NOTDONE;
4801 break;
4802
4803 /*
4804 * "false" constant
4805 */
4806 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4807 && !eval_isnamec((*arg)[5]))
4808 {
4809 *arg += 5;
4810 rettv->v_type = VAR_BOOL;
4811 rettv->vval.v_number = VVAL_FALSE;
4812 }
4813 else
4814 ret = NOTDONE;
4815 break;
4816
4817 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004818 * "null" constant
4819 */
4820 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004821 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004822 {
4823 *arg += 4;
4824 rettv->v_type = VAR_SPECIAL;
4825 rettv->vval.v_number = VVAL_NULL;
4826 }
4827 else
4828 ret = NOTDONE;
4829 break;
4830
4831 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 * List: [expr, expr]
4833 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004834 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4835 return FAIL;
4836 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 break;
4838
4839 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 * Dictionary: {'key': val, 'key': val}
4841 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004842 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4843 return FAIL;
4844 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 break;
4846
4847 /*
4848 * Option value: &name
4849 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004850 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4851 return FAIL;
4852 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 break;
4854
4855 /*
4856 * Environment variable: $VAR.
4857 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004858 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4859 return FAIL;
4860 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 break;
4862
4863 /*
4864 * Register contents: @r.
4865 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004866 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4867 return FAIL;
4868 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869 break;
4870 /*
4871 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004872 * lambda: (arg, arg) => expr
4873 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004875 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4876 ret = compile_lambda(arg, cctx);
4877 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004878 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879 break;
4880
4881 default: ret = NOTDONE;
4882 break;
4883 }
4884 if (ret == FAIL)
4885 return FAIL;
4886
Bram Moolenaar1c747212020-05-09 18:28:34 +02004887 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004889 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004890 clear_tv(rettv);
4891 else
4892 // A constant expression can possibly be handled compile time,
4893 // return the value instead of generating code.
4894 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 }
4896 else if (ret == NOTDONE)
4897 {
4898 char_u *p;
4899 int r;
4900
4901 if (!eval_isnamec1(**arg))
4902 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004903 if (!vim9_bad_comment(*arg))
4904 {
4905 if (ends_excmd(*skipwhite(*arg)))
4906 semsg(_(e_empty_expression_str), *arg);
4907 else
4908 semsg(_(e_name_expected_str), *arg);
4909 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 return FAIL;
4911 }
4912
4913 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004914 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004915 if (p - *arg == (size_t)1 && **arg == '_')
4916 {
4917 emsg(_(e_cannot_use_underscore_here));
4918 return FAIL;
4919 }
4920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004922 {
4923 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004926 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02004927 if (cctx->ctx_skip != SKIP_YES
4928 && generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004929 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004930 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 if (r == FAIL)
4933 return FAIL;
4934 }
4935
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004936 // Handle following "[]", ".member", etc.
4937 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004938 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004939 ppconst) == FAIL)
4940 return FAIL;
4941 if (ppconst->pp_used > 0)
4942 {
4943 // apply the '!', '-' and '+' before the constant
4944 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004945 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004946 return FAIL;
4947 return OK;
4948 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004949 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004951 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952}
4953
4954/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004955 * Give the "white on both sides" error, taking the operator from "p[len]".
4956 */
4957 void
4958error_white_both(char_u *op, int len)
4959{
4960 char_u buf[10];
4961
4962 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004963 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004964}
4965
4966/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004967 * <type>expr7: runtime type check / conversion
4968 */
4969 static int
4970compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4971{
4972 type_T *want_type = NULL;
4973
4974 // Recognize <type>
4975 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4976 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004977 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004978 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4979 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004980 return FAIL;
4981
4982 if (**arg != '>')
4983 {
4984 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004985 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004986 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004987 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004988 return FAIL;
4989 }
4990 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004991 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004992 return FAIL;
4993 }
4994
4995 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4996 return FAIL;
4997
4998 if (want_type != NULL)
4999 {
5000 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02005001 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005002 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005003
Bram Moolenaard1103582020-08-14 22:44:25 +02005004 generate_ppconst(cctx, ppconst);
5005 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005006 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005007 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01005008 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005009 return FAIL;
5010 }
5011 }
5012
5013 return OK;
5014}
5015
5016/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 * * number multiplication
5018 * / number division
5019 * % number modulo
5020 */
5021 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005022compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023{
5024 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005025 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005026 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005028 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005029 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030 return FAIL;
5031
5032 /*
5033 * Repeat computing, until no "*", "/" or "%" is following.
5034 */
5035 for (;;)
5036 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005037 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 if (*op != '*' && *op != '/' && *op != '%')
5039 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005040 if (next != NULL)
5041 {
5042 *arg = next_line_from_context(cctx, TRUE);
5043 op = skipwhite(*arg);
5044 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005045
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005046 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005048 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005049 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01005051 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005052 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005053
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005054 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005055 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005057
5058 if (ppconst->pp_used == ppconst_used + 2
5059 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5060 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005061 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005062 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5063 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
5064 varnumber_T res = 0;
5065 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005067 // both are numbers: compute the result
5068 switch (*op)
5069 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005070 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005071 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005072 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005073 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005074 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005075 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005076 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005077 break;
5078 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005079 if (failed)
5080 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005081 tv1->vval.v_number = res;
5082 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005083 }
5084 else
5085 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005086 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005087 generate_two_op(cctx, op);
5088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 }
5090
5091 return OK;
5092}
5093
5094/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01005095 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 * - number subtraction
5097 * .. string concatenation
5098 */
5099 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005100compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101{
5102 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005103 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005105 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106
5107 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005108 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 return FAIL;
5110
5111 /*
5112 * Repeat computing, until no "+", "-" or ".." is following.
5113 */
5114 for (;;)
5115 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005116 op = may_peek_next_line(cctx, *arg, &next);
5117 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005119 if (op[0] == op[1] && *op != '.' && next)
5120 // Finding "++" or "--" on the next line is a separate command.
5121 // But ".." is concatenation.
5122 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005124 if (next != NULL)
5125 {
5126 *arg = next_line_from_context(cctx, TRUE);
5127 op = skipwhite(*arg);
5128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005130 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005132 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005133 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 }
5135
Bram Moolenaare0de1712020-12-02 17:36:54 +01005136 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005137 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005139 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005140 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141 return FAIL;
5142
Bram Moolenaara5565e42020-05-09 15:44:01 +02005143 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005144 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005145 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5146 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5147 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5148 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005150 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5151 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005152
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005153 // concat/subtract/add constant numbers
5154 if (*op == '+')
5155 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5156 else if (*op == '-')
5157 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5158 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005159 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005160 // concatenate constant strings
5161 char_u *s1 = tv1->vval.v_string;
5162 char_u *s2 = tv2->vval.v_string;
5163 size_t len1 = STRLEN(s1);
5164
5165 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5166 if (tv1->vval.v_string == NULL)
5167 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005168 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005169 return FAIL;
5170 }
5171 mch_memmove(tv1->vval.v_string, s1, len1);
5172 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005173 vim_free(s1);
5174 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005175 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005176 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 }
5178 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005179 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005180 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005181 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005182 if (*op == '.')
5183 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005184 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5185 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005186 return FAIL;
5187 generate_instr_drop(cctx, ISN_CONCAT, 1);
5188 }
5189 else
5190 generate_two_op(cctx, op);
5191 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 }
5193
5194 return OK;
5195}
5196
5197/*
5198 * expr5a == expr5b
5199 * expr5a =~ expr5b
5200 * expr5a != expr5b
5201 * expr5a !~ expr5b
5202 * expr5a > expr5b
5203 * expr5a >= expr5b
5204 * expr5a < expr5b
5205 * expr5a <= expr5b
5206 * expr5a is expr5b
5207 * expr5a isnot expr5b
5208 *
5209 * Produces instructions:
5210 * EVAL expr5a Push result of "expr5a"
5211 * EVAL expr5b Push result of "expr5b"
5212 * COMPARE one of the compare instructions
5213 */
5214 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005215compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005217 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005219 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005222 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223
5224 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005225 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005226 return FAIL;
5227
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005228 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005229 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230
5231 /*
5232 * If there is a comparative operator, use it.
5233 */
5234 if (type != EXPR_UNKNOWN)
5235 {
5236 int ic = FALSE; // Default: do not ignore case
5237
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005238 if (next != NULL)
5239 {
5240 *arg = next_line_from_context(cctx, TRUE);
5241 p = skipwhite(*arg);
5242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243 if (type_is && (p[len] == '?' || p[len] == '#'))
5244 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005245 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246 return FAIL;
5247 }
5248 // extra question mark appended: ignore case
5249 if (p[len] == '?')
5250 {
5251 ic = TRUE;
5252 ++len;
5253 }
5254 // extra '#' appended: match case (ignored)
5255 else if (p[len] == '#')
5256 ++len;
5257 // nothing appended: match case
5258
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005259 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005260 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005261 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005262 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263 }
5264
5265 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005266 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005267 return FAIL;
5268
Bram Moolenaara5565e42020-05-09 15:44:01 +02005269 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005270 return FAIL;
5271
Bram Moolenaara5565e42020-05-09 15:44:01 +02005272 if (ppconst->pp_used == ppconst_used + 2)
5273 {
5274 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5275 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5276 int ret;
5277
5278 // Both sides are a constant, compute the result now.
5279 // First check for a valid combination of types, this is more
5280 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005281 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005282 ret = FAIL;
5283 else
5284 {
5285 ret = typval_compare(tv1, tv2, type, ic);
5286 tv1->v_type = VAR_BOOL;
5287 tv1->vval.v_number = tv1->vval.v_number
5288 ? VVAL_TRUE : VVAL_FALSE;
5289 clear_tv(tv2);
5290 --ppconst->pp_used;
5291 }
5292 return ret;
5293 }
5294
5295 generate_ppconst(cctx, ppconst);
5296 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297 }
5298
5299 return OK;
5300}
5301
Bram Moolenaar7f141552020-05-09 17:35:53 +02005302static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304/*
5305 * Compile || or &&.
5306 */
5307 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005308compile_and_or(
5309 char_u **arg,
5310 cctx_T *cctx,
5311 char *op,
5312 ppconst_T *ppconst,
5313 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005314{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005315 char_u *next;
5316 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317 int opchar = *op;
5318
5319 if (p[0] == opchar && p[1] == opchar)
5320 {
5321 garray_T *instr = &cctx->ctx_instr;
5322 garray_T end_ga;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005323 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324
5325 /*
5326 * Repeat until there is no following "||" or "&&"
5327 */
5328 ga_init2(&end_ga, sizeof(int), 10);
5329 while (p[0] == opchar && p[1] == opchar)
5330 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005331 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005332 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005333 int start_ctx_lnum = cctx->ctx_lnum;
5334 int save_lnum;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005335 int const_used;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005336 int status;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005337 jumpwhen_T jump_when = opchar == '|'
5338 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005339
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005340 if (next != NULL)
5341 {
5342 *arg = next_line_from_context(cctx, TRUE);
5343 p = skipwhite(*arg);
5344 }
5345
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005346 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5347 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005348 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005349 op, p);
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005350 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005351 return FAIL;
5352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005353
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005354 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005355 SOURCING_LNUM = start_lnum;
5356 save_lnum = cctx->ctx_lnum;
5357 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005358
5359 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005360 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005361 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005362 // Use the last ppconst if possible.
5363 if (ppconst->pp_used > 0)
5364 {
5365 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5366 int is_true = tv2bool(tv);
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005367
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005368 if ((is_true && opchar == '|')
5369 || (!is_true && opchar == '&'))
5370 {
5371 // For "false && expr" and "true || expr" the "expr"
5372 // does not need to be evaluated.
5373 cctx->ctx_skip = SKIP_YES;
5374 clear_tv(tv);
5375 tv->v_type = VAR_BOOL;
5376 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
5377 }
5378 else
5379 {
5380 // For "true && expr" and "false || expr" only "expr"
5381 // needs to be evaluated.
5382 --ppconst->pp_used;
5383 jump_when = JUMP_NEVER;
5384 }
5385 }
5386 else
5387 {
5388 // Every part must evaluate to a bool.
5389 status = bool_on_stack(cctx);
5390 }
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005391 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005392 if (status != FAIL)
5393 status = ga_grow(&end_ga, 1);
Bram Moolenaara7511c02021-04-03 21:47:07 +02005394 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005395 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 {
5397 ga_clear(&end_ga);
5398 return FAIL;
5399 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005400
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005401 if (jump_when != JUMP_NEVER)
5402 {
5403 if (cctx->ctx_skip != SKIP_YES)
5404 {
5405 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5406 ++end_ga.ga_len;
5407 }
5408 generate_JUMP(cctx, jump_when, 0);
5409 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005410
5411 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005412 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005413 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005414 {
5415 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005416 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005417 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005418
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005419 const_used = ppconst->pp_used;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005420 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5421 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422 {
5423 ga_clear(&end_ga);
5424 return FAIL;
5425 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005426
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005427 // "0 || 1" results in true, "1 && 0" results in false.
5428 if (ppconst->pp_used == const_used + 1)
5429 {
5430 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5431
5432 if (tv->v_type == VAR_NUMBER
5433 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
5434 {
5435 tv->vval.v_number = tv->vval.v_number == 1
5436 ? VVAL_TRUE : VVAL_FALSE;
5437 tv->v_type = VAR_BOOL;
5438 }
5439 }
5440
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005441 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005443
5444 if (check_ppconst_bool(ppconst) == FAIL)
5445 {
5446 ga_clear(&end_ga);
5447 return FAIL;
5448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005450 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
5451 // Every part must evaluate to a bool.
5452 if (bool_on_stack(cctx) == FAIL)
5453 {
5454 ga_clear(&end_ga);
5455 return FAIL;
5456 }
5457
5458 if (end_ga.ga_len > 0)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005459 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005460 // Fill in the end label in all jumps.
5461 generate_ppconst(cctx, ppconst);
5462 while (end_ga.ga_len > 0)
5463 {
5464 isn_T *isn;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005465
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005466 --end_ga.ga_len;
5467 isn = ((isn_T *)instr->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005469 isn->isn_arg.jump.jump_where = instr->ga_len;
5470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005471 }
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005472 ga_clear(&end_ga);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005473
5474 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005475 }
5476
5477 return OK;
5478}
5479
5480/*
5481 * expr4a && expr4a && expr4a logical AND
5482 *
5483 * Produces instructions:
5484 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005485 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005486 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005487 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005488 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489 * EVAL expr4c Push result of "expr4c"
5490 * end:
5491 */
5492 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005493compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005495 int ppconst_used = ppconst->pp_used;
5496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005498 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005499 return FAIL;
5500
5501 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005502 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005503}
5504
5505/*
5506 * expr3a || expr3b || expr3c logical OR
5507 *
5508 * Produces instructions:
5509 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005510 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005511 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005512 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005513 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005514 * EVAL expr3c Push result of "expr3c"
5515 * end:
5516 */
5517 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005518compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005520 int ppconst_used = ppconst->pp_used;
5521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005522 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005523 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005524 return FAIL;
5525
5526 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005527 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005528}
5529
5530/*
5531 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005532 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005533 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005534 * JUMP_IF_FALSE alt jump if false
5535 * EVAL expr1a
5536 * JUMP_ALWAYS end
5537 * alt: EVAL expr1b
5538 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005539 *
5540 * Toplevel expression: expr2 ?? expr1
5541 * Produces instructions:
5542 * EVAL expr2 Push result of "expr2"
5543 * JUMP_AND_KEEP_IF_TRUE end jump if true
5544 * EVAL expr1
5545 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 */
5547 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005548compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005549{
5550 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005551 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005552 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005553
Bram Moolenaar3988f642020-08-27 22:43:03 +02005554 // Ignore all kinds of errors when not producing code.
5555 if (cctx->ctx_skip == SKIP_YES)
5556 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005557 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005558 return OK;
5559 }
5560
Bram Moolenaar61a89812020-05-07 16:58:17 +02005561 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005562 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005563 return FAIL;
5564
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005565 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005566 if (*p == '?')
5567 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005568 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005569 garray_T *instr = &cctx->ctx_instr;
5570 garray_T *stack = &cctx->ctx_type_stack;
5571 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005572 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005573 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005574 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005575 int has_const_expr = FALSE;
5576 int const_value = FALSE;
5577 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005578
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005579 if (next != NULL)
5580 {
5581 *arg = next_line_from_context(cctx, TRUE);
5582 p = skipwhite(*arg);
5583 }
5584
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005585 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005586 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005587 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005588 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005589 return FAIL;
5590 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005591
Bram Moolenaara5565e42020-05-09 15:44:01 +02005592 if (ppconst->pp_used == ppconst_used + 1)
5593 {
5594 // the condition is a constant, we know whether the ? or the :
5595 // expression is to be evaluated.
5596 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005597 if (op_falsy)
5598 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5599 else
5600 {
5601 int error = FALSE;
5602
5603 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5604 &error);
5605 if (error)
5606 return FAIL;
5607 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005608 cctx->ctx_skip = save_skip == SKIP_YES ||
5609 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5610
5611 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5612 // "left ?? right" and "left" is truthy: produce "left"
5613 generate_ppconst(cctx, ppconst);
5614 else
5615 {
5616 clear_tv(&ppconst->pp_tv[ppconst_used]);
5617 --ppconst->pp_used;
5618 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005619 }
5620 else
5621 {
5622 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005623 if (op_falsy)
5624 end_idx = instr->ga_len;
5625 generate_JUMP(cctx, op_falsy
5626 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5627 if (op_falsy)
5628 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005629 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005630
5631 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005632 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005633 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005634 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005635 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005636
Bram Moolenaara5565e42020-05-09 15:44:01 +02005637 if (!has_const_expr)
5638 {
5639 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005641 if (!op_falsy)
5642 {
5643 // remember the type and drop it
5644 --stack->ga_len;
5645 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005646
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005647 end_idx = instr->ga_len;
5648 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005649
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005650 // jump here from JUMP_IF_FALSE
5651 isn = ((isn_T *)instr->ga_data) + alt_idx;
5652 isn->isn_arg.jump.jump_where = instr->ga_len;
5653 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005655
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005656 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005658 // Check for the ":".
5659 p = may_peek_next_line(cctx, *arg, &next);
5660 if (*p != ':')
5661 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005662 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005663 return FAIL;
5664 }
5665 if (next != NULL)
5666 {
5667 *arg = next_line_from_context(cctx, TRUE);
5668 p = skipwhite(*arg);
5669 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005670
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005671 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5672 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005673 semsg(_(e_white_space_required_before_and_after_str_at_str),
5674 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005675 return FAIL;
5676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005677
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005678 // evaluate the third expression
5679 if (has_const_expr)
5680 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005681 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005682 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005683 return FAIL;
5684 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5685 return FAIL;
5686 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005687
Bram Moolenaara5565e42020-05-09 15:44:01 +02005688 if (!has_const_expr)
5689 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005690 type_T **typep;
5691
Bram Moolenaara5565e42020-05-09 15:44:01 +02005692 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005693
Bram Moolenaara5565e42020-05-09 15:44:01 +02005694 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005695 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5696 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005697
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005698 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005699 isn = ((isn_T *)instr->ga_data) + end_idx;
5700 isn->isn_arg.jump.jump_where = instr->ga_len;
5701 }
5702
5703 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005704 }
5705 return OK;
5706}
5707
5708/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005709 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005710 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5711 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005712 */
5713 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005714compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005715{
5716 ppconst_T ppconst;
5717
5718 CLEAR_FIELD(ppconst);
5719 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5720 {
5721 clear_ppconst(&ppconst);
5722 return FAIL;
5723 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005724 if (is_const != NULL)
5725 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005726 if (generate_ppconst(cctx, &ppconst) == FAIL)
5727 return FAIL;
5728 return OK;
5729}
5730
5731/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005732 * Toplevel expression.
5733 */
5734 static int
5735compile_expr0(char_u **arg, cctx_T *cctx)
5736{
5737 return compile_expr0_ext(arg, cctx, NULL);
5738}
5739
5740/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005741 * Compile "return [expr]".
5742 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005743 */
5744 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005745compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746{
5747 char_u *p = arg;
5748 garray_T *stack = &cctx->ctx_type_stack;
5749 type_T *stack_type;
5750
5751 if (*p != NUL && *p != '|' && *p != '\n')
5752 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005753 if (legacy)
5754 {
5755 int save_flags = cmdmod.cmod_flags;
5756
5757 generate_LEGACY_EVAL(cctx, p);
5758 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5759 0, cctx, FALSE, FALSE) == FAIL)
5760 return NULL;
5761 cmdmod.cmod_flags |= CMOD_LEGACY;
5762 (void)skip_expr(&p, NULL);
5763 cmdmod.cmod_flags = save_flags;
5764 }
5765 else
5766 {
5767 // compile return argument into instructions
5768 if (compile_expr0(&p, cctx) == FAIL)
5769 return NULL;
5770 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005772 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005773 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005774 // "check_return_type" with uf_ret_type set to &t_unknown is used
5775 // for an inline function without a specified return type. Set the
5776 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005777 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005778 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005779 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5780 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005781 || (!check_return_type
5782 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005783 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005784 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005785 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005786 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005787 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005788 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5789 && stack_type->tt_type != VAR_VOID
5790 && stack_type->tt_type != VAR_UNKNOWN)
5791 {
5792 emsg(_(e_returning_value_in_function_without_return_type));
5793 return NULL;
5794 }
5795 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005796 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005797 return NULL;
5798 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005799 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005800 }
5801 else
5802 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005803 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005804 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005805 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5806 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005807 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005808 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005809 return NULL;
5810 }
5811
5812 // No argument, return zero.
5813 generate_PUSHNR(cctx, 0);
5814 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005815
5816 // Undo any command modifiers.
5817 generate_undo_cmdmods(cctx);
5818
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005819 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820 return NULL;
5821
5822 // "return val | endif" is possible
5823 return skipwhite(p);
5824}
5825
5826/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005827 * Get a line from the compilation context, compatible with exarg_T getline().
5828 * Return a pointer to the line in allocated memory.
5829 * Return NULL for end-of-file or some error.
5830 */
5831 static char_u *
5832exarg_getline(
5833 int c UNUSED,
5834 void *cookie,
5835 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005836 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005837{
5838 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005839 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005840
Bram Moolenaar66250c92020-08-20 15:02:42 +02005841 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005842 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005843 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005844 return NULL;
5845 ++cctx->ctx_lnum;
5846 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5847 // Comment lines result in NULL pointers, skip them.
5848 if (p != NULL)
5849 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005850 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005851}
5852
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005853 void
5854fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5855{
5856 eap->getline = exarg_getline;
5857 eap->cookie = cctx;
5858}
5859
Bram Moolenaar04b12692020-05-04 23:24:44 +02005860/*
5861 * Compile a nested :def command.
5862 */
5863 static char_u *
5864compile_nested_function(exarg_T *eap, cctx_T *cctx)
5865{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005866 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005867 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005868 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005869 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005870 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005871 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005872 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005873
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005874 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005875 {
5876 emsg(_(e_cannot_use_bang_with_nested_def));
5877 return NULL;
5878 }
5879
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005880 if (*name_start == '/')
5881 {
5882 name_end = skip_regexp(name_start + 1, '/', TRUE);
5883 if (*name_end == '/')
5884 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005885 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005886 }
5887 if (name_end == name_start || *skipwhite(name_end) != '(')
5888 {
5889 if (!ends_excmd2(name_start, name_end))
5890 {
5891 semsg(_(e_invalid_command_str), eap->cmd);
5892 return NULL;
5893 }
5894
5895 // "def" or "def Name": list functions
5896 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5897 return NULL;
5898 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5899 }
5900
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005901 // Only g:Func() can use a namespace.
5902 if (name_start[1] == ':' && !is_global)
5903 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005904 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005905 return NULL;
5906 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005907 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005908 return NULL;
5909
Bram Moolenaar04b12692020-05-04 23:24:44 +02005910 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005911 fill_exarg_from_cctx(eap, cctx);
5912
Bram Moolenaar04b12692020-05-04 23:24:44 +02005913 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00005914 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005915 lambda_name = vim_strsave(get_lambda_name());
5916 if (lambda_name == NULL)
5917 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005918 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005919
Bram Moolenaar822ba242020-05-24 23:00:18 +02005920 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005921 {
5922 r = eap->skip ? OK : FAIL;
5923 goto theend;
5924 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005925
5926 // copy over the block scope IDs before compiling
5927 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5928 {
5929 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5930
5931 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5932 if (ufunc->uf_block_ids != NULL)
5933 {
5934 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5935 sizeof(int) * block_depth);
5936 ufunc->uf_block_depth = block_depth;
5937 }
5938 }
5939
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005940 compile_type = COMPILE_TYPE(ufunc);
5941#ifdef FEAT_PROFILE
5942 // If the outer function is profiled, also compile the nested function for
5943 // profiling.
5944 if (cctx->ctx_compile_type == CT_PROFILE)
5945 compile_type = CT_PROFILE;
5946#endif
5947 if (func_needs_compiling(ufunc, compile_type)
5948 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005949 {
5950 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005951 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005952 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005953
Bram Moolenaar648594e2021-07-11 17:55:01 +02005954#ifdef FEAT_PROFILE
5955 // When the outer function is compiled for profiling, the nested function
5956 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005957 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005958 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5959#endif
5960
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005961 if (is_global)
5962 {
5963 char_u *func_name = vim_strnsave(name_start + 2,
5964 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005965
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005966 if (func_name == NULL)
5967 r = FAIL;
5968 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005969 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005970 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005971 lambda_name = NULL;
5972 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005973 }
5974 else
5975 {
5976 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005977 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005978 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005979
Bram Moolenaareef21022020-08-01 22:16:43 +02005980 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005981 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005982 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005983 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005984 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5985 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005986
5987theend:
5988 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005989 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005990}
5991
5992/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 * Return the length of an assignment operator, or zero if there isn't one.
5994 */
5995 int
5996assignment_len(char_u *p, int *heredoc)
5997{
5998 if (*p == '=')
5999 {
6000 if (p[1] == '<' && p[2] == '<')
6001 {
6002 *heredoc = TRUE;
6003 return 3;
6004 }
6005 return 1;
6006 }
6007 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
6008 return 2;
6009 if (STRNCMP(p, "..=", 3) == 0)
6010 return 3;
6011 return 0;
6012}
6013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006014/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006015 * Generate the load instruction for "name".
6016 */
6017 static void
6018generate_loadvar(
6019 cctx_T *cctx,
6020 assign_dest_T dest,
6021 char_u *name,
6022 lvar_T *lvar,
6023 type_T *type)
6024{
6025 switch (dest)
6026 {
6027 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006028 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006029 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
6030 break;
6031 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01006032 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
6033 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
6034 else
6035 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006036 break;
6037 case dest_buffer:
6038 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
6039 break;
6040 case dest_window:
6041 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
6042 break;
6043 case dest_tab:
6044 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
6045 break;
6046 case dest_script:
6047 compile_load_scriptvar(cctx,
6048 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
6049 break;
6050 case dest_env:
6051 // Include $ in the name here
6052 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
6053 break;
6054 case dest_reg:
6055 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
6056 break;
6057 case dest_vimvar:
6058 generate_LOADV(cctx, name + 2, TRUE);
6059 break;
6060 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01006061 if (lvar->lv_from_outer > 0)
6062 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
6063 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006064 else
6065 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
6066 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006067 case dest_expr:
6068 // list or dict value should already be on the stack.
6069 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006070 }
6071}
6072
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006073/*
6074 * Skip over "[expr]" or ".member".
6075 * Does not check for any errors.
6076 */
6077 static char_u *
6078skip_index(char_u *start)
6079{
6080 char_u *p = start;
6081
6082 if (*p == '[')
6083 {
6084 p = skipwhite(p + 1);
6085 (void)skip_expr(&p, NULL);
6086 p = skipwhite(p);
6087 if (*p == ']')
6088 return p + 1;
6089 return p;
6090 }
6091 // if (*p == '.')
6092 return to_name_end(p + 1, TRUE);
6093}
6094
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006095 void
6096vim9_declare_error(char_u *name)
6097{
6098 char *scope = "";
6099
6100 switch (*name)
6101 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006102 case 'g': scope = _("global"); break;
6103 case 'b': scope = _("buffer"); break;
6104 case 'w': scope = _("window"); break;
6105 case 't': scope = _("tab"); break;
6106 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006107 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006108 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006109 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006110 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006111 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006112 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006113 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006114 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006115 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006116}
6117
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006118/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006119 * For one assignment figure out the type of destination. Return it in "dest".
6120 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006121 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006122 * For a v:var "vimvaridx" is set.
6123 * "type" is set to the destination type if known, unchanted otherwise.
6124 * Return FAIL if an error message was given.
6125 */
6126 static int
6127get_var_dest(
6128 char_u *name,
6129 assign_dest_T *dest,
6130 int cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006131 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006132 int *vimvaridx,
6133 type_T **type,
6134 cctx_T *cctx)
6135{
6136 char_u *p;
6137
6138 if (*name == '&')
6139 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006140 int cc;
6141 long numval;
6142 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006143 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006144
6145 *dest = dest_option;
6146 if (cmdidx == CMD_final || cmdidx == CMD_const)
6147 {
6148 emsg(_(e_const_option));
6149 return FAIL;
6150 }
6151 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006152 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006153 if (p == NULL)
6154 {
6155 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02006156 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006157 return FAIL;
6158 }
6159 cc = *p;
6160 *p = NUL;
6161 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006162 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006163 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006164 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006165 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006166 case gov_unknown:
Bram Moolenaare1242042021-12-16 20:56:57 +00006167 semsg(_(e_unknown_option_str), name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006168 return FAIL;
6169 case gov_string:
6170 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006171 if (opt_p_flags & P_FUNC)
6172 {
6173 // might be a Funcref, check the type later
6174 *type = &t_any;
6175 *dest = dest_func_option;
6176 }
6177 else
6178 {
6179 *type = &t_string;
6180 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006181 break;
6182 case gov_bool:
6183 case gov_hidden_bool:
6184 *type = &t_bool;
6185 break;
6186 case gov_number:
6187 case gov_hidden_number:
6188 *type = &t_number;
6189 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006190 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006191 }
6192 else if (*name == '$')
6193 {
6194 *dest = dest_env;
6195 *type = &t_string;
6196 }
6197 else if (*name == '@')
6198 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006199 if (name[1] != '@'
6200 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006201 {
6202 emsg_invreg(name[1]);
6203 return FAIL;
6204 }
6205 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006206 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006207 }
6208 else if (STRNCMP(name, "g:", 2) == 0)
6209 {
6210 *dest = dest_global;
6211 }
6212 else if (STRNCMP(name, "b:", 2) == 0)
6213 {
6214 *dest = dest_buffer;
6215 }
6216 else if (STRNCMP(name, "w:", 2) == 0)
6217 {
6218 *dest = dest_window;
6219 }
6220 else if (STRNCMP(name, "t:", 2) == 0)
6221 {
6222 *dest = dest_tab;
6223 }
6224 else if (STRNCMP(name, "v:", 2) == 0)
6225 {
6226 typval_T *vtv;
6227 int di_flags;
6228
6229 *vimvaridx = find_vim_var(name + 2, &di_flags);
6230 if (*vimvaridx < 0)
6231 {
6232 semsg(_(e_variable_not_found_str), name);
6233 return FAIL;
6234 }
6235 // We use the current value of "sandbox" here, is that OK?
6236 if (var_check_ro(di_flags, name, FALSE))
6237 return FAIL;
6238 *dest = dest_vimvar;
6239 vtv = get_vim_var_tv(*vimvaridx);
6240 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6241 }
6242 return OK;
6243}
6244
6245/*
6246 * Generate a STORE instruction for "dest", not being "dest_local".
6247 * Return FAIL when out of memory.
6248 */
6249 static int
6250generate_store_var(
6251 cctx_T *cctx,
6252 assign_dest_T dest,
6253 int opt_flags,
6254 int vimvaridx,
6255 int scriptvar_idx,
6256 int scriptvar_sid,
6257 type_T *type,
6258 char_u *name)
6259{
6260 switch (dest)
6261 {
6262 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006263 return generate_STOREOPT(cctx, ISN_STOREOPT,
6264 skip_option_env_lead(name), opt_flags);
6265 case dest_func_option:
6266 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
6267 skip_option_env_lead(name), opt_flags);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006268 case dest_global:
6269 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006270 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6271 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006272 case dest_buffer:
6273 // include b: with the name, easier to execute that way
6274 return generate_STORE(cctx, ISN_STOREB, 0, name);
6275 case dest_window:
6276 // include w: with the name, easier to execute that way
6277 return generate_STORE(cctx, ISN_STOREW, 0, name);
6278 case dest_tab:
6279 // include t: with the name, easier to execute that way
6280 return generate_STORE(cctx, ISN_STORET, 0, name);
6281 case dest_env:
6282 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6283 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006284 return generate_STORE(cctx, ISN_STOREREG,
6285 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006286 case dest_vimvar:
6287 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6288 case dest_script:
6289 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006290 // "s:" may be included in the name.
6291 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006292 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006293 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6294 scriptvar_sid, scriptvar_idx, type);
6295 case dest_local:
6296 case dest_expr:
6297 // cannot happen
6298 break;
6299 }
6300 return FAIL;
6301}
6302
Bram Moolenaar752fc692021-01-04 21:57:11 +01006303 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006304generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6305{
6306 if (lhs->lhs_dest != dest_local)
6307 return generate_store_var(cctx, lhs->lhs_dest,
6308 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6309 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6310 lhs->lhs_type, lhs->lhs_name);
6311
6312 if (lhs->lhs_lvar != NULL)
6313 {
6314 garray_T *instr = &cctx->ctx_instr;
6315 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6316
6317 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6318 // ISN_STORENR
6319 if (lhs->lhs_lvar->lv_from_outer == 0
6320 && instr->ga_len == instr_count + 1
6321 && isn->isn_type == ISN_PUSHNR)
6322 {
6323 varnumber_T val = isn->isn_arg.number;
6324 garray_T *stack = &cctx->ctx_type_stack;
6325
6326 isn->isn_type = ISN_STORENR;
6327 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6328 isn->isn_arg.storenr.stnr_val = val;
6329 if (stack->ga_len > 0)
6330 --stack->ga_len;
6331 }
6332 else if (lhs->lhs_lvar->lv_from_outer > 0)
6333 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6334 lhs->lhs_lvar->lv_from_outer);
6335 else
6336 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6337 }
6338 return OK;
6339}
6340
6341 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006342is_decl_command(int cmdidx)
6343{
6344 return cmdidx == CMD_let || cmdidx == CMD_var
6345 || cmdidx == CMD_final || cmdidx == CMD_const;
6346}
6347
6348/*
6349 * Figure out the LHS type and other properties for an assignment or one item
6350 * of ":unlet" with an index.
6351 * Returns OK or FAIL.
6352 */
6353 static int
6354compile_lhs(
6355 char_u *var_start,
6356 lhs_T *lhs,
6357 int cmdidx,
6358 int heredoc,
6359 int oplen,
6360 cctx_T *cctx)
6361{
6362 char_u *var_end;
6363 int is_decl = is_decl_command(cmdidx);
6364
6365 CLEAR_POINTER(lhs);
6366 lhs->lhs_dest = dest_local;
6367 lhs->lhs_vimvaridx = -1;
6368 lhs->lhs_scriptvar_idx = -1;
6369
6370 // "dest_end" is the end of the destination, including "[expr]" or
6371 // ".name".
6372 // "var_end" is the end of the variable/option/etc. name.
6373 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6374 if (*var_start == '@')
6375 var_end = var_start + 2;
6376 else
6377 {
6378 // skip over the leading "&", "&l:", "&g:" and "$"
6379 var_end = skip_option_env_lead(var_start);
6380 var_end = to_name_end(var_end, TRUE);
6381 }
6382
6383 // "a: type" is declaring variable "a" with a type, not dict "a:".
6384 if (is_decl && lhs->lhs_dest_end == var_start + 2
6385 && lhs->lhs_dest_end[-1] == ':')
6386 --lhs->lhs_dest_end;
6387 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6388 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006389 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006390
6391 // compute the length of the destination without "[expr]" or ".name"
6392 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006393 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006394 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6395 if (lhs->lhs_name == NULL)
6396 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006397
6398 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6399 // Something follows after the variable: "var[idx]" or "var.key".
6400 lhs->lhs_has_index = TRUE;
6401
Bram Moolenaar752fc692021-01-04 21:57:11 +01006402 if (heredoc)
6403 lhs->lhs_type = &t_list_string;
6404 else
6405 lhs->lhs_type = &t_any;
6406
6407 if (cctx->ctx_skip != SKIP_YES)
6408 {
6409 int declare_error = FALSE;
6410
6411 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6412 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6413 &lhs->lhs_type, cctx) == FAIL)
6414 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006415 if (lhs->lhs_dest != dest_local
6416 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006417 {
6418 // Specific kind of variable recognized.
6419 declare_error = is_decl;
6420 }
6421 else
6422 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006423 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006424 if (check_reserved_name(lhs->lhs_name) == FAIL)
6425 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006426
6427 if (lookup_local(var_start, lhs->lhs_varlen,
6428 &lhs->lhs_local_lvar, cctx) == OK)
6429 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6430 else
6431 {
6432 CLEAR_FIELD(lhs->lhs_arg_lvar);
6433 if (arg_exists(var_start, lhs->lhs_varlen,
6434 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6435 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6436 {
6437 if (is_decl)
6438 {
6439 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6440 return FAIL;
6441 }
6442 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6443 }
6444 }
6445 if (lhs->lhs_lvar != NULL)
6446 {
6447 if (is_decl)
6448 {
6449 semsg(_(e_variable_already_declared), lhs->lhs_name);
6450 return FAIL;
6451 }
6452 }
6453 else
6454 {
6455 int script_namespace = lhs->lhs_varlen > 1
6456 && STRNCMP(var_start, "s:", 2) == 0;
6457 int script_var = (script_namespace
6458 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006459 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006460 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006461 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006462 imported_T *import =
6463 find_imported(var_start, lhs->lhs_varlen, cctx);
6464
6465 if (script_namespace || script_var || import != NULL)
6466 {
6467 char_u *rawname = lhs->lhs_name
6468 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6469
6470 if (is_decl)
6471 {
6472 if (script_namespace)
6473 semsg(_(e_cannot_declare_script_variable_in_function),
6474 lhs->lhs_name);
6475 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006476 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006477 lhs->lhs_name);
6478 return FAIL;
6479 }
6480 else if (cctx->ctx_ufunc->uf_script_ctx_version
6481 == SCRIPT_VERSION_VIM9
6482 && script_namespace
6483 && !script_var && import == NULL)
6484 {
6485 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6486 return FAIL;
6487 }
6488
6489 lhs->lhs_dest = dest_script;
6490
6491 // existing script-local variables should have a type
6492 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6493 if (import != NULL)
6494 lhs->lhs_scriptvar_sid = import->imp_sid;
6495 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6496 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006497 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006498 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006499 lhs->lhs_scriptvar_sid, rawname,
6500 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6501 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006502 if (lhs->lhs_scriptvar_idx >= 0)
6503 {
6504 scriptitem_T *si = SCRIPT_ITEM(
6505 lhs->lhs_scriptvar_sid);
6506 svar_T *sv =
6507 ((svar_T *)si->sn_var_vals.ga_data)
6508 + lhs->lhs_scriptvar_idx;
6509 lhs->lhs_type = sv->sv_type;
6510 }
6511 }
6512 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006513 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006514 == FAIL)
6515 return FAIL;
6516 }
6517 }
6518
6519 if (declare_error)
6520 {
6521 vim9_declare_error(lhs->lhs_name);
6522 return FAIL;
6523 }
6524 }
6525
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006526 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01006527 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6528 var_end = lhs->lhs_dest_end;
6529
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006530 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006531 {
6532 if (is_decl && *var_end == ':')
6533 {
6534 char_u *p;
6535
6536 // parse optional type: "let var: type = expr"
6537 if (!VIM_ISWHITE(var_end[1]))
6538 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006539 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006540 return FAIL;
6541 }
6542 p = skipwhite(var_end + 1);
6543 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6544 if (lhs->lhs_type == NULL)
6545 return FAIL;
6546 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006547 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006548 }
6549 else if (lhs->lhs_lvar != NULL)
6550 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6551 }
6552
Bram Moolenaare42939a2021-04-05 17:11:17 +02006553 if (oplen == 3 && !heredoc
6554 && lhs->lhs_dest != dest_global
6555 && !lhs->lhs_has_index
6556 && lhs->lhs_type->tt_type != VAR_STRING
6557 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006558 {
6559 emsg(_(e_can_only_concatenate_to_string));
6560 return FAIL;
6561 }
6562
6563 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6564 && cctx->ctx_skip != SKIP_YES)
6565 {
6566 if (oplen > 1 && !heredoc)
6567 {
6568 // +=, /=, etc. require an existing variable
6569 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6570 return FAIL;
6571 }
6572 if (!is_decl)
6573 {
6574 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6575 return FAIL;
6576 }
6577
Bram Moolenaar3f327882021-03-17 20:56:38 +01006578 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006579 if ((lhs->lhs_type->tt_type == VAR_FUNC
6580 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006581 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006582 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006583
6584 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006585 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6586 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6587 if (lhs->lhs_lvar == NULL)
6588 return FAIL;
6589 lhs->lhs_new_local = TRUE;
6590 }
6591
6592 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006593 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006594 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006595 char_u *after = var_start + lhs->lhs_varlen;
6596 char_u *p;
6597
Bram Moolenaar752fc692021-01-04 21:57:11 +01006598 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006599 if (is_decl)
6600 {
6601 emsg(_(e_cannot_use_index_when_declaring_variable));
6602 return FAIL;
6603 }
6604
Bram Moolenaarc750d912021-11-29 22:02:12 +00006605 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
6606 // Only the last index is used below, if there are others
6607 // before it generate code for the expression. Thus for
6608 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6609 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006610 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006611 p = skip_index(after);
6612 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01006613 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006614 lhs->lhs_varlen_total = p - var_start;
6615 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006616 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006617 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006618 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006619 if (after > var_start + lhs->lhs_varlen)
6620 {
6621 lhs->lhs_varlen = after - var_start;
6622 lhs->lhs_dest = dest_expr;
6623 // We don't know the type before evaluating the expression,
6624 // use "any" until then.
6625 lhs->lhs_type = &t_any;
6626 }
6627
6628 if (lhs->lhs_type->tt_member == NULL)
6629 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006630 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00006631 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006632 }
6633 return OK;
6634}
6635
6636/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006637 * Figure out the LHS and check a few errors.
6638 */
6639 static int
6640compile_assign_lhs(
6641 char_u *var_start,
6642 lhs_T *lhs,
6643 int cmdidx,
6644 int is_decl,
6645 int heredoc,
6646 int oplen,
6647 cctx_T *cctx)
6648{
6649 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6650 return FAIL;
6651
6652 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6653 {
6654 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6655 return FAIL;
6656 }
6657 if (!is_decl && lhs->lhs_lvar != NULL
6658 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6659 {
6660 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6661 return FAIL;
6662 }
6663 return OK;
6664}
6665
6666/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006667 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6668 */
6669 static int
6670has_list_index(char_u *idx_start, cctx_T *cctx)
6671{
6672 char_u *p = idx_start;
6673 int save_skip;
6674
6675 if (*p != '[')
6676 return FALSE;
6677
6678 p = skipwhite(p + 1);
6679 if (*p == ':')
6680 return TRUE;
6681
6682 save_skip = cctx->ctx_skip;
6683 cctx->ctx_skip = SKIP_YES;
6684 (void)compile_expr0(&p, cctx);
6685 cctx->ctx_skip = save_skip;
6686 return *skipwhite(p) == ':';
6687}
6688
6689/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006690 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6691 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006692 */
6693 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006694compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006695 char_u *var_start,
6696 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006697 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006698 cctx_T *cctx)
6699{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006700 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006701 char_u *p;
6702 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006703 int need_white_before = TRUE;
6704 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006705
Bram Moolenaar752fc692021-01-04 21:57:11 +01006706 p = var_start + varlen;
6707 if (*p == '[')
6708 {
6709 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006710 if (*p == ':')
6711 {
6712 // empty first index, push zero
6713 r = generate_PUSHNR(cctx, 0);
6714 need_white_before = FALSE;
6715 }
6716 else
6717 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006718
6719 if (r == OK && *skipwhite(p) == ':')
6720 {
6721 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006722 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006723 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006724 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006725 empty_second = *skipwhite(p + 1) == ']';
6726 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6727 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006728 {
6729 semsg(_(e_white_space_required_before_and_after_str_at_str),
6730 ":", p);
6731 return FAIL;
6732 }
6733 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006734 if (*p == ']')
6735 // empty second index, push "none"
6736 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6737 else
6738 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006739 }
6740
Bram Moolenaar752fc692021-01-04 21:57:11 +01006741 if (r == OK && *skipwhite(p) != ']')
6742 {
6743 // this should not happen
Bram Moolenaare1242042021-12-16 20:56:57 +00006744 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar752fc692021-01-04 21:57:11 +01006745 r = FAIL;
6746 }
6747 }
6748 else // if (*p == '.')
6749 {
6750 char_u *key_end = to_name_end(p + 1, TRUE);
6751 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6752
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006753 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006754 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006755 return r;
6756}
6757
6758/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006759 * For a LHS with an index, load the variable to be indexed.
6760 */
6761 static int
6762compile_load_lhs(
6763 lhs_T *lhs,
6764 char_u *var_start,
6765 type_T *rhs_type,
6766 cctx_T *cctx)
6767{
6768 if (lhs->lhs_dest == dest_expr)
6769 {
6770 size_t varlen = lhs->lhs_varlen;
6771 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006772 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006773 char_u *p = var_start;
6774 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006775 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006776
Bram Moolenaare97976b2021-08-01 13:17:17 +02006777 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6778 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006779 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006780 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6781 res = compile_expr0(&p, cctx);
6782 var_start[varlen] = c;
6783 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6784 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006785 {
6786 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006787 if (res != FAIL)
Bram Moolenaare1242042021-12-16 20:56:57 +00006788 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006789 return FAIL;
6790 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006791
6792 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6793 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6794 // now we can properly check the type
6795 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6796 && rhs_type != &t_void
6797 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6798 FALSE, FALSE) == FAIL)
6799 return FAIL;
6800 }
6801 else
6802 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6803 lhs->lhs_lvar, lhs->lhs_type);
6804 return OK;
6805}
6806
6807/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006808 * Produce code for loading "lhs" and also take care of an index.
6809 * Return OK/FAIL.
6810 */
6811 static int
6812compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6813{
6814 compile_load_lhs(lhs, var_start, NULL, cctx);
6815
6816 if (lhs->lhs_has_index)
6817 {
6818 int range = FALSE;
6819
6820 // Get member from list or dict. First compile the
6821 // index value.
6822 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6823 return FAIL;
6824 if (range)
6825 {
6826 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6827 var_start);
6828 return FAIL;
6829 }
6830
6831 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006832 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006833 return FAIL;
6834 }
6835 return OK;
6836}
6837
6838/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006839 * Assignment to a list or dict member, or ":unlet" for the item, using the
6840 * information in "lhs".
6841 * Returns OK or FAIL.
6842 */
6843 static int
6844compile_assign_unlet(
6845 char_u *var_start,
6846 lhs_T *lhs,
6847 int is_assign,
6848 type_T *rhs_type,
6849 cctx_T *cctx)
6850{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006851 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006852 garray_T *stack = &cctx->ctx_type_stack;
6853 int range = FALSE;
6854
Bram Moolenaar68452172021-04-12 21:21:02 +02006855 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006856 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006857 if (is_assign && range
6858 && lhs->lhs_type->tt_type != VAR_LIST
6859 && lhs->lhs_type != &t_blob
6860 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006861 {
6862 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6863 return FAIL;
6864 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006865
6866 if (lhs->lhs_type == &t_any)
6867 {
6868 // Index on variable of unknown type: check at runtime.
6869 dest_type = VAR_ANY;
6870 }
6871 else
6872 {
6873 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006874 if (dest_type == VAR_DICT && range)
6875 {
6876 emsg(e_cannot_use_range_with_dictionary);
6877 return FAIL;
6878 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006879 if (dest_type == VAR_DICT
6880 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006881 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006882 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006883 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006884 type_T *type;
6885
6886 if (range)
6887 {
6888 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6889 if (need_type(type, &t_number,
6890 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006891 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006892 }
6893 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006894 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006895 && need_type(type, &t_number,
6896 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006897 return FAIL;
6898 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006899 }
6900
6901 // Load the dict or list. On the stack we then have:
6902 // - value (for assignment, not for :unlet)
6903 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006904 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006905 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006906 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6907 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006908
Bram Moolenaar68452172021-04-12 21:21:02 +02006909 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6910 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006911 {
6912 if (is_assign)
6913 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006914 if (range)
6915 {
6916 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6917 return FAIL;
6918 }
6919 else
6920 {
6921 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006922
Bram Moolenaar68452172021-04-12 21:21:02 +02006923 if (isn == NULL)
6924 return FAIL;
6925 isn->isn_arg.vartype = dest_type;
6926 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006927 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006928 else if (range)
6929 {
6930 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6931 return FAIL;
6932 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006933 else
6934 {
6935 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6936 return FAIL;
6937 }
6938 }
6939 else
6940 {
6941 emsg(_(e_indexable_type_required));
6942 return FAIL;
6943 }
6944
6945 return OK;
6946}
6947
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006948/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006949 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006950 * "let name"
6951 * "var name = expr"
6952 * "final name = expr"
6953 * "const name = expr"
6954 * "name = expr"
6955 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006956 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006957 * Return NULL for an error.
6958 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959 */
6960 static char_u *
6961compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6962{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006963 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006964 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006965 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966 char_u *ret = NULL;
6967 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006968 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006970 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006972 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974 int oplen = 0;
6975 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006976 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006977 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006979 int is_decl = is_decl_command(cmdidx);
6980 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006981 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006983 // Skip over the "var" or "[var, var]" to get to any "=".
6984 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6985 if (p == NULL)
6986 return *arg == '[' ? arg : NULL;
6987
Bram Moolenaar752fc692021-01-04 21:57:11 +01006988 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990 sp = p;
6991 p = skipwhite(p);
6992 op = p;
6993 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006994
6995 if (var_count > 0 && oplen == 0)
6996 // can be something like "[1, 2]->func()"
6997 return arg;
6998
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006999 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02007001 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007002 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007003 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007004 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
7005 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02007006 if (VIM_ISWHITE(eap->cmd[2]))
7007 {
7008 semsg(_(e_no_white_space_allowed_after_str_str),
7009 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
7010 return NULL;
7011 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007012 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
7013 oplen = 2;
7014 incdec = TRUE;
7015 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017 if (heredoc)
7018 {
7019 list_T *l;
7020 listitem_T *li;
7021
7022 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02007023 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02007025 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02007026 if (l == NULL)
7027 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028
Bram Moolenaar078269b2020-09-21 20:35:55 +02007029 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007030 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02007031 // Push each line and the create the list.
7032 FOR_ALL_LIST_ITEMS(l, li)
7033 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02007034 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02007035 li->li_tv.vval.v_string = NULL;
7036 }
7037 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007039 list_free(l);
7040 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007041 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007043 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007044 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007045 char_u *wp;
7046
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007047 // for "[var, var] = expr" evaluate the expression here, loop over the
7048 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007049 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02007050
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007051 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01007052 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007053 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007054 if (compile_expr0(&p, cctx) == FAIL)
7055 return NULL;
7056 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007058 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007060 type_T *stacktype;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007061 int needed_list_len;
7062 int did_check = FALSE;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007063
Bram Moolenaarec5929d2020-04-07 20:53:39 +02007064 stacktype = stack->ga_len == 0 ? &t_void
7065 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007066 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007067 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007068 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007069 goto theend;
7070 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01007071 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007072 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007073 goto theend;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007074 // If a constant list was used we can check the length right here.
7075 needed_list_len = semicolon ? var_count - 1 : var_count;
7076 if (instr->ga_len > 0)
7077 {
7078 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
7079
7080 if (isn->isn_type == ISN_NEWLIST)
7081 {
7082 did_check = TRUE;
7083 if (semicolon ? isn->isn_arg.number < needed_list_len
7084 : isn->isn_arg.number != needed_list_len)
7085 {
7086 semsg(_(e_expected_nr_items_but_got_nr),
7087 needed_list_len, isn->isn_arg.number);
7088 goto theend;
7089 }
7090 }
7091 }
7092 if (!did_check)
7093 generate_CHECKLEN(cctx, needed_list_len, semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007094 if (stacktype->tt_member != NULL)
7095 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007096 }
7097 }
7098
7099 /*
7100 * Loop over variables in "[var, var] = expr".
7101 * For "var = expr" and "let var: type" this is done only once.
7102 */
7103 if (var_count > 0)
7104 var_start = skipwhite(arg + 1); // skip over the "["
7105 else
7106 var_start = arg;
7107 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
7108 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007109 int instr_count = -1;
7110 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007111
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02007112 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
7113 {
7114 // Ignore underscore in "[a, _, b] = list".
7115 if (var_count > 0)
7116 {
7117 var_start = skipwhite(var_start + 2);
7118 continue;
7119 }
7120 emsg(_(e_cannot_use_underscore_here));
7121 goto theend;
7122 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007123 vim_free(lhs.lhs_name);
7124
7125 /*
7126 * Figure out the LHS type and other properties.
7127 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007128 if (compile_assign_lhs(var_start, &lhs, cmdidx,
7129 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007130 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02007131 if (heredoc)
7132 {
7133 SOURCING_LNUM = start_lnum;
7134 if (lhs.lhs_has_type
7135 && need_type(&t_list_string, lhs.lhs_type,
7136 -1, 0, cctx, FALSE, FALSE) == FAIL)
7137 goto theend;
7138 }
7139 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007140 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007141 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007142 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007143 if (oplen > 0 && var_count == 0)
7144 {
7145 // skip over the "=" and the expression
7146 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02007147 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007148 }
7149 }
7150 else if (oplen > 0)
7151 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007152 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01007153 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007154
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007155 // for "+=", "*=", "..=" etc. first load the current value
7156 if (*op != '='
7157 && compile_load_lhs_with_index(&lhs, var_start,
7158 cctx) == FAIL)
7159 goto theend;
7160
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007161 // For "var = expr" evaluate the expression.
7162 if (var_count == 0)
7163 {
7164 int r;
7165
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007166 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007167 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007168 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007169 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007170 r = generate_PUSHNR(cctx, 1);
7171 }
7172 else
7173 {
7174 // Temporarily hide the new local variable here, it is
7175 // not available to this expression.
7176 if (lhs.lhs_new_local)
7177 --cctx->ctx_locals.ga_len;
7178 wp = op + oplen;
7179 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7180 {
7181 if (lhs.lhs_new_local)
7182 ++cctx->ctx_locals.ga_len;
7183 goto theend;
7184 }
7185 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007186 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007187 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007188 if (r == FAIL)
7189 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007190 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007191 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007192 else if (semicolon && var_idx == var_count - 1)
7193 {
7194 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007195 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007196 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7197 goto theend;
7198 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007199 else
7200 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007201 // For "[var, var] = expr" get the "var_idx" item from the
7202 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007203 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007204 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007205 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007206
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007207 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007208 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007209 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007210 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007211 if ((rhs_type->tt_type == VAR_FUNC
7212 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007213 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007214 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007215 goto theend;
7216
Bram Moolenaar752fc692021-01-04 21:57:11 +01007217 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007218 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007219 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007220 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007221 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007222 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007223 }
7224 else
7225 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007226 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007227 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007228 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007229 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007230 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007231 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007232 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007233 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007234 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007235 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007236 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007237 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007238 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007239 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007240 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007241 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007242
Bram Moolenaar77709b12021-04-03 21:01:01 +02007243 // Without operator check type here, otherwise below.
7244 // Use the line number of the assignment.
7245 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007246 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7247 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007248 // If assigning to a list or dict member, use the
7249 // member type. Not for "list[:] =".
7250 if (lhs.lhs_has_index
7251 && !has_list_index(var_start + lhs.lhs_varlen,
7252 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007253 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007254 if (need_type_where(rhs_type, use_type, -1, where,
7255 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007256 goto theend;
7257 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007258 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007259 else
7260 {
7261 type_T *lhs_type = lhs.lhs_member_type;
7262
7263 // Special case: assigning to @# can use a number or a
7264 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007265 // Also: can assign a number to a float.
7266 if ((lhs_type == &t_number_or_string
7267 || lhs_type == &t_float)
7268 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007269 lhs_type = &t_number;
7270 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007271 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007272 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007275 else if (cmdidx == CMD_final)
7276 {
7277 emsg(_(e_final_requires_a_value));
7278 goto theend;
7279 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007280 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007282 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007283 goto theend;
7284 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00007285 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option
7286 || lhs.lhs_dest == dest_func_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007287 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007288 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007289 goto theend;
7290 }
7291 else
7292 {
7293 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007294 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007295 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007296 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007297 {
7298 case VAR_BOOL:
7299 generate_PUSHBOOL(cctx, VVAL_FALSE);
7300 break;
7301 case VAR_FLOAT:
7302#ifdef FEAT_FLOAT
7303 generate_PUSHF(cctx, 0.0);
7304#endif
7305 break;
7306 case VAR_STRING:
7307 generate_PUSHS(cctx, NULL);
7308 break;
7309 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007310 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007311 break;
7312 case VAR_FUNC:
7313 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7314 break;
7315 case VAR_LIST:
7316 generate_NEWLIST(cctx, 0);
7317 break;
7318 case VAR_DICT:
7319 generate_NEWDICT(cctx, 0);
7320 break;
7321 case VAR_JOB:
7322 generate_PUSHJOB(cctx, NULL);
7323 break;
7324 case VAR_CHANNEL:
7325 generate_PUSHCHANNEL(cctx, NULL);
7326 break;
7327 case VAR_NUMBER:
7328 case VAR_UNKNOWN:
7329 case VAR_ANY:
7330 case VAR_PARTIAL:
7331 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007332 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007333 case VAR_SPECIAL: // cannot happen
7334 generate_PUSHNR(cctx, 0);
7335 break;
7336 }
7337 }
7338 if (var_count == 0)
7339 end = p;
7340 }
7341
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007342 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007343 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007344 break;
7345
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007346 if (oplen > 0 && *op != '=')
7347 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007348 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007349 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007350
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007351 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007352 {
7353 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7354 goto theend;
7355 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007356 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007357 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007358 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007359 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7360 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007361#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007362 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007363 !(expected == &t_float && (stacktype == &t_number
7364 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007365#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007366 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007367 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007368 goto theend;
7369 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007370
7371 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007372 {
7373 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7374 goto theend;
7375 }
7376 else if (*op == '+')
7377 {
7378 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007379 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007380 lhs.lhs_member_type, stacktype,
7381 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007382 goto theend;
7383 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007384 else if (generate_two_op(cctx, op) == FAIL)
7385 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007388 // Use the line number of the assignment for store instruction.
7389 save_lnum = cctx->ctx_lnum;
7390 cctx->ctx_lnum = start_lnum - 1;
7391
Bram Moolenaar752fc692021-01-04 21:57:11 +01007392 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007393 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007394 // Use the info in "lhs" to store the value at the index in the
7395 // list or dict.
7396 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7397 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007398 {
7399 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007400 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007401 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007402 }
7403 else
7404 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007405 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007406 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007407 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007408 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007409 generate_LOCKCONST(cctx);
7410
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007411 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007412 && (lhs.lhs_type->tt_type == VAR_DICT
7413 || lhs.lhs_type->tt_type == VAR_LIST)
7414 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007415 && !(lhs.lhs_type->tt_member == &t_any
7416 && oplen > 0
7417 && rhs_type != NULL
7418 && rhs_type->tt_type == lhs.lhs_type->tt_type
7419 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007420 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007421 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007422 // also in legacy script. Not for "list<any> = val", then the
7423 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007424 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007425
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007426 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007427 {
7428 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007429 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007430 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007431 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007432 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007433
7434 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00007435 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007436 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007437
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007438 // For "[var, var] = expr" drop the "expr" value.
7439 // Also for "[var, var; _] = expr".
7440 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007441 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007442 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007443 goto theend;
7444 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007445
Bram Moolenaarb2097502020-07-19 17:17:02 +02007446 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447
7448theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007449 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 return ret;
7451}
7452
7453/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007454 * Check for an assignment at "eap->cmd", compile it if found.
7455 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7456 */
7457 static int
7458may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7459{
7460 char_u *pskip;
7461 char_u *p;
7462
7463 // Assuming the command starts with a variable or function name,
7464 // find what follows.
7465 // Skip over "var.member", "var[idx]" and the like.
7466 // Also "&opt = val", "$ENV = val" and "@r = val".
7467 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7468 ? eap->cmd + 1 : eap->cmd;
7469 p = to_name_end(pskip, TRUE);
7470 if (p > eap->cmd && *p != NUL)
7471 {
7472 char_u *var_end;
7473 int oplen;
7474 int heredoc;
7475
7476 if (eap->cmd[0] == '@')
7477 var_end = eap->cmd + 2;
7478 else
7479 var_end = find_name_end(pskip, NULL, NULL,
7480 FNE_CHECK_START | FNE_INCL_BR);
7481 oplen = assignment_len(skipwhite(var_end), &heredoc);
7482 if (oplen > 0)
7483 {
7484 size_t len = p - eap->cmd;
7485
7486 // Recognize an assignment if we recognize the variable
7487 // name:
7488 // "g:var = expr"
7489 // "local = expr" where "local" is a local var.
7490 // "script = expr" where "script" is a script-local var.
7491 // "import = expr" where "import" is an imported var
7492 // "&opt = expr"
7493 // "$ENV = expr"
7494 // "@r = expr"
7495 if (*eap->cmd == '&'
7496 || *eap->cmd == '$'
7497 || *eap->cmd == '@'
7498 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007499 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007500 {
7501 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7502 if (*line == NULL || *line == eap->cmd)
7503 return FAIL;
7504 return OK;
7505 }
7506 }
7507 }
7508
7509 if (*eap->cmd == '[')
7510 {
7511 // [var, var] = expr
7512 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7513 if (*line == NULL)
7514 return FAIL;
7515 if (*line != eap->cmd)
7516 return OK;
7517 }
7518 return NOTDONE;
7519}
7520
7521/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007522 * Check if "name" can be "unlet".
7523 */
7524 int
7525check_vim9_unlet(char_u *name)
7526{
7527 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7528 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007529 // "unlet s:var" is allowed in legacy script.
7530 if (*name == 's' && !script_is_vim9())
7531 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007532 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007533 return FAIL;
7534 }
7535 return OK;
7536}
7537
7538/*
7539 * Callback passed to ex_unletlock().
7540 */
7541 static int
7542compile_unlet(
7543 lval_T *lvp,
7544 char_u *name_end,
7545 exarg_T *eap,
7546 int deep UNUSED,
7547 void *coookie)
7548{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007549 cctx_T *cctx = coookie;
7550 char_u *p = lvp->ll_name;
7551 int cc = *name_end;
7552 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007553
Bram Moolenaar752fc692021-01-04 21:57:11 +01007554 if (cctx->ctx_skip == SKIP_YES)
7555 return OK;
7556
7557 *name_end = NUL;
7558 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007559 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007560 // :unlet $ENV_VAR
7561 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7562 }
7563 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7564 {
7565 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007566
Bram Moolenaar752fc692021-01-04 21:57:11 +01007567 // This is similar to assigning: lookup the list/dict, compile the
7568 // idx/key. Then instead of storing the value unlet the item.
7569 // unlet {list}[idx]
7570 // unlet {dict}[key] dict.key
7571 //
7572 // Figure out the LHS type and other properties.
7573 //
7574 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7575
7576 // : unlet an indexed item
7577 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007578 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007579 iemsg("called compile_lhs() without an index");
7580 ret = FAIL;
7581 }
7582 else
7583 {
7584 // Use the info in "lhs" to unlet the item at the index in the
7585 // list or dict.
7586 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007587 }
7588
Bram Moolenaar752fc692021-01-04 21:57:11 +01007589 vim_free(lhs.lhs_name);
7590 }
7591 else if (check_vim9_unlet(p) == FAIL)
7592 {
7593 ret = FAIL;
7594 }
7595 else
7596 {
7597 // Normal name. Only supports g:, w:, t: and b: namespaces.
7598 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007599 }
7600
Bram Moolenaar752fc692021-01-04 21:57:11 +01007601 *name_end = cc;
7602 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007603}
7604
7605/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007606 * Callback passed to ex_unletlock().
7607 */
7608 static int
7609compile_lock_unlock(
7610 lval_T *lvp,
7611 char_u *name_end,
7612 exarg_T *eap,
7613 int deep UNUSED,
7614 void *coookie)
7615{
7616 cctx_T *cctx = coookie;
7617 int cc = *name_end;
7618 char_u *p = lvp->ll_name;
7619 int ret = OK;
7620 size_t len;
7621 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007622 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007623
7624 if (cctx->ctx_skip == SKIP_YES)
7625 return OK;
7626
7627 // Cannot use :lockvar and :unlockvar on local variables.
7628 if (p[1] != ':')
7629 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007630 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007631
7632 if (lookup_local(p, end - p, NULL, cctx) == OK)
7633 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007634 char_u *s = p;
7635
7636 if (*end != '.' && *end != '[')
7637 {
7638 emsg(_(e_cannot_lock_unlock_local_variable));
7639 return FAIL;
7640 }
7641
7642 // For "d.member" put the local variable on the stack, it will be
7643 // passed to ex_lockvar() indirectly.
7644 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7645 return FAIL;
7646 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007647 }
7648 }
7649
7650 // Checking is done at runtime.
7651 *name_end = NUL;
7652 len = name_end - p + 20;
7653 buf = alloc(len);
7654 if (buf == NULL)
7655 ret = FAIL;
7656 else
7657 {
7658 vim_snprintf((char *)buf, len, "%s %s",
7659 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7660 p);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00007661 ret = generate_EXEC_copy(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007662
7663 vim_free(buf);
7664 *name_end = cc;
7665 }
7666 return ret;
7667}
7668
7669/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007670 * compile "unlet var", "lock var" and "unlock var"
7671 * "arg" points to "var".
7672 */
7673 static char_u *
7674compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7675{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007676 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7677 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7678 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007679 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7680}
7681
7682/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7684 */
7685 static int
7686compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7687{
7688 garray_T *instr = &cctx->ctx_instr;
7689 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7690
7691 if (endlabel == NULL)
7692 return FAIL;
7693 endlabel->el_next = *el;
7694 *el = endlabel;
7695 endlabel->el_end_label = instr->ga_len;
7696
7697 generate_JUMP(cctx, when, 0);
7698 return OK;
7699}
7700
7701 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007702compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007703{
7704 garray_T *instr = &cctx->ctx_instr;
7705
7706 while (*el != NULL)
7707 {
7708 endlabel_T *cur = (*el);
7709 isn_T *isn;
7710
7711 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007712 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713 *el = cur->el_next;
7714 vim_free(cur);
7715 }
7716}
7717
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007718 static void
7719compile_free_jump_to_end(endlabel_T **el)
7720{
7721 while (*el != NULL)
7722 {
7723 endlabel_T *cur = (*el);
7724
7725 *el = cur->el_next;
7726 vim_free(cur);
7727 }
7728}
7729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007730/*
7731 * Create a new scope and set up the generic items.
7732 */
7733 static scope_T *
7734new_scope(cctx_T *cctx, scopetype_T type)
7735{
7736 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7737
7738 if (scope == NULL)
7739 return NULL;
7740 scope->se_outer = cctx->ctx_scope;
7741 cctx->ctx_scope = scope;
7742 scope->se_type = type;
7743 scope->se_local_count = cctx->ctx_locals.ga_len;
7744 return scope;
7745}
7746
7747/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007748 * Free the current scope and go back to the outer scope.
7749 */
7750 static void
7751drop_scope(cctx_T *cctx)
7752{
7753 scope_T *scope = cctx->ctx_scope;
7754
7755 if (scope == NULL)
7756 {
7757 iemsg("calling drop_scope() without a scope");
7758 return;
7759 }
7760 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007761 switch (scope->se_type)
7762 {
7763 case IF_SCOPE:
7764 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7765 case FOR_SCOPE:
7766 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7767 case WHILE_SCOPE:
7768 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7769 case TRY_SCOPE:
7770 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7771 case NO_SCOPE:
7772 case BLOCK_SCOPE:
7773 break;
7774 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007775 vim_free(scope);
7776}
7777
7778/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779 * compile "if expr"
7780 *
7781 * "if expr" Produces instructions:
7782 * EVAL expr Push result of "expr"
7783 * JUMP_IF_FALSE end
7784 * ... body ...
7785 * end:
7786 *
7787 * "if expr | else" Produces instructions:
7788 * EVAL expr Push result of "expr"
7789 * JUMP_IF_FALSE else
7790 * ... body ...
7791 * JUMP_ALWAYS end
7792 * else:
7793 * ... body ...
7794 * end:
7795 *
7796 * "if expr1 | elseif expr2 | else" Produces instructions:
7797 * EVAL expr Push result of "expr"
7798 * JUMP_IF_FALSE elseif
7799 * ... body ...
7800 * JUMP_ALWAYS end
7801 * elseif:
7802 * EVAL expr Push result of "expr"
7803 * JUMP_IF_FALSE else
7804 * ... body ...
7805 * JUMP_ALWAYS end
7806 * else:
7807 * ... body ...
7808 * end:
7809 */
7810 static char_u *
7811compile_if(char_u *arg, cctx_T *cctx)
7812{
7813 char_u *p = arg;
7814 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007815 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007817 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007818 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007819
Bram Moolenaara5565e42020-05-09 15:44:01 +02007820 CLEAR_FIELD(ppconst);
7821 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007822 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007823 clear_ppconst(&ppconst);
7824 return NULL;
7825 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007826 if (!ends_excmd2(arg, skipwhite(p)))
7827 {
7828 semsg(_(e_trailing_arg), p);
7829 return NULL;
7830 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007831 if (cctx->ctx_skip == SKIP_YES)
7832 clear_ppconst(&ppconst);
7833 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007834 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007835 int error = FALSE;
7836 int v;
7837
Bram Moolenaara5565e42020-05-09 15:44:01 +02007838 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007839 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007840 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007841 if (error)
7842 return NULL;
7843 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007844 }
7845 else
7846 {
7847 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007848 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007849 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007850 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007851 if (bool_on_stack(cctx) == FAIL)
7852 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007854
Bram Moolenaara91a7132021-03-25 21:12:15 +01007855 // CMDMOD_REV must come before the jump
7856 generate_undo_cmdmods(cctx);
7857
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007858 scope = new_scope(cctx, IF_SCOPE);
7859 if (scope == NULL)
7860 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007861 scope->se_skip_save = skip_save;
7862 // "is_had_return" will be reset if any block does not end in :return
7863 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007864
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007865 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007866 {
7867 // "where" is set when ":elseif", "else" or ":endif" is found
7868 scope->se_u.se_if.is_if_label = instr->ga_len;
7869 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7870 }
7871 else
7872 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873
Bram Moolenaarced68a02021-01-24 17:53:47 +01007874#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007875 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007876 && skip_save != SKIP_YES)
7877 {
7878 // generated a profile start, need to generate a profile end, since it
7879 // won't be done after returning
7880 cctx->ctx_skip = SKIP_NOT;
7881 generate_instr(cctx, ISN_PROF_END);
7882 cctx->ctx_skip = SKIP_YES;
7883 }
7884#endif
7885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007886 return p;
7887}
7888
7889 static char_u *
7890compile_elseif(char_u *arg, cctx_T *cctx)
7891{
7892 char_u *p = arg;
7893 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar90770b72021-11-30 20:57:38 +00007894 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007895 isn_T *isn;
7896 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007897 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007898 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007899
7900 if (scope == NULL || scope->se_type != IF_SCOPE)
7901 {
7902 emsg(_(e_elseif_without_if));
7903 return NULL;
7904 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007905 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007906 if (!cctx->ctx_had_return)
7907 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908
Bram Moolenaarced68a02021-01-24 17:53:47 +01007909 if (cctx->ctx_skip == SKIP_NOT)
7910 {
7911 // previous block was executed, this one and following will not
7912 cctx->ctx_skip = SKIP_YES;
7913 scope->se_u.se_if.is_seen_skip_not = TRUE;
7914 }
7915 if (scope->se_u.se_if.is_seen_skip_not)
7916 {
7917 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007918 // Do not count the "elseif" for profiling and cmdmod
7919 instr->ga_len = current_instr_idx(cctx);
7920
Bram Moolenaarced68a02021-01-24 17:53:47 +01007921 skip_expr_cctx(&p, cctx);
7922 return p;
7923 }
7924
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007925 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007926 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007927 int moved_cmdmod = FALSE;
7928 int saved_debug = FALSE;
7929 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007930
7931 // Move any CMDMOD instruction to after the jump
7932 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7933 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007934 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007935 return NULL;
7936 ((isn_T *)instr->ga_data)[instr->ga_len] =
7937 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7938 --instr->ga_len;
7939 moved_cmdmod = TRUE;
7940 }
7941
Bram Moolenaar093165c2021-08-22 13:35:31 +02007942 // Remove the already generated ISN_DEBUG, it is written below the
7943 // ISN_FOR instruction.
7944 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7945 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7946 .isn_type == ISN_DEBUG)
7947 {
7948 --instr->ga_len;
7949 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7950 saved_debug = TRUE;
7951 }
7952
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007953 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007955 return NULL;
7956 // previous "if" or "elseif" jumps here
7957 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7958 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007959
Bram Moolenaara91a7132021-03-25 21:12:15 +01007960 if (moved_cmdmod)
7961 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007962
7963 if (saved_debug)
7964 {
7965 // move the debug instruction here
7966 if (GA_GROW_FAILS(instr, 1))
7967 return NULL;
7968 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7969 ++instr->ga_len;
7970 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007972
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007973 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007974 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007975 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007976 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007977 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007978#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007979 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007980 // the previous block was skipped, need to profile this line
7981 generate_instr(cctx, ISN_PROF_START);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007982#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007983 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007984 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007985 generate_instr_debug(cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007986 }
Bram Moolenaar90770b72021-11-30 20:57:38 +00007987
7988 instr_count = instr->ga_len;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007989 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007990 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007991 clear_ppconst(&ppconst);
7992 return NULL;
7993 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007994 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007995 if (!ends_excmd2(arg, skipwhite(p)))
7996 {
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007997 clear_ppconst(&ppconst);
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007998 semsg(_(e_trailing_arg), p);
7999 return NULL;
8000 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02008001 if (scope->se_skip_save == SKIP_YES)
8002 clear_ppconst(&ppconst);
8003 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02008004 {
Bram Moolenaar13106602020-10-04 16:06:05 +02008005 int error = FALSE;
8006 int v;
8007
Bram Moolenaarfad27422021-11-30 21:58:19 +00008008 // The expression result is a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02008009 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
8010 if (error)
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00008011 {
8012 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02008013 return NULL;
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00008014 }
Bram Moolenaar13106602020-10-04 16:06:05 +02008015 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02008016 clear_ppconst(&ppconst);
8017 scope->se_u.se_if.is_if_label = -1;
8018 }
8019 else
8020 {
8021 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008022 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02008023 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008024 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008025 if (bool_on_stack(cctx) == FAIL)
8026 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008027
Bram Moolenaara91a7132021-03-25 21:12:15 +01008028 // CMDMOD_REV must come before the jump
8029 generate_undo_cmdmods(cctx);
8030
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008031 // "where" is set when ":elseif", "else" or ":endif" is found
8032 scope->se_u.se_if.is_if_label = instr->ga_len;
8033 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
8034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008035
8036 return p;
8037}
8038
8039 static char_u *
8040compile_else(char_u *arg, cctx_T *cctx)
8041{
8042 char_u *p = arg;
8043 garray_T *instr = &cctx->ctx_instr;
8044 isn_T *isn;
8045 scope_T *scope = cctx->ctx_scope;
8046
8047 if (scope == NULL || scope->se_type != IF_SCOPE)
8048 {
8049 emsg(_(e_else_without_if));
8050 return NULL;
8051 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01008052 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008053 if (!cctx->ctx_had_return)
8054 scope->se_u.se_if.is_had_return = FALSE;
8055 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008056
Bram Moolenaarced68a02021-01-24 17:53:47 +01008057#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008058 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01008059 {
8060 if (cctx->ctx_skip == SKIP_NOT
8061 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8062 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02008063 // the previous block was executed, do not count "else" for
8064 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01008065 --instr->ga_len;
8066 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
8067 {
8068 // the previous block was not executed, this one will, do count the
8069 // "else" for profiling
8070 cctx->ctx_skip = SKIP_NOT;
8071 generate_instr(cctx, ISN_PROF_END);
8072 generate_instr(cctx, ISN_PROF_START);
8073 cctx->ctx_skip = SKIP_YES;
8074 }
8075 }
8076#endif
8077
8078 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008079 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008080 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008081 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008082 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008083 if (!cctx->ctx_had_return
8084 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
8085 JUMP_ALWAYS, cctx) == FAIL)
8086 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008087 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008088
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008089 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008090 {
8091 if (scope->se_u.se_if.is_if_label >= 0)
8092 {
8093 // previous "if" or "elseif" jumps here
8094 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8095 isn->isn_arg.jump.jump_where = instr->ga_len;
8096 scope->se_u.se_if.is_if_label = -1;
8097 }
8098 }
8099
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008100 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008101 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
8102 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008103
8104 return p;
8105}
8106
8107 static char_u *
8108compile_endif(char_u *arg, cctx_T *cctx)
8109{
8110 scope_T *scope = cctx->ctx_scope;
8111 ifscope_T *ifscope;
8112 garray_T *instr = &cctx->ctx_instr;
8113 isn_T *isn;
8114
Bram Moolenaarfa984412021-03-25 22:15:28 +01008115 if (misplaced_cmdmod(cctx))
8116 return NULL;
8117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008118 if (scope == NULL || scope->se_type != IF_SCOPE)
8119 {
8120 emsg(_(e_endif_without_if));
8121 return NULL;
8122 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008123 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008124 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008125 if (!cctx->ctx_had_return)
8126 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008127
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008128 if (scope->se_u.se_if.is_if_label >= 0)
8129 {
8130 // previous "if" or "elseif" jumps here
8131 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8132 isn->isn_arg.jump.jump_where = instr->ga_len;
8133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008134 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008135 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01008136
8137#ifdef FEAT_PROFILE
8138 // even when skipping we count the endif as executed, unless the block it's
8139 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02008140 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01008141 && scope->se_skip_save != SKIP_YES)
8142 {
8143 cctx->ctx_skip = SKIP_NOT;
8144 generate_instr(cctx, ISN_PROF_START);
8145 }
8146#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02008147 cctx->ctx_skip = scope->se_skip_save;
8148
8149 // If all the blocks end in :return and there is an :else then the
8150 // had_return flag is set.
8151 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008152
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008153 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008154 return arg;
8155}
8156
8157/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01008158 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008159 *
8160 * Produces instructions:
8161 * PUSHNR -1
8162 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01008163 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008164 * top: FOR loop-idx, end Increment index, use list on bottom of stack
8165 * - if beyond end, jump to "end"
8166 * - otherwise get item from list and push it
8167 * STORE var Store item in "var"
8168 * ... body ...
8169 * JUMP top Jump back to repeat
8170 * end: DROP Drop the result of "expr"
8171 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008172 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
8173 * UNPACK 2 Split item in 2
8174 * STORE var1 Store item in "var1"
8175 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008176 */
8177 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008178compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008179{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008180 char_u *arg;
8181 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008182 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008183 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008184 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008185 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008186 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008187 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008190 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008191 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008192 lvar_T *loop_lvar; // loop iteration variable
8193 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008194 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008195 type_T *item_type = &t_any;
8196 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008197 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008198
Bram Moolenaar792f7862020-11-23 08:31:18 +01008199 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008200 if (p == NULL)
8201 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008202 if (var_count == 0)
8203 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008204 else
8205 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008206
8207 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008208 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008209 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8210 return NULL;
8211 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008212 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008213 if (*p == ':' && wp != p)
8214 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8215 else
8216 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008217 return NULL;
8218 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008219 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008220 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8221 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008222
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008223 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8224 // instruction.
8225 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8226 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8227 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008228 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008229 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008230 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8231 .isn_arg.debug.dbg_break_lnum;
8232 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008234 scope = new_scope(cctx, FOR_SCOPE);
8235 if (scope == NULL)
8236 return NULL;
8237
Bram Moolenaar792f7862020-11-23 08:31:18 +01008238 // Reserve a variable to store the loop iteration counter and initialize it
8239 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008240 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8241 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008242 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008243 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008244 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008245 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008246 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008247 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008248
8249 // compile "expr", it remains on the stack until "endfor"
8250 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008251 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008252 {
8253 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008254 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008255 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008256 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008257
rbtnnbebf0692021-08-21 17:26:50 +02008258 if (cctx->ctx_skip != SKIP_YES)
8259 {
8260 // If we know the type of "var" and it is a not a supported type we can
8261 // give an error now.
8262 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8263 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008264 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008265 {
rbtnnbebf0692021-08-21 17:26:50 +02008266 semsg(_(e_for_loop_on_str_not_supported),
8267 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008268 drop_scope(cctx);
8269 return NULL;
8270 }
rbtnnbebf0692021-08-21 17:26:50 +02008271
8272 if (vartype->tt_type == VAR_STRING)
8273 item_type = &t_string;
8274 else if (vartype->tt_type == VAR_BLOB)
8275 item_type = &t_number;
8276 else if (vartype->tt_type == VAR_LIST
8277 && vartype->tt_member->tt_type != VAR_ANY)
8278 {
8279 if (!var_list)
8280 item_type = vartype->tt_member;
8281 else if (vartype->tt_member->tt_type == VAR_LIST
8282 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
rbtnnbebf0692021-08-21 17:26:50 +02008283 item_type = vartype->tt_member->tt_member;
8284 }
8285
8286 // CMDMOD_REV must come before the FOR instruction.
8287 generate_undo_cmdmods(cctx);
8288
8289 // "for_end" is set when ":endfor" is found
8290 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8291
8292 generate_FOR(cctx, loop_lvar->lv_idx);
8293
8294 arg = arg_start;
8295 if (var_list)
8296 {
8297 generate_UNPACK(cctx, var_count, semicolon);
8298 arg = skipwhite(arg + 1); // skip white after '['
8299
8300 // the list item is replaced by a number of items
8301 if (GA_GROW_FAILS(stack, var_count - 1))
8302 {
8303 drop_scope(cctx);
8304 return NULL;
8305 }
8306 --stack->ga_len;
8307 for (idx = 0; idx < var_count; ++idx)
8308 {
8309 ((type_T **)stack->ga_data)[stack->ga_len] =
8310 (semicolon && idx == 0) ? vartype : item_type;
8311 ++stack->ga_len;
8312 }
8313 }
8314
Bram Moolenaar792f7862020-11-23 08:31:18 +01008315 for (idx = 0; idx < var_count; ++idx)
8316 {
rbtnnbebf0692021-08-21 17:26:50 +02008317 assign_dest_T dest = dest_local;
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008318 int opt_flags = 0;
8319 int vimvaridx = -1;
rbtnnbebf0692021-08-21 17:26:50 +02008320 type_T *type = &t_any;
8321 type_T *lhs_type = &t_any;
8322 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008323
rbtnnbebf0692021-08-21 17:26:50 +02008324 p = skip_var_one(arg, FALSE);
8325 varlen = p - arg;
8326 name = vim_strnsave(arg, varlen);
8327 if (name == NULL)
8328 goto failed;
8329 if (*p == ':')
8330 {
8331 p = skipwhite(p + 1);
8332 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8333 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008334
Bram Moolenaarfad27422021-11-30 21:58:19 +00008335 // Script var is not supported.
rbtnnbebf0692021-08-21 17:26:50 +02008336 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008337 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008338 goto failed;
8339 if (dest != dest_local)
8340 {
8341 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008342 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008343 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008344 }
rbtnnbebf0692021-08-21 17:26:50 +02008345 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008346 {
rbtnnbebf0692021-08-21 17:26:50 +02008347 // Assigning to "_": drop the value.
8348 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8349 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008350 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008351 else
rbtnnbebf0692021-08-21 17:26:50 +02008352 {
Mike Williamscc9d7252021-11-23 12:35:57 +00008353 if (!valid_varname(arg, (int)varlen, FALSE))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008354 goto failed;
rbtnnbebf0692021-08-21 17:26:50 +02008355 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8356 {
8357 semsg(_(e_variable_already_declared), arg);
8358 goto failed;
8359 }
8360
8361 if (STRNCMP(name, "s:", 2) == 0)
8362 {
8363 semsg(_(e_cannot_declare_script_variable_in_function), name);
8364 goto failed;
8365 }
8366
8367 // Reserve a variable to store "var".
8368 where.wt_index = var_list ? idx + 1 : 0;
8369 where.wt_variable = TRUE;
8370 if (lhs_type == &t_any)
8371 lhs_type = item_type;
8372 else if (item_type != &t_unknown
8373 && (item_type == &t_any
8374 ? need_type(item_type, lhs_type,
8375 -1, 0, cctx, FALSE, FALSE)
8376 : check_type(lhs_type, item_type, TRUE, where))
8377 == FAIL)
8378 goto failed;
8379 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8380 if (var_lvar == NULL)
8381 // out of memory or used as an argument
8382 goto failed;
8383
8384 if (semicolon && idx == var_count - 1)
8385 var_lvar->lv_type = vartype;
8386 else
8387 var_lvar->lv_type = item_type;
8388 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8389 }
8390
8391 if (*p == ',' || *p == ';')
8392 ++p;
8393 arg = skipwhite(p);
8394 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008395 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008396
rbtnnbebf0692021-08-21 17:26:50 +02008397 if (cctx->ctx_compile_type == CT_DEBUG)
8398 {
8399 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008400
rbtnnbebf0692021-08-21 17:26:50 +02008401 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8402 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8403 cctx->ctx_prev_lnum = prev_lnum;
8404 generate_instr_debug(cctx);
8405 cctx->ctx_prev_lnum = save_prev_lnum;
8406 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008407 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008408
Bram Moolenaar792f7862020-11-23 08:31:18 +01008409 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008410
8411failed:
8412 vim_free(name);
8413 drop_scope(cctx);
8414 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008415}
8416
8417/*
8418 * compile "endfor"
8419 */
8420 static char_u *
8421compile_endfor(char_u *arg, cctx_T *cctx)
8422{
8423 garray_T *instr = &cctx->ctx_instr;
8424 scope_T *scope = cctx->ctx_scope;
8425 forscope_T *forscope;
8426 isn_T *isn;
8427
Bram Moolenaarfa984412021-03-25 22:15:28 +01008428 if (misplaced_cmdmod(cctx))
8429 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008431 if (scope == NULL || scope->se_type != FOR_SCOPE)
8432 {
8433 emsg(_(e_for));
8434 return NULL;
8435 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008436 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008437 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008438 if (cctx->ctx_skip != SKIP_YES)
8439 {
8440 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008441
rbtnnbebf0692021-08-21 17:26:50 +02008442 // At end of ":for" scope jump back to the FOR instruction.
8443 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008444
rbtnnbebf0692021-08-21 17:26:50 +02008445 // Fill in the "end" label in the FOR statement so it can jump here.
8446 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8447 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008448
rbtnnbebf0692021-08-21 17:26:50 +02008449 // Fill in the "end" label any BREAK statements
8450 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008451
rbtnnbebf0692021-08-21 17:26:50 +02008452 // Below the ":for" scope drop the "expr" list from the stack.
8453 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8454 return NULL;
8455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008456
8457 vim_free(scope);
8458
8459 return arg;
8460}
8461
8462/*
8463 * compile "while expr"
8464 *
8465 * Produces instructions:
8466 * top: EVAL expr Push result of "expr"
8467 * JUMP_IF_FALSE end jump if false
8468 * ... body ...
8469 * JUMP top Jump back to repeat
8470 * end:
8471 *
8472 */
8473 static char_u *
8474compile_while(char_u *arg, cctx_T *cctx)
8475{
8476 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008477 scope_T *scope;
8478
8479 scope = new_scope(cctx, WHILE_SCOPE);
8480 if (scope == NULL)
8481 return NULL;
8482
Bram Moolenaara91a7132021-03-25 21:12:15 +01008483 // "endwhile" jumps back here, one before when profiling or using cmdmods
8484 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008485
8486 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008487 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008488 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008489
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008490 if (!ends_excmd2(arg, skipwhite(p)))
8491 {
8492 semsg(_(e_trailing_arg), p);
8493 return NULL;
8494 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008495
rbtnnd895b1d2021-08-20 20:54:25 +02008496 if (cctx->ctx_skip != SKIP_YES)
8497 {
8498 if (bool_on_stack(cctx) == FAIL)
8499 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008500
rbtnnd895b1d2021-08-20 20:54:25 +02008501 // CMDMOD_REV must come before the jump
8502 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008503
rbtnnd895b1d2021-08-20 20:54:25 +02008504 // "while_end" is set when ":endwhile" is found
8505 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008506 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008507 return FAIL;
8508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008509
8510 return p;
8511}
8512
8513/*
8514 * compile "endwhile"
8515 */
8516 static char_u *
8517compile_endwhile(char_u *arg, cctx_T *cctx)
8518{
8519 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008520 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008521
Bram Moolenaarfa984412021-03-25 22:15:28 +01008522 if (misplaced_cmdmod(cctx))
8523 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008524 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8525 {
8526 emsg(_(e_while));
8527 return NULL;
8528 }
8529 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008530 if (cctx->ctx_skip != SKIP_YES)
8531 {
8532 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008533
Bram Moolenaarf002a412021-01-24 13:34:18 +01008534#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008535 // count the endwhile before jumping
8536 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008537#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008538
rbtnnd895b1d2021-08-20 20:54:25 +02008539 // At end of ":for" scope jump back to the FOR instruction.
8540 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008541
rbtnnd895b1d2021-08-20 20:54:25 +02008542 // Fill in the "end" label in the WHILE statement so it can jump here.
8543 // And in any jumps for ":break"
8544 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008545 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008546 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008547
8548 vim_free(scope);
8549
8550 return arg;
8551}
8552
8553/*
8554 * compile "continue"
8555 */
8556 static char_u *
8557compile_continue(char_u *arg, cctx_T *cctx)
8558{
8559 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008560 int try_scopes = 0;
8561 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008562
8563 for (;;)
8564 {
8565 if (scope == NULL)
8566 {
8567 emsg(_(e_continue));
8568 return NULL;
8569 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008570 if (scope->se_type == FOR_SCOPE)
8571 {
8572 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008573 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008574 }
8575 if (scope->se_type == WHILE_SCOPE)
8576 {
8577 loop_label = scope->se_u.se_while.ws_top_label;
8578 break;
8579 }
8580 if (scope->se_type == TRY_SCOPE)
8581 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008582 scope = scope->se_outer;
8583 }
8584
Bram Moolenaarc150c092021-02-13 15:02:46 +01008585 if (try_scopes > 0)
8586 // Inside one or more try/catch blocks we first need to jump to the
8587 // "finally" or "endtry" to cleanup.
8588 generate_TRYCONT(cctx, try_scopes, loop_label);
8589 else
8590 // Jump back to the FOR or WHILE instruction.
8591 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008593 return arg;
8594}
8595
8596/*
8597 * compile "break"
8598 */
8599 static char_u *
8600compile_break(char_u *arg, cctx_T *cctx)
8601{
8602 scope_T *scope = cctx->ctx_scope;
8603 endlabel_T **el;
8604
8605 for (;;)
8606 {
8607 if (scope == NULL)
8608 {
8609 emsg(_(e_break));
8610 return NULL;
8611 }
8612 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8613 break;
8614 scope = scope->se_outer;
8615 }
8616
8617 // Jump to the end of the FOR or WHILE loop.
8618 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008619 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008620 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008621 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008622 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8623 return FAIL;
8624
8625 return arg;
8626}
8627
8628/*
8629 * compile "{" start of block
8630 */
8631 static char_u *
8632compile_block(char_u *arg, cctx_T *cctx)
8633{
8634 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8635 return NULL;
8636 return skipwhite(arg + 1);
8637}
8638
8639/*
8640 * compile end of block: drop one scope
8641 */
8642 static void
8643compile_endblock(cctx_T *cctx)
8644{
8645 scope_T *scope = cctx->ctx_scope;
8646
8647 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008648 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008649 vim_free(scope);
8650}
8651
8652/*
8653 * compile "try"
8654 * Creates a new scope for the try-endtry, pointing to the first catch and
8655 * finally.
8656 * Creates another scope for the "try" block itself.
8657 * TRY instruction sets up exception handling at runtime.
8658 *
8659 * "try"
8660 * TRY -> catch1, -> finally push trystack entry
8661 * ... try block
8662 * "throw {exception}"
8663 * EVAL {exception}
8664 * THROW create exception
8665 * ... try block
8666 * " catch {expr}"
8667 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008668 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008669 * EVAL {expr}
8670 * MATCH
8671 * JUMP nomatch -> catch2
8672 * CATCH remove exception
8673 * ... catch block
8674 * " catch"
8675 * JUMP -> finally
8676 * catch2: CATCH remove exception
8677 * ... catch block
8678 * " finally"
8679 * finally:
8680 * ... finally block
8681 * " endtry"
8682 * ENDTRY pop trystack entry, may rethrow
8683 */
8684 static char_u *
8685compile_try(char_u *arg, cctx_T *cctx)
8686{
8687 garray_T *instr = &cctx->ctx_instr;
8688 scope_T *try_scope;
8689 scope_T *scope;
8690
Bram Moolenaarfa984412021-03-25 22:15:28 +01008691 if (misplaced_cmdmod(cctx))
8692 return NULL;
8693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008694 // scope that holds the jumps that go to catch/finally/endtry
8695 try_scope = new_scope(cctx, TRY_SCOPE);
8696 if (try_scope == NULL)
8697 return NULL;
8698
Bram Moolenaar69f70502021-01-01 16:10:46 +01008699 if (cctx->ctx_skip != SKIP_YES)
8700 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008701 isn_T *isn;
8702
8703 // "try_catch" is set when the first ":catch" is found or when no catch
8704 // is found and ":finally" is found.
8705 // "try_finally" is set when ":finally" is found
8706 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008707 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008708 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8709 return NULL;
8710 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8711 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008712 return NULL;
8713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008714
8715 // scope for the try block itself
8716 scope = new_scope(cctx, BLOCK_SCOPE);
8717 if (scope == NULL)
8718 return NULL;
8719
8720 return arg;
8721}
8722
8723/*
8724 * compile "catch {expr}"
8725 */
8726 static char_u *
8727compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8728{
8729 scope_T *scope = cctx->ctx_scope;
8730 garray_T *instr = &cctx->ctx_instr;
8731 char_u *p;
8732 isn_T *isn;
8733
Bram Moolenaarfa984412021-03-25 22:15:28 +01008734 if (misplaced_cmdmod(cctx))
8735 return NULL;
8736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008737 // end block scope from :try or :catch
8738 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8739 compile_endblock(cctx);
8740 scope = cctx->ctx_scope;
8741
8742 // Error if not in a :try scope
8743 if (scope == NULL || scope->se_type != TRY_SCOPE)
8744 {
8745 emsg(_(e_catch));
8746 return NULL;
8747 }
8748
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008749 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008750 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008751 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008752 return NULL;
8753 }
8754
Bram Moolenaar69f70502021-01-01 16:10:46 +01008755 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008756 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008757#ifdef FEAT_PROFILE
8758 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008759 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008760 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008761 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008762 .isn_type == ISN_PROF_START)
8763 --instr->ga_len;
8764#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008765 // Jump from end of previous block to :finally or :endtry
8766 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8767 JUMP_ALWAYS, cctx) == FAIL)
8768 return NULL;
8769
8770 // End :try or :catch scope: set value in ISN_TRY instruction
8771 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008772 if (isn->isn_arg.try.try_ref->try_catch == 0)
8773 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008774 if (scope->se_u.se_try.ts_catch_label != 0)
8775 {
8776 // Previous catch without match jumps here
8777 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8778 isn->isn_arg.jump.jump_where = instr->ga_len;
8779 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008780#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008781 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008782 {
8783 // a "throw" that jumps here needs to be counted
8784 generate_instr(cctx, ISN_PROF_END);
8785 // the "catch" is also counted
8786 generate_instr(cctx, ISN_PROF_START);
8787 }
8788#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008789 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008790 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008791 }
8792
8793 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008794 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008795 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008796 scope->se_u.se_try.ts_caught_all = TRUE;
8797 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008798 }
8799 else
8800 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008801 char_u *end;
8802 char_u *pat;
8803 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008804 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008805 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008806
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008807 // Push v:exception, push {expr} and MATCH
8808 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8809
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008810 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008811 if (*end != *p)
8812 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008813 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008814 vim_free(tofree);
8815 return FAIL;
8816 }
8817 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008818 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008819 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008820 len = (int)(end - tofree);
8821 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008822 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008823 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008824 if (pat == NULL)
8825 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008826 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008827 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008829 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8830 return NULL;
8831
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008832 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008833 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8834 return NULL;
8835 }
8836
Bram Moolenaar69f70502021-01-01 16:10:46 +01008837 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008838 return NULL;
8839
8840 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8841 return NULL;
8842 return p;
8843}
8844
8845 static char_u *
8846compile_finally(char_u *arg, cctx_T *cctx)
8847{
8848 scope_T *scope = cctx->ctx_scope;
8849 garray_T *instr = &cctx->ctx_instr;
8850 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008851 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008852
Bram Moolenaarfa984412021-03-25 22:15:28 +01008853 if (misplaced_cmdmod(cctx))
8854 return NULL;
8855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008856 // end block scope from :try or :catch
8857 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8858 compile_endblock(cctx);
8859 scope = cctx->ctx_scope;
8860
8861 // Error if not in a :try scope
8862 if (scope == NULL || scope->se_type != TRY_SCOPE)
8863 {
8864 emsg(_(e_finally));
8865 return NULL;
8866 }
8867
rbtnn84934992021-08-07 13:26:53 +02008868 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008869 {
rbtnn84934992021-08-07 13:26:53 +02008870 // End :catch or :finally scope: set value in ISN_TRY instruction
8871 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8872 if (isn->isn_arg.try.try_ref->try_finally != 0)
8873 {
8874 emsg(_(e_finally_dup));
8875 return NULL;
8876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008877
rbtnn84934992021-08-07 13:26:53 +02008878 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008879#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008880 if (cctx->ctx_compile_type == CT_PROFILE
8881 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008882 .isn_type == ISN_PROF_START)
rbtnn84934992021-08-07 13:26:53 +02008883 {
8884 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008885 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008886
8887 // jump to the profile end above it
8888 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008889 .isn_type == ISN_PROF_END)
rbtnn84934992021-08-07 13:26:53 +02008890 --this_instr;
8891 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008892#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008893
rbtnn84934992021-08-07 13:26:53 +02008894 // Fill in the "end" label in jumps at the end of the blocks.
8895 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarfad27422021-11-30 21:58:19 +00008896 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008897
rbtnn84934992021-08-07 13:26:53 +02008898 // If there is no :catch then an exception jumps to :finally.
8899 if (isn->isn_arg.try.try_ref->try_catch == 0)
8900 isn->isn_arg.try.try_ref->try_catch = this_instr;
8901 isn->isn_arg.try.try_ref->try_finally = this_instr;
8902 if (scope->se_u.se_try.ts_catch_label != 0)
8903 {
8904 // Previous catch without match jumps here
8905 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8906 isn->isn_arg.jump.jump_where = this_instr;
8907 scope->se_u.se_try.ts_catch_label = 0;
8908 }
8909 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8910 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008912
8913 return arg;
8914}
8915
8916 static char_u *
8917compile_endtry(char_u *arg, cctx_T *cctx)
8918{
8919 scope_T *scope = cctx->ctx_scope;
8920 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008921 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008922
Bram Moolenaarfa984412021-03-25 22:15:28 +01008923 if (misplaced_cmdmod(cctx))
8924 return NULL;
8925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008926 // end block scope from :catch or :finally
8927 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8928 compile_endblock(cctx);
8929 scope = cctx->ctx_scope;
8930
8931 // Error if not in a :try scope
8932 if (scope == NULL || scope->se_type != TRY_SCOPE)
8933 {
8934 if (scope == NULL)
8935 emsg(_(e_no_endtry));
8936 else if (scope->se_type == WHILE_SCOPE)
8937 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008938 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008939 emsg(_(e_endfor));
8940 else
8941 emsg(_(e_endif));
8942 return NULL;
8943 }
8944
Bram Moolenaarc150c092021-02-13 15:02:46 +01008945 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008946 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008947 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008948 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8949 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008950 {
8951 emsg(_(e_missing_catch_or_finally));
8952 return NULL;
8953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008954
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008955#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008956 if (cctx->ctx_compile_type == CT_PROFILE
8957 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008958 .isn_type == ISN_PROF_START)
8959 // move the profile start after "endtry" so that it's not counted when
8960 // the exception is rethrown.
8961 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008962#endif
8963
Bram Moolenaar69f70502021-01-01 16:10:46 +01008964 // Fill in the "end" label in jumps at the end of the blocks, if not
8965 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008966 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8967 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008968
Bram Moolenaar69f70502021-01-01 16:10:46 +01008969 if (scope->se_u.se_try.ts_catch_label != 0)
8970 {
8971 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008972 isn_T *isn = ((isn_T *)instr->ga_data)
8973 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008974 isn->isn_arg.jump.jump_where = instr->ga_len;
8975 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008976 }
8977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008978 compile_endblock(cctx);
8979
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008980 if (cctx->ctx_skip != SKIP_YES)
8981 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008982 // End :catch or :finally scope: set instruction index in ISN_TRY
8983 // instruction
8984 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008985 if (cctx->ctx_skip != SKIP_YES
8986 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8987 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008988#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008989 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008990 generate_instr(cctx, ISN_PROF_START);
8991#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008993 return arg;
8994}
8995
8996/*
8997 * compile "throw {expr}"
8998 */
8999 static char_u *
9000compile_throw(char_u *arg, cctx_T *cctx UNUSED)
9001{
9002 char_u *p = skipwhite(arg);
9003
Bram Moolenaara5565e42020-05-09 15:44:01 +02009004 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009005 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01009006 if (cctx->ctx_skip == SKIP_YES)
9007 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009008 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009009 return NULL;
9010 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
9011 return NULL;
9012
9013 return p;
9014}
9015
Bram Moolenaarc3235272021-07-10 19:42:03 +02009016 static char_u *
9017compile_eval(char_u *arg, cctx_T *cctx)
9018{
9019 char_u *p = arg;
9020 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02009021 long lnum = SOURCING_LNUM;
9022
9023 // find_ex_command() will consider a variable name an expression, assuming
9024 // that something follows on the next line. Check that something actually
9025 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02009026 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02009027
Bram Moolenaarc3235272021-07-10 19:42:03 +02009028 if (compile_expr0(&p, cctx) == FAIL)
9029 return NULL;
9030
9031 if (name_only && lnum == SOURCING_LNUM)
9032 {
9033 semsg(_(e_expression_without_effect_str), arg);
9034 return NULL;
9035 }
9036
9037 // drop the result
9038 generate_instr_drop(cctx, ISN_DROP, 1);
9039
9040 return skipwhite(p);
9041}
9042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009043/*
9044 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009045 * compile "echomsg expr"
9046 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02009047 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01009048 * compile "execute expr"
9049 */
9050 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009051compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009052{
9053 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01009054 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009055 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009056 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009057 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009058 garray_T *stack = &cctx->ctx_type_stack;
9059 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009060
9061 for (;;)
9062 {
Bram Moolenaare4984292020-12-13 14:19:25 +01009063 if (ends_excmd2(prev, p))
9064 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009065 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009066 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009067 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009068
9069 if (cctx->ctx_skip != SKIP_YES)
9070 {
9071 // check for non-void type
9072 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
9073 if (type->tt_type == VAR_VOID)
9074 {
9075 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
9076 return NULL;
9077 }
9078 }
9079
Bram Moolenaarad39c092020-02-26 18:23:43 +01009080 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02009081 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009082 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009083 }
9084
Bram Moolenaare4984292020-12-13 14:19:25 +01009085 if (count > 0)
9086 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009087 long save_lnum = cctx->ctx_lnum;
9088
9089 // Use the line number where the command started.
9090 cctx->ctx_lnum = start_ctx_lnum;
9091
Bram Moolenaare4984292020-12-13 14:19:25 +01009092 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
9093 generate_ECHO(cctx, cmdidx == CMD_echo, count);
9094 else if (cmdidx == CMD_execute)
9095 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
9096 else if (cmdidx == CMD_echomsg)
9097 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02009098 else if (cmdidx == CMD_echoconsole)
9099 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01009100 else
9101 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009102
9103 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01009104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009105 return p;
9106}
9107
9108/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01009109 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01009110 * instruction to compute it and return OK.
9111 * Otherwise return FAIL, the caller must deal with any range.
9112 */
9113 static int
9114compile_variable_range(exarg_T *eap, cctx_T *cctx)
9115{
9116 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
9117 char_u *p = skipdigits(eap->cmd);
9118
9119 if (p == range_end)
9120 return FAIL;
9121 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
9122}
9123
9124/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009125 * :put r
9126 * :put ={expr}
9127 */
9128 static char_u *
9129compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
9130{
9131 char_u *line = arg;
9132 linenr_T lnum;
9133 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009134 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009135
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009136 eap->regname = *line;
9137
9138 if (eap->regname == '=')
9139 {
9140 char_u *p = line + 1;
9141
9142 if (compile_expr0(&p, cctx) == FAIL)
9143 return NULL;
9144 line = p;
9145 }
9146 else if (eap->regname != NUL)
9147 ++line;
9148
Bram Moolenaar08597872020-12-10 19:43:40 +01009149 if (compile_variable_range(eap, cctx) == OK)
9150 {
9151 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
9152 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009153 else
Bram Moolenaar08597872020-12-10 19:43:40 +01009154 {
9155 // Either no range or a number.
9156 // "errormsg" will not be set because the range is ADDR_LINES.
9157 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01009158 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01009159 return NULL;
9160 if (eap->addr_count == 0)
9161 lnum = -1;
9162 else
9163 lnum = eap->line2;
9164 if (above)
9165 --lnum;
9166 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009167
9168 generate_PUT(cctx, eap->regname, lnum);
9169 return line;
9170}
9171
9172/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009173 * A command that is not compiled, execute with legacy code.
9174 */
9175 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009176compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009177{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009178 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009179 char_u *p;
9180 int has_expr = FALSE;
9181 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009182 char_u *tofree = NULL;
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009183 char_u *cmd_arg = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009184
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009185 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009186 goto theend;
9187
Bram Moolenaare729ce22021-06-06 21:38:09 +02009188 // If there was a prececing command modifier, drop it and include it in the
9189 // EXEC command.
9190 if (cctx->ctx_has_cmdmod)
9191 {
9192 garray_T *instr = &cctx->ctx_instr;
9193 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9194
9195 if (isn->isn_type == ISN_CMDMOD)
9196 {
9197 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9198 ->cmod_filter_regmatch.regprog);
9199 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9200 --instr->ga_len;
9201 cctx->ctx_has_cmdmod = FALSE;
9202 }
9203 }
9204
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009205 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009206 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009207 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009208 int usefilter = FALSE;
9209
9210 has_expr = argt & (EX_XFILE | EX_EXPAND);
9211
9212 // If the command can be followed by a bar, find the bar and truncate
9213 // it, so that the following command can be compiled.
9214 // The '|' is overwritten with a NUL, it is put back below.
9215 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9216 && *eap->arg == '!')
9217 // :w !filter or :r !filter or :r! filter
9218 usefilter = TRUE;
9219 if ((argt & EX_TRLBAR) && !usefilter)
9220 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009221 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009222 separate_nextcmd(eap);
9223 if (eap->nextcmd != NULL)
9224 nextcmd = eap->nextcmd;
9225 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009226 else if (eap->cmdidx == CMD_wincmd)
9227 {
9228 p = eap->arg;
9229 if (*p != NUL)
9230 ++p;
9231 if (*p == 'g' || *p == Ctrl_G)
9232 ++p;
9233 p = skipwhite(p);
9234 if (*p == '|')
9235 {
9236 *p = NUL;
9237 nextcmd = p + 1;
9238 }
9239 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009240 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9241 {
9242 // If there is a trailing '{' read lines until the '}'
9243 p = eap->arg + STRLEN(eap->arg) - 1;
9244 while (p > eap->arg && VIM_ISWHITE(*p))
9245 --p;
9246 if (*p == '{')
9247 {
9248 exarg_T ea;
9249 int flags; // unused
9250 int start_lnum = SOURCING_LNUM;
9251
9252 CLEAR_FIELD(ea);
9253 ea.arg = eap->arg;
9254 fill_exarg_from_cctx(&ea, cctx);
9255 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9256 if (tofree != NULL)
9257 {
9258 *p = NUL;
9259 line = concat_str(line, tofree);
9260 if (line == NULL)
9261 goto theend;
9262 vim_free(tofree);
9263 tofree = line;
9264 SOURCING_LNUM = start_lnum;
9265 }
9266 }
9267 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009268 }
9269
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009270 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9271 {
9272 // expand filename in "syntax include [@group] filename"
9273 has_expr = TRUE;
9274 eap->arg = skipwhite(eap->arg + 7);
9275 if (*eap->arg == '@')
9276 eap->arg = skiptowhite(eap->arg);
9277 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009278
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009279 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9280 && STRLEN(eap->arg) > 4)
9281 {
9282 int delim = *eap->arg;
9283
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009284 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009285 if (*p == delim)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009286 cmd_arg = p + 1;
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009287 }
9288
Bram Moolenaarecac5912021-01-05 19:23:28 +01009289 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009290 cmd_arg = eap->arg;
9291
9292 if (cmd_arg != NULL)
Bram Moolenaarecac5912021-01-05 19:23:28 +01009293 {
Bram Moolenaarfad27422021-11-30 21:58:19 +00009294 exarg_T nea;
9295
9296 CLEAR_FIELD(nea);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009297 nea.cmd = cmd_arg;
Bram Moolenaarfad27422021-11-30 21:58:19 +00009298 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009299 if (nea.cmdidx < CMD_SIZE)
Bram Moolenaarfad27422021-11-30 21:58:19 +00009300 {
9301 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
9302 if (has_expr)
9303 eap->arg = skiptowhite(eap->arg);
9304 }
Bram Moolenaarecac5912021-01-05 19:23:28 +01009305 }
9306
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009307 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009308 {
9309 int count = 0;
9310 char_u *start = skipwhite(line);
9311
9312 // :cmd xxx`=expr1`yyy`=expr2`zzz
9313 // PUSHS ":cmd xxx"
9314 // eval expr1
9315 // PUSHS "yyy"
9316 // eval expr2
9317 // PUSHS "zzz"
9318 // EXECCONCAT 5
9319 for (;;)
9320 {
9321 if (p > start)
9322 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009323 char_u *val = vim_strnsave(start, p - start);
9324
9325 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009326 ++count;
9327 }
9328 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009329 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009330 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009331 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009332 ++count;
9333 p = skipwhite(p);
9334 if (*p != '`')
9335 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009336 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009337 return NULL;
9338 }
9339 start = p + 1;
9340
9341 p = (char_u *)strstr((char *)start, "`=");
9342 if (p == NULL)
9343 {
9344 if (*skipwhite(start) != NUL)
9345 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009346 char_u *val = vim_strsave(start);
9347
9348 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009349 ++count;
9350 }
9351 break;
9352 }
9353 }
9354 generate_EXECCONCAT(cctx, count);
9355 }
9356 else
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009357 generate_EXEC_copy(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009358
9359theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009360 if (*nextcmd != NUL)
9361 {
9362 // the parser expects a pointer to the bar, put it back
9363 --nextcmd;
9364 *nextcmd = '|';
9365 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009366 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009367
9368 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009369}
9370
Bram Moolenaar20677332021-06-06 17:02:53 +02009371/*
9372 * A script command with heredoc, e.g.
9373 * ruby << EOF
9374 * command
9375 * EOF
9376 * Has been turned into one long line with NL characters by
9377 * get_function_body():
9378 * ruby << EOF<NL> command<NL>EOF
9379 */
9380 static char_u *
9381compile_script(char_u *line, cctx_T *cctx)
9382{
9383 if (cctx->ctx_skip != SKIP_YES)
9384 {
9385 isn_T *isn;
9386
9387 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9388 return NULL;
9389 isn->isn_arg.string = vim_strsave(line);
9390 }
9391 return (char_u *)"";
9392}
9393
Bram Moolenaar8238f082021-04-20 21:10:48 +02009394
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009395/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009396 * :s/pat/repl/
9397 */
9398 static char_u *
9399compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9400{
9401 char_u *cmd = eap->arg;
9402 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9403
9404 if (expr != NULL)
9405 {
9406 int delimiter = *cmd++;
9407
9408 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009409 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009410 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9411 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009412 garray_T save_ga = cctx->ctx_instr;
9413 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009414 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009415 int trailing_error;
9416 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009417 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009418 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009419
9420 cmd += 3;
9421 end = skip_substitute(cmd, delimiter);
9422
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009423 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009424 // labels are correct.
9425 cctx->ctx_instr.ga_len = 0;
9426 cctx->ctx_instr.ga_maxlen = 0;
9427 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009428 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009429 if (end[-1] == NUL)
9430 end[-1] = delimiter;
9431 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009432 trailing_error = *cmd != delimiter && *cmd != NUL;
9433
Bram Moolenaar169502f2021-04-21 12:19:35 +02009434 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009435 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009436 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009437 if (trailing_error)
9438 semsg(_(e_trailing_arg), cmd);
9439 clear_instr_ga(&cctx->ctx_instr);
9440 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009441 return NULL;
9442 }
9443
Bram Moolenaar8238f082021-04-20 21:10:48 +02009444 // Move the generated instructions into the ISN_SUBSTITUTE
9445 // instructions, then restore the list of instructions before
9446 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009447 instr_count = cctx->ctx_instr.ga_len;
9448 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009449 instr[instr_count].isn_type = ISN_FINISH;
9450
9451 cctx->ctx_instr = save_ga;
9452 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9453 {
9454 int idx;
9455
9456 for (idx = 0; idx < instr_count; ++idx)
9457 delete_instr(instr + idx);
9458 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009459 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009460 }
9461 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9462 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009463
9464 // skip over flags
9465 if (*end == '&')
9466 ++end;
9467 while (ASCII_ISALPHA(*end) || *end == '#')
9468 ++end;
9469 return end;
9470 }
9471 }
9472
9473 return compile_exec(arg, eap, cctx);
9474}
9475
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009476 static char_u *
9477compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9478{
9479 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009480 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009481
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009482 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009483 {
9484 if (STRNCMP(arg, "END", 3) == 0)
9485 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009486 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009487 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009488 // First load the current variable value.
9489 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9490 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009491 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009492 }
9493
9494 // Gets the redirected text and put it on the stack, then store it
9495 // in the variable.
9496 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9497
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009498 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009499 generate_instr_drop(cctx, ISN_CONCAT, 1);
9500
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009501 if (lhs->lhs_has_index)
9502 {
9503 // Use the info in "lhs" to store the value at the index in the
9504 // list or dict.
9505 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9506 &t_string, cctx) == FAIL)
9507 return NULL;
9508 }
9509 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009510 return NULL;
9511
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009512 VIM_CLEAR(lhs->lhs_name);
9513 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009514 return arg + 3;
9515 }
9516 emsg(_(e_cannot_nest_redir));
9517 return NULL;
9518 }
9519
9520 if (arg[0] == '=' && arg[1] == '>')
9521 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009522 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009523
9524 // redirect to a variable is compiled
9525 arg += 2;
9526 if (*arg == '>')
9527 {
9528 ++arg;
9529 append = TRUE;
9530 }
9531 arg = skipwhite(arg);
9532
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009533 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009534 FALSE, FALSE, 1, cctx) == FAIL)
9535 return NULL;
9536 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009537 lhs->lhs_append = append;
9538 if (lhs->lhs_has_index)
9539 {
9540 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9541 if (lhs->lhs_whole == NULL)
9542 return NULL;
9543 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009544
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009545 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009546 }
9547
9548 // other redirects are handled like at script level
9549 return compile_exec(line, eap, cctx);
9550}
9551
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009552#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009553 static char_u *
9554compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9555{
9556 isn_T *isn;
9557 char_u *p;
9558
9559 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9560 if (isn == NULL)
9561 return NULL;
9562 isn->isn_arg.number = eap->cmdidx;
9563
9564 p = eap->arg;
9565 if (compile_expr0(&p, cctx) == FAIL)
9566 return NULL;
9567
9568 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9569 if (isn == NULL)
9570 return NULL;
9571 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9572 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9573 return NULL;
9574 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9575 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9576 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9577
9578 return p;
9579}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009580#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009581
Bram Moolenaar4c137212021-04-19 16:48:48 +02009582/*
Bram Moolenaar7b829262021-10-13 15:04:34 +01009583 * Check if the separator for a :global or :substitute command is OK.
9584 */
9585 int
9586check_global_and_subst(char_u *cmd, char_u *arg)
9587{
Bram Moolenaar7f320922021-10-13 15:28:28 +01009588 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
Bram Moolenaar7b829262021-10-13 15:04:34 +01009589 {
9590 semsg(_(e_separator_not_supported_str), arg);
9591 return FAIL;
9592 }
9593 if (VIM_ISWHITE(cmd[1]))
9594 {
9595 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
9596 return FAIL;
9597 }
9598 return OK;
9599}
9600
9601
9602/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009603 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009604 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009605 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009606 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009607add_def_function(ufunc_T *ufunc)
9608{
9609 dfunc_T *dfunc;
9610
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009611 if (def_functions.ga_len == 0)
9612 {
9613 // The first position is not used, so that a zero uf_dfunc_idx means it
9614 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009615 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009616 return FAIL;
9617 ++def_functions.ga_len;
9618 }
9619
Bram Moolenaar09689a02020-05-09 22:50:08 +02009620 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009621 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009622 return FAIL;
9623 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9624 CLEAR_POINTER(dfunc);
9625 dfunc->df_idx = def_functions.ga_len;
9626 ufunc->uf_dfunc_idx = dfunc->df_idx;
9627 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009628 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009629 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009630 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009631 ++def_functions.ga_len;
9632 return OK;
9633}
9634
9635/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009636 * After ex_function() has collected all the function lines: parse and compile
9637 * the lines into instructions.
9638 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009639 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9640 * the return statement (used for lambda). When uf_ret_type is already set
9641 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009642 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009643 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009644 * This can be used recursively through compile_lambda(), which may reallocate
9645 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009646 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009647 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009648 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009649compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009650 ufunc_T *ufunc,
9651 int check_return_type,
9652 compiletype_T compile_type,
9653 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009654{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009655 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009656 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009657 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009658 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009659 cctx_T cctx;
9660 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009661 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009662 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009663 int ret = FAIL;
9664 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009665 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009666 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009667 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009668 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009669#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009670 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009671#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009672 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009673
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009674 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009675 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009676 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009677 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009678 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9679 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009680 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009681
9682 switch (compile_type)
9683 {
9684 case CT_PROFILE:
9685#ifdef FEAT_PROFILE
9686 instr_dest = dfunc->df_instr_prof; break;
9687#endif
9688 case CT_NONE: instr_dest = dfunc->df_instr; break;
9689 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9690 }
9691 if (instr_dest != NULL)
9692 // Was compiled in this mode before: Free old instructions.
9693 delete_def_function_contents(dfunc, FALSE);
9694 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009695 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009696 else
9697 {
9698 if (add_def_function(ufunc) == FAIL)
9699 return FAIL;
9700 new_def_function = TRUE;
9701 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009702
Bram Moolenaar985116a2020-07-12 17:31:09 +02009703 ufunc->uf_def_status = UF_COMPILING;
9704
Bram Moolenaara80faa82020-04-12 19:37:17 +02009705 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009706
Bram Moolenaare99d4222021-06-13 14:01:26 +02009707 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009708 cctx.ctx_ufunc = ufunc;
9709 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009710 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009711 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9712 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9713 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9714 cctx.ctx_type_list = &ufunc->uf_type_list;
9715 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9716 instr = &cctx.ctx_instr;
9717
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009718 // Set the context to the function, it may be compiled when called from
9719 // another script. Set the script version to the most modern one.
9720 // The line number will be set in next_line_from_context().
9721 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009722 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9723
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009724 // Don't use the flag from ":legacy" here.
9725 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9726
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009727 // Make sure error messages are OK.
9728 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9729 if (do_estack_push)
9730 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009731 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009732
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009733 if (ufunc->uf_def_args.ga_len > 0)
9734 {
9735 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009736 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009737 int i;
9738 char_u *arg;
9739 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009740 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009741
9742 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009743 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009744 for (i = 0; i < count; ++i)
9745 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009746 garray_T *stack = &cctx.ctx_type_stack;
9747 type_T *val_type;
9748 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009749 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009750 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009751 int jump_instr_idx = instr->ga_len;
9752 isn_T *isn;
9753
9754 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9755 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9756 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009757
9758 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009759 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009760
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009761 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009762 r = compile_expr0(&arg, &cctx);
9763
Bram Moolenaar12bce952021-03-11 20:04:04 +01009764 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009765 goto erret;
9766
9767 // If no type specified use the type of the default value.
9768 // Otherwise check that the default value type matches the
9769 // specified type.
9770 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009771 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009772 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009773 {
9774 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009775 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009776 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009777 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009778 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009779 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009780
9781 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009782 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009783
9784 // set instruction index in JUMP_IF_ARG_SET to here
9785 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9786 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009787 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009788
9789 if (did_set_arg_type)
9790 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009791 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009792 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009793
9794 /*
9795 * Loop over all the lines of the function and generate instructions.
9796 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009797 for (;;)
9798 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009799 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009800 int starts_with_colon = FALSE;
9801 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009802 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009803
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009804 // Bail out on the first error to avoid a flood of errors and report
9805 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009806 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009807 goto erret;
9808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009809 if (line != NULL && *line == '|')
9810 // the line continues after a '|'
9811 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009812 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009813 && !(*line == '#' && (line == cctx.ctx_line_start
9814 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009815 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009816 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009817 goto erret;
9818 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009819 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9820 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009821 else
9822 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009823 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009824 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009825 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009826 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009827#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009828 if (cctx.ctx_skip != SKIP_YES)
9829 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009830#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009831 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009832 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009833 // Make a copy, splitting off nextcmd and removing trailing spaces
9834 // may change it.
9835 if (line != NULL)
9836 {
9837 line = vim_strsave(line);
9838 vim_free(line_to_free);
9839 line_to_free = line;
9840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009841 }
9842
Bram Moolenaara80faa82020-04-12 19:37:17 +02009843 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009844 ea.cmdlinep = &line;
9845 ea.cmd = skipwhite(line);
9846
Bram Moolenaarb2049902021-01-24 12:53:53 +01009847 if (*ea.cmd == '#')
9848 {
9849 // "#" starts a comment
9850 line = (char_u *)"";
9851 continue;
9852 }
9853
Bram Moolenaarf002a412021-01-24 13:34:18 +01009854#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009855 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9856 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009857 {
9858 may_generate_prof_end(&cctx, prof_lnum);
9859
9860 prof_lnum = cctx.ctx_lnum;
9861 generate_instr(&cctx, ISN_PROF_START);
9862 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009863#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009864 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9865 && cctx.ctx_skip != SKIP_YES)
9866 {
9867 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009868 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009869 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009870 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009871
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009872 // Some things can be recognized by the first character.
9873 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009874 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009875 case '}':
9876 {
9877 // "}" ends a block scope
9878 scopetype_T stype = cctx.ctx_scope == NULL
9879 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009880
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009881 if (stype == BLOCK_SCOPE)
9882 {
9883 compile_endblock(&cctx);
9884 line = ea.cmd;
9885 }
9886 else
9887 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009888 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009889 goto erret;
9890 }
9891 if (line != NULL)
9892 line = skipwhite(ea.cmd + 1);
9893 continue;
9894 }
9895
9896 case '{':
9897 // "{" starts a block scope
9898 // "{'a': 1}->func() is something else
9899 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9900 {
9901 line = compile_block(ea.cmd, &cctx);
9902 continue;
9903 }
9904 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009905 }
9906
9907 /*
9908 * COMMAND MODIFIERS
9909 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009910 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009911 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9912 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009913 {
9914 if (errormsg != NULL)
9915 goto erret;
9916 // empty line or comment
9917 line = (char_u *)"";
9918 continue;
9919 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009920 generate_cmdmods(&cctx, &local_cmdmod);
9921 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009922
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009923 // Check if there was a colon after the last command modifier or before
9924 // the current position.
9925 for (p = ea.cmd; p >= line; --p)
9926 {
9927 if (*p == ':')
9928 starts_with_colon = TRUE;
9929 if (p < ea.cmd && !VIM_ISWHITE(*p))
9930 break;
9931 }
9932
Bram Moolenaarce024c32021-06-26 13:00:49 +02009933 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009934 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009935 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009936 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009937 if (checkforcmd(&ea.cmd, "call", 3))
9938 {
9939 if (*ea.cmd == '(')
9940 // not for "call()"
9941 ea.cmd = p;
9942 else
9943 ea.cmd = skipwhite(ea.cmd);
9944 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009945
Bram Moolenaarce024c32021-06-26 13:00:49 +02009946 if (!starts_with_colon)
9947 {
9948 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009949
Bram Moolenaarce024c32021-06-26 13:00:49 +02009950 // Check for assignment after command modifiers.
9951 assign = may_compile_assignment(&ea, &line, &cctx);
9952 if (assign == OK)
9953 goto nextline;
9954 if (assign == FAIL)
9955 goto erret;
9956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009957 }
9958
9959 /*
9960 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009961 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009962 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009963 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009964 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009965 cmd = ea.cmd;
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009966 if ((*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009967 && (starts_with_colon || !(*cmd == '\''
9968 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009969 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009970 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009971 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009972 {
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009973 if (!starts_with_colon
9974 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009975 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009976 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009977 goto erret;
9978 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009979 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009980 if (ends_excmd2(line, ea.cmd))
9981 {
9982 // A range without a command: jump to the line.
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009983 generate_EXEC(&cctx, ISN_EXECRANGE,
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009984 vim_strnsave(cmd, ea.cmd - cmd));
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009985 line = ea.cmd;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009986 goto nextline;
9987 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009988 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009989 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009990 p = find_ex_command(&ea, NULL,
9991 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009992 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009993
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009994 if (p == NULL)
9995 {
9996 if (cctx.ctx_skip != SKIP_YES)
9997 emsg(_(e_ambiguous_use_of_user_defined_command));
9998 goto erret;
9999 }
10000
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +020010001 // When using ":legacy cmd" always use compile_exec().
10002 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010003 {
10004 char_u *start = ea.cmd;
10005
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +020010006 switch (ea.cmdidx)
10007 {
10008 case CMD_if:
10009 case CMD_elseif:
10010 case CMD_else:
10011 case CMD_endif:
10012 case CMD_for:
10013 case CMD_endfor:
10014 case CMD_continue:
10015 case CMD_break:
10016 case CMD_while:
10017 case CMD_endwhile:
10018 case CMD_try:
10019 case CMD_catch:
10020 case CMD_finally:
10021 case CMD_endtry:
10022 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
10023 goto erret;
10024 default: break;
10025 }
10026
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010027 // ":legacy return expr" needs to be handled differently.
10028 if (checkforcmd(&start, "return", 4))
10029 ea.cmdidx = CMD_return;
10030 else
10031 ea.cmdidx = CMD_legacy;
10032 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +020010033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010034 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
10035 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +020010036 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010037 {
10038 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +010010039 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010040 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +020010041 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010042 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +010010043 // CMD_var cannot happen, compile_assignment() above would be
10044 // used. Most likely an assignment to a non-existing variable.
10045 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010046 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010047 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010048 }
10049
Bram Moolenaar3988f642020-08-27 22:43:03 +020010050 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010051 && ea.cmdidx != CMD_elseif
10052 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +020010053 && ea.cmdidx != CMD_endif
10054 && ea.cmdidx != CMD_endfor
10055 && ea.cmdidx != CMD_endwhile
10056 && ea.cmdidx != CMD_catch
10057 && ea.cmdidx != CMD_finally
10058 && ea.cmdidx != CMD_endtry)
10059 {
Bram Moolenaar3988f642020-08-27 22:43:03 +020010060 emsg(_(e_unreachable_code_after_return));
10061 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +020010062 }
10063
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010064 p = skipwhite(p);
10065 if (ea.cmdidx != CMD_SIZE
10066 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
10067 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +020010068 if (ea.cmdidx >= 0)
10069 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010070 if ((ea.argt & EX_BANG) && *p == '!')
10071 {
10072 ea.forceit = TRUE;
10073 p = skipwhite(p + 1);
10074 }
10075 }
10076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010077 switch (ea.cmdidx)
10078 {
10079 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +000010080 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +020010081 ea.arg = p;
10082 line = compile_nested_function(&ea, &cctx);
10083 break;
10084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010085 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010086 line = compile_return(p, check_return_type,
10087 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010088 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010089 break;
10090
10091 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +020010092 emsg(_(e_cannot_use_let_in_vim9_script));
10093 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +020010094 case CMD_var:
10095 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010096 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +020010097 case CMD_increment:
10098 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010099 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +020010100 if (line == p)
10101 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010102 break;
10103
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010104 case CMD_unlet:
10105 case CMD_unlockvar:
10106 case CMD_lockvar:
10107 line = compile_unletlock(p, &ea, &cctx);
10108 break;
10109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010110 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +020010111 emsg(_(e_import_can_only_be_used_in_script));
10112 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010113 break;
10114
10115 case CMD_if:
10116 line = compile_if(p, &cctx);
10117 break;
10118 case CMD_elseif:
10119 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010120 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010121 break;
10122 case CMD_else:
10123 line = compile_else(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_endif:
10127 line = compile_endif(p, &cctx);
10128 break;
10129
10130 case CMD_while:
10131 line = compile_while(p, &cctx);
10132 break;
10133 case CMD_endwhile:
10134 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010135 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010136 break;
10137
10138 case CMD_for:
10139 line = compile_for(p, &cctx);
10140 break;
10141 case CMD_endfor:
10142 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010143 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010144 break;
10145 case CMD_continue:
10146 line = compile_continue(p, &cctx);
10147 break;
10148 case CMD_break:
10149 line = compile_break(p, &cctx);
10150 break;
10151
10152 case CMD_try:
10153 line = compile_try(p, &cctx);
10154 break;
10155 case CMD_catch:
10156 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010157 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010158 break;
10159 case CMD_finally:
10160 line = compile_finally(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_endtry:
10164 line = compile_endtry(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_throw:
10168 line = compile_throw(p, &cctx);
10169 break;
10170
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010171 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +020010172 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010173 break;
10174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010175 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010176 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +010010177 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010178 case CMD_echomsg:
10179 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010180 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010181 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +010010182 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010183
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010184 case CMD_put:
10185 ea.cmd = cmd;
10186 line = compile_put(p, &ea, &cctx);
10187 break;
10188
Bram Moolenaar4c137212021-04-19 16:48:48 +020010189 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +010010190 if (check_global_and_subst(ea.cmd, p) == FAIL)
10191 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +020010192 if (cctx.ctx_skip == SKIP_YES)
10193 line = (char_u *)"";
10194 else
10195 {
10196 ea.arg = p;
10197 line = compile_substitute(line, &ea, &cctx);
10198 }
10199 break;
10200
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010201 case CMD_redir:
10202 ea.arg = p;
10203 line = compile_redir(line, &ea, &cctx);
10204 break;
10205
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010206 case CMD_cexpr:
10207 case CMD_lexpr:
10208 case CMD_caddexpr:
10209 case CMD_laddexpr:
10210 case CMD_cgetexpr:
10211 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010212#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010213 ea.arg = p;
10214 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010215#else
10216 ex_ni(&ea);
10217 line = NULL;
10218#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010219 break;
10220
Bram Moolenaarae616492020-07-28 20:07:27 +020010221 case CMD_append:
10222 case CMD_change:
10223 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010224 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010225 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010226 case CMD_xit:
10227 not_in_vim9(&ea);
10228 goto erret;
10229
Bram Moolenaar002262f2020-07-08 17:47:57 +020010230 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010231 if (cctx.ctx_skip != SKIP_YES)
10232 {
10233 semsg(_(e_invalid_command_str), ea.cmd);
10234 goto erret;
10235 }
10236 // We don't check for a next command here.
10237 line = (char_u *)"";
10238 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010239
Bram Moolenaar20677332021-06-06 17:02:53 +020010240 case CMD_lua:
10241 case CMD_mzscheme:
10242 case CMD_perl:
10243 case CMD_py3:
10244 case CMD_python3:
10245 case CMD_python:
10246 case CMD_pythonx:
10247 case CMD_ruby:
10248 case CMD_tcl:
10249 ea.arg = p;
10250 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010251 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010252 else
10253 // heredoc lines have been concatenated with NL
10254 // characters in get_function_body()
10255 line = compile_script(line, &cctx);
10256 break;
10257
Bram Moolenaar7b829262021-10-13 15:04:34 +010010258 case CMD_global:
10259 if (check_global_and_subst(ea.cmd, p) == FAIL)
10260 goto erret;
10261 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +020010262 default:
10263 // Not recognized, execute with do_cmdline_cmd().
10264 ea.arg = p;
10265 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010266 break;
10267 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010268nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010269 if (line == NULL)
10270 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010271 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010272
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010273 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010274 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010276 if (cctx.ctx_type_stack.ga_len < 0)
10277 {
10278 iemsg("Type stack underflow");
10279 goto erret;
10280 }
10281 }
10282
10283 if (cctx.ctx_scope != NULL)
10284 {
10285 if (cctx.ctx_scope->se_type == IF_SCOPE)
10286 emsg(_(e_endif));
10287 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10288 emsg(_(e_endwhile));
10289 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10290 emsg(_(e_endfor));
10291 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010292 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010293 goto erret;
10294 }
10295
Bram Moolenaarefd88552020-06-18 20:50:10 +020010296 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010297 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010298 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10299 ufunc->uf_ret_type = &t_void;
10300 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010301 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010302 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010303 goto erret;
10304 }
10305
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010306 // Return void if there is no return at the end.
10307 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010308 }
10309
Bram Moolenaar599410c2021-04-10 14:03:43 +020010310 // When compiled with ":silent!" and there was an error don't consider the
10311 // function compiled.
10312 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010313 {
10314 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10315 + ufunc->uf_dfunc_idx;
10316 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010317 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010318#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010319 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010320 {
10321 dfunc->df_instr_prof = instr->ga_data;
10322 dfunc->df_instr_prof_count = instr->ga_len;
10323 }
10324 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010325#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010326 if (cctx.ctx_compile_type == CT_DEBUG)
10327 {
10328 dfunc->df_instr_debug = instr->ga_data;
10329 dfunc->df_instr_debug_count = instr->ga_len;
10330 }
10331 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010332 {
10333 dfunc->df_instr = instr->ga_data;
10334 dfunc->df_instr_count = instr->ga_len;
10335 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010336 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010337 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010338 if (cctx.ctx_outer_used)
10339 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010340 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010341 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010342
10343 ret = OK;
10344
10345erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010346 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010347 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010348 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10349 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010350
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010351 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010352 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010353 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010354 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010355
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010356 // If using the last entry in the table and it was added above, we
10357 // might as well remove it.
10358 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010359 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010360 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010361 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010362 ufunc->uf_dfunc_idx = 0;
10363 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010364 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010365
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010366 while (cctx.ctx_scope != NULL)
10367 drop_scope(&cctx);
10368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010369 if (errormsg != NULL)
10370 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010371 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010372 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010373 }
10374
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010375 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10376 {
10377 if (ret == OK)
10378 {
10379 emsg(_(e_missing_redir_end));
10380 ret = FAIL;
10381 }
10382 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010383 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010384 }
10385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010386 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010387 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010388 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010389 if (do_estack_push)
10390 estack_pop();
10391
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010392 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010393 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010394 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010395 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010396 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010397}
10398
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010399 void
10400set_function_type(ufunc_T *ufunc)
10401{
10402 int varargs = ufunc->uf_va_name != NULL;
10403 int argcount = ufunc->uf_args.ga_len;
10404
10405 // Create a type for the function, with the return type and any
10406 // argument types.
10407 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10408 // The type is included in "tt_args".
10409 if (argcount > 0 || varargs)
10410 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010411 if (ufunc->uf_type_list.ga_itemsize == 0)
10412 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010413 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10414 argcount, &ufunc->uf_type_list);
10415 // Add argument types to the function type.
10416 if (func_type_add_arg_types(ufunc->uf_func_type,
10417 argcount + varargs,
10418 &ufunc->uf_type_list) == FAIL)
10419 return;
10420 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10421 ufunc->uf_func_type->tt_min_argcount =
10422 argcount - ufunc->uf_def_args.ga_len;
10423 if (ufunc->uf_arg_types == NULL)
10424 {
10425 int i;
10426
10427 // lambda does not have argument types.
10428 for (i = 0; i < argcount; ++i)
10429 ufunc->uf_func_type->tt_args[i] = &t_any;
10430 }
10431 else
10432 mch_memmove(ufunc->uf_func_type->tt_args,
10433 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10434 if (varargs)
10435 {
10436 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010437 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010438 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10439 }
10440 }
10441 else
10442 // No arguments, can use a predefined type.
10443 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10444 argcount, &ufunc->uf_type_list);
10445}
10446
10447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010448/*
10449 * Delete an instruction, free what it contains.
10450 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010451 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010452delete_instr(isn_T *isn)
10453{
10454 switch (isn->isn_type)
10455 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010456 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010457 case ISN_EXEC:
Bram Moolenaare4eed8c2021-12-01 15:22:56 +000010458 case ISN_EXECRANGE:
Bram Moolenaar20677332021-06-06 17:02:53 +020010459 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010460 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010461 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010462 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010463 case ISN_LOADENV:
10464 case ISN_LOADG:
10465 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010466 case ISN_LOADT:
10467 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010468 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010469 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010470 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010471 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010472 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010473 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010474 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010475 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010476 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010477 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010478 case ISN_STOREW:
10479 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010480 vim_free(isn->isn_arg.string);
10481 break;
10482
Bram Moolenaar4c137212021-04-19 16:48:48 +020010483 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010484 {
10485 int idx;
10486 isn_T *list = isn->isn_arg.subs.subs_instr;
10487
10488 vim_free(isn->isn_arg.subs.subs_cmd);
10489 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10490 delete_instr(list + idx);
10491 vim_free(list);
10492 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010493 break;
10494
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010495 case ISN_INSTR:
10496 {
10497 int idx;
10498 isn_T *list = isn->isn_arg.instr;
10499
10500 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10501 delete_instr(list + idx);
10502 vim_free(list);
10503 }
10504 break;
10505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010506 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010507 case ISN_STORES:
10508 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010509 break;
10510
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010511 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010512 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010513 vim_free(isn->isn_arg.unlet.ul_name);
10514 break;
10515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010516 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +000010517 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010518 vim_free(isn->isn_arg.storeopt.so_name);
10519 break;
10520
10521 case ISN_PUSHBLOB: // push blob isn_arg.blob
10522 blob_unref(isn->isn_arg.blob);
10523 break;
10524
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010525 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010526#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010527 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010528#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010529 break;
10530
10531 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010532#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010533 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010534#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010535 break;
10536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010537 case ISN_UCALL:
10538 vim_free(isn->isn_arg.ufunc.cuf_name);
10539 break;
10540
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010541 case ISN_FUNCREF:
10542 {
Bram Moolenaar38453522021-11-28 22:00:12 +000010543 if (isn->isn_arg.funcref.fr_func_name == NULL)
10544 {
10545 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10546 + isn->isn_arg.funcref.fr_dfunc_idx;
10547 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010548
Bram Moolenaar38453522021-11-28 22:00:12 +000010549 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10550 func_ptr_unref(ufunc);
10551 }
10552 else
10553 {
10554 char_u *name = isn->isn_arg.funcref.fr_func_name;
10555
10556 if (name != NULL)
10557 func_unref(name);
10558 vim_free(isn->isn_arg.funcref.fr_func_name);
10559 }
Bram Moolenaara05e5242020-09-19 18:19:19 +020010560 }
10561 break;
10562
10563 case ISN_DCALL:
10564 {
10565 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10566 + isn->isn_arg.dfunc.cdf_idx;
10567
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010568 if (dfunc->df_ufunc != NULL
10569 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010570 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010571 }
10572 break;
10573
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010574 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010575 {
10576 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10577 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10578
10579 if (ufunc != NULL)
10580 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010581 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010582 func_ptr_unref(ufunc);
10583 }
10584
10585 vim_free(lambda);
10586 vim_free(isn->isn_arg.newfunc.nf_global);
10587 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010588 break;
10589
Bram Moolenaar5e654232020-09-16 15:22:00 +020010590 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010591 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010592 free_type(isn->isn_arg.type.ct_type);
10593 break;
10594
Bram Moolenaar02194d22020-10-24 23:08:38 +020010595 case ISN_CMDMOD:
10596 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10597 ->cmod_filter_regmatch.regprog);
10598 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10599 break;
10600
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010601 case ISN_LOADSCRIPT:
10602 case ISN_STORESCRIPT:
10603 vim_free(isn->isn_arg.script.scriptref);
10604 break;
10605
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010606 case ISN_TRY:
10607 vim_free(isn->isn_arg.try.try_ref);
10608 break;
10609
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010610 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010611 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010612 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10613 break;
10614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010615 case ISN_2BOOL:
10616 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010617 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010618 case ISN_ADDBLOB:
10619 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010620 case ISN_ANYINDEX:
10621 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010622 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010623 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010624 case ISN_BLOBINDEX:
10625 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010626 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010627 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010628 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010629 case ISN_CHECKNR:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010630 case ISN_CLEARDICT:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010631 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010632 case ISN_COMPAREANY:
10633 case ISN_COMPAREBLOB:
10634 case ISN_COMPAREBOOL:
10635 case ISN_COMPAREDICT:
10636 case ISN_COMPAREFLOAT:
10637 case ISN_COMPAREFUNC:
10638 case ISN_COMPARELIST:
10639 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010640 case ISN_COMPARESPECIAL:
10641 case ISN_COMPARESTRING:
10642 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010643 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010644 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010645 case ISN_DROP:
10646 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010647 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010648 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010649 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010650 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010651 case ISN_EXECCONCAT:
10652 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010653 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010654 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010655 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010656 case ISN_GETITEM:
10657 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010658 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010659 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010660 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010661 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010662 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010663 case ISN_LOADBDICT:
10664 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010665 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010666 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010667 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010668 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010669 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010670 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010671 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010672 case ISN_NEGATENR:
10673 case ISN_NEWDICT:
10674 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010675 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010676 case ISN_OPFLOAT:
10677 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010678 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010679 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010680 case ISN_PROF_END:
10681 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010682 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010683 case ISN_PUSHF:
10684 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010685 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010686 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010687 case ISN_REDIREND:
10688 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010689 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010690 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010691 case ISN_SHUFFLE:
10692 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010693 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010694 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010695 case ISN_STORENR:
10696 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010697 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010698 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010699 case ISN_STOREV:
10700 case ISN_STRINDEX:
10701 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010702 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010703 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010704 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010705 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010706 case ISN_UNPACK:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010707 case ISN_USEDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010708 // nothing allocated
10709 break;
10710 }
10711}
10712
10713/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010714 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010715 */
10716 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010717delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010718{
10719 int idx;
10720
10721 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010722 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010723
10724 if (dfunc->df_instr != NULL)
10725 {
10726 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10727 delete_instr(dfunc->df_instr + idx);
10728 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010729 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010730 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010731 if (dfunc->df_instr_debug != NULL)
10732 {
10733 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10734 delete_instr(dfunc->df_instr_debug + idx);
10735 VIM_CLEAR(dfunc->df_instr_debug);
10736 dfunc->df_instr_debug = NULL;
10737 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010738#ifdef FEAT_PROFILE
10739 if (dfunc->df_instr_prof != NULL)
10740 {
10741 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10742 delete_instr(dfunc->df_instr_prof + idx);
10743 VIM_CLEAR(dfunc->df_instr_prof);
10744 dfunc->df_instr_prof = NULL;
10745 }
10746#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010747
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010748 if (mark_deleted)
10749 dfunc->df_deleted = TRUE;
10750 if (dfunc->df_ufunc != NULL)
10751 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010752}
10753
10754/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010755 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010756 * function, unless another user function still uses it.
10757 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010758 */
10759 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010760unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010761{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010762 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010763 {
10764 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10765 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010766
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010767 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010768 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010769 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010770 ufunc->uf_dfunc_idx = 0;
10771 if (dfunc->df_ufunc == ufunc)
10772 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010773 }
10774}
10775
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010776/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010777 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010778 */
10779 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010780link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010781{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010782 if (ufunc->uf_dfunc_idx > 0)
10783 {
10784 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10785 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010786
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010787 ++dfunc->df_refcount;
10788 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010789}
10790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010791#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010792/*
10793 * Free all functions defined with ":def".
10794 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010795 void
10796free_def_functions(void)
10797{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010798 int idx;
10799
10800 for (idx = 0; idx < def_functions.ga_len; ++idx)
10801 {
10802 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10803
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010804 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010805 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010806 }
10807
10808 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010809}
10810#endif
10811
10812
10813#endif // FEAT_EVAL