blob: cde56ac208a47d0f14e62950e08f030627e093f7 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200117// Destination for an assignment or ":unlet" with an index.
118typedef enum {
119 dest_local,
120 dest_option,
Bram Moolenaardcb53be2021-12-09 14:23:43 +0000121 dest_func_option,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200122 dest_env,
123 dest_global,
124 dest_buffer,
125 dest_window,
126 dest_tab,
127 dest_vimvar,
128 dest_script,
129 dest_reg,
130 dest_expr,
131} assign_dest_T;
132
133// Used by compile_lhs() to store information about the LHS of an assignment
134// and one argument of ":unlet" with an index.
135typedef struct {
136 assign_dest_T lhs_dest; // type of destination
137
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200138 char_u *lhs_name; // allocated name excluding the last
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200139 // "[expr]" or ".name".
140 size_t lhs_varlen; // length of the variable without
141 // "[expr]" or ".name"
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200142 char_u *lhs_whole; // allocated name including the last
143 // "[expr]" or ".name" for :redir
144 size_t lhs_varlen_total; // length of the variable including
145 // any "[expr]" or ".name"
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200146 char_u *lhs_dest_end; // end of the destination, including
147 // "[expr]" or ".name".
Bram Moolenaarab36e6a2021-11-30 16:14:49 +0000148 char_u *lhs_end; // end including any type
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200149
150 int lhs_has_index; // has "[expr]" or ".name"
151
152 int lhs_new_local; // create new local variable
153 int lhs_opt_flags; // for when destination is an option
154 int lhs_vimvaridx; // for when destination is a v:var
155
156 lvar_T lhs_local_lvar; // used for existing local destination
157 lvar_T lhs_arg_lvar; // used for argument destination
158 lvar_T *lhs_lvar; // points to destination lvar
159 int lhs_scriptvar_sid;
160 int lhs_scriptvar_idx;
161
162 int lhs_has_type; // type was specified
163 type_T *lhs_type;
164 type_T *lhs_member_type;
165
166 int lhs_append; // used by ISN_REDIREND
167} lhs_T;
168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169/*
170 * Context for compiling lines of Vim script.
171 * Stores info about the local variables and condition stack.
172 */
173struct cctx_S {
174 ufunc_T *ctx_ufunc; // current function
175 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200176 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177 garray_T ctx_instr; // generated instructions
178
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200179 int ctx_prev_lnum; // line number below previous command, for
180 // debugging
181
Bram Moolenaare99d4222021-06-13 14:01:26 +0200182 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100184 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100185
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200186 int ctx_has_closure; // set to one if a closures was created in
187 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100189 garray_T ctx_imports; // imported items
190
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200191 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200193 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100194
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200195 cctx_T *ctx_outer; // outer scope for lambda or nested
196 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200197 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200200 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200201
Bram Moolenaar02194d22020-10-24 23:08:38 +0200202 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200203
204 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
205 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206};
207
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100208static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100209
210/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100211 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100212 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100213 * If "lvar" is NULL only check if the variable can be found.
214 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100216 static int
217lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218{
219 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100220 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100222 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100223 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200224
225 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
227 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100228 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
229 if (STRNCMP(name, lvp->lv_name, len) == 0
230 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200231 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100232 if (lvar != NULL)
233 {
234 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100235 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100236 }
237 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200240
241 // Find local in outer function scope.
242 if (cctx->ctx_outer != NULL)
243 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100244 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200245 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100246 if (lvar != NULL)
247 {
248 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100249 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100250 }
251 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200252 }
253 }
254
Bram Moolenaar709664c2020-12-12 14:33:41 +0100255 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256}
257
258/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200259 * Lookup an argument in the current function and an enclosing function.
260 * Returns the argument index in "idxp"
261 * Returns the argument type in "type"
262 * Sets "gen_load_outer" to TRUE if found in outer scope.
263 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 */
265 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200266arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200267 char_u *name,
268 size_t len,
269 int *idxp,
270 type_T **type,
271 int *gen_load_outer,
272 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273{
274 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200275 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100277 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200278 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200279 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280 {
281 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
282
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200283 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
284 {
285 if (idxp != NULL)
286 {
287 // Arguments are located above the frame pointer. One further
288 // if there is a vararg argument
289 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
290 + STACK_FRAME_SIZE)
291 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
292
293 if (cctx->ctx_ufunc->uf_arg_types != NULL)
294 *type = cctx->ctx_ufunc->uf_arg_types[idx];
295 else
296 *type = &t_any;
297 }
298 return OK;
299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100300 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200302 va_name = cctx->ctx_ufunc->uf_va_name;
303 if (va_name != NULL
304 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
305 {
306 if (idxp != NULL)
307 {
308 // varargs is always the last argument
309 *idxp = -STACK_FRAME_SIZE - 1;
310 *type = cctx->ctx_ufunc->uf_va_type;
311 }
312 return OK;
313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200315 if (cctx->ctx_outer != NULL)
316 {
317 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200318 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200319 == OK)
320 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100321 if (gen_load_outer != NULL)
322 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200323 return OK;
324 }
325 }
326
327 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328}
329
330/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331 * Lookup a script-local variable in the current script, possibly defined in a
332 * block that contains the function "cctx->ctx_ufunc".
333 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100334 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200335 * Return NULL when not found.
336 */
337 static sallvar_T *
338find_script_var(char_u *name, size_t len, cctx_T *cctx)
339{
340 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
341 hashitem_T *hi;
342 int cc;
343 sallvar_T *sav;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200344 sallvar_T *found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200345 ufunc_T *ufunc;
346
347 // Find the list of all script variables with the right name.
348 if (len > 0)
349 {
350 cc = name[len];
351 name[len] = NUL;
352 }
353 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
354 if (len > 0)
355 name[len] = cc;
356 if (HASHITEM_EMPTY(hi))
357 return NULL;
358
359 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200360 if (sav->sav_block_id == 0)
361 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200362 return sav;
363
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200364 if (cctx == NULL)
365 {
366 // Not in a function scope, find variable with block id equal to or
367 // smaller than the current block id.
368 while (sav != NULL)
369 {
370 if (sav->sav_block_id <= si->sn_current_block_id)
371 break;
372 sav = sav->sav_next;
373 }
374 return sav;
375 }
376
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200377 // Go over the variables with this name and find one that was visible
378 // from the function.
379 ufunc = cctx->ctx_ufunc;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200380 found_sav = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200381 while (sav != NULL)
382 {
383 int idx;
384
385 // Go over the blocks that this function was defined in. If the
386 // variable block ID matches it was visible to the function.
387 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
388 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
389 return sav;
390 sav = sav->sav_next;
391 }
392
Bram Moolenaar88421d62021-07-24 14:14:52 +0200393 // Not found, assume variable at script level was visible.
394 return found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200395}
396
397/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100398 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200399 */
400 static int
401script_is_vim9()
402{
403 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
404}
405
406/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200407 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200408 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409 * Returns OK or FAIL.
410 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200411 static int
412script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200414 if (current_sctx.sc_sid <= 0)
415 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200416 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200417 {
418 // Check script variables that were visible where the function was
419 // defined.
420 if (find_script_var(name, len, cctx) != NULL)
421 return OK;
422 }
423 else
424 {
425 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
426 dictitem_T *di;
427 int cc;
428
429 // Check script variables that are currently visible
430 cc = name[len];
431 name[len] = NUL;
432 di = find_var_in_ht(ht, 0, name, TRUE);
433 name[len] = cc;
434 if (di != NULL)
435 return OK;
436 }
437
438 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439}
440
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100441/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100442 * Return TRUE if "name" is a local variable, argument, script variable or
443 * imported.
444 */
445 static int
446variable_exists(char_u *name, size_t len, cctx_T *cctx)
447{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100448 return (cctx != NULL
449 && (lookup_local(name, len, NULL, cctx) == OK
450 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200451 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100452 || find_imported(name, len, cctx) != NULL;
453}
454
455/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100456 * Return TRUE if "name" is a local variable, argument, script variable,
457 * imported or function.
458 */
459 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100460item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100461{
462 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100463 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100464
465 if (variable_exists(name, len, cctx))
466 return TRUE;
467
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100468 // This is similar to what is in lookup_scriptitem():
469 // Find a function, so that a following "->" works.
470 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
471 // a function call.
472 p = skipwhite(name + len);
473
474 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
475 {
476 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200477 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100478 // Skip "g:" before a function name.
479 is_global = (name[0] == 'g' && name[1] == ':');
480 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
481 }
482 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100483}
484
485/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100486 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200487 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200488 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100489 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100490 * Return FAIL and give an error if it defined.
491 */
492 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100493check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100494{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200495 int c = p[len];
496 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200497
Bram Moolenaarda479c72021-04-10 21:01:38 +0200498 // underscore argument is OK
499 if (len == 1 && *p == '_')
500 return OK;
501
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200502 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100503 {
504 if (is_arg)
505 semsg(_(e_argument_already_declared_in_script_str), p);
506 else
507 semsg(_(e_variable_already_declared_in_script_str), p);
508 return FAIL;
509 }
510
Bram Moolenaarad486a02020-08-01 23:22:18 +0200511 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100512 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100513 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200514 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200515 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200516 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100517 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200518 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200519 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
520 && (!func_is_global(ufunc)
521 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200522 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100523 if (is_arg)
524 semsg(_(e_argument_name_shadows_existing_variable_str), p);
525 else
526 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200527 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200528 return FAIL;
529 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100530 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200531 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100532 return OK;
533}
534
Bram Moolenaar65b95452020-07-19 14:03:09 +0200535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536/////////////////////////////////////////////////////////////////////
537// Following generate_ functions expect the caller to call ga_grow().
538
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200539#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
540#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100542/*
543 * Generate an instruction without arguments.
544 * Returns a pointer to the new instruction, NULL if failed.
545 */
546 static isn_T *
547generate_instr(cctx_T *cctx, isntype_T isn_type)
548{
549 garray_T *instr = &cctx->ctx_instr;
550 isn_T *isn;
551
Bram Moolenaar080457c2020-03-03 21:53:32 +0100552 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar35578162021-08-02 19:10:38 +0200553 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554 return NULL;
555 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
556 isn->isn_type = isn_type;
557 isn->isn_lnum = cctx->ctx_lnum + 1;
558 ++instr->ga_len;
559
560 return isn;
561}
562
563/*
564 * Generate an instruction without arguments.
565 * "drop" will be removed from the stack.
566 * Returns a pointer to the new instruction, NULL if failed.
567 */
568 static isn_T *
569generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
570{
571 garray_T *stack = &cctx->ctx_type_stack;
572
Bram Moolenaar080457c2020-03-03 21:53:32 +0100573 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 stack->ga_len -= drop;
575 return generate_instr(cctx, isn_type);
576}
577
578/*
579 * Generate instruction "isn_type" and put "type" on the type stack.
580 */
581 static isn_T *
582generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
583{
584 isn_T *isn;
585 garray_T *stack = &cctx->ctx_type_stack;
586
587 if ((isn = generate_instr(cctx, isn_type)) == NULL)
588 return NULL;
589
Bram Moolenaar35578162021-08-02 19:10:38 +0200590 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200592 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 ++stack->ga_len;
594
595 return isn;
596}
597
598/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200599 * Generate an ISN_DEBUG instruction.
600 */
601 static isn_T *
602generate_instr_debug(cctx_T *cctx)
603{
604 isn_T *isn;
605 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
606 + cctx->ctx_ufunc->uf_dfunc_idx;
607
608 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
609 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200610 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
611 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200612 return isn;
613}
614
615/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200617 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200618 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619 */
620 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200621may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622{
623 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200624 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100626 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100628 RETURN_OK_IF_SKIP(cctx);
629 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200630 switch ((*type)->tt_type)
631 {
632 // nothing to be done
633 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200635 // conversion possible
636 case VAR_SPECIAL:
637 case VAR_BOOL:
638 case VAR_NUMBER:
639 case VAR_FLOAT:
640 break;
641
642 // conversion possible (with runtime check)
643 case VAR_ANY:
644 case VAR_UNKNOWN:
645 isntype = ISN_2STRING_ANY;
646 break;
647
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200648 // conversion possible when tolerant
649 case VAR_LIST:
650 if (tolerant)
651 {
652 isntype = ISN_2STRING_ANY;
653 break;
654 }
655 // FALLTHROUGH
656
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200657 // conversion not possible
658 case VAR_VOID:
659 case VAR_BLOB:
660 case VAR_FUNC:
661 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200662 case VAR_DICT:
663 case VAR_JOB:
664 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200665 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200666 to_string_error((*type)->tt_type);
667 return FAIL;
668 }
669
670 *type = &t_string;
671 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200673 isn->isn_arg.tostring.offset = offset;
674 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
676 return OK;
677}
678
679 static int
680check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
681{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200684 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 {
686 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200687 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200689 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690 return FAIL;
691 }
692 return OK;
693}
694
Bram Moolenaar07802042021-09-09 23:01:14 +0200695/*
696 * Generate instruction for "+". For a list this creates a new list.
697 */
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200698 static int
699generate_add_instr(
700 cctx_T *cctx,
701 vartype_T vartype,
702 type_T *type1,
Bram Moolenaar07802042021-09-09 23:01:14 +0200703 type_T *type2,
704 exprtype_T expr_type)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200705{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100706 garray_T *stack = &cctx->ctx_type_stack;
707 isn_T *isn = generate_instr_drop(cctx,
708 vartype == VAR_NUMBER ? ISN_OPNR
709 : vartype == VAR_LIST ? ISN_ADDLIST
710 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200711#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100712 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200713#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100714 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200715
716 if (vartype != VAR_LIST && vartype != VAR_BLOB
717 && type1->tt_type != VAR_ANY
718 && type2->tt_type != VAR_ANY
719 && check_number_or_float(
720 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
721 return FAIL;
722
723 if (isn != NULL)
Bram Moolenaar07802042021-09-09 23:01:14 +0200724 {
725 if (isn->isn_type == ISN_ADDLIST)
726 isn->isn_arg.op.op_type = expr_type;
727 else
728 isn->isn_arg.op.op_type = EXPR_ADD;
729 }
Bram Moolenaar399ea812020-12-15 21:28:57 +0100730
731 // When concatenating two lists with different member types the member type
732 // becomes "any".
733 if (vartype == VAR_LIST
734 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
735 && type1->tt_member != type2->tt_member)
736 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
737
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200738 return isn == NULL ? FAIL : OK;
739}
740
741/*
742 * Get the type to use for an instruction for an operation on "type1" and
743 * "type2". If they are matching use a type-specific instruction. Otherwise
744 * fall back to runtime type checking.
745 */
746 static vartype_T
747operator_type(type_T *type1, type_T *type2)
748{
749 if (type1->tt_type == type2->tt_type
750 && (type1->tt_type == VAR_NUMBER
751 || type1->tt_type == VAR_LIST
752#ifdef FEAT_FLOAT
753 || type1->tt_type == VAR_FLOAT
754#endif
755 || type1->tt_type == VAR_BLOB))
756 return type1->tt_type;
757 return VAR_ANY;
758}
759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760/*
761 * Generate an instruction with two arguments. The instruction depends on the
762 * type of the arguments.
763 */
764 static int
765generate_two_op(cctx_T *cctx, char_u *op)
766{
767 garray_T *stack = &cctx->ctx_type_stack;
768 type_T *type1;
769 type_T *type2;
770 vartype_T vartype;
771 isn_T *isn;
772
Bram Moolenaar080457c2020-03-03 21:53:32 +0100773 RETURN_OK_IF_SKIP(cctx);
774
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200775 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
777 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200778 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779
780 switch (*op)
781 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200782 case '+':
Bram Moolenaar07802042021-09-09 23:01:14 +0200783 if (generate_add_instr(cctx, vartype, type1, type2,
784 EXPR_COPY) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 break;
787
788 case '-':
789 case '*':
790 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
791 op) == FAIL)
792 return FAIL;
793 if (vartype == VAR_NUMBER)
794 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
795#ifdef FEAT_FLOAT
796 else if (vartype == VAR_FLOAT)
797 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
798#endif
799 else
800 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
801 if (isn != NULL)
802 isn->isn_arg.op.op_type = *op == '*'
803 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
804 break;
805
Bram Moolenaar4c683752020-04-05 21:38:23 +0200806 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200808 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 && type2->tt_type != VAR_NUMBER))
810 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200811 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 return FAIL;
813 }
814 isn = generate_instr_drop(cctx,
815 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
816 if (isn != NULL)
817 isn->isn_arg.op.op_type = EXPR_REM;
818 break;
819 }
820
821 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200822 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 {
824 type_T *type = &t_any;
825
826#ifdef FEAT_FLOAT
827 // float+number and number+float results in float
828 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
829 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
830 type = &t_float;
831#endif
832 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
833 }
834
835 return OK;
836}
837
838/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200839 * Get the instruction to use for comparing "type1" with "type2"
840 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200842 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100843get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844{
845 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846
Bram Moolenaar4c683752020-04-05 21:38:23 +0200847 if (type1 == VAR_UNKNOWN)
848 type1 = VAR_ANY;
849 if (type2 == VAR_UNKNOWN)
850 type2 = VAR_ANY;
851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100852 if (type1 == type2)
853 {
854 switch (type1)
855 {
856 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
857 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
858 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
859 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
860 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
861 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
862 case VAR_LIST: isntype = ISN_COMPARELIST; break;
863 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
864 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 default: isntype = ISN_COMPAREANY; break;
866 }
867 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200868 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100870 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871 isntype = ISN_COMPAREANY;
872
Bram Moolenaar657137c2021-01-09 15:45:23 +0100873 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 && (isntype == ISN_COMPAREBOOL
875 || isntype == ISN_COMPARESPECIAL
876 || isntype == ISN_COMPARENR
877 || isntype == ISN_COMPAREFLOAT))
878 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200879 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100880 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200881 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 }
883 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100884 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
886 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100887 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
888 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889 && (type1 == VAR_BLOB || type2 == VAR_BLOB
890 || type1 == VAR_LIST || type2 == VAR_LIST))))
891 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200892 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200894 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200896 return isntype;
897}
898
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200899 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100900check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200901{
902 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
903 return FAIL;
904 return OK;
905}
906
Bram Moolenaara5565e42020-05-09 15:44:01 +0200907/*
908 * Generate an ISN_COMPARE* instruction with a boolean result.
909 */
910 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100911generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200912{
913 isntype_T isntype;
914 isn_T *isn;
915 garray_T *stack = &cctx->ctx_type_stack;
916 vartype_T type1;
917 vartype_T type2;
918
919 RETURN_OK_IF_SKIP(cctx);
920
921 // Get the known type of the two items on the stack. If they are matching
922 // use a type-specific instruction. Otherwise fall back to runtime type
923 // checking.
924 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
925 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100926 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200927 if (isntype == ISN_DROP)
928 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929
930 if ((isn = generate_instr(cctx, isntype)) == NULL)
931 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100932 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 isn->isn_arg.op.op_ic = ic;
934
935 // takes two arguments, puts one bool back
936 if (stack->ga_len >= 2)
937 {
938 --stack->ga_len;
939 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
940 }
941
942 return OK;
943}
944
945/*
946 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200947 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 */
949 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200950generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951{
952 isn_T *isn;
953 garray_T *stack = &cctx->ctx_type_stack;
954
Bram Moolenaar080457c2020-03-03 21:53:32 +0100955 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
957 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200958 isn->isn_arg.tobool.invert = invert;
959 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960
961 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200962 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963
964 return OK;
965}
966
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200967/*
968 * Generate an ISN_COND2BOOL instruction.
969 */
970 static int
971generate_COND2BOOL(cctx_T *cctx)
972{
973 isn_T *isn;
974 garray_T *stack = &cctx->ctx_type_stack;
975
976 RETURN_OK_IF_SKIP(cctx);
977 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
978 return FAIL;
979
980 // type becomes bool
981 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
982
983 return OK;
984}
985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200987generate_TYPECHECK(
988 cctx_T *cctx,
989 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100990 int offset,
991 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992{
993 isn_T *isn;
994 garray_T *stack = &cctx->ctx_type_stack;
995
Bram Moolenaar080457c2020-03-03 21:53:32 +0100996 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
998 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200999 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01001000 isn->isn_arg.type.ct_off = (int8_T)offset;
1001 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002
Bram Moolenaar5e654232020-09-16 15:22:00 +02001003 // type becomes expected
1004 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005
1006 return OK;
1007}
1008
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001009 static int
1010generate_SETTYPE(
1011 cctx_T *cctx,
1012 type_T *expected)
1013{
1014 isn_T *isn;
1015
1016 RETURN_OK_IF_SKIP(cctx);
1017 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
1018 return FAIL;
1019 isn->isn_arg.type.ct_type = alloc_type(expected);
1020 return OK;
1021}
1022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001024 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
1025 * used. Return FALSE if the types will never match.
1026 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02001027 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001028use_typecheck(type_T *actual, type_T *expected)
1029{
1030 if (actual->tt_type == VAR_ANY
1031 || actual->tt_type == VAR_UNKNOWN
1032 || (actual->tt_type == VAR_FUNC
1033 && (expected->tt_type == VAR_FUNC
1034 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001035 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1036 && ((actual->tt_member == &t_void)
1037 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001038 return TRUE;
1039 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1040 && actual->tt_type == expected->tt_type)
1041 // This takes care of a nested list or dict.
1042 return use_typecheck(actual->tt_member, expected->tt_member);
1043 return FALSE;
1044}
1045
1046/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001047 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001048 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001049 * - "actual" is a type that can be "expected" type: add a runtime check; or
1050 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001051 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1052 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001053 */
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001054 static int
1055need_type_where(
1056 type_T *actual,
1057 type_T *expected,
1058 int offset,
1059 where_T where,
1060 cctx_T *cctx,
1061 int silent,
1062 int actual_is_const)
1063{
1064 if (expected == &t_bool && actual != &t_bool
1065 && (actual->tt_flags & TTFLAG_BOOL_OK))
1066 {
1067 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1068 // boolean is OK but requires a conversion.
1069 generate_2BOOL(cctx, FALSE, offset);
1070 return OK;
1071 }
1072
1073 if (check_type(expected, actual, FALSE, where) == OK)
1074 return OK;
1075
1076 // If the actual type can be the expected type add a runtime check.
1077 // If it's a constant a runtime check makes no sense.
1078 if ((!actual_is_const || actual == &t_any)
1079 && use_typecheck(actual, expected))
1080 {
1081 generate_TYPECHECK(cctx, expected, offset, where.wt_index);
1082 return OK;
1083 }
1084
1085 if (!silent)
1086 type_mismatch_where(expected, actual, where);
1087 return FAIL;
1088}
1089
Bram Moolenaar351ead02021-01-16 16:07:01 +01001090 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001091need_type(
1092 type_T *actual,
1093 type_T *expected,
1094 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001095 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001096 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001097 int silent,
1098 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001099{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001100 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001101
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001102 where.wt_index = arg_idx;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001103 return need_type_where(actual, expected, offset, where,
1104 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001105}
1106
1107/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001108 * Check that the top of the type stack has a type that can be used as a
1109 * condition. Give an error and return FAIL if not.
1110 */
1111 static int
1112bool_on_stack(cctx_T *cctx)
1113{
1114 garray_T *stack = &cctx->ctx_type_stack;
1115 type_T *type;
1116
1117 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1118 if (type == &t_bool)
1119 return OK;
1120
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001121 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001122 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1123 // This requires a runtime type check.
1124 return generate_COND2BOOL(cctx);
1125
Bram Moolenaar351ead02021-01-16 16:07:01 +01001126 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001127}
1128
1129/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 * Generate an ISN_PUSHNR instruction.
1131 */
1132 static int
1133generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1134{
1135 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001136 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.number = number;
1142
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001143 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001144 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001145 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 return OK;
1147}
1148
1149/*
1150 * Generate an ISN_PUSHBOOL instruction.
1151 */
1152 static int
1153generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1154{
1155 isn_T *isn;
1156
Bram Moolenaar080457c2020-03-03 21:53:32 +01001157 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1159 return FAIL;
1160 isn->isn_arg.number = number;
1161
1162 return OK;
1163}
1164
1165/*
1166 * Generate an ISN_PUSHSPEC instruction.
1167 */
1168 static int
1169generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1170{
1171 isn_T *isn;
1172
Bram Moolenaar080457c2020-03-03 21:53:32 +01001173 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1175 return FAIL;
1176 isn->isn_arg.number = number;
1177
1178 return OK;
1179}
1180
1181#ifdef FEAT_FLOAT
1182/*
1183 * Generate an ISN_PUSHF instruction.
1184 */
1185 static int
1186generate_PUSHF(cctx_T *cctx, float_T fnumber)
1187{
1188 isn_T *isn;
1189
Bram Moolenaar080457c2020-03-03 21:53:32 +01001190 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1192 return FAIL;
1193 isn->isn_arg.fnumber = fnumber;
1194
1195 return OK;
1196}
1197#endif
1198
1199/*
1200 * Generate an ISN_PUSHS instruction.
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001201 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 */
1203 static int
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001204generate_PUSHS(cctx_T *cctx, char_u **str)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205{
1206 isn_T *isn;
1207
Bram Moolenaar508b5612021-01-02 18:17:26 +01001208 if (cctx->ctx_skip == SKIP_YES)
1209 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001210 if (str != NULL)
1211 VIM_CLEAR(*str);
Bram Moolenaar508b5612021-01-02 18:17:26 +01001212 return OK;
1213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001215 {
1216 if (str != NULL)
1217 VIM_CLEAR(*str);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001219 }
1220 isn->isn_arg.string = str == NULL ? NULL : *str;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221
1222 return OK;
1223}
1224
1225/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001226 * Generate an ISN_PUSHCHANNEL instruction.
1227 * Consumes "channel".
1228 */
1229 static int
1230generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1231{
1232 isn_T *isn;
1233
Bram Moolenaar080457c2020-03-03 21:53:32 +01001234 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001235 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1236 return FAIL;
1237 isn->isn_arg.channel = channel;
1238
1239 return OK;
1240}
1241
1242/*
1243 * Generate an ISN_PUSHJOB instruction.
1244 * Consumes "job".
1245 */
1246 static int
1247generate_PUSHJOB(cctx_T *cctx, job_T *job)
1248{
1249 isn_T *isn;
1250
Bram Moolenaar080457c2020-03-03 21:53:32 +01001251 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001252 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001253 return FAIL;
1254 isn->isn_arg.job = job;
1255
1256 return OK;
1257}
1258
1259/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 * Generate an ISN_PUSHBLOB instruction.
1261 * Consumes "blob".
1262 */
1263 static int
1264generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1265{
1266 isn_T *isn;
1267
Bram Moolenaar080457c2020-03-03 21:53:32 +01001268 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1270 return FAIL;
1271 isn->isn_arg.blob = blob;
1272
1273 return OK;
1274}
1275
1276/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001277 * Generate an ISN_PUSHFUNC instruction with name "name".
1278 * Consumes "name".
1279 */
1280 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001281generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001282{
1283 isn_T *isn;
1284
Bram Moolenaar080457c2020-03-03 21:53:32 +01001285 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001286 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001287 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001288 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001289
1290 return OK;
1291}
1292
1293/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001294 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001295 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1296 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001297 */
1298 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001299generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001300{
1301 isn_T *isn;
1302 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001303 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1304 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001305 type_T *item_type = &t_any;
1306
1307 RETURN_OK_IF_SKIP(cctx);
1308
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001309 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001310 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001311 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001312 emsg(_(e_listreq));
1313 return FAIL;
1314 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001315 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001316 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1317 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001318 isn->isn_arg.getitem.gi_index = index;
1319 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001320
1321 // add the item type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001322 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001323 return FAIL;
1324 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1325 ++stack->ga_len;
1326 return OK;
1327}
1328
1329/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001330 * Generate an ISN_SLICE instruction with "count".
1331 */
1332 static int
1333generate_SLICE(cctx_T *cctx, int count)
1334{
1335 isn_T *isn;
1336
1337 RETURN_OK_IF_SKIP(cctx);
1338 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1339 return FAIL;
1340 isn->isn_arg.number = count;
1341 return OK;
1342}
1343
1344/*
1345 * Generate an ISN_CHECKLEN instruction with "min_len".
1346 */
1347 static int
1348generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1349{
1350 isn_T *isn;
1351
1352 RETURN_OK_IF_SKIP(cctx);
1353
1354 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1355 return FAIL;
1356 isn->isn_arg.checklen.cl_min_len = min_len;
1357 isn->isn_arg.checklen.cl_more_OK = more_OK;
1358
1359 return OK;
1360}
1361
1362/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 * Generate an ISN_STORE instruction.
1364 */
1365 static int
1366generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1367{
1368 isn_T *isn;
1369
Bram Moolenaar080457c2020-03-03 21:53:32 +01001370 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1372 return FAIL;
1373 if (name != NULL)
1374 isn->isn_arg.string = vim_strsave(name);
1375 else
1376 isn->isn_arg.number = idx;
1377
1378 return OK;
1379}
1380
1381/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001382 * Generate an ISN_STOREOUTER instruction.
1383 */
1384 static int
1385generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1386{
1387 isn_T *isn;
1388
1389 RETURN_OK_IF_SKIP(cctx);
1390 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1391 return FAIL;
1392 isn->isn_arg.outer.outer_idx = idx;
1393 isn->isn_arg.outer.outer_depth = level;
1394
1395 return OK;
1396}
1397
1398/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1400 */
1401 static int
1402generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1403{
1404 isn_T *isn;
1405
Bram Moolenaar080457c2020-03-03 21:53:32 +01001406 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1408 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001409 isn->isn_arg.storenr.stnr_idx = idx;
1410 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411
1412 return OK;
1413}
1414
1415/*
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001416 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 */
1418 static int
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001419generate_STOREOPT(
1420 cctx_T *cctx,
1421 isntype_T isn_type,
1422 char_u *name,
1423 int opt_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424{
1425 isn_T *isn;
1426
Bram Moolenaar080457c2020-03-03 21:53:32 +01001427 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001428 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 return FAIL;
1430 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1431 isn->isn_arg.storeopt.so_flags = opt_flags;
1432
1433 return OK;
1434}
1435
1436/*
1437 * Generate an ISN_LOAD or similar instruction.
1438 */
1439 static int
1440generate_LOAD(
1441 cctx_T *cctx,
1442 isntype_T isn_type,
1443 int idx,
1444 char_u *name,
1445 type_T *type)
1446{
1447 isn_T *isn;
1448
Bram Moolenaar080457c2020-03-03 21:53:32 +01001449 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1451 return FAIL;
1452 if (name != NULL)
1453 isn->isn_arg.string = vim_strsave(name);
1454 else
1455 isn->isn_arg.number = idx;
1456
1457 return OK;
1458}
1459
1460/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001461 * Generate an ISN_LOADOUTER instruction
1462 */
1463 static int
1464generate_LOADOUTER(
1465 cctx_T *cctx,
1466 int idx,
1467 int nesting,
1468 type_T *type)
1469{
1470 isn_T *isn;
1471
1472 RETURN_OK_IF_SKIP(cctx);
1473 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1474 return FAIL;
1475 isn->isn_arg.outer.outer_idx = idx;
1476 isn->isn_arg.outer.outer_depth = nesting;
1477
1478 return OK;
1479}
1480
1481/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001482 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001483 */
1484 static int
1485generate_LOADV(
1486 cctx_T *cctx,
1487 char_u *name,
1488 int error)
1489{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001490 int di_flags;
1491 int vidx = find_vim_var(name, &di_flags);
1492 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001493
Bram Moolenaar080457c2020-03-03 21:53:32 +01001494 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001495 if (vidx < 0)
1496 {
1497 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001498 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001499 return FAIL;
1500 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001501 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001502
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001503 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001504}
1505
1506/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001507 * Generate an ISN_UNLET instruction.
1508 */
1509 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001510generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001511{
1512 isn_T *isn;
1513
1514 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001515 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001516 return FAIL;
1517 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1518 isn->isn_arg.unlet.ul_forceit = forceit;
1519
1520 return OK;
1521}
1522
1523/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001524 * Generate an ISN_LOCKCONST instruction.
1525 */
1526 static int
1527generate_LOCKCONST(cctx_T *cctx)
1528{
1529 isn_T *isn;
1530
1531 RETURN_OK_IF_SKIP(cctx);
1532 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1533 return FAIL;
1534 return OK;
1535}
1536
1537/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 * Generate an ISN_LOADS instruction.
1539 */
1540 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001541generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001543 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001545 int sid,
1546 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547{
1548 isn_T *isn;
1549
Bram Moolenaar080457c2020-03-03 21:53:32 +01001550 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001551 if (isn_type == ISN_LOADS)
1552 isn = generate_instr_type(cctx, isn_type, type);
1553 else
1554 isn = generate_instr_drop(cctx, isn_type, 1);
1555 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001557 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1558 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559
1560 return OK;
1561}
1562
1563/*
1564 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1565 */
1566 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001567generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 cctx_T *cctx,
1569 isntype_T isn_type,
1570 int sid,
1571 int idx,
1572 type_T *type)
1573{
1574 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001575 scriptref_T *sref;
1576 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577
Bram Moolenaar080457c2020-03-03 21:53:32 +01001578 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 if (isn_type == ISN_LOADSCRIPT)
1580 isn = generate_instr_type(cctx, isn_type, type);
1581 else
1582 isn = generate_instr_drop(cctx, isn_type, 1);
1583 if (isn == NULL)
1584 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001585
1586 // This requires three arguments, which doesn't fit in an instruction, thus
1587 // we need to allocate a struct for this.
1588 sref = ALLOC_ONE(scriptref_T);
1589 if (sref == NULL)
1590 return FAIL;
1591 isn->isn_arg.script.scriptref = sref;
1592 sref->sref_sid = sid;
1593 sref->sref_idx = idx;
1594 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001595 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 return OK;
1597}
1598
1599/*
1600 * Generate an ISN_NEWLIST instruction.
1601 */
1602 static int
1603generate_NEWLIST(cctx_T *cctx, int count)
1604{
1605 isn_T *isn;
1606 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 type_T *type;
1608 type_T *member;
1609
Bram Moolenaar080457c2020-03-03 21:53:32 +01001610 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1612 return FAIL;
1613 isn->isn_arg.number = count;
1614
Bram Moolenaar127542b2020-08-09 17:22:04 +02001615 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001616 if (count == 0)
Bram Moolenaar6e48b842021-08-10 22:52:02 +02001617 member = &t_unknown;
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001618 else
1619 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001620 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1621 cctx->ctx_type_list);
1622 type = get_list_type(member, cctx->ctx_type_list);
1623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 // drop the value types
1625 stack->ga_len -= count;
1626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 // add the list type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001628 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 return FAIL;
1630 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1631 ++stack->ga_len;
1632
1633 return OK;
1634}
1635
1636/*
1637 * Generate an ISN_NEWDICT instruction.
1638 */
1639 static int
1640generate_NEWDICT(cctx_T *cctx, int count)
1641{
1642 isn_T *isn;
1643 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 type_T *type;
1645 type_T *member;
1646
Bram Moolenaar080457c2020-03-03 21:53:32 +01001647 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1649 return FAIL;
1650 isn->isn_arg.number = count;
1651
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001652 if (count == 0)
1653 member = &t_void;
1654 else
1655 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001656 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1657 cctx->ctx_type_list);
1658 type = get_dict_type(member, cctx->ctx_type_list);
1659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 // drop the key and value types
1661 stack->ga_len -= 2 * count;
1662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 // add the dict type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001664 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 return FAIL;
1666 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1667 ++stack->ga_len;
1668
1669 return OK;
1670}
1671
1672/*
1673 * Generate an ISN_FUNCREF instruction.
1674 */
1675 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001676generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677{
1678 isn_T *isn;
1679 garray_T *stack = &cctx->ctx_type_stack;
1680
Bram Moolenaar080457c2020-03-03 21:53:32 +01001681 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1683 return FAIL;
Bram Moolenaar38453522021-11-28 22:00:12 +00001684 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1685 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1686 else
1687 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001688 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001690 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001691 // the nested context, including this one.
1692 if (ufunc->uf_flags & FC_CLOSURE)
1693 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1694
Bram Moolenaar35578162021-08-02 19:10:38 +02001695 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001697 ((type_T **)stack->ga_data)[stack->ga_len] =
1698 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 ++stack->ga_len;
1700
1701 return OK;
1702}
1703
1704/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001705 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001706 * "lambda_name" and "func_name" must be in allocated memory and will be
1707 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001708 */
1709 static int
1710generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1711{
1712 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001713
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001714 if (cctx->ctx_skip == SKIP_YES)
1715 {
1716 vim_free(lambda_name);
1717 vim_free(func_name);
1718 return OK;
1719 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001720 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001721 {
1722 vim_free(lambda_name);
1723 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001724 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001725 }
1726 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001727 isn->isn_arg.newfunc.nf_global = func_name;
1728
1729 return OK;
1730}
1731
1732/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001733 * Generate an ISN_DEF instruction: list functions
1734 */
1735 static int
1736generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1737{
1738 isn_T *isn;
1739
1740 RETURN_OK_IF_SKIP(cctx);
1741 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1742 return FAIL;
1743 if (len > 0)
1744 {
1745 isn->isn_arg.string = vim_strnsave(name, len);
1746 if (isn->isn_arg.string == NULL)
1747 return FAIL;
1748 }
1749 return OK;
1750}
1751
1752/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 * Generate an ISN_JUMP instruction.
1754 */
1755 static int
1756generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1757{
1758 isn_T *isn;
1759 garray_T *stack = &cctx->ctx_type_stack;
1760
Bram Moolenaar080457c2020-03-03 21:53:32 +01001761 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1763 return FAIL;
1764 isn->isn_arg.jump.jump_when = when;
1765 isn->isn_arg.jump.jump_where = where;
1766
1767 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1768 --stack->ga_len;
1769
1770 return OK;
1771}
1772
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001773/*
1774 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1775 */
1776 static int
1777generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1778{
1779 isn_T *isn;
1780
1781 RETURN_OK_IF_SKIP(cctx);
1782 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1783 return FAIL;
1784 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1785 // jump_where is set later
1786 return OK;
1787}
1788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 static int
1790generate_FOR(cctx_T *cctx, int loop_idx)
1791{
1792 isn_T *isn;
1793 garray_T *stack = &cctx->ctx_type_stack;
1794
Bram Moolenaar080457c2020-03-03 21:53:32 +01001795 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1797 return FAIL;
1798 isn->isn_arg.forloop.for_idx = loop_idx;
1799
Bram Moolenaar35578162021-08-02 19:10:38 +02001800 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 return FAIL;
1802 // type doesn't matter, will be stored next
1803 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1804 ++stack->ga_len;
1805
1806 return OK;
1807}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001808/*
1809 * Generate an ISN_TRYCONT instruction.
1810 */
1811 static int
1812generate_TRYCONT(cctx_T *cctx, int levels, int where)
1813{
1814 isn_T *isn;
1815
1816 RETURN_OK_IF_SKIP(cctx);
1817 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1818 return FAIL;
1819 isn->isn_arg.trycont.tct_levels = levels;
1820 isn->isn_arg.trycont.tct_where = where;
1821
1822 return OK;
1823}
1824
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825
1826/*
1827 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001828 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 * Return FAIL if the number of arguments is wrong.
1830 */
1831 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001832generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833{
1834 isn_T *isn;
1835 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001836 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001837 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001838 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001839 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840
Bram Moolenaar080457c2020-03-03 21:53:32 +01001841 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001842 argoff = check_internal_func(func_idx, argcount);
1843 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 return FAIL;
1845
Bram Moolenaar389df252020-07-09 21:20:47 +02001846 if (method_call && argoff > 1)
1847 {
1848 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1849 return FAIL;
1850 isn->isn_arg.shuffle.shfl_item = argcount;
1851 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1852 }
1853
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001854 if (argcount > 0)
1855 {
1856 // Check the types of the arguments.
1857 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001858 if (method_call && argoff > 1)
1859 {
1860 int i;
1861
1862 for (i = 0; i < argcount; ++i)
1863 shuffled_argtypes[i] = (i < argoff - 1)
1864 ? argtypes[i + 1]
1865 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1866 argtypes = shuffled_argtypes;
1867 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001868 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1869 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001870 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001871 if (internal_func_is_map(func_idx))
1872 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001873 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1876 return FAIL;
1877 isn->isn_arg.bfunc.cbf_idx = func_idx;
1878 isn->isn_arg.bfunc.cbf_argcount = argcount;
1879
Bram Moolenaar94738d82020-10-21 14:25:07 +02001880 // Drop the argument types and push the return type.
1881 stack->ga_len -= argcount;
Bram Moolenaar35578162021-08-02 19:10:38 +02001882 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 return FAIL;
1884 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001885 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001886 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001888 if (maptype != NULL && maptype->tt_member != NULL
1889 && maptype->tt_member != &t_any)
1890 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001891 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 return OK;
1894}
1895
1896/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001897 * Generate an ISN_LISTAPPEND instruction. Works like add().
1898 * Argument count is already checked.
1899 */
1900 static int
1901generate_LISTAPPEND(cctx_T *cctx)
1902{
1903 garray_T *stack = &cctx->ctx_type_stack;
1904 type_T *list_type;
1905 type_T *item_type;
1906 type_T *expected;
1907
1908 // Caller already checked that list_type is a list.
1909 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1910 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1911 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001912 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001913 return FAIL;
1914
1915 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1916 return FAIL;
1917
1918 --stack->ga_len; // drop the argument
1919 return OK;
1920}
1921
1922/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001923 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1924 * Argument count is already checked.
1925 */
1926 static int
1927generate_BLOBAPPEND(cctx_T *cctx)
1928{
1929 garray_T *stack = &cctx->ctx_type_stack;
1930 type_T *item_type;
1931
1932 // Caller already checked that blob_type is a blob.
1933 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001934 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001935 return FAIL;
1936
1937 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1938 return FAIL;
1939
1940 --stack->ga_len; // drop the argument
1941 return OK;
1942}
1943
1944/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001945 * Return TRUE if "ufunc" should be compiled, taking into account whether
1946 * "profile" indicates profiling is to be done.
1947 */
1948 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001949func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001950{
1951 switch (ufunc->uf_def_status)
1952 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001953 case UF_TO_BE_COMPILED:
1954 return TRUE;
1955
Bram Moolenaarb2049902021-01-24 12:53:53 +01001956 case UF_COMPILED:
1957 {
1958 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1959 + ufunc->uf_dfunc_idx;
1960
Bram Moolenaare99d4222021-06-13 14:01:26 +02001961 switch (compile_type)
1962 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001963 case CT_PROFILE:
1964#ifdef FEAT_PROFILE
1965 return dfunc->df_instr_prof == NULL;
1966#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001967 case CT_NONE:
1968 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001969 case CT_DEBUG:
1970 return dfunc->df_instr_debug == NULL;
1971 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001972 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001973
1974 case UF_NOT_COMPILED:
1975 case UF_COMPILE_ERROR:
1976 case UF_COMPILING:
1977 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001978 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001979 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001980}
1981
1982/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 * Generate an ISN_DCALL or ISN_UCALL instruction.
1984 * Return FAIL if the number of arguments is wrong.
1985 */
1986 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001987generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988{
1989 isn_T *isn;
1990 garray_T *stack = &cctx->ctx_type_stack;
1991 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001992 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993
Bram Moolenaar080457c2020-03-03 21:53:32 +01001994 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 if (argcount > regular_args && !has_varargs(ufunc))
1996 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001997 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 return FAIL;
1999 }
2000 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
2001 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02002002 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 return FAIL;
2004 }
2005
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002006 if (ufunc->uf_def_status != UF_NOT_COMPILED
2007 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002008 {
2009 int i;
2010
2011 for (i = 0; i < argcount; ++i)
2012 {
2013 type_T *expected;
2014 type_T *actual;
2015
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002016 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
2017 if (actual == &t_special
2018 && i >= regular_args - ufunc->uf_def_args.ga_len)
2019 {
2020 // assume v:none used for default argument value
2021 continue;
2022 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002023 if (i < regular_args)
2024 {
2025 if (ufunc->uf_arg_types == NULL)
2026 continue;
2027 expected = ufunc->uf_arg_types[i];
2028 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02002029 else if (ufunc->uf_va_type == NULL
2030 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02002031 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02002032 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002033 else
2034 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01002035 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002036 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002037 {
2038 arg_type_mismatch(expected, actual, i + 1);
2039 return FAIL;
2040 }
2041 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002042 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002043 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002044 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002045 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002046 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002047 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2048 {
2049 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2050 ufunc->uf_name);
2051 return FAIL;
2052 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002055 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002056 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002058 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 {
2060 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2061 isn->isn_arg.dfunc.cdf_argcount = argcount;
2062 }
2063 else
2064 {
2065 // A user function may be deleted and redefined later, can't use the
2066 // ufunc pointer, need to look it up again at runtime.
2067 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2068 isn->isn_arg.ufunc.cuf_argcount = argcount;
2069 }
2070
2071 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002072 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 return FAIL;
2074 // add return value
2075 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2076 ++stack->ga_len;
2077
2078 return OK;
2079}
2080
2081/*
2082 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2083 */
2084 static int
2085generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2086{
2087 isn_T *isn;
2088 garray_T *stack = &cctx->ctx_type_stack;
2089
Bram Moolenaar080457c2020-03-03 21:53:32 +01002090 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2092 return FAIL;
2093 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2094 isn->isn_arg.ufunc.cuf_argcount = argcount;
2095
2096 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002097 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002098 return FAIL;
2099 // add return value
2100 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2101 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102
2103 return OK;
2104}
2105
2106/*
2107 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002108 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 */
2110 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002111generate_PCALL(
2112 cctx_T *cctx,
2113 int argcount,
2114 char_u *name,
2115 type_T *type,
2116 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117{
2118 isn_T *isn;
2119 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002120 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121
Bram Moolenaar080457c2020-03-03 21:53:32 +01002122 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002123
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002124 if (type->tt_type == VAR_ANY)
2125 ret_type = &t_any;
2126 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002127 {
2128 if (type->tt_argcount != -1)
2129 {
2130 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2131
2132 if (argcount < type->tt_min_argcount - varargs)
2133 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002134 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002135 return FAIL;
2136 }
2137 if (!varargs && argcount > type->tt_argcount)
2138 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002139 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002140 return FAIL;
2141 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002142 if (type->tt_args != NULL)
2143 {
2144 int i;
2145
2146 for (i = 0; i < argcount; ++i)
2147 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002148 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002149 type_T *actual = ((type_T **)stack->ga_data)[
2150 stack->ga_len + offset];
2151 type_T *expected;
2152
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002153 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002154 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002155 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002156 else if (i >= type->tt_min_argcount
2157 && actual == &t_special)
2158 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002159 else
2160 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002161 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002162 cctx, TRUE, FALSE) == FAIL)
2163 {
2164 arg_type_mismatch(expected, actual, i + 1);
2165 return FAIL;
2166 }
2167 }
2168 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002169 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002170 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002171 if (ret_type == &t_unknown)
2172 // return type not known yet, use a runtime check
2173 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002174 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002175 else
2176 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002177 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002178 return FAIL;
2179 }
2180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2182 return FAIL;
2183 isn->isn_arg.pfunc.cpf_top = at_top;
2184 isn->isn_arg.pfunc.cpf_argcount = argcount;
2185
2186 stack->ga_len -= argcount; // drop the arguments
2187
2188 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002189 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002191 // If partial is above the arguments it must be cleared and replaced with
2192 // the return value.
2193 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2194 return FAIL;
2195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 return OK;
2197}
2198
2199/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002200 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 */
2202 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002203generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204{
2205 isn_T *isn;
2206 garray_T *stack = &cctx->ctx_type_stack;
2207 type_T *type;
2208
Bram Moolenaar080457c2020-03-03 21:53:32 +01002209 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002210 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002212 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002214 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002216 if (type->tt_type != VAR_DICT && type != &t_any)
2217 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002218 char *tofree;
2219
2220 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2221 name, type_name(type, &tofree));
2222 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002223 return FAIL;
2224 }
2225 // change dict type to dict member type
2226 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002227 {
2228 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2229 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231
2232 return OK;
2233}
2234
2235/*
2236 * Generate an ISN_ECHO instruction.
2237 */
2238 static int
2239generate_ECHO(cctx_T *cctx, int with_white, int count)
2240{
2241 isn_T *isn;
2242
Bram Moolenaar080457c2020-03-03 21:53:32 +01002243 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2245 return FAIL;
2246 isn->isn_arg.echo.echo_with_white = with_white;
2247 isn->isn_arg.echo.echo_count = count;
2248
2249 return OK;
2250}
2251
Bram Moolenaarad39c092020-02-26 18:23:43 +01002252/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002253 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002254 */
2255 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002256generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002257{
2258 isn_T *isn;
2259
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002260 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002261 return FAIL;
2262 isn->isn_arg.number = count;
2263
2264 return OK;
2265}
2266
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002267/*
2268 * Generate an ISN_PUT instruction.
2269 */
2270 static int
2271generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2272{
2273 isn_T *isn;
2274
2275 RETURN_OK_IF_SKIP(cctx);
2276 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2277 return FAIL;
2278 isn->isn_arg.put.put_regname = regname;
2279 isn->isn_arg.put.put_lnum = lnum;
2280 return OK;
2281}
2282
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002283/*
2284 * Generate an EXEC instruction that takes a string argument.
2285 * A copy is made of "line".
2286 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 static int
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002288generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289{
2290 isn_T *isn;
2291
Bram Moolenaar080457c2020-03-03 21:53:32 +01002292 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002293 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 return FAIL;
2295 isn->isn_arg.string = vim_strsave(line);
2296 return OK;
2297}
2298
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002299/*
2300 * Generate an EXEC instruction that takes a string argument.
2301 * "str" must be allocated, it is consumed.
2302 */
2303 static int
2304generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2305{
2306 isn_T *isn;
2307
2308 if (cctx->ctx_skip == SKIP_YES)
2309 {
2310 vim_free(str);
2311 return OK;
2312 }
2313 if ((isn = generate_instr(cctx, isntype)) == NULL)
2314 {
2315 vim_free(str);
2316 return FAIL;
2317 }
2318 isn->isn_arg.string = str;
2319 return OK;
2320}
2321
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002322 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002323generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2324{
2325 isn_T *isn;
2326 garray_T *stack = &cctx->ctx_type_stack;
2327
2328 RETURN_OK_IF_SKIP(cctx);
2329 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2330 return FAIL;
2331 isn->isn_arg.string = vim_strsave(line);
2332
Bram Moolenaar35578162021-08-02 19:10:38 +02002333 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002334 return FAIL;
2335 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2336 ++stack->ga_len;
2337
2338 return OK;
2339}
2340
2341 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002342generate_EXECCONCAT(cctx_T *cctx, int count)
2343{
2344 isn_T *isn;
2345
2346 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2347 return FAIL;
2348 isn->isn_arg.number = count;
2349 return OK;
2350}
2351
Bram Moolenaar08597872020-12-10 19:43:40 +01002352/*
2353 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2354 */
2355 static int
2356generate_RANGE(cctx_T *cctx, char_u *range)
2357{
2358 isn_T *isn;
2359 garray_T *stack = &cctx->ctx_type_stack;
2360
2361 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2362 return FAIL;
2363 isn->isn_arg.string = range;
2364
Bram Moolenaar35578162021-08-02 19:10:38 +02002365 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002366 return FAIL;
2367 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2368 ++stack->ga_len;
2369 return OK;
2370}
2371
Bram Moolenaar792f7862020-11-23 08:31:18 +01002372 static int
2373generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2374{
2375 isn_T *isn;
2376
2377 RETURN_OK_IF_SKIP(cctx);
2378 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2379 return FAIL;
2380 isn->isn_arg.unpack.unp_count = var_count;
2381 isn->isn_arg.unpack.unp_semicolon = semicolon;
2382 return OK;
2383}
2384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002386 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002387 */
2388 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002389generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002390{
2391 isn_T *isn;
2392
Bram Moolenaar917c46a2021-08-10 19:53:01 +02002393 if (has_cmdmod(cmod, FALSE))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002394 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002395 cctx->ctx_has_cmdmod = TRUE;
2396
2397 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002398 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002399 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2400 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2401 return FAIL;
2402 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002403 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002404 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002405 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002406
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002407 return OK;
2408}
2409
2410 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002411generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002412{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002413 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2414 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002415 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002416 return OK;
2417}
2418
Bram Moolenaarfa984412021-03-25 22:15:28 +01002419 static int
2420misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002421{
2422 garray_T *instr = &cctx->ctx_instr;
2423
Bram Moolenaara91a7132021-03-25 21:12:15 +01002424 if (cctx->ctx_has_cmdmod
2425 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2426 == ISN_CMDMOD)
2427 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002428 emsg(_(e_misplaced_command_modifier));
2429 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002430 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002431 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002432}
2433
2434/*
2435 * Get the index of the current instruction.
Bram Moolenaar093165c2021-08-22 13:35:31 +02002436 * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
Bram Moolenaara91a7132021-03-25 21:12:15 +01002437 */
2438 static int
2439current_instr_idx(cctx_T *cctx)
2440{
2441 garray_T *instr = &cctx->ctx_instr;
2442 int idx = instr->ga_len;
2443
Bram Moolenaare99d4222021-06-13 14:01:26 +02002444 while (idx > 0)
2445 {
2446 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002447 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002448 {
2449 --idx;
2450 continue;
2451 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002452#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002453 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2454 {
2455 --idx;
2456 continue;
2457 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002458#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002459 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2460 {
2461 --idx;
2462 continue;
2463 }
2464 break;
2465 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002466 return idx;
2467}
2468
Bram Moolenaarf002a412021-01-24 13:34:18 +01002469#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002470 static void
2471may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2472{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002473 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002474 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002475}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002476#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002477
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002478/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002480 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002482 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002483reserve_local(
2484 cctx_T *cctx,
2485 char_u *name,
2486 size_t len,
2487 int isConst,
2488 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002491 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002493 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002495 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002496 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 }
2498
Bram Moolenaar35578162021-08-02 19:10:38 +02002499 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002500 return NULL;
2501 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002502 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002504 // Every local variable uses the next entry on the stack. We could re-use
2505 // the last ones when leaving a scope, but then variables used in a closure
2506 // might get overwritten. To keep things simple do not re-use stack
2507 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002508 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2509 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002510
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002511 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 lvar->lv_const = isConst;
2513 lvar->lv_type = type;
2514
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002515 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002516 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002517 return NULL;
2518 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2519 vim_strsave(lvar->lv_name);
2520 ++dfunc->df_var_names.ga_len;
2521
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002522 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523}
2524
2525/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002526 * Remove local variables above "new_top".
2527 */
2528 static void
2529unwind_locals(cctx_T *cctx, int new_top)
2530{
2531 if (cctx->ctx_locals.ga_len > new_top)
2532 {
2533 int idx;
2534 lvar_T *lvar;
2535
2536 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2537 {
2538 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2539 vim_free(lvar->lv_name);
2540 }
2541 }
2542 cctx->ctx_locals.ga_len = new_top;
2543}
2544
2545/*
2546 * Free all local variables.
2547 */
2548 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002549free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002550{
2551 unwind_locals(cctx, 0);
2552 ga_clear(&cctx->ctx_locals);
2553}
2554
2555/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002556 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2557 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2558 * error if the variable was defined with :const.
2559 */
2560 static int
2561check_item_writable(svar_T *sv, int check_writable, char_u *name)
2562{
2563 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2564 || (check_writable == ASSIGN_FINAL
2565 && sv->sv_const == ASSIGN_CONST))
2566 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002567 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002568 return FAIL;
2569 }
2570 return OK;
2571}
2572
2573/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002575 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 * Returns the index in "sn_var_vals" if found.
2577 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002578 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 */
2580 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002581get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582{
2583 hashtab_T *ht;
2584 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002585 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002586 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 int idx;
2588
Bram Moolenaare3d46852020-08-29 13:39:17 +02002589 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002591 if (sid == current_sctx.sc_sid)
2592 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002593 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002594
2595 if (sav == NULL)
2596 return -2;
2597 idx = sav->sav_var_vals_idx;
2598 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002599 if (check_item_writable(sv, check_writable, name) == FAIL)
2600 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002601 return idx;
2602 }
2603
2604 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 ht = &SCRIPT_VARS(sid);
2606 di = find_var_in_ht(ht, 0, name, TRUE);
2607 if (di == NULL)
2608 return -2;
2609
2610 // Now find the svar_T index in sn_var_vals.
2611 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2612 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002613 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 if (sv->sv_tv == &di->di_tv)
2615 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002616 if (check_item_writable(sv, check_writable, name) == FAIL)
2617 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 return idx;
2619 }
2620 }
2621 return -1;
2622}
2623
2624/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002625 * Find "name" in imported items of the current script or in "cctx" if not
2626 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 */
2628 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002629find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 int idx;
2632
Bram Moolenaare3d46852020-08-29 13:39:17 +02002633 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002634 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 if (cctx != NULL)
2636 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2637 {
2638 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2639 + idx;
2640
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002641 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2642 : STRLEN(import->imp_name) == len
2643 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 return import;
2645 }
2646
Bram Moolenaarefa94442020-08-08 22:16:00 +02002647 return find_imported_in_script(name, len, current_sctx.sc_sid);
2648}
2649
2650 imported_T *
2651find_imported_in_script(char_u *name, size_t len, int sid)
2652{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002653 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002654 int idx;
2655
Bram Moolenaare3d46852020-08-29 13:39:17 +02002656 if (!SCRIPT_ID_VALID(sid))
2657 return NULL;
2658 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2660 {
2661 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2662
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002663 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2664 : STRLEN(import->imp_name) == len
2665 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 return import;
2667 }
2668 return NULL;
2669}
2670
2671/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002672 * Free all imported variables.
2673 */
2674 static void
2675free_imported(cctx_T *cctx)
2676{
2677 int idx;
2678
2679 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2680 {
2681 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2682
2683 vim_free(import->imp_name);
2684 }
2685 ga_clear(&cctx->ctx_imports);
2686}
2687
2688/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002689 * Return a pointer to the next line that isn't empty or only contains a
2690 * comment. Skips over white space.
2691 * Returns NULL if there is none.
2692 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002693 char_u *
2694peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002695{
2696 int lnum = cctx->ctx_lnum;
2697
2698 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2699 {
2700 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002701 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002702
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002703 // ignore NULLs inserted for continuation lines
2704 if (line != NULL)
2705 {
2706 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002707 if (vim9_bad_comment(p))
2708 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002709 if (*p != NUL && !vim9_comment_start(p))
2710 return p;
2711 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002712 }
2713 return NULL;
2714}
2715
2716/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002717 * Called when checking for a following operator at "arg". When the rest of
2718 * the line is empty or only a comment, peek the next line. If there is a next
2719 * line return a pointer to it and set "nextp".
2720 * Otherwise skip over white space.
2721 */
2722 static char_u *
2723may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2724{
2725 char_u *p = skipwhite(arg);
2726
2727 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002728 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002729 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002730 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002731 if (*nextp != NULL)
2732 return *nextp;
2733 }
2734 return p;
2735}
2736
2737/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002738 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002739 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002740 * Returns NULL when at the end.
2741 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002742 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002743next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002744{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002745 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002746
2747 do
2748 {
2749 ++cctx->ctx_lnum;
2750 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002751 {
2752 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002753 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002754 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002755 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002756 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002757 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002758 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002759 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002760 return line;
2761}
2762
2763/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002764 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002765 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002766 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002767 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2768 */
2769 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002770may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002771{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002772 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002773 if (vim9_bad_comment(*arg))
2774 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002775 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002776 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002777 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002778
2779 if (next == NULL)
2780 return FAIL;
2781 *arg = skipwhite(next);
2782 }
2783 return OK;
2784}
2785
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002786/*
2787 * Idem, and give an error when failed.
2788 */
2789 static int
2790may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2791{
2792 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2793 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002794 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002795 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002796 return FAIL;
2797 }
2798 return OK;
2799}
2800
2801
Bram Moolenaara5565e42020-05-09 15:44:01 +02002802// Structure passed between the compile_expr* functions to keep track of
2803// constants that have been parsed but for which no code was produced yet. If
2804// possible expressions on these constants are applied at compile time. If
2805// that is not possible, the code to push the constants needs to be generated
2806// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002807// Using 50 should be more than enough of 5 levels of ().
2808#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002809typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002810 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002811 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002812 int pp_is_const; // all generated code was constants, used for a
2813 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002814} ppconst_T;
2815
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002816static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002817static int compile_expr0(char_u **arg, cctx_T *cctx);
2818static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2819
Bram Moolenaara5565e42020-05-09 15:44:01 +02002820/*
2821 * Generate a PUSH instruction for "tv".
2822 * "tv" will be consumed or cleared.
2823 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2824 */
2825 static int
2826generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2827{
2828 if (tv != NULL)
2829 {
2830 switch (tv->v_type)
2831 {
2832 case VAR_UNKNOWN:
2833 break;
2834 case VAR_BOOL:
2835 generate_PUSHBOOL(cctx, tv->vval.v_number);
2836 break;
2837 case VAR_SPECIAL:
2838 generate_PUSHSPEC(cctx, tv->vval.v_number);
2839 break;
2840 case VAR_NUMBER:
2841 generate_PUSHNR(cctx, tv->vval.v_number);
2842 break;
2843#ifdef FEAT_FLOAT
2844 case VAR_FLOAT:
2845 generate_PUSHF(cctx, tv->vval.v_float);
2846 break;
2847#endif
2848 case VAR_BLOB:
2849 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2850 tv->vval.v_blob = NULL;
2851 break;
2852 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002853 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002854 tv->vval.v_string = NULL;
2855 break;
2856 default:
2857 iemsg("constant type not supported");
2858 clear_tv(tv);
2859 return FAIL;
2860 }
2861 tv->v_type = VAR_UNKNOWN;
2862 }
2863 return OK;
2864}
2865
2866/*
2867 * Generate code for any ppconst entries.
2868 */
2869 static int
2870generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2871{
2872 int i;
2873 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002874 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002875
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002876 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002877 for (i = 0; i < ppconst->pp_used; ++i)
2878 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2879 ret = FAIL;
2880 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002881 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002882 return ret;
2883}
2884
2885/*
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02002886 * Check that the last item of "ppconst" is a bool, if there is an item.
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002887 */
2888 static int
2889check_ppconst_bool(ppconst_T *ppconst)
2890{
2891 if (ppconst->pp_used > 0)
2892 {
2893 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002894 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002895
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002896 return check_typval_type(&t_bool, tv, where);
2897 }
2898 return OK;
2899}
2900
2901/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002902 * Clear ppconst constants. Used when failing.
2903 */
2904 static void
2905clear_ppconst(ppconst_T *ppconst)
2906{
2907 int i;
2908
2909 for (i = 0; i < ppconst->pp_used; ++i)
2910 clear_tv(&ppconst->pp_tv[i]);
2911 ppconst->pp_used = 0;
2912}
2913
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002914/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002915 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002916 * indexable value and the index or the two indexes of a slice.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002917 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002918 */
2919 static int
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002920compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002921{
2922 type_T **typep;
2923 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002924 vartype_T vartype;
2925 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002926
Bram Moolenaar261417b2021-05-06 21:04:55 +02002927 // We can index a list, dict and blob. If we don't know the type
2928 // we can use the index value type. If we still don't know use an "ANY"
2929 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002930 typep = ((type_T **)stack->ga_data) + stack->ga_len
2931 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002932 vartype = (*typep)->tt_type;
2933 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002934 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002935 if (*typep == &t_any && idxtype == &t_string)
2936 vartype = VAR_DICT;
2937 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002938 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002939 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002940 return FAIL;
2941 if (is_slice)
2942 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002943 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2944 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002945 FALSE, FALSE) == FAIL)
2946 return FAIL;
2947 }
2948 }
2949
Bram Moolenaar261417b2021-05-06 21:04:55 +02002950 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002951 {
2952 if (is_slice)
2953 {
2954 emsg(_(e_cannot_slice_dictionary));
2955 return FAIL;
2956 }
2957 if ((*typep)->tt_type == VAR_DICT)
2958 {
2959 *typep = (*typep)->tt_member;
2960 if (*typep == &t_unknown)
2961 // empty dict was used
2962 *typep = &t_any;
2963 }
2964 else
2965 {
2966 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2967 FALSE, FALSE) == FAIL)
2968 return FAIL;
2969 *typep = &t_any;
2970 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002971 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002972 return FAIL;
2973 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2974 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002975 if (keeping_dict != NULL)
2976 *keeping_dict = TRUE;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002977 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002978 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002979 {
2980 *typep = &t_string;
2981 if ((is_slice
2982 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2983 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2984 return FAIL;
2985 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002986 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002987 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002988 if (is_slice)
2989 {
2990 *typep = &t_blob;
2991 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2992 return FAIL;
2993 }
2994 else
2995 {
2996 *typep = &t_number;
2997 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2998 return FAIL;
2999 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02003000 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02003001 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003002 {
3003 if (is_slice)
3004 {
3005 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02003006 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02003007 2) == FAIL)
3008 return FAIL;
3009 }
3010 else
3011 {
3012 if ((*typep)->tt_type == VAR_LIST)
3013 {
3014 *typep = (*typep)->tt_member;
3015 if (*typep == &t_unknown)
3016 // empty list was used
3017 *typep = &t_any;
3018 }
3019 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02003020 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
3021 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003022 return FAIL;
3023 }
3024 }
3025 else
3026 {
3027 emsg(_(e_string_list_dict_or_blob_required));
3028 return FAIL;
3029 }
3030 return OK;
3031}
3032
3033/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003034 * Generate an instruction to load script-local variable "name", without the
3035 * leading "s:".
3036 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 */
3038 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003039compile_load_scriptvar(
3040 cctx_T *cctx,
3041 char_u *name, // variable NUL terminated
3042 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003043 char_u **end, // end of variable
3044 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045{
Bram Moolenaare3d46852020-08-29 13:39:17 +02003046 scriptitem_T *si;
3047 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 imported_T *import;
3049
Bram Moolenaare3d46852020-08-29 13:39:17 +02003050 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3051 return FAIL;
3052 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003053 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003054 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003056 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003057 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3058 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 }
3060 if (idx >= 0)
3061 {
3062 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3063
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003064 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 current_sctx.sc_sid, idx, sv->sv_type);
3066 return OK;
3067 }
3068
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003069 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 if (import != NULL)
3071 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003072 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003073 {
3074 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003075 char_u *exp_name;
3076 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003077 ufunc_T *ufunc;
3078 type_T *type;
3079
3080 // Used "import * as Name", need to lookup the member.
3081 if (*p != '.')
3082 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003083 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003084 return FAIL;
3085 }
3086 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003087 if (VIM_ISWHITE(*p))
3088 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003089 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003090 return FAIL;
3091 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003092
Bram Moolenaar1c991142020-07-04 13:15:31 +02003093 // isolate one name
3094 exp_name = p;
3095 while (eval_isnamec(*p))
3096 ++p;
3097 cc = *p;
3098 *p = NUL;
3099
Bram Moolenaaredba7072021-03-13 21:14:18 +01003100 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3101 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003102 *p = cc;
3103 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003104 *end = p;
3105
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003106 if (idx < 0)
3107 {
3108 if (*p == '(' && ufunc != NULL)
3109 {
3110 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3111 return OK;
3112 }
3113 return FAIL;
3114 }
3115
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003116 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3117 import->imp_sid,
3118 idx,
3119 type);
3120 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003121 else if (import->imp_funcname != NULL)
3122 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003123 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003124 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3125 import->imp_sid,
3126 import->imp_var_vals_idx,
3127 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 return OK;
3129 }
3130
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003131 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003132 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 return FAIL;
3134}
3135
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003136 static int
3137generate_funcref(cctx_T *cctx, char_u *name)
3138{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003139 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003140
3141 if (ufunc == NULL)
3142 return FAIL;
3143
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003144 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003145 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3146 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003147 == FAIL)
3148 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003149 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003150}
3151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152/*
3153 * Compile a variable name into a load instruction.
3154 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003155 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 * When "error" is FALSE do not give an error when not found.
3157 */
3158 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003159compile_load(
3160 char_u **arg,
3161 char_u *end_arg,
3162 cctx_T *cctx,
3163 int is_expr,
3164 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165{
3166 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003167 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003168 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003170 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171
3172 if (*(*arg + 1) == ':')
3173 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003174 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003175 {
3176 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177
Bram Moolenaarfa596382021-04-07 21:58:16 +02003178 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003179 switch (**arg)
3180 {
3181 case 'g': isn_type = ISN_LOADGDICT; break;
3182 case 'w': isn_type = ISN_LOADWDICT; break;
3183 case 't': isn_type = ISN_LOADTDICT; break;
3184 case 'b': isn_type = ISN_LOADBDICT; break;
3185 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003186 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003187 goto theend;
3188 }
3189 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3190 goto theend;
3191 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 else
3194 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003195 isntype_T isn_type = ISN_DROP;
3196
Bram Moolenaarfa596382021-04-07 21:58:16 +02003197 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003198 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3199 if (name == NULL)
3200 return FAIL;
3201
3202 switch (**arg)
3203 {
3204 case 'v': res = generate_LOADV(cctx, name, error);
3205 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003206 case 's': if (is_expr && ASCII_ISUPPER(*name)
3207 && find_func(name, FALSE, cctx) != NULL)
3208 res = generate_funcref(cctx, name);
3209 else
3210 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003211 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003212 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003213 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003214 {
3215 if (is_expr && ASCII_ISUPPER(*name)
3216 && find_func(name, FALSE, cctx) != NULL)
3217 res = generate_funcref(cctx, name);
3218 else
3219 isn_type = ISN_LOADG;
3220 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003221 else
3222 {
3223 isn_type = ISN_LOADAUTO;
3224 vim_free(name);
3225 name = vim_strnsave(*arg, end - *arg);
3226 if (name == NULL)
3227 return FAIL;
3228 }
3229 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003230 case 'w': isn_type = ISN_LOADW; break;
3231 case 't': isn_type = ISN_LOADT; break;
3232 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003233 default: // cannot happen, just in case
3234 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003235 goto theend;
3236 }
3237 if (isn_type != ISN_DROP)
3238 {
3239 // Global, Buffer-local, Window-local and Tabpage-local
3240 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003241 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003242 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 }
3245 }
3246 else
3247 {
3248 size_t len = end - *arg;
3249 int idx;
3250 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003251 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252
3253 name = vim_strnsave(*arg, end - *arg);
3254 if (name == NULL)
3255 return FAIL;
3256
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003257 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3258 {
3259 script_autoload(name, FALSE);
3260 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3261 }
3262 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3263 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003265 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003266 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 }
3268 else
3269 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003270 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003271
Bram Moolenaar709664c2020-12-12 14:33:41 +01003272 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003274 type = lvar.lv_type;
3275 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003276 if (lvar.lv_from_outer != 0)
3277 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003278 else
3279 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 }
3281 else
3282 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003283 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003284 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003285 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003286 || find_imported(name, 0, cctx) != NULL)
3287 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003288
Bram Moolenaar0f769812020-09-12 18:32:34 +02003289 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003290 // uppercase letter it can be a user defined function.
3291 // generate_funcref() will fail if the function can't be found.
3292 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003293 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 }
3295 }
3296 if (gen_load)
3297 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003298 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003299 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003300 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003301 cctx->ctx_outer_used = TRUE;
3302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 }
3304
3305 *arg = end;
3306
3307theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003308 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003309 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 vim_free(name);
3311 return res;
3312}
3313
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003314 static void
3315clear_instr_ga(garray_T *gap)
3316{
3317 int idx;
3318
3319 for (idx = 0; idx < gap->ga_len; ++idx)
3320 delete_instr(((isn_T *)gap->ga_data) + idx);
3321 ga_clear(gap);
3322}
3323
3324/*
3325 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3326 * Returns FAIL if compilation fails.
3327 */
3328 static int
3329compile_string(isn_T *isn, cctx_T *cctx)
3330{
3331 char_u *s = isn->isn_arg.string;
3332 garray_T save_ga = cctx->ctx_instr;
3333 int expr_res;
3334 int trailing_error;
3335 int instr_count;
3336 isn_T *instr = NULL;
3337
Bram Moolenaarcd268012021-07-22 19:11:08 +02003338 // Remove the string type from the stack.
3339 --cctx->ctx_type_stack.ga_len;
3340
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003341 // Temporarily reset the list of instructions so that the jump labels are
3342 // correct.
3343 cctx->ctx_instr.ga_len = 0;
3344 cctx->ctx_instr.ga_maxlen = 0;
3345 cctx->ctx_instr.ga_data = NULL;
3346 expr_res = compile_expr0(&s, cctx);
3347 s = skipwhite(s);
3348 trailing_error = *s != NUL;
3349
Bram Moolenaarff652882021-05-16 15:24:49 +02003350 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003351 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003352 {
3353 if (trailing_error)
3354 semsg(_(e_trailing_arg), s);
3355 clear_instr_ga(&cctx->ctx_instr);
3356 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003357 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003358 return FAIL;
3359 }
3360
3361 // Move the generated instructions into the ISN_INSTR instruction, then
3362 // restore the list of instructions.
3363 instr_count = cctx->ctx_instr.ga_len;
3364 instr = cctx->ctx_instr.ga_data;
3365 instr[instr_count].isn_type = ISN_FINISH;
3366
3367 cctx->ctx_instr = save_ga;
3368 vim_free(isn->isn_arg.string);
3369 isn->isn_type = ISN_INSTR;
3370 isn->isn_arg.instr = instr;
3371 return OK;
3372}
3373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374/*
3375 * Compile the argument expressions.
3376 * "arg" points to just after the "(" and is advanced to after the ")"
3377 */
3378 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003379compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003381 char_u *p = *arg;
3382 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003383 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003384 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385
Bram Moolenaare6085c52020-04-12 20:19:16 +02003386 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003388 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3389 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003390 if (*p == ')')
3391 {
3392 *arg = p + 1;
3393 return OK;
3394 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003395 if (must_end)
3396 {
3397 semsg(_(e_missing_comma_before_argument_str), p);
3398 return FAIL;
3399 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003400
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003401 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003402 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 return FAIL;
3404 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003405
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003406 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003407 && cctx->ctx_instr.ga_len == instr_count + 1)
3408 {
3409 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3410
3411 // {skip} argument of searchpair() can be compiled if not empty
3412 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3413 compile_string(isn, cctx);
3414 }
3415
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003416 if (*p != ',' && *skipwhite(p) == ',')
3417 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003418 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003419 p = skipwhite(p);
3420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003422 {
3423 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003424 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003425 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003426 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003427 else
3428 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003429 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003430 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003432failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003433 emsg(_(e_missing_close));
3434 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435}
3436
3437/*
3438 * Compile a function call: name(arg1, arg2)
3439 * "arg" points to "name", "arg + varlen" to the "(".
3440 * "argcount_init" is 1 for "value->method()"
3441 * Instructions:
3442 * EVAL arg1
3443 * EVAL arg2
3444 * BCALL / DCALL / UCALL
3445 */
3446 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003447compile_call(
3448 char_u **arg,
3449 size_t varlen,
3450 cctx_T *cctx,
3451 ppconst_T *ppconst,
3452 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453{
3454 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003455 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 int argcount = argcount_init;
3457 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003458 char_u fname_buf[FLEN_FIXED + 1];
3459 char_u *tofree = NULL;
3460 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003461 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003462 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003463 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003464 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003466 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003467 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003468 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003469 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003470 {
3471 char_u *s = skipwhite(*arg + varlen + 1);
3472 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003473 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003474
3475 argvars[0].v_type = VAR_UNKNOWN;
3476 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003477 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003478 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003479 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003480 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003481 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003482 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003483 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003484 {
3485 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3486
3487 *arg = s + 1;
3488 argvars[1].v_type = VAR_UNKNOWN;
3489 tv->v_type = VAR_NUMBER;
3490 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003491 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003492 f_has(argvars, tv);
3493 else
3494 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003495 clear_tv(&argvars[0]);
3496 ++ppconst->pp_used;
3497 return OK;
3498 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003499 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003500 if (!is_has)
3501 {
3502 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3503 return FAIL;
3504 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003505 }
3506
3507 if (generate_ppconst(cctx, ppconst) == FAIL)
3508 return FAIL;
3509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 if (varlen >= sizeof(namebuf))
3511 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003512 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 return FAIL;
3514 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003515 vim_strncpy(namebuf, *arg, varlen);
3516 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003518 // We handle the "skip" argument of searchpair() and searchpairpos()
3519 // differently.
3520 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3521 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3522 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3523 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003526 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003527 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528
Bram Moolenaar03290b82020-12-19 16:30:44 +01003529 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003530 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 {
3532 int idx;
3533
3534 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003535 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003537 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003538 if (STRCMP(name, "flatten") == 0)
3539 {
3540 emsg(_(e_cannot_use_flatten_in_vim9_script));
3541 goto theend;
3542 }
3543
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003544 if (STRCMP(name, "add") == 0 && argcount == 2)
3545 {
3546 garray_T *stack = &cctx->ctx_type_stack;
3547 type_T *type = ((type_T **)stack->ga_data)[
3548 stack->ga_len - 2];
3549
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003550 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003551 if (type->tt_type == VAR_LIST)
3552 {
3553 // inline "add(list, item)" so that the type can be checked
3554 res = generate_LISTAPPEND(cctx);
3555 idx = -1;
3556 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003557 else if (type->tt_type == VAR_BLOB)
3558 {
3559 // inline "add(blob, nr)" so that the type can be checked
3560 res = generate_BLOBAPPEND(cctx);
3561 idx = -1;
3562 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003563 }
3564
3565 if (idx >= 0)
3566 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3567 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003568 else
3569 semsg(_(e_unknownfunc), namebuf);
3570 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 }
3572
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003573 // An argument or local variable can be a function reference, this
3574 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003575 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003576 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003577 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003578 // If we can find the function by name generate the right call.
3579 // Skip global functions here, a local funcref takes precedence.
3580 ufunc = find_func(name, FALSE, cctx);
3581 if (ufunc != NULL && !func_is_global(ufunc))
3582 {
3583 res = generate_CALL(cctx, ufunc, argcount);
3584 goto theend;
3585 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003586 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587
3588 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003589 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003590 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003592 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003593 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003594 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003595 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003596 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003597
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003598 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003599 goto theend;
3600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601
Bram Moolenaar0f769812020-09-12 18:32:34 +02003602 // If we can find a global function by name generate the right call.
3603 if (ufunc != NULL)
3604 {
3605 res = generate_CALL(cctx, ufunc, argcount);
3606 goto theend;
3607 }
3608
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003609 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003610 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003611 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003612 res = generate_UCALL(cctx, name, argcount);
3613 else
3614 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003615
3616theend:
3617 vim_free(tofree);
3618 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619}
3620
3621// like NAMESPACE_CHAR but with 'a' and 'l'.
3622#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3623
3624/*
3625 * Find the end of a variable or function name. Unlike find_name_end() this
3626 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003627 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 * Return a pointer to just after the name. Equal to "arg" if there is no
3629 * valid name.
3630 */
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003631 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003632to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633{
3634 char_u *p;
3635
3636 // Quick check for valid starting character.
3637 if (!eval_isnamec1(*arg))
3638 return arg;
3639
3640 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3641 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3642 // and can be used in slice "[n:]".
3643 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003644 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3646 break;
3647 return p;
3648}
3649
3650/*
3651 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003652 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003653 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 */
3655 char_u *
3656to_name_const_end(char_u *arg)
3657{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003658 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 typval_T rettv;
3660
Bram Moolenaar678b2072021-07-26 21:10:11 +02003661 if (STRNCMP(p, "<SNR>", 5) == 0)
3662 p = skipdigits(p + 5);
3663 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 if (p == arg && *arg == '[')
3665 {
3666
3667 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003668 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 p = arg;
3670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 return p;
3672}
3673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674/*
3675 * parse a list: [expr, expr]
3676 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003677 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 */
3679 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003680compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681{
3682 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003683 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003685 int is_const;
3686 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003688 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003690 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003691 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003692 semsg(_(e_list_end), *arg);
3693 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003694 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003695 if (*p == ',')
3696 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003697 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003698 return FAIL;
3699 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003700 if (*p == ']')
3701 {
3702 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003703 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003704 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003705 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003706 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003707 if (!is_const)
3708 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 ++count;
3710 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003711 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003713 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3714 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003715 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003716 return FAIL;
3717 }
3718 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003719 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 p = skipwhite(p);
3721 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003722 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003724 ppconst->pp_is_const = is_all_const;
3725 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726}
3727
3728/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003729 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003730 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003731 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 */
3733 static int
3734compile_lambda(char_u **arg, cctx_T *cctx)
3735{
Bram Moolenaare462f522020-12-27 14:43:30 +01003736 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 typval_T rettv;
3738 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003739 evalarg_T evalarg;
3740
Bram Moolenaar844fb642021-10-23 13:32:30 +01003741 init_evalarg(&evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003742 evalarg.eval_flags = EVAL_EVALUATE;
3743 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744
3745 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003746 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3747 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003748 {
3749 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003750 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003751 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003752
Bram Moolenaar65c44152020-12-24 15:14:01 +01003753 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003755 ++ufunc->uf_refcount;
3756 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757
Bram Moolenaara9931532021-06-12 15:58:16 +02003758 // Compile it here to get the return type. The return type is optional,
3759 // when it's missing use t_unknown. This is recognized in
3760 // compile_return().
3761 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3762 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003763 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764
Bram Moolenaar648594e2021-07-11 17:55:01 +02003765#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003766 // When the outer function is compiled for profiling, the lambda may be
3767 // called without profiling. Compile it here in the right context.
3768 if (cctx->ctx_compile_type == CT_PROFILE)
3769 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003770#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003771
Bram Moolenaar844fb642021-10-23 13:32:30 +01003772 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
3773 // "*arg" may point into it. Point into the original line to avoid a
3774 // dangling pointer.
3775 if (evalarg.eval_using_cmdline)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003776 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003777 garray_T *gap = &evalarg.eval_tofree_ga;
3778 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003779
3780 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3781 + off;
3782 }
3783
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003784 clear_evalarg(&evalarg, NULL);
3785
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003786 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003787 {
3788 // The return type will now be known.
3789 set_function_type(ufunc);
3790
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003791 // The function reference count will be 1. When the ISN_FUNCREF
3792 // instruction is deleted the reference count is decremented and the
3793 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003794 return generate_FUNCREF(cctx, ufunc);
3795 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003796
3797 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 return FAIL;
3799}
3800
3801/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003802 * Get a lambda and compile it. Uses Vim9 syntax.
3803 */
3804 int
3805get_lambda_tv_and_compile(
3806 char_u **arg,
3807 typval_T *rettv,
3808 int types_optional,
3809 evalarg_T *evalarg)
3810{
3811 int r;
3812 ufunc_T *ufunc;
3813 int save_sc_version = current_sctx.sc_version;
3814
3815 // Get the funcref in "rettv".
3816 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3817 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3818 current_sctx.sc_version = save_sc_version;
3819 if (r != OK)
3820 return r;
3821
3822 // "rettv" will now be a partial referencing the function.
3823 ufunc = rettv->vval.v_partial->pt_func;
3824
3825 // Compile it here to get the return type. The return type is optional,
3826 // when it's missing use t_unknown. This is recognized in
3827 // compile_return().
3828 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3829 ufunc->uf_ret_type = &t_unknown;
3830 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3831
3832 if (ufunc->uf_def_status == UF_COMPILED)
3833 {
3834 // The return type will now be known.
3835 set_function_type(ufunc);
3836 return OK;
3837 }
3838 clear_tv(rettv);
3839 return FAIL;
3840}
3841
3842/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003843 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003845 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 */
3847 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003848compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849{
3850 garray_T *instr = &cctx->ctx_instr;
3851 int count = 0;
3852 dict_T *d = dict_alloc();
3853 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003854 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003855 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003856 int is_const;
3857 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858
3859 if (d == NULL)
3860 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003861 if (generate_ppconst(cctx, ppconst) == FAIL)
3862 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003863 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003865 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866
Bram Moolenaar23c55272020-06-21 16:58:13 +02003867 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003868 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003869 *arg = NULL;
3870 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003871 }
3872
3873 if (**arg == '}')
3874 break;
3875
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003876 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003878 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003880 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003881 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003882 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003885 if (isn->isn_type == ISN_PUSHNR)
3886 {
3887 char buf[NUMBUFLEN];
3888
3889 // Convert to string at compile time.
3890 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3891 isn->isn_type = ISN_PUSHS;
3892 isn->isn_arg.string = vim_strsave((char_u *)buf);
3893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 if (isn->isn_type == ISN_PUSHS)
3895 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003896 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003897 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003898 *arg = skipwhite(*arg);
3899 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003900 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003901 emsg(_(e_missing_matching_bracket_after_dict_key));
3902 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003903 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003904 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003906 else
3907 {
3908 // {"name": value},
3909 // {'name': value},
3910 // {name: value} use "name" as a literal key
3911 key = get_literal_key(arg);
3912 if (key == NULL)
3913 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003914 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003915 return FAIL;
3916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917
3918 // Check for duplicate keys, if using string keys.
3919 if (key != NULL)
3920 {
3921 item = dict_find(d, key, -1);
3922 if (item != NULL)
3923 {
3924 semsg(_(e_duplicate_key), key);
3925 goto failret;
3926 }
3927 item = dictitem_alloc(key);
3928 if (item != NULL)
3929 {
3930 item->di_tv.v_type = VAR_UNKNOWN;
3931 item->di_tv.v_lock = 0;
3932 if (dict_add(d, item) == FAIL)
3933 dictitem_free(item);
3934 }
3935 }
3936
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 if (**arg != ':')
3938 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003939 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003940 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003941 else
3942 semsg(_(e_missing_dict_colon), *arg);
3943 return FAIL;
3944 }
3945 whitep = *arg + 1;
3946 if (!IS_WHITE_OR_NUL(*whitep))
3947 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003948 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 return FAIL;
3950 }
3951
Bram Moolenaar23c55272020-06-21 16:58:13 +02003952 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003953 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003954 *arg = NULL;
3955 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003956 }
3957
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003958 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003960 if (!is_const)
3961 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 ++count;
3963
Bram Moolenaar2c330432020-04-13 14:41:35 +02003964 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003965 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003966 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003967 *arg = NULL;
3968 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 if (**arg == '}')
3971 break;
3972 if (**arg != ',')
3973 {
3974 semsg(_(e_missing_dict_comma), *arg);
3975 goto failret;
3976 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003977 if (IS_WHITE_OR_NUL(*whitep))
3978 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003979 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003980 return FAIL;
3981 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003982 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003983 if (!IS_WHITE_OR_NUL(*whitep))
3984 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003985 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003986 return FAIL;
3987 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003988 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 }
3990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 *arg = *arg + 1;
3992
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003993 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003994 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003995 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003996 *arg += STRLEN(*arg);
3997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003999 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 return generate_NEWDICT(cctx, count);
4001
4002failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004003 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004004 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004005 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004006 *arg = (char_u *)"";
4007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 dict_unref(d);
4009 return FAIL;
4010}
4011
4012/*
4013 * Compile "&option".
4014 */
4015 static int
4016compile_get_option(char_u **arg, cctx_T *cctx)
4017{
4018 typval_T rettv;
4019 char_u *start = *arg;
4020 int ret;
4021
4022 // parse the option and get the current value to get the type.
4023 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004024 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 if (ret == OK)
4026 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004027 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01004028 char_u *name = vim_strnsave(start, *arg - start);
4029 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
4030 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031
4032 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4033 vim_free(name);
4034 }
4035 clear_tv(&rettv);
4036
4037 return ret;
4038}
4039
4040/*
4041 * Compile "$VAR".
4042 */
4043 static int
4044compile_get_env(char_u **arg, cctx_T *cctx)
4045{
4046 char_u *start = *arg;
4047 int len;
4048 int ret;
4049 char_u *name;
4050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 ++*arg;
4052 len = get_env_len(arg);
4053 if (len == 0)
4054 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004055 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 return FAIL;
4057 }
4058
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004059 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 name = vim_strnsave(start, len + 1);
4061 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4062 vim_free(name);
4063 return ret;
4064}
4065
4066/*
4067 * Compile "@r".
4068 */
4069 static int
4070compile_get_register(char_u **arg, cctx_T *cctx)
4071{
4072 int ret;
4073
4074 ++*arg;
4075 if (**arg == NUL)
4076 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004077 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 return FAIL;
4079 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004080 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 {
4082 emsg_invreg(**arg);
4083 return FAIL;
4084 }
4085 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4086 ++*arg;
4087 return ret;
4088}
4089
4090/*
4091 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004092 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 */
4094 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004095apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004097 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098
4099 // this works from end to start
4100 while (p > start)
4101 {
4102 --p;
4103 if (*p == '-' || *p == '+')
4104 {
4105 // only '-' has an effect, for '+' we only check the type
4106#ifdef FEAT_FLOAT
4107 if (rettv->v_type == VAR_FLOAT)
4108 {
4109 if (*p == '-')
4110 rettv->vval.v_float = -rettv->vval.v_float;
4111 }
4112 else
4113#endif
4114 {
4115 varnumber_T val;
4116 int error = FALSE;
4117
4118 // tv_get_number_chk() accepts a string, but we don't want that
4119 // here
4120 if (check_not_string(rettv) == FAIL)
4121 return FAIL;
4122 val = tv_get_number_chk(rettv, &error);
4123 clear_tv(rettv);
4124 if (error)
4125 return FAIL;
4126 if (*p == '-')
4127 val = -val;
4128 rettv->v_type = VAR_NUMBER;
4129 rettv->vval.v_number = val;
4130 }
4131 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004132 else if (numeric_only)
4133 {
4134 ++p;
4135 break;
4136 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004137 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 {
4139 int v = tv2bool(rettv);
4140
4141 // '!' is permissive in the type.
4142 clear_tv(rettv);
4143 rettv->v_type = VAR_BOOL;
4144 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4145 }
4146 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004147 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 return OK;
4149}
4150
4151/*
4152 * Recognize v: variables that are constants and set "rettv".
4153 */
4154 static void
4155get_vim_constant(char_u **arg, typval_T *rettv)
4156{
4157 if (STRNCMP(*arg, "v:true", 6) == 0)
4158 {
4159 rettv->v_type = VAR_BOOL;
4160 rettv->vval.v_number = VVAL_TRUE;
4161 *arg += 6;
4162 }
4163 else if (STRNCMP(*arg, "v:false", 7) == 0)
4164 {
4165 rettv->v_type = VAR_BOOL;
4166 rettv->vval.v_number = VVAL_FALSE;
4167 *arg += 7;
4168 }
4169 else if (STRNCMP(*arg, "v:null", 6) == 0)
4170 {
4171 rettv->v_type = VAR_SPECIAL;
4172 rettv->vval.v_number = VVAL_NULL;
4173 *arg += 6;
4174 }
4175 else if (STRNCMP(*arg, "v:none", 6) == 0)
4176 {
4177 rettv->v_type = VAR_SPECIAL;
4178 rettv->vval.v_number = VVAL_NONE;
4179 *arg += 6;
4180 }
4181}
4182
Bram Moolenaar657137c2021-01-09 15:45:23 +01004183 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004184get_compare_type(char_u *p, int *len, int *type_is)
4185{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004186 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004187 int i;
4188
4189 switch (p[0])
4190 {
4191 case '=': if (p[1] == '=')
4192 type = EXPR_EQUAL;
4193 else if (p[1] == '~')
4194 type = EXPR_MATCH;
4195 break;
4196 case '!': if (p[1] == '=')
4197 type = EXPR_NEQUAL;
4198 else if (p[1] == '~')
4199 type = EXPR_NOMATCH;
4200 break;
4201 case '>': if (p[1] != '=')
4202 {
4203 type = EXPR_GREATER;
4204 *len = 1;
4205 }
4206 else
4207 type = EXPR_GEQUAL;
4208 break;
4209 case '<': if (p[1] != '=')
4210 {
4211 type = EXPR_SMALLER;
4212 *len = 1;
4213 }
4214 else
4215 type = EXPR_SEQUAL;
4216 break;
4217 case 'i': if (p[1] == 's')
4218 {
4219 // "is" and "isnot"; but not a prefix of a name
4220 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4221 *len = 5;
4222 i = p[*len];
4223 if (!isalnum(i) && i != '_')
4224 {
4225 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4226 *type_is = TRUE;
4227 }
4228 }
4229 break;
4230 }
4231 return type;
4232}
4233
4234/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004235 * Skip over an expression, ignoring most errors.
4236 */
4237 static void
4238skip_expr_cctx(char_u **arg, cctx_T *cctx)
4239{
4240 evalarg_T evalarg;
4241
Bram Moolenaar844fb642021-10-23 13:32:30 +01004242 init_evalarg(&evalarg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004243 evalarg.eval_cctx = cctx;
4244 skip_expr(arg, &evalarg);
Bram Moolenaar844fb642021-10-23 13:32:30 +01004245 clear_evalarg(&evalarg, NULL);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004246}
4247
4248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004250 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 */
4252 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004253compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004255 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256
4257 // this works from end to start
4258 while (p > start)
4259 {
4260 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004261 while (VIM_ISWHITE(*p))
4262 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 if (*p == '-' || *p == '+')
4264 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004265 int negate = *p == '-';
4266 isn_T *isn;
4267 garray_T *stack = &cctx->ctx_type_stack;
4268 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004270 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4271 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4272 return FAIL;
4273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4275 {
4276 --p;
4277 if (*p == '-')
4278 negate = !negate;
4279 }
4280 // only '-' has an effect, for '+' we only check the type
4281 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004282 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004284 if (isn == NULL)
4285 return FAIL;
4286 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004288 else if (numeric_only)
4289 {
4290 ++p;
4291 break;
4292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 else
4294 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004295 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004297 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004299 if (p[-1] == '!')
4300 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004303 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 return FAIL;
4305 }
4306 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004307 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 return OK;
4309}
4310
4311/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004312 * Compile "(expression)": recursive!
4313 * Return FAIL/OK.
4314 */
4315 static int
4316compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4317{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004318 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004319 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004320
Bram Moolenaar24156692021-01-14 20:35:49 +01004321 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4322 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004323 if (ppconst->pp_used <= PPSIZE - 10)
4324 {
4325 ret = compile_expr1(arg, cctx, ppconst);
4326 }
4327 else
4328 {
4329 // Not enough space in ppconst, flush constants.
4330 if (generate_ppconst(cctx, ppconst) == FAIL)
4331 return FAIL;
4332 ret = compile_expr0(arg, cctx);
4333 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004334 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4335 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004336 if (**arg == ')')
4337 ++*arg;
4338 else if (ret == OK)
4339 {
4340 emsg(_(e_missing_close));
4341 ret = FAIL;
4342 }
4343 return ret;
4344}
4345
4346/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004348 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 */
4350 static int
4351compile_subscript(
4352 char_u **arg,
4353 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004354 char_u *start_leader,
4355 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004356 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004358 char_u *name_start = *end_leader;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004359 int keeping_dict = FALSE;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 for (;;)
4362 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004363 char_u *p = skipwhite(*arg);
4364
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004365 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004366 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004367 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004368
4369 // If a following line starts with "->{" or "->X" advance to that
4370 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004371 // Also if a following line starts with ".x".
4372 if (next != NULL &&
4373 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004374 && (next[2] == '{'
4375 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004376 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004377 {
4378 next = next_line_from_context(cctx, TRUE);
4379 if (next == NULL)
4380 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004381 *arg = next;
4382 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004383 }
4384 }
4385
Bram Moolenaarcd268012021-07-22 19:11:08 +02004386 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4387 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004388 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004390 garray_T *stack = &cctx->ctx_type_stack;
4391 type_T *type;
4392 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004394 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004395 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004396 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004399 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4400
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004401 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004402 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004404 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004406 if (keeping_dict)
4407 {
4408 keeping_dict = FALSE;
4409 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4410 return FAIL;
4411 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004413 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004415 char_u *pstart = p;
4416
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004417 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004418 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004419 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 // something->method()
4422 // Apply the '!', '-' and '+' first:
4423 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004424 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004427 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004428 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004429 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004430 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004431 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004432 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004433 garray_T *stack = &cctx->ctx_type_stack;
4434 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004435 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004436 int expr_isn_start = cctx->ctx_instr.ga_len;
4437 int expr_isn_end;
4438 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004439
4440 // Funcref call: list->(Refs[2])(arg)
4441 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004442 //
4443 // Fist compile the function expression.
4444 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004445 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004446
4447 // Remember the next instruction index, where the instructions
4448 // for arguments are being written.
4449 expr_isn_end = cctx->ctx_instr.ga_len;
4450
4451 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004452 if (**arg != '(')
4453 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004454 if (*skipwhite(*arg) == '(')
4455 emsg(_(e_nowhitespace));
4456 else
4457 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004458 return FAIL;
4459 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004460 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004461 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004462 return FAIL;
4463
Bram Moolenaar2927c072021-04-05 19:41:21 +02004464 // Move the instructions for the arguments to before the
4465 // instructions of the expression and move the type of the
4466 // expression after the argument types. This is what ISN_PCALL
4467 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004468 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004469 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4470 if (arg_isn_count > 0)
4471 {
4472 int expr_isn_count = expr_isn_end - expr_isn_start;
4473 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4474
4475 if (isn == NULL)
4476 return FAIL;
4477 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4478 + expr_isn_start,
4479 sizeof(isn_T) * expr_isn_count);
4480 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4481 + expr_isn_start,
4482 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4483 sizeof(isn_T) * arg_isn_count);
4484 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4485 + expr_isn_start + arg_isn_count,
4486 isn, sizeof(isn_T) * expr_isn_count);
4487 vim_free(isn);
4488
4489 type = ((type_T **)stack->ga_data)[type_idx_start];
4490 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4491 ((type_T **)stack->ga_data) + type_idx_start + 1,
4492 sizeof(type_T *)
4493 * (stack->ga_len - type_idx_start - 1));
4494 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4495 }
4496
Bram Moolenaar7e368202020-12-25 21:56:57 +01004497 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004498 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 return FAIL;
4500 }
4501 else
4502 {
4503 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004504 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004505 if (!eval_isnamec1(*p))
4506 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004507 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004508 return FAIL;
4509 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004510 if (ASCII_ISALPHA(*p) && p[1] == ':')
4511 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004512 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 ;
4514 if (*p != '(')
4515 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004516 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 return FAIL;
4518 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004519 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 return FAIL;
4521 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004522 if (keeping_dict)
4523 {
4524 keeping_dict = FALSE;
4525 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4526 return FAIL;
4527 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004529 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004531 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004532
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004533 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004534 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004535 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004536 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004537 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004538 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004539 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004540
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004541 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004542 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004543 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004544 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004545 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004546 // missing first index is equal to zero
4547 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004548 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004549 else
4550 {
4551 if (compile_expr0(arg, cctx) == FAIL)
4552 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004553 if (**arg == ':')
4554 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004555 semsg(_(e_white_space_required_before_and_after_str_at_str),
4556 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004557 return FAIL;
4558 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004559 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004560 return FAIL;
4561 *arg = skipwhite(*arg);
4562 }
4563 if (**arg == ':')
4564 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004565 is_slice = TRUE;
4566 ++*arg;
4567 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4568 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004569 semsg(_(e_white_space_required_before_and_after_str_at_str),
4570 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004571 return FAIL;
4572 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004573 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004574 return FAIL;
4575 if (**arg == ']')
4576 // missing second index is equal to end of string
4577 generate_PUSHNR(cctx, -1);
4578 else
4579 {
4580 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004581 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004582 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004583 return FAIL;
4584 *arg = skipwhite(*arg);
4585 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004586 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587
4588 if (**arg != ']')
4589 {
4590 emsg(_(e_missbrac));
4591 return FAIL;
4592 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004593 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004595 if (keeping_dict)
4596 {
4597 keeping_dict = FALSE;
4598 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4599 return FAIL;
4600 }
4601 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004602 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004604 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004606 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004607 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004608 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004609 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004610
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004611 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004612 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004613 {
4614 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004615 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004616 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004617 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004618 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 while (eval_isnamec(*p))
4620 MB_PTR_ADV(p);
4621 if (p == *arg)
4622 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004623 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 return FAIL;
4625 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004626 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
4627 return FAIL;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004628 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004630 keeping_dict = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 *arg = p;
4632 }
4633 else
4634 break;
4635 }
4636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 // Turn "dict.Func" into a partial for "Func" bound to "dict".
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004638 // This needs to be done at runtime to be able to check the type.
4639 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
4640 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641
4642 return OK;
4643}
4644
4645/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004646 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4647 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004649 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004650 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004651 *
4652 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 */
4654
4655/*
4656 * number number constant
4657 * 0zFFFFFFFF Blob constant
4658 * "string" string constant
4659 * 'string' literal string constant
4660 * &option-name option value
4661 * @r register contents
4662 * identifier variable value
4663 * function() function call
4664 * $VAR environment variable
4665 * (expression) nested expression
4666 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004667 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 *
4669 * Also handle:
4670 * ! in front logical NOT
4671 * - in front unary minus
4672 * + in front unary plus (ignored)
4673 * trailing (arg) funcref/partial call
4674 * trailing [] subscript in String or List
4675 * trailing .name entry in Dictionary
4676 * trailing ->name() method call
4677 */
4678 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004679compile_expr7(
4680 char_u **arg,
4681 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004682 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 char_u *start_leader, *end_leader;
4685 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004686 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004687 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004689 ppconst->pp_is_const = FALSE;
4690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 /*
4692 * Skip '!', '-' and '+' characters. They are handled later.
4693 */
4694 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004695 if (eval_leader(arg, TRUE) == FAIL)
4696 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 end_leader = *arg;
4698
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004699 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 switch (**arg)
4701 {
4702 /*
4703 * Number constant.
4704 */
4705 case '0': // also for blob starting with 0z
4706 case '1':
4707 case '2':
4708 case '3':
4709 case '4':
4710 case '5':
4711 case '6':
4712 case '7':
4713 case '8':
4714 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004715 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004717 // Apply "-" and "+" just before the number now, right to
4718 // left. Matters especially when "->" follows. Stops at
4719 // '!'.
4720 if (apply_leader(rettv, TRUE,
4721 start_leader, &end_leader) == FAIL)
4722 {
4723 clear_tv(rettv);
4724 return FAIL;
4725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 break;
4727
4728 /*
4729 * String constant: "string".
4730 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004731 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 return FAIL;
4733 break;
4734
4735 /*
4736 * Literal string constant: 'str''ing'.
4737 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004738 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 return FAIL;
4740 break;
4741
4742 /*
4743 * Constant Vim variable.
4744 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004745 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 ret = NOTDONE;
4747 break;
4748
4749 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004750 * "true" constant
4751 */
4752 case 't': if (STRNCMP(*arg, "true", 4) == 0
4753 && !eval_isnamec((*arg)[4]))
4754 {
4755 *arg += 4;
4756 rettv->v_type = VAR_BOOL;
4757 rettv->vval.v_number = VVAL_TRUE;
4758 }
4759 else
4760 ret = NOTDONE;
4761 break;
4762
4763 /*
4764 * "false" constant
4765 */
4766 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4767 && !eval_isnamec((*arg)[5]))
4768 {
4769 *arg += 5;
4770 rettv->v_type = VAR_BOOL;
4771 rettv->vval.v_number = VVAL_FALSE;
4772 }
4773 else
4774 ret = NOTDONE;
4775 break;
4776
4777 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004778 * "null" constant
4779 */
4780 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004781 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004782 {
4783 *arg += 4;
4784 rettv->v_type = VAR_SPECIAL;
4785 rettv->vval.v_number = VVAL_NULL;
4786 }
4787 else
4788 ret = NOTDONE;
4789 break;
4790
4791 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 * List: [expr, expr]
4793 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004794 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4795 return FAIL;
4796 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 break;
4798
4799 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 * Dictionary: {'key': val, 'key': val}
4801 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004802 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4803 return FAIL;
4804 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 break;
4806
4807 /*
4808 * Option value: &name
4809 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004810 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4811 return FAIL;
4812 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004813 break;
4814
4815 /*
4816 * Environment variable: $VAR.
4817 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004818 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4819 return FAIL;
4820 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 break;
4822
4823 /*
4824 * Register contents: @r.
4825 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004826 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4827 return FAIL;
4828 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 break;
4830 /*
4831 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004832 * lambda: (arg, arg) => expr
4833 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004835 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4836 ret = compile_lambda(arg, cctx);
4837 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004838 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 break;
4840
4841 default: ret = NOTDONE;
4842 break;
4843 }
4844 if (ret == FAIL)
4845 return FAIL;
4846
Bram Moolenaar1c747212020-05-09 18:28:34 +02004847 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004849 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004850 clear_tv(rettv);
4851 else
4852 // A constant expression can possibly be handled compile time,
4853 // return the value instead of generating code.
4854 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855 }
4856 else if (ret == NOTDONE)
4857 {
4858 char_u *p;
4859 int r;
4860
4861 if (!eval_isnamec1(**arg))
4862 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004863 if (!vim9_bad_comment(*arg))
4864 {
4865 if (ends_excmd(*skipwhite(*arg)))
4866 semsg(_(e_empty_expression_str), *arg);
4867 else
4868 semsg(_(e_name_expected_str), *arg);
4869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 return FAIL;
4871 }
4872
4873 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004874 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004875 if (p - *arg == (size_t)1 && **arg == '_')
4876 {
4877 emsg(_(e_cannot_use_underscore_here));
4878 return FAIL;
4879 }
4880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004882 {
4883 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004886 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02004887 if (cctx->ctx_skip != SKIP_YES
4888 && generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004889 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004890 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 if (r == FAIL)
4893 return FAIL;
4894 }
4895
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004896 // Handle following "[]", ".member", etc.
4897 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004898 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004899 ppconst) == FAIL)
4900 return FAIL;
4901 if (ppconst->pp_used > 0)
4902 {
4903 // apply the '!', '-' and '+' before the constant
4904 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004905 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004906 return FAIL;
4907 return OK;
4908 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004909 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004911 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912}
4913
4914/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004915 * Give the "white on both sides" error, taking the operator from "p[len]".
4916 */
4917 void
4918error_white_both(char_u *op, int len)
4919{
4920 char_u buf[10];
4921
4922 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004923 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004924}
4925
4926/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004927 * <type>expr7: runtime type check / conversion
4928 */
4929 static int
4930compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4931{
4932 type_T *want_type = NULL;
4933
4934 // Recognize <type>
4935 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4936 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004937 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004938 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4939 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004940 return FAIL;
4941
4942 if (**arg != '>')
4943 {
4944 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004945 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004946 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004947 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004948 return FAIL;
4949 }
4950 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004951 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004952 return FAIL;
4953 }
4954
4955 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4956 return FAIL;
4957
4958 if (want_type != NULL)
4959 {
4960 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004961 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004962 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004963
Bram Moolenaard1103582020-08-14 22:44:25 +02004964 generate_ppconst(cctx, ppconst);
4965 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004966 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004967 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004968 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004969 return FAIL;
4970 }
4971 }
4972
4973 return OK;
4974}
4975
4976/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 * * number multiplication
4978 * / number division
4979 * % number modulo
4980 */
4981 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004982compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983{
4984 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004985 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004986 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004988 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004989 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 return FAIL;
4991
4992 /*
4993 * Repeat computing, until no "*", "/" or "%" is following.
4994 */
4995 for (;;)
4996 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004997 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 if (*op != '*' && *op != '/' && *op != '%')
4999 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005000 if (next != NULL)
5001 {
5002 *arg = next_line_from_context(cctx, TRUE);
5003 op = skipwhite(*arg);
5004 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005005
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005006 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005008 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005009 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01005011 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005012 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005014 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005015 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005017
5018 if (ppconst->pp_used == ppconst_used + 2
5019 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5020 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005021 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005022 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5023 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
5024 varnumber_T res = 0;
5025 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005027 // both are numbers: compute the result
5028 switch (*op)
5029 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005030 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005031 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005032 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005033 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005034 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005035 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005036 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005037 break;
5038 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005039 if (failed)
5040 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005041 tv1->vval.v_number = res;
5042 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005043 }
5044 else
5045 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005046 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005047 generate_two_op(cctx, op);
5048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 }
5050
5051 return OK;
5052}
5053
5054/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01005055 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 * - number subtraction
5057 * .. string concatenation
5058 */
5059 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005060compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061{
5062 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005063 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005065 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066
5067 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005068 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 return FAIL;
5070
5071 /*
5072 * Repeat computing, until no "+", "-" or ".." is following.
5073 */
5074 for (;;)
5075 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005076 op = may_peek_next_line(cctx, *arg, &next);
5077 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005079 if (op[0] == op[1] && *op != '.' && next)
5080 // Finding "++" or "--" on the next line is a separate command.
5081 // But ".." is concatenation.
5082 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005084 if (next != NULL)
5085 {
5086 *arg = next_line_from_context(cctx, TRUE);
5087 op = skipwhite(*arg);
5088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005090 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005092 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005093 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 }
5095
Bram Moolenaare0de1712020-12-02 17:36:54 +01005096 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005097 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005099 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005100 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 return FAIL;
5102
Bram Moolenaara5565e42020-05-09 15:44:01 +02005103 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005104 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005105 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5106 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5107 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5108 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005110 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5111 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005112
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005113 // concat/subtract/add constant numbers
5114 if (*op == '+')
5115 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5116 else if (*op == '-')
5117 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5118 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005119 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005120 // concatenate constant strings
5121 char_u *s1 = tv1->vval.v_string;
5122 char_u *s2 = tv2->vval.v_string;
5123 size_t len1 = STRLEN(s1);
5124
5125 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5126 if (tv1->vval.v_string == NULL)
5127 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005128 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005129 return FAIL;
5130 }
5131 mch_memmove(tv1->vval.v_string, s1, len1);
5132 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005133 vim_free(s1);
5134 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005135 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005136 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 }
5138 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005139 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005140 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005141 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005142 if (*op == '.')
5143 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005144 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5145 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005146 return FAIL;
5147 generate_instr_drop(cctx, ISN_CONCAT, 1);
5148 }
5149 else
5150 generate_two_op(cctx, op);
5151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152 }
5153
5154 return OK;
5155}
5156
5157/*
5158 * expr5a == expr5b
5159 * expr5a =~ expr5b
5160 * expr5a != expr5b
5161 * expr5a !~ expr5b
5162 * expr5a > expr5b
5163 * expr5a >= expr5b
5164 * expr5a < expr5b
5165 * expr5a <= expr5b
5166 * expr5a is expr5b
5167 * expr5a isnot expr5b
5168 *
5169 * Produces instructions:
5170 * EVAL expr5a Push result of "expr5a"
5171 * EVAL expr5b Push result of "expr5b"
5172 * COMPARE one of the compare instructions
5173 */
5174 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005175compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005177 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005179 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005182 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183
5184 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005185 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186 return FAIL;
5187
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005188 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005189 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190
5191 /*
5192 * If there is a comparative operator, use it.
5193 */
5194 if (type != EXPR_UNKNOWN)
5195 {
5196 int ic = FALSE; // Default: do not ignore case
5197
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005198 if (next != NULL)
5199 {
5200 *arg = next_line_from_context(cctx, TRUE);
5201 p = skipwhite(*arg);
5202 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203 if (type_is && (p[len] == '?' || p[len] == '#'))
5204 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005205 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 return FAIL;
5207 }
5208 // extra question mark appended: ignore case
5209 if (p[len] == '?')
5210 {
5211 ic = TRUE;
5212 ++len;
5213 }
5214 // extra '#' appended: match case (ignored)
5215 else if (p[len] == '#')
5216 ++len;
5217 // nothing appended: match case
5218
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005219 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005221 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005222 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 }
5224
5225 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005226 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005227 return FAIL;
5228
Bram Moolenaara5565e42020-05-09 15:44:01 +02005229 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230 return FAIL;
5231
Bram Moolenaara5565e42020-05-09 15:44:01 +02005232 if (ppconst->pp_used == ppconst_used + 2)
5233 {
5234 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5235 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5236 int ret;
5237
5238 // Both sides are a constant, compute the result now.
5239 // First check for a valid combination of types, this is more
5240 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005241 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005242 ret = FAIL;
5243 else
5244 {
5245 ret = typval_compare(tv1, tv2, type, ic);
5246 tv1->v_type = VAR_BOOL;
5247 tv1->vval.v_number = tv1->vval.v_number
5248 ? VVAL_TRUE : VVAL_FALSE;
5249 clear_tv(tv2);
5250 --ppconst->pp_used;
5251 }
5252 return ret;
5253 }
5254
5255 generate_ppconst(cctx, ppconst);
5256 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005257 }
5258
5259 return OK;
5260}
5261
Bram Moolenaar7f141552020-05-09 17:35:53 +02005262static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005264/*
5265 * Compile || or &&.
5266 */
5267 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005268compile_and_or(
5269 char_u **arg,
5270 cctx_T *cctx,
5271 char *op,
5272 ppconst_T *ppconst,
5273 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005274{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005275 char_u *next;
5276 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005277 int opchar = *op;
5278
5279 if (p[0] == opchar && p[1] == opchar)
5280 {
5281 garray_T *instr = &cctx->ctx_instr;
5282 garray_T end_ga;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005283 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284
5285 /*
5286 * Repeat until there is no following "||" or "&&"
5287 */
5288 ga_init2(&end_ga, sizeof(int), 10);
5289 while (p[0] == opchar && p[1] == opchar)
5290 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005291 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005292 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005293 int start_ctx_lnum = cctx->ctx_lnum;
5294 int save_lnum;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005295 int const_used;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005296 int status;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005297 jumpwhen_T jump_when = opchar == '|'
5298 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005299
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005300 if (next != NULL)
5301 {
5302 *arg = next_line_from_context(cctx, TRUE);
5303 p = skipwhite(*arg);
5304 }
5305
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005306 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5307 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005308 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005309 op, p);
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005310 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005311 return FAIL;
5312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005313
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005314 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005315 SOURCING_LNUM = start_lnum;
5316 save_lnum = cctx->ctx_lnum;
5317 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005318
5319 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005320 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005321 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005322 // Use the last ppconst if possible.
5323 if (ppconst->pp_used > 0)
5324 {
5325 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5326 int is_true = tv2bool(tv);
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005327
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005328 if ((is_true && opchar == '|')
5329 || (!is_true && opchar == '&'))
5330 {
5331 // For "false && expr" and "true || expr" the "expr"
5332 // does not need to be evaluated.
5333 cctx->ctx_skip = SKIP_YES;
5334 clear_tv(tv);
5335 tv->v_type = VAR_BOOL;
5336 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
5337 }
5338 else
5339 {
5340 // For "true && expr" and "false || expr" only "expr"
5341 // needs to be evaluated.
5342 --ppconst->pp_used;
5343 jump_when = JUMP_NEVER;
5344 }
5345 }
5346 else
5347 {
5348 // Every part must evaluate to a bool.
5349 status = bool_on_stack(cctx);
5350 }
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005351 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005352 if (status != FAIL)
5353 status = ga_grow(&end_ga, 1);
Bram Moolenaara7511c02021-04-03 21:47:07 +02005354 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005355 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005356 {
5357 ga_clear(&end_ga);
5358 return FAIL;
5359 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005360
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005361 if (jump_when != JUMP_NEVER)
5362 {
5363 if (cctx->ctx_skip != SKIP_YES)
5364 {
5365 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5366 ++end_ga.ga_len;
5367 }
5368 generate_JUMP(cctx, jump_when, 0);
5369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370
5371 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005372 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005373 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005374 {
5375 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005376 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005377 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005378
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005379 const_used = ppconst->pp_used;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005380 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5381 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005382 {
5383 ga_clear(&end_ga);
5384 return FAIL;
5385 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005386
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005387 // "0 || 1" results in true, "1 && 0" results in false.
5388 if (ppconst->pp_used == const_used + 1)
5389 {
5390 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5391
5392 if (tv->v_type == VAR_NUMBER
5393 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
5394 {
5395 tv->vval.v_number = tv->vval.v_number == 1
5396 ? VVAL_TRUE : VVAL_FALSE;
5397 tv->v_type = VAR_BOOL;
5398 }
5399 }
5400
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005401 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005403
5404 if (check_ppconst_bool(ppconst) == FAIL)
5405 {
5406 ga_clear(&end_ga);
5407 return FAIL;
5408 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005410 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
5411 // Every part must evaluate to a bool.
5412 if (bool_on_stack(cctx) == FAIL)
5413 {
5414 ga_clear(&end_ga);
5415 return FAIL;
5416 }
5417
5418 if (end_ga.ga_len > 0)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005419 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005420 // Fill in the end label in all jumps.
5421 generate_ppconst(cctx, ppconst);
5422 while (end_ga.ga_len > 0)
5423 {
5424 isn_T *isn;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005425
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005426 --end_ga.ga_len;
5427 isn = ((isn_T *)instr->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005429 isn->isn_arg.jump.jump_where = instr->ga_len;
5430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 }
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005432 ga_clear(&end_ga);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005433
5434 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005435 }
5436
5437 return OK;
5438}
5439
5440/*
5441 * expr4a && expr4a && expr4a logical AND
5442 *
5443 * Produces instructions:
5444 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005445 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005446 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005448 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449 * EVAL expr4c Push result of "expr4c"
5450 * end:
5451 */
5452 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005453compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005455 int ppconst_used = ppconst->pp_used;
5456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005458 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459 return FAIL;
5460
5461 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005462 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463}
5464
5465/*
5466 * expr3a || expr3b || expr3c logical OR
5467 *
5468 * Produces instructions:
5469 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005470 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005471 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005472 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005473 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474 * EVAL expr3c Push result of "expr3c"
5475 * end:
5476 */
5477 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005478compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005479{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005480 int ppconst_used = ppconst->pp_used;
5481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005483 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005484 return FAIL;
5485
5486 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005487 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005488}
5489
5490/*
5491 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005492 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005493 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494 * JUMP_IF_FALSE alt jump if false
5495 * EVAL expr1a
5496 * JUMP_ALWAYS end
5497 * alt: EVAL expr1b
5498 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005499 *
5500 * Toplevel expression: expr2 ?? expr1
5501 * Produces instructions:
5502 * EVAL expr2 Push result of "expr2"
5503 * JUMP_AND_KEEP_IF_TRUE end jump if true
5504 * EVAL expr1
5505 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005506 */
5507 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005508compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509{
5510 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005511 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005512 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513
Bram Moolenaar3988f642020-08-27 22:43:03 +02005514 // Ignore all kinds of errors when not producing code.
5515 if (cctx->ctx_skip == SKIP_YES)
5516 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005517 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005518 return OK;
5519 }
5520
Bram Moolenaar61a89812020-05-07 16:58:17 +02005521 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005522 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005523 return FAIL;
5524
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005525 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005526 if (*p == '?')
5527 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005528 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005529 garray_T *instr = &cctx->ctx_instr;
5530 garray_T *stack = &cctx->ctx_type_stack;
5531 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005532 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005534 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005535 int has_const_expr = FALSE;
5536 int const_value = FALSE;
5537 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005538
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005539 if (next != NULL)
5540 {
5541 *arg = next_line_from_context(cctx, TRUE);
5542 p = skipwhite(*arg);
5543 }
5544
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005545 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005546 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005547 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005548 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005549 return FAIL;
5550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005551
Bram Moolenaara5565e42020-05-09 15:44:01 +02005552 if (ppconst->pp_used == ppconst_used + 1)
5553 {
5554 // the condition is a constant, we know whether the ? or the :
5555 // expression is to be evaluated.
5556 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005557 if (op_falsy)
5558 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5559 else
5560 {
5561 int error = FALSE;
5562
5563 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5564 &error);
5565 if (error)
5566 return FAIL;
5567 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005568 cctx->ctx_skip = save_skip == SKIP_YES ||
5569 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5570
5571 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5572 // "left ?? right" and "left" is truthy: produce "left"
5573 generate_ppconst(cctx, ppconst);
5574 else
5575 {
5576 clear_tv(&ppconst->pp_tv[ppconst_used]);
5577 --ppconst->pp_used;
5578 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005579 }
5580 else
5581 {
5582 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005583 if (op_falsy)
5584 end_idx = instr->ga_len;
5585 generate_JUMP(cctx, op_falsy
5586 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5587 if (op_falsy)
5588 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005589 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590
5591 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005592 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005593 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005594 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005595 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005596
Bram Moolenaara5565e42020-05-09 15:44:01 +02005597 if (!has_const_expr)
5598 {
5599 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005600
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005601 if (!op_falsy)
5602 {
5603 // remember the type and drop it
5604 --stack->ga_len;
5605 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005606
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005607 end_idx = instr->ga_len;
5608 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005609
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005610 // jump here from JUMP_IF_FALSE
5611 isn = ((isn_T *)instr->ga_data) + alt_idx;
5612 isn->isn_arg.jump.jump_where = instr->ga_len;
5613 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005615
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005616 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005617 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005618 // Check for the ":".
5619 p = may_peek_next_line(cctx, *arg, &next);
5620 if (*p != ':')
5621 {
5622 emsg(_(e_missing_colon));
5623 return FAIL;
5624 }
5625 if (next != NULL)
5626 {
5627 *arg = next_line_from_context(cctx, TRUE);
5628 p = skipwhite(*arg);
5629 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005630
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005631 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5632 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005633 semsg(_(e_white_space_required_before_and_after_str_at_str),
5634 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005635 return FAIL;
5636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005637
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005638 // evaluate the third expression
5639 if (has_const_expr)
5640 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005641 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005642 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005643 return FAIL;
5644 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5645 return FAIL;
5646 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647
Bram Moolenaara5565e42020-05-09 15:44:01 +02005648 if (!has_const_expr)
5649 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005650 type_T **typep;
5651
Bram Moolenaara5565e42020-05-09 15:44:01 +02005652 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005653
Bram Moolenaara5565e42020-05-09 15:44:01 +02005654 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005655 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5656 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005657
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005658 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005659 isn = ((isn_T *)instr->ga_data) + end_idx;
5660 isn->isn_arg.jump.jump_where = instr->ga_len;
5661 }
5662
5663 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005664 }
5665 return OK;
5666}
5667
5668/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005669 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005670 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5671 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005672 */
5673 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005674compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005675{
5676 ppconst_T ppconst;
5677
5678 CLEAR_FIELD(ppconst);
5679 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5680 {
5681 clear_ppconst(&ppconst);
5682 return FAIL;
5683 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005684 if (is_const != NULL)
5685 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005686 if (generate_ppconst(cctx, &ppconst) == FAIL)
5687 return FAIL;
5688 return OK;
5689}
5690
5691/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005692 * Toplevel expression.
5693 */
5694 static int
5695compile_expr0(char_u **arg, cctx_T *cctx)
5696{
5697 return compile_expr0_ext(arg, cctx, NULL);
5698}
5699
5700/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005701 * Compile "return [expr]".
5702 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005703 */
5704 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005705compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005706{
5707 char_u *p = arg;
5708 garray_T *stack = &cctx->ctx_type_stack;
5709 type_T *stack_type;
5710
5711 if (*p != NUL && *p != '|' && *p != '\n')
5712 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005713 if (legacy)
5714 {
5715 int save_flags = cmdmod.cmod_flags;
5716
5717 generate_LEGACY_EVAL(cctx, p);
5718 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5719 0, cctx, FALSE, FALSE) == FAIL)
5720 return NULL;
5721 cmdmod.cmod_flags |= CMOD_LEGACY;
5722 (void)skip_expr(&p, NULL);
5723 cmdmod.cmod_flags = save_flags;
5724 }
5725 else
5726 {
5727 // compile return argument into instructions
5728 if (compile_expr0(&p, cctx) == FAIL)
5729 return NULL;
5730 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005731
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005732 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005733 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005734 // "check_return_type" with uf_ret_type set to &t_unknown is used
5735 // for an inline function without a specified return type. Set the
5736 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005737 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005738 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005739 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5740 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005741 || (!check_return_type
5742 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005743 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005744 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005745 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005746 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005747 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005748 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5749 && stack_type->tt_type != VAR_VOID
5750 && stack_type->tt_type != VAR_UNKNOWN)
5751 {
5752 emsg(_(e_returning_value_in_function_without_return_type));
5753 return NULL;
5754 }
5755 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005756 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005757 return NULL;
5758 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005760 }
5761 else
5762 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005763 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005764 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005765 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5766 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005767 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005768 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005769 return NULL;
5770 }
5771
5772 // No argument, return zero.
5773 generate_PUSHNR(cctx, 0);
5774 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005775
5776 // Undo any command modifiers.
5777 generate_undo_cmdmods(cctx);
5778
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005779 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005780 return NULL;
5781
5782 // "return val | endif" is possible
5783 return skipwhite(p);
5784}
5785
5786/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005787 * Get a line from the compilation context, compatible with exarg_T getline().
5788 * Return a pointer to the line in allocated memory.
5789 * Return NULL for end-of-file or some error.
5790 */
5791 static char_u *
5792exarg_getline(
5793 int c UNUSED,
5794 void *cookie,
5795 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005796 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005797{
5798 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005799 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005800
Bram Moolenaar66250c92020-08-20 15:02:42 +02005801 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005802 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005803 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005804 return NULL;
5805 ++cctx->ctx_lnum;
5806 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5807 // Comment lines result in NULL pointers, skip them.
5808 if (p != NULL)
5809 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005810 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005811}
5812
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005813 void
5814fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5815{
5816 eap->getline = exarg_getline;
5817 eap->cookie = cctx;
5818}
5819
Bram Moolenaar04b12692020-05-04 23:24:44 +02005820/*
5821 * Compile a nested :def command.
5822 */
5823 static char_u *
5824compile_nested_function(exarg_T *eap, cctx_T *cctx)
5825{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005826 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005827 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005828 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005829 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005830 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005831 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005832 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005833
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005834 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005835 {
5836 emsg(_(e_cannot_use_bang_with_nested_def));
5837 return NULL;
5838 }
5839
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005840 if (*name_start == '/')
5841 {
5842 name_end = skip_regexp(name_start + 1, '/', TRUE);
5843 if (*name_end == '/')
5844 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005845 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005846 }
5847 if (name_end == name_start || *skipwhite(name_end) != '(')
5848 {
5849 if (!ends_excmd2(name_start, name_end))
5850 {
5851 semsg(_(e_invalid_command_str), eap->cmd);
5852 return NULL;
5853 }
5854
5855 // "def" or "def Name": list functions
5856 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5857 return NULL;
5858 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5859 }
5860
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005861 // Only g:Func() can use a namespace.
5862 if (name_start[1] == ':' && !is_global)
5863 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005864 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005865 return NULL;
5866 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005867 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005868 return NULL;
5869
Bram Moolenaar04b12692020-05-04 23:24:44 +02005870 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005871 fill_exarg_from_cctx(eap, cctx);
5872
Bram Moolenaar04b12692020-05-04 23:24:44 +02005873 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00005874 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005875 lambda_name = vim_strsave(get_lambda_name());
5876 if (lambda_name == NULL)
5877 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005878 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005879
Bram Moolenaar822ba242020-05-24 23:00:18 +02005880 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005881 {
5882 r = eap->skip ? OK : FAIL;
5883 goto theend;
5884 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005885
5886 // copy over the block scope IDs before compiling
5887 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5888 {
5889 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5890
5891 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5892 if (ufunc->uf_block_ids != NULL)
5893 {
5894 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5895 sizeof(int) * block_depth);
5896 ufunc->uf_block_depth = block_depth;
5897 }
5898 }
5899
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005900 compile_type = COMPILE_TYPE(ufunc);
5901#ifdef FEAT_PROFILE
5902 // If the outer function is profiled, also compile the nested function for
5903 // profiling.
5904 if (cctx->ctx_compile_type == CT_PROFILE)
5905 compile_type = CT_PROFILE;
5906#endif
5907 if (func_needs_compiling(ufunc, compile_type)
5908 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005909 {
5910 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005911 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005912 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005913
Bram Moolenaar648594e2021-07-11 17:55:01 +02005914#ifdef FEAT_PROFILE
5915 // When the outer function is compiled for profiling, the nested function
5916 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005917 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005918 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5919#endif
5920
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005921 if (is_global)
5922 {
5923 char_u *func_name = vim_strnsave(name_start + 2,
5924 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005925
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005926 if (func_name == NULL)
5927 r = FAIL;
5928 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005929 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005930 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005931 lambda_name = NULL;
5932 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005933 }
5934 else
5935 {
5936 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005937 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005938 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005939
Bram Moolenaareef21022020-08-01 22:16:43 +02005940 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005941 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005942 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005943 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005944 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5945 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005946
5947theend:
5948 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005949 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005950}
5951
5952/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953 * Return the length of an assignment operator, or zero if there isn't one.
5954 */
5955 int
5956assignment_len(char_u *p, int *heredoc)
5957{
5958 if (*p == '=')
5959 {
5960 if (p[1] == '<' && p[2] == '<')
5961 {
5962 *heredoc = TRUE;
5963 return 3;
5964 }
5965 return 1;
5966 }
5967 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5968 return 2;
5969 if (STRNCMP(p, "..=", 3) == 0)
5970 return 3;
5971 return 0;
5972}
5973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005975 * Generate the load instruction for "name".
5976 */
5977 static void
5978generate_loadvar(
5979 cctx_T *cctx,
5980 assign_dest_T dest,
5981 char_u *name,
5982 lvar_T *lvar,
5983 type_T *type)
5984{
5985 switch (dest)
5986 {
5987 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005988 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005989 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5990 break;
5991 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005992 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5993 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5994 else
5995 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005996 break;
5997 case dest_buffer:
5998 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5999 break;
6000 case dest_window:
6001 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
6002 break;
6003 case dest_tab:
6004 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
6005 break;
6006 case dest_script:
6007 compile_load_scriptvar(cctx,
6008 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
6009 break;
6010 case dest_env:
6011 // Include $ in the name here
6012 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
6013 break;
6014 case dest_reg:
6015 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
6016 break;
6017 case dest_vimvar:
6018 generate_LOADV(cctx, name + 2, TRUE);
6019 break;
6020 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01006021 if (lvar->lv_from_outer > 0)
6022 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
6023 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006024 else
6025 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
6026 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006027 case dest_expr:
6028 // list or dict value should already be on the stack.
6029 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006030 }
6031}
6032
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006033/*
6034 * Skip over "[expr]" or ".member".
6035 * Does not check for any errors.
6036 */
6037 static char_u *
6038skip_index(char_u *start)
6039{
6040 char_u *p = start;
6041
6042 if (*p == '[')
6043 {
6044 p = skipwhite(p + 1);
6045 (void)skip_expr(&p, NULL);
6046 p = skipwhite(p);
6047 if (*p == ']')
6048 return p + 1;
6049 return p;
6050 }
6051 // if (*p == '.')
6052 return to_name_end(p + 1, TRUE);
6053}
6054
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006055 void
6056vim9_declare_error(char_u *name)
6057{
6058 char *scope = "";
6059
6060 switch (*name)
6061 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006062 case 'g': scope = _("global"); break;
6063 case 'b': scope = _("buffer"); break;
6064 case 'w': scope = _("window"); break;
6065 case 't': scope = _("tab"); break;
6066 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006067 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006068 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006069 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006070 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006071 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006072 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006073 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006074 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006075 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006076}
6077
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006078/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006079 * For one assignment figure out the type of destination. Return it in "dest".
6080 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006081 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006082 * For a v:var "vimvaridx" is set.
6083 * "type" is set to the destination type if known, unchanted otherwise.
6084 * Return FAIL if an error message was given.
6085 */
6086 static int
6087get_var_dest(
6088 char_u *name,
6089 assign_dest_T *dest,
6090 int cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006091 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006092 int *vimvaridx,
6093 type_T **type,
6094 cctx_T *cctx)
6095{
6096 char_u *p;
6097
6098 if (*name == '&')
6099 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006100 int cc;
6101 long numval;
6102 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006103 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006104
6105 *dest = dest_option;
6106 if (cmdidx == CMD_final || cmdidx == CMD_const)
6107 {
6108 emsg(_(e_const_option));
6109 return FAIL;
6110 }
6111 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006112 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006113 if (p == NULL)
6114 {
6115 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02006116 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006117 return FAIL;
6118 }
6119 cc = *p;
6120 *p = NUL;
6121 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006122 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006123 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006124 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006125 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006126 case gov_unknown:
6127 semsg(_(e_unknown_option), name);
6128 return FAIL;
6129 case gov_string:
6130 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006131 if (opt_p_flags & P_FUNC)
6132 {
6133 // might be a Funcref, check the type later
6134 *type = &t_any;
6135 *dest = dest_func_option;
6136 }
6137 else
6138 {
6139 *type = &t_string;
6140 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006141 break;
6142 case gov_bool:
6143 case gov_hidden_bool:
6144 *type = &t_bool;
6145 break;
6146 case gov_number:
6147 case gov_hidden_number:
6148 *type = &t_number;
6149 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006150 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006151 }
6152 else if (*name == '$')
6153 {
6154 *dest = dest_env;
6155 *type = &t_string;
6156 }
6157 else if (*name == '@')
6158 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006159 if (name[1] != '@'
6160 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006161 {
6162 emsg_invreg(name[1]);
6163 return FAIL;
6164 }
6165 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006166 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006167 }
6168 else if (STRNCMP(name, "g:", 2) == 0)
6169 {
6170 *dest = dest_global;
6171 }
6172 else if (STRNCMP(name, "b:", 2) == 0)
6173 {
6174 *dest = dest_buffer;
6175 }
6176 else if (STRNCMP(name, "w:", 2) == 0)
6177 {
6178 *dest = dest_window;
6179 }
6180 else if (STRNCMP(name, "t:", 2) == 0)
6181 {
6182 *dest = dest_tab;
6183 }
6184 else if (STRNCMP(name, "v:", 2) == 0)
6185 {
6186 typval_T *vtv;
6187 int di_flags;
6188
6189 *vimvaridx = find_vim_var(name + 2, &di_flags);
6190 if (*vimvaridx < 0)
6191 {
6192 semsg(_(e_variable_not_found_str), name);
6193 return FAIL;
6194 }
6195 // We use the current value of "sandbox" here, is that OK?
6196 if (var_check_ro(di_flags, name, FALSE))
6197 return FAIL;
6198 *dest = dest_vimvar;
6199 vtv = get_vim_var_tv(*vimvaridx);
6200 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6201 }
6202 return OK;
6203}
6204
6205/*
6206 * Generate a STORE instruction for "dest", not being "dest_local".
6207 * Return FAIL when out of memory.
6208 */
6209 static int
6210generate_store_var(
6211 cctx_T *cctx,
6212 assign_dest_T dest,
6213 int opt_flags,
6214 int vimvaridx,
6215 int scriptvar_idx,
6216 int scriptvar_sid,
6217 type_T *type,
6218 char_u *name)
6219{
6220 switch (dest)
6221 {
6222 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006223 return generate_STOREOPT(cctx, ISN_STOREOPT,
6224 skip_option_env_lead(name), opt_flags);
6225 case dest_func_option:
6226 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
6227 skip_option_env_lead(name), opt_flags);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006228 case dest_global:
6229 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006230 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6231 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006232 case dest_buffer:
6233 // include b: with the name, easier to execute that way
6234 return generate_STORE(cctx, ISN_STOREB, 0, name);
6235 case dest_window:
6236 // include w: with the name, easier to execute that way
6237 return generate_STORE(cctx, ISN_STOREW, 0, name);
6238 case dest_tab:
6239 // include t: with the name, easier to execute that way
6240 return generate_STORE(cctx, ISN_STORET, 0, name);
6241 case dest_env:
6242 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6243 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006244 return generate_STORE(cctx, ISN_STOREREG,
6245 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006246 case dest_vimvar:
6247 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6248 case dest_script:
6249 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006250 // "s:" may be included in the name.
6251 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006252 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006253 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6254 scriptvar_sid, scriptvar_idx, type);
6255 case dest_local:
6256 case dest_expr:
6257 // cannot happen
6258 break;
6259 }
6260 return FAIL;
6261}
6262
Bram Moolenaar752fc692021-01-04 21:57:11 +01006263 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006264generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6265{
6266 if (lhs->lhs_dest != dest_local)
6267 return generate_store_var(cctx, lhs->lhs_dest,
6268 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6269 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6270 lhs->lhs_type, lhs->lhs_name);
6271
6272 if (lhs->lhs_lvar != NULL)
6273 {
6274 garray_T *instr = &cctx->ctx_instr;
6275 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6276
6277 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6278 // ISN_STORENR
6279 if (lhs->lhs_lvar->lv_from_outer == 0
6280 && instr->ga_len == instr_count + 1
6281 && isn->isn_type == ISN_PUSHNR)
6282 {
6283 varnumber_T val = isn->isn_arg.number;
6284 garray_T *stack = &cctx->ctx_type_stack;
6285
6286 isn->isn_type = ISN_STORENR;
6287 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6288 isn->isn_arg.storenr.stnr_val = val;
6289 if (stack->ga_len > 0)
6290 --stack->ga_len;
6291 }
6292 else if (lhs->lhs_lvar->lv_from_outer > 0)
6293 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6294 lhs->lhs_lvar->lv_from_outer);
6295 else
6296 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6297 }
6298 return OK;
6299}
6300
6301 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006302is_decl_command(int cmdidx)
6303{
6304 return cmdidx == CMD_let || cmdidx == CMD_var
6305 || cmdidx == CMD_final || cmdidx == CMD_const;
6306}
6307
6308/*
6309 * Figure out the LHS type and other properties for an assignment or one item
6310 * of ":unlet" with an index.
6311 * Returns OK or FAIL.
6312 */
6313 static int
6314compile_lhs(
6315 char_u *var_start,
6316 lhs_T *lhs,
6317 int cmdidx,
6318 int heredoc,
6319 int oplen,
6320 cctx_T *cctx)
6321{
6322 char_u *var_end;
6323 int is_decl = is_decl_command(cmdidx);
6324
6325 CLEAR_POINTER(lhs);
6326 lhs->lhs_dest = dest_local;
6327 lhs->lhs_vimvaridx = -1;
6328 lhs->lhs_scriptvar_idx = -1;
6329
6330 // "dest_end" is the end of the destination, including "[expr]" or
6331 // ".name".
6332 // "var_end" is the end of the variable/option/etc. name.
6333 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6334 if (*var_start == '@')
6335 var_end = var_start + 2;
6336 else
6337 {
6338 // skip over the leading "&", "&l:", "&g:" and "$"
6339 var_end = skip_option_env_lead(var_start);
6340 var_end = to_name_end(var_end, TRUE);
6341 }
6342
6343 // "a: type" is declaring variable "a" with a type, not dict "a:".
6344 if (is_decl && lhs->lhs_dest_end == var_start + 2
6345 && lhs->lhs_dest_end[-1] == ':')
6346 --lhs->lhs_dest_end;
6347 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6348 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006349 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006350
6351 // compute the length of the destination without "[expr]" or ".name"
6352 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006353 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006354 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6355 if (lhs->lhs_name == NULL)
6356 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006357
6358 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6359 // Something follows after the variable: "var[idx]" or "var.key".
6360 lhs->lhs_has_index = TRUE;
6361
Bram Moolenaar752fc692021-01-04 21:57:11 +01006362 if (heredoc)
6363 lhs->lhs_type = &t_list_string;
6364 else
6365 lhs->lhs_type = &t_any;
6366
6367 if (cctx->ctx_skip != SKIP_YES)
6368 {
6369 int declare_error = FALSE;
6370
6371 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6372 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6373 &lhs->lhs_type, cctx) == FAIL)
6374 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006375 if (lhs->lhs_dest != dest_local
6376 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006377 {
6378 // Specific kind of variable recognized.
6379 declare_error = is_decl;
6380 }
6381 else
6382 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006383 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006384 if (check_reserved_name(lhs->lhs_name) == FAIL)
6385 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006386
6387 if (lookup_local(var_start, lhs->lhs_varlen,
6388 &lhs->lhs_local_lvar, cctx) == OK)
6389 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6390 else
6391 {
6392 CLEAR_FIELD(lhs->lhs_arg_lvar);
6393 if (arg_exists(var_start, lhs->lhs_varlen,
6394 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6395 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6396 {
6397 if (is_decl)
6398 {
6399 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6400 return FAIL;
6401 }
6402 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6403 }
6404 }
6405 if (lhs->lhs_lvar != NULL)
6406 {
6407 if (is_decl)
6408 {
6409 semsg(_(e_variable_already_declared), lhs->lhs_name);
6410 return FAIL;
6411 }
6412 }
6413 else
6414 {
6415 int script_namespace = lhs->lhs_varlen > 1
6416 && STRNCMP(var_start, "s:", 2) == 0;
6417 int script_var = (script_namespace
6418 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006419 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006420 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006421 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006422 imported_T *import =
6423 find_imported(var_start, lhs->lhs_varlen, cctx);
6424
6425 if (script_namespace || script_var || import != NULL)
6426 {
6427 char_u *rawname = lhs->lhs_name
6428 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6429
6430 if (is_decl)
6431 {
6432 if (script_namespace)
6433 semsg(_(e_cannot_declare_script_variable_in_function),
6434 lhs->lhs_name);
6435 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006436 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006437 lhs->lhs_name);
6438 return FAIL;
6439 }
6440 else if (cctx->ctx_ufunc->uf_script_ctx_version
6441 == SCRIPT_VERSION_VIM9
6442 && script_namespace
6443 && !script_var && import == NULL)
6444 {
6445 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6446 return FAIL;
6447 }
6448
6449 lhs->lhs_dest = dest_script;
6450
6451 // existing script-local variables should have a type
6452 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6453 if (import != NULL)
6454 lhs->lhs_scriptvar_sid = import->imp_sid;
6455 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6456 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006457 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006458 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006459 lhs->lhs_scriptvar_sid, rawname,
6460 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6461 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006462 if (lhs->lhs_scriptvar_idx >= 0)
6463 {
6464 scriptitem_T *si = SCRIPT_ITEM(
6465 lhs->lhs_scriptvar_sid);
6466 svar_T *sv =
6467 ((svar_T *)si->sn_var_vals.ga_data)
6468 + lhs->lhs_scriptvar_idx;
6469 lhs->lhs_type = sv->sv_type;
6470 }
6471 }
6472 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006473 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006474 == FAIL)
6475 return FAIL;
6476 }
6477 }
6478
6479 if (declare_error)
6480 {
6481 vim9_declare_error(lhs->lhs_name);
6482 return FAIL;
6483 }
6484 }
6485
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006486 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01006487 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6488 var_end = lhs->lhs_dest_end;
6489
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006490 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006491 {
6492 if (is_decl && *var_end == ':')
6493 {
6494 char_u *p;
6495
6496 // parse optional type: "let var: type = expr"
6497 if (!VIM_ISWHITE(var_end[1]))
6498 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006499 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006500 return FAIL;
6501 }
6502 p = skipwhite(var_end + 1);
6503 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6504 if (lhs->lhs_type == NULL)
6505 return FAIL;
6506 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006507 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006508 }
6509 else if (lhs->lhs_lvar != NULL)
6510 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6511 }
6512
Bram Moolenaare42939a2021-04-05 17:11:17 +02006513 if (oplen == 3 && !heredoc
6514 && lhs->lhs_dest != dest_global
6515 && !lhs->lhs_has_index
6516 && lhs->lhs_type->tt_type != VAR_STRING
6517 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006518 {
6519 emsg(_(e_can_only_concatenate_to_string));
6520 return FAIL;
6521 }
6522
6523 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6524 && cctx->ctx_skip != SKIP_YES)
6525 {
6526 if (oplen > 1 && !heredoc)
6527 {
6528 // +=, /=, etc. require an existing variable
6529 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6530 return FAIL;
6531 }
6532 if (!is_decl)
6533 {
6534 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6535 return FAIL;
6536 }
6537
Bram Moolenaar3f327882021-03-17 20:56:38 +01006538 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006539 if ((lhs->lhs_type->tt_type == VAR_FUNC
6540 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006541 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006542 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006543
6544 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006545 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6546 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6547 if (lhs->lhs_lvar == NULL)
6548 return FAIL;
6549 lhs->lhs_new_local = TRUE;
6550 }
6551
6552 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006553 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006554 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006555 char_u *after = var_start + lhs->lhs_varlen;
6556 char_u *p;
6557
Bram Moolenaar752fc692021-01-04 21:57:11 +01006558 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006559 if (is_decl)
6560 {
6561 emsg(_(e_cannot_use_index_when_declaring_variable));
6562 return FAIL;
6563 }
6564
Bram Moolenaarc750d912021-11-29 22:02:12 +00006565 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
6566 // Only the last index is used below, if there are others
6567 // before it generate code for the expression. Thus for
6568 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6569 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006570 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006571 p = skip_index(after);
6572 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01006573 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006574 lhs->lhs_varlen_total = p - var_start;
6575 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006576 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006577 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006578 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006579 if (after > var_start + lhs->lhs_varlen)
6580 {
6581 lhs->lhs_varlen = after - var_start;
6582 lhs->lhs_dest = dest_expr;
6583 // We don't know the type before evaluating the expression,
6584 // use "any" until then.
6585 lhs->lhs_type = &t_any;
6586 }
6587
6588 if (lhs->lhs_type->tt_member == NULL)
6589 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006590 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00006591 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006592 }
6593 return OK;
6594}
6595
6596/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006597 * Figure out the LHS and check a few errors.
6598 */
6599 static int
6600compile_assign_lhs(
6601 char_u *var_start,
6602 lhs_T *lhs,
6603 int cmdidx,
6604 int is_decl,
6605 int heredoc,
6606 int oplen,
6607 cctx_T *cctx)
6608{
6609 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6610 return FAIL;
6611
6612 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6613 {
6614 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6615 return FAIL;
6616 }
6617 if (!is_decl && lhs->lhs_lvar != NULL
6618 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6619 {
6620 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6621 return FAIL;
6622 }
6623 return OK;
6624}
6625
6626/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006627 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6628 */
6629 static int
6630has_list_index(char_u *idx_start, cctx_T *cctx)
6631{
6632 char_u *p = idx_start;
6633 int save_skip;
6634
6635 if (*p != '[')
6636 return FALSE;
6637
6638 p = skipwhite(p + 1);
6639 if (*p == ':')
6640 return TRUE;
6641
6642 save_skip = cctx->ctx_skip;
6643 cctx->ctx_skip = SKIP_YES;
6644 (void)compile_expr0(&p, cctx);
6645 cctx->ctx_skip = save_skip;
6646 return *skipwhite(p) == ':';
6647}
6648
6649/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006650 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6651 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006652 */
6653 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006654compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006655 char_u *var_start,
6656 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006657 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006658 cctx_T *cctx)
6659{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006660 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006661 char_u *p;
6662 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006663 int need_white_before = TRUE;
6664 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006665
Bram Moolenaar752fc692021-01-04 21:57:11 +01006666 p = var_start + varlen;
6667 if (*p == '[')
6668 {
6669 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006670 if (*p == ':')
6671 {
6672 // empty first index, push zero
6673 r = generate_PUSHNR(cctx, 0);
6674 need_white_before = FALSE;
6675 }
6676 else
6677 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006678
6679 if (r == OK && *skipwhite(p) == ':')
6680 {
6681 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006682 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006683 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006684 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006685 empty_second = *skipwhite(p + 1) == ']';
6686 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6687 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006688 {
6689 semsg(_(e_white_space_required_before_and_after_str_at_str),
6690 ":", p);
6691 return FAIL;
6692 }
6693 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006694 if (*p == ']')
6695 // empty second index, push "none"
6696 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6697 else
6698 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006699 }
6700
Bram Moolenaar752fc692021-01-04 21:57:11 +01006701 if (r == OK && *skipwhite(p) != ']')
6702 {
6703 // this should not happen
6704 emsg(_(e_missbrac));
6705 r = FAIL;
6706 }
6707 }
6708 else // if (*p == '.')
6709 {
6710 char_u *key_end = to_name_end(p + 1, TRUE);
6711 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6712
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006713 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006714 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006715 return r;
6716}
6717
6718/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006719 * For a LHS with an index, load the variable to be indexed.
6720 */
6721 static int
6722compile_load_lhs(
6723 lhs_T *lhs,
6724 char_u *var_start,
6725 type_T *rhs_type,
6726 cctx_T *cctx)
6727{
6728 if (lhs->lhs_dest == dest_expr)
6729 {
6730 size_t varlen = lhs->lhs_varlen;
6731 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006732 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006733 char_u *p = var_start;
6734 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006735 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006736
Bram Moolenaare97976b2021-08-01 13:17:17 +02006737 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6738 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006739 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006740 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6741 res = compile_expr0(&p, cctx);
6742 var_start[varlen] = c;
6743 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6744 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006745 {
6746 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006747 if (res != FAIL)
6748 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006749 return FAIL;
6750 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006751
6752 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6753 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6754 // now we can properly check the type
6755 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6756 && rhs_type != &t_void
6757 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6758 FALSE, FALSE) == FAIL)
6759 return FAIL;
6760 }
6761 else
6762 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6763 lhs->lhs_lvar, lhs->lhs_type);
6764 return OK;
6765}
6766
6767/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006768 * Produce code for loading "lhs" and also take care of an index.
6769 * Return OK/FAIL.
6770 */
6771 static int
6772compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6773{
6774 compile_load_lhs(lhs, var_start, NULL, cctx);
6775
6776 if (lhs->lhs_has_index)
6777 {
6778 int range = FALSE;
6779
6780 // Get member from list or dict. First compile the
6781 // index value.
6782 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6783 return FAIL;
6784 if (range)
6785 {
6786 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6787 var_start);
6788 return FAIL;
6789 }
6790
6791 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006792 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006793 return FAIL;
6794 }
6795 return OK;
6796}
6797
6798/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006799 * Assignment to a list or dict member, or ":unlet" for the item, using the
6800 * information in "lhs".
6801 * Returns OK or FAIL.
6802 */
6803 static int
6804compile_assign_unlet(
6805 char_u *var_start,
6806 lhs_T *lhs,
6807 int is_assign,
6808 type_T *rhs_type,
6809 cctx_T *cctx)
6810{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006811 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006812 garray_T *stack = &cctx->ctx_type_stack;
6813 int range = FALSE;
6814
Bram Moolenaar68452172021-04-12 21:21:02 +02006815 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006816 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006817 if (is_assign && range
6818 && lhs->lhs_type->tt_type != VAR_LIST
6819 && lhs->lhs_type != &t_blob
6820 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006821 {
6822 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6823 return FAIL;
6824 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006825
6826 if (lhs->lhs_type == &t_any)
6827 {
6828 // Index on variable of unknown type: check at runtime.
6829 dest_type = VAR_ANY;
6830 }
6831 else
6832 {
6833 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006834 if (dest_type == VAR_DICT && range)
6835 {
6836 emsg(e_cannot_use_range_with_dictionary);
6837 return FAIL;
6838 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006839 if (dest_type == VAR_DICT
6840 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006841 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006842 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006843 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006844 type_T *type;
6845
6846 if (range)
6847 {
6848 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6849 if (need_type(type, &t_number,
6850 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006851 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006852 }
6853 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006854 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006855 && need_type(type, &t_number,
6856 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006857 return FAIL;
6858 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006859 }
6860
6861 // Load the dict or list. On the stack we then have:
6862 // - value (for assignment, not for :unlet)
6863 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006864 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006865 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006866 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6867 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006868
Bram Moolenaar68452172021-04-12 21:21:02 +02006869 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6870 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006871 {
6872 if (is_assign)
6873 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006874 if (range)
6875 {
6876 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6877 return FAIL;
6878 }
6879 else
6880 {
6881 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006882
Bram Moolenaar68452172021-04-12 21:21:02 +02006883 if (isn == NULL)
6884 return FAIL;
6885 isn->isn_arg.vartype = dest_type;
6886 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006887 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006888 else if (range)
6889 {
6890 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6891 return FAIL;
6892 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006893 else
6894 {
6895 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6896 return FAIL;
6897 }
6898 }
6899 else
6900 {
6901 emsg(_(e_indexable_type_required));
6902 return FAIL;
6903 }
6904
6905 return OK;
6906}
6907
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006908/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006909 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006910 * "let name"
6911 * "var name = expr"
6912 * "final name = expr"
6913 * "const name = expr"
6914 * "name = expr"
6915 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006916 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006917 * Return NULL for an error.
6918 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919 */
6920 static char_u *
6921compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6922{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006923 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006925 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006926 char_u *ret = NULL;
6927 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006928 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006930 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006932 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 int oplen = 0;
6935 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006936 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006937 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006939 int is_decl = is_decl_command(cmdidx);
6940 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006941 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006943 // Skip over the "var" or "[var, var]" to get to any "=".
6944 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6945 if (p == NULL)
6946 return *arg == '[' ? arg : NULL;
6947
Bram Moolenaar752fc692021-01-04 21:57:11 +01006948 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 sp = p;
6951 p = skipwhite(p);
6952 op = p;
6953 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006954
6955 if (var_count > 0 && oplen == 0)
6956 // can be something like "[1, 2]->func()"
6957 return arg;
6958
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006959 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006960 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006961 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006962 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006963 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006964 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6965 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006966 if (VIM_ISWHITE(eap->cmd[2]))
6967 {
6968 semsg(_(e_no_white_space_allowed_after_str_str),
6969 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6970 return NULL;
6971 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006972 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6973 oplen = 2;
6974 incdec = TRUE;
6975 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 if (heredoc)
6978 {
6979 list_T *l;
6980 listitem_T *li;
6981
6982 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006983 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006985 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006986 if (l == NULL)
6987 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988
Bram Moolenaar078269b2020-09-21 20:35:55 +02006989 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006991 // Push each line and the create the list.
6992 FOR_ALL_LIST_ITEMS(l, li)
6993 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006994 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02006995 li->li_tv.vval.v_string = NULL;
6996 }
6997 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006998 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 list_free(l);
7000 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007001 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007003 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007004 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007005 char_u *wp;
7006
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007007 // for "[var, var] = expr" evaluate the expression here, loop over the
7008 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007009 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02007010
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007011 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01007012 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007013 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007014 if (compile_expr0(&p, cctx) == FAIL)
7015 return NULL;
7016 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007018 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007020 type_T *stacktype;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007021 int needed_list_len;
7022 int did_check = FALSE;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007023
Bram Moolenaarec5929d2020-04-07 20:53:39 +02007024 stacktype = stack->ga_len == 0 ? &t_void
7025 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007026 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007028 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007029 goto theend;
7030 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01007031 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007032 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007033 goto theend;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007034 // If a constant list was used we can check the length right here.
7035 needed_list_len = semicolon ? var_count - 1 : var_count;
7036 if (instr->ga_len > 0)
7037 {
7038 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
7039
7040 if (isn->isn_type == ISN_NEWLIST)
7041 {
7042 did_check = TRUE;
7043 if (semicolon ? isn->isn_arg.number < needed_list_len
7044 : isn->isn_arg.number != needed_list_len)
7045 {
7046 semsg(_(e_expected_nr_items_but_got_nr),
7047 needed_list_len, isn->isn_arg.number);
7048 goto theend;
7049 }
7050 }
7051 }
7052 if (!did_check)
7053 generate_CHECKLEN(cctx, needed_list_len, semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007054 if (stacktype->tt_member != NULL)
7055 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007056 }
7057 }
7058
7059 /*
7060 * Loop over variables in "[var, var] = expr".
7061 * For "var = expr" and "let var: type" this is done only once.
7062 */
7063 if (var_count > 0)
7064 var_start = skipwhite(arg + 1); // skip over the "["
7065 else
7066 var_start = arg;
7067 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
7068 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007069 int instr_count = -1;
7070 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007071
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02007072 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
7073 {
7074 // Ignore underscore in "[a, _, b] = list".
7075 if (var_count > 0)
7076 {
7077 var_start = skipwhite(var_start + 2);
7078 continue;
7079 }
7080 emsg(_(e_cannot_use_underscore_here));
7081 goto theend;
7082 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007083 vim_free(lhs.lhs_name);
7084
7085 /*
7086 * Figure out the LHS type and other properties.
7087 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007088 if (compile_assign_lhs(var_start, &lhs, cmdidx,
7089 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007090 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02007091 if (heredoc)
7092 {
7093 SOURCING_LNUM = start_lnum;
7094 if (lhs.lhs_has_type
7095 && need_type(&t_list_string, lhs.lhs_type,
7096 -1, 0, cctx, FALSE, FALSE) == FAIL)
7097 goto theend;
7098 }
7099 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007100 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007101 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007102 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007103 if (oplen > 0 && var_count == 0)
7104 {
7105 // skip over the "=" and the expression
7106 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02007107 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007108 }
7109 }
7110 else if (oplen > 0)
7111 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007112 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01007113 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007114
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007115 // for "+=", "*=", "..=" etc. first load the current value
7116 if (*op != '='
7117 && compile_load_lhs_with_index(&lhs, var_start,
7118 cctx) == FAIL)
7119 goto theend;
7120
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007121 // For "var = expr" evaluate the expression.
7122 if (var_count == 0)
7123 {
7124 int r;
7125
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007126 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007127 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007128 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007129 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007130 r = generate_PUSHNR(cctx, 1);
7131 }
7132 else
7133 {
7134 // Temporarily hide the new local variable here, it is
7135 // not available to this expression.
7136 if (lhs.lhs_new_local)
7137 --cctx->ctx_locals.ga_len;
7138 wp = op + oplen;
7139 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7140 {
7141 if (lhs.lhs_new_local)
7142 ++cctx->ctx_locals.ga_len;
7143 goto theend;
7144 }
7145 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007146 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007147 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007148 if (r == FAIL)
7149 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007150 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007151 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007152 else if (semicolon && var_idx == var_count - 1)
7153 {
7154 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007155 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007156 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7157 goto theend;
7158 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007159 else
7160 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007161 // For "[var, var] = expr" get the "var_idx" item from the
7162 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007163 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007164 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007165 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007166
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007167 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007168 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007169 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007170 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007171 if ((rhs_type->tt_type == VAR_FUNC
7172 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007173 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007174 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007175 goto theend;
7176
Bram Moolenaar752fc692021-01-04 21:57:11 +01007177 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007178 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007179 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007180 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007181 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007182 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007183 }
7184 else
7185 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007186 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007187 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007188 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007189 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007190 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007191 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007192 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007193 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007194 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007195 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007196 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007197 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007198 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007199 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007200 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007201 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007202
Bram Moolenaar77709b12021-04-03 21:01:01 +02007203 // Without operator check type here, otherwise below.
7204 // Use the line number of the assignment.
7205 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007206 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7207 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007208 // If assigning to a list or dict member, use the
7209 // member type. Not for "list[:] =".
7210 if (lhs.lhs_has_index
7211 && !has_list_index(var_start + lhs.lhs_varlen,
7212 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007213 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007214 if (need_type_where(rhs_type, use_type, -1, where,
7215 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007216 goto theend;
7217 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007218 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007219 else
7220 {
7221 type_T *lhs_type = lhs.lhs_member_type;
7222
7223 // Special case: assigning to @# can use a number or a
7224 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007225 // Also: can assign a number to a float.
7226 if ((lhs_type == &t_number_or_string
7227 || lhs_type == &t_float)
7228 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007229 lhs_type = &t_number;
7230 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007231 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007232 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007235 else if (cmdidx == CMD_final)
7236 {
7237 emsg(_(e_final_requires_a_value));
7238 goto theend;
7239 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007240 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007242 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007243 goto theend;
7244 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00007245 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option
7246 || lhs.lhs_dest == dest_func_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007247 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007248 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007249 goto theend;
7250 }
7251 else
7252 {
7253 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007254 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007255 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007256 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007257 {
7258 case VAR_BOOL:
7259 generate_PUSHBOOL(cctx, VVAL_FALSE);
7260 break;
7261 case VAR_FLOAT:
7262#ifdef FEAT_FLOAT
7263 generate_PUSHF(cctx, 0.0);
7264#endif
7265 break;
7266 case VAR_STRING:
7267 generate_PUSHS(cctx, NULL);
7268 break;
7269 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007270 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007271 break;
7272 case VAR_FUNC:
7273 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7274 break;
7275 case VAR_LIST:
7276 generate_NEWLIST(cctx, 0);
7277 break;
7278 case VAR_DICT:
7279 generate_NEWDICT(cctx, 0);
7280 break;
7281 case VAR_JOB:
7282 generate_PUSHJOB(cctx, NULL);
7283 break;
7284 case VAR_CHANNEL:
7285 generate_PUSHCHANNEL(cctx, NULL);
7286 break;
7287 case VAR_NUMBER:
7288 case VAR_UNKNOWN:
7289 case VAR_ANY:
7290 case VAR_PARTIAL:
7291 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007292 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007293 case VAR_SPECIAL: // cannot happen
7294 generate_PUSHNR(cctx, 0);
7295 break;
7296 }
7297 }
7298 if (var_count == 0)
7299 end = p;
7300 }
7301
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007302 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007303 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007304 break;
7305
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007306 if (oplen > 0 && *op != '=')
7307 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007308 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007309 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007310
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007311 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007312 {
7313 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7314 goto theend;
7315 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007316 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007317 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007318 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007319 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7320 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007321#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007322 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007323 !(expected == &t_float && (stacktype == &t_number
7324 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007325#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007326 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007327 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007328 goto theend;
7329 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007330
7331 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007332 {
7333 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7334 goto theend;
7335 }
7336 else if (*op == '+')
7337 {
7338 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007339 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007340 lhs.lhs_member_type, stacktype,
7341 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007342 goto theend;
7343 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007344 else if (generate_two_op(cctx, op) == FAIL)
7345 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007348 // Use the line number of the assignment for store instruction.
7349 save_lnum = cctx->ctx_lnum;
7350 cctx->ctx_lnum = start_lnum - 1;
7351
Bram Moolenaar752fc692021-01-04 21:57:11 +01007352 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007353 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007354 // Use the info in "lhs" to store the value at the index in the
7355 // list or dict.
7356 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7357 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007358 {
7359 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007360 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007361 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007362 }
7363 else
7364 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007365 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007366 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007367 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007368 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007369 generate_LOCKCONST(cctx);
7370
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007371 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007372 && (lhs.lhs_type->tt_type == VAR_DICT
7373 || lhs.lhs_type->tt_type == VAR_LIST)
7374 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007375 && !(lhs.lhs_type->tt_member == &t_any
7376 && oplen > 0
7377 && rhs_type != NULL
7378 && rhs_type->tt_type == lhs.lhs_type->tt_type
7379 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007380 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007381 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007382 // also in legacy script. Not for "list<any> = val", then the
7383 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007384 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007385
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007386 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007387 {
7388 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007389 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007390 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007391 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007392 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007393
7394 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00007395 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007396 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007397
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007398 // For "[var, var] = expr" drop the "expr" value.
7399 // Also for "[var, var; _] = expr".
7400 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007401 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007402 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007403 goto theend;
7404 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007405
Bram Moolenaarb2097502020-07-19 17:17:02 +02007406 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407
7408theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007409 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 return ret;
7411}
7412
7413/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007414 * Check for an assignment at "eap->cmd", compile it if found.
7415 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7416 */
7417 static int
7418may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7419{
7420 char_u *pskip;
7421 char_u *p;
7422
7423 // Assuming the command starts with a variable or function name,
7424 // find what follows.
7425 // Skip over "var.member", "var[idx]" and the like.
7426 // Also "&opt = val", "$ENV = val" and "@r = val".
7427 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7428 ? eap->cmd + 1 : eap->cmd;
7429 p = to_name_end(pskip, TRUE);
7430 if (p > eap->cmd && *p != NUL)
7431 {
7432 char_u *var_end;
7433 int oplen;
7434 int heredoc;
7435
7436 if (eap->cmd[0] == '@')
7437 var_end = eap->cmd + 2;
7438 else
7439 var_end = find_name_end(pskip, NULL, NULL,
7440 FNE_CHECK_START | FNE_INCL_BR);
7441 oplen = assignment_len(skipwhite(var_end), &heredoc);
7442 if (oplen > 0)
7443 {
7444 size_t len = p - eap->cmd;
7445
7446 // Recognize an assignment if we recognize the variable
7447 // name:
7448 // "g:var = expr"
7449 // "local = expr" where "local" is a local var.
7450 // "script = expr" where "script" is a script-local var.
7451 // "import = expr" where "import" is an imported var
7452 // "&opt = expr"
7453 // "$ENV = expr"
7454 // "@r = expr"
7455 if (*eap->cmd == '&'
7456 || *eap->cmd == '$'
7457 || *eap->cmd == '@'
7458 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007459 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007460 {
7461 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7462 if (*line == NULL || *line == eap->cmd)
7463 return FAIL;
7464 return OK;
7465 }
7466 }
7467 }
7468
7469 if (*eap->cmd == '[')
7470 {
7471 // [var, var] = expr
7472 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7473 if (*line == NULL)
7474 return FAIL;
7475 if (*line != eap->cmd)
7476 return OK;
7477 }
7478 return NOTDONE;
7479}
7480
7481/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007482 * Check if "name" can be "unlet".
7483 */
7484 int
7485check_vim9_unlet(char_u *name)
7486{
7487 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7488 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007489 // "unlet s:var" is allowed in legacy script.
7490 if (*name == 's' && !script_is_vim9())
7491 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007492 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007493 return FAIL;
7494 }
7495 return OK;
7496}
7497
7498/*
7499 * Callback passed to ex_unletlock().
7500 */
7501 static int
7502compile_unlet(
7503 lval_T *lvp,
7504 char_u *name_end,
7505 exarg_T *eap,
7506 int deep UNUSED,
7507 void *coookie)
7508{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007509 cctx_T *cctx = coookie;
7510 char_u *p = lvp->ll_name;
7511 int cc = *name_end;
7512 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007513
Bram Moolenaar752fc692021-01-04 21:57:11 +01007514 if (cctx->ctx_skip == SKIP_YES)
7515 return OK;
7516
7517 *name_end = NUL;
7518 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007519 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007520 // :unlet $ENV_VAR
7521 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7522 }
7523 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7524 {
7525 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007526
Bram Moolenaar752fc692021-01-04 21:57:11 +01007527 // This is similar to assigning: lookup the list/dict, compile the
7528 // idx/key. Then instead of storing the value unlet the item.
7529 // unlet {list}[idx]
7530 // unlet {dict}[key] dict.key
7531 //
7532 // Figure out the LHS type and other properties.
7533 //
7534 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7535
7536 // : unlet an indexed item
7537 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007538 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007539 iemsg("called compile_lhs() without an index");
7540 ret = FAIL;
7541 }
7542 else
7543 {
7544 // Use the info in "lhs" to unlet the item at the index in the
7545 // list or dict.
7546 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007547 }
7548
Bram Moolenaar752fc692021-01-04 21:57:11 +01007549 vim_free(lhs.lhs_name);
7550 }
7551 else if (check_vim9_unlet(p) == FAIL)
7552 {
7553 ret = FAIL;
7554 }
7555 else
7556 {
7557 // Normal name. Only supports g:, w:, t: and b: namespaces.
7558 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007559 }
7560
Bram Moolenaar752fc692021-01-04 21:57:11 +01007561 *name_end = cc;
7562 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007563}
7564
7565/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007566 * Callback passed to ex_unletlock().
7567 */
7568 static int
7569compile_lock_unlock(
7570 lval_T *lvp,
7571 char_u *name_end,
7572 exarg_T *eap,
7573 int deep UNUSED,
7574 void *coookie)
7575{
7576 cctx_T *cctx = coookie;
7577 int cc = *name_end;
7578 char_u *p = lvp->ll_name;
7579 int ret = OK;
7580 size_t len;
7581 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007582 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007583
7584 if (cctx->ctx_skip == SKIP_YES)
7585 return OK;
7586
7587 // Cannot use :lockvar and :unlockvar on local variables.
7588 if (p[1] != ':')
7589 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007590 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007591
7592 if (lookup_local(p, end - p, NULL, cctx) == OK)
7593 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007594 char_u *s = p;
7595
7596 if (*end != '.' && *end != '[')
7597 {
7598 emsg(_(e_cannot_lock_unlock_local_variable));
7599 return FAIL;
7600 }
7601
7602 // For "d.member" put the local variable on the stack, it will be
7603 // passed to ex_lockvar() indirectly.
7604 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7605 return FAIL;
7606 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007607 }
7608 }
7609
7610 // Checking is done at runtime.
7611 *name_end = NUL;
7612 len = name_end - p + 20;
7613 buf = alloc(len);
7614 if (buf == NULL)
7615 ret = FAIL;
7616 else
7617 {
7618 vim_snprintf((char *)buf, len, "%s %s",
7619 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7620 p);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00007621 ret = generate_EXEC_copy(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007622
7623 vim_free(buf);
7624 *name_end = cc;
7625 }
7626 return ret;
7627}
7628
7629/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007630 * compile "unlet var", "lock var" and "unlock var"
7631 * "arg" points to "var".
7632 */
7633 static char_u *
7634compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7635{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007636 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7637 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7638 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007639 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7640}
7641
7642/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007643 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7644 */
7645 static int
7646compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7647{
7648 garray_T *instr = &cctx->ctx_instr;
7649 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7650
7651 if (endlabel == NULL)
7652 return FAIL;
7653 endlabel->el_next = *el;
7654 *el = endlabel;
7655 endlabel->el_end_label = instr->ga_len;
7656
7657 generate_JUMP(cctx, when, 0);
7658 return OK;
7659}
7660
7661 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007662compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007663{
7664 garray_T *instr = &cctx->ctx_instr;
7665
7666 while (*el != NULL)
7667 {
7668 endlabel_T *cur = (*el);
7669 isn_T *isn;
7670
7671 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007672 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007673 *el = cur->el_next;
7674 vim_free(cur);
7675 }
7676}
7677
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007678 static void
7679compile_free_jump_to_end(endlabel_T **el)
7680{
7681 while (*el != NULL)
7682 {
7683 endlabel_T *cur = (*el);
7684
7685 *el = cur->el_next;
7686 vim_free(cur);
7687 }
7688}
7689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007690/*
7691 * Create a new scope and set up the generic items.
7692 */
7693 static scope_T *
7694new_scope(cctx_T *cctx, scopetype_T type)
7695{
7696 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7697
7698 if (scope == NULL)
7699 return NULL;
7700 scope->se_outer = cctx->ctx_scope;
7701 cctx->ctx_scope = scope;
7702 scope->se_type = type;
7703 scope->se_local_count = cctx->ctx_locals.ga_len;
7704 return scope;
7705}
7706
7707/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007708 * Free the current scope and go back to the outer scope.
7709 */
7710 static void
7711drop_scope(cctx_T *cctx)
7712{
7713 scope_T *scope = cctx->ctx_scope;
7714
7715 if (scope == NULL)
7716 {
7717 iemsg("calling drop_scope() without a scope");
7718 return;
7719 }
7720 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007721 switch (scope->se_type)
7722 {
7723 case IF_SCOPE:
7724 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7725 case FOR_SCOPE:
7726 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7727 case WHILE_SCOPE:
7728 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7729 case TRY_SCOPE:
7730 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7731 case NO_SCOPE:
7732 case BLOCK_SCOPE:
7733 break;
7734 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007735 vim_free(scope);
7736}
7737
7738/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007739 * compile "if expr"
7740 *
7741 * "if expr" Produces instructions:
7742 * EVAL expr Push result of "expr"
7743 * JUMP_IF_FALSE end
7744 * ... body ...
7745 * end:
7746 *
7747 * "if expr | else" Produces instructions:
7748 * EVAL expr Push result of "expr"
7749 * JUMP_IF_FALSE else
7750 * ... body ...
7751 * JUMP_ALWAYS end
7752 * else:
7753 * ... body ...
7754 * end:
7755 *
7756 * "if expr1 | elseif expr2 | else" Produces instructions:
7757 * EVAL expr Push result of "expr"
7758 * JUMP_IF_FALSE elseif
7759 * ... body ...
7760 * JUMP_ALWAYS end
7761 * elseif:
7762 * EVAL expr Push result of "expr"
7763 * JUMP_IF_FALSE else
7764 * ... body ...
7765 * JUMP_ALWAYS end
7766 * else:
7767 * ... body ...
7768 * end:
7769 */
7770 static char_u *
7771compile_if(char_u *arg, cctx_T *cctx)
7772{
7773 char_u *p = arg;
7774 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007775 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007776 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007777 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007778 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779
Bram Moolenaara5565e42020-05-09 15:44:01 +02007780 CLEAR_FIELD(ppconst);
7781 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007782 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007783 clear_ppconst(&ppconst);
7784 return NULL;
7785 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007786 if (!ends_excmd2(arg, skipwhite(p)))
7787 {
7788 semsg(_(e_trailing_arg), p);
7789 return NULL;
7790 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007791 if (cctx->ctx_skip == SKIP_YES)
7792 clear_ppconst(&ppconst);
7793 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007794 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007795 int error = FALSE;
7796 int v;
7797
Bram Moolenaara5565e42020-05-09 15:44:01 +02007798 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007799 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007800 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007801 if (error)
7802 return NULL;
7803 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007804 }
7805 else
7806 {
7807 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007808 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007809 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007810 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007811 if (bool_on_stack(cctx) == FAIL)
7812 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007813 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814
Bram Moolenaara91a7132021-03-25 21:12:15 +01007815 // CMDMOD_REV must come before the jump
7816 generate_undo_cmdmods(cctx);
7817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818 scope = new_scope(cctx, IF_SCOPE);
7819 if (scope == NULL)
7820 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007821 scope->se_skip_save = skip_save;
7822 // "is_had_return" will be reset if any block does not end in :return
7823 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007825 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007826 {
7827 // "where" is set when ":elseif", "else" or ":endif" is found
7828 scope->se_u.se_if.is_if_label = instr->ga_len;
7829 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7830 }
7831 else
7832 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007833
Bram Moolenaarced68a02021-01-24 17:53:47 +01007834#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007835 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007836 && skip_save != SKIP_YES)
7837 {
7838 // generated a profile start, need to generate a profile end, since it
7839 // won't be done after returning
7840 cctx->ctx_skip = SKIP_NOT;
7841 generate_instr(cctx, ISN_PROF_END);
7842 cctx->ctx_skip = SKIP_YES;
7843 }
7844#endif
7845
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007846 return p;
7847}
7848
7849 static char_u *
7850compile_elseif(char_u *arg, cctx_T *cctx)
7851{
7852 char_u *p = arg;
7853 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar90770b72021-11-30 20:57:38 +00007854 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007855 isn_T *isn;
7856 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007857 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007858 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007859
7860 if (scope == NULL || scope->se_type != IF_SCOPE)
7861 {
7862 emsg(_(e_elseif_without_if));
7863 return NULL;
7864 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007865 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007866 if (!cctx->ctx_had_return)
7867 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868
Bram Moolenaarced68a02021-01-24 17:53:47 +01007869 if (cctx->ctx_skip == SKIP_NOT)
7870 {
7871 // previous block was executed, this one and following will not
7872 cctx->ctx_skip = SKIP_YES;
7873 scope->se_u.se_if.is_seen_skip_not = TRUE;
7874 }
7875 if (scope->se_u.se_if.is_seen_skip_not)
7876 {
7877 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007878 // Do not count the "elseif" for profiling and cmdmod
7879 instr->ga_len = current_instr_idx(cctx);
7880
Bram Moolenaarced68a02021-01-24 17:53:47 +01007881 skip_expr_cctx(&p, cctx);
7882 return p;
7883 }
7884
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007885 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007886 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007887 int moved_cmdmod = FALSE;
7888 int saved_debug = FALSE;
7889 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007890
7891 // Move any CMDMOD instruction to after the jump
7892 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7893 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007894 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007895 return NULL;
7896 ((isn_T *)instr->ga_data)[instr->ga_len] =
7897 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7898 --instr->ga_len;
7899 moved_cmdmod = TRUE;
7900 }
7901
Bram Moolenaar093165c2021-08-22 13:35:31 +02007902 // Remove the already generated ISN_DEBUG, it is written below the
7903 // ISN_FOR instruction.
7904 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7905 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7906 .isn_type == ISN_DEBUG)
7907 {
7908 --instr->ga_len;
7909 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7910 saved_debug = TRUE;
7911 }
7912
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007913 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007914 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007915 return NULL;
7916 // previous "if" or "elseif" jumps here
7917 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7918 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007919
Bram Moolenaara91a7132021-03-25 21:12:15 +01007920 if (moved_cmdmod)
7921 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007922
7923 if (saved_debug)
7924 {
7925 // move the debug instruction here
7926 if (GA_GROW_FAILS(instr, 1))
7927 return NULL;
7928 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7929 ++instr->ga_len;
7930 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007933 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007934 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007935 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007936 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007937 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007938#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007939 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007940 // the previous block was skipped, need to profile this line
7941 generate_instr(cctx, ISN_PROF_START);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007942#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007943 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007944 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007945 generate_instr_debug(cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007946 }
Bram Moolenaar90770b72021-11-30 20:57:38 +00007947
7948 instr_count = instr->ga_len;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007949 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007950 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007951 clear_ppconst(&ppconst);
7952 return NULL;
7953 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007954 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007955 if (!ends_excmd2(arg, skipwhite(p)))
7956 {
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007957 clear_ppconst(&ppconst);
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007958 semsg(_(e_trailing_arg), p);
7959 return NULL;
7960 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007961 if (scope->se_skip_save == SKIP_YES)
7962 clear_ppconst(&ppconst);
7963 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007964 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007965 int error = FALSE;
7966 int v;
7967
Bram Moolenaarfad27422021-11-30 21:58:19 +00007968 // The expression result is a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007969 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7970 if (error)
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007971 {
7972 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007973 return NULL;
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007974 }
Bram Moolenaar13106602020-10-04 16:06:05 +02007975 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007976 clear_ppconst(&ppconst);
7977 scope->se_u.se_if.is_if_label = -1;
7978 }
7979 else
7980 {
7981 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007982 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007983 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007984 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007985 if (bool_on_stack(cctx) == FAIL)
7986 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007987
Bram Moolenaara91a7132021-03-25 21:12:15 +01007988 // CMDMOD_REV must come before the jump
7989 generate_undo_cmdmods(cctx);
7990
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007991 // "where" is set when ":elseif", "else" or ":endif" is found
7992 scope->se_u.se_if.is_if_label = instr->ga_len;
7993 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007995
7996 return p;
7997}
7998
7999 static char_u *
8000compile_else(char_u *arg, cctx_T *cctx)
8001{
8002 char_u *p = arg;
8003 garray_T *instr = &cctx->ctx_instr;
8004 isn_T *isn;
8005 scope_T *scope = cctx->ctx_scope;
8006
8007 if (scope == NULL || scope->se_type != IF_SCOPE)
8008 {
8009 emsg(_(e_else_without_if));
8010 return NULL;
8011 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01008012 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008013 if (!cctx->ctx_had_return)
8014 scope->se_u.se_if.is_had_return = FALSE;
8015 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008016
Bram Moolenaarced68a02021-01-24 17:53:47 +01008017#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008018 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01008019 {
8020 if (cctx->ctx_skip == SKIP_NOT
8021 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8022 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02008023 // the previous block was executed, do not count "else" for
8024 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01008025 --instr->ga_len;
8026 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
8027 {
8028 // the previous block was not executed, this one will, do count the
8029 // "else" for profiling
8030 cctx->ctx_skip = SKIP_NOT;
8031 generate_instr(cctx, ISN_PROF_END);
8032 generate_instr(cctx, ISN_PROF_START);
8033 cctx->ctx_skip = SKIP_YES;
8034 }
8035 }
8036#endif
8037
8038 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008039 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008040 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008041 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008042 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008043 if (!cctx->ctx_had_return
8044 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
8045 JUMP_ALWAYS, cctx) == FAIL)
8046 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008047 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008048
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008049 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008050 {
8051 if (scope->se_u.se_if.is_if_label >= 0)
8052 {
8053 // previous "if" or "elseif" jumps here
8054 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8055 isn->isn_arg.jump.jump_where = instr->ga_len;
8056 scope->se_u.se_if.is_if_label = -1;
8057 }
8058 }
8059
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008060 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008061 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
8062 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008063
8064 return p;
8065}
8066
8067 static char_u *
8068compile_endif(char_u *arg, cctx_T *cctx)
8069{
8070 scope_T *scope = cctx->ctx_scope;
8071 ifscope_T *ifscope;
8072 garray_T *instr = &cctx->ctx_instr;
8073 isn_T *isn;
8074
Bram Moolenaarfa984412021-03-25 22:15:28 +01008075 if (misplaced_cmdmod(cctx))
8076 return NULL;
8077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008078 if (scope == NULL || scope->se_type != IF_SCOPE)
8079 {
8080 emsg(_(e_endif_without_if));
8081 return NULL;
8082 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008083 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008084 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008085 if (!cctx->ctx_had_return)
8086 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008087
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008088 if (scope->se_u.se_if.is_if_label >= 0)
8089 {
8090 // previous "if" or "elseif" jumps here
8091 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8092 isn->isn_arg.jump.jump_where = instr->ga_len;
8093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008094 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008095 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01008096
8097#ifdef FEAT_PROFILE
8098 // even when skipping we count the endif as executed, unless the block it's
8099 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02008100 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01008101 && scope->se_skip_save != SKIP_YES)
8102 {
8103 cctx->ctx_skip = SKIP_NOT;
8104 generate_instr(cctx, ISN_PROF_START);
8105 }
8106#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02008107 cctx->ctx_skip = scope->se_skip_save;
8108
8109 // If all the blocks end in :return and there is an :else then the
8110 // had_return flag is set.
8111 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008112
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008113 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008114 return arg;
8115}
8116
8117/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01008118 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008119 *
8120 * Produces instructions:
8121 * PUSHNR -1
8122 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01008123 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008124 * top: FOR loop-idx, end Increment index, use list on bottom of stack
8125 * - if beyond end, jump to "end"
8126 * - otherwise get item from list and push it
8127 * STORE var Store item in "var"
8128 * ... body ...
8129 * JUMP top Jump back to repeat
8130 * end: DROP Drop the result of "expr"
8131 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008132 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
8133 * UNPACK 2 Split item in 2
8134 * STORE var1 Store item in "var1"
8135 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008136 */
8137 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008138compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008139{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008140 char_u *arg;
8141 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008142 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008143 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008144 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008145 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008146 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008147 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008148 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008149 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008150 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008151 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008152 lvar_T *loop_lvar; // loop iteration variable
8153 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008154 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008155 type_T *item_type = &t_any;
8156 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008157 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008158
Bram Moolenaar792f7862020-11-23 08:31:18 +01008159 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008160 if (p == NULL)
8161 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008162 if (var_count == 0)
8163 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008164 else
8165 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166
8167 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008168 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008169 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8170 return NULL;
8171 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008172 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008173 if (*p == ':' && wp != p)
8174 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8175 else
8176 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177 return NULL;
8178 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008179 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008180 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8181 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008182
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008183 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8184 // instruction.
8185 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8186 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8187 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008188 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008189 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008190 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8191 .isn_arg.debug.dbg_break_lnum;
8192 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008194 scope = new_scope(cctx, FOR_SCOPE);
8195 if (scope == NULL)
8196 return NULL;
8197
Bram Moolenaar792f7862020-11-23 08:31:18 +01008198 // Reserve a variable to store the loop iteration counter and initialize it
8199 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008200 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8201 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008202 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008203 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008204 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008205 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008206 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008207 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008208
8209 // compile "expr", it remains on the stack until "endfor"
8210 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008211 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008212 {
8213 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008214 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008215 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008216 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008217
rbtnnbebf0692021-08-21 17:26:50 +02008218 if (cctx->ctx_skip != SKIP_YES)
8219 {
8220 // If we know the type of "var" and it is a not a supported type we can
8221 // give an error now.
8222 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8223 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008224 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008225 {
rbtnnbebf0692021-08-21 17:26:50 +02008226 semsg(_(e_for_loop_on_str_not_supported),
8227 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008228 drop_scope(cctx);
8229 return NULL;
8230 }
rbtnnbebf0692021-08-21 17:26:50 +02008231
8232 if (vartype->tt_type == VAR_STRING)
8233 item_type = &t_string;
8234 else if (vartype->tt_type == VAR_BLOB)
8235 item_type = &t_number;
8236 else if (vartype->tt_type == VAR_LIST
8237 && vartype->tt_member->tt_type != VAR_ANY)
8238 {
8239 if (!var_list)
8240 item_type = vartype->tt_member;
8241 else if (vartype->tt_member->tt_type == VAR_LIST
8242 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
rbtnnbebf0692021-08-21 17:26:50 +02008243 item_type = vartype->tt_member->tt_member;
8244 }
8245
8246 // CMDMOD_REV must come before the FOR instruction.
8247 generate_undo_cmdmods(cctx);
8248
8249 // "for_end" is set when ":endfor" is found
8250 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8251
8252 generate_FOR(cctx, loop_lvar->lv_idx);
8253
8254 arg = arg_start;
8255 if (var_list)
8256 {
8257 generate_UNPACK(cctx, var_count, semicolon);
8258 arg = skipwhite(arg + 1); // skip white after '['
8259
8260 // the list item is replaced by a number of items
8261 if (GA_GROW_FAILS(stack, var_count - 1))
8262 {
8263 drop_scope(cctx);
8264 return NULL;
8265 }
8266 --stack->ga_len;
8267 for (idx = 0; idx < var_count; ++idx)
8268 {
8269 ((type_T **)stack->ga_data)[stack->ga_len] =
8270 (semicolon && idx == 0) ? vartype : item_type;
8271 ++stack->ga_len;
8272 }
8273 }
8274
Bram Moolenaar792f7862020-11-23 08:31:18 +01008275 for (idx = 0; idx < var_count; ++idx)
8276 {
rbtnnbebf0692021-08-21 17:26:50 +02008277 assign_dest_T dest = dest_local;
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008278 int opt_flags = 0;
8279 int vimvaridx = -1;
rbtnnbebf0692021-08-21 17:26:50 +02008280 type_T *type = &t_any;
8281 type_T *lhs_type = &t_any;
8282 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008283
rbtnnbebf0692021-08-21 17:26:50 +02008284 p = skip_var_one(arg, FALSE);
8285 varlen = p - arg;
8286 name = vim_strnsave(arg, varlen);
8287 if (name == NULL)
8288 goto failed;
8289 if (*p == ':')
8290 {
8291 p = skipwhite(p + 1);
8292 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8293 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008294
Bram Moolenaarfad27422021-11-30 21:58:19 +00008295 // Script var is not supported.
rbtnnbebf0692021-08-21 17:26:50 +02008296 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008297 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008298 goto failed;
8299 if (dest != dest_local)
8300 {
8301 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008302 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008303 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008304 }
rbtnnbebf0692021-08-21 17:26:50 +02008305 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008306 {
rbtnnbebf0692021-08-21 17:26:50 +02008307 // Assigning to "_": drop the value.
8308 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8309 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008310 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008311 else
rbtnnbebf0692021-08-21 17:26:50 +02008312 {
Mike Williamscc9d7252021-11-23 12:35:57 +00008313 if (!valid_varname(arg, (int)varlen, FALSE))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008314 goto failed;
rbtnnbebf0692021-08-21 17:26:50 +02008315 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8316 {
8317 semsg(_(e_variable_already_declared), arg);
8318 goto failed;
8319 }
8320
8321 if (STRNCMP(name, "s:", 2) == 0)
8322 {
8323 semsg(_(e_cannot_declare_script_variable_in_function), name);
8324 goto failed;
8325 }
8326
8327 // Reserve a variable to store "var".
8328 where.wt_index = var_list ? idx + 1 : 0;
8329 where.wt_variable = TRUE;
8330 if (lhs_type == &t_any)
8331 lhs_type = item_type;
8332 else if (item_type != &t_unknown
8333 && (item_type == &t_any
8334 ? need_type(item_type, lhs_type,
8335 -1, 0, cctx, FALSE, FALSE)
8336 : check_type(lhs_type, item_type, TRUE, where))
8337 == FAIL)
8338 goto failed;
8339 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8340 if (var_lvar == NULL)
8341 // out of memory or used as an argument
8342 goto failed;
8343
8344 if (semicolon && idx == var_count - 1)
8345 var_lvar->lv_type = vartype;
8346 else
8347 var_lvar->lv_type = item_type;
8348 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8349 }
8350
8351 if (*p == ',' || *p == ';')
8352 ++p;
8353 arg = skipwhite(p);
8354 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008355 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008356
rbtnnbebf0692021-08-21 17:26:50 +02008357 if (cctx->ctx_compile_type == CT_DEBUG)
8358 {
8359 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008360
rbtnnbebf0692021-08-21 17:26:50 +02008361 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8362 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8363 cctx->ctx_prev_lnum = prev_lnum;
8364 generate_instr_debug(cctx);
8365 cctx->ctx_prev_lnum = save_prev_lnum;
8366 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008367 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008368
Bram Moolenaar792f7862020-11-23 08:31:18 +01008369 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008370
8371failed:
8372 vim_free(name);
8373 drop_scope(cctx);
8374 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008375}
8376
8377/*
8378 * compile "endfor"
8379 */
8380 static char_u *
8381compile_endfor(char_u *arg, cctx_T *cctx)
8382{
8383 garray_T *instr = &cctx->ctx_instr;
8384 scope_T *scope = cctx->ctx_scope;
8385 forscope_T *forscope;
8386 isn_T *isn;
8387
Bram Moolenaarfa984412021-03-25 22:15:28 +01008388 if (misplaced_cmdmod(cctx))
8389 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008390
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008391 if (scope == NULL || scope->se_type != FOR_SCOPE)
8392 {
8393 emsg(_(e_for));
8394 return NULL;
8395 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008396 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008397 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008398 if (cctx->ctx_skip != SKIP_YES)
8399 {
8400 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008401
rbtnnbebf0692021-08-21 17:26:50 +02008402 // At end of ":for" scope jump back to the FOR instruction.
8403 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008404
rbtnnbebf0692021-08-21 17:26:50 +02008405 // Fill in the "end" label in the FOR statement so it can jump here.
8406 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8407 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008408
rbtnnbebf0692021-08-21 17:26:50 +02008409 // Fill in the "end" label any BREAK statements
8410 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008411
rbtnnbebf0692021-08-21 17:26:50 +02008412 // Below the ":for" scope drop the "expr" list from the stack.
8413 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8414 return NULL;
8415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008416
8417 vim_free(scope);
8418
8419 return arg;
8420}
8421
8422/*
8423 * compile "while expr"
8424 *
8425 * Produces instructions:
8426 * top: EVAL expr Push result of "expr"
8427 * JUMP_IF_FALSE end jump if false
8428 * ... body ...
8429 * JUMP top Jump back to repeat
8430 * end:
8431 *
8432 */
8433 static char_u *
8434compile_while(char_u *arg, cctx_T *cctx)
8435{
8436 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008437 scope_T *scope;
8438
8439 scope = new_scope(cctx, WHILE_SCOPE);
8440 if (scope == NULL)
8441 return NULL;
8442
Bram Moolenaara91a7132021-03-25 21:12:15 +01008443 // "endwhile" jumps back here, one before when profiling or using cmdmods
8444 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008445
8446 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008447 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008448 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008449
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008450 if (!ends_excmd2(arg, skipwhite(p)))
8451 {
8452 semsg(_(e_trailing_arg), p);
8453 return NULL;
8454 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008455
rbtnnd895b1d2021-08-20 20:54:25 +02008456 if (cctx->ctx_skip != SKIP_YES)
8457 {
8458 if (bool_on_stack(cctx) == FAIL)
8459 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008460
rbtnnd895b1d2021-08-20 20:54:25 +02008461 // CMDMOD_REV must come before the jump
8462 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008463
rbtnnd895b1d2021-08-20 20:54:25 +02008464 // "while_end" is set when ":endwhile" is found
8465 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008466 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008467 return FAIL;
8468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008469
8470 return p;
8471}
8472
8473/*
8474 * compile "endwhile"
8475 */
8476 static char_u *
8477compile_endwhile(char_u *arg, cctx_T *cctx)
8478{
8479 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008480 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008481
Bram Moolenaarfa984412021-03-25 22:15:28 +01008482 if (misplaced_cmdmod(cctx))
8483 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008484 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8485 {
8486 emsg(_(e_while));
8487 return NULL;
8488 }
8489 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008490 if (cctx->ctx_skip != SKIP_YES)
8491 {
8492 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008493
Bram Moolenaarf002a412021-01-24 13:34:18 +01008494#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008495 // count the endwhile before jumping
8496 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008497#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008498
rbtnnd895b1d2021-08-20 20:54:25 +02008499 // At end of ":for" scope jump back to the FOR instruction.
8500 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008501
rbtnnd895b1d2021-08-20 20:54:25 +02008502 // Fill in the "end" label in the WHILE statement so it can jump here.
8503 // And in any jumps for ":break"
8504 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008505 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008506 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008507
8508 vim_free(scope);
8509
8510 return arg;
8511}
8512
8513/*
8514 * compile "continue"
8515 */
8516 static char_u *
8517compile_continue(char_u *arg, cctx_T *cctx)
8518{
8519 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008520 int try_scopes = 0;
8521 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008522
8523 for (;;)
8524 {
8525 if (scope == NULL)
8526 {
8527 emsg(_(e_continue));
8528 return NULL;
8529 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008530 if (scope->se_type == FOR_SCOPE)
8531 {
8532 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008533 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008534 }
8535 if (scope->se_type == WHILE_SCOPE)
8536 {
8537 loop_label = scope->se_u.se_while.ws_top_label;
8538 break;
8539 }
8540 if (scope->se_type == TRY_SCOPE)
8541 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008542 scope = scope->se_outer;
8543 }
8544
Bram Moolenaarc150c092021-02-13 15:02:46 +01008545 if (try_scopes > 0)
8546 // Inside one or more try/catch blocks we first need to jump to the
8547 // "finally" or "endtry" to cleanup.
8548 generate_TRYCONT(cctx, try_scopes, loop_label);
8549 else
8550 // Jump back to the FOR or WHILE instruction.
8551 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008553 return arg;
8554}
8555
8556/*
8557 * compile "break"
8558 */
8559 static char_u *
8560compile_break(char_u *arg, cctx_T *cctx)
8561{
8562 scope_T *scope = cctx->ctx_scope;
8563 endlabel_T **el;
8564
8565 for (;;)
8566 {
8567 if (scope == NULL)
8568 {
8569 emsg(_(e_break));
8570 return NULL;
8571 }
8572 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8573 break;
8574 scope = scope->se_outer;
8575 }
8576
8577 // Jump to the end of the FOR or WHILE loop.
8578 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008579 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008580 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008581 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008582 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8583 return FAIL;
8584
8585 return arg;
8586}
8587
8588/*
8589 * compile "{" start of block
8590 */
8591 static char_u *
8592compile_block(char_u *arg, cctx_T *cctx)
8593{
8594 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8595 return NULL;
8596 return skipwhite(arg + 1);
8597}
8598
8599/*
8600 * compile end of block: drop one scope
8601 */
8602 static void
8603compile_endblock(cctx_T *cctx)
8604{
8605 scope_T *scope = cctx->ctx_scope;
8606
8607 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008608 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008609 vim_free(scope);
8610}
8611
8612/*
8613 * compile "try"
8614 * Creates a new scope for the try-endtry, pointing to the first catch and
8615 * finally.
8616 * Creates another scope for the "try" block itself.
8617 * TRY instruction sets up exception handling at runtime.
8618 *
8619 * "try"
8620 * TRY -> catch1, -> finally push trystack entry
8621 * ... try block
8622 * "throw {exception}"
8623 * EVAL {exception}
8624 * THROW create exception
8625 * ... try block
8626 * " catch {expr}"
8627 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008628 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008629 * EVAL {expr}
8630 * MATCH
8631 * JUMP nomatch -> catch2
8632 * CATCH remove exception
8633 * ... catch block
8634 * " catch"
8635 * JUMP -> finally
8636 * catch2: CATCH remove exception
8637 * ... catch block
8638 * " finally"
8639 * finally:
8640 * ... finally block
8641 * " endtry"
8642 * ENDTRY pop trystack entry, may rethrow
8643 */
8644 static char_u *
8645compile_try(char_u *arg, cctx_T *cctx)
8646{
8647 garray_T *instr = &cctx->ctx_instr;
8648 scope_T *try_scope;
8649 scope_T *scope;
8650
Bram Moolenaarfa984412021-03-25 22:15:28 +01008651 if (misplaced_cmdmod(cctx))
8652 return NULL;
8653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008654 // scope that holds the jumps that go to catch/finally/endtry
8655 try_scope = new_scope(cctx, TRY_SCOPE);
8656 if (try_scope == NULL)
8657 return NULL;
8658
Bram Moolenaar69f70502021-01-01 16:10:46 +01008659 if (cctx->ctx_skip != SKIP_YES)
8660 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008661 isn_T *isn;
8662
8663 // "try_catch" is set when the first ":catch" is found or when no catch
8664 // is found and ":finally" is found.
8665 // "try_finally" is set when ":finally" is found
8666 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008667 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008668 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8669 return NULL;
8670 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8671 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008672 return NULL;
8673 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008674
8675 // scope for the try block itself
8676 scope = new_scope(cctx, BLOCK_SCOPE);
8677 if (scope == NULL)
8678 return NULL;
8679
8680 return arg;
8681}
8682
8683/*
8684 * compile "catch {expr}"
8685 */
8686 static char_u *
8687compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8688{
8689 scope_T *scope = cctx->ctx_scope;
8690 garray_T *instr = &cctx->ctx_instr;
8691 char_u *p;
8692 isn_T *isn;
8693
Bram Moolenaarfa984412021-03-25 22:15:28 +01008694 if (misplaced_cmdmod(cctx))
8695 return NULL;
8696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008697 // end block scope from :try or :catch
8698 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8699 compile_endblock(cctx);
8700 scope = cctx->ctx_scope;
8701
8702 // Error if not in a :try scope
8703 if (scope == NULL || scope->se_type != TRY_SCOPE)
8704 {
8705 emsg(_(e_catch));
8706 return NULL;
8707 }
8708
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008709 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008710 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008711 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008712 return NULL;
8713 }
8714
Bram Moolenaar69f70502021-01-01 16:10:46 +01008715 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008716 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008717#ifdef FEAT_PROFILE
8718 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008719 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008720 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008721 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008722 .isn_type == ISN_PROF_START)
8723 --instr->ga_len;
8724#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008725 // Jump from end of previous block to :finally or :endtry
8726 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8727 JUMP_ALWAYS, cctx) == FAIL)
8728 return NULL;
8729
8730 // End :try or :catch scope: set value in ISN_TRY instruction
8731 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008732 if (isn->isn_arg.try.try_ref->try_catch == 0)
8733 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008734 if (scope->se_u.se_try.ts_catch_label != 0)
8735 {
8736 // Previous catch without match jumps here
8737 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8738 isn->isn_arg.jump.jump_where = instr->ga_len;
8739 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008740#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008741 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008742 {
8743 // a "throw" that jumps here needs to be counted
8744 generate_instr(cctx, ISN_PROF_END);
8745 // the "catch" is also counted
8746 generate_instr(cctx, ISN_PROF_START);
8747 }
8748#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008749 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008750 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008751 }
8752
8753 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008754 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008755 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008756 scope->se_u.se_try.ts_caught_all = TRUE;
8757 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008758 }
8759 else
8760 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008761 char_u *end;
8762 char_u *pat;
8763 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008764 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008765 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008767 // Push v:exception, push {expr} and MATCH
8768 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8769
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008770 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008771 if (*end != *p)
8772 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008773 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008774 vim_free(tofree);
8775 return FAIL;
8776 }
8777 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008778 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008779 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008780 len = (int)(end - tofree);
8781 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008782 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008783 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008784 if (pat == NULL)
8785 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008786 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008787 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008789 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8790 return NULL;
8791
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008792 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008793 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8794 return NULL;
8795 }
8796
Bram Moolenaar69f70502021-01-01 16:10:46 +01008797 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008798 return NULL;
8799
8800 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8801 return NULL;
8802 return p;
8803}
8804
8805 static char_u *
8806compile_finally(char_u *arg, cctx_T *cctx)
8807{
8808 scope_T *scope = cctx->ctx_scope;
8809 garray_T *instr = &cctx->ctx_instr;
8810 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008811 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008812
Bram Moolenaarfa984412021-03-25 22:15:28 +01008813 if (misplaced_cmdmod(cctx))
8814 return NULL;
8815
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008816 // end block scope from :try or :catch
8817 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8818 compile_endblock(cctx);
8819 scope = cctx->ctx_scope;
8820
8821 // Error if not in a :try scope
8822 if (scope == NULL || scope->se_type != TRY_SCOPE)
8823 {
8824 emsg(_(e_finally));
8825 return NULL;
8826 }
8827
rbtnn84934992021-08-07 13:26:53 +02008828 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008829 {
rbtnn84934992021-08-07 13:26:53 +02008830 // End :catch or :finally scope: set value in ISN_TRY instruction
8831 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8832 if (isn->isn_arg.try.try_ref->try_finally != 0)
8833 {
8834 emsg(_(e_finally_dup));
8835 return NULL;
8836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008837
rbtnn84934992021-08-07 13:26:53 +02008838 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008839#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008840 if (cctx->ctx_compile_type == CT_PROFILE
8841 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008842 .isn_type == ISN_PROF_START)
rbtnn84934992021-08-07 13:26:53 +02008843 {
8844 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008845 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008846
8847 // jump to the profile end above it
8848 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008849 .isn_type == ISN_PROF_END)
rbtnn84934992021-08-07 13:26:53 +02008850 --this_instr;
8851 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008852#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008853
rbtnn84934992021-08-07 13:26:53 +02008854 // Fill in the "end" label in jumps at the end of the blocks.
8855 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarfad27422021-11-30 21:58:19 +00008856 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008857
rbtnn84934992021-08-07 13:26:53 +02008858 // If there is no :catch then an exception jumps to :finally.
8859 if (isn->isn_arg.try.try_ref->try_catch == 0)
8860 isn->isn_arg.try.try_ref->try_catch = this_instr;
8861 isn->isn_arg.try.try_ref->try_finally = this_instr;
8862 if (scope->se_u.se_try.ts_catch_label != 0)
8863 {
8864 // Previous catch without match jumps here
8865 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8866 isn->isn_arg.jump.jump_where = this_instr;
8867 scope->se_u.se_try.ts_catch_label = 0;
8868 }
8869 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8870 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008871 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008872
8873 return arg;
8874}
8875
8876 static char_u *
8877compile_endtry(char_u *arg, cctx_T *cctx)
8878{
8879 scope_T *scope = cctx->ctx_scope;
8880 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008881 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008882
Bram Moolenaarfa984412021-03-25 22:15:28 +01008883 if (misplaced_cmdmod(cctx))
8884 return NULL;
8885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008886 // end block scope from :catch or :finally
8887 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8888 compile_endblock(cctx);
8889 scope = cctx->ctx_scope;
8890
8891 // Error if not in a :try scope
8892 if (scope == NULL || scope->se_type != TRY_SCOPE)
8893 {
8894 if (scope == NULL)
8895 emsg(_(e_no_endtry));
8896 else if (scope->se_type == WHILE_SCOPE)
8897 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008898 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008899 emsg(_(e_endfor));
8900 else
8901 emsg(_(e_endif));
8902 return NULL;
8903 }
8904
Bram Moolenaarc150c092021-02-13 15:02:46 +01008905 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008906 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008907 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008908 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8909 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008910 {
8911 emsg(_(e_missing_catch_or_finally));
8912 return NULL;
8913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008914
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008915#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008916 if (cctx->ctx_compile_type == CT_PROFILE
8917 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008918 .isn_type == ISN_PROF_START)
8919 // move the profile start after "endtry" so that it's not counted when
8920 // the exception is rethrown.
8921 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008922#endif
8923
Bram Moolenaar69f70502021-01-01 16:10:46 +01008924 // Fill in the "end" label in jumps at the end of the blocks, if not
8925 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008926 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8927 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008928
Bram Moolenaar69f70502021-01-01 16:10:46 +01008929 if (scope->se_u.se_try.ts_catch_label != 0)
8930 {
8931 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008932 isn_T *isn = ((isn_T *)instr->ga_data)
8933 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008934 isn->isn_arg.jump.jump_where = instr->ga_len;
8935 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008936 }
8937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008938 compile_endblock(cctx);
8939
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008940 if (cctx->ctx_skip != SKIP_YES)
8941 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008942 // End :catch or :finally scope: set instruction index in ISN_TRY
8943 // instruction
8944 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008945 if (cctx->ctx_skip != SKIP_YES
8946 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8947 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008948#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008949 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008950 generate_instr(cctx, ISN_PROF_START);
8951#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008953 return arg;
8954}
8955
8956/*
8957 * compile "throw {expr}"
8958 */
8959 static char_u *
8960compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8961{
8962 char_u *p = skipwhite(arg);
8963
Bram Moolenaara5565e42020-05-09 15:44:01 +02008964 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008965 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008966 if (cctx->ctx_skip == SKIP_YES)
8967 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008968 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008969 return NULL;
8970 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8971 return NULL;
8972
8973 return p;
8974}
8975
Bram Moolenaarc3235272021-07-10 19:42:03 +02008976 static char_u *
8977compile_eval(char_u *arg, cctx_T *cctx)
8978{
8979 char_u *p = arg;
8980 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02008981 long lnum = SOURCING_LNUM;
8982
8983 // find_ex_command() will consider a variable name an expression, assuming
8984 // that something follows on the next line. Check that something actually
8985 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02008986 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02008987
Bram Moolenaarc3235272021-07-10 19:42:03 +02008988 if (compile_expr0(&p, cctx) == FAIL)
8989 return NULL;
8990
8991 if (name_only && lnum == SOURCING_LNUM)
8992 {
8993 semsg(_(e_expression_without_effect_str), arg);
8994 return NULL;
8995 }
8996
8997 // drop the result
8998 generate_instr_drop(cctx, ISN_DROP, 1);
8999
9000 return skipwhite(p);
9001}
9002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009003/*
9004 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009005 * compile "echomsg expr"
9006 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02009007 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01009008 * compile "execute expr"
9009 */
9010 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009011compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009012{
9013 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01009014 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009015 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009016 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009017 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009018 garray_T *stack = &cctx->ctx_type_stack;
9019 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009020
9021 for (;;)
9022 {
Bram Moolenaare4984292020-12-13 14:19:25 +01009023 if (ends_excmd2(prev, p))
9024 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009025 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009026 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009027 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009028
9029 if (cctx->ctx_skip != SKIP_YES)
9030 {
9031 // check for non-void type
9032 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
9033 if (type->tt_type == VAR_VOID)
9034 {
9035 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
9036 return NULL;
9037 }
9038 }
9039
Bram Moolenaarad39c092020-02-26 18:23:43 +01009040 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02009041 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009042 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009043 }
9044
Bram Moolenaare4984292020-12-13 14:19:25 +01009045 if (count > 0)
9046 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009047 long save_lnum = cctx->ctx_lnum;
9048
9049 // Use the line number where the command started.
9050 cctx->ctx_lnum = start_ctx_lnum;
9051
Bram Moolenaare4984292020-12-13 14:19:25 +01009052 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
9053 generate_ECHO(cctx, cmdidx == CMD_echo, count);
9054 else if (cmdidx == CMD_execute)
9055 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
9056 else if (cmdidx == CMD_echomsg)
9057 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02009058 else if (cmdidx == CMD_echoconsole)
9059 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01009060 else
9061 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009062
9063 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01009064 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009065 return p;
9066}
9067
9068/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01009069 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01009070 * instruction to compute it and return OK.
9071 * Otherwise return FAIL, the caller must deal with any range.
9072 */
9073 static int
9074compile_variable_range(exarg_T *eap, cctx_T *cctx)
9075{
9076 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
9077 char_u *p = skipdigits(eap->cmd);
9078
9079 if (p == range_end)
9080 return FAIL;
9081 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
9082}
9083
9084/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009085 * :put r
9086 * :put ={expr}
9087 */
9088 static char_u *
9089compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
9090{
9091 char_u *line = arg;
9092 linenr_T lnum;
9093 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009094 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009095
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009096 eap->regname = *line;
9097
9098 if (eap->regname == '=')
9099 {
9100 char_u *p = line + 1;
9101
9102 if (compile_expr0(&p, cctx) == FAIL)
9103 return NULL;
9104 line = p;
9105 }
9106 else if (eap->regname != NUL)
9107 ++line;
9108
Bram Moolenaar08597872020-12-10 19:43:40 +01009109 if (compile_variable_range(eap, cctx) == OK)
9110 {
9111 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
9112 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009113 else
Bram Moolenaar08597872020-12-10 19:43:40 +01009114 {
9115 // Either no range or a number.
9116 // "errormsg" will not be set because the range is ADDR_LINES.
9117 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01009118 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01009119 return NULL;
9120 if (eap->addr_count == 0)
9121 lnum = -1;
9122 else
9123 lnum = eap->line2;
9124 if (above)
9125 --lnum;
9126 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009127
9128 generate_PUT(cctx, eap->regname, lnum);
9129 return line;
9130}
9131
9132/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009133 * A command that is not compiled, execute with legacy code.
9134 */
9135 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009136compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009137{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009138 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009139 char_u *p;
9140 int has_expr = FALSE;
9141 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009142 char_u *tofree = NULL;
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009143 char_u *cmd_arg = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009144
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009145 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009146 goto theend;
9147
Bram Moolenaare729ce22021-06-06 21:38:09 +02009148 // If there was a prececing command modifier, drop it and include it in the
9149 // EXEC command.
9150 if (cctx->ctx_has_cmdmod)
9151 {
9152 garray_T *instr = &cctx->ctx_instr;
9153 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9154
9155 if (isn->isn_type == ISN_CMDMOD)
9156 {
9157 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9158 ->cmod_filter_regmatch.regprog);
9159 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9160 --instr->ga_len;
9161 cctx->ctx_has_cmdmod = FALSE;
9162 }
9163 }
9164
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009165 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009166 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009167 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009168 int usefilter = FALSE;
9169
9170 has_expr = argt & (EX_XFILE | EX_EXPAND);
9171
9172 // If the command can be followed by a bar, find the bar and truncate
9173 // it, so that the following command can be compiled.
9174 // The '|' is overwritten with a NUL, it is put back below.
9175 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9176 && *eap->arg == '!')
9177 // :w !filter or :r !filter or :r! filter
9178 usefilter = TRUE;
9179 if ((argt & EX_TRLBAR) && !usefilter)
9180 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009181 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009182 separate_nextcmd(eap);
9183 if (eap->nextcmd != NULL)
9184 nextcmd = eap->nextcmd;
9185 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009186 else if (eap->cmdidx == CMD_wincmd)
9187 {
9188 p = eap->arg;
9189 if (*p != NUL)
9190 ++p;
9191 if (*p == 'g' || *p == Ctrl_G)
9192 ++p;
9193 p = skipwhite(p);
9194 if (*p == '|')
9195 {
9196 *p = NUL;
9197 nextcmd = p + 1;
9198 }
9199 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009200 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9201 {
9202 // If there is a trailing '{' read lines until the '}'
9203 p = eap->arg + STRLEN(eap->arg) - 1;
9204 while (p > eap->arg && VIM_ISWHITE(*p))
9205 --p;
9206 if (*p == '{')
9207 {
9208 exarg_T ea;
9209 int flags; // unused
9210 int start_lnum = SOURCING_LNUM;
9211
9212 CLEAR_FIELD(ea);
9213 ea.arg = eap->arg;
9214 fill_exarg_from_cctx(&ea, cctx);
9215 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9216 if (tofree != NULL)
9217 {
9218 *p = NUL;
9219 line = concat_str(line, tofree);
9220 if (line == NULL)
9221 goto theend;
9222 vim_free(tofree);
9223 tofree = line;
9224 SOURCING_LNUM = start_lnum;
9225 }
9226 }
9227 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009228 }
9229
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009230 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9231 {
9232 // expand filename in "syntax include [@group] filename"
9233 has_expr = TRUE;
9234 eap->arg = skipwhite(eap->arg + 7);
9235 if (*eap->arg == '@')
9236 eap->arg = skiptowhite(eap->arg);
9237 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009238
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009239 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9240 && STRLEN(eap->arg) > 4)
9241 {
9242 int delim = *eap->arg;
9243
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009244 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009245 if (*p == delim)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009246 cmd_arg = p + 1;
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009247 }
9248
Bram Moolenaarecac5912021-01-05 19:23:28 +01009249 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009250 cmd_arg = eap->arg;
9251
9252 if (cmd_arg != NULL)
Bram Moolenaarecac5912021-01-05 19:23:28 +01009253 {
Bram Moolenaarfad27422021-11-30 21:58:19 +00009254 exarg_T nea;
9255
9256 CLEAR_FIELD(nea);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009257 nea.cmd = cmd_arg;
Bram Moolenaarfad27422021-11-30 21:58:19 +00009258 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009259 if (nea.cmdidx < CMD_SIZE)
Bram Moolenaarfad27422021-11-30 21:58:19 +00009260 {
9261 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
9262 if (has_expr)
9263 eap->arg = skiptowhite(eap->arg);
9264 }
Bram Moolenaarecac5912021-01-05 19:23:28 +01009265 }
9266
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009267 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009268 {
9269 int count = 0;
9270 char_u *start = skipwhite(line);
9271
9272 // :cmd xxx`=expr1`yyy`=expr2`zzz
9273 // PUSHS ":cmd xxx"
9274 // eval expr1
9275 // PUSHS "yyy"
9276 // eval expr2
9277 // PUSHS "zzz"
9278 // EXECCONCAT 5
9279 for (;;)
9280 {
9281 if (p > start)
9282 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009283 char_u *val = vim_strnsave(start, p - start);
9284
9285 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009286 ++count;
9287 }
9288 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009289 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009290 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009291 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009292 ++count;
9293 p = skipwhite(p);
9294 if (*p != '`')
9295 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009296 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009297 return NULL;
9298 }
9299 start = p + 1;
9300
9301 p = (char_u *)strstr((char *)start, "`=");
9302 if (p == NULL)
9303 {
9304 if (*skipwhite(start) != NUL)
9305 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009306 char_u *val = vim_strsave(start);
9307
9308 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009309 ++count;
9310 }
9311 break;
9312 }
9313 }
9314 generate_EXECCONCAT(cctx, count);
9315 }
9316 else
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009317 generate_EXEC_copy(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009318
9319theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009320 if (*nextcmd != NUL)
9321 {
9322 // the parser expects a pointer to the bar, put it back
9323 --nextcmd;
9324 *nextcmd = '|';
9325 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009326 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009327
9328 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009329}
9330
Bram Moolenaar20677332021-06-06 17:02:53 +02009331/*
9332 * A script command with heredoc, e.g.
9333 * ruby << EOF
9334 * command
9335 * EOF
9336 * Has been turned into one long line with NL characters by
9337 * get_function_body():
9338 * ruby << EOF<NL> command<NL>EOF
9339 */
9340 static char_u *
9341compile_script(char_u *line, cctx_T *cctx)
9342{
9343 if (cctx->ctx_skip != SKIP_YES)
9344 {
9345 isn_T *isn;
9346
9347 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9348 return NULL;
9349 isn->isn_arg.string = vim_strsave(line);
9350 }
9351 return (char_u *)"";
9352}
9353
Bram Moolenaar8238f082021-04-20 21:10:48 +02009354
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009355/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009356 * :s/pat/repl/
9357 */
9358 static char_u *
9359compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9360{
9361 char_u *cmd = eap->arg;
9362 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9363
9364 if (expr != NULL)
9365 {
9366 int delimiter = *cmd++;
9367
9368 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009369 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009370 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9371 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009372 garray_T save_ga = cctx->ctx_instr;
9373 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009374 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009375 int trailing_error;
9376 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009377 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009378 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009379
9380 cmd += 3;
9381 end = skip_substitute(cmd, delimiter);
9382
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009383 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009384 // labels are correct.
9385 cctx->ctx_instr.ga_len = 0;
9386 cctx->ctx_instr.ga_maxlen = 0;
9387 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009388 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009389 if (end[-1] == NUL)
9390 end[-1] = delimiter;
9391 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009392 trailing_error = *cmd != delimiter && *cmd != NUL;
9393
Bram Moolenaar169502f2021-04-21 12:19:35 +02009394 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009395 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009396 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009397 if (trailing_error)
9398 semsg(_(e_trailing_arg), cmd);
9399 clear_instr_ga(&cctx->ctx_instr);
9400 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009401 return NULL;
9402 }
9403
Bram Moolenaar8238f082021-04-20 21:10:48 +02009404 // Move the generated instructions into the ISN_SUBSTITUTE
9405 // instructions, then restore the list of instructions before
9406 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009407 instr_count = cctx->ctx_instr.ga_len;
9408 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009409 instr[instr_count].isn_type = ISN_FINISH;
9410
9411 cctx->ctx_instr = save_ga;
9412 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9413 {
9414 int idx;
9415
9416 for (idx = 0; idx < instr_count; ++idx)
9417 delete_instr(instr + idx);
9418 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009419 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009420 }
9421 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9422 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009423
9424 // skip over flags
9425 if (*end == '&')
9426 ++end;
9427 while (ASCII_ISALPHA(*end) || *end == '#')
9428 ++end;
9429 return end;
9430 }
9431 }
9432
9433 return compile_exec(arg, eap, cctx);
9434}
9435
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009436 static char_u *
9437compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9438{
9439 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009440 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009441
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009442 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009443 {
9444 if (STRNCMP(arg, "END", 3) == 0)
9445 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009446 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009447 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009448 // First load the current variable value.
9449 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9450 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009451 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009452 }
9453
9454 // Gets the redirected text and put it on the stack, then store it
9455 // in the variable.
9456 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9457
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009458 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009459 generate_instr_drop(cctx, ISN_CONCAT, 1);
9460
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009461 if (lhs->lhs_has_index)
9462 {
9463 // Use the info in "lhs" to store the value at the index in the
9464 // list or dict.
9465 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9466 &t_string, cctx) == FAIL)
9467 return NULL;
9468 }
9469 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009470 return NULL;
9471
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009472 VIM_CLEAR(lhs->lhs_name);
9473 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009474 return arg + 3;
9475 }
9476 emsg(_(e_cannot_nest_redir));
9477 return NULL;
9478 }
9479
9480 if (arg[0] == '=' && arg[1] == '>')
9481 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009482 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009483
9484 // redirect to a variable is compiled
9485 arg += 2;
9486 if (*arg == '>')
9487 {
9488 ++arg;
9489 append = TRUE;
9490 }
9491 arg = skipwhite(arg);
9492
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009493 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009494 FALSE, FALSE, 1, cctx) == FAIL)
9495 return NULL;
9496 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009497 lhs->lhs_append = append;
9498 if (lhs->lhs_has_index)
9499 {
9500 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9501 if (lhs->lhs_whole == NULL)
9502 return NULL;
9503 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009504
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009505 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009506 }
9507
9508 // other redirects are handled like at script level
9509 return compile_exec(line, eap, cctx);
9510}
9511
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009512#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009513 static char_u *
9514compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9515{
9516 isn_T *isn;
9517 char_u *p;
9518
9519 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9520 if (isn == NULL)
9521 return NULL;
9522 isn->isn_arg.number = eap->cmdidx;
9523
9524 p = eap->arg;
9525 if (compile_expr0(&p, cctx) == FAIL)
9526 return NULL;
9527
9528 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9529 if (isn == NULL)
9530 return NULL;
9531 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9532 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9533 return NULL;
9534 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9535 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9536 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9537
9538 return p;
9539}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009540#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009541
Bram Moolenaar4c137212021-04-19 16:48:48 +02009542/*
Bram Moolenaar7b829262021-10-13 15:04:34 +01009543 * Check if the separator for a :global or :substitute command is OK.
9544 */
9545 int
9546check_global_and_subst(char_u *cmd, char_u *arg)
9547{
Bram Moolenaar7f320922021-10-13 15:28:28 +01009548 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
Bram Moolenaar7b829262021-10-13 15:04:34 +01009549 {
9550 semsg(_(e_separator_not_supported_str), arg);
9551 return FAIL;
9552 }
9553 if (VIM_ISWHITE(cmd[1]))
9554 {
9555 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
9556 return FAIL;
9557 }
9558 return OK;
9559}
9560
9561
9562/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009563 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009564 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009565 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009566 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009567add_def_function(ufunc_T *ufunc)
9568{
9569 dfunc_T *dfunc;
9570
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009571 if (def_functions.ga_len == 0)
9572 {
9573 // The first position is not used, so that a zero uf_dfunc_idx means it
9574 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009575 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009576 return FAIL;
9577 ++def_functions.ga_len;
9578 }
9579
Bram Moolenaar09689a02020-05-09 22:50:08 +02009580 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009581 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009582 return FAIL;
9583 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9584 CLEAR_POINTER(dfunc);
9585 dfunc->df_idx = def_functions.ga_len;
9586 ufunc->uf_dfunc_idx = dfunc->df_idx;
9587 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009588 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009589 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009590 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009591 ++def_functions.ga_len;
9592 return OK;
9593}
9594
9595/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009596 * After ex_function() has collected all the function lines: parse and compile
9597 * the lines into instructions.
9598 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009599 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9600 * the return statement (used for lambda). When uf_ret_type is already set
9601 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009602 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009603 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009604 * This can be used recursively through compile_lambda(), which may reallocate
9605 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009606 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009607 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009608 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009609compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009610 ufunc_T *ufunc,
9611 int check_return_type,
9612 compiletype_T compile_type,
9613 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009614{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009615 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009616 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009617 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009618 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009619 cctx_T cctx;
9620 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009621 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009622 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009623 int ret = FAIL;
9624 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009625 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009626 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009627 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009628 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009629#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009630 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009631#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009632 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009633
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009634 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009635 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009636 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009637 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009638 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9639 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009640 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009641
9642 switch (compile_type)
9643 {
9644 case CT_PROFILE:
9645#ifdef FEAT_PROFILE
9646 instr_dest = dfunc->df_instr_prof; break;
9647#endif
9648 case CT_NONE: instr_dest = dfunc->df_instr; break;
9649 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9650 }
9651 if (instr_dest != NULL)
9652 // Was compiled in this mode before: Free old instructions.
9653 delete_def_function_contents(dfunc, FALSE);
9654 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009655 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009656 else
9657 {
9658 if (add_def_function(ufunc) == FAIL)
9659 return FAIL;
9660 new_def_function = TRUE;
9661 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009662
Bram Moolenaar985116a2020-07-12 17:31:09 +02009663 ufunc->uf_def_status = UF_COMPILING;
9664
Bram Moolenaara80faa82020-04-12 19:37:17 +02009665 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009666
Bram Moolenaare99d4222021-06-13 14:01:26 +02009667 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009668 cctx.ctx_ufunc = ufunc;
9669 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009670 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009671 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9672 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9673 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9674 cctx.ctx_type_list = &ufunc->uf_type_list;
9675 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9676 instr = &cctx.ctx_instr;
9677
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009678 // Set the context to the function, it may be compiled when called from
9679 // another script. Set the script version to the most modern one.
9680 // The line number will be set in next_line_from_context().
9681 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009682 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9683
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009684 // Don't use the flag from ":legacy" here.
9685 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9686
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009687 // Make sure error messages are OK.
9688 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9689 if (do_estack_push)
9690 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009691 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009692
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009693 if (ufunc->uf_def_args.ga_len > 0)
9694 {
9695 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009696 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009697 int i;
9698 char_u *arg;
9699 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009700 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009701
9702 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009703 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009704 for (i = 0; i < count; ++i)
9705 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009706 garray_T *stack = &cctx.ctx_type_stack;
9707 type_T *val_type;
9708 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009709 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009710 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009711 int jump_instr_idx = instr->ga_len;
9712 isn_T *isn;
9713
9714 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9715 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9716 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009717
9718 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009719 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009720
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009721 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009722 r = compile_expr0(&arg, &cctx);
9723
Bram Moolenaar12bce952021-03-11 20:04:04 +01009724 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009725 goto erret;
9726
9727 // If no type specified use the type of the default value.
9728 // Otherwise check that the default value type matches the
9729 // specified type.
9730 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009731 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009732 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009733 {
9734 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009735 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009736 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009737 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009738 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009739 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009740
9741 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009742 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009743
9744 // set instruction index in JUMP_IF_ARG_SET to here
9745 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9746 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009747 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009748
9749 if (did_set_arg_type)
9750 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009751 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009752 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009753
9754 /*
9755 * Loop over all the lines of the function and generate instructions.
9756 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009757 for (;;)
9758 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009759 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009760 int starts_with_colon = FALSE;
9761 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009762 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009763
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009764 // Bail out on the first error to avoid a flood of errors and report
9765 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009766 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009767 goto erret;
9768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009769 if (line != NULL && *line == '|')
9770 // the line continues after a '|'
9771 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009772 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009773 && !(*line == '#' && (line == cctx.ctx_line_start
9774 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009775 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009776 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009777 goto erret;
9778 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009779 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9780 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009781 else
9782 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009783 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009784 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009785 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009786 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009787#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009788 if (cctx.ctx_skip != SKIP_YES)
9789 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009790#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009791 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009792 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009793 // Make a copy, splitting off nextcmd and removing trailing spaces
9794 // may change it.
9795 if (line != NULL)
9796 {
9797 line = vim_strsave(line);
9798 vim_free(line_to_free);
9799 line_to_free = line;
9800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009801 }
9802
Bram Moolenaara80faa82020-04-12 19:37:17 +02009803 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009804 ea.cmdlinep = &line;
9805 ea.cmd = skipwhite(line);
9806
Bram Moolenaarb2049902021-01-24 12:53:53 +01009807 if (*ea.cmd == '#')
9808 {
9809 // "#" starts a comment
9810 line = (char_u *)"";
9811 continue;
9812 }
9813
Bram Moolenaarf002a412021-01-24 13:34:18 +01009814#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009815 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9816 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009817 {
9818 may_generate_prof_end(&cctx, prof_lnum);
9819
9820 prof_lnum = cctx.ctx_lnum;
9821 generate_instr(&cctx, ISN_PROF_START);
9822 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009823#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009824 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9825 && cctx.ctx_skip != SKIP_YES)
9826 {
9827 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009828 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009829 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009830 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009831
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009832 // Some things can be recognized by the first character.
9833 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009834 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009835 case '}':
9836 {
9837 // "}" ends a block scope
9838 scopetype_T stype = cctx.ctx_scope == NULL
9839 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009840
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009841 if (stype == BLOCK_SCOPE)
9842 {
9843 compile_endblock(&cctx);
9844 line = ea.cmd;
9845 }
9846 else
9847 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009848 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009849 goto erret;
9850 }
9851 if (line != NULL)
9852 line = skipwhite(ea.cmd + 1);
9853 continue;
9854 }
9855
9856 case '{':
9857 // "{" starts a block scope
9858 // "{'a': 1}->func() is something else
9859 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9860 {
9861 line = compile_block(ea.cmd, &cctx);
9862 continue;
9863 }
9864 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009865 }
9866
9867 /*
9868 * COMMAND MODIFIERS
9869 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009870 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009871 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9872 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009873 {
9874 if (errormsg != NULL)
9875 goto erret;
9876 // empty line or comment
9877 line = (char_u *)"";
9878 continue;
9879 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009880 generate_cmdmods(&cctx, &local_cmdmod);
9881 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009882
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009883 // Check if there was a colon after the last command modifier or before
9884 // the current position.
9885 for (p = ea.cmd; p >= line; --p)
9886 {
9887 if (*p == ':')
9888 starts_with_colon = TRUE;
9889 if (p < ea.cmd && !VIM_ISWHITE(*p))
9890 break;
9891 }
9892
Bram Moolenaarce024c32021-06-26 13:00:49 +02009893 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009894 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009895 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009896 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009897 if (checkforcmd(&ea.cmd, "call", 3))
9898 {
9899 if (*ea.cmd == '(')
9900 // not for "call()"
9901 ea.cmd = p;
9902 else
9903 ea.cmd = skipwhite(ea.cmd);
9904 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009905
Bram Moolenaarce024c32021-06-26 13:00:49 +02009906 if (!starts_with_colon)
9907 {
9908 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009909
Bram Moolenaarce024c32021-06-26 13:00:49 +02009910 // Check for assignment after command modifiers.
9911 assign = may_compile_assignment(&ea, &line, &cctx);
9912 if (assign == OK)
9913 goto nextline;
9914 if (assign == FAIL)
9915 goto erret;
9916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009917 }
9918
9919 /*
9920 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009921 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009922 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009923 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009924 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009925 cmd = ea.cmd;
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009926 if ((*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009927 && (starts_with_colon || !(*cmd == '\''
9928 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009929 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009930 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009931 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009932 {
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009933 if (!starts_with_colon
9934 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009935 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009936 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009937 goto erret;
9938 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009939 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009940 if (ends_excmd2(line, ea.cmd))
9941 {
9942 // A range without a command: jump to the line.
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009943 generate_EXEC(&cctx, ISN_EXECRANGE,
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009944 vim_strnsave(cmd, ea.cmd - cmd));
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009945 line = ea.cmd;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009946 goto nextline;
9947 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009948 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009949 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009950 p = find_ex_command(&ea, NULL,
9951 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009952 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009953
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009954 if (p == NULL)
9955 {
9956 if (cctx.ctx_skip != SKIP_YES)
9957 emsg(_(e_ambiguous_use_of_user_defined_command));
9958 goto erret;
9959 }
9960
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009961 // When using ":legacy cmd" always use compile_exec().
9962 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009963 {
9964 char_u *start = ea.cmd;
9965
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009966 switch (ea.cmdidx)
9967 {
9968 case CMD_if:
9969 case CMD_elseif:
9970 case CMD_else:
9971 case CMD_endif:
9972 case CMD_for:
9973 case CMD_endfor:
9974 case CMD_continue:
9975 case CMD_break:
9976 case CMD_while:
9977 case CMD_endwhile:
9978 case CMD_try:
9979 case CMD_catch:
9980 case CMD_finally:
9981 case CMD_endtry:
9982 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9983 goto erret;
9984 default: break;
9985 }
9986
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009987 // ":legacy return expr" needs to be handled differently.
9988 if (checkforcmd(&start, "return", 4))
9989 ea.cmdidx = CMD_return;
9990 else
9991 ea.cmdidx = CMD_legacy;
9992 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009994 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9995 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009996 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009997 {
9998 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009999 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010000 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +020010001 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010002 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +010010003 // CMD_var cannot happen, compile_assignment() above would be
10004 // used. Most likely an assignment to a non-existing variable.
10005 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010006 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010008 }
10009
Bram Moolenaar3988f642020-08-27 22:43:03 +020010010 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010011 && ea.cmdidx != CMD_elseif
10012 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +020010013 && ea.cmdidx != CMD_endif
10014 && ea.cmdidx != CMD_endfor
10015 && ea.cmdidx != CMD_endwhile
10016 && ea.cmdidx != CMD_catch
10017 && ea.cmdidx != CMD_finally
10018 && ea.cmdidx != CMD_endtry)
10019 {
Bram Moolenaar3988f642020-08-27 22:43:03 +020010020 emsg(_(e_unreachable_code_after_return));
10021 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +020010022 }
10023
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010024 p = skipwhite(p);
10025 if (ea.cmdidx != CMD_SIZE
10026 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
10027 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +020010028 if (ea.cmdidx >= 0)
10029 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010030 if ((ea.argt & EX_BANG) && *p == '!')
10031 {
10032 ea.forceit = TRUE;
10033 p = skipwhite(p + 1);
10034 }
10035 }
10036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010037 switch (ea.cmdidx)
10038 {
10039 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +000010040 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +020010041 ea.arg = p;
10042 line = compile_nested_function(&ea, &cctx);
10043 break;
10044
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010045 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010046 line = compile_return(p, check_return_type,
10047 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010048 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010049 break;
10050
10051 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +020010052 emsg(_(e_cannot_use_let_in_vim9_script));
10053 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +020010054 case CMD_var:
10055 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010056 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +020010057 case CMD_increment:
10058 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010059 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +020010060 if (line == p)
10061 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010062 break;
10063
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010064 case CMD_unlet:
10065 case CMD_unlockvar:
10066 case CMD_lockvar:
10067 line = compile_unletlock(p, &ea, &cctx);
10068 break;
10069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010070 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +020010071 emsg(_(e_import_can_only_be_used_in_script));
10072 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010073 break;
10074
10075 case CMD_if:
10076 line = compile_if(p, &cctx);
10077 break;
10078 case CMD_elseif:
10079 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010080 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010081 break;
10082 case CMD_else:
10083 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010084 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010085 break;
10086 case CMD_endif:
10087 line = compile_endif(p, &cctx);
10088 break;
10089
10090 case CMD_while:
10091 line = compile_while(p, &cctx);
10092 break;
10093 case CMD_endwhile:
10094 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010095 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010096 break;
10097
10098 case CMD_for:
10099 line = compile_for(p, &cctx);
10100 break;
10101 case CMD_endfor:
10102 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010103 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010104 break;
10105 case CMD_continue:
10106 line = compile_continue(p, &cctx);
10107 break;
10108 case CMD_break:
10109 line = compile_break(p, &cctx);
10110 break;
10111
10112 case CMD_try:
10113 line = compile_try(p, &cctx);
10114 break;
10115 case CMD_catch:
10116 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010117 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010118 break;
10119 case CMD_finally:
10120 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010121 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010122 break;
10123 case CMD_endtry:
10124 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010125 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010126 break;
10127 case CMD_throw:
10128 line = compile_throw(p, &cctx);
10129 break;
10130
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010131 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +020010132 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010133 break;
10134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010135 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010136 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +010010137 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010138 case CMD_echomsg:
10139 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010140 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010141 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +010010142 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010143
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010144 case CMD_put:
10145 ea.cmd = cmd;
10146 line = compile_put(p, &ea, &cctx);
10147 break;
10148
Bram Moolenaar4c137212021-04-19 16:48:48 +020010149 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +010010150 if (check_global_and_subst(ea.cmd, p) == FAIL)
10151 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +020010152 if (cctx.ctx_skip == SKIP_YES)
10153 line = (char_u *)"";
10154 else
10155 {
10156 ea.arg = p;
10157 line = compile_substitute(line, &ea, &cctx);
10158 }
10159 break;
10160
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010161 case CMD_redir:
10162 ea.arg = p;
10163 line = compile_redir(line, &ea, &cctx);
10164 break;
10165
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010166 case CMD_cexpr:
10167 case CMD_lexpr:
10168 case CMD_caddexpr:
10169 case CMD_laddexpr:
10170 case CMD_cgetexpr:
10171 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010172#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010173 ea.arg = p;
10174 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010175#else
10176 ex_ni(&ea);
10177 line = NULL;
10178#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010179 break;
10180
Bram Moolenaarae616492020-07-28 20:07:27 +020010181 case CMD_append:
10182 case CMD_change:
10183 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010184 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010185 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010186 case CMD_xit:
10187 not_in_vim9(&ea);
10188 goto erret;
10189
Bram Moolenaar002262f2020-07-08 17:47:57 +020010190 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010191 if (cctx.ctx_skip != SKIP_YES)
10192 {
10193 semsg(_(e_invalid_command_str), ea.cmd);
10194 goto erret;
10195 }
10196 // We don't check for a next command here.
10197 line = (char_u *)"";
10198 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010199
Bram Moolenaar20677332021-06-06 17:02:53 +020010200 case CMD_lua:
10201 case CMD_mzscheme:
10202 case CMD_perl:
10203 case CMD_py3:
10204 case CMD_python3:
10205 case CMD_python:
10206 case CMD_pythonx:
10207 case CMD_ruby:
10208 case CMD_tcl:
10209 ea.arg = p;
10210 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010211 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010212 else
10213 // heredoc lines have been concatenated with NL
10214 // characters in get_function_body()
10215 line = compile_script(line, &cctx);
10216 break;
10217
Bram Moolenaar7b829262021-10-13 15:04:34 +010010218 case CMD_global:
10219 if (check_global_and_subst(ea.cmd, p) == FAIL)
10220 goto erret;
10221 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +020010222 default:
10223 // Not recognized, execute with do_cmdline_cmd().
10224 ea.arg = p;
10225 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010226 break;
10227 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010228nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010229 if (line == NULL)
10230 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010231 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010232
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010233 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010234 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010236 if (cctx.ctx_type_stack.ga_len < 0)
10237 {
10238 iemsg("Type stack underflow");
10239 goto erret;
10240 }
10241 }
10242
10243 if (cctx.ctx_scope != NULL)
10244 {
10245 if (cctx.ctx_scope->se_type == IF_SCOPE)
10246 emsg(_(e_endif));
10247 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10248 emsg(_(e_endwhile));
10249 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10250 emsg(_(e_endfor));
10251 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010252 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010253 goto erret;
10254 }
10255
Bram Moolenaarefd88552020-06-18 20:50:10 +020010256 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010257 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010258 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10259 ufunc->uf_ret_type = &t_void;
10260 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010261 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010262 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010263 goto erret;
10264 }
10265
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010266 // Return void if there is no return at the end.
10267 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010268 }
10269
Bram Moolenaar599410c2021-04-10 14:03:43 +020010270 // When compiled with ":silent!" and there was an error don't consider the
10271 // function compiled.
10272 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010273 {
10274 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10275 + ufunc->uf_dfunc_idx;
10276 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010277 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010278#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010279 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010280 {
10281 dfunc->df_instr_prof = instr->ga_data;
10282 dfunc->df_instr_prof_count = instr->ga_len;
10283 }
10284 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010285#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010286 if (cctx.ctx_compile_type == CT_DEBUG)
10287 {
10288 dfunc->df_instr_debug = instr->ga_data;
10289 dfunc->df_instr_debug_count = instr->ga_len;
10290 }
10291 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010292 {
10293 dfunc->df_instr = instr->ga_data;
10294 dfunc->df_instr_count = instr->ga_len;
10295 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010296 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010297 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010298 if (cctx.ctx_outer_used)
10299 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010300 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010301 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010302
10303 ret = OK;
10304
10305erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010306 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010307 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010308 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10309 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010310
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010311 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010312 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010313 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010314 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010315
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010316 // If using the last entry in the table and it was added above, we
10317 // might as well remove it.
10318 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010319 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010320 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010321 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010322 ufunc->uf_dfunc_idx = 0;
10323 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010324 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010325
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010326 while (cctx.ctx_scope != NULL)
10327 drop_scope(&cctx);
10328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010329 if (errormsg != NULL)
10330 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010331 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010332 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010333 }
10334
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010335 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10336 {
10337 if (ret == OK)
10338 {
10339 emsg(_(e_missing_redir_end));
10340 ret = FAIL;
10341 }
10342 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010343 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010344 }
10345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010346 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010347 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010348 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010349 if (do_estack_push)
10350 estack_pop();
10351
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010352 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010353 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010354 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010355 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010356 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010357}
10358
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010359 void
10360set_function_type(ufunc_T *ufunc)
10361{
10362 int varargs = ufunc->uf_va_name != NULL;
10363 int argcount = ufunc->uf_args.ga_len;
10364
10365 // Create a type for the function, with the return type and any
10366 // argument types.
10367 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10368 // The type is included in "tt_args".
10369 if (argcount > 0 || varargs)
10370 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010371 if (ufunc->uf_type_list.ga_itemsize == 0)
10372 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010373 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10374 argcount, &ufunc->uf_type_list);
10375 // Add argument types to the function type.
10376 if (func_type_add_arg_types(ufunc->uf_func_type,
10377 argcount + varargs,
10378 &ufunc->uf_type_list) == FAIL)
10379 return;
10380 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10381 ufunc->uf_func_type->tt_min_argcount =
10382 argcount - ufunc->uf_def_args.ga_len;
10383 if (ufunc->uf_arg_types == NULL)
10384 {
10385 int i;
10386
10387 // lambda does not have argument types.
10388 for (i = 0; i < argcount; ++i)
10389 ufunc->uf_func_type->tt_args[i] = &t_any;
10390 }
10391 else
10392 mch_memmove(ufunc->uf_func_type->tt_args,
10393 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10394 if (varargs)
10395 {
10396 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010397 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010398 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10399 }
10400 }
10401 else
10402 // No arguments, can use a predefined type.
10403 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10404 argcount, &ufunc->uf_type_list);
10405}
10406
10407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010408/*
10409 * Delete an instruction, free what it contains.
10410 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010411 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010412delete_instr(isn_T *isn)
10413{
10414 switch (isn->isn_type)
10415 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010416 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010417 case ISN_EXEC:
Bram Moolenaare4eed8c2021-12-01 15:22:56 +000010418 case ISN_EXECRANGE:
Bram Moolenaar20677332021-06-06 17:02:53 +020010419 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010420 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010421 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010422 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010423 case ISN_LOADENV:
10424 case ISN_LOADG:
10425 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010426 case ISN_LOADT:
10427 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010428 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010429 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010430 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010431 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010432 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010433 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010434 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010435 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010436 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010437 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010438 case ISN_STOREW:
10439 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010440 vim_free(isn->isn_arg.string);
10441 break;
10442
Bram Moolenaar4c137212021-04-19 16:48:48 +020010443 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010444 {
10445 int idx;
10446 isn_T *list = isn->isn_arg.subs.subs_instr;
10447
10448 vim_free(isn->isn_arg.subs.subs_cmd);
10449 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10450 delete_instr(list + idx);
10451 vim_free(list);
10452 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010453 break;
10454
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010455 case ISN_INSTR:
10456 {
10457 int idx;
10458 isn_T *list = isn->isn_arg.instr;
10459
10460 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10461 delete_instr(list + idx);
10462 vim_free(list);
10463 }
10464 break;
10465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010466 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010467 case ISN_STORES:
10468 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010469 break;
10470
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010471 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010472 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010473 vim_free(isn->isn_arg.unlet.ul_name);
10474 break;
10475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010476 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +000010477 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010478 vim_free(isn->isn_arg.storeopt.so_name);
10479 break;
10480
10481 case ISN_PUSHBLOB: // push blob isn_arg.blob
10482 blob_unref(isn->isn_arg.blob);
10483 break;
10484
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010485 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010486#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010487 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010488#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010489 break;
10490
10491 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010492#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010493 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010494#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010495 break;
10496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010497 case ISN_UCALL:
10498 vim_free(isn->isn_arg.ufunc.cuf_name);
10499 break;
10500
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010501 case ISN_FUNCREF:
10502 {
Bram Moolenaar38453522021-11-28 22:00:12 +000010503 if (isn->isn_arg.funcref.fr_func_name == NULL)
10504 {
10505 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10506 + isn->isn_arg.funcref.fr_dfunc_idx;
10507 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010508
Bram Moolenaar38453522021-11-28 22:00:12 +000010509 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10510 func_ptr_unref(ufunc);
10511 }
10512 else
10513 {
10514 char_u *name = isn->isn_arg.funcref.fr_func_name;
10515
10516 if (name != NULL)
10517 func_unref(name);
10518 vim_free(isn->isn_arg.funcref.fr_func_name);
10519 }
Bram Moolenaara05e5242020-09-19 18:19:19 +020010520 }
10521 break;
10522
10523 case ISN_DCALL:
10524 {
10525 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10526 + isn->isn_arg.dfunc.cdf_idx;
10527
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010528 if (dfunc->df_ufunc != NULL
10529 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010530 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010531 }
10532 break;
10533
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010534 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010535 {
10536 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10537 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10538
10539 if (ufunc != NULL)
10540 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010541 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010542 func_ptr_unref(ufunc);
10543 }
10544
10545 vim_free(lambda);
10546 vim_free(isn->isn_arg.newfunc.nf_global);
10547 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010548 break;
10549
Bram Moolenaar5e654232020-09-16 15:22:00 +020010550 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010551 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010552 free_type(isn->isn_arg.type.ct_type);
10553 break;
10554
Bram Moolenaar02194d22020-10-24 23:08:38 +020010555 case ISN_CMDMOD:
10556 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10557 ->cmod_filter_regmatch.regprog);
10558 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10559 break;
10560
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010561 case ISN_LOADSCRIPT:
10562 case ISN_STORESCRIPT:
10563 vim_free(isn->isn_arg.script.scriptref);
10564 break;
10565
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010566 case ISN_TRY:
10567 vim_free(isn->isn_arg.try.try_ref);
10568 break;
10569
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010570 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010571 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010572 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10573 break;
10574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010575 case ISN_2BOOL:
10576 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010577 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010578 case ISN_ADDBLOB:
10579 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010580 case ISN_ANYINDEX:
10581 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010582 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010583 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010584 case ISN_BLOBINDEX:
10585 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010586 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010587 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010588 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010589 case ISN_CHECKNR:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010590 case ISN_CLEARDICT:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010591 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010592 case ISN_COMPAREANY:
10593 case ISN_COMPAREBLOB:
10594 case ISN_COMPAREBOOL:
10595 case ISN_COMPAREDICT:
10596 case ISN_COMPAREFLOAT:
10597 case ISN_COMPAREFUNC:
10598 case ISN_COMPARELIST:
10599 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010600 case ISN_COMPARESPECIAL:
10601 case ISN_COMPARESTRING:
10602 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010603 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010604 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010605 case ISN_DROP:
10606 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010607 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010608 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010609 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010610 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010611 case ISN_EXECCONCAT:
10612 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010613 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010614 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010615 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010616 case ISN_GETITEM:
10617 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010618 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010619 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010620 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010621 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010622 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010623 case ISN_LOADBDICT:
10624 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010625 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010626 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010627 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010628 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010629 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010630 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010631 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010632 case ISN_NEGATENR:
10633 case ISN_NEWDICT:
10634 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010635 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010636 case ISN_OPFLOAT:
10637 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010638 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010639 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010640 case ISN_PROF_END:
10641 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010642 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010643 case ISN_PUSHF:
10644 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010645 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010646 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010647 case ISN_REDIREND:
10648 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010649 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010650 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010651 case ISN_SHUFFLE:
10652 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010653 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010654 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010655 case ISN_STORENR:
10656 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010657 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010658 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010659 case ISN_STOREV:
10660 case ISN_STRINDEX:
10661 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010662 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010663 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010664 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010665 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010666 case ISN_UNPACK:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010667 case ISN_USEDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010668 // nothing allocated
10669 break;
10670 }
10671}
10672
10673/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010674 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010675 */
10676 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010677delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010678{
10679 int idx;
10680
10681 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010682 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010683
10684 if (dfunc->df_instr != NULL)
10685 {
10686 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10687 delete_instr(dfunc->df_instr + idx);
10688 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010689 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010690 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010691 if (dfunc->df_instr_debug != NULL)
10692 {
10693 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10694 delete_instr(dfunc->df_instr_debug + idx);
10695 VIM_CLEAR(dfunc->df_instr_debug);
10696 dfunc->df_instr_debug = NULL;
10697 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010698#ifdef FEAT_PROFILE
10699 if (dfunc->df_instr_prof != NULL)
10700 {
10701 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10702 delete_instr(dfunc->df_instr_prof + idx);
10703 VIM_CLEAR(dfunc->df_instr_prof);
10704 dfunc->df_instr_prof = NULL;
10705 }
10706#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010707
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010708 if (mark_deleted)
10709 dfunc->df_deleted = TRUE;
10710 if (dfunc->df_ufunc != NULL)
10711 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010712}
10713
10714/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010715 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010716 * function, unless another user function still uses it.
10717 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010718 */
10719 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010720unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010721{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010722 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010723 {
10724 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10725 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010726
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010727 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010728 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010729 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010730 ufunc->uf_dfunc_idx = 0;
10731 if (dfunc->df_ufunc == ufunc)
10732 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010733 }
10734}
10735
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010736/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010737 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010738 */
10739 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010740link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010741{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010742 if (ufunc->uf_dfunc_idx > 0)
10743 {
10744 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10745 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010746
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010747 ++dfunc->df_refcount;
10748 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010749}
10750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010751#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010752/*
10753 * Free all functions defined with ":def".
10754 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010755 void
10756free_def_functions(void)
10757{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010758 int idx;
10759
10760 for (idx = 0; idx < def_functions.ga_len; ++idx)
10761 {
10762 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10763
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010764 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010765 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010766 }
10767
10768 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010769}
10770#endif
10771
10772
10773#endif // FEAT_EVAL